@@ -61,4 +61,69 @@ - (void)testBackgroundTracking {
61
61
}];
62
62
}
63
63
64
+ /* * Tests that synchronous observer registration works correctly and observers are immediately
65
+ * available. */
66
+ - (void )testObservers_synchronousRegistrationAddsObserver {
67
+ NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter ];
68
+ FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc ] init ];
69
+ XCTAssertNotNil (tracker);
70
+
71
+ [notificationCenter postNotificationName: UIApplicationDidBecomeActiveNotification
72
+ object: [UIApplication sharedApplication ]];
73
+ XCTAssertEqual (tracker.traceBackgroundState , FPRTraceStateForegroundOnly);
74
+
75
+ tracker = nil ;
76
+ XCTAssertNil (tracker);
77
+ XCTAssertNoThrow ([notificationCenter postNotificationName: UIApplicationDidBecomeActiveNotification
78
+ object: [UIApplication sharedApplication ]]);
79
+ XCTAssertNoThrow ([notificationCenter
80
+ postNotificationName: UIApplicationDidEnterBackgroundNotification
81
+ object: [UIApplication sharedApplication ]]);
82
+ }
83
+
84
+ /* * Tests rapid creation and deallocation to verify race condition. */
85
+ - (void )testRapidCreationAndDeallocation_noRaceCondition {
86
+ for (int i = 0 ; i < 100 ; i++) {
87
+ @autoreleasepool {
88
+ FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc ] init ];
89
+ XCTAssertNotNil (tracker);
90
+
91
+ [[NSNotificationCenter defaultCenter ]
92
+ postNotificationName: UIApplicationDidBecomeActiveNotification
93
+ object: [UIApplication sharedApplication ]];
94
+ }
95
+ }
96
+
97
+ XCTAssertNoThrow ([[NSNotificationCenter defaultCenter ]
98
+ postNotificationName: UIApplicationDidBecomeActiveNotification
99
+ object: [UIApplication sharedApplication ]]);
100
+ XCTAssertNoThrow ([[NSNotificationCenter defaultCenter ]
101
+ postNotificationName: UIApplicationDidEnterBackgroundNotification
102
+ object: [UIApplication sharedApplication ]]);
103
+ }
104
+
105
+ /* * Tests observer registration when created from background thread. */
106
+ - (void )testObservers_registrationFromBackgroundThread {
107
+ XCTestExpectation *expectation = [self expectationWithDescription: @" Background thread creation" ];
108
+
109
+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
110
+ FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc ] init ];
111
+ XCTAssertNotNil (tracker);
112
+
113
+ dispatch_async (dispatch_get_main_queue (), ^{
114
+ [[NSNotificationCenter defaultCenter ]
115
+ postNotificationName: UIApplicationDidBecomeActiveNotification
116
+ object: [UIApplication sharedApplication ]];
117
+
118
+ XCTAssertEqual (tracker.traceBackgroundState , FPRTraceStateForegroundOnly);
119
+ [expectation fulfill ];
120
+ });
121
+ });
122
+
123
+ [self waitForExpectationsWithTimeout: 5.0
124
+ handler: ^(NSError *error) {
125
+ XCTAssertNil (error, @" Test timed out" );
126
+ }];
127
+ }
128
+
64
129
@end
0 commit comments