FIRIAMFetchFlowTests.m 16 KB

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