Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Unified Diff: content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java

Issue 2632043002: Create ContentShellActivityTestRule and BaseJUnitRunner (Closed)
Patch Set: Refactor to TestBase and TestRule to use same delegate class (Only ContentShellTestBase) Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java
diff --git a/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java
index 3fa2a4deb28cfaefd9f59d8f5d1f6d24de18b4f8..5a1841c7f19a9b2a8b36a2ef70c5e5c2bb0c7f34 100644
--- a/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java
+++ b/content/shell/android/javatests/src/org/chromium/content_shell_apk/ContentShellTestBase.java
@@ -4,42 +4,22 @@
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.text.TextUtils;
-import android.view.ViewGroup;
-
-import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseActivityInstrumentationTestCase;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
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.common.ContentSwitches;
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 org.chromium.content_shell_apk.ContentShellActivityTestRule.RerunWithUpdatedContainerView;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
-import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
/**
* Base test class for all ContentShell based tests.
@@ -47,32 +27,19 @@ import java.util.concurrent.TimeUnit;
@CommandLineFlags.Add(ContentSwitches.ENABLE_TEST_INTENTS)
public class ContentShellTestBase
extends BaseActivityInstrumentationTestCase<ContentShellActivity> {
- /** The maximum time the waitForActiveShellToBeDoneLoading method will wait. */
- private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
-
- protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
+ private final ContentShellTestBaseDelegate mDelegate;
+ protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS =
+ ContentShellTestBaseDelegate.WAIT_PAGE_LOADING_TIMEOUT_SECONDS;
public ContentShellTestBase() {
super(ContentShellActivity.class);
+ mDelegate = new ContentShellTestBaseDelegate();
}
@Override
protected void setUp() throws Exception {
super.setUp();
- assertScreenIsOn();
- }
-
- @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
- @SuppressWarnings("deprecation")
- private void assertScreenIsOn() {
- PowerManager pm = (PowerManager) getInstrumentation().getContext().getSystemService(
- Context.POWER_SERVICE);
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
- assertTrue("Many tests will fail if the screen is not on.", pm.isInteractive());
- } else {
- assertTrue("Many tests will fail if the screen is not on.", pm.isScreenOn());
- }
+ mDelegate.assertScreenIsOn(getInstrumentation());
}
/**
@@ -80,13 +47,8 @@ public class ContentShellTestBase
* The URL can be null, in which case will default to ContentShellActivity.DEFAULT_SHELL_URL.
*/
protected 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(getInstrumentation().getTargetContext(),
- ContentShellActivity.class));
- setActivityIntent(intent);
+ setActivityIntent(
+ mDelegate.getActivityLaunchIntent(getInstrumentation().getTargetContext(), url));
return getActivity();
}
@@ -110,14 +72,14 @@ public class ContentShellTestBase
* Returns the current ContentViewCore or null if there is no ContentView.
*/
protected ContentViewCore getContentViewCore() {
- return getActivity().getActiveShell().getContentViewCore();
+ return mDelegate.getContentViewCore(getActivity());
}
/**
* Returns the WebContents of this Shell.
*/
protected WebContents getWebContents() {
- return getActivity().getActiveShell().getWebContents();
+ return mDelegate.getWebContents(getActivity());
}
/**
@@ -127,32 +89,7 @@ public class ContentShellTestBase
* to wait is to use a TestCallbackHelperContainer after the initial load is completed.
*/
protected 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);
+ mDelegate.waitForActiveShellToBeDoneLoading(getActivity());
}
/**
@@ -161,21 +98,8 @@ public class ContentShellTestBase
* @return A new instance of a {@link Shell}.
* @throws ExecutionException
*/
- protected 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();
- }
- });
-
- assertNotNull("Unable to create shell.", shell);
- assertEquals("Active shell unexpected.", shell, getActivity().getActiveShell());
-
- waitForActiveShellToBeDoneLoading();
-
- return shell;
+ protected Shell loadNewShell(String url) throws ExecutionException {
+ return mDelegate.loadNewShell(getActivity(), url);
}
/**
* Loads a URL in the specified content view.
@@ -206,11 +130,18 @@ public class ContentShellTestBase
* @param action The action to be performed on the UI thread.
*/
protected void handleBlockingCallbackAction(
- CallbackHelper callbackHelper, Runnable action) throws Throwable {
- int currentCallCount = callbackHelper.getCallCount();
- runTestOnUiThread(action);
- callbackHelper.waitForCallback(
- currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ CallbackHelper callbackHelper, final Runnable action) throws Throwable {
+ Runnable uiThreadRunnable = new Runnable() {
+ @Override
+ public void run() {
+ try {
+ runTestOnUiThread(action);
+ } catch (Throwable e) {
+ throw new RuntimeException("Unable to run on Ui Thread", e);
+ }
+ }
+ };
+ mDelegate.handleBlockingCallbackAction(callbackHelper, uiThreadRunnable);
}
// TODO(aelias): This method needs to be removed once http://crbug.com/179511 is fixed.
@@ -220,13 +151,7 @@ public class ContentShellTestBase
* from the compositor and asserts that this happens.
*/
protected void assertWaitForPageScaleFactorMatch(float expectedScale) {
- CriteriaHelper.pollInstrumentationThread(
- Criteria.equals(expectedScale, new Callable<Float>() {
- @Override
- public Float call() {
- return getContentViewCore().getScale();
- }
- }));
+ mDelegate.assertWaitForPageScaleFactorMatch(getContentViewCore(), expectedScale);
}
/**
@@ -235,16 +160,7 @@ public class ContentShellTestBase
*/
@SuppressWarnings("javadoc")
protected 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();
- }
- });
+ mDelegate.replaceContainerView(getActivity());
}
@Override
@@ -261,16 +177,4 @@ public class ContentShellTestBase
+ " See ContentShellTestBase#runTest.", e);
}
}
-
- /**
- * Annotation for tests that should be executed a second time after replacing
- * the ContentViewCore's container view (see {@link #runTest()}).
- *
- * <p>Please note that {@link #setUp()} 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 {
- }
}

Powered by Google App Engine
This is Rietveld 408576698