Commit e08bd060 authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

[Android WebAPK] Add extra lightweight first run experience tests

This CL adds two new tests for the lightweight first run experience.
The motivation for the new tests is because the purpose of removing the
WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME in
WebappLauncherActivity#tryCreateWebappInfo() is to make the lightweight
FRE logic work correctly but the logic was not tested in the FRE
context.

BUG=None

Change-Id: I5958a7333722f10de1e3599b07d82be9e9e39446
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2147042
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarGlenn Hartmann <hartmanng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760389}
parent 6103dc6e
...@@ -32,11 +32,11 @@ import org.robolectric.shadows.ShadowApplication; ...@@ -32,11 +32,11 @@ import org.robolectric.shadows.ShadowApplication;
import org.chromium.base.test.BaseRobolectricTestRunner; import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.ChromeTabbedActivity; import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.ShortcutHelper;
import org.chromium.chrome.browser.document.ChromeLauncherActivity; import org.chromium.chrome.browser.document.ChromeLauncherActivity;
import org.chromium.chrome.browser.init.BrowserParts; import org.chromium.chrome.browser.init.BrowserParts;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer; import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.searchwidget.SearchActivity; import org.chromium.chrome.browser.searchwidget.SearchActivity;
import org.chromium.chrome.browser.webapps.WebApkInfo;
import org.chromium.chrome.browser.webapps.WebappActivity; import org.chromium.chrome.browser.webapps.WebappActivity;
import org.chromium.chrome.browser.webapps.WebappLauncherActivity; import org.chromium.chrome.browser.webapps.WebappLauncherActivity;
import org.chromium.webapk.lib.client.WebApkValidator; import org.chromium.webapk.lib.client.WebApkValidator;
...@@ -116,6 +116,20 @@ public final class FirstRunIntegrationUnitTest { ...@@ -116,6 +116,20 @@ public final class FirstRunIntegrationUnitTest {
Robolectric.buildActivity(activityClass, intent).create(); Robolectric.buildActivity(activityClass, intent).create();
} }
/**
* Launches {@link WebappLauncherActivity}. If WebappLauncherActivity is relaunched, waits for
* the relaunch to occur.
*/
private void launchWebappLauncherActivityProcessRelaunch(Intent intent) {
Robolectric.buildActivity(WebappLauncherActivity.class, intent).create();
Intent launchedIntent = mShadowApplication.peekNextStartedActivity();
if (checkIntentComponentClass(launchedIntent, WebappLauncherActivity.class)) {
// Pop the WebappLauncherActivity from the 'started activities' list.
mShadowApplication.getNextStartedActivity();
buildActivityWithClassNameFromIntent(launchedIntent);
}
}
/** /**
* Checks that either {@link FirstRunActivity} or {@link TabbedModeFirstRunActivity} * Checks that either {@link FirstRunActivity} or {@link TabbedModeFirstRunActivity}
* was launched. * was launched.
...@@ -192,19 +206,12 @@ public final class FirstRunIntegrationUnitTest { ...@@ -192,19 +206,12 @@ public final class FirstRunIntegrationUnitTest {
webApkPackageName, bundle, null /* shareTargetMetaData */); webApkPackageName, bundle, null /* shareTargetMetaData */);
WebApkTestHelper.addIntentFilterForUrl(webApkPackageName, startUrl); WebApkTestHelper.addIntentFilterForUrl(webApkPackageName, startUrl);
Intent intent = new Intent(); Intent intent = WebApkTestHelper.createMinimalWebApkIntent(webApkPackageName, startUrl);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackageName);
intent.putExtra(ShortcutHelper.EXTRA_URL, startUrl);
intent.putExtra(WebApkConstants.EXTRA_SPLASH_PROVIDED_BY_WEBAPK, true); intent.putExtra(WebApkConstants.EXTRA_SPLASH_PROVIDED_BY_WEBAPK, true);
Robolectric.buildActivity(WebappLauncherActivity.class, intent).create(); launchWebappLauncherActivityProcessRelaunch(intent);
Intent launchedIntent = mShadowApplication.getNextStartedActivity(); Intent launchedIntent = mShadowApplication.getNextStartedActivity();
while (checkIntentComponentClass(launchedIntent, WebappLauncherActivity.class)) {
buildActivityWithClassNameFromIntent(launchedIntent);
launchedIntent = mShadowApplication.getNextStartedActivity();
}
Assert.assertTrue(checkIntentIsForFre(launchedIntent)); Assert.assertTrue(checkIntentIsForFre(launchedIntent));
PendingIntent freCompleteLaunchIntent = launchedIntent.getParcelableExtra( PendingIntent freCompleteLaunchIntent = launchedIntent.getParcelableExtra(
FirstRunActivityBase.EXTRA_FRE_COMPLETE_LAUNCH_INTENT); FirstRunActivityBase.EXTRA_FRE_COMPLETE_LAUNCH_INTENT);
...@@ -230,11 +237,9 @@ public final class FirstRunIntegrationUnitTest { ...@@ -230,11 +237,9 @@ public final class FirstRunIntegrationUnitTest {
webApkPackageName, bundle, null /* shareTargetMetaData */); webApkPackageName, bundle, null /* shareTargetMetaData */);
WebApkTestHelper.addIntentFilterForUrl(webApkPackageName, startUrl); WebApkTestHelper.addIntentFilterForUrl(webApkPackageName, startUrl);
Intent intent = new Intent(); Intent intent = WebApkTestHelper.createMinimalWebApkIntent(webApkPackageName, startUrl);
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackageName);
intent.putExtra(ShortcutHelper.EXTRA_URL, startUrl);
Robolectric.buildActivity(WebappLauncherActivity.class, intent).create(); launchWebappLauncherActivityProcessRelaunch(intent);
Intent launchedIntent = mShadowApplication.getNextStartedActivity(); Intent launchedIntent = mShadowApplication.getNextStartedActivity();
Assert.assertTrue(checkIntentComponentClass(launchedIntent, WebappActivity.class)); Assert.assertTrue(checkIntentComponentClass(launchedIntent, WebappActivity.class));
...@@ -243,4 +248,61 @@ public final class FirstRunIntegrationUnitTest { ...@@ -243,4 +248,61 @@ public final class FirstRunIntegrationUnitTest {
// No FRE should have been launched. // No FRE should have been launched.
Assert.assertNull(mShadowApplication.getNextStartedActivity()); Assert.assertNull(mShadowApplication.getNextStartedActivity());
} }
/**
* Test that the lightweight first run experience is used for unbound WebAPKs.
*/
@Test
public void testLightweightFre() {
String webApkPackageName = "unbound.webapk";
String startUrl = "https://pwa.rocks/";
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, startUrl);
WebApkTestHelper.registerWebApkWithMetaData(
webApkPackageName, bundle, null /* shareTargetMetaData */);
WebApkTestHelper.addIntentFilterForUrl(webApkPackageName, startUrl);
Intent intent = WebApkTestHelper.createMinimalWebApkIntent(webApkPackageName, startUrl);
launchWebappLauncherActivityProcessRelaunch(intent);
Intent launchedIntent = mShadowApplication.getNextStartedActivity();
Assert.assertTrue(
checkIntentComponentClass(launchedIntent, LightweightFirstRunActivity.class));
}
/**
* Test that {@link WebappLauncherActivity} shows the regular full first run experience when it
* is launched with an intent which both:
* - Has a WebAPK package extra which meets the lightweight first run activity requirements
* - Refers to an invalid WebAPK
*/
@Test
public void testFullFreIfWebApkInvalid() {
String webApkPackageName = "unbound.webapk";
String startUrl = "https://pwa.rocks/";
Bundle bundle = new Bundle();
bundle.putString(WebApkMetaDataKeys.START_URL, startUrl);
WebApkTestHelper.registerWebApkWithMetaData(
webApkPackageName, bundle, null /* shareTargetMetaData */);
// Cause WebApkValidator#canWebApkHandleUrl() to fail (but not WebApkInfo#create()) by not
// registering the intent handlers for the WebAPK.
Intent intent = WebApkTestHelper.createMinimalWebApkIntent(webApkPackageName, startUrl);
Assert.assertNotNull(WebApkInfo.create(intent));
launchWebappLauncherActivityProcessRelaunch(intent);
Intent launchedIntent = mShadowApplication.getNextStartedActivity();
Assert.assertTrue(checkIntentComponentClass(launchedIntent, FirstRunActivity.class));
// WebappLauncherActivity (not the WebAPK) should be launched when the WebAPK completes.
PendingIntent freCompleteLaunchIntent = launchedIntent.getParcelableExtra(
FirstRunActivityBase.EXTRA_FRE_COMPLETE_LAUNCH_INTENT);
Assert.assertNotNull(freCompleteLaunchIntent);
Assert.assertEquals(mContext.getPackageName(),
Shadows.shadowOf(freCompleteLaunchIntent).getSavedIntent().getPackage());
}
} }
...@@ -19,6 +19,7 @@ import org.robolectric.Shadows; ...@@ -19,6 +19,7 @@ import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager; import org.robolectric.shadows.ShadowPackageManager;
import org.chromium.webapk.lib.common.WebApkCommonUtils; import org.chromium.webapk.lib.common.WebApkCommonUtils;
import org.chromium.webapk.lib.common.WebApkConstants;
import java.net.URISyntaxException; import java.net.URISyntaxException;
...@@ -28,6 +29,17 @@ import java.net.URISyntaxException; ...@@ -28,6 +29,17 @@ import java.net.URISyntaxException;
public class WebApkTestHelper { public class WebApkTestHelper {
private static final String SHARE_TARGET_ACTIVITY_CLASS_NAME_PREFIX = "TestShareTargetActivity"; private static final String SHARE_TARGET_ACTIVITY_CLASS_NAME_PREFIX = "TestShareTargetActivity";
/**
* Returns the simplest intent for launching a WebAPK.
*/
public static Intent createMinimalWebApkIntent(String webApkPackageName, String url) {
Intent intent = new Intent();
intent.setPackage(RuntimeEnvironment.application.getPackageName());
intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackageName);
intent.putExtra(WebApkConstants.EXTRA_URL, url);
return intent;
}
/** /**
* Registers WebAPK. This function also creates an empty resource for the WebAPK. * Registers WebAPK. This function also creates an empty resource for the WebAPK.
* @param packageName The package to register * @param packageName The package to register
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment