Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabFromChromeExternalNavigationTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabFromChromeExternalNavigationTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabFromChromeExternalNavigationTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..544e9db86581dbbffc4a9b1a5bf9b785d8d22fad |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabFromChromeExternalNavigationTest.java |
| @@ -0,0 +1,162 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.customtabs; |
| + |
| +import android.app.Activity; |
| +import android.content.Intent; |
| +import android.net.Uri; |
| +import android.os.Environment; |
| +import android.test.suitebuilder.annotation.LargeTest; |
| +import android.test.suitebuilder.annotation.MediumTest; |
| +import android.util.Base64; |
| + |
| +import org.chromium.base.ActivityState; |
| +import org.chromium.base.ApplicationStatus; |
| +import org.chromium.base.ApplicationStatus.ActivityStateListener; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.CommandLineFlags; |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.chrome.browser.ChromeSwitches; |
| +import org.chromium.chrome.browser.customtabs.CustomTabDelegateFactory.CustomTabNavigationDelegate; |
| +import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
| +import org.chromium.chrome.browser.externalnav.ExternalNavigationHandler.OverrideUrlLoadingResult; |
| +import org.chromium.chrome.browser.preferences.ChromePreferenceManager; |
| +import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl; |
| +import org.chromium.chrome.browser.tab.Tab; |
| +import org.chromium.chrome.browser.tab.TabDelegateFactory; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| +import org.chromium.net.test.EmbeddedTestServer; |
| + |
| +import java.util.concurrent.Callable; |
| +import java.util.concurrent.atomic.AtomicReference; |
| + |
| +/** |
| + * Tests for external navigation handling of Custom Tabs generated by Chrome. |
| + */ |
| +@CommandLineFlags.Add(ChromeSwitches.HERB_FLAVOR_ELDERBERRY_SWITCH) |
| +public class CustomTabFromChromeExternalNavigationTest extends CustomTabActivityTestBase { |
| + |
| + private EmbeddedTestServer mTestServer; |
| + |
| + @Override |
| + public void setUp() throws Exception { |
| + mTestServer = EmbeddedTestServer.createAndStartFileServer( |
| + getInstrumentation().getContext(), Environment.getExternalStorageDirectory()); |
| + super.setUp(); |
| + |
| + ChromePreferenceManager.getInstance(getInstrumentation().getTargetContext()) |
| + .setCachedHerbFlavor(ChromeSwitches.HERB_FLAVOR_ELDERBERRY); |
| + } |
| + |
| + @Override |
| + public void tearDown() throws Exception { |
| + mTestServer.stopAndDestroyServer(); |
| + super.tearDown(); |
| + } |
| + |
| + @Override |
| + protected void startActivityCompletely(Intent intent) { |
| + final AtomicReference<CustomTabActivity> createdCct = new AtomicReference<>(); |
| + ActivityStateListener stateListener = new ActivityStateListener() { |
| + @Override |
| + public void onActivityStateChange(Activity activity, int newState) { |
| + if (newState != ActivityState.CREATED) return; |
| + if (activity instanceof SeparateTaskCustomTabActivity) { |
| + createdCct.set((CustomTabActivity) activity); |
| + } |
| + } |
| + }; |
| + ApplicationStatus.registerStateListenerForAllActivities(stateListener); |
| + Activity activity = getInstrumentation().startActivitySync(intent); |
| + assertNotNull("Main activity did not start", activity); |
| + try { |
| + CriteriaHelper.pollUiThread(new Criteria() { |
|
Maria
2016/10/11 21:48:16
why do we need to poll UI thread for this? I thoug
Ted C
2016/10/11 23:38:58
Very true. I copied this from other places that e
|
| + @Override |
| + public boolean isSatisfied() { |
| + return createdCct.get() != null; |
| + } |
| + }); |
| + } catch (InterruptedException e) { |
| + fail(e.getMessage()); |
| + } finally { |
| + ApplicationStatus.unregisterActivityStateListener(stateListener); |
| + } |
| + setActivity(createdCct.get()); |
| + } |
| + |
| + private Intent getCustomTabFromChromeIntent(final String url) { |
| + return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Intent>() { |
| + @Override |
| + public Intent call() throws Exception { |
| + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| + return ChromeLauncherActivity.createCustomTabActivityIntent( |
| + getInstrumentation().getTargetContext(), intent, true); |
| + } |
| + }); |
| + |
| + } |
| + |
| + private void startCustomTabFromChrome(String url) throws InterruptedException { |
| + startCustomTabActivityWithIntent(getCustomTabFromChromeIntent(url)); |
| + } |
| + |
| + @Feature("CustomTabFromChrome") |
| + @MediumTest |
| + public void testUsingStandardExternalNavigationHandler() throws Exception { |
| + startCustomTabFromChrome("about:blank"); |
| + |
| + Tab tab = getActivity().getActivityTab(); |
| + TabDelegateFactory delegateFactory = tab.getDelegateFactory(); |
| + assertTrue(delegateFactory instanceof CustomTabDelegateFactory); |
| + CustomTabDelegateFactory customTabDelegateFactory = |
| + ((CustomTabDelegateFactory) delegateFactory); |
| + assertFalse(customTabDelegateFactory.getExternalNavigationDelegate() |
| + instanceof CustomTabNavigationDelegate); |
| + } |
| + |
| + @Feature("CustomTabFromChrome") |
| + @LargeTest |
| + public void testIntentWithRedirectToApp() throws Exception { |
| + final String redirectUrl = "https://maps.google.com/maps?q=1600+amphitheatre+parkway"; |
| + final String initialUrl = mTestServer.getURL( |
| + "/chrome/test/data/android/redirect/js_redirect.html" |
| + + "?replace_text=" |
| + + Base64.encodeToString("PARAM_URL".getBytes("utf-8"), Base64.URL_SAFE) + ":" |
| + + Base64.encodeToString(redirectUrl.getBytes("utf-8"), Base64.URL_SAFE)); |
| + |
| + startActivityCompletely(getCustomTabFromChromeIntent(initialUrl)); |
| + |
| + final AtomicReference<InterceptNavigationDelegateImpl> navigationDelegate = |
| + new AtomicReference<>(); |
| + CriteriaHelper.pollUiThread(new Criteria("Tab never selected/initialized.") { |
| + @Override |
| + public boolean isSatisfied() { |
| + Tab tab = getActivity().getActivityTab(); |
| + if (tab == null || tab.getInterceptNavigationDelegate() == null) return false; |
| + navigationDelegate.set(tab.getInterceptNavigationDelegate()); |
| + return true; |
| + } |
| + }); |
| + |
| + Criteria.equals(OverrideUrlLoadingResult.OVERRIDE_WITH_EXTERNAL_INTENT, |
| + new Callable<OverrideUrlLoadingResult>() { |
| + @Override |
| + public OverrideUrlLoadingResult call() throws Exception { |
| + return navigationDelegate.get().getLastOverrideUrlLoadingResultForTests(); |
| + } |
| + }); |
| + |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
|
Maria
2016/10/11 21:48:16
Hmm, do we restart everything on per-testcase or p
Ted C
2016/10/11 23:38:58
Switched the ordering and it ran (tried both the r
|
| + int activityState = ApplicationStatus.getStateForActivity(getActivity()); |
| + return activityState == ActivityState.STOPPED |
| + || activityState == ActivityState.DESTROYED; |
| + } |
| + }); |
| + } |
| +} |