GDTCOREventTest.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 <GoogleDataTransport/GDTCOREvent.h>
  18. #import "GDTCORLibrary/Private/GDTCOREvent_Private.h"
  19. @interface GDTCOREventTest : GDTCORTestCase
  20. @end
  21. @implementation GDTCOREventTest
  22. /** Tests the designated initializer. */
  23. - (void)testInit {
  24. XCTAssertNotNil([[GDTCOREvent alloc] initWithMappingID:@"1" target:1]);
  25. XCTAssertNil([[GDTCOREvent alloc] initWithMappingID:@"" target:1]);
  26. }
  27. /** Tests NSKeyedArchiver encoding and decoding. */
  28. - (void)testArchiving {
  29. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  30. GDTCORClock *clockSnapshot = [GDTCORClock snapshot];
  31. int64_t timeMillis = clockSnapshot.timeMillis;
  32. int64_t timezoneOffsetSeconds = clockSnapshot.timezoneOffsetSeconds;
  33. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testID" target:42];
  34. event.dataObjectTransportBytes = [@"someData" dataUsingEncoding:NSUTF8StringEncoding];
  35. event.qosTier = GDTCOREventQoSTelemetry;
  36. event.clockSnapshot = clockSnapshot;
  37. NSData *archiveData;
  38. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  39. archiveData = [NSKeyedArchiver archivedDataWithRootObject:event
  40. requiringSecureCoding:YES
  41. error:nil];
  42. } else {
  43. #if !TARGET_OS_MACCATALYST
  44. archiveData = [NSKeyedArchiver archivedDataWithRootObject:event];
  45. #endif
  46. }
  47. // To ensure that all the objects being retained by the original event are dealloc'd.
  48. event = nil;
  49. GDTCOREvent *decodedEvent;
  50. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  51. decodedEvent = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCOREvent class]
  52. fromData:archiveData
  53. error:nil];
  54. } else {
  55. #if !TARGET_OS_MACCATALYST
  56. decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
  57. #endif
  58. }
  59. XCTAssertEqualObjects(decodedEvent.mappingID, @"testID");
  60. XCTAssertEqual(decodedEvent.target, 42);
  61. XCTAssertEqualObjects(decodedEvent.dataObjectTransportBytes,
  62. [@"someData" dataUsingEncoding:NSUTF8StringEncoding]);
  63. XCTAssertEqual(decodedEvent.qosTier, GDTCOREventQoSTelemetry);
  64. XCTAssertEqual(decodedEvent.clockSnapshot.timeMillis, timeMillis);
  65. XCTAssertEqual(decodedEvent.clockSnapshot.timezoneOffsetSeconds, timezoneOffsetSeconds);
  66. }
  67. @end