GDTEventTest.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <GoogleDataTransport/GDTEvent.h>
  18. #import "GDTLibrary/Private/GDTEvent_Private.h"
  19. @interface GDTEventTest : GDTTestCase
  20. @end
  21. @implementation GDTEventTest
  22. /** Tests the designated initializer. */
  23. - (void)testInit {
  24. XCTAssertNotNil([[GDTEvent alloc] initWithMappingID:@"1" target:1]);
  25. XCTAssertThrows([[GDTEvent alloc] initWithMappingID:@"" target:1]);
  26. }
  27. /** Tests NSKeyedArchiver encoding and decoding. */
  28. - (void)testArchiving {
  29. XCTAssertTrue([GDTEvent supportsSecureCoding]);
  30. GDTClock *clockSnapshot = [GDTClock snapshot];
  31. int64_t timeMillis = clockSnapshot.timeMillis;
  32. int64_t timezoneOffsetSeconds = clockSnapshot.timezoneOffsetSeconds;
  33. GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"testID" target:42];
  34. event.dataObjectTransportBytes = [@"someData" dataUsingEncoding:NSUTF8StringEncoding];
  35. event.qosTier = GDTEventQoSTelemetry;
  36. event.clockSnapshot = clockSnapshot;
  37. NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:event];
  38. // To ensure that all the objects being retained by the original event are dealloc'd.
  39. event = nil;
  40. GDTEvent *decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
  41. XCTAssertEqualObjects(decodedEvent.mappingID, @"testID");
  42. XCTAssertEqual(decodedEvent.target, 42);
  43. XCTAssertEqualObjects(decodedEvent.dataObjectTransportBytes,
  44. [@"someData" dataUsingEncoding:NSUTF8StringEncoding]);
  45. XCTAssertEqual(decodedEvent.qosTier, GDTEventQoSTelemetry);
  46. XCTAssertEqual(decodedEvent.clockSnapshot.timeMillis, timeMillis);
  47. XCTAssertEqual(decodedEvent.clockSnapshot.timezoneOffsetSeconds, timezoneOffsetSeconds);
  48. }
  49. @end