OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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.android_webview.test; |
| 6 |
| 7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; |
| 8 |
| 9 import android.app.Instrumentation; |
| 10 import android.content.Context; |
| 11 import android.support.test.InstrumentationRegistry; |
| 12 import android.support.test.rule.ActivityTestRule; |
| 13 |
| 14 import org.junit.runner.Description; |
| 15 import org.junit.runners.model.Statement; |
| 16 |
| 17 import org.chromium.android_webview.AwBrowserContext; |
| 18 import org.chromium.android_webview.AwContents; |
| 19 import org.chromium.android_webview.AwContentsClient; |
| 20 import org.chromium.android_webview.AwSettings; |
| 21 import org.chromium.android_webview.test.AwTestBase.PopupInfo; |
| 22 import org.chromium.android_webview.test.AwTestBase.TestDependencyFactory; |
| 23 import org.chromium.android_webview.test.AwTestCommon.AwTestCommonCallback; |
| 24 import org.chromium.base.test.util.CallbackHelper; |
| 25 import org.chromium.base.test.util.InMemorySharedPreferences; |
| 26 import org.chromium.net.test.util.TestWebServer; |
| 27 |
| 28 import java.lang.annotation.Annotation; |
| 29 import java.util.Map; |
| 30 import java.util.concurrent.Callable; |
| 31 |
| 32 /** Custom ActivityTestRunner for WebView instrumentation tests */ |
| 33 public class AwActivityTestRule |
| 34 extends ActivityTestRule<AwTestRunnerActivity> implements AwTestCommonCa
llback { |
| 35 public static final long WAIT_TIMEOUT_MS = scaleTimeout(15000); |
| 36 |
| 37 public static final int CHECK_INTERVAL = 100; |
| 38 |
| 39 private Description mCurrentTestDescription; |
| 40 |
| 41 private final AwTestCommon mTestCommon; |
| 42 |
| 43 public AwActivityTestRule() { |
| 44 super(AwTestRunnerActivity.class); |
| 45 mTestCommon = new AwTestCommon(this); |
| 46 } |
| 47 |
| 48 @Override |
| 49 public Statement apply(final Statement base, Description description) { |
| 50 mCurrentTestDescription = description; |
| 51 return super.apply(new Statement() { |
| 52 @Override |
| 53 public void evaluate() throws Throwable { |
| 54 setUp(); |
| 55 base.evaluate(); |
| 56 } |
| 57 }, description); |
| 58 } |
| 59 |
| 60 public void setUp() throws Exception { |
| 61 mTestCommon.setUp(); |
| 62 } |
| 63 |
| 64 public void createAwBrowserContext() { |
| 65 mTestCommon.createAwBrowserContext(); |
| 66 } |
| 67 |
| 68 public AwBrowserContext createAwBrowserContextOnUiThread( |
| 69 InMemorySharedPreferences prefs, Context appContext) { |
| 70 return mTestCommon.createAwBrowserContextOnUiThread(prefs, appContext); |
| 71 } |
| 72 |
| 73 public void startBrowserProcess() throws Exception { |
| 74 mTestCommon.startBrowserProcess(); |
| 75 } |
| 76 |
| 77 /** |
| 78 * Override this to return false if the test doesn't want to create an AwBro
wserContext |
| 79 * automatically. |
| 80 */ |
| 81 protected boolean needsAwBrowserContextCreated() { |
| 82 return mTestCommon.needsAwBrowserContextCreated(); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Override this to return false if the test doesn't want the browser startu
p sequence to |
| 87 * be run automatically. |
| 88 * @return Whether the instrumentation test requires the browser process to
already be started. |
| 89 */ |
| 90 protected boolean needsBrowserProcessStarted() { |
| 91 return mTestCommon.needsBrowserProcessStarted(); |
| 92 } |
| 93 |
| 94 /** |
| 95 * Runs a {@link Callable} on the main thread, blocking until it is |
| 96 * complete, and returns the result. Calls |
| 97 * {@link Instrumentation#waitForIdleSync()} first to help avoid certain |
| 98 * race conditions. |
| 99 * |
| 100 * @param <R> Type of result to return |
| 101 */ |
| 102 public <R> R runTestOnUiThreadAndGetResult(Callable<R> callable) throws Exce
ption { |
| 103 return mTestCommon.runTestOnUiThreadAndGetResult(callable); |
| 104 } |
| 105 |
| 106 public void enableJavaScriptOnUiThread(final AwContents awContents) { |
| 107 mTestCommon.enableJavaScriptOnUiThread(awContents); |
| 108 } |
| 109 |
| 110 public void setNetworkAvailableOnUiThread( |
| 111 final AwContents awContents, final boolean networkUp) { |
| 112 mTestCommon.setNetworkAvailableOnUiThread(awContents, networkUp); |
| 113 } |
| 114 |
| 115 /** |
| 116 * Loads url on the UI thread and blocks until onPageFinished is called. |
| 117 */ |
| 118 public void loadUrlSync(final AwContents awContents, CallbackHelper onPageFi
nishedHelper, |
| 119 final String url) throws Exception { |
| 120 mTestCommon.loadUrlSync(awContents, onPageFinishedHelper, url); |
| 121 } |
| 122 |
| 123 public void loadUrlSync(final AwContents awContents, CallbackHelper onPageFi
nishedHelper, |
| 124 final String url, final Map<String, String> extraHeaders) throws Exc
eption { |
| 125 mTestCommon.loadUrlSync(awContents, onPageFinishedHelper, url, extraHead
ers); |
| 126 } |
| 127 |
| 128 public void loadUrlSyncAndExpectError(final AwContents awContents, |
| 129 CallbackHelper onPageFinishedHelper, CallbackHelper onReceivedErrorH
elper, |
| 130 final String url) throws Exception { |
| 131 mTestCommon.loadUrlSyncAndExpectError( |
| 132 awContents, onPageFinishedHelper, onReceivedErrorHelper, url); |
| 133 } |
| 134 |
| 135 /** |
| 136 * Loads url on the UI thread but does not block. |
| 137 */ |
| 138 public void loadUrlAsync(final AwContents awContents, final String url) thro
ws Exception { |
| 139 mTestCommon.loadUrlAsync(awContents, url); |
| 140 } |
| 141 |
| 142 public void loadUrlAsync( |
| 143 final AwContents awContents, final String url, final Map<String, Str
ing> extraHeaders) { |
| 144 mTestCommon.loadUrlAsync(awContents, url, extraHeaders); |
| 145 } |
| 146 |
| 147 /** |
| 148 * Posts url on the UI thread and blocks until onPageFinished is called. |
| 149 */ |
| 150 public void postUrlSync(final AwContents awContents, CallbackHelper onPageFi
nishedHelper, |
| 151 final String url, byte[] postData) throws Exception { |
| 152 mTestCommon.postUrlSync(awContents, onPageFinishedHelper, url, postData)
; |
| 153 } |
| 154 |
| 155 /** |
| 156 * Loads url on the UI thread but does not block. |
| 157 */ |
| 158 public void postUrlAsync(final AwContents awContents, final String url, byte
[] postData) |
| 159 throws Exception { |
| 160 mTestCommon.postUrlAsync(awContents, url, postData); |
| 161 } |
| 162 |
| 163 /** |
| 164 * Loads data on the UI thread and blocks until onPageFinished is called. |
| 165 */ |
| 166 public void loadDataSync(final AwContents awContents, CallbackHelper onPageF
inishedHelper, |
| 167 final String data, final String mimeType, final boolean isBase64Enco
ded) |
| 168 throws Exception { |
| 169 mTestCommon.loadDataSync(awContents, onPageFinishedHelper, data, mimeTyp
e, isBase64Encoded); |
| 170 } |
| 171 |
| 172 public void loadDataSyncWithCharset(final AwContents awContents, |
| 173 CallbackHelper onPageFinishedHelper, final String data, final String
mimeType, |
| 174 final boolean isBase64Encoded, final String charset) throws Exceptio
n { |
| 175 mTestCommon.loadDataSyncWithCharset( |
| 176 awContents, onPageFinishedHelper, data, mimeType, isBase64Encode
d, charset); |
| 177 } |
| 178 |
| 179 /** |
| 180 * Loads data on the UI thread but does not block. |
| 181 */ |
| 182 public void loadDataAsync(final AwContents awContents, final String data, fi
nal String mimeType, |
| 183 final boolean isBase64Encoded) throws Exception { |
| 184 mTestCommon.loadDataAsync(awContents, data, mimeType, isBase64Encoded); |
| 185 } |
| 186 |
| 187 public void loadDataWithBaseUrlSync(final AwContents awContents, |
| 188 CallbackHelper onPageFinishedHelper, final String data, final String
mimeType, |
| 189 final boolean isBase64Encoded, final String baseUrl, final String hi
storyUrl) |
| 190 throws Throwable { |
| 191 mTestCommon.loadDataWithBaseUrlSync(awContents, onPageFinishedHelper, da
ta, mimeType, |
| 192 isBase64Encoded, baseUrl, historyUrl); |
| 193 } |
| 194 |
| 195 public void loadDataWithBaseUrlAsync(final AwContents awContents, final Stri
ng data, |
| 196 final String mimeType, final boolean isBase64Encoded, final String b
aseUrl, |
| 197 final String historyUrl) throws Throwable { |
| 198 mTestCommon.loadDataWithBaseUrlAsync( |
| 199 awContents, data, mimeType, isBase64Encoded, baseUrl, historyUrl
); |
| 200 } |
| 201 |
| 202 /** |
| 203 * Reloads the current page synchronously. |
| 204 */ |
| 205 public void reloadSync(final AwContents awContents, CallbackHelper onPageFin
ishedHelper) |
| 206 throws Exception { |
| 207 mTestCommon.reloadSync(awContents, onPageFinishedHelper); |
| 208 } |
| 209 |
| 210 /** |
| 211 * Stops loading on the UI thread. |
| 212 */ |
| 213 public void stopLoading(final AwContents awContents) { |
| 214 mTestCommon.stopLoading(awContents); |
| 215 } |
| 216 |
| 217 public void waitForVisualStateCallback(final AwContents awContents) throws E
xception { |
| 218 mTestCommon.waitForVisualStateCallback(awContents); |
| 219 } |
| 220 |
| 221 public void insertVisualStateCallbackOnUIThread(final AwContents awContents, |
| 222 final long requestId, final AwContents.VisualStateCallback callback)
{ |
| 223 mTestCommon.insertVisualStateCallbackOnUIThread(awContents, requestId, c
allback); |
| 224 } |
| 225 |
| 226 // Waits for the pixel at the center of AwContents to color up into expected
Color. |
| 227 // Note that this is a stricter condition that waiting for a visual state ca
llback, |
| 228 // as visual state callback only indicates that *something* has appeared in
WebView. |
| 229 public void waitForPixelColorAtCenterOfView(final AwContents awContents, |
| 230 final AwTestContainerView testContainerView, final int expectedColor
) throws Exception { |
| 231 mTestCommon.waitForPixelColorAtCenterOfView(awContents, testContainerVie
w, expectedColor); |
| 232 } |
| 233 |
| 234 protected TestDependencyFactory createTestDependencyFactory() { |
| 235 return mTestCommon.createTestDependencyFactory(); |
| 236 } |
| 237 |
| 238 public AwTestContainerView createAwTestContainerView(final AwContentsClient
awContentsClient) { |
| 239 return mTestCommon.createAwTestContainerView(awContentsClient); |
| 240 } |
| 241 |
| 242 public AwTestContainerView createAwTestContainerView(final AwContentsClient
awContentsClient, |
| 243 boolean supportsLegacyQuirks, final TestDependencyFactory testDepend
encyFactory) { |
| 244 return mTestCommon.createAwTestContainerView( |
| 245 awContentsClient, supportsLegacyQuirks, testDependencyFactory); |
| 246 } |
| 247 |
| 248 public AwBrowserContext getAwBrowserContext() { |
| 249 return mTestCommon.getAwBrowserContext(); |
| 250 } |
| 251 |
| 252 public AwTestContainerView createDetachedAwTestContainerView( |
| 253 final AwContentsClient awContentsClient) { |
| 254 return mTestCommon.createDetachedAwTestContainerView(awContentsClient); |
| 255 } |
| 256 |
| 257 public AwTestContainerView createDetachedAwTestContainerView( |
| 258 final AwContentsClient awContentsClient, boolean supportsLegacyQuirk
s, |
| 259 TestDependencyFactory testDependencyFactory) { |
| 260 return mTestCommon.createDetachedAwTestContainerView( |
| 261 awContentsClient, supportsLegacyQuirks, testDependencyFactory); |
| 262 } |
| 263 |
| 264 protected boolean isHardwareAcceleratedTest() { |
| 265 return mTestCommon.isHardwareAcceleratedTest(); |
| 266 } |
| 267 |
| 268 public AwTestContainerView createAwTestContainerViewOnMainSync(final AwConte
ntsClient client) |
| 269 throws Exception { |
| 270 return mTestCommon.createAwTestContainerViewOnMainSync(client); |
| 271 } |
| 272 |
| 273 public AwTestContainerView createAwTestContainerViewOnMainSync( |
| 274 final AwContentsClient client, final boolean supportsLegacyQuirks) { |
| 275 return mTestCommon.createAwTestContainerViewOnMainSync(client, supportsL
egacyQuirks); |
| 276 } |
| 277 |
| 278 public AwTestContainerView createAwTestContainerViewOnMainSync(final AwConte
ntsClient client, |
| 279 final boolean supportsLegacyQuirks, final TestDependencyFactory test
DependencyFactory) { |
| 280 return mTestCommon.createAwTestContainerViewOnMainSync( |
| 281 client, supportsLegacyQuirks, testDependencyFactory); |
| 282 } |
| 283 |
| 284 public void destroyAwContentsOnMainSync(final AwContents awContents) { |
| 285 mTestCommon.destroyAwContentsOnMainSync(awContents); |
| 286 } |
| 287 |
| 288 public String getTitleOnUiThread(final AwContents awContents) throws Excepti
on { |
| 289 return mTestCommon.getTitleOnUiThread(awContents); |
| 290 } |
| 291 |
| 292 public AwSettings getAwSettingsOnUiThread(final AwContents awContents) throw
s Exception { |
| 293 return mTestCommon.getAwSettingsOnUiThread(awContents); |
| 294 } |
| 295 |
| 296 /** |
| 297 * Verify double quotes in both sides of the raw string. Strip the double qu
otes and |
| 298 * returns rest of the string. |
| 299 */ |
| 300 protected String maybeStripDoubleQuotes(String raw) { |
| 301 return mTestCommon.maybeStripDoubleQuotes(raw); |
| 302 } |
| 303 |
| 304 /** |
| 305 * Executes the given snippet of JavaScript code within the given ContentVie
w. Returns the |
| 306 * result of its execution in JSON format. |
| 307 */ |
| 308 public String executeJavaScriptAndWaitForResult(final AwContents awContents, |
| 309 TestAwContentsClient viewClient, final String code) throws Exception
{ |
| 310 return mTestCommon.executeJavaScriptAndWaitForResult(awContents, viewCli
ent, code); |
| 311 } |
| 312 |
| 313 /** |
| 314 * Executes JavaScript code within the given ContentView to get the text con
tent in |
| 315 * document body. Returns the result string without double quotes. |
| 316 */ |
| 317 public String getJavaScriptResultBodyTextContent( |
| 318 final AwContents awContents, final TestAwContentsClient viewClient)
throws Exception { |
| 319 return mTestCommon.getJavaScriptResultBodyTextContent(awContents, viewCl
ient); |
| 320 } |
| 321 |
| 322 |
| 323 /** |
| 324 * Wrapper around CriteriaHelper.pollInstrumentationThread. This uses AwTest
Base-specifc |
| 325 * timeouts and treats timeouts and exceptions as test failures automaticall
y. |
| 326 */ |
| 327 public static void pollInstrumentationThread(final Callable<Boolean> callabl
e) |
| 328 throws Exception { |
| 329 AwTestCommon.pollInstrumentationThread(callable); |
| 330 } |
| 331 |
| 332 /** |
| 333 * Wrapper around {@link AwTestBase#poll()} but runs the callable on the UI
thread. |
| 334 */ |
| 335 public void pollUiThread(final Callable<Boolean> callable) throws Exception
{ |
| 336 mTestCommon.pollUiThread(callable); |
| 337 } |
| 338 |
| 339 /** |
| 340 * Clears the resource cache. Note that the cache is per-application, so thi
s will clear the |
| 341 * cache for all WebViews used. |
| 342 */ |
| 343 public void clearCacheOnUiThread(final AwContents awContents, final boolean
includeDiskFiles) |
| 344 throws Exception { |
| 345 mTestCommon.clearCacheOnUiThread(awContents, includeDiskFiles); |
| 346 } |
| 347 |
| 348 /** |
| 349 * Returns pure page scale. |
| 350 */ |
| 351 public float getScaleOnUiThread(final AwContents awContents) throws Exceptio
n { |
| 352 return mTestCommon.getScaleOnUiThread(awContents); |
| 353 } |
| 354 |
| 355 /** |
| 356 * Returns page scale multiplied by the screen density. |
| 357 */ |
| 358 public float getPixelScaleOnUiThread(final AwContents awContents) throws Exc
eption { |
| 359 return mTestCommon.getPixelScaleOnUiThread(awContents); |
| 360 } |
| 361 |
| 362 /** |
| 363 * Returns whether a user can zoom the page in. |
| 364 */ |
| 365 public boolean canZoomInOnUiThread(final AwContents awContents) throws Excep
tion { |
| 366 return mTestCommon.canZoomInOnUiThread(awContents); |
| 367 } |
| 368 |
| 369 /** |
| 370 * Returns whether a user can zoom the page out. |
| 371 */ |
| 372 public boolean canZoomOutOnUiThread(final AwContents awContents) throws Exce
ption { |
| 373 return mTestCommon.canZoomOutOnUiThread(awContents); |
| 374 } |
| 375 |
| 376 public void killRenderProcessOnUiThreadAsync(final AwContents awContents) th
rows Exception { |
| 377 mTestCommon.killRenderProcessOnUiThreadAsync(awContents); |
| 378 } |
| 379 |
| 380 /** |
| 381 * Loads the main html then triggers the popup window. |
| 382 */ |
| 383 public void triggerPopup(final AwContents parentAwContents, |
| 384 TestAwContentsClient parentAwContentsClient, TestWebServer testWebSe
rver, |
| 385 String mainHtml, String popupHtml, String popupPath, String triggerS
cript) |
| 386 throws Exception { |
| 387 mTestCommon.triggerPopup(parentAwContents, parentAwContentsClient, testW
ebServer, mainHtml, |
| 388 popupHtml, popupPath, triggerScript); |
| 389 } |
| 390 |
| 391 /** |
| 392 * Supplies the popup window with AwContents then waits for the popup window
to finish loading. |
| 393 */ |
| 394 public PopupInfo connectPendingPopup(final AwContents parentAwContents) thro
ws Exception { |
| 395 return mTestCommon.connectPendingPopup(parentAwContents); |
| 396 } |
| 397 |
| 398 @Override |
| 399 public boolean testMethodHasAnnotation(Class<? extends Annotation> clazz) { |
| 400 return mCurrentTestDescription.getAnnotation(clazz) != null ? true : fal
se; |
| 401 } |
| 402 |
| 403 @Override |
| 404 public Instrumentation getInstrumentation() { |
| 405 return InstrumentationRegistry.getInstrumentation(); |
| 406 } |
| 407 } |
OLD | NEW |