FIRIAMAnalyticsEventLoggerImplTests.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. - (nonnull NSArray<FIRAConditionalUserProperty *> *)
  88. conditionalUserProperties:(nonnull NSString *)origin
  89. propertyNamePrefix:(nonnull NSString *)propertyNamePrefix {
  90. return @[];
  91. }
  92. - (NSInteger)maxUserProperties:(nonnull NSString *)origin {
  93. return -1;
  94. }
  95. - (void)setConditionalUserProperty:(nonnull FIRAConditionalUserProperty *)conditionalUserProperty {
  96. }
  97. - (void)registerAnalyticsListener:(nonnull id<FIRAnalyticsInteropListener>)listener
  98. withOrigin:(nonnull NSString *)origin {
  99. }
  100. - (void)unregisterAnalyticsListenerWithOrigin:(nonnull NSString *)origin {
  101. }
  102. @end
  103. @implementation FIRIAMAnalyticsEventLoggerImplTests
  104. - (void)setUp {
  105. [super setUp];
  106. self.mockClearcutLogger = OCMClassMock(FIRIAMClearcutLogger.class);
  107. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  108. self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
  109. }
  110. - (void)tearDown {
  111. [super tearDown];
  112. }
  113. - (void)testLogImpressionEvent {
  114. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  115. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  116. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  117. XCTAssertEqualObjects(origin, @"fiam");
  118. XCTAssertEqualObjects(name, @"firebase_in_app_message_impression");
  119. XCTAssertEqual([parameters count], 3);
  120. XCTAssertNotNil(parameters);
  121. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  122. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  123. [expectation1 fulfill];
  124. }];
  125. FIRIAMAnalyticsEventLoggerImpl *logger =
  126. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  127. usingTimeFetcher:self.mockTimeFetcher
  128. usingUserDefaults:nil
  129. analytics:analytics];
  130. NSTimeInterval currentMoment = 10000;
  131. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  132. OCMExpect([self.mockClearcutLogger
  133. logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  134. forCampaignID:[OCMArg isEqual:campaignID]
  135. withCampaignName:[OCMArg isEqual:campaignName]
  136. eventTimeInMs:[OCMArg isNil]
  137. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  138. XCTestExpectation *expectation2 =
  139. [self expectationWithDescription:@"Completion Callback Triggered"];
  140. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  141. forCampaignID:campaignID
  142. withCampaignName:campaignName
  143. eventTimeInMs:nil
  144. completion:^(BOOL success) {
  145. [expectation2 fulfill];
  146. }];
  147. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  148. }
  149. - (void)testLogActionEvent {
  150. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  151. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  152. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  153. XCTAssertEqualObjects(origin, @"fiam");
  154. XCTAssertEqualObjects(name, @"firebase_in_app_message_action");
  155. XCTAssertEqual([parameters count], 3);
  156. XCTAssertNotNil(parameters);
  157. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  158. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  159. [expectation1 fulfill];
  160. }];
  161. FIRIAMAnalyticsEventLoggerImpl *logger =
  162. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  163. usingTimeFetcher:self.mockTimeFetcher
  164. usingUserDefaults:self.mockUserDefaults
  165. analytics:analytics];
  166. NSTimeInterval currentMoment = 10000;
  167. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  168. OCMExpect([self.mockClearcutLogger
  169. logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  170. forCampaignID:[OCMArg isEqual:campaignID]
  171. withCampaignName:[OCMArg isEqual:campaignName]
  172. eventTimeInMs:[OCMArg isNil]
  173. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  174. XCTestExpectation *expectation2 =
  175. [self expectationWithDescription:@"Completion Callback Triggered"];
  176. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  177. forCampaignID:campaignID
  178. withCampaignName:campaignName
  179. eventTimeInMs:nil
  180. completion:^(BOOL success) {
  181. [expectation2 fulfill];
  182. }];
  183. [self waitForExpectationsWithTimeout:2.0 handler:nil];
  184. OCMVerifyAll((id)self.mockClearcutLogger);
  185. }
  186. @end