Index: content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestBase.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/JavaBridgeTestBase.java |
index aa7308444a9bed93d32f3238037223a360fa281f..2011b106523aa58808875adaecdf4a1093027653 100644 |
--- a/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestBase.java |
+++ b/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeTestBase.java |
@@ -4,15 +4,56 @@ |
package org.chromium.content.browser; |
+import android.util.Log; |
+ |
import junit.framework.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 java.lang.annotation.Annotation; |
/** |
* Common functionality for testing the Java Bridge. |
*/ |
-public class JavaBridgeTestBase extends ContentViewTestBase { |
+public class JavaBridgeTestBase extends ContentShellTestBase { |
+ |
+ 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>")); |
+ |
+ 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(); |
+ } |
+ |
@SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
protected class Controller { |
private boolean mIsResultReady; |
@@ -50,4 +91,39 @@ public class JavaBridgeTestBase extends ContentViewTestBase { |
} |
}); |
} |
+ |
+ 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 { |
+ TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
+ mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
+ int currentCallCount = onPageFinishedHelper.getCallCount(); |
+ try { |
+ runTestOnUiThread(new Runnable() { |
+ @Override |
+ public void run() { |
+ getContentViewCore().addPossiblyUnsafeJavascriptInterface(object1, |
+ name1, requiredAnnotation); |
+ if (object2 != null && name2 != null) { |
+ getContentViewCore().addPossiblyUnsafeJavascriptInterface(object2, |
+ name2, requiredAnnotation); |
+ } |
+ getContentViewCore().getWebContents().getNavigationController().reload(true); |
+ } |
+ }); |
+ onPageFinishedHelper.waitForCallback(currentCallCount); |
+ } catch (Throwable e) { |
+ throw new RuntimeException( |
+ "Failed to injectObjectsAndReload: " + Log.getStackTraceString(e)); |
+ } |
+ } |
} |