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..e295c8926ae77022c20ae7dbd163396d00a2c75b |
| --- /dev/null |
| +++ b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java |
| @@ -0,0 +1,183 @@ |
| +// 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.support.test.InstrumentationRegistry; |
| +import android.support.test.rule.ActivityTestRule; |
| + |
| +import org.junit.Assert; |
| + |
| +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.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.ExecutionException; |
| + |
| +/** |
| + * ActivityTestRule for ContentShellActivity. |
| + * |
| + * Test can use this ActivityTestRule to launch or get ContentShellActivity. |
| + */ |
| +public class ContentShellActivityTestRule extends ActivityTestRule<ContentShellActivity> { |
| + private final boolean mLaunchActivity; |
| + private final ContentShellTestBaseDelegate mDelegate; |
| + |
| + protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15); |
| + |
| + public ContentShellActivityTestRule() { |
| + this(false, false); |
| + } |
| + |
| + public ContentShellActivityTestRule(boolean initialTouchMode, boolean launchActivity) { |
| + super(ContentShellActivity.class, initialTouchMode, launchActivity); |
| + mLaunchActivity = launchActivity; |
| + mDelegate = new ContentShellTestBaseDelegate(); |
| + } |
| + |
| + @Override |
| + protected void beforeActivityLaunched() { |
| + mDelegate.assertScreenIsOn(InstrumentationRegistry.getInstrumentation()); |
| + } |
| + |
| + /** |
| + * 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) { |
| + Assert.assertFalse(mLaunchActivity); |
| + return launchActivity( |
| + mDelegate.getActivityLaunchIntent(InstrumentationRegistry.getTargetContext(), url)); |
| + } |
| + |
| + // TODO(yolandyan): This should use the url exactly without the getIsolatedTestFileUrl call. |
| + /** |
| + * Starts the content shell activity with the provided test url. |
| + * The url is synchronously loaded. |
| + * @param url Test url to load. |
| + */ |
| + public void launchContentShellWithUrlSync(String url) { |
|
boliu
2017/03/01 23:01:23
can this move to delegate too? I guess you would l
the real yoland
2017/03/02 20:16:48
Changed to adapter pattern
|
| + 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 mDelegate.getContentViewCore(getActivity()); |
| + } |
| + |
| + /** |
| + * Returns the WebContents of this Shell. |
| + */ |
| + public WebContents getWebContents() { |
| + return mDelegate.getWebContents(getActivity()); |
| + } |
| + |
| + /** |
| + * 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() { |
| + mDelegate.waitForActiveShellToBeDoneLoading(getActivity()); |
| + } |
| + |
| + /** |
| + * 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 { |
| + return mDelegate.loadNewShell(getActivity(), url); |
| + } |
| + |
| + /** |
| + * 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, |
|
boliu
2017/03/01 23:01:23
this and handleBlockingCallbackAction can just mov
the real yoland
2017/03/02 20:16:48
Done
|
| + 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, final Runnable action) |
| + throws Throwable { |
| + Runnable uiThreadRunnable = new Runnable() { |
| + @Override |
| + public void run() { |
| + try { |
| + runOnUiThread(action); |
| + } catch (Throwable e) { |
| + throw new RuntimeException("Unable to run on Ui thread"); |
| + } |
| + } |
| + }; |
| + mDelegate.handleBlockingCallbackAction(callbackHelper, uiThreadRunnable); |
| + } |
| + |
| + // 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) { |
| + mDelegate.assertWaitForPageScaleFactorMatch(getContentViewCore(), expectedScale); |
| + } |
| + |
| + /** |
| + * Replaces the {@link ContentViewCore#mContainerView} with a newly created |
| + * {@link ContentView}. |
| + */ |
| + @SuppressWarnings("javadoc") |
| + public void replaceContainerView() throws Throwable { |
| + mDelegate.replaceContainerView(getActivity()); |
| + } |
| + |
| + /** |
| + * Annotation for tests that should be executed a second time after replacing |
| + * the ContentViewCore's container view. |
| + * <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 {} |
| +} |