FIRIAMFetchFlowTests.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright 2017 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 <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FIRIAMAnalyticsEventLogger.h"
  19. #import "FIRIAMDisplayTriggerDefinition.h"
  20. #import "FIRIAMFetchFlow.h"
  21. #import "FIRIAMMessageContentDataWithImageURL.h"
  22. #import "FIRIAMSDKModeManager.h"
  23. @interface FIRIAMFetchFlow (Testing)
  24. // Expose to verify that this gets called on initial app launch fetch.
  25. - (void)checkForAppLaunchMessage;
  26. @end
  27. @interface FIRIAMFetchFlowTests : XCTestCase
  28. @property(nonatomic) FIRIAMFetchSetting *fetchSetting;
  29. @property FIRIAMMessageClientCache *clientMessageCache;
  30. @property id<FIRIAMMessageFetcher> mockMessageFetcher;
  31. @property id<FIRIAMBookKeeper> mockBookkeeper;
  32. @property id<FIRIAMTimeFetcher> mockTimeFetcher;
  33. @property FIRIAMFetchFlow *flow;
  34. @property FIRIAMActivityLogger *activityLogger;
  35. @property FIRIAMSDKModeManager *mockSDKModeManager;
  36. @property id<FIRIAMAnalyticsEventLogger> mockAnaltycisEventLogger;
  37. // three pre-defined messages
  38. @property FIRIAMMessageDefinition *m1, *m2, *m3;
  39. @end
  40. CGFloat FETCH_MIN_INTERVALS = 1;
  41. @implementation FIRIAMFetchFlowTests
  42. - (void)setupMessageTexture {
  43. // startTime, endTime here ensures messages with them are active
  44. NSTimeInterval activeStartTime = 0;
  45. NSTimeInterval activeEndTime = [[NSDate date] timeIntervalSince1970] + 10000;
  46. FIRIAMDisplayTriggerDefinition *triggerDefinition =
  47. [[FIRIAMDisplayTriggerDefinition alloc] initWithFirebaseAnalyticEvent:@"test_event"];
  48. FIRIAMMessageContentDataWithImageURL *m1ContentData =
  49. [[FIRIAMMessageContentDataWithImageURL alloc]
  50. initWithMessageTitle:@"m1 title"
  51. messageBody:@"message body"
  52. actionButtonText:nil
  53. secondaryActionButtonText:nil
  54. actionURL:[NSURL URLWithString:@"http://google.com"]
  55. secondaryActionURL:nil
  56. imageURL:[NSURL URLWithString:@"https://unsplash.it/300/300"]
  57. landscapeImageURL:nil
  58. usingURLSession:nil];
  59. FIRIAMRenderingEffectSetting *renderSetting1 =
  60. [FIRIAMRenderingEffectSetting getDefaultRenderingEffectSetting];
  61. renderSetting1.viewMode = FIRIAMRenderAsBannerView;
  62. FIRIAMMessageRenderData *renderData1 =
  63. [[FIRIAMMessageRenderData alloc] initWithMessageID:@"m1"
  64. messageName:@"name"
  65. contentData:m1ContentData
  66. renderingEffect:renderSetting1];
  67. self.m1 = [[FIRIAMMessageDefinition alloc] initWithRenderData:renderData1
  68. startTime:activeStartTime
  69. endTime:activeEndTime
  70. triggerDefinition:@[ triggerDefinition ]];
  71. FIRIAMMessageContentDataWithImageURL *m2ContentData =
  72. [[FIRIAMMessageContentDataWithImageURL alloc]
  73. initWithMessageTitle:@"m2 title"
  74. messageBody:@"message body"
  75. actionButtonText:nil
  76. secondaryActionButtonText:nil
  77. actionURL:[NSURL URLWithString:@"http://google.com"]
  78. secondaryActionURL:nil
  79. imageURL:[NSURL URLWithString:@"https://unsplash.it/300/400"]
  80. landscapeImageURL:nil
  81. usingURLSession:nil];
  82. FIRIAMRenderingEffectSetting *renderSetting2 =
  83. [FIRIAMRenderingEffectSetting getDefaultRenderingEffectSetting];
  84. renderSetting2.viewMode = FIRIAMRenderAsModalView;
  85. FIRIAMMessageRenderData *renderData2 =
  86. [[FIRIAMMessageRenderData alloc] initWithMessageID:@"m2"
  87. messageName:@"name"
  88. contentData:m2ContentData
  89. renderingEffect:renderSetting2];
  90. self.m2 = [[FIRIAMMessageDefinition alloc] initWithRenderData:renderData2
  91. startTime:activeStartTime
  92. endTime:activeEndTime
  93. triggerDefinition:@[ triggerDefinition ]];
  94. FIRIAMMessageContentDataWithImageURL *m3ContentData =
  95. [[FIRIAMMessageContentDataWithImageURL alloc]
  96. initWithMessageTitle:@"m3 title"
  97. messageBody:@"message body"
  98. actionButtonText:nil
  99. secondaryActionButtonText:nil
  100. actionURL:[NSURL URLWithString:@"http://google.com"]
  101. secondaryActionURL:nil
  102. imageURL:[NSURL URLWithString:@"https://unsplash.it/400/300"]
  103. landscapeImageURL:nil
  104. usingURLSession:nil];
  105. FIRIAMRenderingEffectSetting *renderSetting3 =
  106. [FIRIAMRenderingEffectSetting getDefaultRenderingEffectSetting];
  107. renderSetting3.viewMode = FIRIAMRenderAsImageOnlyView;
  108. FIRIAMMessageRenderData *renderData3 =
  109. [[FIRIAMMessageRenderData alloc] initWithMessageID:@"m3"
  110. messageName:@"name"
  111. contentData:m3ContentData
  112. renderingEffect:renderSetting3];
  113. self.m3 = [[FIRIAMMessageDefinition alloc] initWithRenderData:renderData3
  114. startTime:activeStartTime
  115. endTime:activeEndTime
  116. triggerDefinition:@[ triggerDefinition ]];
  117. }
  118. - (void)setUp {
  119. [super setUp];
  120. [self setupMessageTexture];
  121. self.fetchSetting = [[FIRIAMFetchSetting alloc] init];
  122. self.fetchSetting.fetchMinIntervalInMinutes = FETCH_MIN_INTERVALS;
  123. self.mockMessageFetcher = OCMProtocolMock(@protocol(FIRIAMMessageFetcher));
  124. FIRIAMFetchResponseParser *parser =
  125. [[FIRIAMFetchResponseParser alloc] initWithTimeFetcher:[[FIRIAMTimerWithNSDate alloc] init]];
  126. self.clientMessageCache = [[FIRIAMMessageClientCache alloc] initWithBookkeeper:self.mockBookkeeper
  127. usingResponseParser:parser];
  128. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  129. self.mockBookkeeper = OCMProtocolMock(@protocol(FIRIAMBookKeeper));
  130. self.activityLogger = OCMClassMock([FIRIAMActivityLogger class]);
  131. self.mockAnaltycisEventLogger = OCMProtocolMock(@protocol(FIRIAMAnalyticsEventLogger));
  132. self.mockSDKModeManager = OCMClassMock([FIRIAMSDKModeManager class]);
  133. self.mockTimeFetcher = OCMProtocolMock(@protocol(FIRIAMTimeFetcher));
  134. self.flow = [[FIRIAMFetchFlow alloc] initWithSetting:self.fetchSetting
  135. messageCache:self.clientMessageCache
  136. messageFetcher:self.mockMessageFetcher
  137. timeFetcher:self.mockTimeFetcher
  138. bookKeeper:self.mockBookkeeper
  139. activityLogger:self.activityLogger
  140. analyticsEventLogger:self.mockAnaltycisEventLogger
  141. FIRIAMSDKModeManager:self.mockSDKModeManager];
  142. }
  143. - (void)tearDown {
  144. // Put teardown code here. This method is called after the invocation of each test method in the
  145. // class.
  146. [super tearDown];
  147. }
  148. // In happy path, the fetch is allowed and we are able to fetch two messages back
  149. - (void)testHappyPath {
  150. OCMStub([self.mockBookkeeper lastFetchTime]).andReturn(0);
  151. // Set it up so that we already have impressions for m1 and m3
  152. FIRIAMImpressionRecord *impression1 =
  153. [[FIRIAMImpressionRecord alloc] initWithMessageID:self.m1.renderData.messageID
  154. impressionTimeInSeconds:1233];
  155. FIRIAMImpressionRecord *impression2 = [[FIRIAMImpressionRecord alloc] initWithMessageID:@"m3"
  156. impressionTimeInSeconds:5678];
  157. NSArray<FIRIAMImpressionRecord *> *impressions = @[ impression1, impression2 ];
  158. OCMStub([self.mockBookkeeper getImpressions]).andReturn(impressions);
  159. NSArray<FIRIAMMessageDefinition *> *fetchedMessages = @[ self.m1, self.m2 ];
  160. // 200 seconds is larger than fetch wait time which is 100 in this setup
  161. OCMStub([self.mockBookkeeper nextFetchWaitTime]).andReturn(100);
  162. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(200);
  163. OCMStub([self.mockSDKModeManager currentMode]).andReturn(FIRIAMSDKModeRegular);
  164. NSNumber *fetchWaitTimeFromResponse = [NSNumber numberWithInt:2000];
  165. OCMStub([self.mockMessageFetcher
  166. fetchMessagesWithImpressionList:[OCMArg any]
  167. withCompletion:([OCMArg invokeBlockWithArgs:fetchedMessages,
  168. fetchWaitTimeFromResponse,
  169. [NSNull null], [NSNull null],
  170. nil])]);
  171. [self.flow checkAndFetchForInitialAppLaunch:NO];
  172. // We expect m1 and m2 to be dumped into clientMessageCache.
  173. NSArray<FIRIAMMessageDefinition *> *foundMessages = [self.clientMessageCache allRegularMessages];
  174. XCTAssertEqual(2, foundMessages.count);
  175. XCTAssertEqualObjects(foundMessages[0].renderData.messageID, self.m1.renderData.messageID);
  176. XCTAssertEqualObjects(foundMessages[1].renderData.messageID, self.m2.renderData.messageID);
  177. // Verify that we record the new fetch with bookkeeper
  178. OCMVerify([self.mockBookkeeper recordNewFetchWithFetchCount:2
  179. withTimestampInSeconds:200
  180. nextFetchWaitTime:fetchWaitTimeFromResponse]);
  181. // So we are sending the request with impression for m1 and m3 and getting back messages for m1
  182. // and m2. In here m1 is a recurring message and after the fetch, we should call
  183. // book keeper's clearImpressionsWithMessageList: method with m1 which is an intersection
  184. // between the request impression list and the response message id list. We are skipping
  185. // m2 since it's not included in the impression records sent along with the request.
  186. OCMVerify(
  187. [self.mockBookkeeper clearImpressionsWithMessageList:@[ self.m1.renderData.messageID ]]);
  188. }
  189. // No fetch is to be performed if the required fetch interval is not met
  190. - (void)testNoFetchDueToIntervalConstraint {
  191. OCMStub([self.mockSDKModeManager currentMode]).andReturn(FIRIAMSDKModeRegular);
  192. // We need to wait at least 300 seconds before making another fetch
  193. OCMStub([self.mockBookkeeper nextFetchWaitTime]).andReturn(300);
  194. // And it's only been 200 seconds since last fetch, so no fetch should happen
  195. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(200);
  196. OCMStub([self.mockBookkeeper lastFetchTime]).andReturn(0);
  197. // We don't expect fetchMessages: for self.mockMessageFetcher to be triggred
  198. OCMReject([self.mockMessageFetcher fetchMessagesWithImpressionList:[OCMArg any]
  199. withCompletion:[OCMArg any]]);
  200. [self.flow checkAndFetchForInitialAppLaunch:NO];
  201. NSArray<FIRIAMMessageDefinition *> *foundMessages = [self.clientMessageCache allRegularMessages];
  202. XCTAssertEqual(0, foundMessages.count);
  203. }
  204. // Fetch always in newly installed mode
  205. - (void)testAlwaysFetchForNewlyInstalledMode {
  206. OCMStub([self.mockBookkeeper lastFetchTime]).andReturn(0);
  207. OCMStub([self.mockSDKModeManager currentMode]).andReturn(FIRIAMSDKModeNewlyInstalled);
  208. OCMStub([self.mockMessageFetcher
  209. fetchMessagesWithImpressionList:[OCMArg any]
  210. withCompletion:([OCMArg invokeBlockWithArgs:@[ self.m1, self.m2 ],
  211. [NSNull null], [NSNull null],
  212. [NSNull null], nil])]);
  213. // 100 seconds is less than fetch wait time which is 1000 in this setup,
  214. // but since we are in newly installed mode, fetch would still happen
  215. OCMStub([self.mockBookkeeper nextFetchWaitTime]).andReturn(1000);
  216. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(100);
  217. [self.flow checkAndFetchForInitialAppLaunch:YES];
  218. // we expect m1 and m2 to be dumped into clientMessageCache
  219. NSArray<FIRIAMMessageDefinition *> *foundMessages = [self.clientMessageCache allRegularMessages];
  220. XCTAssertEqual(2, foundMessages.count);
  221. XCTAssertEqualObjects(foundMessages[0].renderData.messageID, self.m1.renderData.messageID);
  222. XCTAssertEqualObjects(foundMessages[1].renderData.messageID, self.m2.renderData.messageID);
  223. // we expect to register a fetch with sdk manager
  224. OCMVerify([self.mockSDKModeManager registerOneMoreFetch]);
  225. // we expect that the message cache is checked for app launch messages
  226. OCMVerify([self.flow checkForAppLaunchMessage]);
  227. }
  228. // Fetch always in testing app instance mode
  229. - (void)testAlwaysFetchForTestingAppInstanceMode {
  230. OCMStub([self.mockBookkeeper lastFetchTime]).andReturn(0);
  231. OCMStub([self.mockSDKModeManager currentMode]).andReturn(FIRIAMSDKModeTesting);
  232. OCMStub([self.mockMessageFetcher
  233. fetchMessagesWithImpressionList:[OCMArg any]
  234. withCompletion:([OCMArg invokeBlockWithArgs:@[ self.m1, self.m2 ],
  235. [NSNull null], [NSNull null],
  236. [NSNull null], nil])]);
  237. // 100 seconds is less than fetch wait time which is 1000 in this setup,
  238. // but since we are in testing app instance mode, fetch would still happen
  239. OCMStub([self.mockBookkeeper nextFetchWaitTime]).andReturn(1000);
  240. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(100);
  241. [self.flow checkAndFetchForInitialAppLaunch:NO];
  242. // we expect m1 and m2 to be dumped into clientMessageCache
  243. NSArray<FIRIAMMessageDefinition *> *foundMessages = [self.clientMessageCache allRegularMessages];
  244. XCTAssertEqual(2, foundMessages.count);
  245. XCTAssertEqualObjects(foundMessages[0].renderData.messageID, self.m1.renderData.messageID);
  246. XCTAssertEqualObjects(foundMessages[1].renderData.messageID, self.m2.renderData.messageID);
  247. // we expect to register a fetch with sdk manager
  248. OCMVerify([self.mockSDKModeManager registerOneMoreFetch]);
  249. }
  250. - (void)testTurnIntoTestigModeOnSeeingTestMessage {
  251. OCMStub([self.mockBookkeeper lastFetchTime]).andReturn(0);
  252. OCMStub([self.mockSDKModeManager currentMode]).andReturn(FIRIAMSDKModeNewlyInstalled);
  253. FIRIAMMessageDefinition *testMessage =
  254. [[FIRIAMMessageDefinition alloc] initTestMessageWithRenderData:self.m2.renderData];
  255. OCMStub([self.mockMessageFetcher
  256. fetchMessagesWithImpressionList:[OCMArg any]
  257. withCompletion:([OCMArg invokeBlockWithArgs:@[ self.m1, testMessage ],
  258. [NSNull null], [NSNull null],
  259. [NSNull null], nil])]);
  260. self.fetchSetting.fetchMinIntervalInMinutes = 10; // at least 600 seconds between fetches
  261. // 100 seconds is larger than FETCH_MIN_INTERVALS minutes
  262. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(100);
  263. [self.flow checkAndFetchForInitialAppLaunch:NO];
  264. // Expecting turning sdk mode into a testing instance
  265. OCMVerify([self.mockSDKModeManager becomeTestingInstance]);
  266. }
  267. - (void)testNotTurningIntoTestingModeIfAlreadyInTestingMode {
  268. OCMStub([self.mockBookkeeper lastFetchTime]).andReturn(0);
  269. OCMStub([self.mockSDKModeManager currentMode]).andReturn(FIRIAMSDKModeTesting);
  270. FIRIAMMessageDefinition *testMessage =
  271. [[FIRIAMMessageDefinition alloc] initTestMessageWithRenderData:self.m2.renderData];
  272. OCMStub([self.mockMessageFetcher
  273. fetchMessagesWithImpressionList:[OCMArg any]
  274. withCompletion:([OCMArg invokeBlockWithArgs:@[ self.m1, testMessage ],
  275. [NSNull null], [NSNull null],
  276. [NSNull null], nil])]);
  277. self.fetchSetting.fetchMinIntervalInMinutes = 10; // at least 600 seconds between fetches
  278. OCMStub([self.mockTimeFetcher currentTimestampInSeconds]).andReturn(1000);
  279. OCMReject([self.mockSDKModeManager becomeTestingInstance]);
  280. [self.flow checkAndFetchForInitialAppLaunch:NO];
  281. }
  282. @end