Skip to content

Commit 65c52d2

Browse files
committed
Fix the failing tests.
1 parent e875ee1 commit 65c52d2

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

FirebasePerformance/Tests/Unit/Timer/FIRTraceTest.m

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,15 +427,28 @@ - (void)testValidTraceWithStageAndMetrics {
427427

428428
/** Validates the value of background state when the app is backgrounded. */
429429
- (void)testValidTraceWithBackgrounding {
430+
XCTestExpectation *expectation = [self expectationWithDescription:@"Application state change"];
430431
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
431432
FIRTrace *trace = [[FIRTrace alloc] initWithName:@"Random"];
432433
[trace start];
433-
[defaultCenter postNotificationName:UIApplicationDidEnterBackgroundNotification
434-
object:[UIApplication sharedApplication]];
435-
[defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
436-
object:[UIApplication sharedApplication]];
437-
XCTAssertEqual(trace.backgroundTraceState, FPRTraceStateBackgroundAndForeground);
438-
[trace stop];
434+
dispatch_async(dispatch_get_main_queue(), ^{
435+
[defaultCenter postNotificationName:UIApplicationDidEnterBackgroundNotification
436+
object:[UIApplication sharedApplication]];
437+
[defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
438+
object:[UIApplication sharedApplication]];
439+
[expectation fulfill];
440+
});
441+
442+
[self waitForExpectationsWithTimeout:5.0
443+
handler:^(NSError *_Nullable error) {
444+
if (error) {
445+
XCTFail(@"Expectation failed with error: %@", error);
446+
} else {
447+
XCTAssertEqual(trace.backgroundTraceState,
448+
FPRTraceStateBackgroundAndForeground);
449+
[trace stop];
450+
}
451+
}];
439452
}
440453

441454
/** Validates the value of background state when trace is not started. */
@@ -452,15 +465,28 @@ - (void)testValidTraceWithoutStart {
452465

453466
/** Validates the value of background state is available after trace is stopped. */
454467
- (void)testBackgroundStateAfterTraceStop {
468+
XCTestExpectation *expectation = [self expectationWithDescription:@"Application state change"];
469+
455470
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
456471
FIRTrace *trace = [[FIRTrace alloc] initWithName:@"Random"];
457472
[trace start];
458-
[defaultCenter postNotificationName:UIApplicationDidEnterBackgroundNotification
459-
object:[UIApplication sharedApplication]];
460-
[defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
461-
object:[UIApplication sharedApplication]];
462-
[trace stop];
463-
XCTAssertEqual(trace.backgroundTraceState, FPRTraceStateBackgroundAndForeground);
473+
dispatch_async(dispatch_get_main_queue(), ^{
474+
[defaultCenter postNotificationName:UIApplicationDidEnterBackgroundNotification
475+
object:[UIApplication sharedApplication]];
476+
[defaultCenter postNotificationName:UIApplicationDidBecomeActiveNotification
477+
object:[UIApplication sharedApplication]];
478+
});
479+
480+
[self waitForExpectationsWithTimeout:5.0
481+
handler:^(NSError *_Nullable error) {
482+
if (error) {
483+
XCTFail(@"Expectation failed with error: %@", error);
484+
} else {
485+
XCTAssertEqual(trace.backgroundTraceState,
486+
FPRTraceStateBackgroundAndForeground);
487+
[trace stop];
488+
}
489+
}];
464490
}
465491

466492
/** Validates that stages do not have any valid background state. */

0 commit comments

Comments
 (0)