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 | 7 |
8 import org.chromium.base.ThreadUtils; | 8 import org.chromium.base.ThreadUtils; |
9 import org.chromium.content.browser.ContentViewCore; | 9 import org.chromium.content.browser.ContentViewCore; |
10 import org.chromium.content_public.browser.JavaScriptCallback; | 10 import org.chromium.content_public.browser.JavaScriptCallback; |
| 11 import org.chromium.content_public.browser.WebContents; |
11 | 12 |
12 import java.util.concurrent.TimeUnit; | 13 import java.util.concurrent.TimeUnit; |
13 import java.util.concurrent.TimeoutException; | 14 import java.util.concurrent.TimeoutException; |
14 | 15 |
15 /** | 16 /** |
16 * This class is used to provide callback hooks for tests and related classes. | 17 * This class is used to provide callback hooks for tests and related classes. |
17 */ | 18 */ |
18 public class TestCallbackHelperContainer { | 19 public class TestCallbackHelperContainer { |
19 private final TestContentViewClient mTestContentViewClient; | 20 private final TestContentViewClient mTestContentViewClient; |
20 private TestWebContentsObserver mTestWebContentsObserver; | 21 private TestWebContentsObserver mTestWebContentsObserver; |
21 | 22 |
22 public TestCallbackHelperContainer(final ContentViewCore contentViewCore) { | 23 public TestCallbackHelperContainer(final ContentViewCore contentViewCore) { |
23 mTestContentViewClient = new TestContentViewClient(); | 24 mTestContentViewClient = new TestContentViewClient(); |
24 contentViewCore.setContentViewClient(mTestContentViewClient); | 25 contentViewCore.setContentViewClient(mTestContentViewClient); |
25 // TODO(yfriedman): Change callers to be executed on the UI thread. Unfo
rtunately this is | 26 // TODO(yfriedman): Change callers to be executed on the UI thread. Unfo
rtunately this is |
26 // super convenient as the caller is nearly always on the test thread wh
ich is fine to block | 27 // super convenient as the caller is nearly always on the test thread wh
ich is fine to block |
27 // and it's cumbersome to keep bouncing to the UI thread. | 28 // and it's cumbersome to keep bouncing to the UI thread. |
28 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 29 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
29 @Override | 30 @Override |
30 public void run() { | 31 public void run() { |
31 mTestWebContentsObserver = new TestWebContentsObserver(contentVi
ewCore); | 32 mTestWebContentsObserver = new TestWebContentsObserver(contentVi
ewCore |
| 33 .getWebContents()); |
32 } | 34 } |
33 }); | 35 }); |
34 } | 36 } |
35 | 37 |
36 protected TestCallbackHelperContainer( | 38 protected TestCallbackHelperContainer( |
37 TestContentViewClient viewClient, TestWebContentsObserver contentsOb
server) { | 39 TestContentViewClient viewClient, TestWebContentsObserver contentsOb
server) { |
38 mTestContentViewClient = viewClient; | 40 mTestContentViewClient = viewClient; |
39 mTestWebContentsObserver = contentsObserver; | 41 mTestWebContentsObserver = contentsObserver; |
40 } | 42 } |
41 | 43 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 100 |
99 /** | 101 /** |
100 * CallbackHelper for OnEvaluateJavaScriptResult. | 102 * CallbackHelper for OnEvaluateJavaScriptResult. |
101 * This class wraps the evaluation of JavaScript code allowing test code to | 103 * This class wraps the evaluation of JavaScript code allowing test code to |
102 * synchronously evaluate JavaScript and then test the result. | 104 * synchronously evaluate JavaScript and then test the result. |
103 */ | 105 */ |
104 public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper
{ | 106 public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper
{ |
105 private String mJsonResult; | 107 private String mJsonResult; |
106 | 108 |
107 /** | 109 /** |
108 * Starts evaluation of a given JavaScript code on a given contentViewCo
re. | 110 * Starts evaluation of a given JavaScript code on a given webContents. |
109 * @param contentViewCore A ContentViewCore instance to be used. | 111 * @param webContents A WebContents instance to be used. |
110 * @param code A JavaScript code to be evaluated. | 112 * @param code A JavaScript code to be evaluated. |
111 */ | 113 */ |
112 public void evaluateJavaScript(ContentViewCore contentViewCore, String c
ode) { | 114 public void evaluateJavaScript(WebContents webContents, String code) { |
113 JavaScriptCallback callback = | 115 JavaScriptCallback callback = |
114 new JavaScriptCallback() { | 116 new JavaScriptCallback() { |
115 @Override | 117 @Override |
116 public void handleJavaScriptResult(String jsonResult) { | 118 public void handleJavaScriptResult(String jsonResult) { |
117 notifyCalled(jsonResult); | 119 notifyCalled(jsonResult); |
118 } | 120 } |
119 }; | 121 }; |
120 contentViewCore.evaluateJavaScript(code, callback); | 122 webContents.evaluateJavaScript(code, callback); |
121 mJsonResult = null; | 123 mJsonResult = null; |
122 } | 124 } |
123 | 125 |
124 /** | 126 /** |
125 * Returns true if the evaluation started by evaluateJavaScript() has co
mpleted. | 127 * Returns true if the evaluation started by evaluateJavaScript() has co
mpleted. |
126 */ | 128 */ |
127 public boolean hasValue() { | 129 public boolean hasValue() { |
128 return mJsonResult != null; | 130 return mJsonResult != null; |
129 } | 131 } |
130 | 132 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 201 } |
200 | 202 |
201 public OnReceivedErrorHelper getOnReceivedErrorHelper() { | 203 public OnReceivedErrorHelper getOnReceivedErrorHelper() { |
202 return mTestWebContentsObserver.getOnReceivedErrorHelper(); | 204 return mTestWebContentsObserver.getOnReceivedErrorHelper(); |
203 } | 205 } |
204 | 206 |
205 public OnStartContentIntentHelper getOnStartContentIntentHelper() { | 207 public OnStartContentIntentHelper getOnStartContentIntentHelper() { |
206 return mTestContentViewClient.getOnStartContentIntentHelper(); | 208 return mTestContentViewClient.getOnStartContentIntentHelper(); |
207 } | 209 } |
208 } | 210 } |
OLD | NEW |