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