OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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.android_webview.test; | 5 package org.chromium.android_webview.test; |
6 | 6 |
7 import android.app.Instrumentation; | 7 import android.app.Instrumentation; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.test.ActivityInstrumentationTestCase2; | 9 import android.test.ActivityInstrumentationTestCase2; |
10 | 10 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 getInstrumentation().waitForIdleSync(); | 67 getInstrumentation().waitForIdleSync(); |
68 getInstrumentation().runOnMainSync(task); | 68 getInstrumentation().runOnMainSync(task); |
69 try { | 69 try { |
70 return task.get(); | 70 return task.get(); |
71 } catch (ExecutionException e) { | 71 } catch (ExecutionException e) { |
72 // Unwrap the cause of the exception and re-throw it. | 72 // Unwrap the cause of the exception and re-throw it. |
73 throw e.getCause(); | 73 throw e.getCause(); |
74 } | 74 } |
75 } | 75 } |
76 | 76 |
77 protected void enableJavaScriptOnUiThread(final ContentViewCore contentViewC
ore) { | 77 protected void enableJavaScriptOnUiThread(final AwContents awContents) { |
78 getInstrumentation().runOnMainSync(new Runnable() { | 78 getInstrumentation().runOnMainSync(new Runnable() { |
79 @Override | 79 @Override |
80 public void run() { | 80 public void run() { |
81 contentViewCore.getContentSettings().setJavaScriptEnabled(true); | 81 awContents.getContentViewCore().getContentSettings().setJavaScri
ptEnabled(true); |
82 } | 82 } |
83 }); | 83 }); |
84 } | 84 } |
85 | 85 |
86 /** | 86 /** |
87 * Loads url on the UI thread and blocks until onPageFinished is called. | 87 * Loads url on the UI thread and blocks until onPageFinished is called. |
88 */ | 88 */ |
89 protected void loadUrlSync(final ContentViewCore contentViewCore, | 89 protected void loadUrlSync(final AwContents awContents, |
90 CallbackHelper onPageFinishedHelper, | 90 CallbackHelper onPageFinishedHelper, |
91 final String url) throws Throwable { | 91 final String url) throws Throwable { |
92 int currentCallCount = onPageFinishedHelper.getCallCount(); | 92 int currentCallCount = onPageFinishedHelper.getCallCount(); |
93 loadUrlAsync(contentViewCore, url); | 93 loadUrlAsync(awContents, url); |
94 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, | 94 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, |
95 TimeUnit.SECONDS); | 95 TimeUnit.SECONDS); |
96 } | 96 } |
97 | 97 |
98 protected void loadUrlSyncAndExpectError(final ContentViewCore contentViewCo
re, | 98 protected void loadUrlSyncAndExpectError(final AwContents awContents, |
99 CallbackHelper onPageFinishedHelper, | 99 CallbackHelper onPageFinishedHelper, |
100 CallbackHelper onReceivedErrorHelper, | 100 CallbackHelper onReceivedErrorHelper, |
101 final String url) throws Throwable { | 101 final String url) throws Throwable { |
102 int onErrorCallCount = onReceivedErrorHelper.getCallCount(); | 102 int onErrorCallCount = onReceivedErrorHelper.getCallCount(); |
103 int onFinishedCallCount = onPageFinishedHelper.getCallCount(); | 103 int onFinishedCallCount = onPageFinishedHelper.getCallCount(); |
104 loadUrlAsync(contentViewCore, url); | 104 loadUrlAsync(awContents, url); |
105 onReceivedErrorHelper.waitForCallback(onErrorCallCount, 1, WAIT_TIMEOUT_
SECONDS, | 105 onReceivedErrorHelper.waitForCallback(onErrorCallCount, 1, WAIT_TIMEOUT_
SECONDS, |
106 TimeUnit.SECONDS); | 106 TimeUnit.SECONDS); |
107 onPageFinishedHelper.waitForCallback(onFinishedCallCount, 1, WAIT_TIMEOU
T_SECONDS, | 107 onPageFinishedHelper.waitForCallback(onFinishedCallCount, 1, WAIT_TIMEOU
T_SECONDS, |
108 TimeUnit.SECONDS); | 108 TimeUnit.SECONDS); |
109 } | 109 } |
110 | 110 |
111 /** | 111 /** |
112 * Loads url on the UI thread but does not block. | 112 * Loads url on the UI thread but does not block. |
113 */ | 113 */ |
114 protected void loadUrlAsync(final ContentViewCore contentViewCore, | 114 protected void loadUrlAsync(final AwContents awContents, |
115 final String url) throws Throwable { | 115 final String url) throws Throwable { |
116 runTestOnUiThread(new Runnable() { | 116 runTestOnUiThread(new Runnable() { |
117 @Override | 117 @Override |
118 public void run() { | 118 public void run() { |
119 contentViewCore.loadUrl(new LoadUrlParams(url)); | 119 awContents.getContentViewCore().loadUrl(new LoadUrlParams(url)); |
120 } | 120 } |
121 }); | 121 }); |
122 } | 122 } |
123 | 123 |
124 /** | 124 /** |
125 * Loads data on the UI thread and blocks until onPageFinished is called. | 125 * Loads data on the UI thread and blocks until onPageFinished is called. |
126 */ | 126 */ |
127 protected void loadDataSync(final ContentViewCore contentViewCore, | 127 protected void loadDataSync(final AwContents awContents, |
128 CallbackHelper onPageFinishedHelper, | 128 CallbackHelper onPageFinishedHelper, |
129 final String data, final String mimeType, | 129 final String data, final String mimeType, |
130 final boolean isBase64Encoded) throws Throwable
{ | 130 final boolean isBase64Encoded) throws Throwable
{ |
131 int currentCallCount = onPageFinishedHelper.getCallCount(); | 131 int currentCallCount = onPageFinishedHelper.getCallCount(); |
132 loadDataAsync(contentViewCore, data, mimeType, isBase64Encoded); | 132 loadDataAsync(awContents, data, mimeType, isBase64Encoded); |
133 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, | 133 onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_S
ECONDS, |
134 TimeUnit.SECONDS); | 134 TimeUnit.SECONDS); |
135 } | 135 } |
136 | 136 |
137 /** | 137 /** |
138 * Loads data on the UI thread but does not block. | 138 * Loads data on the UI thread but does not block. |
139 */ | 139 */ |
140 protected void loadDataAsync(final ContentViewCore contentViewCore, final St
ring data, | 140 protected void loadDataAsync(final AwContents awContents, final String data, |
141 final String mimeType, final boolean isBase64En
coded) | 141 final String mimeType, final boolean isBase64En
coded) |
142 throws Throwable { | 142 throws Throwable { |
143 runTestOnUiThread(new Runnable() { | 143 runTestOnUiThread(new Runnable() { |
144 @Override | 144 @Override |
145 public void run() { | 145 public void run() { |
146 contentViewCore.loadUrl(LoadUrlParams.createLoadDataParams( | 146 awContents.loadUrl(LoadUrlParams.createLoadDataParams( |
147 data, mimeType, isBase64Encoded)); | 147 data, mimeType, isBase64Encoded)); |
148 } | 148 } |
149 }); | 149 }); |
150 } | 150 } |
151 | 151 |
152 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, | 152 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, |
153 final AwContentsClient contentsClient) { | 153 final AwContentsClient awContentsClient) { |
154 return createAwTestContainerView(incognito, new AwTestContainerView(getA
ctivity()), | 154 return createAwTestContainerView(incognito, new AwTestContainerView(getA
ctivity()), |
155 contentsClient); | 155 awContentsClient); |
156 } | 156 } |
157 | 157 |
158 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, | 158 protected AwTestContainerView createAwTestContainerView(final boolean incogn
ito, |
159 final AwTestContainerView testContainerView, | 159 final AwTestContainerView testContainerView, |
160 final AwContentsClient contentsClient) { | 160 final AwContentsClient awContentsClient) { |
161 ContentViewCore contentViewCore = new ContentViewCore( | 161 ContentViewCore contentViewCore = new ContentViewCore( |
162 getActivity(), ContentViewCore.PERSONALITY_VIEW); | 162 getActivity(), ContentViewCore.PERSONALITY_VIEW); |
163 testContainerView.initialize(contentViewCore, | 163 testContainerView.initialize(contentViewCore, |
164 new AwContents(testContainerView, testContainerView.getInternalA
ccessDelegate(), | 164 new AwContents(testContainerView, testContainerView.getInternalA
ccessDelegate(), |
165 contentViewCore, contentsClient, new ActivityNativeWindow(getAct
ivity()), | 165 contentViewCore, awContentsClient, new ActivityNativeWindow(getA
ctivity()), |
166 incognito, false)); | 166 incognito, false)); |
167 getActivity().addView(testContainerView); | 167 getActivity().addView(testContainerView); |
168 return testContainerView; | 168 return testContainerView; |
169 } | 169 } |
170 | 170 |
171 protected AwTestContainerView createAwTestContainerViewOnMainSync( | 171 protected AwTestContainerView createAwTestContainerViewOnMainSync( |
172 final AwContentsClient client) throws Exception { | 172 final AwContentsClient client) throws Exception { |
173 return createAwTestContainerViewOnMainSync(NORMAL_VIEW, client); | 173 return createAwTestContainerViewOnMainSync(NORMAL_VIEW, client); |
174 } | 174 } |
175 | 175 |
176 protected AwTestContainerView createAwTestContainerViewOnMainSync( | 176 protected AwTestContainerView createAwTestContainerViewOnMainSync( |
177 final boolean incognito, | 177 final boolean incognito, |
178 final AwContentsClient client) throws Exception { | 178 final AwContentsClient client) throws Exception { |
179 final AtomicReference<AwTestContainerView> testContainerView = | 179 final AtomicReference<AwTestContainerView> testContainerView = |
180 new AtomicReference<AwTestContainerView>(); | 180 new AtomicReference<AwTestContainerView>(); |
181 final Context context = getActivity(); | 181 final Context context = getActivity(); |
182 getInstrumentation().runOnMainSync(new Runnable() { | 182 getInstrumentation().runOnMainSync(new Runnable() { |
183 @Override | 183 @Override |
184 public void run() { | 184 public void run() { |
185 testContainerView.set(createAwTestContainerView(incognito, clien
t)); | 185 testContainerView.set(createAwTestContainerView(incognito, clien
t)); |
186 } | 186 } |
187 }); | 187 }); |
188 return testContainerView.get(); | 188 return testContainerView.get(); |
189 } | 189 } |
190 | 190 |
191 protected void destroyAwContentsOnMainSync(final AwContents contents) { | 191 protected void destroyAwContentsOnMainSync(final AwContents awContents) { |
192 if (contents == null) return; | 192 if (awContents == null) return; |
193 getInstrumentation().runOnMainSync(new Runnable() { | 193 getInstrumentation().runOnMainSync(new Runnable() { |
194 @Override | 194 @Override |
195 public void run() { | 195 public void run() { |
196 contents.destroy(); | 196 awContents.destroy(); |
197 } | 197 } |
198 }); | 198 }); |
199 } | 199 } |
200 | 200 |
201 protected String getTitleOnUiThread(final ContentViewCore contentViewCore) t
hrows Throwable { | 201 protected String getTitleOnUiThread(final AwContents awContents) throws Thro
wable { |
202 return runTestOnUiThreadAndGetResult(new Callable<String>() { | 202 return runTestOnUiThreadAndGetResult(new Callable<String>() { |
203 @Override | 203 @Override |
204 public String call() throws Exception { | 204 public String call() throws Exception { |
205 return contentViewCore.getTitle(); | 205 return awContents.getContentViewCore().getTitle(); |
206 } | 206 } |
207 }); | 207 }); |
208 } | 208 } |
209 | 209 |
210 protected ContentSettings getContentSettingsOnUiThread( | 210 protected ContentSettings getContentSettingsOnUiThread( |
211 final ContentViewCore contentViewCore) throws Throwable { | 211 final AwContents awContents) throws Throwable { |
212 return runTestOnUiThreadAndGetResult(new Callable<ContentSettings>() { | 212 return runTestOnUiThreadAndGetResult(new Callable<ContentSettings>() { |
213 @Override | 213 @Override |
214 public ContentSettings call() throws Exception { | 214 public ContentSettings call() throws Exception { |
215 return contentViewCore.getContentSettings(); | 215 return awContents.getContentViewCore().getContentSettings(); |
216 } | 216 } |
217 }); | 217 }); |
218 } | 218 } |
219 | 219 |
220 protected AwSettings getAwSettingsOnUiThread( | 220 protected AwSettings getAwSettingsOnUiThread( |
221 final AwContents awContents) throws Throwable { | 221 final AwContents awContents) throws Throwable { |
222 return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { | 222 return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() { |
223 @Override | 223 @Override |
224 public AwSettings call() throws Exception { | 224 public AwSettings call() throws Exception { |
225 return awContents.getSettings(); | 225 return awContents.getSettings(); |
226 } | 226 } |
227 }); | 227 }); |
228 } | 228 } |
229 | 229 |
230 /** | 230 /** |
231 * Executes the given snippet of JavaScript code within the given ContentVie
w. Returns the | 231 * Executes the given snippet of JavaScript code within the given ContentVie
w. Returns the |
232 * result of its execution in JSON format. | 232 * result of its execution in JSON format. |
233 */ | 233 */ |
234 protected String executeJavaScriptAndWaitForResult(final ContentViewCore cor
e, | 234 protected String executeJavaScriptAndWaitForResult(final AwContents awConten
ts, |
235 TestAwContentsClient viewClient, final String code) throws Throwable
{ | 235 TestAwContentsClient viewClient, final String code) throws Throwable
{ |
236 final AtomicInteger requestId = new AtomicInteger(); | 236 final AtomicInteger requestId = new AtomicInteger(); |
237 TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper | 237 TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper |
238 onEvaluateJavaScriptResultHelper = viewClient.getOnEvaluateJavaS
criptResultHelper(); | 238 onEvaluateJavaScriptResultHelper = viewClient.getOnEvaluateJavaS
criptResultHelper(); |
239 int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount(); | 239 int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount(); |
240 runTestOnUiThread(new Runnable() { | 240 runTestOnUiThread(new Runnable() { |
241 @Override | 241 @Override |
242 public void run() { | 242 public void run() { |
243 requestId.set(core.evaluateJavaScript(code)); | 243 requestId.set(awContents.getContentViewCore().evaluateJavaScript
(code)); |
244 } | 244 } |
245 }); | 245 }); |
246 onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); | 246 onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); |
247 Assert.assertEquals("Response ID mismatch when evaluating JavaScript.", | 247 Assert.assertEquals("Response ID mismatch when evaluating JavaScript.", |
248 requestId.get(), onEvaluateJavaScriptResultHelper.getId()); | 248 requestId.get(), onEvaluateJavaScriptResultHelper.getId()); |
249 return onEvaluateJavaScriptResultHelper.getJsonResult(); | 249 return onEvaluateJavaScriptResultHelper.getJsonResult(); |
250 } | 250 } |
251 } | 251 } |
OLD | NEW |