GDTClockTest.m 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "GDTTests/Unit/GDTTestCase.h"
  17. #import "GDTLibrary/Public/GDTClock.h"
  18. @interface GDTClockTest : GDTTestCase
  19. @end
  20. @implementation GDTClockTest
  21. /** Tests the default initializer. */
  22. - (void)testInit {
  23. XCTAssertNotNil([[GDTClockTest alloc] init]);
  24. }
  25. /** Tests taking a snapshot. */
  26. - (void)testSnapshot {
  27. GDTClock *snapshot;
  28. XCTAssertNoThrow(snapshot = [GDTClock 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. GDTClock *snapshot1 = [GDTClock snapshot];
  34. GDTClock *snapshot2 = [GDTClock snapshot];
  35. XCTAssertNotEqual([snapshot1 hash], [snapshot2 hash]);
  36. }
  37. /** Tests that the class supports NSSecureEncoding. */
  38. - (void)testSupportsSecureEncoding {
  39. XCTAssertTrue([GDTClock supportsSecureCoding]);
  40. }
  41. /** Tests encoding and decoding a clock using a keyed archiver. */
  42. - (void)testEncoding {
  43. GDTClock *clock = [GDTClock snapshot];
  44. NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock];
  45. GDTClock *unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData];
  46. XCTAssertEqual([clock hash], [unarchivedClock hash]);
  47. XCTAssertEqualObjects(clock, unarchivedClock);
  48. }
  49. /** Tests creating a clock that represents a future time. */
  50. - (void)testClockSnapshotInTheFuture {
  51. GDTClock *clock1 = [GDTClock snapshot];
  52. GDTClock *clock2 = [GDTClock clockSnapshotInTheFuture:1];
  53. XCTAssertTrue([clock2 isAfter:clock1]);
  54. }
  55. /** Tests creating a snapshot in the future and comparing using isAfter: */
  56. - (void)testIsAfter {
  57. GDTClock *clock1 = [GDTClock clockSnapshotInTheFuture:123456];
  58. [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:5.0]];
  59. GDTClock *clock2 = [GDTClock snapshot];
  60. XCTAssertFalse([clock2 isAfter:clock1]);
  61. }
  62. @end