Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / test / android / javatests / src / org / chromium / content / browser / test / util / JavaScriptUtils.java
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
5 package org.chromium.content.browser.test.util;
6
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9 import junit.framework.Assert;
10
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper;
13 import org.chromium.content_public.browser.WebContents;
14
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17
18 /**
19  * Collection of JavaScript utilities.
20  */
21 public 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             WebContents webContents, String code) throws InterruptedException, TimeoutException {
30         return executeJavaScriptAndWaitForResult(
31                 webContents, code, EVALUATION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
32     }
33
34     /**
35      * Executes the given snippet of JavaScript code within the given ContentViewCore.
36      * Does not depend on ContentView and TestCallbackHelperContainer.
37      * Returns the result of its execution in JSON format.
38      */
39     public static String executeJavaScriptAndWaitForResult(
40             final WebContents webContents,
41             final String code,
42             final long timeout,
43             final TimeUnit timeoutUnits)
44                     throws InterruptedException, TimeoutException {
45         final OnEvaluateJavaScriptResultHelper helper = new OnEvaluateJavaScriptResultHelper();
46         // Calling this from the UI thread causes it to time-out: the UI thread being blocked won't
47         // have a chance to process the JavaScript eval response).
48         Assert.assertFalse("Executing JavaScript should be done from the test thread, "
49                 + " not the UI thread", ThreadUtils.runningOnUiThread());
50         ThreadUtils.runOnUiThread(new Runnable() {
51             @Override
52             public void run() {
53                 helper.evaluateJavaScript(webContents, code);
54             }
55         });
56         helper.waitUntilHasValue(timeout, timeoutUnits);
57         Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
58         return helper.getJsonResultAndClear();
59     }
60
61     /**
62      * Executes the given snippet of JavaScript code but does not wait for the result.
63      */
64     public static void executeJavaScript(final WebContents webContents, final String code) {
65         ThreadUtils.runOnUiThread(new Runnable() {
66             @Override
67             public void run() {
68                 webContents.evaluateJavaScript(code, null);
69             }
70         });
71     }
72 }