FIRIAMBookKeeperViaUserDefaultsTests.m 7.5 KB

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