Skip to content

Commit 6b069db

Browse files
committed
Reverting activity trampolining tests
While this commit isn't a git revert most of the changes are undoing the following commit: 5f72750 Additionally this fixes tests due to the dropped activity param from OneSignal_handleNotificationOpen.
1 parent 4c01f3e commit 6b069db

File tree

4 files changed

+164
-79
lines changed

4 files changed

+164
-79
lines changed

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,49 +1142,25 @@ public void shouldUseCorrectActivityForAndroid23Plus() throws Exception {
11421142
threadAndTaskWait();
11431143

11441144
Intent[] intents = lastNotificationIntents();
1145-
assertEquals("android.intent.action.MAIN", intents[0].getAction());
11461145
assertEquals(
11471146
com.onesignal.NotificationOpenedReceiver.class.getName(),
1148-
intents[1].getComponent().getClassName()
1147+
intents[0].getComponent().getClassName()
11491148
);
1150-
assertEquals(2, intents.length);
1151-
}
1152-
1153-
@Test
1154-
@Config(shadows = { ShadowGenerateNotification.class })
1155-
public void shouldSetContentIntentForLaunchURL() throws Exception {
1156-
generateNotificationWithLaunchURL();
1157-
1158-
Intent[] intents = lastNotificationIntents();
1159-
assertEquals(2, intents.length);
1160-
Intent intentLaunchURL = intents[0];
1161-
assertEquals("android.intent.action.VIEW", intentLaunchURL.getAction());
1162-
assertEquals("https://google.com", intentLaunchURL.getData().toString());
1163-
1164-
assertNotificationOpenedReceiver(intents[1]);
1165-
}
1166-
1167-
@Test
1168-
@Config(shadows = { ShadowGenerateNotification.class })
1169-
public void shouldNotSetContentIntentForLaunchURLIfDefaultNotificationOpenIsDisabled() throws Exception {
1170-
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE");
1171-
generateNotificationWithLaunchURL();
1172-
1173-
Intent[] intents = lastNotificationIntents();
11741149
assertEquals(1, intents.length);
1175-
assertNotificationOpenedReceiver(intents[0]);
11761150
}
11771151

11781152
@Test
1179-
@Config(shadows = { ShadowGenerateNotification.class })
1180-
public void shouldNotSetContentIntentForLaunchURLIfSuppress() throws Exception {
1181-
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", true);
1182-
generateNotificationWithLaunchURL();
1153+
@Config(sdk = 21, shadows = { ShadowGenerateNotification.class })
1154+
public void shouldUseCorrectActivityForLessThanAndroid23() throws Exception {
1155+
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, getBaseNotifBundle());
1156+
threadAndTaskWait();
11831157

11841158
Intent[] intents = lastNotificationIntents();
1185-
assertEquals(2, intents.length);
1186-
assertOpenMainActivityIntent(intents[0]);
1187-
assertNotificationOpenedReceiver(intents[1]);
1159+
assertEquals(
1160+
com.onesignal.NotificationOpenedReceiverAndroid22AndOlder.class.getName(),
1161+
intents[0].getComponent().getClassName()
1162+
);
1163+
assertEquals(1, intents.length);
11881164
}
11891165

11901166
private Intent[] lastNotificationIntents() {
@@ -1193,21 +1169,6 @@ private Intent[] lastNotificationIntents() {
11931169
return shadowOf(pendingIntent).getSavedIntents();
11941170
}
11951171

1196-
private void generateNotificationWithLaunchURL() throws Exception {
1197-
Bundle bundle = launchURLMockPayloadBundle();
1198-
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
1199-
threadAndTaskWait();
1200-
}
1201-
1202-
private void assertNotificationOpenedReceiver(@NonNull Intent intent) {
1203-
assertEquals(com.onesignal.NotificationOpenedReceiverAndroid22AndOlder.class.getName(), intent.getComponent().getClassName());
1204-
}
1205-
1206-
private void assertOpenMainActivityIntent(@NonNull Intent intent) {
1207-
assertEquals(Intent.ACTION_MAIN, intent.getAction());
1208-
assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER));
1209-
}
1210-
12111172
@Test
12121173
@Config(shadows = { ShadowGenerateNotification.class })
12131174
public void shouldSetAlertnessFieldsOnNormalPriority() {
@@ -1312,17 +1273,6 @@ public void shouldSetExpireTimeCorrectlyWhenMissingFromPayload() throws Exceptio
13121273
return bundle;
13131274
}
13141275

