| Index: content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestCommon.java
|
| diff --git a/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestBase.java b/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestCommon.java
|
| similarity index 53%
|
| copy from content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestBase.java
|
| copy to content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestCommon.java
|
| index 592e6f9db44871834678e47a89c5c404fa0ba888..83f21bfdb8d4fa27f735b7fdf75ff1046bad30da 100644
|
| --- a/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestBase.java
|
| +++ b/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestCommon.java
|
| @@ -1,4 +1,4 @@
|
| -// Copyright 2012 The Chromium Authors. All rights reserved.
|
| +// 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.
|
|
|
| @@ -6,66 +6,44 @@ package org.chromium.content.browser;
|
|
|
| import android.util.Log;
|
|
|
| -import junit.framework.Assert;
|
| +import org.junit.Assert;
|
|
|
| import org.chromium.base.annotations.SuppressFBWarnings;
|
| import org.chromium.base.test.util.UrlUtils;
|
| import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
|
| import org.chromium.content_public.browser.LoadUrlParams;
|
| import org.chromium.content_shell_apk.ContentShellActivity;
|
| -import org.chromium.content_shell_apk.ContentShellTestBase;
|
| +import org.chromium.content_shell_apk.ContentShellTestCommon.TestCommonCallback;
|
|
|
| import java.lang.annotation.Annotation;
|
|
|
| /**
|
| - * Common functionality for testing the Java Bridge.
|
| + * Common functionality implementation for testing the Java Bridge.
|
| */
|
| -public class JavaBridgeTestBase extends ContentShellTestBase {
|
| -
|
| +public final class JavaBridgeTestCommon {
|
| protected TestCallbackHelperContainer mTestCallbackHelperContainer;
|
|
|
| - /**
|
| - * Sets up the ContentView. Intended to be called from setUp().
|
| - */
|
| - private void setUpContentView() throws Exception {
|
| - // This starts the activity, so must be called on the test thread.
|
| - final ContentShellActivity activity = launchContentShellWithUrl(
|
| - UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body></html>"));
|
| + private final TestCommonCallback<ContentShellActivity> mCallback;
|
|
|
| - waitForActiveShellToBeDoneLoading();
|
| -
|
| - try {
|
| - runTestOnUiThread(new Runnable() {
|
| - @Override
|
| - public void run() {
|
| - mTestCallbackHelperContainer = new TestCallbackHelperContainer(
|
| - activity.getActiveContentViewCore());
|
| - }
|
| - });
|
| - } catch (Throwable e) {
|
| - throw new RuntimeException(
|
| - "Failed to set up ContentView: " + Log.getStackTraceString(e));
|
| - }
|
| - }
|
| -
|
| - @Override
|
| - protected void setUp() throws Exception {
|
| - super.setUp();
|
| - setUpContentView();
|
| + public JavaBridgeTestCommon(TestCommonCallback<ContentShellActivity> callback) {
|
| + mCallback = callback;
|
| }
|
|
|
| @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD")
|
| - protected static class Controller {
|
| + public static class Controller {
|
| + private static final int RESULT_WAIT_TIME = 5000;
|
| +
|
| private boolean mIsResultReady;
|
|
|
| protected synchronized void notifyResultIsReady() {
|
| mIsResultReady = true;
|
| notify();
|
| }
|
| +
|
| protected synchronized void waitForResult() {
|
| while (!mIsResultReady) {
|
| try {
|
| - wait(5000);
|
| + wait(RESULT_WAIT_TIME);
|
| } catch (Exception e) {
|
| continue;
|
| }
|
| @@ -77,8 +55,32 @@ public class JavaBridgeTestBase extends ContentShellTestBase {
|
| }
|
| }
|
|
|
| - protected void executeJavaScript(final String script) throws Throwable {
|
| - runTestOnUiThread(new Runnable() {
|
| + TestCallbackHelperContainer getTestCallBackHelperContainer() {
|
| + return mTestCallbackHelperContainer;
|
| + }
|
| +
|
| + void setUpContentView() {
|
| + // This starts the activity, so must be called on the test thread.
|
| + final ContentShellActivity activity = mCallback.launchContentShellWithUrlForTestCommon(
|
| + UrlUtils.encodeHtmlDataUri("<html><head></head><body>test</body></html>"));
|
| + mCallback.waitForActiveShellToBeDoneLoadingForTestCommon();
|
| +
|
| + try {
|
| + mCallback.runOnUiThreadForTestCommon(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + mTestCallbackHelperContainer =
|
| + new TestCallbackHelperContainer(activity.getActiveContentViewCore());
|
| + }
|
| + });
|
| + } catch (Throwable e) {
|
| + throw new RuntimeException(
|
| + "Failed to set up ContentView: " + Log.getStackTraceString(e));
|
| + }
|
| + }
|
| +
|
| + void executeJavaScript(final String script) throws Throwable {
|
| + mCallback.runOnUiThreadForTestCommon(new Runnable() {
|
| @Override
|
| public void run() {
|
| // When a JavaScript URL is executed, if the value of the last
|
| @@ -86,38 +88,37 @@ public class JavaBridgeTestBase extends ContentShellTestBase {
|
| // converted to a string and used as the new document for the
|
| // frame. We don't want this behaviour, so wrap the script in
|
| // an anonymous function.
|
| - getWebContents().getNavigationController().loadUrl(
|
| - new LoadUrlParams("javascript:(function() { " + script + " })()"));
|
| + mCallback.getActivityForTestCommon()
|
| + .getActiveShell()
|
| + .getWebContents()
|
| + .getNavigationController()
|
| + .loadUrl(new LoadUrlParams("javascript:(function() { " + script + " })()"));
|
| }
|
| });
|
| }
|
|
|
| - protected void injectObjectAndReload(final Object object, final String name) throws Exception {
|
| - injectObjectAndReload(object, name, null);
|
| - }
|
| -
|
| - protected void injectObjectAndReload(final Object object, final String name,
|
| - final Class<? extends Annotation> requiredAnnotation) throws Exception {
|
| - injectObjectsAndReload(object, name, null, null, requiredAnnotation);
|
| - }
|
| -
|
| - protected void injectObjectsAndReload(final Object object1, final String name1,
|
| - final Object object2, final String name2,
|
| - final Class<? extends Annotation> requiredAnnotation) throws Exception {
|
| + void injectObjectsAndReload(final Object object1, final String name1, final Object object2,
|
| + final String name2, final Class<? extends Annotation> requiredAnnotation)
|
| + throws Exception {
|
| TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
|
| mTestCallbackHelperContainer.getOnPageFinishedHelper();
|
| int currentCallCount = onPageFinishedHelper.getCallCount();
|
| try {
|
| - runTestOnUiThread(new Runnable() {
|
| + mCallback.runOnUiThreadForTestCommon(new Runnable() {
|
| @Override
|
| public void run() {
|
| - getContentViewCore().addPossiblyUnsafeJavascriptInterface(object1,
|
| - name1, requiredAnnotation);
|
| + mCallback.getContentViewCoreForTestCommon()
|
| + .addPossiblyUnsafeJavascriptInterface(
|
| + object1, name1, requiredAnnotation);
|
| if (object2 != null && name2 != null) {
|
| - getContentViewCore().addPossiblyUnsafeJavascriptInterface(object2,
|
| - name2, requiredAnnotation);
|
| + mCallback.getContentViewCoreForTestCommon()
|
| + .addPossiblyUnsafeJavascriptInterface(
|
| + object2, name2, requiredAnnotation);
|
| }
|
| - getContentViewCore().getWebContents().getNavigationController().reload(true);
|
| + mCallback.getContentViewCoreForTestCommon()
|
| + .getWebContents()
|
| + .getNavigationController()
|
| + .reload(true);
|
| }
|
| });
|
| onPageFinishedHelper.waitForCallback(currentCallCount);
|
| @@ -127,14 +128,17 @@ public class JavaBridgeTestBase extends ContentShellTestBase {
|
| }
|
| }
|
|
|
| - protected void synchronousPageReload() throws Throwable {
|
| + void synchronousPageReload() throws Throwable {
|
| TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
|
| mTestCallbackHelperContainer.getOnPageFinishedHelper();
|
| int currentCallCount = onPageFinishedHelper.getCallCount();
|
| - runTestOnUiThread(new Runnable() {
|
| + mCallback.runOnUiThreadForTestCommon(new Runnable() {
|
| @Override
|
| public void run() {
|
| - getContentViewCore().getWebContents().getNavigationController().reload(true);
|
| + mCallback.getContentViewCoreForTestCommon()
|
| + .getWebContents()
|
| + .getNavigationController()
|
| + .reload(true);
|
| }
|
| });
|
| onPageFinishedHelper.waitForCallback(currentCallCount);
|
|
|