Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
| index e7e96e3981a3be8c16abb453cbc2043f0370ddd9..63f04cb3d95204acd336266317b9dbc4bd7f4dea 100644 |
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/webapps/WebappNavigationTest.java |
| @@ -4,6 +4,11 @@ |
| package org.chromium.chrome.browser.webapps; |
| +import static org.chromium.base.ApplicationState.HAS_DESTROYED_ACTIVITIES; |
| +import static org.chromium.base.ApplicationState.HAS_PAUSED_ACTIVITIES; |
| +import static org.chromium.base.ApplicationState.HAS_STOPPED_ACTIVITIES; |
| + |
| +import android.app.Activity; |
| import android.content.Intent; |
| import android.graphics.Color; |
| import android.support.test.InstrumentationRegistry; |
| @@ -17,6 +22,7 @@ import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.chromium.base.ApiCompatibilityUtils; |
| +import org.chromium.base.ApplicationStatus; |
| import org.chromium.base.test.util.CommandLineFlags; |
| import org.chromium.base.test.util.Feature; |
| import org.chromium.chrome.R; |
| @@ -24,9 +30,13 @@ import org.chromium.chrome.browser.ChromeSwitches; |
| import org.chromium.chrome.browser.ChromeTabbedActivity; |
| import org.chromium.chrome.browser.ShortcutHelper; |
| import org.chromium.chrome.browser.customtabs.CustomTabActivity; |
| +import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.OverrideUrlLoadingResult; |
| import org.chromium.chrome.browser.firstrun.FirstRunStatus; |
| +import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl; |
| import org.chromium.chrome.test.ChromeJUnit4ClassRunner; |
| import org.chromium.chrome.test.util.browser.contextmenu.ContextMenuUtils; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| import org.chromium.content.browser.test.util.DOMUtils; |
| import org.chromium.net.test.EmbeddedTestServer; |
| import org.chromium.ui.base.PageTransition; |
| @@ -37,6 +47,7 @@ import org.chromium.ui.base.PageTransition; |
| @RunWith(ChromeJUnit4ClassRunner.class) |
| @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) |
| public class WebappNavigationTest { |
| + private static final String YOUTUBE_URL = "https://www.youtube.com/watch?v=EYmjoW4vIX8"; |
| private static final String OFF_ORIGIN_URL = "https://www.google.com/"; |
| private static final String WEB_APP_PATH = "/chrome/test/data/banners/manifest_test_page.html"; |
| private static final String IN_SCOPE_PAGE_PATH = |
| @@ -45,9 +56,6 @@ public class WebappNavigationTest { |
| @Rule |
| public final WebappActivityTestRule mActivityTestRule = new WebappActivityTestRule(); |
| - @Rule |
| - public final TopActivityListener activityListener = new TopActivityListener(); |
| - |
| private EmbeddedTestServer mTestServer; |
| @Before |
| @@ -120,7 +128,7 @@ public class WebappNavigationTest { |
| addAnchor("testId", mTestServer.getURL(IN_SCOPE_PAGE_PATH), "_blank"); |
| DOMUtils.clickNode( |
| mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testId"); |
| - CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity.class); |
| + CustomTabActivity customTab = waitFor(CustomTabActivity.class); |
| mActivityTestRule.waitUntilIdle(customTab); |
| Assert.assertTrue( |
| mActivityTestRule.runJavaScriptCodeInCurrentTab("document.body.textContent") |
| @@ -169,7 +177,7 @@ public class WebappNavigationTest { |
| otherPageUrl, mActivityTestRule.getActivity().getActivityTab().getUrl()); |
| Assert.assertSame( |
| - mActivityTestRule.getActivity(), activityListener.getMostRecentActivity()); |
| + mActivityTestRule.getActivity(), ApplicationStatus.getLastTrackedFocusedActivity()); |
| } |
| @Test |
| @@ -187,13 +195,52 @@ public class WebappNavigationTest { |
| mActivityTestRule.getActivity().getActivityTab(), "myTestAnchorId", |
| R.id.menu_id_open_in_chrome); |
| - ChromeTabbedActivity tabbedChrome = activityListener.waitFor(ChromeTabbedActivity.class); |
| + ChromeTabbedActivity tabbedChrome = waitFor(ChromeTabbedActivity.class); |
| mActivityTestRule.waitUntilIdle(tabbedChrome); |
| // Dropping the TLD as Google can redirect to a local site, so this could fail outside US. |
| Assert.assertTrue(tabbedChrome.getActivityTab().getUrl().startsWith("https://www.google.")); |
| } |
| + @Test |
| + @SmallTest |
| + @Feature({"Webapps"}) |
| + public void testRegularLinkToExternalApp() throws Exception { |
| + runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
| + |
| + InterceptNavigationDelegateImpl navigationDelegate = |
| + mActivityTestRule.getActivity().getActivityTab().getInterceptNavigationDelegate(); |
| + |
| + addAnchor("testLink", YOUTUBE_URL, "_self"); |
| + DOMUtils.clickNode( |
| + mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testLink"); |
| + |
| + waitForExternalAppOrAppPicker(); |
| + Assert.assertEquals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT, |
| + navigationDelegate.getLastOverrideUrlLoadingResultForTests()); |
| + } |
| + |
| + @Test |
| + @SmallTest |
| + @Feature({"Webapps"}) |
| + public void testNewTabLinkToExternalApp() throws Exception { |
| + runWebappActivityAndWaitForIdle(mActivityTestRule.createIntent()); |
| + |
| + addAnchor("testLink", YOUTUBE_URL, "_blank"); |
| + DOMUtils.clickNode( |
| + mActivityTestRule.getActivity().getActivityTab().getContentViewCore(), "testLink"); |
| + |
| + // For _blank anchors, we open the CustomTab which does the redirecting if necessary. |
| + CustomTabActivity customTab = waitFor(CustomTabActivity.class); |
| + |
| + waitForExternalAppOrAppPicker(); |
| + |
| + InterceptNavigationDelegateImpl navigationDelegate = |
| + customTab.getActivityTab().getInterceptNavigationDelegate(); |
| + Assert.assertEquals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT, |
| + navigationDelegate.getLastOverrideUrlLoadingResultForTests()); |
| + } |
| + |
| private void runWebappActivityAndWaitForIdle(Intent intent) throws Exception { |
| mActivityTestRule.startWebappActivity( |
| intent.putExtra(ShortcutHelper.EXTRA_URL, mTestServer.getURL(WEB_APP_PATH))); |
| @@ -203,7 +250,7 @@ public class WebappNavigationTest { |
| } |
| private CustomTabActivity assertCustomTabActivityLaunchedForOffOriginUrl() { |
| - CustomTabActivity customTab = activityListener.waitFor(CustomTabActivity.class); |
| + CustomTabActivity customTab = waitFor(CustomTabActivity.class); |
| mActivityTestRule.waitUntilIdle(customTab); |
| // Dropping the TLD as Google can redirect to a local site, so this could fail outside US. |
| @@ -222,4 +269,28 @@ public class WebappNavigationTest { |
| + "document.body.appendChild(aTag);", |
| id, url, target)); |
| } |
| + |
| + @SuppressWarnings("unchecked") |
| + private <T extends Activity> T waitFor(final Class<T> expectedClass) { |
| + final Activity[] holder = new Activity[1]; |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + holder[0] = ApplicationStatus.getLastTrackedFocusedActivity(); |
| + return holder[0] != null && expectedClass.isAssignableFrom(holder[0].getClass()); |
| + } |
| + }); |
| + return (T) holder[0]; |
| + } |
| + |
| + private void waitForExternalAppOrAppPicker() { |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + 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
|
| + || ApplicationStatus.getStateForApplication() == HAS_STOPPED_ACTIVITIES |
| + || ApplicationStatus.getStateForApplication() == HAS_DESTROYED_ACTIVITIES; |
| + } |
| + }); |
| + } |
| } |