Chromium Code Reviews| Index: content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java |
| diff --git a/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..89c8df63dfcf53aa266dbb028bbe29889758e8d2 |
| --- /dev/null |
| +++ b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java |
| @@ -0,0 +1,283 @@ |
| +// Copyright 2017 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.content_shell_apk; |
| + |
| +import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| + |
| +import android.annotation.TargetApi; |
| +import android.content.ComponentName; |
| +import android.content.Context; |
| +import android.content.Intent; |
| +import android.net.Uri; |
| +import android.os.Build; |
| +import android.os.PowerManager; |
| +import android.support.test.InstrumentationRegistry; |
| +import android.support.test.rule.ActivityTestRule; |
| +import android.text.TextUtils; |
| +import android.view.ViewGroup; |
| + |
| +import org.junit.Assert; |
| +import org.junit.runner.Description; |
| +import org.junit.runners.model.Statement; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.CallbackHelper; |
| +import org.chromium.base.test.util.UrlUtils; |
| +import org.chromium.content.browser.ContentView; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| +import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| +import org.chromium.content_public.browser.LoadUrlParams; |
| +import org.chromium.content_public.browser.NavigationController; |
| +import org.chromium.content_public.browser.WebContents; |
| +import org.chromium.content_shell.Shell; |
| + |
| +import java.lang.annotation.ElementType; |
| +import java.lang.annotation.Retention; |
| +import java.lang.annotation.RetentionPolicy; |
| +import java.lang.annotation.Target; |
| +import java.util.concurrent.Callable; |
| +import java.util.concurrent.ExecutionException; |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** |
| + * ActivityTestRule for ContentShellActivity. |
| + * |
| + * Test would use this ActivityTestRule to launch or get ContentShellActivity. |
|
jbudorick
2017/02/22 16:09:30
nits: "Test would" -> "Tests can"
"launch or
the real yoland
2017/02/22 19:35:37
Done
|
| + * For more information on ActivityTestRule, check |
|
jbudorick
2017/02/22 16:09:30
nit: I'm not sure this is necessary.
the real yoland
2017/02/22 19:35:37
Done
|
| + * https://developer.android.com/reference/android/support/test/rule/ActivityTestRule.html |
| + */ |
| +public class ContentShellActivityTestRule extends ActivityTestRule<ContentShellActivity> { |
| + private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000); |
| + |
| + protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15); |
| + |
| + public ContentShellActivityTestRule() { |
| + super(ContentShellActivity.class, false, false); |
| + } |
| + |
| + public ContentShellActivityTestRule(boolean initialTouchMode, boolean launchActivity) { |
| + super(ContentShellActivity.class, initialTouchMode, launchActivity); |
| + } |
| + |
| + @Override |
| + protected void beforeActivityLaunched() { |
| + assertScreenIsOn(); |
| + } |
| + |
| + @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) |
| + @SuppressWarnings("deprecation") |
| + private void assertScreenIsOn() { |
| + PowerManager pm = (PowerManager) InstrumentationRegistry.getContext().getSystemService( |
| + Context.POWER_SERVICE); |
| + |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { |
| + Assert.assertTrue("Many tests will fail if the screen is not on.", pm.isInteractive()); |
| + } else { |
| + Assert.assertTrue("Many tests will fail if the screen is not on.", pm.isScreenOn()); |
| + } |
| + } |
| + |
| + /** |
| + * Starts the ContentShell activity and loads the given URL. |
| + * The URL can be null, in which case will default to ContentShellActivity.DEFAULT_SHELL_URL. |
| + */ |
| + public ContentShellActivity launchContentShellWithUrl(String url) { |
| + Intent intent = new Intent(Intent.ACTION_MAIN); |
| + intent.addCategory(Intent.CATEGORY_LAUNCHER); |
| + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| + if (url != null) intent.setData(Uri.parse(url)); |
| + intent.setComponent( |
| + new ComponentName(InstrumentationRegistry.getInstrumentation().getTargetContext(), |
| + ContentShellActivity.class)); |
| + return launchActivity(intent); |
| + } |
| + |
| + /** |
| + * Starts the content shell activity with the provided test url. |
| + * The url is synchronously loaded. |
| + * @param url Test url to load. |
| + */ |
| + public void launchActivityWithTestUrl(String url) { |
|
jbudorick
2017/02/22 16:09:29
Either leave the TODO in or address it. I think it
the real yoland
2017/02/22 19:35:37
I see, changed to launchContentShellWithUrlSync(..
jbudorick
2017/02/22 21:27:39
We should either:
1) move the getIsolatedTestFile
the real yoland
2017/02/23 01:11:59
Done 2
|
| + launchContentShellWithUrl(UrlUtils.getIsolatedTestFileUrl(url)); |
| + Assert.assertNotNull(getActivity()); |
| + waitForActiveShellToBeDoneLoading(); |
| + Assert.assertEquals(UrlUtils.getIsolatedTestFileUrl(url), |
| + getContentViewCore().getWebContents().getUrl()); |
| + } |
| + |
| + /** |
| + * Returns the current ContentViewCore or null if there is no ContentView. |
| + */ |
| + public ContentViewCore getContentViewCore() { |
| + return getActivity().getActiveShell().getContentViewCore(); |
| + } |
| + |
| + /** |
| + * Returns the WebContents of this Shell. |
| + */ |
| + public WebContents getWebContents() { |
| + return getActivity().getActiveShell().getWebContents(); |
| + } |
| + |
| + /** |
| + * Waits for the Active shell to finish loading. This times out after |
| + * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be used for long |
| + * loading pages. Instead it should be used more for test initialization. The proper way |
| + * to wait is to use a TestCallbackHelperContainer after the initial load is completed. |
| + */ |
| + public void waitForActiveShellToBeDoneLoading() { |
| + final ContentShellActivity activity = getActivity(); |
| + |
| + // Wait for the Content Shell to be initialized. |
| + CriteriaHelper.pollUiThread(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + Shell shell = activity.getActiveShell(); |
| + // There are two cases here that need to be accounted for. |
| + // The first is that we've just created a Shell and it isn't |
| + // loading because it has no URL set yet. The second is that |
| + // we've set a URL and it actually is loading. |
| + if (shell == null) { |
| + updateFailureReason("Shell is null."); |
| + return false; |
| + } |
| + if (shell.isLoading()) { |
| + updateFailureReason("Shell is still loading."); |
| + return false; |
| + } |
| + if (TextUtils.isEmpty(shell.getContentViewCore().getWebContents().getUrl())) { |
| + updateFailureReason("Shell's URL is empty or null."); |
| + return false; |
| + } |
| + return true; |
| + } |
| + }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL); |
| + } |
| + |
| + /** |
| + * Creates a new {@link Shell} and waits for it to finish loading. |
| + * @param url The URL to create the new {@link Shell} with. |
| + * @return A new instance of a {@link Shell}. |
| + * @throws ExecutionException |
| + */ |
| + public Shell loadNewShell(final String url) throws ExecutionException { |
| + Shell shell = ThreadUtils.runOnUiThreadBlocking(new Callable<Shell>() { |
| + @Override |
| + public Shell call() { |
| + getActivity().getShellManager().launchShell(url); |
| + return getActivity().getActiveShell(); |
| + } |
| + }); |
| + |
| + Assert.assertNotNull("Unable to create shell.", shell); |
| + Assert.assertEquals("Active shell unexpected.", shell, getActivity().getActiveShell()); |
| + |
| + waitForActiveShellToBeDoneLoading(); |
| + |
| + return shell; |
| + } |
| + |
| + /** |
| + * Loads a URL in the specified content view. |
| + * |
| + * @param navigationController The navigation controller to load the URL in. |
| + * @param callbackHelperContainer The callback helper container used to monitor progress. |
| + * @param params The URL params to use. |
| + */ |
| + public void loadUrl(final NavigationController navigationController, |
| + TestCallbackHelperContainer callbackHelperContainer, final LoadUrlParams params) |
| + throws Throwable { |
| + handleBlockingCallbackAction( |
| + callbackHelperContainer.getOnPageFinishedHelper(), new Runnable() { |
| + @Override |
| + public void run() { |
| + navigationController.loadUrl(params); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Handles performing an action on the UI thread that will return when the specified callback |
| + * is incremented. |
| + * |
| + * @param callbackHelper The callback helper that will be blocked on. |
| + * @param action The action to be performed on the UI thread. |
| + */ |
| + public void handleBlockingCallbackAction(CallbackHelper callbackHelper, Runnable action) |
| + throws Throwable { |
| + int currentCallCount = callbackHelper.getCallCount(); |
| + runOnUiThread(action); |
| + callbackHelper.waitForCallback( |
| + currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| + } |
| + |
| + // TODO(aelias): This method needs to be removed once http://crbug.com/179511 is fixed. |
| + // Meanwhile, we have to wait if the page has the <meta viewport> tag. |
| + /** |
| + * Waits till the ContentViewCore receives the expected page scale factor |
| + * from the compositor and asserts that this happens. |
| + */ |
| + public void assertWaitForPageScaleFactorMatch(float expectedScale) { |
| + CriteriaHelper.pollInstrumentationThread( |
| + Criteria.equals(expectedScale, new Callable<Float>() { |
| + @Override |
| + public Float call() { |
| + return getContentViewCore().getScale(); |
| + } |
| + })); |
| + } |
| + |
| + /** |
| + * Replaces the {@link ContentViewCore#mContainerView} with a newly created |
| + * {@link ContentView}. |
| + */ |
| + @SuppressWarnings("javadoc") |
| + public void replaceContainerView() throws Throwable { |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + ContentView cv = ContentView.createContentView(getActivity(), getContentViewCore()); |
| + ((ViewGroup) getContentViewCore().getContainerView().getParent()).addView(cv); |
| + getContentViewCore().setContainerView(cv); |
| + getContentViewCore().setContainerViewInternals(cv); |
| + cv.requestFocus(); |
| + } |
| + }); |
| + } |
| + |
| + // TODO(yolandyan): this should be done through parameterized test |
|
jbudorick
2017/02/22 16:09:30
This appears to be a preland TODO.
|
| + @Override |
| + public Statement apply(final Statement base, final Description desc) { |
| + return super.apply(new Statement() { |
| + @Override |
| + public void evaluate() throws Throwable { |
| + base.evaluate(); |
| + try { |
| + if (desc.getAnnotation(RerunWithUpdatedContainerView.class) != null) { |
| + replaceContainerView(); |
| + base.evaluate(); |
| + } |
| + } catch (Throwable e) { |
| + throw new Throwable("@RerunWithUpdatedContainerView failed." |
| + + " See ContentShellTestBase#runTest.", |
| + e); |
| + } |
| + } |
| + }, desc); |
| + } |
| + |
| + /** |
| + * Annotation for tests that should be executed a second time after replacing |
| + * the ContentViewCore's container view (see {@link #apply()}). |
| + * |
| + * <p>Please note that activity launch is only invoked once before both runs, |
| + * and that any state changes produced by the first run are visible to the second run. |
| + */ |
| + @Target(ElementType.METHOD) |
| + @Retention(RetentionPolicy.RUNTIME) |
| + public @interface RerunWithUpdatedContainerView {} |
| +} |