FIRIAMAnalyticsEventLoggerImplTests.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FIRIAMAnalyticsEventLoggerImpl.h"
  19. #import "FIRIAMClearcutLogger.h"
  20. #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
  21. #import <FirebaseAnalyticsInterop/FIRInteropEventNames.h>
  22. #import <FirebaseAnalyticsInterop/FIRInteropParameterNames.h>
  23. @interface FIRIAMAnalyticsEventLoggerImplTests : XCTestCase
  24. @property(nonatomic) FIRIAMClearcutLogger *mockClearcutLogger;
  25. @property(nonatomic) id<FIRIAMTimeFetcher> mockTimeFetcher;
  26. @property(nonatomic) id mockFirebaseAnalytics;
  27. @property(nonatomic) NSUserDefaults *mockUserDefaults;
  28. @end
  29. static NSString *campaignID = @"campaign id";
  30. static NSString *campaignName = @"campaign name";
  31. typedef void (^FIRAUserPropertiesCallback)(NSDictionary *userProperties);
  32. typedef void (^FakeAnalyticsLogEventHandler)(NSString *origin,
  33. NSString *name,
  34. NSDictionary *parameters);
  35. typedef void (^FakeAnalyticsUserPropertyHandler)(NSString *origin, NSString *name, id value);
  36. typedef void (^LastNotificationCallback)(NSString *);
  37. typedef void (^FakeAnalyticsLastNotificationHandler)(NSString *origin, LastNotificationCallback);
  38. @interface FakeAnalytics : NSObject <FIRAnalyticsInterop>
  39. @property FakeAnalyticsLogEventHandler eventHandler;
  40. @property FakeAnalyticsLogEventHandler userPropertyHandler;
  41. @property FakeAnalyticsLastNotificationHandler lastNotificationHandler;
  42. - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler;
  43. - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler;
  44. @end
  45. @implementation FakeAnalytics
  46. - (instancetype)initWithEventHandler:(FakeAnalyticsLogEventHandler)eventHandler {
  47. self = [super init];
  48. if (self) {
  49. _eventHandler = eventHandler;
  50. }
  51. return self;
  52. }
  53. - (instancetype)initWithUserPropertyHandler:(FakeAnalyticsUserPropertyHandler)userPropertyHandler {
  54. self = [super init];
  55. if (self) {
  56. _userPropertyHandler = userPropertyHandler;
  57. }
  58. return self;
  59. }
  60. - (void)logEventWithOrigin:(nonnull NSString *)origin
  61. name:(nonnull NSString *)name
  62. parameters:(nullable NSDictionary<NSString *, id> *)parameters {
  63. if (_eventHandler) {
  64. _eventHandler(origin, name, parameters);
  65. }
  66. }
  67. - (void)setUserPropertyWithOrigin:(nonnull NSString *)origin
  68. name:(nonnull NSString *)name
  69. value:(nonnull id)value {
  70. if (_userPropertyHandler) {
  71. _userPropertyHandler(origin, name, value);
  72. }
  73. }
  74. - (void)checkLastNotificationForOrigin:(nonnull NSString *)origin
  75. queue:(nonnull dispatch_queue_t)queue
  76. callback:(nonnull void (^)(NSString *_Nullable))
  77. currentLastNotificationProperty {
  78. if (_lastNotificationHandler) {
  79. _lastNotificationHandler(origin, currentLastNotificationProperty);
  80. }
  81. }
  82. // Stubs
  83. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  84. clearEventName:(nonnull NSString *)clearEventName
  85. clearEventParameters:(nonnull NSDictionary *)clearEventParameters {
  86. }
  87. - (NSInteger)maxUserProperties:(nonnull NSString *)origin {
  88. return -1;
  89. }
  90. - (void)registerAnalyticsListener:(nonnull id<FIRAnalyticsInteropListener>)listener
  91. withOrigin:(nonnull NSString *)origin {
  92. }
  93. - (void)unregisterAnalyticsListenerWithOrigin:(nonnull NSString *)origin {
  94. }
  95. - (void)clearConditionalUserProperty:(nonnull NSString *)userPropertyName
  96. forOrigin:(nonnull NSString *)origin
  97. clearEventName:(nonnull NSString *)clearEventName
  98. clearEventParameters:
  99. (nonnull NSDictionary<NSString *, NSString *> *)clearEventParameters {
  100. }
  101. - (nonnull NSArray<NSDictionary<NSString *, NSString *> *> *)
  102. conditionalUserProperties:(nonnull NSString *)origin
  103. propertyNamePrefix:(nonnull NSString *)propertyNamePrefix {
  104. return nil;
  105. }
  106. - (void)setConditionalUserProperty:(nonnull NSDictionary<NSString *, id> *)conditionalUserProperty {
  107. }
  108. @end
  109. @implementation FIRIAMAnalyticsEventLoggerImplTests
  110. - (void)setUp {
  111. [super setUp];
  112. self.mockClearcutLogger = OCMClassMock(FIRIAMClearcutLogger.class);
  113. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  114. self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
  115. }
  116. - (void)tearDown {
  117. [super tearDown];
  118. }
  119. - (void)testLogImpressionEvent {
  120. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  121. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  122. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  123. XCTAssertEqualObjects(origin, @"fiam");
  124. XCTAssertEqualObjects(name, @"firebase_in_app_message_impression");
  125. XCTAssertEqual([parameters count], 3);
  126. XCTAssertNotNil(parameters);
  127. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  128. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  129. [expectation1 fulfill];
  130. }];
  131. FIRIAMAnalyticsEventLoggerImpl *logger =
  132. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  133. usingTimeFetcher:self.mockTimeFetcher
  134. usingUserDefaults:nil
  135. analytics:analytics];
  136. NSTimeInterval currentMoment = 10000;
  137. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  138. OCMExpect([self.mockClearcutLogger
  139. logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  140. forCampaignID:[OCMArg isEqual:campaignID]
  141. withCampaignName:[OCMArg isEqual:campaignName]
  142. eventTimeInMs:[OCMArg isNil]
  143. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  144. XCTestExpectation *expectation2 =
  145. [self expectationWithDescription:@"Completion Callback Triggered"];
  146. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  147. forCampaignID:campaignID
  148. withCampaignName:campaignName
  149. eventTimeInMs:nil
  150. completion:^(BOOL success) {
  151. [expectation2 fulfill];
  152. }];
  153. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  154. }
  155. - (void)testLogActionEvent {
  156. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  157. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  158. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  159. XCTAssertEqualObjects(origin, @"fiam");
  160. XCTAssertEqualObjects(name, @"firebase_in_app_message_action");
  161. XCTAssertEqual([parameters count], 3);
  162. XCTAssertNotNil(parameters);
  163. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  164. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  165. [expectation1 fulfill];
  166. }];
  167. FIRIAMAnalyticsEventLoggerImpl *logger =
  168. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  169. usingTimeFetcher:self.mockTimeFetcher
  170. usingUserDefaults:self.mockUserDefaults
  171. analytics:analytics];
  172. NSTimeInterval currentMoment = 10000;
  173. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  174. OCMExpect([self.mockClearcutLogger
  175. logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  176. forCampaignID:[OCMArg isEqual:campaignID]
  177. withCampaignName:[OCMArg isEqual:campaignName]
  178. eventTimeInMs:[OCMArg isNil]
  179. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  180. XCTestExpectation *expectation2 =
  181. [self expectationWithDescription:@"Completion Callback Triggered"];
  182. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  183. forCampaignID:campaignID
  184. withCampaignName:campaignName
  185. eventTimeInMs:nil
  186. completion:^(BOOL success) {
  187. [expectation2 fulfill];
  188. }];
  189. [self waitForExpectationsWithTimeout:2.0 handler:nil];
  190. OCMVerifyAll((id)self.mockClearcutLogger);
  191. }
  192. @end