JavaScriptUtils.java revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser.test.util;
6
7import junit.framework.Assert;
8
9import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
10
11import org.chromium.base.ThreadUtils;
12import org.chromium.content.browser.ContentView;
13import org.chromium.content.browser.ContentViewCore;
14
15import java.util.concurrent.TimeUnit;
16import java.util.concurrent.TimeoutException;
17
18/**
19 * Collection of JavaScript utilities.
20 */
21public class JavaScriptUtils {
22    private static final long EVALUATION_TIMEOUT_SECONDS = scaleTimeout(5);
23
24    /**
25     * Executes the given snippet of JavaScript code within the given ContentView.
26     * Returns the result of its execution in JSON format.
27     */
28    public static String executeJavaScriptAndWaitForResult(
29            final ContentView view, TestCallbackHelperContainer viewClient,
30            final String code) throws InterruptedException, TimeoutException {
31        return executeJavaScriptAndWaitForResult(
32                view.getContentViewCore(),
33                viewClient.getOnEvaluateJavaScriptResultHelper(),
34                code);
35    }
36
37    /**
38     * Executes the given snippet of JavaScript code within the given ContentViewCore.
39     * Does not depend on ContentView and TestCallbackHelperContainer.
40     * Returns the result of its execution in JSON format.
41     */
42    public static String executeJavaScriptAndWaitForResult(
43            final ContentViewCore viewCore,
44            final TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper helper,
45            final String code) throws InterruptedException, TimeoutException {
46        return executeJavaScriptAndWaitForResult(
47                viewCore, helper, code, EVALUATION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
48    }
49
50    /**
51     * Executes the given snippet of JavaScript code within the given ContentViewCore.
52     * Does not depend on ContentView and TestCallbackHelperContainer.
53     * Returns the result of its execution in JSON format.
54     */
55    public static String executeJavaScriptAndWaitForResult(
56            final ContentViewCore viewCore,
57            final TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper helper,
58            final String code,
59            final long timeout, final TimeUnit timeoutUnits)
60                    throws InterruptedException, TimeoutException {
61        // Calling this from the UI thread causes it to time-out: the UI thread being blocked won't
62        // have a chance to process the JavaScript eval response).
63        Assert.assertFalse("Executing JavaScript should be done from the test thread, "
64                + " not the UI thread", ThreadUtils.runningOnUiThread());
65        ThreadUtils.runOnUiThread(new Runnable() {
66            @Override
67            public void run() {
68                helper.evaluateJavaScript(viewCore, code);
69            }
70        });
71        helper.waitUntilHasValue(timeout, timeoutUnits);
72        Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
73        return helper.getJsonResultAndClear();
74    }
75
76    /**
77     * Executes the given snippet of JavaScript code but does not wait for the result.
78     */
79    public static void executeJavaScript(final ContentView view, final String code) {
80        ThreadUtils.runOnUiThread(new Runnable() {
81            @Override
82            public void run() {
83                view.evaluateJavaScript(code);
84            }
85        });
86    }
87}
88