FIRIAMBookKeeperViaUserDefaultsTests.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMBookKeeper.h"
  19. @interface FIRIAMBookKeeperViaUserDefaultsTests : XCTestCase
  20. @property(nonatomic) NSUserDefaults *userDefaultsForTesting;
  21. @end
  22. extern NSString *FIRIAM_UserDefaultsKeyForImpressions;
  23. extern NSString *FIRIAM_UserDefaultsKeyForLastImpressionTimestamp;
  24. extern NSString *FIRIAM_ImpressionDictKeyForID;
  25. extern NSString *FIRIAM_ImpressionDictKeyForTimestamp;
  26. @implementation FIRIAMBookKeeperViaUserDefaultsTests
  27. - (void)setUp {
  28. [super setUp];
  29. self.userDefaultsForTesting =
  30. [[NSUserDefaults alloc] initWithSuiteName:@"FIRIAMBookKeeperViaUserDefaultsTests"];
  31. }
  32. - (void)tearDown {
  33. // Put teardown code here. This method is called after the invocation of each test method in the
  34. // class.
  35. [super tearDown];
  36. [self.userDefaultsForTesting removeSuiteNamed:@"FIRIAMBookKeeperViaUserDefaultsTests"];
  37. }
  38. - (void)testRecordImpressionRecords {
  39. FIRIAMBookKeeperViaUserDefaults *bookKeeper =
  40. [[FIRIAMBookKeeperViaUserDefaults alloc] initWithUserDefaults:self.userDefaultsForTesting];
  41. [bookKeeper cleanupImpressions];
  42. NSArray<FIRIAMImpressionRecord *> *impressions = [bookKeeper getImpressions];
  43. XCTAssertEqual(0, [impressions count]);
  44. double impression1_ts = 12345;
  45. double impression2_ts = 34567;
  46. [bookKeeper recordNewImpressionForMessage:@"m1" withStartTimestampInSeconds:impression1_ts];
  47. [bookKeeper recordNewImpressionForMessage:@"m1" withStartTimestampInSeconds:impression2_ts];
  48. impressions = [bookKeeper getImpressions];
  49. // For the same message, we only record the last impression record.
  50. XCTAssertEqual(1, [impressions count]);
  51. XCTAssertEqualWithAccuracy(impression2_ts, impressions[0].impressionTimeInSeconds, 0.1);
  52. // Verify the last display time.
  53. XCTAssertEqualWithAccuracy(impression2_ts, [bookKeeper lastDisplayTime], 0.1);
  54. double impression3_ts = 45000;
  55. [bookKeeper recordNewImpressionForMessage:@"m2" withStartTimestampInSeconds:impression3_ts];
  56. impressions = [bookKeeper getImpressions];
  57. // Now we should see two different impression records for two different messages.
  58. XCTAssertEqual(2, [impressions count]);
  59. // Verify the last display time is updated again.
  60. XCTAssertEqualWithAccuracy(impression3_ts, [bookKeeper lastDisplayTime], 0.1);
  61. }
  62. - (void)testRecordFetchTimes {
  63. FIRIAMBookKeeperViaUserDefaults *bookKeeper =
  64. [[FIRIAMBookKeeperViaUserDefaults alloc] initWithUserDefaults:self.userDefaultsForTesting];
  65. [bookKeeper cleanupImpressions];
  66. double fetch1_ts = 12345;
  67. double fetch2_ts = 34567;
  68. [bookKeeper recordNewFetchWithFetchCount:10
  69. withTimestampInSeconds:fetch1_ts
  70. nextFetchWaitTime:nil];
  71. [bookKeeper recordNewFetchWithFetchCount:10
  72. withTimestampInSeconds:fetch2_ts
  73. nextFetchWaitTime:nil];
  74. XCTAssertEqualWithAccuracy(fetch2_ts, [bookKeeper lastFetchTime], 0.1);
  75. }
  76. - (void)testRecordFetchTimesWithFetchWaitTime {
  77. FIRIAMBookKeeperViaUserDefaults *bookKeeper =
  78. [[FIRIAMBookKeeperViaUserDefaults alloc] initWithUserDefaults:self.userDefaultsForTesting];
  79. [bookKeeper cleanupImpressions];
  80. double fetch1_ts = 12345;
  81. NSNumber *fetchWaitTime = [NSNumber numberWithInt:30000];
  82. [bookKeeper recordNewFetchWithFetchCount:10
  83. withTimestampInSeconds:fetch1_ts
  84. nextFetchWaitTime:fetchWaitTime];
  85. XCTAssertEqualWithAccuracy(fetchWaitTime.doubleValue, [bookKeeper nextFetchWaitTime], 0.1);
  86. }
  87. - (void)testRecordFetchTimesWithFetchWaitTimeOverCap {
  88. FIRIAMBookKeeperViaUserDefaults *bookKeeper =
  89. [[FIRIAMBookKeeperViaUserDefaults alloc] initWithUserDefaults:self.userDefaultsForTesting];
  90. [bookKeeper cleanupImpressions];
  91. double fetch1_ts = 12345;
  92. NSNumber *fetchWaitTime = [NSNumber numberWithInt:30000];
  93. [bookKeeper recordNewFetchWithFetchCount:10
  94. withTimestampInSeconds:fetch1_ts
  95. nextFetchWaitTime:fetchWaitTime];
  96. XCTAssertEqualWithAccuracy(fetchWaitTime.doubleValue, [bookKeeper nextFetchWaitTime], 0.1);
  97. // Second recording use a very large fetch wait time: 30000000 is to large to be accepted.
  98. NSNumber *fetchWaitTime2 = [NSNumber numberWithInt:30000000];
  99. [bookKeeper recordNewFetchWithFetchCount:10
  100. withTimestampInSeconds:fetch1_ts
  101. nextFetchWaitTime:fetchWaitTime2];
  102. // Next fetch wait time is still the same as from fetchWaitTime
  103. XCTAssertEqualWithAccuracy(fetchWaitTime.doubleValue, [bookKeeper nextFetchWaitTime], 0.1);
  104. }
  105. - (void)testFetchImpressions {
  106. NSString *message1 = @"message1 id";
  107. double message1ImpressionTime = 1000.0;
  108. NSString *message2 = @"message2 id";
  109. double message2ImpressionTime = 2000.0;
  110. FIRIAMBookKeeperViaUserDefaults *bookKeeper =
  111. [[FIRIAMBookKeeperViaUserDefaults alloc] initWithUserDefaults:self.userDefaultsForTesting];
  112. [bookKeeper cleanupImpressions];
  113. // Set up existing impressions.
  114. [bookKeeper recordNewImpressionForMessage:message1
  115. withStartTimestampInSeconds:message1ImpressionTime];
  116. [bookKeeper recordNewImpressionForMessage:message2
  117. withStartTimestampInSeconds:message2ImpressionTime];
  118. NSArray<FIRIAMImpressionRecord *> *fetchedImpressions = [bookKeeper getImpressions];
  119. XCTAssertEqual(2, fetchedImpressions.count);
  120. FIRIAMImpressionRecord *first = fetchedImpressions[0];
  121. XCTAssertEqualObjects(first.messageID, message1);
  122. XCTAssertEqualWithAccuracy((double)first.impressionTimeInSeconds, message1ImpressionTime, 0.1);
  123. FIRIAMImpressionRecord *second = fetchedImpressions[1];
  124. XCTAssertEqualObjects(second.messageID, message2);
  125. XCTAssertEqualWithAccuracy((double)second.impressionTimeInSeconds, message2ImpressionTime, 0.1);
  126. NSArray<NSString *> *messageIDs = [bookKeeper getMessageIDsFromImpressions];
  127. XCTAssertEqualObjects(messageIDs[0], message1);
  128. XCTAssertEqualObjects(messageIDs[1], message2);
  129. }
  130. - (void)testClearImpressionsForMessageIDs {
  131. FIRIAMBookKeeperViaUserDefaults *bookKeeper =
  132. [[FIRIAMBookKeeperViaUserDefaults alloc] initWithUserDefaults:self.userDefaultsForTesting];
  133. [bookKeeper cleanupImpressions];
  134. NSArray<FIRIAMImpressionRecord *> *impressions = [bookKeeper getImpressions];
  135. XCTAssertEqual(0, [impressions count]);
  136. double impression1_ts = 12345;
  137. double impression2_ts = 34567;
  138. double impression3_ts = 34567;
  139. [bookKeeper recordNewImpressionForMessage:@"m1" withStartTimestampInSeconds:impression1_ts];
  140. [bookKeeper recordNewImpressionForMessage:@"m2" withStartTimestampInSeconds:impression2_ts];
  141. [bookKeeper recordNewImpressionForMessage:@"m3" withStartTimestampInSeconds:impression3_ts];
  142. [bookKeeper clearImpressionsWithMessageList:@[ @"m1", @"m3" ]];
  143. impressions = [bookKeeper getImpressions];
  144. // Only impressions about m2 remains.
  145. XCTAssertEqual(1, [impressions count]);
  146. XCTAssertEqualObjects(impressions[0].messageID, @"m2");
  147. }
  148. @end