GDTCORClockTest.m 2.6 KB

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