29
29
30
30
#import " GoogleDataTransport/GDTCORTests/Common/Categories/GDTCORRegistrar+Testing.h"
31
31
32
+ #import " GoogleDataTransport/GDTCORTests/Common/Fakes/GDTCORApplicationFake.h"
32
33
#import " GoogleDataTransport/GDTCORTests/Common/Fakes/GDTCORStorageFake.h"
33
34
34
35
@interface GDTCORTransformerTestNilingTransformer : NSObject <GDTCOREventTransformer>
35
-
36
36
@end
37
37
38
38
@implementation GDTCORTransformerTestNilingTransformer
@@ -44,7 +44,6 @@ - (GDTCOREvent *)transform:(GDTCOREvent *)eventEvent {
44
44
@end
45
45
46
46
@interface GDTCORTransformerTestNewEventTransformer : NSObject <GDTCOREventTransformer>
47
-
48
47
@end
49
48
50
49
@implementation GDTCORTransformerTestNewEventTransformer
@@ -57,50 +56,73 @@ - (GDTCOREvent *)transform:(GDTCOREvent *)eventEvent {
57
56
58
57
@interface GDTCORTransformerTest : GDTCORTestCase
59
58
59
+ @property (nonatomic ) GDTCORApplicationFake *fakeApplication;
60
+ @property (nonatomic ) GDTCORTransformer *transformer;
61
+
60
62
@end
61
63
62
64
@implementation GDTCORTransformerTest
63
65
64
66
- (void )setUp {
65
67
[super setUp ];
66
- dispatch_sync ([GDTCORTransformer sharedInstance ].eventWritingQueue , ^{
68
+
69
+ self.fakeApplication = [[GDTCORApplicationFake alloc ] init ];
70
+
71
+ self.transformer = [[GDTCORTransformer alloc ] initWithApplication: self .fakeApplication];
72
+ dispatch_sync (self.transformer .eventWritingQueue , ^{
67
73
[[GDTCORRegistrar sharedInstance ] registerStorage: [[GDTCORStorageFake alloc ] init ]
68
74
target: kGDTCORTargetTest ];
69
75
});
70
76
}
71
77
72
78
- (void )tearDown {
73
79
[super tearDown ];
74
- dispatch_sync ([GDTCORTransformer sharedInstance ] .eventWritingQueue , ^{
80
+ dispatch_sync (self. transformer .eventWritingQueue , ^{
75
81
[[GDTCORRegistrar sharedInstance ] reset ];
76
82
});
83
+ self.transformer = nil ;
84
+
85
+ self.fakeApplication .beginTaskHandler = nil ;
86
+ self.fakeApplication = nil ;
77
87
}
78
88
79
89
/* * Tests the default initializer. */
80
90
- (void )testInit {
81
- XCTAssertNotNil ([[GDTCORTransformer alloc ] init ]);
91
+ GDTCORTransformer *transformer = [[GDTCORTransformer alloc ] init ];
92
+ XCTAssertNotNil (transformer);
93
+ XCTAssertEqualObjects (transformer.application , [GDTCORApplication sharedApplication ]);
82
94
}
83
95
84
96
/* * Tests the pointer equality of result of the -sharedInstance method. */
85
97
- (void )testSharedInstance {
86
98
XCTAssertEqual ([GDTCORTransformer sharedInstance ], [GDTCORTransformer sharedInstance ]);
99
+ XCTAssertEqualObjects ([GDTCORTransformer sharedInstance ].application ,
100
+ [GDTCORApplication sharedApplication ]);
87
101
}
88
102
89
103
/* * Tests writing a event without a transformer. */
90
104
- (void )testWriteEventWithoutTransformers {
91
- GDTCORTransformer *transformer = [GDTCORTransformer sharedInstance ];
105
+ __auto_type bgTaskExpectations =
106
+ [self expectationsBackgroundTaskBeginAndEndWithName: @" GDTTransformer" ];
107
+
108
+ GDTCORTransformer *transformer = self.transformer ;
92
109
GDTCOREvent *event = [[GDTCOREvent alloc ] initWithMappingID: @" 1" target: kGDTCORTargetTest ];
93
110
event.dataObject = [[GDTCORDataObjectTesterSimple alloc ] init ];
94
111
XCTAssertNoThrow ([transformer transformEvent: event
95
112
withTransformers: nil
96
113
onComplete: ^(BOOL wasWritten, NSError *_Nullable error) {
97
114
XCTAssertTrue (wasWritten);
98
115
}]);
116
+
117
+ [self waitForExpectations: bgTaskExpectations timeout: 0.5 ];
99
118
}
100
119
101
120
/* * Tests writing a event with a transformer that nils out the event. */
102
121
- (void )testWriteEventWithTransformersThatNilTheEvent {
103
- GDTCORTransformer *transformer = [GDTCORTransformer sharedInstance ];
122
+ __auto_type bgTaskExpectations =
123
+ [self expectationsBackgroundTaskBeginAndEndWithName: @" GDTTransformer" ];
124
+
125
+ GDTCORTransformer *transformer = self.transformer ;
104
126
GDTCOREvent *event = [[GDTCOREvent alloc ] initWithMappingID: @" 2" target: kGDTCORTargetTest ];
105
127
event.dataObject = [[GDTCORDataObjectTesterSimple alloc ] init ];
106
128
NSArray <id <GDTCOREventTransformer>> *transformers =
@@ -110,11 +132,16 @@ - (void)testWriteEventWithTransformersThatNilTheEvent {
110
132
onComplete: ^(BOOL wasWritten, NSError *_Nullable error) {
111
133
XCTAssertFalse (wasWritten);
112
134
}]);
135
+
136
+ [self waitForExpectations: bgTaskExpectations timeout: 0.5 ];
113
137
}
114
138
115
139
/* * Tests writing a event with a transformer that creates a new event. */
116
140
- (void )testWriteEventWithTransformersThatCreateANewEvent {
117
- GDTCORTransformer *transformer = [GDTCORTransformer sharedInstance ];
141
+ __auto_type bgTaskExpectations =
142
+ [self expectationsBackgroundTaskBeginAndEndWithName: @" GDTTransformer" ];
143
+
144
+ GDTCORTransformer *transformer = self.transformer ;
118
145
GDTCOREvent *event = [[GDTCOREvent alloc ] initWithMappingID: @" 2" target: kGDTCORTargetTest ];
119
146
event.dataObject = [[GDTCORDataObjectTesterSimple alloc ] init ];
120
147
NSArray <id <GDTCOREventTransformer>> *transformers =
@@ -125,6 +152,41 @@ - (void)testWriteEventWithTransformersThatCreateANewEvent {
125
152
XCTAssertTrue (wasWritten);
126
153
XCTAssertNil (error);
127
154
}]);
155
+
156
+ [self waitForExpectations: bgTaskExpectations timeout: 0.5 ];
157
+ }
158
+
159
+ #pragma mark - Helpers
160
+
161
+ /* * Sets GDTCORApplicationFake handlers to expect the begin and the end of a background task with
162
+ * the specified name.
163
+ * @return An array with the task begin and end XCTestExpectation.
164
+ */
165
+ - (NSArray <XCTestExpectation *> *)expectationsBackgroundTaskBeginAndEndWithName :
166
+ (NSString *)expectedName {
167
+ XCTestExpectation *beginExpectation = [self expectationWithDescription: @" Background task begin" ];
168
+ XCTestExpectation *endExpectation = [self expectationWithDescription: @" Background task end" ];
169
+
170
+ GDTCORBackgroundIdentifier taskID = arc4random ();
171
+
172
+ __auto_type __weak weakSelf = self;
173
+
174
+ self.fakeApplication .beginTaskHandler =
175
+ ^GDTCORBackgroundIdentifier (NSString *_Nonnull name, dispatch_block_t _Nonnull handler) {
176
+ __auto_type self = weakSelf;
177
+ XCTAssertEqualObjects (expectedName, name);
178
+
179
+ [beginExpectation fulfill ];
180
+ return taskID;
181
+ };
182
+
183
+ self.fakeApplication .endTaskHandler = ^(GDTCORBackgroundIdentifier endTaskID) {
184
+ __auto_type self = weakSelf;
185
+ XCTAssert (endTaskID == taskID);
186
+ [endExpectation fulfill ];
187
+ };
188
+
189
+ return @[ beginExpectation, endExpectation ];
128
190
}
129
191
130
192
@end
0 commit comments