| Index: content/shell/android/javatests/src/org/chromium/content_shell/JavaBridgeTestBase.java
|
| diff --git a/content/shell/android/javatests/src/org/chromium/content_shell/JavaBridgeTestBase.java b/content/shell/android/javatests/src/org/chromium/content_shell/JavaBridgeTestBase.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..774c0bd173be890809ca89993e9eee3a0ee4a9bb
|
| --- /dev/null
|
| +++ b/content/shell/android/javatests/src/org/chromium/content_shell/JavaBridgeTestBase.java
|
| @@ -0,0 +1,83 @@
|
| +// Copyright (c) 2012 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_shell;
|
| +
|
| +import android.util.Log;
|
| +
|
| +import junit.framework.Assert;
|
| +
|
| +import org.chromium.content.browser.ContentView;
|
| +import org.chromium.content.browser.LoadUrlParams;
|
| +import org.chromium.content.browser.test.TestContentViewClient;
|
| +
|
| +/**
|
| + * Common functionality for testing the Java Bridge.
|
| + */
|
| +public class JavaBridgeTestBase extends ContentShellTestBase {
|
| + protected class Controller {
|
| + private boolean mIsResultReady;
|
| +
|
| + protected synchronized void notifyResultIsReady() {
|
| + mIsResultReady = true;
|
| + notify();
|
| + }
|
| + protected synchronized void waitForResult() {
|
| + while (!mIsResultReady) {
|
| + try {
|
| + wait(5000);
|
| + } catch (Exception e) {
|
| + continue;
|
| + }
|
| + if (!mIsResultReady) {
|
| + Assert.fail("Wait timed out");
|
| + }
|
| + }
|
| + mIsResultReady = false;
|
| + }
|
| + }
|
| +
|
| + protected TestContentViewClient mContentViewClient;
|
| +
|
| + // Sets up the ContentView and injects the supplied object. Intended to be called from setUp().
|
| + protected void setUpContentView(final Object object, final String name) throws Exception {
|
| + mContentViewClient = new TestContentViewClient();
|
| + // This starts the activity, so must be called on the test thread.
|
| + final ContentShellActivity activity = launchContentShellWithUrl(
|
| + "data:text/html;utf-8,<html><head></head><body>test</body></html>");
|
| + // On the UI thread, load an empty page and wait for it to finish
|
| + // loading so that the Java object is injected.
|
| + try {
|
| + runTestOnUiThread(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + ContentView contentView = activity.getActiveContentView();
|
| + contentView.getContentViewCore().addJavascriptInterface(object, name, false);
|
| + setTestContentViewClient(contentView, mContentViewClient);
|
| + }
|
| + });
|
| + TestContentViewClient.OnPageFinishedHelper onPageFinishedHelper =
|
| + mContentViewClient.getOnPageFinishedHelper();
|
| + onPageFinishedHelper.waitForCallback(0);
|
| + } catch (Throwable e) {
|
| + throw new RuntimeException(
|
| + "Failed to set up ContentView: " + Log.getStackTraceString(e));
|
| + }
|
| + }
|
| +
|
| + protected void executeJavaScript(final String script) throws Throwable {
|
| + runTestOnUiThread(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.
|
| + getContentView().loadUrl(new LoadUrlParams(
|
| + "javascript:(function() { " + script + " })()"));
|
| + }
|
| + });
|
| + }
|
| +}
|
|
|