Chromium Code Reviews| Index: content/public/test/android/javatests/src/org/chromium/content/browser/test/util/JavaScriptUtils.java |
| diff --git a/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/JavaScriptUtils.java b/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/JavaScriptUtils.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..49e8c184ebdead5b5032dc0d8d24009b8e54e3cf |
| --- /dev/null |
| +++ b/content/public/test/android/javatests/src/org/chromium/content/browser/test/util/JavaScriptUtils.java |
| @@ -0,0 +1,53 @@ |
| +// 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.browser.test.util; |
| + |
| +import android.test.InstrumentationTestCase; |
| + |
| +import junit.framework.Assert; |
| + |
| +import org.chromium.content.browser.ContentView; |
| + |
| +/** |
| + * Collection of JavaScript utilities. |
| + */ |
| +public class JavaScriptUtils { |
| + private static int mRequestId; |
| + |
| + /** |
| + * Executes the given snippet of JavaScript code within the given ContentView. Returns the |
| + * result of its execution in JSON format. |
| + */ |
| + public static String executeJavaScriptAndWaitForResult(InstrumentationTestCase test, |
| + final ContentView view, TestCallbackHelperContainer viewClient, final String code) |
|
Yaron
2012/10/09 02:12:45
We should probably make this operate on a ContentV
Leandro GraciĆ” Gil
2012/10/10 00:35:54
Deferring this to later patches / follow-ups as su
|
| + throws Throwable { |
| + TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper |
| + onEvaluateJavaScriptResultHelper = viewClient.getOnEvaluateJavaScriptResultHelper(); |
| + int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount(); |
| + test.runTestOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + mRequestId = view.evaluateJavaScript(code); |
| + } |
| + }); |
| + onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); |
| + Assert.assertEquals("Response ID mismatch when evaluating JavaScript.", |
| + mRequestId, onEvaluateJavaScriptResultHelper.getId()); |
| + return onEvaluateJavaScriptResultHelper.getJsonResult(); |
| + } |
| + |
| + /** |
| + * Executes the given snippet of JavaScript code but does not wait for the result. |
| + */ |
| + public static void executeJavaScript(InstrumentationTestCase test, final ContentView view, |
| + final String code) throws Throwable { |
| + test.runTestOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + view.evaluateJavaScript(code); |
| + } |
| + }); |
| + } |
| +} |