| 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.test.suitebuilder.annotation.MediumTest; |
| 8 | import android.webkit.WebSettings; |
| 9 | |
| 10 | import org.chromium.android_webview.AwContents; |
| 11 | import org.chromium.android_webview.AndroidProtocolHandler; |
| 12 | import org.chromium.android_webview.ErrorCodeConversionHelper; |
| 13 | import org.chromium.base.test.util.Feature; |
| 14 | import org.chromium.content.browser.ContentViewCore; |
| 15 | import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 16 | |
| 17 | import java.util.concurrent.TimeUnit; |
| 18 | |
| 19 | /** |
| 20 | * Tests for the ContentViewClient.onReceivedError() method. |
| 21 | */ |
| 22 | public class ClientOnReceivedErrorTest extends AwTestBase { |
| 23 | |
| 24 | private TestAwContentsClient mContentsClient; |
| 25 | private AwContents mAwContents; |
| 26 | |
| 27 | @Override |
| 28 | public void setUp() throws Exception { |
| 29 | super.setUp(); |
| 30 | mContentsClient = new TestAwContentsClient(); |
| 31 | final AwTestContainerView testContainerView = |
| 32 | createAwTestContainerViewOnMainSync(mContentsClient); |
| 33 | mAwContents = testContainerView.getAwContents(); |
| 34 | } |
| 35 | |
| 36 | @MediumTest |
| 37 | @Feature({"AndroidWebView"}) |
| 38 | public void testOnReceivedErrorOnInvalidUrl() throws Throwable { |
| 39 | TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 40 | mContentsClient.getOnReceivedErrorHelper(); |
| 41 | |
| 42 | String url = "http://man.id.be.really.surprised.if.this.address.existed.blah/"; |
| 43 | int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 44 | loadUrlAsync(mAwContents, url); |
| 45 | |
| 46 | onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount, |
| 47 | 1 /* numberOfCallsToWaitFor */, |
| 48 | WAIT_TIMEOUT_SECONDS, |
| 49 | TimeUnit.SECONDS); |
| 50 | assertEquals(ErrorCodeConversionHelper.ERROR_HOST_LOOKUP, |
| 51 | onReceivedErrorHelper.getErrorCode()); |
| 52 | assertEquals(url, onReceivedErrorHelper.getFailingUrl()); |
| 53 | assertNotNull(onReceivedErrorHelper.getDescription()); |
| 54 | } |
| 55 | |
| 56 | @MediumTest |
| 57 | @Feature({"AndroidWebView"}) |
| 58 | public void testOnReceivedErrorOnInvalidScheme() throws Throwable { |
| 59 | TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 60 | mContentsClient.getOnReceivedErrorHelper(); |
| 61 | |
| 62 | String url = "foo://some/resource"; |
| 63 | int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 64 | loadUrlAsync(mAwContents, url); |
| 65 | |
| 66 | onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount); |
| 67 | assertEquals(ErrorCodeConversionHelper.ERROR_UNSUPPORTED_SCHEME, |
| 68 | onReceivedErrorHelper.getErrorCode()); |
| 69 | assertEquals(url, onReceivedErrorHelper.getFailingUrl()); |
| 70 | assertNotNull(onReceivedErrorHelper.getDescription()); |
| 71 | } |
| 72 | |
| 73 | @MediumTest |
| 74 | @Feature({"AndroidWebView"}) |
| 75 | public void testNoErrorOnFailedSubresourceLoad() throws Throwable { |
| 76 | TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 77 | mContentsClient.getOnReceivedErrorHelper(); |
| 78 | TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
| 79 | mContentsClient.getOnPageFinishedHelper(); |
| 80 | |
| 81 | int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 82 | loadDataAsync(mAwContents, |
| 83 | "<html><iframe src=\"http//invalid.url.co/\" /></html>", |
| 84 | "text/html", |
| 85 | false); |
| 86 | |
| 87 | onPageFinishedHelper.waitForCallback(currentCallCount); |
| 88 | assertEquals(0, onReceivedErrorHelper.getCallCount()); |
| 89 | } |
| 90 | |
| 91 | @MediumTest |
| 92 | @Feature({"AndroidWebView"}) |
| 93 | public void testNonExistentAssetUrl() throws Throwable { |
| 94 | TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 95 | mContentsClient.getOnReceivedErrorHelper(); |
| 96 | final String url = "file:///android_asset/does_not_exist.html"; |
| 97 | int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 98 | loadUrlAsync(mAwContents, url); |
| 99 | |
| 100 | onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount); |
| 101 | assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, |
| 102 | onReceivedErrorHelper.getErrorCode()); |
| 103 | assertEquals(url, onReceivedErrorHelper.getFailingUrl()); |
| 104 | assertNotNull(onReceivedErrorHelper.getDescription()); |
| 105 | } |
| 106 | |
| 107 | @MediumTest |
| 108 | @Feature({"AndroidWebView"}) |
| 109 | public void testNonExistentResourceUrl() throws Throwable { |
| 110 | TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 111 | mContentsClient.getOnReceivedErrorHelper(); |
| 112 | final String url = "file:///android_res/raw/does_not_exist.html"; |
| 113 | int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 114 | loadUrlAsync(mAwContents, url); |
| 115 | |
| 116 | onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount); |
| 117 | assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, |
| 118 | onReceivedErrorHelper.getErrorCode()); |
| 119 | assertEquals(url, onReceivedErrorHelper.getFailingUrl()); |
| 120 | assertNotNull(onReceivedErrorHelper.getDescription()); |
| 121 | } |
| 122 | |
| 123 | @MediumTest |
| 124 | @Feature({"AndroidWebView"}) |
| 125 | public void testCacheMiss() throws Throwable { |
| 126 | TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 127 | mContentsClient.getOnReceivedErrorHelper(); |
| 128 | final String url = "http://example.com/index.html"; |
| 129 | int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 130 | getAwSettingsOnUiThread(mAwContents).setCacheMode(WebSettings.LOAD_CACHE_ONLY); |
| 131 | loadUrlAsync(mAwContents, url); |
| 132 | |
| 133 | onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount); |
| 134 | assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN, |
| 135 | onReceivedErrorHelper.getErrorCode()); |
| 136 | assertEquals(url, onReceivedErrorHelper.getFailingUrl()); |
| 137 | assertFalse(onReceivedErrorHelper.getDescription().isEmpty()); |
| 138 | } |
| 139 | } |