FIRIAMAnalyticsEventLoggerImplTests.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <GoogleUtilities/GULUserDefaults.h>
  17. #import <OCMock/OCMock.h>
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseInAppMessaging/Sources/Analytics/FIRIAMAnalyticsEventLoggerImpl.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Analytics/FIRIAMClearcutLogger.h"
  21. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  22. #import "Interop/Analytics/Public/FIRInteropEventNames.h"
  23. #import "Interop/Analytics/Public/FIRInteropParameterNames.h"
  24. @interface FIRIAMAnalyticsEventLoggerImplTests : XCTestCase
  25. @property(nonatomic) FIRIAMClearcutLogger *mockClearcutLogger;
  26. @property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
  27. @property(nonatomic) id mockFirebaseAnalytics;
  28. @property(nonatomic) GULUserDefaults *mockUserDefaults;
  29. @end
  30. static NSString *campaignID = @"campaign id";
  31. static NSString *campaignName = @"campaign name";
  32. typedef void (^FIRAUserPropertiesCallback)(NSDictionary *userProperties);
  33. typedef void (^FakeAnalyticsLogEventHandler)(NSString *origin,
  34. NSString *name,
  35. NSDictionary *parameters);
  36. typedef void (^FakeAnalyticsUserPropertyHandler)(NSString *origin, NSString *name, id value);
  37. typedef void (^LastNotificationCallback)(NSString *);
  38. typedef void (^FakeAnalyticsLastNotificationHandler)(NSString *origin, LastNotificationCallback);
  39. @interface FakeAnalytics : NSObject <FIRAnalyticsInterop>
  40. @property FakeAnalyticsLogEventHandler eventHandler;
  41. @property FakeAnalyticsLogEventHandler userPropertyHandler;
  42. @property FakeAnalyticsLastNotificationHandler lastNotificationHandler;
  43. - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler;
  44. - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler;
  45. @end
  46. @implementation FakeAnalytics
  47. - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler {
  48. self = [super init];
  49. if (self) {
  50. _eventHandler = eventHandler;
  51. }
  52. return self;
  53. }
  54. - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler {
  55. self = [super init];
  56. if (self) {
  57. _userPropertyHandler = userPropertyHandler;
  58. }
  59. return self;
  60. }
  61. - (void)logEventWithOrigin:(nonnull NSString *)origin
  62. name:(nonnull NSString *)name
  63. parameters:(nullable NSDictionary<NSString *, id> *)parameters {
  64. if (_eventHandler) {
  65. _eventHandler(origin, name, parameters);
  66. }
  67. }
  68. - (void)setUserPropertyWithOrigin:(nonnull NSString *)origin
  69. name:(nonnull NSString *)name
  70. value:(nonnull id)value {
  71. if (_userPropertyHandler) {
  72. _userPropertyHandler(origin, name, value);
  73. }
  74. }
  75. - (void)checkLastNotificationForOrigin:(nonnull NSString *)origin
  76. queue:(nonnull dispatch_queue_t)queue
  77. callback:(nonnull void (^)(NSString *_Nullable))
  78. currentLastNotificationProperty {
  79. if (_lastNotificationHandler) {
  80. _lastNotificationHandler(origin, currentLastNotificationProperty);
  81. }
  82. }
  83. // Stubs
  84. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  85. clearEventName:(nonnull NSString *)clearEventName
  86. clearEventParameters:(nonnull NSDictionary *)clearEventParameters {
  87. }
  88. - (NSInteger)maxUserProperties:(nonnull NSString *)origin {
  89. return -1;
  90. }
  91. - (void)registerAnalyticsListener:(nonnull id<FIRAnalyticsInteropListener>)listener
  92. withOrigin:(nonnull NSString *)origin {
  93. }
  94. - (void)unregisterAnalyticsListenerWithOrigin:(nonnull NSString *)origin {
  95. }
  96. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  97. forOrigin:(nonnull NSString *)origin
  98. clearEventName:(nonnull NSString *)clearEventName
  99. clearEventParameters:
  100. (nonnull NSDictionary<NSString *, NSString *> *)clearEventParameters {
  101. }
  102. - (nonnull NSArray<NSDictionary<NSString *, NSString *> *> *)
  103. conditionalUserProperties:(nonnull NSString *)origin
  104. propertyNamePrefix:(nonnull NSString *)propertyNamePrefix {
  105. return nil;
  106. }
  107. - (void)setConditionalUserProperty:(nonnull NSDictionary<NSString *, id> *)conditionalUserProperty {
  108. }
  109. - (void)getUserPropertiesWithCallback:(nonnull FIRAInteropUserPropertiesCallback)callback {
  110. }
  111. @end
  112. @implementation FIRIAMAnalyticsEventLoggerImplTests
  113. - (void)setUp {
  114. [super setUp];
  115. self.mockClearcutLogger = OCMClassMock(FIRIAMClearcutLogger.class);
  116. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  117. self.mockUserDefaults = OCMClassMock(GULUserDefaults.class);
  118. }
  119. - (void)tearDown {
  120. [super tearDown];
  121. }
  122. - (void)testLogImpressionEvent {
  123. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  124. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  125. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  126. XCTAssertEqualObjects(origin, @"fiam");
  127. XCTAssertEqualObjects(name, @"firebase_in_app_message_impression");
  128. XCTAssertEqual([parameters count], 3);
  129. XCTAssertNotNil(parameters);
  130. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  131. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  132. [expectation1 fulfill];
  133. }];
  134. FIRIAMAnalyticsEventLoggerImpl *logger =
  135. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  136. usingTimeFetcher:self.mockTimeFetcher
  137. usingUserDefaults:nil
  138. analytics:analytics];
  139. NSTimeInterval currentMoment = 10000;
  140. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  141. OCMExpect([self.mockClearcutLogger
  142. logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  143. forCampaignID:[OCMArg isEqual:campaignID]
  144. withCampaignName:[OCMArg isEqual:campaignName]
  145. eventTimeInMs:[OCMArg isNil]
  146. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  147. XCTestExpectation *expectation2 =
  148. [self expectationWithDescription:@"Completion Callback Triggered"];
  149. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  150. forCampaignID:campaignID
  151. withCampaignName:campaignName
  152. eventTimeInMs:nil
  153. completion:^(BOOL success) {
  154. [expectation2 fulfill];
  155. }];
  156. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  157. }
  158. - (void)testLogActionEvent {
  159. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  160. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  161. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  162. XCTAssertEqualObjects(origin, @"fiam");
  163. XCTAssertEqualObjects(name, @"firebase_in_app_message_action");
  164. XCTAssertEqual([parameters count], 3);
  165. XCTAssertNotNil(parameters);
  166. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  167. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  168. [expectation1 fulfill];
  169. }];
  170. FIRIAMAnalyticsEventLoggerImpl *logger =
  171. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  172. usingTimeFetcher:self.mockTimeFetcher
  173. usingUserDefaults:self.mockUserDefaults
  174. analytics:analytics];
  175. NSTimeInterval currentMoment = 10000;
  176. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  177. OCMExpect([self.mockClearcutLogger
  178. logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  179. forCampaignID:[OCMArg isEqual:campaignID]
  180. withCampaignName:[OCMArg isEqual:campaignName]
  181. eventTimeInMs:[OCMArg isNil]
  182. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  183. XCTestExpectation *expectation2 =
  184. [self expectationWithDescription:@"Completion Callback Triggered"];
  185. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  186. forCampaignID:campaignID
  187. withCampaignName:campaignName
  188. eventTimeInMs:nil
  189. completion:^(BOOL success) {
  190. [expectation2 fulfill];
  191. }];
  192. [self waitForExpectationsWithTimeout:2.0 handler:nil];
  193. OCMVerifyAll((id)self.mockClearcutLogger);
  194. }
  195. @end