FIRIAMActivityLoggerTests.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMActivityLogger.h"
  18. @interface FIRIAMActivityLogger ()
  19. - (void)loadFromCachePath:(NSString *)cacheFilePath;
  20. - (BOOL)saveIntoCacheWithPath:(NSString *)cacheFilePath;
  21. @end
  22. @interface FIRIAMActivityLoggerTests : XCTestCase
  23. @end
  24. @implementation FIRIAMActivityLoggerTests
  25. - (void)setUp {
  26. [super setUp];
  27. // Put setup code here. This method is called before the invocation of each test method in the
  28. // class.
  29. }
  30. - (void)tearDown {
  31. // Put teardown code here. This method is called after the invocation of each test method in the
  32. // class.
  33. [super tearDown];
  34. }
  35. - (void)testNormalFlow {
  36. FIRIAMActivityLogger *logger = [[FIRIAMActivityLogger alloc] initWithMaxCountBeforeReduce:100
  37. withSizeAfterReduce:80
  38. verboseMode:YES
  39. loadFromCache:NO];
  40. FIRIAMActivityRecord *first =
  41. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeRenderMessage
  42. isSuccessful:NO
  43. withDetail:@"log detail"
  44. timestamp:nil];
  45. [logger addLogRecord:first];
  46. NSDate *now = [[NSDate alloc] init];
  47. FIRIAMActivityRecord *second =
  48. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeCheckForFetch
  49. isSuccessful:YES
  50. withDetail:@"log detail2"
  51. timestamp:now];
  52. [logger addLogRecord:second];
  53. // now read them back
  54. NSArray<FIRIAMActivityRecord *> *records = [logger readRecords];
  55. XCTAssertEqual(2, [records count]);
  56. // notice that log records read out would be [second, first] in LIFO order
  57. FIRIAMActivityRecord *firstFetched = records[0];
  58. XCTAssertEqualObjects(@"log detail2", firstFetched.detail);
  59. XCTAssertEqual(YES, firstFetched.success);
  60. XCTAssertEqual(FIRIAMActivityTypeCheckForFetch, firstFetched.activityType);
  61. // second's timestamp should be equal to now since it's used to construct that log record
  62. XCTAssertEqualWithAccuracy(now.timeIntervalSince1970,
  63. firstFetched.timestamp.timeIntervalSince1970, 0.001);
  64. FIRIAMActivityRecord *secondFetched = records[1];
  65. XCTAssertEqualObjects(@"log detail", secondFetched.detail);
  66. XCTAssertEqual(NO, secondFetched.success);
  67. XCTAssertEqual(FIRIAMActivityTypeRenderMessage, secondFetched.activityType);
  68. // 60 seconds is large enough buffer for the timestamp comparison
  69. XCTAssertEqualWithAccuracy([[NSDate alloc] init].timeIntervalSince1970,
  70. secondFetched.timestamp.timeIntervalSince1970, 60);
  71. }
  72. - (void)testReduceAfterReachingMaxCount {
  73. // expected behavior for logger regarding reducing is to come down to 1 after reaching size of 3
  74. FIRIAMActivityLogger *logger = [[FIRIAMActivityLogger alloc] initWithMaxCountBeforeReduce:3
  75. withSizeAfterReduce:1
  76. verboseMode:YES
  77. loadFromCache:NO];
  78. FIRIAMActivityRecord *first =
  79. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeRenderMessage
  80. isSuccessful:NO
  81. withDetail:@"log detail"
  82. timestamp:nil];
  83. [logger addLogRecord:first];
  84. FIRIAMActivityRecord *second =
  85. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeCheckForFetch
  86. isSuccessful:YES
  87. withDetail:@"log detail2"
  88. timestamp:nil];
  89. [logger addLogRecord:second];
  90. FIRIAMActivityRecord *third =
  91. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeCheckForFetch
  92. isSuccessful:YES
  93. withDetail:@"log detail3"
  94. timestamp:nil];
  95. [logger addLogRecord:third];
  96. NSArray<FIRIAMActivityRecord *> *records = [logger readRecords];
  97. XCTAssertEqual(1, [records count]);
  98. // and the remaining one would be the last one being inserted
  99. XCTAssertEqualObjects(@"log detail3", records[0].detail);
  100. }
  101. - (void)testNonVerboseMode {
  102. // certain types of messages would get dropped
  103. FIRIAMActivityLogger *logger = [[FIRIAMActivityLogger alloc] initWithMaxCountBeforeReduce:100
  104. withSizeAfterReduce:50
  105. verboseMode:NO
  106. loadFromCache:NO];
  107. // this one would be added
  108. FIRIAMActivityRecord *next =
  109. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeRenderMessage
  110. isSuccessful:NO
  111. withDetail:@"log detail"
  112. timestamp:nil];
  113. [logger addLogRecord:next];
  114. // this one would be dropped
  115. next = [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeCheckForOnOpenMessage
  116. isSuccessful:NO
  117. withDetail:@"log detail"
  118. timestamp:nil];
  119. [logger addLogRecord:next];
  120. // this one would be added
  121. next = [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeFetchMessage
  122. isSuccessful:NO
  123. withDetail:@"log detail"
  124. timestamp:nil];
  125. [logger addLogRecord:next];
  126. NSArray<FIRIAMActivityRecord *> *records = [logger readRecords];
  127. XCTAssertEqual(2, [records count]);
  128. }
  129. - (void)testReadingAndWritingCache {
  130. FIRIAMActivityLogger *logger = [[FIRIAMActivityLogger alloc] initWithMaxCountBeforeReduce:100
  131. withSizeAfterReduce:50
  132. verboseMode:YES
  133. loadFromCache:NO];
  134. FIRIAMActivityRecord *next =
  135. [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeRenderMessage
  136. isSuccessful:NO
  137. withDetail:@"log detail"
  138. timestamp:nil];
  139. [logger addLogRecord:next];
  140. next = [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeCheckForOnOpenMessage
  141. isSuccessful:NO
  142. withDetail:@"log detail2"
  143. timestamp:nil];
  144. [logger addLogRecord:next];
  145. next = [[FIRIAMActivityRecord alloc] initWithActivityType:FIRIAMActivityTypeCheckForOnOpenMessage
  146. isSuccessful:NO
  147. withDetail:@"log detail3"
  148. timestamp:nil];
  149. [logger addLogRecord:next];
  150. NSString *cacheFilePath = [NSString stringWithFormat:@"%@/temp-cache", NSTemporaryDirectory()];
  151. [logger saveIntoCacheWithPath:cacheFilePath];
  152. // read it back
  153. FIRIAMActivityLogger *logger2 = [[FIRIAMActivityLogger alloc] initWithMaxCountBeforeReduce:100
  154. withSizeAfterReduce:50
  155. verboseMode:YES
  156. loadFromCache:NO];
  157. [logger2 loadFromCachePath:cacheFilePath];
  158. XCTAssertEqual(3, [[logger2 readRecords] count]);
  159. }
  160. @end