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