| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
| 6 | 6 |
| 7 import static org.chromium.base.ApplicationState.HAS_DESTROYED_ACTIVITIES; | |
| 8 import static org.chromium.base.ApplicationState.HAS_PAUSED_ACTIVITIES; | |
| 9 import static org.chromium.base.ApplicationState.HAS_STOPPED_ACTIVITIES; | |
| 10 | |
| 11 import android.app.Activity; | |
| 12 import android.content.Intent; | 7 import android.content.Intent; |
| 13 import android.graphics.Color; | 8 import android.graphics.Color; |
| 14 import android.support.test.InstrumentationRegistry; | 9 import android.support.test.InstrumentationRegistry; |
| 15 import android.support.test.filters.SmallTest; | 10 import android.support.test.filters.SmallTest; |
| 16 | 11 |
| 17 import org.junit.After; | 12 import org.junit.After; |
| 18 import org.junit.Assert; | 13 import org.junit.Assert; |
| 19 import org.junit.Before; | 14 import org.junit.Before; |
| 20 import org.junit.Rule; | 15 import org.junit.Rule; |
| 21 import org.junit.Test; | 16 import org.junit.Test; |
| 22 import org.junit.runner.RunWith; | 17 import org.junit.runner.RunWith; |
| 23 | 18 |
| 24 import org.chromium.base.ApiCompatibilityUtils; | 19 import org.chromium.base.ApiCompatibilityUtils; |
| 25 import org.chromium.base.ApplicationStatus; | |
| 26 import org.chromium.base.ThreadUtils; | |
| 27 import org.chromium.base.test.util.CommandLineFlags; | 20 import org.chromium.base.test.util.CommandLineFlags; |
| 28 import org.chromium.base.test.util.Feature; | 21 import org.chromium.base.test.util.Feature; |
| 29 import org.chromium.chrome.R; | 22 import org.chromium.chrome.R; |
| 30 import org.chromium.chrome.browser.ChromeActivity; | |
| 31 import org.chromium.chrome.browser.ChromeSwitches; | 23 import org.chromium.chrome.browser.ChromeSwitches; |
| 32 import org.chromium.chrome.browser.ChromeTabbedActivity; | 24 import org.chromium.chrome.browser.ChromeTabbedActivity; |
| 33 import org.chromium.chrome.browser.ShortcutHelper; | 25 import org.chromium.chrome.browser.ShortcutHelper; |
| 34 import org.chromium.chrome.browser.customtabs.CustomTabActivity; | 26 import org.chromium.chrome.browser.customtabs.CustomTabActivity; |
| 35 import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.Overrid
eUrlLoadingResult; | |
| 36 import org.chromium.chrome.browser.firstrun.FirstRunStatus; | 27 import org.chromium.chrome.browser.firstrun.FirstRunStatus; |
| 37 import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl; | |
| 38 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | 28 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| 39 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; | 29 import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; |
| 40 import org.chromium.content.browser.test.util.Criteria; | |
| 41 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 42 import org.chromium.content.browser.test.util.DOMUtils; | 30 import org.chromium.content.browser.test.util.DOMUtils; |
| 43 import org.chromium.net.test.EmbeddedTestServer; | 31 import org.chromium.net.test.EmbeddedTestServer; |
| 44 import org.chromium.ui.base.PageTransition; | 32 import org.chromium.ui.base.PageTransition; |
| 45 | 33 |
| 46 import java.util.concurrent.Callable; | |
| 47 | |
| 48 /** | 34 /** |
| 49 * Tests web navigations originating from a WebappActivity. | 35 * Tests web navigations originating from a WebappActivity. |
| 50 */ | 36 */ |
| 51 @RunWith(ChromeJUnit4ClassRunner.class) | 37 @RunWith(ChromeJUnit4ClassRunner.class) |
| 52 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) | 38 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) |
| 53 public class WebappNavigationTest { | 39 public class WebappNavigationTest { |
| 54 private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=E
YmjoW4vIX8"; | |
| 55 private static final String OFF_ORIGIN_URL = "https://www.google.com/"; | 40 private static final String OFF_ORIGIN_URL = "https://www.google.com/"; |
| 56 private static final String WEB_APP_PATH = "/chrome/test/data/banners/manife
st_test_page.html"; | 41 private static final String WEB_APP_PATH = "/chrome/test/data/banners/manife
st_test_page.html"; |
| 57 private static final String IN_SCOPE_PAGE_PATH = | 42 private static final String IN_SCOPE_PAGE_PATH = |
| 58 "/chrome/test/data/banners/manifest_no_service_worker.html"; | 43 "/chrome/test/data/banners/manifest_no_service_worker.html"; |
| 59 | 44 |
| 60 @Rule | 45 @Rule |
| 61 public final WebappActivityTestRule mActivityTestRule = new WebappActivityTe
stRule(); | 46 public final WebappActivityTestRule mActivityTestRule = new WebappActivityTe
stRule(); |
| 62 | 47 |
| 48 @Rule |
| 49 public final TopActivityListener activityListener = new TopActivityListener(
); |
| 50 |
| 63 private EmbeddedTestServer mTestServer; | 51 private EmbeddedTestServer mTestServer; |
| 64 | 52 |
| 65 @Before | 53 @Before |
| 66 public void setUp() throws Exception { | 54 public void setUp() throws Exception { |
| 67 mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationReg
istry.getContext()); | 55 mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationReg
istry.getContext()); |
| 68 } | 56 } |
| 69 | 57 |
| 70 @After | 58 @After |
| 71 public void tearDown() throws Exception { | 59 public void tearDown() throws Exception { |
| 72 mTestServer.stopAndDestroyServer(); | 60 mTestServer.stopAndDestroyServer(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 113 |
| 126 @Test | 114 @Test |
| 127 @SmallTest | 115 @SmallTest |
| 128 @Feature({"Webapps"}) | 116 @Feature({"Webapps"}) |
| 129 public void testInScopeNewTabLinkOpensInCct() throws Exception { | 117 public void testInScopeNewTabLinkOpensInCct() throws Exception { |
| 130 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtr
a( | 118 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent().putExtr
a( |
| 131 ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); | 119 ShortcutHelper.EXTRA_THEME_COLOR, (long) Color.CYAN)); |
| 132 addAnchor("testId", mTestServer.getURL(IN_SCOPE_PAGE_PATH), "_blank"); | 120 addAnchor("testId", mTestServer.getURL(IN_SCOPE_PAGE_PATH), "_blank"); |
| 133 DOMUtils.clickNode( | 121 DOMUtils.clickNode( |
| 134 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testId"); | 122 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testId"); |
| 135 CustomTabActivity customTab = waitFor(CustomTabActivity.class); | 123 CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity
.class); |
| 136 mActivityTestRule.waitUntilIdle(customTab); | 124 mActivityTestRule.waitUntilIdle(customTab); |
| 137 Assert.assertTrue( | 125 Assert.assertTrue( |
| 138 mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.t
extContent") | 126 mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.t
extContent") |
| 139 .contains("Do-nothing page with a service worker")); | 127 .contains("Do-nothing page with a service worker")); |
| 140 } | 128 } |
| 141 | 129 |
| 142 @Test | 130 @Test |
| 143 @SmallTest | 131 @SmallTest |
| 144 @Feature({"Webapps"}) | 132 @Feature({"Webapps"}) |
| 145 public void testWindowOpenInCct() throws Exception { | 133 public void testWindowOpenInCct() throws Exception { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 174 | 162 |
| 175 String otherPageUrl = mTestServer.getURL(IN_SCOPE_PAGE_PATH); | 163 String otherPageUrl = mTestServer.getURL(IN_SCOPE_PAGE_PATH); |
| 176 mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, | 164 mActivityTestRule.loadUrlInTab(otherPageUrl, PageTransition.LINK, |
| 177 mActivityTestRule.getActivity().getActivityTab()); | 165 mActivityTestRule.getActivity().getActivityTab()); |
| 178 | 166 |
| 179 mActivityTestRule.waitUntilIdle(); | 167 mActivityTestRule.waitUntilIdle(); |
| 180 Assert.assertEquals( | 168 Assert.assertEquals( |
| 181 otherPageUrl, mActivityTestRule.getActivity().getActivityTab().g
etUrl()); | 169 otherPageUrl, mActivityTestRule.getActivity().getActivityTab().g
etUrl()); |
| 182 | 170 |
| 183 Assert.assertSame( | 171 Assert.assertSame( |
| 184 mActivityTestRule.getActivity(), ApplicationStatus.getLastTracke
dFocusedActivity()); | 172 mActivityTestRule.getActivity(), activityListener.getMostRecentA
ctivity()); |
| 185 } | 173 } |
| 186 | 174 |
| 187 @Test | 175 @Test |
| 188 @SmallTest | 176 @SmallTest |
| 189 @Feature({"Webapps"}) | 177 @Feature({"Webapps"}) |
| 190 public void testOpenInChromeFromContextMenuTabbedChrome() throws Exception { | 178 public void testOpenInChromeFromContextMenuTabbedChrome() throws Exception { |
| 191 // Needed to get full context menu. | 179 // Needed to get full context menu. |
| 192 FirstRunStatus.setFirstRunFlowComplete(true); | 180 FirstRunStatus.setFirstRunFlowComplete(true); |
| 193 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | 181 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
| 194 | 182 |
| 195 addAnchor("myTestAnchorId", OFF_ORIGIN_URL, "_self"); | 183 addAnchor("myTestAnchorId", OFF_ORIGIN_URL, "_self"); |
| 196 | 184 |
| 197 ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstru
mentation(), | 185 ContextMenuUtils.selectContextMenuItem(InstrumentationRegistry.getInstru
mentation(), |
| 198 null /* activity to check for focus after click */, | 186 null /* activity to check for focus after click */, |
| 199 mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorI
d", | 187 mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorI
d", |
| 200 R.id.menu_id_open_in_chrome); | 188 R.id.menu_id_open_in_chrome); |
| 201 | 189 |
| 202 ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class); | 190 ChromeTabbedActivity tabbedChrome = activityListener.waitFor(ChromeTabbe
dActivity.class); |
| 203 | 191 |
| 204 mActivityTestRule.waitUntilIdle(tabbedChrome); | 192 mActivityTestRule.waitUntilIdle(tabbedChrome); |
| 205 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. | 193 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. |
| 206 Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("htt
ps://www.google.")); | 194 Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("htt
ps://www.google.")); |
| 207 } | 195 } |
| 208 | 196 |
| 209 @Test | |
| 210 @SmallTest | |
| 211 @Feature({"Webapps"}) | |
| 212 public void testRegularLinkToExternalApp() throws Exception { | |
| 213 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | |
| 214 | |
| 215 InterceptNavigationDelegateImpl navigationDelegate = | |
| 216 mActivityTestRule.getActivity().getActivityTab().getInterceptNav
igationDelegate(); | |
| 217 | |
| 218 addAnchor("testLink", YOUTUBE_URL, "_self"); | |
| 219 DOMUtils.clickNode( | |
| 220 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testLink"); | |
| 221 | |
| 222 waitForExternalAppOrIntentPicker(); | |
| 223 Assert.assertEquals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTE
NT, | |
| 224 navigationDelegate.getLastOverrideUrlLoadingResultForTests()); | |
| 225 } | |
| 226 | |
| 227 @Test | |
| 228 @SmallTest | |
| 229 @Feature({"Webapps"}) | |
| 230 public void testNewTabLinkToExternalApp() throws Exception { | |
| 231 runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); | |
| 232 | |
| 233 addAnchor("testLink", YOUTUBE_URL, "_blank"); | |
| 234 DOMUtils.clickNode( | |
| 235 mActivityTestRule.getActivity().getActivityTab().getContentViewC
ore(), "testLink"); | |
| 236 | |
| 237 // For _blank anchors, we open the CustomTab which does the redirecting
if necessary. | |
| 238 CustomTabActivity customTab = waitFor(CustomTabActivity.class); | |
| 239 | |
| 240 waitForExternalAppOrIntentPicker(); | |
| 241 | |
| 242 InterceptNavigationDelegateImpl navigationDelegate = | |
| 243 customTab.getActivityTab().getInterceptNavigationDelegate(); | |
| 244 Assert.assertEquals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTE
NT, | |
| 245 navigationDelegate.getLastOverrideUrlLoadingResultForTests()); | |
| 246 } | |
| 247 | |
| 248 private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception
{ | 197 private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception
{ |
| 249 mActivityTestRule.startWebappActivity( | 198 mActivityTestRule.startWebappActivity( |
| 250 intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB
_APP_PATH))); | 199 intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB
_APP_PATH))); |
| 251 | 200 |
| 252 mActivityTestRule.waitUntilSplashscreenHides(); | 201 mActivityTestRule.waitUntilSplashscreenHides(); |
| 253 mActivityTestRule.waitUntilIdle(); | 202 mActivityTestRule.waitUntilIdle(); |
| 254 } | 203 } |
| 255 | 204 |
| 256 private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { | 205 private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { |
| 257 CustomTabActivity customTab = waitFor(CustomTabActivity.class); | 206 CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity
.class); |
| 258 | 207 |
| 259 mActivityTestRule.waitUntilIdle(customTab); | 208 mActivityTestRule.waitUntilIdle(customTab); |
| 260 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. | 209 // Dropping the TLD as Google can redirect to a local site, so this coul
d fail outside US. |
| 261 Assert.assertTrue(customTab.getActivityTab().getUrl().contains("https://
www.google.")); | 210 Assert.assertTrue(customTab.getActivityTab().getUrl().contains("https://
www.google.")); |
| 262 | 211 |
| 263 return customTab; | 212 return customTab; |
| 264 } | 213 } |
| 265 | 214 |
| 266 private void addAnchor(String id, String url, String target) throws Exceptio
n { | 215 private void addAnchor(String id, String url, String target) throws Exceptio
n { |
| 267 mActivityTestRule.runJavaScriptCodeInCurrentTab( | 216 mActivityTestRule.runJavaScriptCodeInCurrentTab( |
| 268 String.format("var aTag = document.createElement('a');" | 217 String.format("var aTag = document.createElement('a');" |
| 269 + "aTag.id = '%s';" | 218 + "aTag.id = '%s';" |
| 270 + "aTag.setAttribute('href','%s');" | 219 + "aTag.setAttribute('href','%s');" |
| 271 + "aTag.setAttribute('target','%s');" | 220 + "aTag.setAttribute('target','%s');" |
| 272 + "aTag.innerHTML = 'Click Me!';" | 221 + "aTag.innerHTML = 'Click Me!';" |
| 273 + "document.body.appendChild(aTag);", | 222 + "document.body.appendChild(aTag);", |
| 274 id, url, target)); | 223 id, url, target)); |
| 275 } | 224 } |
| 276 | |
| 277 @SuppressWarnings("unchecked") | |
| 278 private <T extends Activity> T waitFor(final Class<T> expectedClass) { | |
| 279 final Activity[] holder = new Activity[1]; | |
| 280 CriteriaHelper.pollUiThread(new Criteria() { | |
| 281 @Override | |
| 282 public boolean isSatisfied() { | |
| 283 holder[0] = ApplicationStatus.getLastTrackedFocusedActivity(); | |
| 284 return holder[0] != null && expectedClass.isAssignableFrom(holde
r[0].getClass()); | |
| 285 } | |
| 286 }); | |
| 287 return (T) holder[0]; | |
| 288 } | |
| 289 | |
| 290 private void waitForExternalAppOrIntentPicker() { | |
| 291 final Callable<Boolean> callable = new Callable<Boolean>() { | |
| 292 @Override | |
| 293 public Boolean call() throws Exception { | |
| 294 return ApplicationStatus.getStateForApplication() == HAS_PAUSED_
ACTIVITIES | |
| 295 || ApplicationStatus.getStateForApplication() == HAS_STO
PPED_ACTIVITIES | |
| 296 || ApplicationStatus.getStateForApplication() == HAS_DES
TROYED_ACTIVITIES; | |
| 297 } | |
| 298 }; | |
| 299 CriteriaHelper.pollInstrumentationThread(new Criteria() { | |
| 300 @Override | |
| 301 public boolean isSatisfied() { | |
| 302 return ThreadUtils.runOnUiThreadBlockingNoException(callable); | |
| 303 } | |
| 304 | |
| 305 @Override | |
| 306 public String getFailureReason() { | |
| 307 Activity lastFocusedActivity = ApplicationStatus.getLastTrackedF
ocusedActivity(); | |
| 308 return String.format( | |
| 309 "Application state is: %d, most recent activity is %s an
d it is on URL %s", | |
| 310 ApplicationStatus.getStateForApplication(), | |
| 311 lastFocusedActivity.getClass().getSimpleName(), | |
| 312 lastFocusedActivity instanceof ChromeActivity | |
| 313 ? ((ChromeActivity) lastFocusedActivity).getActi
vityTab().getUrl() | |
| 314 : "[not a ChromeActivity]"); | |
| 315 } | |
| 316 }, CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL * 2, CriteriaHelper.DEFAULT_P
OLLING_INTERVAL); | |
| 317 } | |
| 318 } | 225 } |
| OLD | NEW |