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 9 import org.chromium.android_webview.AwContents; 10 import org.chromium.base.test.util.Feature; 11 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; 12 import org.chromium.net.test.util.TestWebServer; 13 14 import java.util.concurrent.TimeUnit; 15 16 /** 17 * Tests for the ContentViewClient.onPageFinished() method. 18 */ 19 public class ClientOnPageFinishedTest extends AwTestBase { 20 21 private TestAwContentsClient mContentsClient; 22 private AwContents mAwContents; 23 24 @Override 25 public void setUp() throws Exception { 26 super.setUp(); 27 mContentsClient = new TestAwContentsClient(); 28 final AwTestContainerView testContainerView = 29 createAwTestContainerViewOnMainSync(mContentsClient); 30 mAwContents = testContainerView.getAwContents(); 31 } 32 33 @MediumTest 34 @Feature({"AndroidWebView"}) 35 public void testOnPageFinishedPassesCorrectUrl() throws Throwable { 36 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 37 mContentsClient.getOnPageFinishedHelper(); 38 39 String html = "<html><body>Simple page.</body></html>"; 40 int currentCallCount = onPageFinishedHelper.getCallCount(); 41 loadDataAsync(mAwContents, html, "text/html", false); 42 43 onPageFinishedHelper.waitForCallback(currentCallCount); 44 assertEquals("data:text/html," + html, onPageFinishedHelper.getUrl()); 45 } 46 47 @MediumTest 48 @Feature({"AndroidWebView"}) 49 public void testOnPageFinishedCalledAfterError() throws Throwable { 50 TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = 51 mContentsClient.getOnReceivedErrorHelper(); 52 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 53 mContentsClient.getOnPageFinishedHelper(); 54 55 assertEquals(0, onReceivedErrorHelper.getCallCount()); 56 57 String url = "http://localhost:7/non_existent"; 58 int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); 59 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount(); 60 loadUrlAsync(mAwContents, url); 61 62 onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount, 63 1 /* numberOfCallsToWaitFor */, 64 WAIT_TIMEOUT_SECONDS, 65 TimeUnit.SECONDS); 66 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount, 67 1 /* numberOfCallsToWaitFor */, 68 WAIT_TIMEOUT_SECONDS, 69 TimeUnit.SECONDS); 70 assertEquals(1, onReceivedErrorHelper.getCallCount()); 71 } 72 73 @MediumTest 74 @Feature({"AndroidWebView"}) 75 public void testOnPageFinishedNotCalledForValidSubresources() throws Throwable { 76 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = 77 mContentsClient.getOnPageFinishedHelper(); 78 79 TestWebServer webServer = null; 80 try { 81 webServer = new TestWebServer(false); 82 83 final String testHtml = "<html><head>Header</head><body>Body</body></html>"; 84 final String testPath = "/test.html"; 85 final String syncPath = "/sync.html"; 86 87 webServer.setResponse(testPath, testHtml, null); 88 final String syncUrl = webServer.setResponse(syncPath, testHtml, null); 89 90 assertEquals(0, onPageFinishedHelper.getCallCount()); 91 final int pageWithSubresourcesCallCount = onPageFinishedHelper.getCallCount(); 92 loadDataAsync(mAwContents, 93 "<html><iframe src=\"" + testPath + "\" /></html>", 94 "text/html", 95 false); 96 97 onPageFinishedHelper.waitForCallback(pageWithSubresourcesCallCount); 98 99 // Rather than wait a fixed time to see that an onPageFinished callback isn't issued 100 // we load another valid page. Since callbacks arrive sequentially if the next callback 101 // we get is for the synchronizationUrl we know that the previous load did not schedule 102 // a callback for the iframe. 103 final int synchronizationPageCallCount = onPageFinishedHelper.getCallCount(); 104 loadUrlAsync(mAwContents, syncUrl); 105 106 onPageFinishedHelper.waitForCallback(synchronizationPageCallCount); 107 assertEquals(syncUrl, onPageFinishedHelper.getUrl()); 108 assertEquals(2, onPageFinishedHelper.getCallCount()); 109 110 } finally { 111 if (webServer != null) webServer.shutdown(); 112 } 113 } 114 } 115