GDTCORTransportTest.m 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <GoogleDataTransport/GDTCORRegistrar.h>
  19. #import <GoogleDataTransport/GDTCORTransport.h>
  20. #import "GDTCORLibrary/Private/GDTCORTransport_Private.h"
  21. #import "GDTCORTests/Common/Fakes/GDTCORStorageFake.h"
  22. #import "GDTCORTests/Common/Fakes/GDTCORTransformerFake.h"
  23. #import "GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h"
  24. #import "GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
  25. @interface GDTCORTransportTest : GDTCORTestCase
  26. @end
  27. @implementation GDTCORTransportTest
  28. - (void)setUp {
  29. [super setUp];
  30. [[GDTCORRegistrar sharedInstance] registerStorage:[[GDTCORStorageFake alloc] init]
  31. target:kGDTCORTargetTest];
  32. }
  33. - (void)tearDown {
  34. [super tearDown];
  35. [[GDTCORRegistrar sharedInstance] reset];
  36. }
  37. /** Tests the default initializer. */
  38. - (void)testInit {
  39. XCTAssertNotNil([[GDTCORTransport alloc] initWithMappingID:@"1"
  40. transformers:nil
  41. target:kGDTCORTargetTest]);
  42. XCTAssertNil([[GDTCORTransport alloc] initWithMappingID:@""
  43. transformers:nil
  44. target:kGDTCORTargetTest]);
  45. }
  46. /** Tests sending a telemetry event. */
  47. - (void)testSendTelemetryEvent {
  48. GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"1"
  49. transformers:nil
  50. target:kGDTCORTargetTest];
  51. transport.transformerInstance = [[GDTCORTransformerFake alloc] init];
  52. GDTCOREvent *event = [transport eventForTransport];
  53. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  54. XCTestExpectation *writtenExpectation = [self expectationWithDescription:@"event written"];
  55. XCTAssertNoThrow([transport sendTelemetryEvent:event
  56. onComplete:^(BOOL wasWritten, NSError *_Nullable error) {
  57. XCTAssertTrue(wasWritten);
  58. XCTAssertNil(error);
  59. [writtenExpectation fulfill];
  60. }]);
  61. [self waitForExpectations:@[ writtenExpectation ] timeout:10.0];
  62. }
  63. /** Tests sending a data event. */
  64. - (void)testSendDataEvent {
  65. GDTCORTransport *transport = [[GDTCORTransport alloc] initWithMappingID:@"1"
  66. transformers:nil
  67. target:kGDTCORTargetTest];
  68. transport.transformerInstance = [[GDTCORTransformerFake alloc] init];
  69. GDTCOREvent *event = [transport eventForTransport];
  70. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  71. XCTestExpectation *writtenExpectation = [self expectationWithDescription:@"event written"];
  72. XCTAssertNoThrow([transport sendDataEvent:event
  73. onComplete:^(BOOL wasWritten, NSError *_Nullable error) {
  74. XCTAssertTrue(wasWritten);
  75. XCTAssertNil(error);
  76. [writtenExpectation fulfill];
  77. }]);
  78. [self waitForExpectations:@[ writtenExpectation ] timeout:10.0];
  79. }
  80. @end