FIRIAMAnalyticsEventLoggerImplTests.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "FirebaseInAppMessaging/Sources/Analytics/FIRIAMAnalyticsEventLoggerImpl.h"
  19. #import "FirebaseInAppMessaging/Sources/Private/Analytics/FIRIAMClearcutLogger.h"
  20. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  21. #import "Interop/Analytics/Public/FIRInteropEventNames.h"
  22. #import "Interop/Analytics/Public/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. - (void)getUserPropertiesWithCallback:(nonnull FIRAInteropUserPropertiesCallback)callback {
  109. }
  110. @end
  111. @implementation FIRIAMAnalyticsEventLoggerImplTests
  112. - (void)setUp {
  113. [super setUp];
  114. self.mockClearcutLogger = OCMClassMock(FIRIAMClearcutLogger.class);
  115. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  116. self.mockUserDefaults = OCMClassMock(NSUserDefaults.class);
  117. }
  118. - (void)tearDown {
  119. [super tearDown];
  120. }
  121. - (void)testLogImpressionEvent {
  122. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  123. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  124. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  125. XCTAssertEqualObjects(origin, @"fiam");
  126. XCTAssertEqualObjects(name, @"firebase_in_app_message_impression");
  127. XCTAssertEqual([parameters count], 3);
  128. XCTAssertNotNil(parameters);
  129. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  130. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  131. [expectation1 fulfill];
  132. }];
  133. FIRIAMAnalyticsEventLoggerImpl *logger =
  134. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  135. usingTimeFetcher:self.mockTimeFetcher
  136. usingUserDefaults:nil
  137. analytics:analytics];
  138. NSTimeInterval currentMoment = 10000;
  139. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  140. OCMExpect([self.mockClearcutLogger
  141. logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  142. forCampaignID:[OCMArg isEqual:campaignID]
  143. withCampaignName:[OCMArg isEqual:campaignName]
  144. eventTimeInMs:[OCMArg isNil]
  145. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  146. XCTestExpectation *expectation2 =
  147. [self expectationWithDescription:@"Completion Callback Triggered"];
  148. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventMessageImpression
  149. forCampaignID:campaignID
  150. withCampaignName:campaignName
  151. eventTimeInMs:nil
  152. completion:^(BOOL success) {
  153. [expectation2 fulfill];
  154. }];
  155. [self waitForExpectationsWithTimeout:1.0 handler:nil];
  156. }
  157. - (void)testLogActionEvent {
  158. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Log to Analytics"];
  159. FakeAnalytics *analytics = [[FakeAnalytics alloc]
  160. initWithEventHandler:^(NSString *origin, NSString *name, NSDictionary *parameters) {
  161. XCTAssertEqualObjects(origin, @"fiam");
  162. XCTAssertEqualObjects(name, @"firebase_in_app_message_action");
  163. XCTAssertEqual([parameters count], 3);
  164. XCTAssertNotNil(parameters);
  165. XCTAssertEqual(parameters[@"_nmid"], campaignID);
  166. XCTAssertEqual(parameters[@"_nmn"], campaignName);
  167. [expectation1 fulfill];
  168. }];
  169. FIRIAMAnalyticsEventLoggerImpl *logger =
  170. [[FIRIAMAnalyticsEventLoggerImpl alloc] initWithClearcutLogger:self.mockClearcutLogger
  171. usingTimeFetcher:self.mockTimeFetcher
  172. usingUserDefaults:self.mockUserDefaults
  173. analytics:analytics];
  174. NSTimeInterval currentMoment = 10000;
  175. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(currentMoment);
  176. OCMExpect([self.mockClearcutLogger
  177. logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  178. forCampaignID:[OCMArg isEqual:campaignID]
  179. withCampaignName:[OCMArg isEqual:campaignName]
  180. eventTimeInMs:[OCMArg isNil]
  181. completion:([OCMArg invokeBlockWithArgs:@YES, nil])]);
  182. XCTestExpectation *expectation2 =
  183. [self expectationWithDescription:@"Completion Callback Triggered"];
  184. [logger logAnalyticsEventForType:FIRIAMAnalyticsEventActionURLFollow
  185. forCampaignID:campaignID
  186. withCampaignName:campaignName
  187. eventTimeInMs:nil
  188. completion:^(BOOL success) {
  189. [expectation2 fulfill];
  190. }];
  191. [self waitForExpectationsWithTimeout:2.0 handler:nil];
  192. OCMVerifyAll((id)self.mockClearcutLogger);
  193. }
  194. @end