1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package org.chromium.android_webview.test;
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import android.test.suitebuilder.annotation.MediumTest;
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)import android.webkit.WebSettings;
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.chromium.android_webview.AwContents;
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.chromium.android_webview.ErrorCodeConversionHelper;
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.chromium.base.test.util.Feature;
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import java.util.concurrent.TimeUnit;
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Tests for the ContentViewClient.onReceivedError() method.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public class ClientOnReceivedErrorTest extends AwTestBase {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private TestAwContentsClient mContentsClient;
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    private AwContents mAwContents;
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Override
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public void setUp() throws Exception {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        super.setUp();
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mContentsClient = new TestAwContentsClient();
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        final AwTestContainerView testContainerView =
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                createAwTestContainerViewOnMainSync(mContentsClient);
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        mAwContents = testContainerView.getAwContents();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @MediumTest
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Feature({"AndroidWebView"})
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public void testOnReceivedErrorOnInvalidUrl() throws Throwable {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mContentsClient.getOnReceivedErrorHelper();
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        String url = "http://man.id.be.really.surprised.if.this.address.existed.blah/";
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        loadUrlAsync(mAwContents, url);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount,
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                              1 /* numberOfCallsToWaitFor */,
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              WAIT_TIMEOUT_MS,
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              TimeUnit.MILLISECONDS);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertEquals(ErrorCodeConversionHelper.ERROR_HOST_LOOKUP,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                onReceivedErrorHelper.getErrorCode());
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertEquals(url, onReceivedErrorHelper.getFailingUrl());
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertNotNull(onReceivedErrorHelper.getDescription());
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @MediumTest
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Feature({"AndroidWebView"})
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public void testOnReceivedErrorOnInvalidScheme() throws Throwable {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mContentsClient.getOnReceivedErrorHelper();
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        String url = "foo://some/resource";
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        loadUrlAsync(mAwContents, url);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertEquals(ErrorCodeConversionHelper.ERROR_UNSUPPORTED_SCHEME,
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                onReceivedErrorHelper.getErrorCode());
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertEquals(url, onReceivedErrorHelper.getFailingUrl());
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertNotNull(onReceivedErrorHelper.getDescription());
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @MediumTest
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Feature({"AndroidWebView"})
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public void testNoErrorOnFailedSubresourceLoad() throws Throwable {
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mContentsClient.getOnReceivedErrorHelper();
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper =
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mContentsClient.getOnPageFinishedHelper();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        int currentCallCount = onPageFinishedHelper.getCallCount();
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        loadDataAsync(mAwContents,
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      "<html><iframe src=\"http//invalid.url.co/\" /></html>",
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      "text/html",
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      false);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        onPageFinishedHelper.waitForCallback(currentCallCount);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        assertEquals(0, onReceivedErrorHelper.getCallCount());
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @MediumTest
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Feature({"AndroidWebView"})
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public void testNonExistentAssetUrl() throws Throwable {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mContentsClient.getOnReceivedErrorHelper();
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        final String url = "file:///android_asset/does_not_exist.html";
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        loadUrlAsync(mAwContents, url);
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount);
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN,
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     onReceivedErrorHelper.getErrorCode());
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assertEquals(url, onReceivedErrorHelper.getFailingUrl());
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assertNotNull(onReceivedErrorHelper.getDescription());
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @MediumTest
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    @Feature({"AndroidWebView"})
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    public void testNonExistentResourceUrl() throws Throwable {
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                mContentsClient.getOnReceivedErrorHelper();
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        final String url = "file:///android_res/raw/does_not_exist.html";
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        loadUrlAsync(mAwContents, url);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount);
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN,
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     onReceivedErrorHelper.getErrorCode());
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assertEquals(url, onReceivedErrorHelper.getFailingUrl());
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        assertNotNull(onReceivedErrorHelper.getDescription());
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
120868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
121868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    @MediumTest
122868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    @Feature({"AndroidWebView"})
123868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    public void testCacheMiss() throws Throwable {
124868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper =
125868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                mContentsClient.getOnReceivedErrorHelper();
126868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        final String url = "http://example.com/index.html";
127868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount();
128868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        getAwSettingsOnUiThread(mAwContents).setCacheMode(WebSettings.LOAD_CACHE_ONLY);
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        loadUrlAsync(mAwContents, url);
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount);
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        assertEquals(ErrorCodeConversionHelper.ERROR_UNKNOWN,
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                     onReceivedErrorHelper.getErrorCode());
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        assertEquals(url, onReceivedErrorHelper.getFailingUrl());
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        assertFalse(onReceivedErrorHelper.getDescription().isEmpty());
136868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
138