GDTCORClockTest.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2018 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 "GDTCORTests/Unit/GDTCORTestCase.h"
  17. #import "GDTCORLibrary/Public/GDTCORClock.h"
  18. @interface GDTCORClockTest : GDTCORTestCase
  19. @end
  20. @implementation GDTCORClockTest
  21. /** Tests the default initializer. */
  22. - (void)testInit {
  23. XCTAssertNotNil([[GDTCORClockTest alloc] init]);
  24. }
  25. /** Tests taking a snapshot. */
  26. - (void)testSnapshot {
  27. GDTCORClock *snapshot;
  28. XCTAssertNoThrow(snapshot = [GDTCORClock snapshot]);
  29. XCTAssertGreaterThan(snapshot.timeMillis, 0);
  30. }
  31. /** Tests that the hash of two snapshots right after each other isn't equal. */
  32. - (void)testHash {
  33. GDTCORClock *snapshot1 = [GDTCORClock snapshot];
  34. GDTCORClock *snapshot2 = [GDTCORClock snapshot];
  35. XCTAssertNotEqual([snapshot1 hash], [snapshot2 hash]);
  36. }
  37. /** Tests that the class supports NSSecureEncoding. */
  38. - (void)testSupportsSecureEncoding {
  39. XCTAssertTrue([GDTCORClock supportsSecureCoding]);
  40. }
  41. /** Tests encoding and decoding a clock using a keyed archiver. */
  42. - (void)testEncoding {
  43. GDTCORClock *clock = [GDTCORClock snapshot];
  44. GDTCORClock *unarchivedClock;
  45. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  46. NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock
  47. requiringSecureCoding:YES
  48. error:nil];
  49. unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCORClock class]
  50. fromData:clockData
  51. error:nil];
  52. } else {
  53. #if !TARGET_OS_MACCATALYST
  54. NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock];
  55. unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData];
  56. #endif
  57. }
  58. XCTAssertEqual([clock hash], [unarchivedClock hash]);
  59. XCTAssertEqualObjects(clock, unarchivedClock);
  60. }
  61. /** Tests creating a clock that represents a future time. */
  62. - (void)testClockSnapshotInTheFuture {
  63. GDTCORClock *clock1 = [GDTCORClock snapshot];
  64. GDTCORClock *clock2 = [GDTCORClock clockSnapshotInTheFuture:1];
  65. XCTAssertTrue([clock2 isAfter:clock1]);
  66. }
  67. /** Tests creating a snapshot in the future and comparing using isAfter: */
  68. - (void)testIsAfter {
  69. GDTCORClock *clock1 = [GDTCORClock clockSnapshotInTheFuture:123456];
  70. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];
  71. GDTCORClock *clock2 = [GDTCORClock snapshot];
  72. XCTAssertFalse([clock2 isAfter:clock1]);
  73. }
  74. @end