Commit e3acc3e8 authored by Dominick Ng's avatar Dominick Ng Committed by Commit Bot

Reland "Consolidate app banner testing."

This is a reland of 58ab87ff.

Original change's description:
> Consolidate app banner testing.
> 
> This CL removes several different app banner testing files, and replaces
> them by calling the necessary JavaScript setup code in one test file.
> For instance, all tests will no longer have the "appinstalled" title
> overwriting, as that will only be active when the appropriate setup
> JavaScript is run.
> 
> This allows future tests to be more easily added, and reduces collisions
> when manipulating the page title.
> 
> BUG=806923
> 
> Change-Id: Ia28837b8675a2e1fe329cb1df1be4798ddb03d02
> Reviewed-on: https://chromium-review.googlesource.com/892182
> Commit-Queue: Dominick Ng <dominickn@chromium.org>
> Reviewed-by: Matt Giuca <mgiuca@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#532978}

Bug: 806923
Change-Id: Ia85a5be0828ad0820a891f8cde9d9e492dc0b831
Reviewed-on: https://chromium-review.googlesource.com/896326Reviewed-by: default avatarMatt Giuca <mgiuca@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Dominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533745}
parent 89a7c021
...@@ -45,7 +45,6 @@ import org.chromium.chrome.browser.infobar.InfoBarContainer; ...@@ -45,7 +45,6 @@ import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.browser.infobar.InfoBarContainer.InfoBarAnimationListener; import org.chromium.chrome.browser.infobar.InfoBarContainer.InfoBarAnimationListener;
import org.chromium.chrome.browser.infobar.InfoBarContainerLayout.Item; import org.chromium.chrome.browser.infobar.InfoBarContainerLayout.Item;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.webapps.WebappDataStorage; import org.chromium.chrome.browser.webapps.WebappDataStorage;
import org.chromium.chrome.test.ChromeActivityTestRule; import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
...@@ -77,8 +76,11 @@ public class AppBannerManagerTest { ...@@ -77,8 +76,11 @@ public class AppBannerManagerTest {
@Rule @Rule
public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule(); public CustomTabActivityTestRule mCustomTabActivityTestRule = new CustomTabActivityTestRule();
private static final String NATIVE_APP_PATH = private static final String NATIVE_APP_MANIFEST_WITH_ID =
"/chrome/test/data/banners/play_app_test_page.html"; "/chrome/test/data/banners/play_app_manifest.json";
private static final String NATIVE_APP_MANIFEST_WITH_URL =
"/chrome/test/data/banners/play_app_url_manifest.json";
private static final String NATIVE_ICON_PATH = "/chrome/test/data/banners/launcher-icon-4x.png"; private static final String NATIVE_ICON_PATH = "/chrome/test/data/banners/launcher-icon-4x.png";
...@@ -92,14 +94,6 @@ public class AppBannerManagerTest { ...@@ -92,14 +94,6 @@ public class AppBannerManagerTest {
private static final String NATIVE_APP_BLANK_REFERRER = "playinline=chrome_inline"; private static final String NATIVE_APP_BLANK_REFERRER = "playinline=chrome_inline";
private static final String NATIVE_APP_URL_WITH_MANIFEST_PATH =
"/chrome/test/data/banners/play_app_url_test_page.html";
private static final String WEB_APP_PATH = "/chrome/test/data/banners/manifest_test_page.html";
private static final String WEB_APP_BEFORE_INSTALL_PROMPT_PATH =
"/chrome/test/data/banners/prompt_test_page.html";
private static final String WEB_APP_SHORT_TITLE_MANIFEST = private static final String WEB_APP_SHORT_TITLE_MANIFEST =
"/chrome/test/data/banners/manifest_short_name_only.json"; "/chrome/test/data/banners/manifest_short_name_only.json";
...@@ -193,7 +187,6 @@ public class AppBannerManagerTest { ...@@ -193,7 +187,6 @@ public class AppBannerManagerTest {
private TestPackageManager mPackageManager; private TestPackageManager mPackageManager;
private EmbeddedTestServer mTestServer; private EmbeddedTestServer mTestServer;
private String mWebAppUrl; private String mWebAppUrl;
private String mWebAppApiUrl;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
...@@ -215,9 +208,9 @@ public class AppBannerManagerTest { ...@@ -215,9 +208,9 @@ public class AppBannerManagerTest {
AppBannerManager.setTotalEngagementForTesting(10); AppBannerManager.setTotalEngagementForTesting(10);
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext()); mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
mNativeAppUrl = mTestServer.getURL(NATIVE_APP_PATH); mNativeAppUrl = WebappTestPage.getNativeBannerUrlWithManifest(
mWebAppUrl = mTestServer.getURL(WEB_APP_PATH); mTestServer, NATIVE_APP_MANIFEST_WITH_ID);
mWebAppApiUrl = mTestServer.getURL(WEB_APP_BEFORE_INSTALL_PROMPT_PATH); mWebAppUrl = WebappTestPage.getBannerUrl(mTestServer);
} }
@After @After
...@@ -386,8 +379,9 @@ public class AppBannerManagerTest { ...@@ -386,8 +379,9 @@ public class AppBannerManagerTest {
@SmallTest @SmallTest
@Feature({"AppBanners"}) @Feature({"AppBanners"})
public void testFullNativeInstallPathwayFromUrl() throws Exception { public void testFullNativeInstallPathwayFromUrl() throws Exception {
runFullNativeInstallPathway( runFullNativeInstallPathway(WebappTestPage.getNativeBannerUrlWithManifest(
mTestServer.getURL(NATIVE_APP_URL_WITH_MANIFEST_PATH), NATIVE_APP_REFERRER); mTestServer, NATIVE_APP_MANIFEST_WITH_URL),
NATIVE_APP_REFERRER);
} }
@Test @Test
...@@ -604,8 +598,7 @@ public class AppBannerManagerTest { ...@@ -604,8 +598,7 @@ public class AppBannerManagerTest {
@Feature({"AppBanners"}) @Feature({"AppBanners"})
public void testBannerFallsBackToShortNameWhenNameNotPresent() throws Exception { public void testBannerFallsBackToShortNameWhenNameNotPresent() throws Exception {
triggerWebAppBanner(mTabbedActivityTestRule, triggerWebAppBanner(mTabbedActivityTestRule,
WebappTestPage.urlOfPageWithServiceWorkerAndManifest( WebappTestPage.getBannerUrlWithManifest(mTestServer, WEB_APP_SHORT_TITLE_MANIFEST),
mTestServer, WEB_APP_SHORT_TITLE_MANIFEST),
WEB_APP_SHORT_TITLE, false); WEB_APP_SHORT_TITLE, false);
} }
...@@ -614,21 +607,43 @@ public class AppBannerManagerTest { ...@@ -614,21 +607,43 @@ public class AppBannerManagerTest {
@Feature({"AppBanners"}) @Feature({"AppBanners"})
public void testBannerFallsBackToShortNameWhenNameIsEmpty() throws Exception { public void testBannerFallsBackToShortNameWhenNameIsEmpty() throws Exception {
triggerWebAppBanner(mTabbedActivityTestRule, triggerWebAppBanner(mTabbedActivityTestRule,
WebappTestPage.urlOfPageWithServiceWorkerAndManifest( WebappTestPage.getBannerUrlWithManifest(mTestServer, WEB_APP_EMPTY_NAME_MANIFEST),
mTestServer, WEB_APP_EMPTY_NAME_MANIFEST),
WEB_APP_SHORT_TITLE, false); WEB_APP_SHORT_TITLE, false);
} }
@Test
@MediumTest
@Feature({"AppBanners"})
public void testAppInstalledEventAutomaticPrompt() throws Exception {
triggerWebAppBanner(mTabbedActivityTestRule,
WebappTestPage.getBannerUrlWithAction(mTestServer, "verify_appinstalled"),
WEB_APP_TITLE, true);
// The appinstalled event should fire (and cause the title to change).
new TabTitleObserver(mTabbedActivityTestRule.getActivity().getActivityTab(),
"Got appinstalled: listener, attr")
.waitForTitleUpdate(3);
}
@Test
@MediumTest
@Feature({"AppBanners"})
public void testAppInstalledEventApi() throws Exception {
triggerWebAppBanner(mTabbedActivityTestRule,
WebappTestPage.getBannerUrlWithAction(mTestServer, "verify_prompt_appinstalled"),
WEB_APP_TITLE, true);
// The appinstalled event should fire (and cause the title to change).
new TabTitleObserver(mTabbedActivityTestRule.getActivity().getActivityTab(),
"Got appinstalled: listener, attr")
.waitForTitleUpdate(3);
}
private void runWebAppBannerAndCheckInstallEvent( private void runWebAppBannerAndCheckInstallEvent(
ChromeActivityTestRule<? extends ChromeActivity> rule, String webAppUrl, ChromeActivityTestRule<? extends ChromeActivity> rule, String webAppUrl,
int expectedBucket) throws Exception { int expectedBucket) throws Exception {
triggerWebAppBanner(rule, webAppUrl, WEB_APP_TITLE, true); triggerWebAppBanner(rule, webAppUrl, WEB_APP_TITLE, true);
// The appinstalled event should fire (and cause the title to change).
Tab tab = rule.getActivity().getActivityTab();
new TabTitleObserver(tab, "Got appinstalled").waitForTitleUpdate(3);
// We should have recorded the AUTOMATIC_BROWSER_TAB install source.
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
Assert.assertEquals(1, Assert.assertEquals(1,
RecordHistogram.getHistogramValueCountForTesting( RecordHistogram.getHistogramValueCountForTesting(
...@@ -657,7 +672,8 @@ public class AppBannerManagerTest { ...@@ -657,7 +672,8 @@ public class AppBannerManagerTest {
@SmallTest @SmallTest
@Feature({"AppBanners"}) @Feature({"AppBanners"})
public void testPostInstallationApiBrowserTab() throws Exception { public void testPostInstallationApiBrowserTab() throws Exception {
runWebAppBannerAndCheckInstallEvent(mTabbedActivityTestRule, mWebAppApiUrl, 4); runWebAppBannerAndCheckInstallEvent(mTabbedActivityTestRule,
WebappTestPage.getBannerUrlWithAction(mTestServer, "call_prompt_delayed"), 4);
} }
@Test @Test
...@@ -667,7 +683,8 @@ public class AppBannerManagerTest { ...@@ -667,7 +683,8 @@ public class AppBannerManagerTest {
mCustomTabActivityTestRule.startCustomTabActivityWithIntent( mCustomTabActivityTestRule.startCustomTabActivityWithIntent(
CustomTabsTestUtils.createMinimalCustomTabIntent( CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), "about:blank")); InstrumentationRegistry.getTargetContext(), "about:blank"));
runWebAppBannerAndCheckInstallEvent(mCustomTabActivityTestRule, mWebAppApiUrl, 5); runWebAppBannerAndCheckInstallEvent(mCustomTabActivityTestRule,
WebappTestPage.getBannerUrlWithAction(mTestServer, "call_prompt_delayed"), 5);
} }
@Test @Test
......
...@@ -32,6 +32,7 @@ import org.chromium.chrome.test.ChromeActivityTestRule; ...@@ -32,6 +32,7 @@ import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.TabLoadObserver; import org.chromium.chrome.test.util.browser.TabLoadObserver;
import org.chromium.chrome.test.util.browser.TabTitleObserver; import org.chromium.chrome.test.util.browser.TabTitleObserver;
import org.chromium.chrome.test.util.browser.WebappTestPage;
import org.chromium.content.browser.test.util.Criteria; import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper; import org.chromium.content.browser.test.util.CriteriaHelper;
import org.chromium.content.common.ContentSwitches; import org.chromium.content.common.ContentSwitches;
...@@ -88,10 +89,6 @@ public class AddToHomescreenManagerTest { ...@@ -88,10 +89,6 @@ public class AddToHomescreenManagerTest {
private static final String MANIFEST_PATH = "/chrome/test/data/banners/manifest_test_page.html"; private static final String MANIFEST_PATH = "/chrome/test/data/banners/manifest_test_page.html";
private static final String MANIFEST_TITLE = "Web app banner test page"; private static final String MANIFEST_TITLE = "Web app banner test page";
private static final String EVENT_WEBAPP_PATH =
"/chrome/test/data/banners/appinstalled_test_page.html";
private static final String EVENT_WEBAPP_TITLE = "appinstalled event test page";
private static class TestShortcutHelperDelegate extends ShortcutHelper.Delegate { private static class TestShortcutHelperDelegate extends ShortcutHelper.Delegate {
public String mRequestedShortcutTitle; public String mRequestedShortcutTitle;
public Intent mRequestedShortcutIntent; public Intent mRequestedShortcutIntent;
...@@ -302,7 +299,9 @@ public class AddToHomescreenManagerTest { ...@@ -302,7 +299,9 @@ public class AddToHomescreenManagerTest {
@SmallTest @SmallTest
@Feature("{Webapp}") @Feature("{Webapp}")
public void testAddWebappShortcutAppInstalledEvent() throws Exception { public void testAddWebappShortcutAppInstalledEvent() throws Exception {
loadUrl(mTestServerRule.getServer().getURL(EVENT_WEBAPP_PATH), EVENT_WEBAPP_TITLE); loadUrl(WebappTestPage.getBannerUrlWithAction(
mTestServerRule.getServer(), "verify_appinstalled"),
MANIFEST_TITLE);
addShortcutToTab(mTab, "", true); addShortcutToTab(mTab, "", true);
// Wait for the tab title to change. This will happen (due to the JavaScript that runs // Wait for the tab title to change. This will happen (due to the JavaScript that runs
......
...@@ -219,7 +219,7 @@ public class WebappNavigationTest { ...@@ -219,7 +219,7 @@ public class WebappNavigationTest {
ChromeTabUtils.waitForTabPageLoaded(customTab.getActivityTab(), inScopeUrl); ChromeTabUtils.waitForTabPageLoaded(customTab.getActivityTab(), inScopeUrl);
Assert.assertTrue( Assert.assertTrue(
mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.textContent") mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.textContent")
.contains("Do-nothing page with a service worker")); .contains("Do-nothing page with a manifest and a service worker."));
} }
/** /**
......
...@@ -494,7 +494,8 @@ IN_PROC_BROWSER_TEST_F(InstallableManagerBrowserTest, ...@@ -494,7 +494,8 @@ IN_PROC_BROWSER_TEST_F(InstallableManagerBrowserTest,
NavigateAndRunInstallableManager(browser(), tester.get(), NavigateAndRunInstallableManager(browser(), tester.get(),
GetManifestParams(), GetManifestParams(),
"/banners/play_app_test_page.html"); GetURLOfPageWithServiceWorkerAndManifest(
"/banners/play_app_manifest.json"));
run_loop.Run(); run_loop.Run();
EXPECT_FALSE(tester->manifest().IsEmpty()); EXPECT_FALSE(tester->manifest().IsEmpty());
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
package org.chromium.chrome.test.util.browser; package org.chromium.chrome.test.util.browser;
import android.net.Uri;
import org.chromium.chrome.browser.tab.Tab; import org.chromium.chrome.browser.tab.Tab;
import org.chromium.net.test.EmbeddedTestServer; import org.chromium.net.test.EmbeddedTestServer;
...@@ -14,16 +16,52 @@ public class WebappTestPage { ...@@ -14,16 +16,52 @@ public class WebappTestPage {
/** /**
* Title of pages whose URL was generated by {@link #urlOfPageWithServiceWorkerAndManifest()}. * Title of pages whose URL was generated by {@link #urlOfPageWithServiceWorkerAndManifest()}.
*/ */
public static final String TITLE = "Web app banner test page"; public static final String WEB_APP_TITLE = "Web app banner test page";
private static final String WEB_APP_PATH = "/chrome/test/data/banners/manifest_test_page.html";
private static final String NO_SERVICE_WORKER_APP_PATH =
"/chrome/test/data/banners/manifest_no_service_worker.html";
/**
* Returns the URL of a banner-eligible page.
*/
public static String getBannerUrl(EmbeddedTestServer testServer) {
String url = testServer.getURL(WEB_APP_PATH);
Uri.Builder builder = Uri.parse(url).buildUpon();
return builder.build().toString();
}
/**
* Returns the URL of a native banner-eligible page with the specified web manifest URL.
*/
public static String getNativeBannerUrlWithManifest(
EmbeddedTestServer testServer, String manifestUrl) {
String url = testServer.getURL(NO_SERVICE_WORKER_APP_PATH);
Uri.Builder builder = Uri.parse(url).buildUpon();
builder.appendQueryParameter("manifest", manifestUrl);
return builder.build().toString();
}
/** /**
* Computes URL of page which is controlled by a service worker and uses the passed in Web * Returns the URL of a banner-eligible page with the specified web manifest URL.
* Manifest URL.
*/ */
public static String urlOfPageWithServiceWorkerAndManifest( public static String getBannerUrlWithManifest(
EmbeddedTestServer testServer, String manifestUrl) { EmbeddedTestServer testServer, String manifestUrl) {
String url = "/chrome/test/data/banners/manifest_test_page.html?manifest=" + manifestUrl; String url = testServer.getURL(WEB_APP_PATH);
return testServer.getURL(url); Uri.Builder builder = Uri.parse(url).buildUpon();
builder.appendQueryParameter("manifest", manifestUrl);
return builder.build().toString();
}
/**
* Returns the URL of a banner-eligible page with the specified action query parameter.
*/
public static String getBannerUrlWithAction(EmbeddedTestServer testServer, String action) {
String url = testServer.getURL(WEB_APP_PATH);
Uri.Builder builder = Uri.parse(url).buildUpon();
builder.appendQueryParameter("action", action);
return builder.build().toString();
} }
/** /**
...@@ -32,7 +70,7 @@ public class WebappTestPage { ...@@ -32,7 +70,7 @@ public class WebappTestPage {
*/ */
public static void navigateToPageWithServiceWorkerAndManifest( public static void navigateToPageWithServiceWorkerAndManifest(
EmbeddedTestServer testServer, Tab tab, String manifestUrl) throws Exception { EmbeddedTestServer testServer, Tab tab, String manifestUrl) throws Exception {
TabLoadObserver observer = new TabLoadObserver(tab, TITLE, null); TabLoadObserver observer = new TabLoadObserver(tab, WEB_APP_TITLE, null);
observer.fullyLoadUrl(urlOfPageWithServiceWorkerAndManifest(testServer, manifestUrl)); observer.fullyLoadUrl(getBannerUrlWithManifest(testServer, manifestUrl));
} }
} }
<!DOCTYPE html>
<html>
<head>
<title>appinstalled event test page</title>
<script>
// These blanks will get filled in when each event comes through.
var gotEventsFrom = ['________', '____'];
function setTitle() {
window.document.title = 'Got appinstalled: ' +
gotEventsFrom.join(', ');
}
window.addEventListener('load', () => {
// Test both the addEventListener and onX attribute versions.
// When an app is installed, each should fire once.
window.addEventListener('appinstalled', () => {
gotEventsFrom[0] = 'listener';
setTitle();
});
window.onappinstalled = () => {
gotEventsFrom[1] = 'attr';
setTitle();
};
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>beforeinstallprompt event test page</title>
<link rel="manifest" href="manifest.json" />
<script src="main.js"></script>
<script>
// These blanks will get filled in when each event comes through.
var gotEventsFrom = ['________', '____'];
function setTitle() {
window.document.title = 'Got beforeinstallprompt: ' +
gotEventsFrom.join(', ');
}
window.addEventListener('load', () => {
// Test both the addEventListener and onX attribute versions.
// When a prompt is shown, each should fire once.
window.addEventListener('beforeinstallprompt', () => {
gotEventsFrom[0] = 'listener';
setTitle();
});
window.onbeforeinstallprompt = () => {
gotEventsFrom[1] = 'attr';
setTitle();
};
});
</script>
</head>
<body onload="initialize()"></body>
</html>
<html>
<head>
<title>Web app banner test page</title>
<script src="main.js"></script>
<script>
// If a "manifest=/path/to/manifest.json" query argument is provided to
// the URL accessing this page, that path is injected as the manifest tag.
// Otherwise, "manifest.json" is used as the manifest tag.
addManifestLinkTag();
window.addEventListener('beforeinstallprompt', function(e) {
console.log('Preventing banner from appearing');
e.preventDefault();
// Navigate the window to trigger the banner cancellation.
window.location.href = "/";
});
</script>
</head>
<body onload="initialize()">Cancels the banner.</body>
</html>
...@@ -2,19 +2,84 @@ ...@@ -2,19 +2,84 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
function initialize() { const Action = {
navigator.serviceWorker.register('service_worker.js'); VERIFY_APPINSTALLED: "verify_appinstalled",
VERIFY_PROMPT_APPINSTALLED: "verify_prompt_appinstalled",
VERIFY_BEFOREINSTALLPROMPT: "verify_beforeinstallprompt",
CALL_PROMPT_DELAYED: "call_prompt_delayed",
CALL_PROMPT_IN_HANDLER: "call_prompt_in_handler",
CANCEL_PROMPT: "cancel_prompt",
STASH_EVENT: "stash_event",
};
const LISTENER = "listener";
const ATTR = "attr";
// These blanks will get filled in when each event comes through.
let gotEventsFrom = ['_'.repeat(LISTENER.length), '_'.repeat(ATTR.length)];
let stashedEvent = null;
function startWorker(worker) {
navigator.serviceWorker.register(worker);
}
function verifyEvents(eventName) {
function setTitle() {
window.document.title = 'Got ' + eventName + ': ' +
gotEventsFrom.join(', ');
}
window.addEventListener(eventName, () => {
gotEventsFrom[0] = LISTENER;
setTitle();
});
window['on' + eventName] = () => {
gotEventsFrom[1] = ATTR;
setTitle();
};
}
function callPrompt(event) {
event.prompt();
}
function callStashedPrompt() {
if (stashedEvent === null) {
throw new Error('No event was previously stashed');
}
stashedEvent.prompt();
}
window.addEventListener('appinstalled', () => { function addPromptListener(action) {
window.document.title = 'Got appinstalled'; window.addEventListener('beforeinstallprompt', function(e) {
e.preventDefault();
switch (action) {
case Action.CALL_PROMPT_DELAYED:
setTimeout(callPrompt, 0, e);
break;
case Action.CALL_PROMPT_IN_HANDLER:
callPrompt(e);
break;
case Action.CANCEL_PROMPT:
// Navigate the window to trigger the banner cancellation.
window.location.href = "/";
break;
case Action.STASH_EVENT:
stashedEvent = e;
break;
}
}); });
} }
function addManifestLinkTag() { function addManifestLinkTag() {
var url = window.location.href; const url = new URL(window.location.href);
var manifestIndex = url.indexOf("?manifest="); let manifestUrl = url.searchParams.get('manifest');
var manifestUrl = if (!manifestUrl) {
(manifestIndex >= 0) ? url.slice(manifestIndex + 10) : 'manifest.json'; manifestUrl = 'manifest.json';
}
var linkTag = document.createElement("link"); var linkTag = document.createElement("link");
linkTag.id = "manifest"; linkTag.id = "manifest";
linkTag.rel = "manifest"; linkTag.rel = "manifest";
...@@ -22,6 +87,40 @@ function addManifestLinkTag() { ...@@ -22,6 +87,40 @@ function addManifestLinkTag() {
document.head.append(linkTag); document.head.append(linkTag);
} }
function initialize() {
const url = new URL(window.location.href);
const action = url.searchParams.get('action');
if (!action) {
return;
}
switch (action) {
case Action.VERIFY_APPINSTALLED:
verifyEvents('appinstalled');
break;
case Action.VERIFY_PROMPT_APPINSTALLED:
addPromptListener("call_prompt_delayed");
verifyEvents('appinstalled');
break;
case Action.VERIFY_BEFOREINSTALLPROMPT:
verifyEvents('beforeinstallprompt');
break;
case Action.CALL_PROMPT_DELAYED:
case Action.CALL_PROMPT_IN_HANDLER:
case Action.CANCEL_PROMPT:
case Action.STASH_EVENT:
addPromptListener(action);
break;
default:
throw new Error("Unrecognised action: " + action);
}
}
function initializeWithWorker(worker) {
startWorker(worker);
initialize();
}
function changeManifestUrl(newManifestUrl) { function changeManifestUrl(newManifestUrl) {
var linkTag = document.getElementById("manifest"); var linkTag = document.getElementById("manifest");
linkTag.href = newManifestUrl; linkTag.href = newManifestUrl;
......
<html> <html>
<head> <head>
<title>Web app banner test page</title> <title>Web app banner test page</title>
<link rel="manifest" href="manifest.json" /> <script src="main.js"></script>
<script>
// If a "manifest=/path/to/manifest.json" query argument is provided to
// the URL accessing this page, that path is injected as the manifest tag.
// Otherwise, "manifest.json" is used as the manifest tag.
addManifestLinkTag();
</script>
</head> </head>
<body> <body>
Do-nothing page with a manifest but no service worker. Do-nothing page with a manifest but no service worker.
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
addManifestLinkTag(); addManifestLinkTag();
</script> </script>
</head> </head>
<body onload="initialize()"> <body onload="initializeWithWorker('service_worker.js')">
Do-nothing page with a service worker. Do-nothing page with a manifest and a service worker.
</body> </body>
</html> </html>
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
<title>Web app banner test page</title> <title>Web app banner test page</title>
<script src="main.js"></script> <script src="main.js"></script>
</head> </head>
<body onload="initialize()"> <script>
Do-nothing page with a service worker, but no manifest. navigator.serviceWorker.register('service_worker.js');
</script>
<body>
Do-nothing page with a service worker, but no manifest.
</body> </body>
</html> </html>
\ No newline at end of file
<html> <html>
<head> <head>
<link rel="manifest" href="manifest.json" /> <title>Web app banner test page</title>
<script src="main.js"></script>
<script> <script>
navigator.serviceWorker.register('service_worker_no_fetch_handler.js'); // If a "manifest=/path/to/manifest.json" query argument is provided to
// the URL accessing this page, that path is injected as the manifest tag.
// Otherwise, "manifest.json" is used as the manifest tag.
addManifestLinkTag();
</script> </script>
</head> </head>
<body> <body onload="initializeWithWorker('service_worker_no_fetch_handler.js')">
Page with a service worker missing a fetch handler. Do-nothing page with a manifest and a service worker missing a fetch handler.
</body> </body>
</html> </html>
<html>
<head>
<title>Native app banner test page</title>
<link rel="manifest" href="play_app_manifest.json" />
</head>
<body>
Do-nothing page with a manifest requesting a native app banner.
</body>
</html>
<html>
<head>
<title>Native app banner test page</title>
<link rel="manifest" href="play_app_url_manifest.json" />
</head>
<body onload="initialize()">
Do-nothing page with a service worker.
</body>
</html>
<html>
<head>
<title>Web app banner test page</title>
<link rel="manifest" href="manifest.json" />
<script src="main.js"></script>
<script>
window.addEventListener('beforeinstallprompt', function(e) {
e.preventDefault();
e.prompt();
});
</script>
</head>
<body onload="initialize()">Cancels the banner, then prompts for it to be shown in the handler.</body>
</html>
<html>
<head>
<title>Web app banner test page</title>
<link rel="manifest" href="manifest.json" />
<script src="main.js"></script>
<script>
var e = 1;
function callPrompt() {
e.prompt();
}
window.addEventListener('beforeinstallprompt', function(event) {
e = event;
});
</script>
</head>
<body onload="initialize()"></body>
</html>
<html>
<head>
<title>Web app banner test page</title>
<link rel="manifest" href="manifest.json" />
<script src="main.js"></script>
<script>
function callPrompt(event) {
event.prompt();
}
window.addEventListener('beforeinstallprompt', function(e) {
e.preventDefault();
setTimeout(callPrompt, 0, e);
});
</script>
</head>
<body onload="initialize()">Cancels the banner, then prompts for it to be shown later.</body>
</html>
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