GDTCORTransformerTest.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "GoogleDataTransport/GDTCORTests/Unit/GDTCORTestCase.h"
  17. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREventTransformer.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORFlatFileStorage.h"
  20. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
  21. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer.h"
  22. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  23. #import "GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCORAssertHelper.h"
  24. #import "GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
  25. #import "GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h"
  26. #import "GoogleDataTransport/GDTCORTests/Common/Fakes/GDTCORApplicationFake.h"
  27. #import "GoogleDataTransport/GDTCORTests/Common/Fakes/GDTCORStorageFake.h"
  28. @interface GDTCORTransformerTestNilingTransformer : NSObject <GDTCOREventTransformer>
  29. @end
  30. @implementation GDTCORTransformerTestNilingTransformer
  31. - (GDTCOREvent *)transform:(GDTCOREvent *)eventEvent {
  32. return nil;
  33. }
  34. @end
  35. @interface GDTCORTransformerTestNewEventTransformer : NSObject <GDTCOREventTransformer>
  36. @end
  37. @implementation GDTCORTransformerTestNewEventTransformer
  38. - (GDTCOREvent *)transform:(GDTCOREvent *)eventEvent {
  39. return [[GDTCOREvent alloc] initWithMappingID:@"new" target:kGDTCORTargetTest];
  40. }
  41. @end
  42. @interface GDTCORTransformerTest : GDTCORTestCase
  43. @property(nonatomic) GDTCORApplicationFake *fakeApplication;
  44. @property(nonatomic) GDTCORTransformer *transformer;
  45. @end
  46. @implementation GDTCORTransformerTest
  47. - (void)setUp {
  48. [super setUp];
  49. self.fakeApplication = [[GDTCORApplicationFake alloc] init];
  50. self.transformer = [[GDTCORTransformer alloc] initWithApplication:self.fakeApplication];
  51. dispatch_sync(self.transformer.eventWritingQueue, ^{
  52. [[GDTCORRegistrar sharedInstance] registerStorage:[[GDTCORStorageFake alloc] init]
  53. target:kGDTCORTargetTest];
  54. });
  55. }
  56. - (void)tearDown {
  57. [super tearDown];
  58. dispatch_sync(self.transformer.eventWritingQueue, ^{
  59. [[GDTCORRegistrar sharedInstance] reset];
  60. });
  61. self.transformer = nil;
  62. self.fakeApplication.beginTaskHandler = nil;
  63. self.fakeApplication = nil;
  64. }
  65. /** Tests the default initializer. */
  66. - (void)testInit {
  67. GDTCORTransformer *transformer = [[GDTCORTransformer alloc] init];
  68. XCTAssertNotNil(transformer);
  69. XCTAssertEqualObjects(transformer.application, [GDTCORApplication sharedApplication]);
  70. }
  71. /** Tests the pointer equality of result of the -sharedInstance method. */
  72. - (void)testSharedInstance {
  73. XCTAssertEqual([GDTCORTransformer sharedInstance], [GDTCORTransformer sharedInstance]);
  74. XCTAssertEqualObjects([GDTCORTransformer sharedInstance].application,
  75. [GDTCORApplication sharedApplication]);
  76. }
  77. /** Tests writing a event without a transformer. */
  78. - (void)testWriteEventWithoutTransformers {
  79. __auto_type bgTaskExpectations =
  80. [self expectationsBackgroundTaskBeginAndEndWithName:@"GDTTransformer"];
  81. GDTCORTransformer *transformer = self.transformer;
  82. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"1" target:kGDTCORTargetTest];
  83. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  84. XCTAssertNoThrow([transformer transformEvent:event
  85. withTransformers:nil
  86. onComplete:^(BOOL wasWritten, NSError *_Nullable error) {
  87. XCTAssertTrue(wasWritten);
  88. }]);
  89. [self waitForExpectations:bgTaskExpectations timeout:0.5];
  90. }
  91. /** Tests writing a event with a transformer that nils out the event. */
  92. - (void)testWriteEventWithTransformersThatNilTheEvent {
  93. __auto_type bgTaskExpectations =
  94. [self expectationsBackgroundTaskBeginAndEndWithName:@"GDTTransformer"];
  95. GDTCORTransformer *transformer = self.transformer;
  96. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"2" target:kGDTCORTargetTest];
  97. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  98. NSArray<id<GDTCOREventTransformer>> *transformers =
  99. @[ [[GDTCORTransformerTestNilingTransformer alloc] init] ];
  100. XCTAssertNoThrow([transformer transformEvent:event
  101. withTransformers:transformers
  102. onComplete:^(BOOL wasWritten, NSError *_Nullable error) {
  103. XCTAssertFalse(wasWritten);
  104. }]);
  105. [self waitForExpectations:bgTaskExpectations timeout:0.5];
  106. }
  107. /** Tests writing a event with a transformer that creates a new event. */
  108. - (void)testWriteEventWithTransformersThatCreateANewEvent {
  109. __auto_type bgTaskExpectations =
  110. [self expectationsBackgroundTaskBeginAndEndWithName:@"GDTTransformer"];
  111. GDTCORTransformer *transformer = self.transformer;
  112. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"2" target:kGDTCORTargetTest];
  113. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  114. NSArray<id<GDTCOREventTransformer>> *transformers =
  115. @[ [[GDTCORTransformerTestNewEventTransformer alloc] init] ];
  116. XCTAssertNoThrow([transformer transformEvent:event
  117. withTransformers:transformers
  118. onComplete:^(BOOL wasWritten, NSError *_Nullable error) {
  119. XCTAssertTrue(wasWritten);
  120. XCTAssertNil(error);
  121. }]);
  122. [self waitForExpectations:bgTaskExpectations timeout:0.5];
  123. }
  124. #pragma mark - Helpers
  125. /** Sets GDTCORApplicationFake handlers to expect the begin and the end of a background task with
  126. * the specified name.
  127. * @return An array with the task begin and end XCTestExpectation.
  128. */
  129. - (NSArray<XCTestExpectation *> *)expectationsBackgroundTaskBeginAndEndWithName:
  130. (NSString *)expectedName {
  131. XCTestExpectation *beginExpectation = [self expectationWithDescription:@"Background task begin"];
  132. XCTestExpectation *endExpectation = [self expectationWithDescription:@"Background task end"];
  133. GDTCORBackgroundIdentifier taskID = arc4random();
  134. __auto_type __weak weakSelf = self;
  135. self.fakeApplication.beginTaskHandler =
  136. ^GDTCORBackgroundIdentifier(NSString *_Nonnull name, dispatch_block_t _Nonnull handler) {
  137. __unused __auto_type self = weakSelf;
  138. XCTAssertEqualObjects(expectedName, name);
  139. [beginExpectation fulfill];
  140. return taskID;
  141. };
  142. self.fakeApplication.endTaskHandler = ^(GDTCORBackgroundIdentifier endTaskID) {
  143. __unused __auto_type self = weakSelf;
  144. XCTAssert(endTaskID == taskID);
  145. [endExpectation fulfill];
  146. };
  147. return @[ beginExpectation, endExpectation ];
  148. }
  149. @end