@@ -74,11 +74,12 @@ - (void)setUp {
74
74
75
75
- (void )tearDown {
76
76
self.uploader = nil ;
77
+ [FIRApp resetApps ];
77
78
78
79
[super tearDown ];
79
80
}
80
81
81
- - (void )setupUploaderWithInstallations : (FIRMockInstallations *)installations {
82
+ - (void )setupUploaderWithInstallations : (FIRInstallations *)installations {
82
83
// Allow nil values only in tests
83
84
#pragma clang diagnostic push
84
85
#pragma clang diagnostic ignored "-Wnonnull"
@@ -95,17 +96,33 @@ - (void)setupUploaderWithInstallations:(FIRMockInstallations *)installations {
95
96
self.uploader = [[FIRCLSReportUploader alloc ] initWithManagerData: self .managerData];
96
97
}
97
98
99
+ - (NSString *)resourcePath {
100
+ #if SWIFT_PACKAGE
101
+ NSBundle *bundle = SWIFTPM_MODULE_BUNDLE;
102
+ return [bundle.resourcePath stringByAppendingPathComponent: @" Data" ];
103
+ #else
104
+ NSBundle *bundle = [NSBundle bundleForClass: [self class ]];
105
+ return bundle.resourcePath ;
106
+ #endif
107
+ }
108
+
109
+ - (FIRCLSInternalReport *)createReport {
110
+ NSString *path = [self .fileManager.activePath stringByAppendingPathComponent: @" pkg_uuid" ];
111
+ FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc ] initWithPath: path];
112
+ self.fileManager .moveItemAtPathResult = [NSNumber numberWithInt: 1 ];
113
+ return report;
114
+ }
115
+
98
116
#pragma mark - Tests
99
117
100
118
- (void )testPrepareReport {
101
- FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc ] initWithPath: [self packagePath ]];
102
- self.fileManager .moveItemAtPathResult = [NSNumber numberWithInt: 1 ];
119
+ FIRCLSInternalReport *report = [self createReport ];
103
120
104
121
XCTAssertNil (self.uploader .fiid );
105
122
106
123
[self .uploader prepareAndSubmitReport: report
107
124
dataCollectionToken: FIRCLSDataCollectionToken.validToken
108
- asUrgent: YES
125
+ asUrgent: NO
109
126
withProcessing: YES ];
110
127
111
128
XCTAssertEqual (self.uploader .fiid , TestFIID);
@@ -115,9 +132,64 @@ - (void)testPrepareReport {
115
132
[self .fileManager.moveItemAtPath_destDir containsString: self .fileManager.preparedPath]);
116
133
}
117
134
135
+ - (void )testPrepareReportOnMainThread {
136
+ NSString *pathToPlist =
137
+ [[self resourcePath ] stringByAppendingPathComponent: @" GoogleService-Info.plist" ];
138
+ FIROptions *options = [[FIROptions alloc ] initWithContentsOfFile: pathToPlist];
139
+
140
+ [FIRApp configureWithName: @" __FIRAPP_DEFAULT" options: options];
141
+ XCTAssertNotNil ([FIRApp defaultApp ], @" configureWithName must have been initialized" );
142
+
143
+ FIRInstallations *installations = [FIRInstallations installationsWithApp: [FIRApp defaultApp ]];
144
+ FIRCLSInternalReport *report = [self createReport ];
145
+ [self setupUploaderWithInstallations: installations];
146
+
147
+ /*
148
+ if a report is urgent report will be processed on the Main Thread
149
+ otherwise, it will be dispatched to a NSOperationQueue (see `FIRCLSExistingReportManager.m:230`)
150
+
151
+ This test checks if `prepareAndSubmitReport` finishes in a reasonable time.
152
+ */
153
+
154
+ NSOperationQueue *queue = [NSOperationQueue new ];
155
+
156
+ // target call will block the main thread, so we need a background thread
157
+ // that will wait on a semaphore for a timeout
158
+ dispatch_semaphore_t backgroundWaiter = dispatch_semaphore_create (0 );
159
+ [queue addOperationWithBlock: ^{
160
+ intptr_t result = dispatch_semaphore_wait (backgroundWaiter,
161
+ dispatch_time (DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC));
162
+ BOOL exitBecauseOfTimeout = result != 0 ;
163
+ XCTAssertFalse (exitBecauseOfTimeout, @" Main Thread was blocked for more than 1 second" );
164
+ }];
165
+
166
+ // Urgent (on the Main thread)
167
+ [self .uploader prepareAndSubmitReport: report
168
+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
169
+ asUrgent: YES
170
+ withProcessing: YES ];
171
+ dispatch_semaphore_signal (backgroundWaiter);
172
+
173
+ // Not urgent (on a background thread)
174
+ XCTestExpectation *expectation =
175
+ [self expectationWithDescription: @" wait for a preparation to complete" ];
176
+
177
+ [queue addOperationWithBlock: ^{
178
+ [self .uploader prepareAndSubmitReport: report
179
+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
180
+ asUrgent: YES
181
+ withProcessing: YES ];
182
+ [expectation fulfill ];
183
+ }];
184
+
185
+ [self waitForExpectationsWithTimeout: 1
186
+ handler: ^(NSError *_Nullable error) {
187
+ XCTAssertNil (error, @" expectations failed: %@ " , error);
188
+ }];
189
+ }
190
+
118
191
- (void )test_NilFIID_DoesNotCrash {
119
- FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc ] initWithPath: [self packagePath ]];
120
- self.fileManager .moveItemAtPathResult = [NSNumber numberWithInt: 1 ];
192
+ FIRCLSInternalReport *report = [self createReport ];
121
193
122
194
self.mockInstallations = [[FIRMockInstallations alloc ]
123
195
initWithError: [NSError errorWithDomain: @" TestDomain" code: -1 userInfo: nil ]];
0 commit comments