Skip to content

Commit add0227

Browse files
Nightsd01jkasten2
authored andcommitted
Add Timeout Test
• Adds a new test case to make sure that if a developer implements the `OSNotificationDisplayTypeDelegate`, and their `willPresentInFocusNotificationWithPayload:` method gets called but they somehow don't call the callback, this test makes sure the SDK correctly waits for a while but then times out and uses the default display type • Fixes the test project's AppDelegate to fix the delegate method signature
1 parent 0acb014 commit add0227

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

iOS_SDK/OneSignalDevApp/OneSignalDevApp/AppDelegate.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,12 @@ -(void)onOSEmailSubscriptionChanged:(OSEmailSubscriptionStateChanges *)stateChan
9999

100100
}
101101

102-
- (void)willPresentInFocusNotificationWithPayload:(OSNotificationPayload *)payload withDefaultDisplayType:(OSNotificationDisplayType)displayType withCompletion:(OSNotificationDisplayTypeResponse)completion {
102+
- (void)willPresentInFocusNotificationWithPayload:(OSNotificationPayload *)payload
103+
withCompletion:(OSNotificationDisplayTypeResponse)completion {
103104
if (payload.additionalData[@"overrideDisplayType"]) {
104105
OSNotificationDisplayType newType = (OSNotificationDisplayType)[payload.additionalData[@"overrideDisplayType"] intValue];
105106

106107
completion(newType);
107-
} else {
108-
completion(displayType);
109108
}
110109
}
111110

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,7 @@ - (void)testNotificationWithButtonsRegistersUniqueCategory {
22812281
XCTAssertEqualObjects(content.categoryIdentifier, @"__onesignal__dynamic__b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
22822282
}
22832283

2284-
- (NSDictionary *)setUpNotificationDisplayTypeTestWithDummyDelegate:(DummyNotificationDisplayTypeDelegate *)dummyDelegate withDisplayType:(OSNotificationDisplayType)displayType {
2284+
- (NSDictionary *)setUpNotificationDisplayTypeTestWithDummyDelegate:(DummyNotificationDisplayTypeDelegate *)dummyDelegate withDisplayType:(OSNotificationDisplayType)displayType withNotificationReceivedBlock:(OSHandleNotificationReceivedBlock)receivedBlock {
22852285
id userInfo = @{@"aps": @{
22862286
@"mutable-content": @1,
22872287
@"alert": @{@"body": @"Message Body", @"title": @"title"},
@@ -2292,15 +2292,16 @@ - (NSDictionary *)setUpNotificationDisplayTypeTestWithDummyDelegate:(DummyNotifi
22922292
@"buttons": @[@{@"i": @"id1", @"n": @"text1"}],
22932293
}};
22942294

2295-
[OneSignal initWithLaunchOptions:nil appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationAction:nil];
2295+
[OneSignal initWithLaunchOptions:@{} appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationReceived:receivedBlock handleNotificationAction:nil settings:@{}];
2296+
2297+
[UnitTestCommonMethods runBackgroundThreads];
22962298

22972299
[OneSignal setNotificationDisplayTypeDelegate:dummyDelegate];
22982300

22992301
[OneSignal setInFocusDisplayType:displayType];
23002302

23012303
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateActive;
23022304

2303-
[UnitTestCommonMethods resumeApp];
23042305
[UnitTestCommonMethods runBackgroundThreads];
23052306

23062307
return userInfo;
@@ -2315,7 +2316,8 @@ - (void)testOverrideNotificationDisplayType {
23152316
dummyDelegate.shouldFireCompletionBlock = true;
23162317

23172318
let userInfo = [self setUpNotificationDisplayTypeTestWithDummyDelegate:dummyDelegate
2318-
withDisplayType:OSNotificationDisplayTypeNone];
2319+
withDisplayType:OSNotificationDisplayTypeNone
2320+
withNotificationReceivedBlock:nil];
23192321

23202322
id notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:userInfo];
23212323
[notifResponse setValue:@"id1" forKeyPath:@"actionIdentifier"];
@@ -2332,4 +2334,45 @@ - (void)testOverrideNotificationDisplayType {
23322334
XCTAssertEqual(dummyDelegate.numberOfCalls, 1);
23332335
}
23342336

2337+
// If the OSNotificationDisplayTypeDelegate never fires its completion block, the SDK should
2338+
// time out after a period of time and use the existing default inFocusDisplayType
2339+
// If we set it using OneSignal.setInFocusDisplayType(alert) we can make sure that the
2340+
// timeout logic completed correctly by making sure the dummy notification was displayed as an alert
2341+
- (void)testTimeoutOverrideNotificationDisplayType {
2342+
let dummyDelegate = [DummyNotificationDisplayTypeDelegate new];
2343+
2344+
dummyDelegate.overrideDisplayType = OSNotificationDisplayTypeNone;
2345+
dummyDelegate.shouldFireCompletionBlock = false;
2346+
2347+
let expectation = [self expectationWithDescription:@"wait_for_timeout"];
2348+
expectation.expectedFulfillmentCount = 1;
2349+
2350+
let receivedBlock = ^(OSNotification *notification) {
2351+
[expectation fulfill];
2352+
};
2353+
2354+
let userInfo = [self setUpNotificationDisplayTypeTestWithDummyDelegate:dummyDelegate
2355+
withDisplayType:OSNotificationDisplayTypeInAppAlert
2356+
withNotificationReceivedBlock:receivedBlock];
2357+
2358+
id notifResponse = [UnitTestCommonMethods createBasiciOSNotificationResponseWithPayload:userInfo];
2359+
[notifResponse setValue:@"id1" forKeyPath:@"actionIdentifier"];
2360+
2361+
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
2362+
id notifCenterDelegate = notifCenter.delegate;
2363+
[notifCenterDelegate userNotificationCenter:notifCenter
2364+
willPresentNotification:[notifResponse notification]
2365+
withCompletionHandler:^(UNNotificationPresentationOptions options) {}];
2366+
2367+
[UnitTestCommonMethods runBackgroundThreads];
2368+
2369+
[self waitForExpectationsWithTimeout:1.0 handler:^(NSError * _Nullable error) {
2370+
// by this point, the SDK will have timed out waiting for the display delegate
2371+
// and should continue handling the notification with the default display type
2372+
XCTAssertEqual(UIAlertViewOverrider.uiAlertButtonArrayCount, 1);
2373+
2374+
XCTAssertEqual(dummyDelegate.numberOfCalls, 1);
2375+
}];
2376+
}
2377+
23352378
@end

0 commit comments

Comments
 (0)