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