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