Chromium Code Reviews| Index: content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeActivityTestRule.java |
| diff --git a/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeActivityTestRule.java b/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeActivityTestRule.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d600829988c2ca907438025a07611ac2ef3f4b62 |
| --- /dev/null |
| +++ b/content/public/android/javatests/src/org/chromium/content/browser/JavaBridgeActivityTestRule.java |
| @@ -0,0 +1,142 @@ |
| +// 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.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.ContentShellActivityTestRule; |
| + |
| +import java.lang.annotation.Annotation; |
| + |
| +/** |
| + * ActivityTestRule with common functionality for testing the Java Bridge. |
| + */ |
| +public class JavaBridgeActivityTestRule extends ContentShellActivityTestRule { |
| + protected TestCallbackHelperContainer mTestCallbackHelperContainer; |
| + |
| + /** |
| + * Sets up the ContentView. Intended to be called from setUp(). |
| + */ |
| + private void 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>")); |
| + |
| + waitForActiveShellToBeDoneLoading(); |
| + |
| + 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)); |
| + } |
| + } |
| + |
| + @Override |
| + protected void beforeActivityLaunched() { |
| + super.beforeActivityLaunched(); |
| + setUpContentView(); |
| + } |
| + |
| + @SuppressFBWarnings("CHROMIUM_SYNCHRONIZED_METHOD") |
| + protected class Controller { |
| + private boolean mIsResultReady; |
| + |
| + protected synchronized void notifyResultIsReady() { |
| + mIsResultReady = true; |
| + notify(); |
| + } |
| + |
| + protected synchronized void waitForResult() { |
| + while (!mIsResultReady) { |
| + try { |
| + wait(5000); |
|
mikecase (-- gone --)
2017/02/23 19:21:35
Consider replacing this value with CONSTANT. Maybe
the real yoland
2017/02/27 18:32:49
Done
|
| + } catch (Exception e) { |
| + continue; |
| + } |
| + if (!mIsResultReady) { |
| + Assert.fail("Wait timed out"); |
| + } |
| + } |
| + mIsResultReady = false; |
| + } |
| + } |
| + |
| + public void executeJavaScript(final String script) throws Throwable { |
| + 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(final Object object, final String name) throws Exception { |
| + injectObjectAndReload(object, name, null); |
| + } |
| + |
| + public void injectObjectAndReload(final Object object, final String name, |
| + final Class<? extends Annotation> requiredAnnotation) throws Exception { |
| + injectObjectsAndReload(object, name, null, null, requiredAnnotation); |
| + } |
| + |
| + public 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 { |
| + runOnUiThread(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)); |
| + } |
| + } |
| + |
| + public void synchronousPageReload() throws Throwable { |
| + TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
| + mTestCallbackHelperContainer.getOnPageFinishedHelper(); |
| + int currentCallCount = onPageFinishedHelper.getCallCount(); |
| + runOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + getContentViewCore().getWebContents().getNavigationController().reload(true); |
| + } |
| + }); |
| + onPageFinishedHelper.waitForCallback(currentCallCount); |
| + } |
| +} |