Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ @interface FPRTraceBackgroundActivityTracker ()

@property(nonatomic, readwrite) FPRTraceState traceBackgroundState;

- (void)registerNotificationObservers;

@end

@implementation FPRTraceBackgroundActivityTracker
Expand All @@ -35,21 +37,26 @@ - (instancetype)init {
} else {
_traceBackgroundState = FPRTraceStateForegroundOnly;
}
__strong typeof(self) strongSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
[strongSelf registerNotificationObservers];
});
}
return self;
}

- (void)registerNotificationObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]];
}

- (void)dealloc {
// Remove all the notification observers registered.
[[NSNotificationCenter defaultCenter] removeObserver:self];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,82 @@ - (void)testBackgroundTracking {
}];
}

/** Tests that synchronous observer registration works correctly and observers are immediately
* available. */
- (void)testObservers_synchronousRegistrationAddsObserver {
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
XCTAssertNotNil(tracker);

[notificationCenter postNotificationName:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);

tracker = nil;
XCTAssertNil(tracker);
XCTAssertNoThrow([notificationCenter postNotificationName:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]]);
XCTAssertNoThrow([notificationCenter
postNotificationName:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]]);
}

/** Tests rapid creation and deallocation to verify race condition. */
- (void)testRapidCreationAndDeallocation_noRaceCondition {
for (int i = 0; i < 100; i++) {
@autoreleasepool {
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
XCTAssertNotNil(tracker);

[[NSNotificationCenter defaultCenter]
postNotificationName:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
}
}

XCTAssertNoThrow([[NSNotificationCenter defaultCenter]
postNotificationName:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]]);
XCTAssertNoThrow([[NSNotificationCenter defaultCenter]
postNotificationName:UIApplicationDidEnterBackgroundNotification
object:[UIApplication sharedApplication]]);
}

/** Tests that observers are registered immediately after init on main thread. */
- (void)testObservers_immediateRegistrationOnMainThread {
XCTAssertTrue([NSThread isMainThread]);

FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];

[[NSNotificationCenter defaultCenter]
postNotificationName:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];

XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);
}
Comment on lines +106 to +116
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

This test case, testObservers_immediateRegistrationOnMainThread, appears to be redundant. Its logic is already covered in the first half of the testObservers_synchronousRegistrationAddsObserver test (lines 66-73). To improve the conciseness of the test suite and reduce future maintenance, consider removing this test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:We usually don't want to duplicate code if possible, if possible try to make it a single tests, so we don't have to maintain extra tests.


/** Tests observer registration when created from background thread. */
- (void)testObservers_registrationFromBackgroundThread {
XCTestExpectation *expectation = [self expectationWithDescription:@"Background thread creation"];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
FPRTraceBackgroundActivityTracker *tracker = [[FPRTraceBackgroundActivityTracker alloc] init];
XCTAssertNotNil(tracker);

dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]
postNotificationName:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];

XCTAssertEqual(tracker.traceBackgroundState, FPRTraceStateForegroundOnly);
[expectation fulfill];
});
});

[self waitForExpectationsWithTimeout:5.0
handler:^(NSError *error) {
XCTAssertNil(error, @"Test timed out");
}];
}

@end