| Index: chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java
|
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9605f72966aa6c74bffe239df5b31e8dae55426d
|
| --- /dev/null
|
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java
|
| @@ -0,0 +1,135 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.webapps;
|
| +
|
| +import android.app.Activity;
|
| +import android.content.Intent;
|
| +import android.graphics.Color;
|
| +import android.support.test.filters.SmallTest;
|
| +
|
| +import org.chromium.base.ActivityState;
|
| +import org.chromium.base.ApiCompatibilityUtils;
|
| +import org.chromium.base.ApplicationStatus;
|
| +import org.chromium.base.ApplicationStatus.ActivityStateListener;
|
| +import org.chromium.base.ThreadUtils;
|
| +import org.chromium.base.test.util.Feature;
|
| +import org.chromium.chrome.R;
|
| +import org.chromium.chrome.browser.ShortcutHelper;
|
| +import org.chromium.chrome.browser.customtabs.CustomTabActivity;
|
| +import org.chromium.content.browser.test.util.Criteria;
|
| +import org.chromium.content.browser.test.util.CriteriaHelper;
|
| +import org.chromium.content_public.browser.LoadUrlParams;
|
| +import org.chromium.net.test.EmbeddedTestServer;
|
| +import org.chromium.ui.base.PageTransition;
|
| +
|
| +/**
|
| + * Tests web navigations originating from a WebappActivity.
|
| + */
|
| +public class WebappNavigationTest extends WebappActivityTestBase implements ActivityStateListener {
|
| + private static final String OFF_ORIGIN_URL = "https://www.google.com/";
|
| + private static final String WEB_APP_PATH = "/chrome/test/data/banners/manifest_test_page.html";
|
| + private static final String OTHER_PAGE_PATH =
|
| + "/chrome/test/data/banners/manifest_no_service_worker.html";
|
| +
|
| + private EmbeddedTestServer mTestServer;
|
| + private Activity mMostRecentActivity;
|
| +
|
| + @Override
|
| + protected void setUp() throws Exception {
|
| + super.setUp();
|
| + mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation().getContext());
|
| + ApplicationStatus.registerStateListenerForAllActivities(this);
|
| + }
|
| +
|
| + @Override
|
| + protected void tearDown() throws Exception {
|
| + ApplicationStatus.unregisterActivityStateListener(this);
|
| + mTestServer.stopAndDestroyServer();
|
| + super.tearDown();
|
| + }
|
| +
|
| + @Override
|
| + public void onActivityStateChange(Activity activity, int newState) {
|
| + if (newState == ActivityState.RESUMED) {
|
| + mMostRecentActivity = activity;
|
| + }
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Webapps"})
|
| + public void testOffOriginNavigationUsingLinkAndNoWebappThemeColor() throws Exception {
|
| + runWebappActivityAndWaitForIdle(createIntent());
|
| +
|
| + // Not using #loadUrl, as it expects the URL to load in the activity under test,
|
| + // which is not happening here.
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + getActivity().getActivityTab().loadUrl(
|
| + new LoadUrlParams(OFF_ORIGIN_URL, PageTransition.LINK));
|
| + }
|
| + });
|
| +
|
| + CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl();
|
| +
|
| + assertEquals("CCT Toolbar should use default primary color if theme color is not specified",
|
| + ApiCompatibilityUtils.getColor(
|
| + customTab.getResources(), R.color.default_primary_color),
|
| + customTab.getToolbarManager().getPrimaryColor());
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Webapps"})
|
| + public void testOffOriginNavigationUsingJavaScriptAndWebappThemeColor() throws Exception {
|
| + runWebappActivityAndWaitForIdle(
|
| + createIntent().putExtra(ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN));
|
| +
|
| + runJavaScriptCodeInCurrentTab(String.format("window.top.location = '%s'", OFF_ORIGIN_URL));
|
| +
|
| + CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOriginUrl();
|
| +
|
| + assertEquals("CCT Toolbar should use the theme color of a webapp", Color.CYAN,
|
| + customTab.getToolbarManager().getPrimaryColor());
|
| + }
|
| +
|
| + private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() {
|
| + CriteriaHelper.pollUiThread(new Criteria() {
|
| + @Override
|
| + public boolean isSatisfied() {
|
| + return mMostRecentActivity instanceof CustomTabActivity;
|
| + }
|
| + });
|
| +
|
| + CustomTabActivity customTab = (CustomTabActivity) mMostRecentActivity;
|
| +
|
| + waitUntilIdle(customTab);
|
| + // Dropping the TLD as Google can redirect to a local site, so this could fail outside US.
|
| + assertTrue(customTab.getActivityTab().getUrl().startsWith("https://www.google."));
|
| +
|
| + return customTab;
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"Webapps"})
|
| + public void testInOriginNavigationStaysInWebapp() throws Exception {
|
| + runWebappActivityAndWaitForIdle(createIntent());
|
| +
|
| + String otherPageUrl = mTestServer.getURL(OTHER_PAGE_PATH);
|
| + loadUrlInTab(otherPageUrl, PageTransition.LINK, getActivity().getActivityTab());
|
| +
|
| + waitUntilIdle();
|
| + assertEquals(otherPageUrl, getActivity().getActivityTab().getUrl());
|
| +
|
| + assertSame(getActivity(), mMostRecentActivity);
|
| + }
|
| +
|
| + private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception {
|
| + startWebappActivity(
|
| + intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB_APP_PATH)));
|
| +
|
| + waitUntilSplashscreenHides();
|
| + waitUntilIdle();
|
| + }
|
| +}
|
|
|