@@ -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,29 @@ - (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+ [expectation fulfill ];
479+ });
480+
481+ [self waitForExpectationsWithTimeout: 5.0
482+ handler: ^(NSError *_Nullable error) {
483+ if (error) {
484+ XCTFail (@" Expectation failed with error: %@ " , error);
485+ } else {
486+ [trace stop ];
487+ XCTAssertEqual (trace.backgroundTraceState ,
488+ FPRTraceStateBackgroundAndForeground);
489+ }
490+ }];
464491}
465492
466493/* * Validates that stages do not have any valid background state. */
0 commit comments