Skip to content

Commit 0a9fadd

Browse files
committed
Unit test for inactive state
1 parent 717fa41 commit 0a9fadd

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ NSString * serverUrlWithPath(NSString *path);
4848
+ (void)beforeEachTest:(XCTestCase *)testCase;
4949
+ (void)clearStateForAppRestart:(XCTestCase *)testCase;
5050
+ (UNNotificationResponse*)createBasiciOSNotificationResponseWithPayload:(NSDictionary*)userInfo;
51+
+ (UNNotification *)createBasiciOSNotificationWithPayload:(NSDictionary *)userInfo;
5152
+ (void)answerNotificationPrompt:(BOOL)accept;
5253
+ (void)setCurrentNotificationPermission:(BOOL)accepted;
5354
+ (void)receiveNotification:(NSString*)notificationId wasOpened:(BOOL)opened;
54-
+ (void)handleNotificationReceived:(NSString*)notificationId messageDict:(NSDictionary*)messageDict wasOpened:(BOOL)opened;
55+
+ (void)handleNotificationReceived:(NSDictionary*)messageDict wasOpened:(BOOL)opened;
5556
+ (XCTestCase*)currentXCTestCase;
5657
@end
5758

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,21 @@ + (UNNotificationResponse*)createBasiciOSNotificationResponseWithPayload:(NSDict
111111
// Normal tap on notification
112112
[notifResponse setValue:@"com.apple.UNNotificationDefaultActionIdentifier" forKeyPath:@"actionIdentifier"];
113113

114+
[notifResponse setValue:[self createBasiciOSNotificationWithPayload:userInfo] forKeyPath:@"notification"];
115+
116+
return notifResponse;
117+
}
118+
119+
+ (UNNotification *)createBasiciOSNotificationWithPayload:(NSDictionary *)userInfo {
114120
UNNotificationContent *unNotifContent = [UNNotificationContent alloc];
115121
UNNotification *unNotif = [UNNotification alloc];
116122
UNNotificationRequest *unNotifRequqest = [UNNotificationRequest alloc];
117123
// Set as remote push type
118124
[unNotifRequqest setValue:[UNPushNotificationTrigger alloc] forKey:@"trigger"];
119-
120-
[unNotif setValue:unNotifRequqest forKeyPath:@"request"];
121-
[notifResponse setValue:unNotif forKeyPath:@"notification"];
122-
[unNotifRequqest setValue:unNotifContent forKeyPath:@"content"];
123125
[unNotifContent setValue:userInfo forKey:@"userInfo"];
124-
125-
return notifResponse;
126+
[unNotifRequqest setValue:unNotifContent forKeyPath:@"content"];
127+
[unNotif setValue:unNotifRequqest forKeyPath:@"request"];
128+
return unNotif;
126129
}
127130

128131
+ (void)clearStateForAppRestart:(XCTestCase *)testCase {

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,42 @@ - (void)testNotificationOpenFromButtonPressWithNewformat {
11861186
XCTAssertEqual(OneSignalClientOverrider.networkRequestCount, 3);
11871187
}
11881188

1189+
// Testing receiving a notification while the app is in the foreground but inactive.
1190+
// Received should be called but opened should not be called
1191+
- (void)testNotificationReceivedWhileAppInactive {
1192+
__block BOOL openedWasFired = false;
1193+
__block BOOL receivedWasFired = false;
1194+
1195+
[OneSignal initWithLaunchOptions:nil appId:@"b2f7f966-d8cc-11e4-bed1-df8f05be55ba" handleNotificationReceived:^(OSNotification *notification) {
1196+
receivedWasFired = true;
1197+
} handleNotificationAction:^(OSNotificationOpenedResult *result) {
1198+
openedWasFired = true;
1199+
} settings:nil];
1200+
1201+
[UnitTestCommonMethods runBackgroundThreads];
1202+
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateInactive;
1203+
1204+
id userInfo = @{@"aps": @{
1205+
@"mutable-content": @1,
1206+
@"alert": @"Message Body"
1207+
},
1208+
@"os_data": @{
1209+
@"i": @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
1210+
@"buttons": @[@{@"i": @"id1", @"n": @"text1"}],
1211+
}};
1212+
1213+
UNNotification *notif = [UnitTestCommonMethods createBasiciOSNotificationWithPayload:userInfo];
1214+
1215+
UNUserNotificationCenter *notifCenter = [UNUserNotificationCenter currentNotificationCenter];
1216+
id notifCenterDelegate = notifCenter.delegate;
1217+
1218+
[notifCenterDelegate userNotificationCenter:notifCenter willPresentNotification:notif withCompletionHandler:^(UNNotificationPresentationOptions options) {}];
1219+
1220+
1221+
XCTAssertEqual(openedWasFired, false);
1222+
XCTAssertEqual(receivedWasFired, true);
1223+
}
1224+
11891225
// Testing iOS 10 - 2.4.0+ button fromat - with os_data aps payload format
11901226
- (void)notificationAlertButtonsDisplayWithFormat:(NSDictionary *)userInfo {
11911227
[[OneSignalDialogController sharedInstance] clearQueue];

0 commit comments

Comments
 (0)