1315-
@NonNull
1316-
private static Bundle launchURLMockPayloadBundle() throws JSONException {
1317-
Bundle bundle = new Bundle();
1318-
bundle.putString("alert", "test");
1319-
bundle.putString("custom", new JSONObject() {{
1320-
put("i", "UUID");
1321-
put("u", "https://google.com");
1322-
}}.toString());
1323-
return bundle;
1324-
}
1325-
13261276
@Test
13271277
@Config(shadows = { ShadowOneSignalRestClient.class, ShadowOSWebView.class })
13281278
public void shouldShowInAppPreviewWhenInFocus() throws Exception {

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 132 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import android.net.ConnectivityManager;
4040
import android.os.Bundle;
4141

42+
import androidx.annotation.NonNull;
4243
import androidx.test.core.app.ApplicationProvider;
4344

4445
import com.onesignal.MockOSLog;
@@ -557,7 +558,7 @@ public void testAppOnFocus_containsOutcomeData_withOutcomeEventFlagsEnabled() th
557558
time.advanceSystemTimeBy(31);
558559

559560
// Click notification
560-
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false, "notification_id");
561+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), "notification_id");
561562
threadAndTaskWait();
562563

563564
// Foreground app
@@ -600,7 +601,7 @@ public void testAppOnFocus_wontContainOutcomeData_withOutcomeEventFlagsDisabled(
600601
time.advanceSystemTimeBy(31);
601602

602603
// Click notification
603-
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID + "1");
604+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID + "1");
604605
threadAndTaskWait();
605606

606607
// Foreground app
@@ -968,7 +969,7 @@ public void testAndroidParamsProjectNumberOverridesLocal() throws Exception {
968969
@Config(shadows = {ShadowOneSignal.class})
969970
public void testOpenFromNotificationWhenAppIsDead() throws Exception {
970971
OneSignal.initWithContext(blankActivity);
971-
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID);
972+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID);
972973

973974
OneSignal.setAppId(ONESIGNAL_APP_ID);
974975
OneSignal.initWithContext(blankActivity);
@@ -1024,7 +1025,7 @@ public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() t
10241025
threadAndTaskWait();
10251026

10261027
OneSignal.setNotificationOpenedHandler(null);
1027-
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false, ONESIGNAL_NOTIFICATION_ID);
1028+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID);
10281029
assertNull(lastNotificationOpenedBody);
10291030

10301031
OneSignalInit();
@@ -1057,6 +1058,132 @@ public void shouldNotFireNotificationOpenAgainAfterAppRestart() throws Exception
10571058
assertEquals(null, lastNotificationOpenedBody);
10581059
}
10591060

