Commit 090ff5d8 authored by Yoland Yan's avatar Yoland Yan Committed by Commit Bot

Merge TestCommon implementation to TestRule

Merge two TestCommon into ActivityTestRule since there is
no long need for sharing the implementation with other classes

Bug: 711517
Change-Id: I21c902f76891e2e75b4716a924871e0c2410f30f
Reviewed-on: https://chromium-review.googlesource.com/887341Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Yoland Yan <yolandyan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532003}
parent 844af309
...@@ -466,7 +466,6 @@ android_library("content_javatests") { ...@@ -466,7 +466,6 @@ android_library("content_javatests") {
"javatests/src/org/chromium/content/browser/JavaBridgeCoercionTest.java", "javatests/src/org/chromium/content/browser/JavaBridgeCoercionTest.java",
"javatests/src/org/chromium/content/browser/JavaBridgeFieldsTest.java", "javatests/src/org/chromium/content/browser/JavaBridgeFieldsTest.java",
"javatests/src/org/chromium/content/browser/JavaBridgeReturnValuesTest.java", "javatests/src/org/chromium/content/browser/JavaBridgeReturnValuesTest.java",
"javatests/src/org/chromium/content/browser/JavaBridgeTestCommon.java",
"javatests/src/org/chromium/content/browser/MediaResourceGetterTest.java", "javatests/src/org/chromium/content/browser/MediaResourceGetterTest.java",
"javatests/src/org/chromium/content/browser/MediaSessionTest.java", "javatests/src/org/chromium/content/browser/MediaSessionTest.java",
"javatests/src/org/chromium/content/browser/NavigationTest.java", "javatests/src/org/chromium/content/browser/NavigationTest.java",
......
...@@ -69,7 +69,7 @@ public class ContentViewPointerTypeTest { ...@@ -69,7 +69,7 @@ public class ContentViewPointerTypeTest {
final Rect rect = DOMUtils.getNodeBounds(mActivityTestRule.getWebContents(), nodeId); final Rect rect = DOMUtils.getNodeBounds(mActivityTestRule.getWebContents(), nodeId);
OnCursorUpdateHelper onCursorUpdateHelper = mActivityTestRule.getOnCursorUpdateHelper(); OnCursorUpdateHelper onCursorUpdateHelper = mActivityTestRule.getOnCursorUpdateHelper();
int onCursorUpdateCount = onCursorUpdateHelper.getCallCount(); int onCursorUpdateCount = onCursorUpdateHelper.getCallCount();
mActivityTestRule.runOnUiThreadForTestCommon(new Runnable() { mActivityTestRule.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
RenderCoordinates coord = mActivityTestRule.getRenderCoordinates(); RenderCoordinates coord = mActivityTestRule.getRenderCoordinates();
......
...@@ -4,15 +4,20 @@ ...@@ -4,15 +4,20 @@
package org.chromium.content.browser; package org.chromium.content.browser;
import org.junit.Assert;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
import org.chromium.base.Log;
import org.chromium.base.test.SetUpStatement; import org.chromium.base.test.SetUpStatement;
import org.chromium.base.test.SetUpTestRule; import org.chromium.base.test.SetUpTestRule;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer; import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content_public.browser.JavascriptInjector;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_shell_apk.ContentShellActivity; import org.chromium.content_shell_apk.ContentShellActivity;
import org.chromium.content_shell_apk.ContentShellActivityTestRule; import org.chromium.content_shell_apk.ContentShellActivityTestRule;
import org.chromium.content_shell_apk.ContentShellTestCommon.TestCommonCallback;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
...@@ -20,29 +25,75 @@ import java.lang.annotation.Annotation; ...@@ -20,29 +25,75 @@ import java.lang.annotation.Annotation;
* ActivityTestRule with common functionality for testing the Java Bridge. * ActivityTestRule with common functionality for testing the Java Bridge.
*/ */
public class JavaBridgeActivityTestRule public class JavaBridgeActivityTestRule
extends ContentShellActivityTestRule implements TestCommonCallback<ContentShellActivity>, extends ContentShellActivityTestRule implements SetUpTestRule<JavaBridgeActivityTestRule> {
SetUpTestRule<JavaBridgeActivityTestRule> { private TestCallbackHelperContainer mTestCallbackHelperContainer;
private JavaBridgeTestCommon mTestCommon;
private boolean mSetup = false; private boolean mSetup = false;
public JavaBridgeActivityTestRule() { public static class Controller {
super(); private static final int RESULT_WAIT_TIME = 5000;
mTestCommon = new JavaBridgeTestCommon(this);
private boolean mIsResultReady;
protected synchronized void notifyResultIsReady() {
mIsResultReady = true;
notify();
}
protected synchronized void waitForResult() {
while (!mIsResultReady) {
try {
wait(RESULT_WAIT_TIME);
} catch (Exception e) {
continue;
}
if (!mIsResultReady) {
Assert.fail("Wait timed out");
}
}
mIsResultReady = false;
}
}
public TestCallbackHelperContainer getTestCallBackHelperContainer() {
return mTestCallbackHelperContainer;
} }
/** /**
* Sets up the ContentView. Intended to be called from setUp(). * Sets up the ContentView. Intended to be called from setUp().
*/ */
public void setUpContentView() { public void setUpContentView() {
mTestCommon.setUpContentView(); // 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>"));
public TestCallbackHelperContainer getTestCallBackHelperContainer() { waitForActiveShellToBeDoneLoading();
return mTestCommon.getTestCallBackHelperContainer();
try {
runOnUiThread(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));
}
} }
public void executeJavaScript(String script) throws Throwable { public void executeJavaScript(String script) throws Throwable {
mTestCommon.executeJavaScript(script); runOnUiThread(new Runnable() {
@Override
public void run() {
// When a JavaScript URL is executed, if the value of the last
// expression evaluated is not 'undefined', this value is
// 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 + " })()"));
}
});
} }
public void injectObjectAndReload(Object object, String name) throws Exception { public void injectObjectAndReload(Object object, String name) throws Exception {
...@@ -51,19 +102,49 @@ public class JavaBridgeActivityTestRule ...@@ -51,19 +102,49 @@ public class JavaBridgeActivityTestRule
public void injectObjectAndReload(Object object, String name, public void injectObjectAndReload(Object object, String name,
Class<? extends Annotation> requiredAnnotation) throws Exception { Class<? extends Annotation> requiredAnnotation) throws Exception {
mTestCommon.injectObjectsAndReload(object, name, null, null, requiredAnnotation); injectObjectsAndReload(object, name, null, null, requiredAnnotation);
} }
public void injectObjectsAndReload(final Object object1, final String name1, public void injectObjectsAndReload(final Object object1, final String name1,
final Object object2, final String name2, final Object object2, final String name2,
final Class<? extends Annotation> requiredAnnotation) throws Exception { final Class<? extends Annotation> requiredAnnotation) throws Exception {
mTestCommon.injectObjectsAndReload(object1, name1, object2, name2, requiredAnnotation); TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mTestCallbackHelperContainer.getOnPageFinishedHelper();
int currentCallCount = onPageFinishedHelper.getCallCount();
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
WebContents webContents = getWebContents();
JavascriptInjector injector = JavascriptInjector.fromWebContents(webContents);
injector.addPossiblyUnsafeInterface(object1, name1, requiredAnnotation);
if (object2 != null && name2 != null) {
injector.addPossiblyUnsafeInterface(object2, name2, requiredAnnotation);
}
webContents.getNavigationController().reload(true);
}
});
onPageFinishedHelper.waitForCallback(currentCallCount);
} catch (Throwable e) {
throw new RuntimeException(
"Failed to injectObjectsAndReload: " + Log.getStackTraceString(e));
}
} }
public void synchronousPageReload() throws Throwable { public void synchronousPageReload() throws Throwable {
mTestCommon.synchronousPageReload(); TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mTestCallbackHelperContainer.getOnPageFinishedHelper();
int currentCallCount = onPageFinishedHelper.getCallCount();
runOnUiThread(new Runnable() {
@Override
public void run() {
getWebContents().getNavigationController().reload(true);
}
});
onPageFinishedHelper.waitForCallback(currentCallCount);
} }
@Override @Override
public Statement apply(Statement base, Description desc) { public Statement apply(Statement base, Description desc) {
SetUpStatement setUpBase = new SetUpStatement(base, this, mSetup); SetUpStatement setUpBase = new SetUpStatement(base, this, mSetup);
......
...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith; ...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
/** /**
* Part of the test suite for the Java Bridge. This class tests that we correctly convert * Part of the test suite for the Java Bridge. This class tests that we correctly convert
......
...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith; ...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
/** /**
* Part of the test suite for the Java Bridge. This class tests the general use of arrays. * Part of the test suite for the Java Bridge. This class tests the general use of arrays.
......
...@@ -29,7 +29,6 @@ public class JavaBridgeBareboneTest { ...@@ -29,7 +29,6 @@ public class JavaBridgeBareboneTest {
new JavaBridgeActivityTestRule().shouldSetUp(false); new JavaBridgeActivityTestRule().shouldSetUp(false);
private TestCallbackHelperContainer mTestCallbackHelperContainer; private TestCallbackHelperContainer mTestCallbackHelperContainer;
private final JavaBridgeTestCommon mTestCommon = new JavaBridgeTestCommon(mActivityTestRule);
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
......
...@@ -18,7 +18,7 @@ import org.junit.runner.RunWith; ...@@ -18,7 +18,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
import org.chromium.content.browser.test.ContentJUnit4ClassRunner; import org.chromium.content.browser.test.ContentJUnit4ClassRunner;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer; import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
......
...@@ -17,7 +17,7 @@ import org.chromium.base.test.util.CommandLineFlags; ...@@ -17,7 +17,7 @@ import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.RetryOnFailure; import org.chromium.base.test.util.RetryOnFailure;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
import org.chromium.content.browser.test.ContentJUnit4ClassRunner; import org.chromium.content.browser.test.ContentJUnit4ClassRunner;
import org.chromium.content_public.browser.JavaScriptCallback; import org.chromium.content_public.browser.JavaScriptCallback;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
......
...@@ -17,7 +17,7 @@ import org.junit.runner.RunWith; ...@@ -17,7 +17,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils; import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
import java.io.File; import java.io.File;
......
...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith; ...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
/** /**
* Part of the test suite for the Java Bridge. This test tests the * Part of the test suite for the Java Bridge. This test tests the
......
...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith; ...@@ -14,7 +14,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.JavaBridgeTestCommon.Controller; import org.chromium.content.browser.JavaBridgeActivityTestRule.Controller;
/** /**
* Part of the test suite for the Java Bridge. This test checks that we correctly convert Java * Part of the test suite for the Java Bridge. This test checks that we correctly convert Java
......
// 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.browser;
import android.util.Log;
import org.junit.Assert;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content_public.browser.JavascriptInjector;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_shell_apk.ContentShellActivity;
import org.chromium.content_shell_apk.ContentShellTestCommon.TestCommonCallback;
import java.lang.annotation.Annotation;
/**
* Common functionality implementation for testing the Java Bridge.
*/
public final class JavaBridgeTestCommon {
protected TestCallbackHelperContainer mTestCallbackHelperContainer;
private final TestCommonCallback<ContentShellActivity> mCallback;
public JavaBridgeTestCommon(TestCommonCallback<ContentShellActivity> callback) {
mCallback = callback;
}
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(RESULT_WAIT_TIME);
} catch (Exception e) {
continue;
}
if (!mIsResultReady) {
Assert.fail("Wait timed out");
}
}
mIsResultReady = false;
}
}
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
// expression evaluated is not 'undefined', this value is
// 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.
mCallback.getWebContentsForTestCommon().getNavigationController().loadUrl(
new LoadUrlParams("javascript:(function() { " + script + " })()"));
}
});
}
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 {
mCallback.runOnUiThreadForTestCommon(new Runnable() {
@Override
public void run() {
WebContents webContents = mCallback.getWebContentsForTestCommon();
JavascriptInjector injector = JavascriptInjector.fromWebContents(webContents);
injector.addPossiblyUnsafeInterface(object1, name1, requiredAnnotation);
if (object2 != null && name2 != null) {
injector.addPossiblyUnsafeInterface(object2, name2, requiredAnnotation);
}
webContents.getNavigationController().reload(true);
}
});
onPageFinishedHelper.waitForCallback(currentCallCount);
} catch (Throwable e) {
throw new RuntimeException(
"Failed to injectObjectsAndReload: " + Log.getStackTraceString(e));
}
}
void synchronousPageReload() throws Throwable {
TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
mTestCallbackHelperContainer.getOnPageFinishedHelper();
int currentCallCount = onPageFinishedHelper.getCallCount();
mCallback.runOnUiThreadForTestCommon(new Runnable() {
@Override
public void run() {
mCallback.getWebContentsForTestCommon().getNavigationController().reload(true);
}
});
onPageFinishedHelper.waitForCallback(currentCallCount);
}
}
...@@ -227,7 +227,7 @@ public class DialogOverlayImplTestRule extends ContentShellActivityTestRule { ...@@ -227,7 +227,7 @@ public class DialogOverlayImplTestRule extends ContentShellActivityTestRule {
} }
}; };
getActivityForTestCommon().getActiveShell().setOverayModeChangedCallbackForTesting( getActivity().getActiveShell().setOverayModeChangedCallbackForTesting(
overlayModeChanged); overlayModeChanged);
} }
}, desc); }, desc);
......
...@@ -187,7 +187,6 @@ android_library("content_shell_test_java") { ...@@ -187,7 +187,6 @@ android_library("content_shell_test_java") {
java_files = [ java_files = [
"javatests/src/org/chromium/content_shell_apk/ContentShellPreconditionsTest.java", "javatests/src/org/chromium/content_shell_apk/ContentShellPreconditionsTest.java",
"javatests/src/org/chromium/content_shell_apk/ContentShellShellManagementTest.java", "javatests/src/org/chromium/content_shell_apk/ContentShellShellManagementTest.java",
"javatests/src/org/chromium/content_shell_apk/ContentShellTestCommon.java",
"javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java", "javatests/src/org/chromium/content_shell_apk/ContentShellActivityTestRule.java",
"javatests/src/org/chromium/content_shell_apk/ContentShellUrlTest.java", "javatests/src/org/chromium/content_shell_apk/ContentShellUrlTest.java",
] ]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment