 Chromium Code Reviews
 Chromium Code Reviews Issue 2748013004:
  CustomTabs: Base version for prerender switch  (Closed)
    
  
    Issue 2748013004:
  CustomTabs: Base version for prerender switch  (Closed) 
  | Index: chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java | 
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java | 
| index fd2d93a561410358a5caeebce1b2e7c15f0386e6..5b9adf777c9a0e9a27d907717e081a7b1997747f 100644 | 
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java | 
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/customtabs/CustomTabActivityTest.java | 
| @@ -88,6 +88,7 @@ import org.chromium.net.test.EmbeddedTestServer; | 
| import org.chromium.net.test.util.TestWebServer; | 
| import java.util.ArrayList; | 
| +import java.util.List; | 
| import java.util.concurrent.Callable; | 
| import java.util.concurrent.TimeoutException; | 
| import java.util.concurrent.atomic.AtomicBoolean; | 
| @@ -140,6 +141,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| private String mTestPage2; | 
| private EmbeddedTestServer mTestServer; | 
| private TestWebServer mWebServer; | 
| + private int mDefaultDebugOverrideValue; | 
| + private int mLatestDebugOverrideValue; | 
| 
ahemery1
2017/03/21 15:15:58
This is some sort of state that we use here only.
 | 
| @Override | 
| protected void setUp() throws Exception { | 
| @@ -163,6 +166,7 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| CustomTabsConnection connection = | 
| CustomTabsConnection.getInstance((Application) appContext); | 
| connection.setForcePrerender(true); | 
| + mDefaultDebugOverrideValue = CustomTabsConnection.NO_OVERRIDE; | 
| 
ahemery1
2017/03/21 15:15:58
The switch that can be acted upon to modify the en
 | 
| } | 
| @Override | 
| @@ -1385,7 +1389,7 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| assertTrue(channelRequested); | 
| } | 
| - session.mayLaunchUrl(Uri.parse(url), null, null); | 
| + mayLaunchUrlForTesting(null, session, null, Uri.parse(url), null, null); | 
| try { | 
| ensureCompletedPrerenderForUrl( | 
| CustomTabsConnection.getInstance((Application) | 
| @@ -1484,9 +1488,11 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| CustomTabsSessionToken token = CustomTabsSessionToken.getSessionTokenFromIntent(intent); | 
| assertTrue(connection.newSession(token)); | 
| Bundle extras = new Bundle(); | 
| + // Forcing no prerendering implies falling back to simply creating a spare WebContent | 
| extras.putInt( | 
| CustomTabsConnection.DEBUG_OVERRIDE_KEY, CustomTabsConnection.NO_PRERENDERING); | 
| - assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), extras, null)); | 
| + assertTrue(mayLaunchUrlForTesting( | 
| + connection, null, token, Uri.parse(mTestPage), extras, null)); | 
| try { | 
| startCustomTabActivityWithIntent(intent); | 
| } catch (InterruptedException e) { | 
| @@ -1626,7 +1632,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| CustomTabsSessionToken token = CustomTabsSessionToken.getSessionTokenFromIntent(intent); | 
| connection.newSession(token); | 
| connection.setIgnoreUrlFragmentsForSession(token, ignoreFragments); | 
| - assertTrue(connection.mayLaunchUrl(token, Uri.parse(initialUrl), null, null)); | 
| + assertTrue( | 
| + mayLaunchUrlForTesting(connection, null, token, Uri.parse(initialUrl), null, null)); | 
| if (wait) ensureCompletedPrerenderForUrl(connection, initialUrl); | 
| @@ -1636,14 +1643,16 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| fail(); | 
| } | 
| final Tab tab = mActivity.getActivityTab(); | 
| - ElementContentCriteria initialVisibilityCriteria = new ElementContentCriteria( | 
| - tab, "visibility", ignoreFragments ? "prerender" : "visible"); | 
| - ElementContentCriteria initialFragmentCriteria = new ElementContentCriteria( | 
| - tab, "initial-fragment", ignoreFragments ? initialFragment : fragment); | 
| - ElementContentCriteria fragmentCriteria = new ElementContentCriteria( | 
| - tab, "fragment", fragment); | 
| if (wait) { | 
| + ElementContentCriteria initialVisibilityCriteria = new ElementContentCriteria(tab, | 
| 
ahemery1
2017/03/21 15:15:58
Unrelated change but used only inside the wait sco
 | 
| + "visibility", | 
| + ignoreFragments ? getExpectedVisibilityForOverride(mLatestDebugOverrideValue) | 
| + : "visible"); | 
| + ElementContentCriteria initialFragmentCriteria = new ElementContentCriteria( | 
| + tab, "initial-fragment", ignoreFragments ? initialFragment : fragment); | 
| + ElementContentCriteria fragmentCriteria = | 
| + new ElementContentCriteria(tab, "fragment", fragment); | 
| // The tab hasn't been reloaded. | 
| CriteriaHelper.pollInstrumentationThread(initialVisibilityCriteria, 2000, 200); | 
| // No reload (initial fragment is correct). | 
| @@ -1672,7 +1681,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| final CustomTabsConnection connection = warmUpAndWait(); | 
| CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); | 
| connection.newSession(token); | 
| - assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), null, null)); | 
| + assertTrue( | 
| + mayLaunchUrlForTesting(connection, null, token, Uri.parse(mTestPage), null, null)); | 
| ensureCompletedPrerenderForUrl(connection, mTestPage); | 
| try { | 
| @@ -1696,7 +1706,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| final CustomTabsConnection connection = warmUpAndWait(); | 
| CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); | 
| connection.newSession(token); | 
| - assertFalse(connection.mayLaunchUrl(token, Uri.parse("chrome://version"), null, null)); | 
| + assertFalse(mayLaunchUrlForTesting( | 
| + connection, null, token, Uri.parse("chrome://version"), null, null)); | 
| } | 
| /** | 
| @@ -1764,7 +1775,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| final CustomTabsConnection connection = warmUpAndWait(); | 
| CustomTabsSessionToken token = CustomTabsSessionToken.createDummySessionTokenForTesting(); | 
| connection.newSession(token); | 
| - assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), null, null)); | 
| + assertTrue( | 
| + mayLaunchUrlForTesting(connection, null, token, Uri.parse(mTestPage), null, null)); | 
| try { | 
| startCustomTabActivityWithIntent( | 
| CustomTabsTestUtils.createMinimalCustomTabIntent(context, mTestPage)); | 
| @@ -1824,7 +1836,9 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| Tab tab = mActivity.getActivityTab(); | 
| // The tab hasn't been reloaded. | 
| CriteriaHelper.pollInstrumentationThread( | 
| - new ElementContentCriteria(tab, "visibility", "prerender"), 2000, 200); | 
| + new ElementContentCriteria(tab, "visibility", | 
| + getExpectedVisibilityForOverride(mLatestDebugOverrideValue)), | 
| + 2000, 200); | 
| // The Referrer is correctly set. | 
| CriteriaHelper.pollInstrumentationThread( | 
| new TabsOpenedFromExternalAppTest.ReferrerCriteria(tab, referrer), 2000, 200); | 
| @@ -1976,7 +1990,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| extras = new Bundle(); | 
| extras.putParcelable(Intent.EXTRA_REFERRER, Uri.parse(prerenderReferrer)); | 
| } | 
| - assertTrue(connection.mayLaunchUrl(token, Uri.parse(url), extras, null)); | 
| + assertTrue( | 
| + mayLaunchUrlForTesting(connection, null, token, Uri.parse(url), extras, null)); | 
| ensureCompletedPrerenderForUrl(connection, url); | 
| } | 
| @@ -2002,7 +2017,8 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| extras.putInt( | 
| CustomTabsConnection.DEBUG_OVERRIDE_KEY, CustomTabsConnection.NO_PRERENDERING); | 
| } | 
| - assertTrue(connection.mayLaunchUrl(token, Uri.parse(mTestPage), extras, null)); | 
| + assertTrue(mayLaunchUrlForTesting( | 
| + connection, null, token, Uri.parse(mTestPage), extras, null)); | 
| try { | 
| startCustomTabActivityWithIntent(CustomTabsTestUtils.createMinimalCustomTabIntent( | 
| context, mTestPage)); | 
| @@ -2113,16 +2129,26 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| private static void ensureCompletedPrerenderForUrl( | 
| final CustomTabsConnection connection, final String url) throws Exception { | 
| - CriteriaHelper.pollUiThread(new Criteria("No Prerender") { | 
| + // We wait until we have a speculation to pull information from first. | 
| + CriteriaHelper.pollInstrumentationThread(new Criteria("No Speculation") { | 
| @Override | 
| public boolean isSatisfied() { | 
| - return connection.mSpeculation != null | 
| - && connection.mSpeculation.webContents != null | 
| - && ExternalPrerenderHandler.hasPrerenderedAndFinishedLoadingUrl( | 
| - Profile.getLastUsedProfile(), url, | 
| - connection.mSpeculation.webContents); | 
| + return connection.mSpeculation != null; | 
| } | 
| }, LONG_TIMEOUT_MS, CriteriaHelper.DEFAULT_POLLING_INTERVAL); | 
| + | 
| + // Then do the specific depending on debug override. | 
| + if (connection.mSpeculation.debugOverrideValue == CustomTabsConnection.NO_OVERRIDE) { | 
| 
ahemery1
2017/03/21 15:15:58
We are sure this exist so we can use this value he
 | 
| + CriteriaHelper.pollUiThread(new Criteria("No Prerender") { | 
| + @Override | 
| + public boolean isSatisfied() { | 
| + return connection.mSpeculation.webContents != null | 
| + && ExternalPrerenderHandler.hasPrerenderedAndFinishedLoadingUrl( | 
| + Profile.getLastUsedProfile(), url, | 
| + connection.mSpeculation.webContents); | 
| + } | 
| + }, LONG_TIMEOUT_MS, CriteriaHelper.DEFAULT_POLLING_INTERVAL); | 
| + } | 
| } | 
| private CustomTabsSession bindWithCallback(final CustomTabsCallback callback) { | 
| @@ -2210,4 +2236,38 @@ public class CustomTabActivityTest extends CustomTabActivityTestBase { | 
| return TextUtils.equals(mExpected, value); | 
| } | 
| } | 
| + | 
| + private boolean mayLaunchUrlForTesting(CustomTabsConnection connection, | 
| 
ahemery1
2017/03/21 15:15:58
A wrapper that allows us to do some additional ins
 | 
| + CustomTabsSession session, CustomTabsSessionToken token, Uri url, Bundle extras, | 
| + List<Bundle> otherLikelyBundles) { | 
| + instrumentExtrasForTesting(extras); | 
| + if (connection != null) { | 
| + return connection.mayLaunchUrl(token, url, extras, otherLikelyBundles); | 
| + } | 
| + if (session != null) { | 
| + return session.mayLaunchUrl(url, extras, otherLikelyBundles); | 
| + } | 
| + return false; | 
| + } | 
| + | 
| + /* | 
| + * Does the following modifications to the extras bundle : | 
| + * - Uses the default debug override if none is specified. | 
| + */ | 
| + private void instrumentExtrasForTesting(Bundle extras) { | 
| + if (extras == null) extras = new Bundle(); | 
| + int debugOverrideValue = | 
| + extras.getInt(CustomTabsConnection.DEBUG_OVERRIDE_KEY, mDefaultDebugOverrideValue); | 
| + extras.putInt(CustomTabsConnection.DEBUG_OVERRIDE_KEY, debugOverrideValue); | 
| + mLatestDebugOverrideValue = debugOverrideValue; | 
| + } | 
| + | 
| + private static String getExpectedVisibilityForOverride(int mode) { | 
| + switch (mode) { | 
| + case CustomTabsConnection.NO_OVERRIDE: | 
| + return "prerender"; | 
| + default: | 
| + return "visible"; | 
| + } | 
| + } | 
| } |