| 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.android_webview.test; | 5 package org.chromium.android_webview.test; |
| 6 | 6 |
| 7 import android.test.suitebuilder.annotation.LargeTest; | |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
| 9 import android.util.Pair; | 8 import android.util.Pair; |
| 10 | 9 |
| 11 import junit.framework.Assert; | |
| 12 | |
| 13 import org.apache.http.Header; | 10 import org.apache.http.Header; |
| 14 import org.apache.http.HttpRequest; | 11 import org.apache.http.HttpRequest; |
| 15 import org.chromium.android_webview.AwContents; | 12 import org.chromium.android_webview.AwContents; |
| 16 import org.chromium.android_webview.AwSettings; | 13 import org.chromium.android_webview.AwSettings; |
| 17 import org.chromium.android_webview.test.util.CommonResources; | 14 import org.chromium.android_webview.test.util.CommonResources; |
| 18 import org.chromium.android_webview.test.util.JSUtils; | 15 import org.chromium.android_webview.test.util.JSUtils; |
| 19 import org.chromium.base.test.util.Feature; | 16 import org.chromium.base.test.util.Feature; |
| 20 import org.chromium.content.browser.LoadUrlParams; | 17 import org.chromium.content.browser.LoadUrlParams; |
| 21 import org.chromium.content.browser.test.util.CallbackHelper; | 18 import org.chromium.content.browser.test.util.CallbackHelper; |
| 22 import org.chromium.content.browser.test.util.HistoryUtils; | 19 import org.chromium.content.browser.test.util.HistoryUtils; |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 324 |
| 328 HistoryUtils.goBackSync(getInstrumentation(), | 325 HistoryUtils.goBackSync(getInstrumentation(), |
| 329 awContents.getContentViewCore(), | 326 awContents.getContentViewCore(), |
| 330 contentsClient.getOnPageFinishedHelper()); | 327 contentsClient.getOnPageFinishedHelper()); |
| 331 assertEquals(2, webServer.getRequestCount(path)); | 328 assertEquals(2, webServer.getRequestCount(path)); |
| 332 validateRequestHeaders(extraHeaders, webServer.getLastRequest(path))
; | 329 validateRequestHeaders(extraHeaders, webServer.getLastRequest(path))
; |
| 333 } finally { | 330 } finally { |
| 334 if (webServer != null) webServer.shutdown(); | 331 if (webServer != null) webServer.shutdown(); |
| 335 } | 332 } |
| 336 } | 333 } |
| 337 | |
| 338 private static class TestController { | |
| 339 private final Object mLock = new Object(); | |
| 340 private boolean mIsReady = false; | |
| 341 public void notifyPageIsReady() { | |
| 342 synchronized (mLock) { | |
| 343 mIsReady = true; | |
| 344 mLock.notify(); | |
| 345 } | |
| 346 } | |
| 347 public void waitUntilIsReady() { | |
| 348 synchronized (mLock) { | |
| 349 while (!mIsReady) { | |
| 350 try { | |
| 351 mLock.wait(WAIT_TIMEOUT_MS); | |
| 352 } catch (Exception e) { | |
| 353 continue; | |
| 354 } | |
| 355 if (!mIsReady) { | |
| 356 Assert.fail("Wait timed out"); | |
| 357 } | |
| 358 } | |
| 359 mIsReady = false; | |
| 360 } | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 // Verify that it is possible to interrupt JS scripts stuck in an infinite l
oop | |
| 365 // by calling loadUrl on a WebView. | |
| 366 @LargeTest | |
| 367 @Feature({"AndroidWebView"}) | |
| 368 public void testLoadUrlInterruptsLoopedScripts() throws Throwable { | |
| 369 final String infiniteLoopPage = | |
| 370 "<html><head>" + | |
| 371 " <script>" + | |
| 372 " function infiniteLoop() {" + | |
| 373 " test.notifyPageIsReady();" + | |
| 374 " while(1);" + | |
| 375 " }" + | |
| 376 " </script>" + | |
| 377 "</head><body onload='setTimeout(infiniteLoop, 0)'>" + | |
| 378 "</body></html>"; | |
| 379 final String simplePage = "<html><body onload='test.notifyPageIsReady()'
></body></html>"; | |
| 380 final String expectedTitle = "PASS"; | |
| 381 final String pageWithTitle = "<html><body onload='document.title=\"" + e
xpectedTitle + | |
| 382 "\"; test.notifyPageIsReady()'></body></html>"; | |
| 383 | |
| 384 final AwTestContainerView testContainerView = | |
| 385 createAwTestContainerViewOnMainSync(new TestAwContentsClient()); | |
| 386 final AwContents awContents = testContainerView.getAwContents(); | |
| 387 getAwSettingsOnUiThread(awContents).setJavaScriptEnabled(true); | |
| 388 final TestController testController = new TestController(); | |
| 389 getInstrumentation().runOnMainSync(new Runnable() { | |
| 390 @Override | |
| 391 public void run() { | |
| 392 awContents.addPossiblyUnsafeJavascriptInterface(testController,
"test", null); | |
| 393 } | |
| 394 }); | |
| 395 loadDataAsync(awContents, infiniteLoopPage, "text/html", false); | |
| 396 testController.waitUntilIsReady(); | |
| 397 loadDataAsync(awContents, simplePage, "text/html", false); | |
| 398 testController.waitUntilIsReady(); | |
| 399 // Load another page that runs JS to make sure that the WebView is still
functional. | |
| 400 loadDataAsync(awContents, pageWithTitle, "text/html", false); | |
| 401 testController.waitUntilIsReady(); | |
| 402 assertEquals(expectedTitle, getTitleOnUiThread(awContents)); | |
| 403 } | |
| 404 } | 334 } |
| OLD | NEW |