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 CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity
.class); |
| 107 |
| 108 mActivityTestRule.waitUntilIdle(customTab); |
| 109 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. |
| 110 Assert.assertTrue(customTab.getActivityTab().getUrl().startsWith("https:
//www.google.")); |
| 111 |
| 112 return customTab; |
| 113 } |
| 114 |
| 115 @Test |
| 116 @SmallTest |
| 117 @Feature({"Webapps"}) |
| 118 public void testInOriginNavigationStaysInWebapp() throws Exception { |
| 119 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
| 120 |
| 121 String otherPageUrl = mTestServer.getURL(OTHER_PAGE_PATH); |
| 122 mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, |
| 123 mActivityTestRule.getActivity().getActivityTab()); |
| 124 |
| 125 mActivityTestRule.waitUntilIdle(); |
| 126 Assert.assertEquals( |
| 127 otherPageUrl, mActivityTestRule.getActivity().getActivityTab().g
etUrl()); |
| 128 |
| 129 Assert.assertSame( |
| 130 mActivityTestRule.getActivity(), activityListener.getMostRecentA
ctivity()); |
| 131 } |
| 132 |
| 133 @Test |
| 134 @SmallTest |
| 135 @Feature({"Webapps"}) |
| 136 public void testOpenInChromeFromContextMenuTabbedChrome() throws Exception { |
| 137 // Needed to get full context menu. |
| 138 FirstRunStatus.setFirstRunFlowComplete(true); |
| 139 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
| 140 |
| 141 mActivityTestRule.runJavaScriptCodeInCurrentTab("var aTag = document.cre
ateElement('a');" |
| 142 + "aTag.id = 'myTestAnchorId';" |
| 143 + "aTag.setAttribute('href','https://www.google.com/');" |
| 144 + "aTag.innerHTML = 'Click Me!';" |
| 145 + "document.body.appendChild(aTag);"); |
| 146 |
| 147 ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstru
mentation(), |
| 148 null /* activity to check for focus after click */, |
| 149 mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorI
d", |
| 150 R.id.menu_id_open_in_chrome); |
| 151 |
| 152 ChromeTabbedActivity tabbedChrome = activityListener.waitFor(ChromeTabbe
dActivity.class); |
| 153 |
| 154 mActivityTestRule.waitUntilIdle(tabbedChrome); |
| 155 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. |
| 156 Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("htt
ps://www.google.")); |
| 157 } |
| 158 |
| 159 private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception
{ |
| 160 mActivityTestRule.startWebappActivity( |
| 161 intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB
_APP_PATH))); |
| 162 |
| 163 mActivityTestRule.waitUntilSplashscreenHides(); |
| 164 mActivityTestRule.waitUntilIdle(); |
| 165 } |
| 166 } |
OLD | NEW |