Skip to content

Commit

Permalink
Reverting activity trampolining tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkasten2 committed May 5, 2022
1 parent e50724c commit 6d03271
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1142,49 +1142,25 @@ public void shouldUseCorrectActivityForAndroid23Plus() throws Exception {
threadAndTaskWait();

Intent[] intents = lastNotificationIntents();
assertEquals("android.intent.action.MAIN", intents[0].getAction());
assertEquals(
com.onesignal.NotificationOpenedReceiver.class.getName(),
intents[1].getComponent().getClassName()
intents[0].getComponent().getClassName()
);
assertEquals(2, intents.length);
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void shouldSetContentIntentForLaunchURL() throws Exception {
generateNotificationWithLaunchURL();

Intent[] intents = lastNotificationIntents();
assertEquals(2, intents.length);
Intent intentLaunchURL = intents[0];
assertEquals("android.intent.action.VIEW", intentLaunchURL.getAction());
assertEquals("https://google.com", intentLaunchURL.getData().toString());

assertNotificationOpenedReceiver(intents[1]);
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void shouldNotSetContentIntentForLaunchURLIfDefaultNotificationOpenIsDisabled() throws Exception {
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE");
generateNotificationWithLaunchURL();

Intent[] intents = lastNotificationIntents();
assertEquals(1, intents.length);
assertNotificationOpenedReceiver(intents[0]);
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void shouldNotSetContentIntentForLaunchURLIfSuppress() throws Exception {
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", true);
generateNotificationWithLaunchURL();
@Config(sdk = 21, shadows = { ShadowGenerateNotification.class })
public void shouldUseCorrectActivityForLessThanAndroid23() throws Exception {
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, getBaseNotifBundle());
threadAndTaskWait();

Intent[] intents = lastNotificationIntents();
assertEquals(2, intents.length);
assertOpenMainActivityIntent(intents[0]);
assertNotificationOpenedReceiver(intents[1]);
assertEquals(
com.onesignal.NotificationOpenedReceiverAndroid22AndOlder.class.getName(),
intents[0].getComponent().getClassName()
);
assertEquals(1, intents.length);
}

private Intent[] lastNotificationIntents() {
Expand All @@ -1193,21 +1169,6 @@ private Intent[] lastNotificationIntents() {
return shadowOf(pendingIntent).getSavedIntents();
}

private void generateNotificationWithLaunchURL() throws Exception {
Bundle bundle = launchURLMockPayloadBundle();
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
threadAndTaskWait();
}

private void assertNotificationOpenedReceiver(@NonNull Intent intent) {
assertEquals(com.onesignal.NotificationOpenedReceiverAndroid22AndOlder.class.getName(), intent.getComponent().getClassName());
}

private void assertOpenMainActivityIntent(@NonNull Intent intent) {
assertEquals(Intent.ACTION_MAIN, intent.getAction());
assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER));
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void shouldSetAlertnessFieldsOnNormalPriority() {
Expand Down Expand Up @@ -1309,17 +1270,6 @@ public void shouldSetExpireTimeCorrectlyWhenMissingFromPayload() throws Exceptio
return bundle;
}

@NonNull
private static Bundle launchURLMockPayloadBundle() throws JSONException {
Bundle bundle = new Bundle();
bundle.putString("alert", "test");
bundle.putString("custom", new JSONObject() {{
put("i", "UUID");
put("u", "https://google.com");
}}.toString());
return bundle;
}

@Test
@Config(shadows = { ShadowOneSignalRestClient.class, ShadowOSWebView.class })
public void shouldShowInAppPreviewWhenInFocus() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import android.net.ConnectivityManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.test.core.app.ApplicationProvider;

import com.onesignal.MockOSLog;
Expand Down Expand Up @@ -557,7 +558,7 @@ public void testAppOnFocus_containsOutcomeData_withOutcomeEventFlagsEnabled() th
time.advanceSystemTimeBy(31);

// Click notification
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), false, "notification_id");
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), "notification_id");
threadAndTaskWait();

// Foreground app
Expand Down Expand Up @@ -600,7 +601,7 @@ public void testAppOnFocus_wontContainOutcomeData_withOutcomeEventFlagsDisabled(
time.advanceSystemTimeBy(31);

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

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

OneSignal.setAppId(ONESIGNAL_APP_ID);
OneSignal.initWithContext(blankActivity);
Expand Down Expand Up @@ -1024,7 +1025,7 @@ public void shouldCorrectlyRemoveOpenedHandlerAndFireMissedOnesWhenAddedBack() t
threadAndTaskWait();

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

OneSignalInit();
Expand Down Expand Up @@ -1057,6 +1058,132 @@ public void shouldNotFireNotificationOpenAgainAfterAppRestart() throws Exception
assertEquals(null, lastNotificationOpenedBody);
}

@Test
public void testOpeningLauncherActivity() throws Exception {
// First init run for appId to be saved
// At least OneSignal was init once for user to be subscribed
// If this doesn't' happen, notifications will not arrive
OneSignalInit();
fastColdRestartApp();

AddLauncherIntentFilter();
// From app launching normally
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
// Will get appId saved
OneSignal.initWithContext(blankActivity.getApplicationContext());
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID);

assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
assertNull(shadowOf(blankActivity).getNextStartedActivity());
}

@Test
public void testOpeningLaunchUrl() throws Exception {
// First init run for appId to be saved
// At least OneSignal was init once for user to be subscribed
// If this doesn't' happen, notifications will not arrive
OneSignalInit();
fastColdRestartApp();
OneSignal.initWithContext(blankActivity);
// Removes app launch
shadowOf(blankActivity).getNextStartedActivity();

// No OneSignal init here to test case where it is located in an Activity.
OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
Intent intent = shadowOf(blankActivity).getNextStartedActivity();
assertEquals("android.intent.action.VIEW", intent.getAction());
assertEquals("http://google.com", intent.getData().toString());
assertNull(shadowOf(blankActivity).getNextStartedActivity());
}

@Test
public void testOpeningLaunchUrlWithDisableDefault() throws Exception {
// Add the 'com.onesignal.NotificationOpened.DEFAULT' as 'DISABLE' meta-data tag
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE");

// Removes app launch
shadowOf(blankActivity).getNextStartedActivity();

// No OneSignal init here to test case where it is located in an Activity.

OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
assertNull(shadowOf(blankActivity).getNextStartedActivity());
}

@Test
public void testDisableOpeningLauncherActivityOnNotificationOpen() throws Exception {
// Add the 'com.onesignal.NotificationOpened.DEFAULT' as 'DISABLE' meta-data tag
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.NotificationOpened.DEFAULT", "DISABLE");

// From app launching normally
assertNotNull(shadowOf(blankActivity).getNextStartedActivity());
OneSignal.setAppId(ONESIGNAL_APP_ID);
OneSignal.initWithContext(blankActivity);
OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler());
assertNull(lastNotificationOpenedBody);

OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\" } }]"), ONESIGNAL_NOTIFICATION_ID);

assertNull(shadowOf(blankActivity).getNextStartedActivity());
assertEquals("Test Msg", lastNotificationOpenedBody);
}

@Test
public void testLaunchUrlSuppressTrue() throws Exception {
// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
// First init run for appId to be saved
// At least OneSignal was init once for user to be subscribed
// If this doesn't' happen, notifications will not arrive
OneSignalInit();
fastColdRestartApp();

// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", true);

// Removes app launch
shadowOf(blankActivity).getNextStartedActivity();

// Init with context since this is call before calling OneSignal_handleNotificationOpen internally
OneSignal.initWithContext(blankActivity);

OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
threadAndTaskWait();

assertOpenMainActivityIntent(shadowOf(blankActivity).getNextStartedActivity());
}

private void assertOpenMainActivityIntent(@NonNull Intent intent) {
assertEquals(Intent.ACTION_MAIN, intent.getAction());
assertTrue(intent.getCategories().contains(Intent.CATEGORY_LAUNCHER));
}

@Test
public void testLaunchUrlSuppressFalse() throws Exception {
// Add the 'com.onesignal.suppressLaunchURLs' as 'true' meta-data tag
// First init run for appId to be saved
// At least OneSignal was init once for user to be subscribed
// If this doesn't' happen, notifications will not arrive
OneSignalInit();
fastColdRestartApp();

OneSignalShadowPackageManager.addManifestMetaData("com.onesignal.suppressLaunchURLs", false);
OneSignal.initWithContext(blankActivity);

// Removes app launch
shadowOf(blankActivity).getNextStartedActivity();

// Init with context since this is call before calling OneSignal_handleNotificationOpen internally
OneSignal.initWithContext(blankActivity);

OneSignal_handleNotificationOpen(blankActivity, new JSONArray("[{ \"alert\": \"Test Msg\", \"custom\": { \"i\": \"UUID\", \"u\": \"http://google.com\" } }]"), ONESIGNAL_NOTIFICATION_ID);
threadAndTaskWait();

Intent intent = shadowOf(blankActivity).getNextStartedActivity();
assertEquals("android.intent.action.VIEW", intent.getAction());
assertEquals("http://google.com", intent.getData().toString());
assertNull(shadowOf(blankActivity).getNextStartedActivity());
}

private static String notificationReceivedBody;
private static int androidNotificationId;

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

assertEquals("os_notification_opened", ShadowFirebaseAnalytics.lastEventString);
Bundle expectedBundle = new Bundle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ public void emptyIntent_doesNotThrow() {
helper_startHMSOpenActivity(helper_baseHMSOpenIntent());
}

@Test
public void barebonesOSPayload_startsMainActivity() throws Exception {
helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent();

Intent startedActivity = shadowOf((Application) ApplicationProvider.getApplicationContext()).getNextStartedActivity();
assertNotNull(startedActivity);
assertEquals(startedActivity.getComponent().getClassName(), BlankActivity.class.getName());
}

@Test
public void barebonesOSPayload_makesNotificationOpenRequest() throws Exception {
helper_initSDKAndFireHMSNotificationBarebonesOSOpenIntent();
Expand Down
Loading

0 comments on commit 6d03271

Please sign in to comment.