1061+
@Test
1062+
public void testOpeningLauncherActivity() throws Exception {
1063+
// First init run for appId to be saved
1064+
// At least OneSignal was init once for user to be subscribed
1065+
// If this doesn't' happen, notifications will not arrive
1066+
OneSignalInit();
1067+
fastColdRestartApp();
1068+
1069+
AddLauncherIntentFilter();
1070+
// From app launching normally
1071+
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
1072+
// Will get appId saved
1073+
OneSignal.initWithContext(blankActivity.getApplicationContext());
1074+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID);
1075+
1076+
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
1077+
assertNull(shadowOf(blankActivity).getNextStartedActivity());
1078+
}
1079+
1080+
@Test
1081+
public void testOpeningLaunchUrl() throws Exception {
1082+
// First init run for appId to be saved
1083+
// At least OneSignal was init once for user to be subscribed
1084+
// If this doesn't' happen, notifications will not arrive
1085+
OneSignalInit();
1086+
fastColdRestartApp();
1087+
OneSignal.initWithContext(blankActivity);
1088+
// Removes app launch
1089+
shadowOf(blankActivity).getNextStartedActivity();
1090+
1091+
// No OneSignal init here to test case where it is located in an Activity.
1092+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
1093+
Intent intent = shadowOf(blankActivity).getNextStartedActivity();
1094+
assertEquals("android.intent.action.VIEW", intent.getAction());
1095+
assertEquals("http://google.com", intent.getData().toString());
1096+
assertNull(shadowOf(blankActivity).getNextStartedActivity());
1097+
}
1098+
1099+
@Test
1100+
public void testOpeningLaunchUrlWithDisableDefault() throws Exception {
1101+
// Add the 'com.onesignal.NotificationOpened.DEFAULT' as 'DISABLE' meta-data tag
1102+
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE");
1103+
1104+
// Removes app launch
1105+
shadowOf(blankActivity).getNextStartedActivity();
1106+
1107+
// No OneSignal init here to test case where it is located in an Activity.
1108+
1109+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
1110+
assertNull(shadowOf(blankActivity).getNextStartedActivity());
1111+
}
1112+
1113+
@Test
1114+
public void testDisableOpeningLauncherActivityOnNotificationOpen() throws Exception {
1115+
// Add the 'com.onesignal.NotificationOpened.DEFAULT' as 'DISABLE' meta-data tag
1116+
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE");
1117+
1118+
// From app launching normally
1119+
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
1120+
OneSignal.setAppId(ONESIGNAL_APP_ID);
1121+
OneSignal.initWithContext(blankActivity);
1122+
OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler());
1123+
assertNull(lastNotificationOpenedBody);
1124+
1125+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID);
1126+
1127+
assertNull(shadowOf(blankActivity).getNextStartedActivity());
1128+
assertEquals("Test Msg", lastNotificationOpenedBody);
1129+
}
1130+
1131+
@Test
1132+
public void testLaunchUrlSuppressTrue() throws Exception {
1133+
// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
1134+
// First init run for appId to be saved
1135+
// At least OneSignal was init once for user to be subscribed
1136+
// If this doesn't' happen, notifications will not arrive
1137+
OneSignalInit();
1138+
fastColdRestartApp();
1139+
1140+
// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
1141+
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", true);
1142+
1143+
// Removes app launch
1144+
shadowOf(blankActivity).getNextStartedActivity();
1145+
1146+
// Init with context since this is call before calling OneSignal_handleNotificationOpen internally
1147+
OneSignal.initWithContext(blankActivity);
1148+
1149+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
1150+
threadAndTaskWait();
1151+
1152+
assertOpenMainActivityIntent(shadowOf(blankActivity).getNextStartedActivity());
1153+
}
1154+
1155+
private void assertOpenMainActivityIntent(@NonNull Intent intent) {
1156+
assertEquals(Intent.ACTION_MAIN, intent.getAction());
1157+
assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER));
1158+
}
1159+
1160+
@Test
1161+
public void testLaunchUrlSuppressFalse() throws Exception {
1162+
// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
1163+
// First init run for appId to be saved
1164+
// At least OneSignal was init once for user to be subscribed
1165+
// If this doesn't' happen, notifications will not arrive
1166+
OneSignalInit();
1167+
fastColdRestartApp();
1168+
1169+
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", false);
1170+
OneSignal.initWithContext(blankActivity);
1171+
1172+
// Removes app launch
1173+
shadowOf(blankActivity).getNextStartedActivity();
1174+
1175+
// Init with context since this is call before calling OneSignal_handleNotificationOpen internally
1176+
OneSignal.initWithContext(blankActivity);
1177+
1178+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
1179+
threadAndTaskWait();
1180+
1181+
Intent intent = shadowOf(blankActivity).getNextStartedActivity();
1182+
assertEquals("android.intent.action.VIEW", intent.getAction());
1183+
assertEquals("http://google.com", intent.getData().toString());
1184+
assertNull(shadowOf(blankActivity).getNextStartedActivity());
1185+
}
1186+
10601187
private static String notificationReceivedBody;
10611188
private static int androidNotificationId;
10621189

@@ -3812,7 +3939,7 @@ public void shouldSendFirebaseAnalyticsNotificationOpen() throws Exception {
38123939
openPayload.put("title", "Test title");
38133940
openPayload.put("alert", "Test Msg");
38143941
openPayload.put("custom", new JSONObject("{ \"i\": \"UUID\" }"));
3815-
OneSignal_handleNotificationOpen(blankActivity, new JSONArray().put(openPayload), false, ONESIGNAL_NOTIFICATION_ID);
3942+
OneSignal_handleNotificationOpen(blankActivity, new JSONArray().put(openPayload), ONESIGNAL_NOTIFICATION_ID);
38163943

38173944
assertEquals("os_notification_opened", ShadowFirebaseAnalytics.lastEventString);
38183945
Bundle expectedBundle = new Bundle();

OneSignalSDK/unittest/src/test/java/com/test/onesignal/NotificationOpenedActivityHMSIntegrationTestsRunner.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ public void emptyIntent_doesNotThrow() {
154154
helper_startHMSOpenActivity(helper_baseHMSOpenIntent());
155155
}
156156

157+
@Test
158+
public void barebonesOSPayload_startsMainActivity() throws Exception {
159+
helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent();
160+
161+
Intent startedActivity = shadowOf((Application) ApplicationProvider.getApplicationContext()).getNextStartedActivity();
162+
assertNotNull(startedActivity);
163+
assertEquals(startedActivity.getComponent().getClassName(), BlankActivity.class.getName());
164+
}
165+
157166
@Test
158167
public void barebonesOSPayload_makesNotificationOpenRequest() throws Exception {
159168
helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent();

0 commit comments

Comments
 (0)