FIRHeartbeatInfoTest.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <GoogleUtilities/GULHeartbeatDateStorable.h>
  15. #import <GoogleUtilities/GULHeartbeatDateStorage.h>
  16. #import <GoogleUtilities/GULHeartbeatDateStorageUserDefaults.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FirebaseCore/Sources/Private/FIRHeartbeatInfo.h"
  19. /// Taken from the implementation of `FIRHeartbeatInfo.m`.
  20. NSString *const kFIRCoreSuiteName = @"com.firebase.core";
  21. @interface FIRHeartbeatInfoTest : XCTestCase
  22. @property(nonatomic, strong) id<GULHeartbeatDateStorable> dataStorage;
  23. #if TARGET_OS_TV
  24. @property(nonatomic, strong) NSUserDefaults *defaults;
  25. #endif // TARGET_OS_TV
  26. @end
  27. @implementation FIRHeartbeatInfoTest
  28. - (void)setUp {
  29. NSString *const kHeartbeatStorageName = @"HEARTBEAT_INFO_STORAGE";
  30. #if TARGET_OS_TV
  31. self.defaults = [[NSUserDefaults alloc] initWithSuiteName:kFIRCoreSuiteName];
  32. self.dataStorage =
  33. [[GULHeartbeatDateStorageUserDefaults alloc] initWithDefaults:self.defaults
  34. key:kHeartbeatStorageName];
  35. #else
  36. self.dataStorage = [[GULHeartbeatDateStorage alloc] initWithFileName:kHeartbeatStorageName];
  37. #endif // TARGET_OS_TV
  38. NSDateComponents *componentsToAdd = [[NSDateComponents alloc] init];
  39. componentsToAdd.day = -1;
  40. NSDate *dayAgo = [[NSCalendar currentCalendar] dateByAddingComponents:componentsToAdd
  41. toDate:[NSDate date]
  42. options:0];
  43. [self.dataStorage setHearbeatDate:dayAgo forTag:@"fire-iid"];
  44. [self.dataStorage setHearbeatDate:dayAgo forTag:@"GLOBAL"];
  45. }
  46. #if TARGET_OS_TV
  47. - (void)tearDown {
  48. // Delete any residual storage.
  49. [self.defaults removePersistentDomainForName:kFIRCoreSuiteName];
  50. self.defaults = nil;
  51. [super tearDown];
  52. }
  53. #endif // TARGET_OS_TV
  54. - (void)testCombinedHeartbeat {
  55. FIRHeartbeatInfoCode heartbeatCode = [FIRHeartbeatInfo heartbeatCodeForTag:@"fire-iid"];
  56. XCTAssertEqual(heartbeatCode, FIRHeartbeatInfoCodeCombined);
  57. }
  58. - (void)testSdkOnlyHeartbeat {
  59. [self.dataStorage setHearbeatDate:[NSDate date] forTag:@"GLOBAL"];
  60. FIRHeartbeatInfoCode heartbeatCode = [FIRHeartbeatInfo heartbeatCodeForTag:@"fire-iid"];
  61. XCTAssertEqual(heartbeatCode, FIRHeartbeatInfoCodeSDK);
  62. }
  63. - (void)testGlobalOnlyHeartbeat {
  64. [self.dataStorage setHearbeatDate:[NSDate date] forTag:@"fire-iid"];
  65. FIRHeartbeatInfoCode heartbeatCode = [FIRHeartbeatInfo heartbeatCodeForTag:@"fire-iid"];
  66. XCTAssertEqual(heartbeatCode, FIRHeartbeatInfoCodeGlobal);
  67. }
  68. - (void)testNoHeartbeat {
  69. [self.dataStorage setHearbeatDate:[NSDate date] forTag:@"fire-iid"];
  70. [self.dataStorage setHearbeatDate:[NSDate date] forTag:@"GLOBAL"];
  71. FIRHeartbeatInfoCode heartbeatCode = [FIRHeartbeatInfo heartbeatCodeForTag:@"fire-iid"];
  72. XCTAssertEqual(heartbeatCode, FIRHeartbeatInfoCodeNone);
  73. }
  74. @end