| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.webapps; | |
| 6 | |
| 7 import android.content.Intent; | |
| 8 import android.graphics.Color; | |
| 9 import android.support.test.InstrumentationRegistry; | |
| 10 import android.support.test.filters.SmallTest; | |
| 11 | |
| 12 import org.junit.After; | |
| 13 import org.junit.Assert; | |
| 14 import org.junit.Before; | |
| 15 import org.junit.Rule; | |
| 16 import org.junit.Test; | |
| 17 import org.junit.runner.RunWith; | |
| 18 | |
| 19 import org.chromium.base.ApiCompatibilityUtils; | |
| 20 import org.chromium.base.ThreadUtils; | |
| 21 import org.chromium.base.test.util.CommandLineFlags; | |
| 22 import org.chromium.base.test.util.Feature; | |
| 23 import org.chromium.chrome.R; | |
| 24 import org.chromium.chrome.browser.ChromeSwitches; | |
| 25 import org.chromium.chrome.browser.ChromeTabbedActivity; | |
| 26 import org.chromium.chrome.browser.ShortcutHelper; | |
| 27 import org.chromium.chrome.browser.customtabs.CustomTabActivity; | |
| 28 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
| 29 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; | |
| 30 import org.chromium.content_public.browser.LoadUrlParams; | |
| 31 import org.chromium.net.test.EmbeddedTestServer; | |
| 32 import org.chromium.ui.base.PageTransition; | |
| 33 | |
| 34 /** | |
| 35 * Tests web navigations originating from a WebappActivity. | |
| 36 */ | |
| 37 @RunWith(ChromeJUnit4ClassRunner.class) | |
| 38 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) | |
| 39 public class WebappNavigationTest { | |
| 40 private static final String OFF_ORIGIN_URL = "https://www.google.com/"; | |
| 41 private static final String WEB_APP_PATH = "/chrome/test/data/banners/manife
st_test_page.html"; | |
| 42 private static final String OTHER_PAGE_PATH = | |
| 43 "/chrome/test/data/banners/manifest_no_service_worker.html"; | |
| 44 | |
| 45 @Rule | |
| 46 public final WebappActivityTestRule mActivityTestRule = new WebappActivityTe
stRule(); | |
| 47 | |
| 48 @Rule | |
| 49 public final TopActivityListener activityListener = new TopActivityListener(
); | |
| 50 | |
| 51 private EmbeddedTestServer mTestServer; | |
| 52 | |
| 53 @Before | |
| 54 public void setUp() throws Exception { | |
| 55 mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationReg
istry.getContext()); | |
| 56 } | |
| 57 | |
| 58 @After | |
| 59 public void tearDown() throws Exception { | |
| 60 mTestServer.stopAndDestroyServer(); | |
| 61 } | |
| 62 | |
| 63 @Test | |
| 64 @SmallTest | |
| 65 @Feature({"Webapps"}) | |
| 66 public void testOffOriginNavigationUsingLinkAndNoWebappThemeColor() throws E
xception { | |
| 67 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | |
| 68 | |
| 69 // Not using #loadUrl, as it expects the URL to load in the activity und
er test, | |
| 70 // which is not happening here. | |
| 71 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
| 72 @Override | |
| 73 public void run() { | |
| 74 mActivityTestRule.getActivity().getActivityTab().loadUrl( | |
| 75 new LoadUrlParams(OFF_ORIGIN_URL, PageTransition.LINK)); | |
| 76 } | |
| 77 }); | |
| 78 | |
| 79 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); | |
| 80 | |
| 81 Assert.assertEquals( | |
| 82 "CCT Toolbar should use default primary color if theme color is
not specified", | |
| 83 ApiCompatibilityUtils.getColor( | |
| 84 customTab.getResources(), R.color.default_primary_color)
, | |
| 85 customTab.getToolbarManager().getPrimaryColor()); | |
| 86 } | |
| 87 | |
| 88 @Test | |
| 89 @SmallTest | |
| 90 @Feature({"Webapps"}) | |
| 91 public void testOffOriginNavigationUsingJavaScriptAndWebappThemeColor() thro
ws Exception { | |
| 92 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtr
a( | |
| 93 ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); | |
| 94 | |
| 95 mActivityTestRule.runJavaScriptCodeInCurrentTab( | |
| 96 String.format("window.top.location = '%s'", OFF_ORIGIN_URL)); | |
| 97 | |
| 98 CustomTabActivity customTab = assertCustomTabActivityLaunchedForOffOrigi
nUrl(); | |
| 99 | |
| 100 Assert.assertEquals("CCT Toolbar should use the theme color of a webapp"
, Color.CYAN, | |
| 101 customTab.getToolbarManager().getPrimaryColor()); | |
| 102 } | |
| 103 | |
| 104 private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { | |
| 105 activityListener.waitFor(CustomTabActivity.class); | |
| 106 | |
| 107 CustomTabActivity customTab = (CustomTabActivity) activityListener.getMo
stRecentActivity(); | |
| 108 | |
| 109 mActivityTestRule.waitUntilIdle(customTab); | |
| 110 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. | |
| 111 Assert.assertTrue(customTab.getActivityTab().getUrl().startsWith("https:
//www.google.")); | |
| 112 | |
| 113 return customTab; | |
| 114 } | |
| 115 | |
| 116 @Test | |
| 117 @SmallTest | |
| 118 @Feature({"Webapps"}) | |
| 119 public void testInOriginNavigationStaysInWebapp() throws Exception { | |
| 120 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | |
| 121 | |
| 122 String otherPageUrl = mTestServer.getURL(OTHER_PAGE_PATH); | |
| 123 mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, | |
| 124 mActivityTestRule.getActivity().getActivityTab()); | |
| 125 | |
| 126 mActivityTestRule.waitUntilIdle(); | |
| 127 Assert.assertEquals( | |
| 128 otherPageUrl, mActivityTestRule.getActivity().getActivityTab().g
etUrl()); | |
| 129 | |
| 130 Assert.assertSame( | |
| 131 mActivityTestRule.getActivity(), activityListener.getMostRecentA
ctivity()); | |
| 132 } | |
| 133 | |
| 134 @Test | |
| 135 @SmallTest | |
| 136 @Feature({"Webapps"}) | |
| 137 public void testOpenInChromeFromContextMenuTabbedChrome() throws Exception { | |
| 138 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | |
| 139 | |
| 140 mActivityTestRule.runJavaScriptCodeInCurrentTab("var aTag = document.cre
ateElement('a');" | |
| 141 + "aTag.id = 'myTestAnchorId';" | |
| 142 + "aTag.setAttribute('href','https://www.google.com/');" | |
| 143 + "aTag.innerHTML = 'Click Me!';" | |
| 144 + "document.body.appendChild(aTag);"); | |
| 145 | |
| 146 ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstru
mentation(), | |
| 147 null /* activity to check for focus after click */, | |
| 148 mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorI
d", | |
| 149 R.id.menu_id_open_in_chrome); | |
| 150 | |
| 151 activityListener.waitFor(ChromeTabbedActivity.class); | |
| 152 | |
| 153 ChromeTabbedActivity tabbedChrome = | |
| 154 (ChromeTabbedActivity) activityListener.getMostRecentActivity(); | |
| 155 | |
| 156 mActivityTestRule.waitUntilIdle(tabbedChrome); | |
| 157 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. | |
| 158 Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("htt
ps://www.google.")); | |
| 159 } | |
| 160 | |
| 161 private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception
{ | |
| 162 mActivityTestRule.startWebappActivity( | |
| 163 intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB
_APP_PATH))); | |
| 164 | |
| 165 mActivityTestRule.waitUntilSplashscreenHides(); | |
| 166 mActivityTestRule.waitUntilIdle(); | |
| 167 } | |
| 168 } | |
| OLD | NEW |