Chromium Code Reviews| 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.MediumTest; | 7 import android.test.suitebuilder.annotation.MediumTest; |
| 8 | 8 |
| 9 import org.chromium.android_webview.AwContents; | 9 import org.chromium.android_webview.AwContents; |
| 10 import org.chromium.android_webview.test.util.CommonResources; | 10 import org.chromium.android_webview.test.util.CommonResources; |
| 11 import org.chromium.android_webview.test.util.JSUtils; | 11 import org.chromium.android_webview.test.util.JSUtils; |
| 12 import org.chromium.base.test.util.Feature; | 12 import org.chromium.base.test.util.Feature; |
| 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; | 13 import org.chromium.content.browser.test.util.TestCallbackHelperContainer; |
| 14 import org.chromium.net.test.util.TestWebServer; | 14 import org.chromium.net.test.util.TestWebServer; |
| 15 | 15 |
| 16 import java.util.concurrent.TimeUnit; | |
| 17 | |
| 18 /** | 16 /** |
| 19 * Tests for the ContentViewClient.onPageFinished() method. | 17 * Tests for the ContentViewClient.onPageFinished() method. |
| 20 */ | 18 */ |
| 21 public class ClientOnPageFinishedTest extends AwTestBase { | 19 public class ClientOnPageFinishedTest extends AwTestBase { |
| 22 | 20 |
| 23 private TestAwContentsClient mContentsClient; | 21 private TestAwContentsClient mContentsClient; |
| 24 private AwContents mAwContents; | 22 private AwContents mAwContents; |
| 25 | 23 |
| 26 @Override | 24 @Override |
| 27 public void setUp() throws Exception { | 25 public void setUp() throws Exception { |
| 28 super.setUp(); | 26 super.setUp(); |
| 29 mContentsClient = new TestAwContentsClient(); | 27 setTestAwContentsClient(new TestAwContentsClient()); |
| 28 } | |
| 29 | |
| 30 private void setTestAwContentsClient(TestAwContentsClient contentsClient) th rows Exception { | |
| 31 mContentsClient = contentsClient; | |
| 30 final AwTestContainerView testContainerView = | 32 final AwTestContainerView testContainerView = |
| 31 createAwTestContainerViewOnMainSync(mContentsClient); | 33 createAwTestContainerViewOnMainSync(mContentsClient); |
| 32 mAwContents = testContainerView.getAwContents(); | 34 mAwContents = testContainerView.getAwContents(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 @MediumTest | 37 @MediumTest |
| 36 @Feature({"AndroidWebView"}) | 38 @Feature({"AndroidWebView"}) |
| 37 public void testOnPageFinishedPassesCorrectUrl() throws Throwable { | 39 public void testOnPageFinishedPassesCorrectUrl() throws Throwable { |
| 38 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 40 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
| 39 mContentsClient.getOnPageFinishedHelper(); | 41 mContentsClient.getOnPageFinishedHelper(); |
| 40 | 42 |
| 41 String html = "<html><body>Simple page.</body></html>"; | 43 String html = "<html><body>Simple page.</body></html>"; |
| 42 int currentCallCount = onPageFinishedHelper.getCallCount(); | 44 int currentCallCount = onPageFinishedHelper.getCallCount(); |
| 43 loadDataAsync(mAwContents, html, "text/html", false); | 45 loadDataAsync(mAwContents, html, "text/html", false); |
| 44 | 46 |
| 45 onPageFinishedHelper.waitForCallback(currentCallCount); | 47 onPageFinishedHelper.waitForCallback(currentCallCount); |
| 46 assertEquals("data:text/html," + html, onPageFinishedHelper.getUrl()); | 48 assertEquals("data:text/html," + html, onPageFinishedHelper.getUrl()); |
| 47 } | 49 } |
| 48 | 50 |
| 49 @MediumTest | 51 @MediumTest |
| 50 @Feature({"AndroidWebView"}) | 52 @Feature({"AndroidWebView"}) |
| 51 public void testOnPageFinishedCalledAfterError() throws Throwable { | 53 public void testOnPageFinishedCalledAfterError() throws Throwable { |
| 54 setTestAwContentsClient(new TestAwContentsClient() { | |
| 55 private boolean isOnReceivedErrorCalled = false; | |
| 56 private boolean isOnPageFinishedCalled = false; | |
| 57 | |
| 58 @Override | |
| 59 public void onReceivedError(int errorCode, String description, Strin g failingUrl) { | |
| 60 isOnReceivedErrorCalled = true; | |
| 61 // Make sure onReceivedError is called before onPageFinished | |
| 62 assertEquals(false, isOnPageFinishedCalled); | |
| 63 super.onReceivedError(errorCode, description, failingUrl); | |
| 64 } | |
| 65 | |
| 66 @Override | |
| 67 public void onPageFinished(String url) { | |
| 68 isOnPageFinishedCalled = true; | |
| 69 super.onPageFinished(url); | |
|
mkosiba (inactive)
2014/02/13 17:21:50
maybe assert onReceivedError was called?
| |
| 70 } | |
| 71 }); | |
| 72 | |
| 52 TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = | 73 TestCallbackHelperContainer.OnReceivedErrorHelper onReceivedErrorHelper = |
| 53 mContentsClient.getOnReceivedErrorHelper(); | 74 mContentsClient.getOnReceivedErrorHelper(); |
| 54 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 75 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
| 55 mContentsClient.getOnPageFinishedHelper(); | 76 mContentsClient.getOnPageFinishedHelper(); |
| 56 | 77 |
| 57 assertEquals(0, onReceivedErrorHelper.getCallCount()); | 78 assertEquals(0, onReceivedErrorHelper.getCallCount()); |
|
mkosiba (inactive)
2014/02/13 17:21:50
I don't think this is needed.
| |
| 58 | 79 |
| 59 String url = "http://localhost:7/non_existent"; | 80 String invalidUrl = "http://localhost:7/non_existent"; |
| 60 int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); | 81 int onReceivedErrorCallCount = onReceivedErrorHelper.getCallCount(); |
| 61 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount(); | 82 int onPageFinishedCallCount = onPageFinishedHelper.getCallCount(); |
| 62 loadUrlAsync(mAwContents, url); | 83 loadUrlSync(mAwContents, onPageFinishedHelper, invalidUrl); |
| 63 | 84 |
| 64 onReceivedErrorHelper.waitForCallback(onReceivedErrorCallCount, | 85 assertEquals(invalidUrl, onReceivedErrorHelper.getFailingUrl()); |
| 65 1 /* numberOfCallsToWaitFor */, | 86 assertEquals(invalidUrl, onPageFinishedHelper.getUrl()); |
| 66 WAIT_TIMEOUT_MS, | |
| 67 TimeUnit.MILLISECONDS); | |
| 68 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount, | |
| 69 1 /* numberOfCallsToWaitFor */, | |
| 70 WAIT_TIMEOUT_MS, | |
| 71 TimeUnit.MILLISECONDS); | |
| 72 assertEquals(1, onReceivedErrorHelper.getCallCount()); | |
| 73 } | 87 } |
| 74 | 88 |
| 75 @MediumTest | 89 @MediumTest |
| 90 @Feature({"AndroidWebView"}) | |
| 91 public void testOnPageFinishedCalledAfterRedirectedUrlIsOverridden() throws Throwable { | |
| 92 /* | |
| 93 * If url1 is redirected url2, and url2 load is overridden, onPageFinish ed should still be | |
| 94 * called for url2. | |
| 95 * Steps: | |
| 96 * 1. load url1. url1 onPageStarted | |
| 97 * 2. server redirects url1 to url2. url2 onPageStarted | |
| 98 * 3. shouldOverridedUrlLoading called for url2 and returns true | |
| 99 * 4. url2 onPageFinishedCalled | |
| 100 */ | |
| 101 | |
| 102 TestWebServer webServer = null; | |
| 103 try { | |
| 104 webServer = new TestWebServer(false); | |
| 105 final String redirectTargetPath = "/redirect_target.html"; | |
| 106 final String redirectTargetUrl = webServer.setResponse(redirectTarge tPath, | |
| 107 "<html><body>hello world</body></html>", null); | |
| 108 final String redirectUrl = webServer.setRedirect("/302.html", redire ctTargetUrl); | |
| 109 | |
| 110 final TestAwContentsClient.ShouldOverrideUrlLoadingHelper urlOverrid eHelper = | |
| 111 mContentsClient.getShouldOverrideUrlLoadingHelper(); | |
| 112 // Override the load of redirectTargetUrl | |
| 113 urlOverrideHelper.setShouldOverrideUrlLoadingReturnValue(true); | |
| 114 | |
| 115 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelpe r = | |
| 116 mContentsClient.getOnPageFinishedHelper(); | |
| 117 | |
| 118 final int currentOnPageFinishedCallCount = onPageFinishedHelper.getC allCount(); | |
| 119 loadUrlAsync(mAwContents, redirectUrl); | |
| 120 | |
| 121 onPageFinishedHelper.waitForCallback(currentOnPageFinishedCallCount) ; | |
| 122 // onPageFinished needs to be called for redirectTargetUrl, but not for redirectUrl | |
| 123 assertEquals(redirectTargetUrl, onPageFinishedHelper.getUrl()); | |
| 124 } finally { | |
| 125 if (webServer != null) webServer.shutdown(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 @MediumTest | |
| 76 @Feature({"AndroidWebView"}) | 130 @Feature({"AndroidWebView"}) |
| 77 public void testOnPageFinishedNotCalledForValidSubresources() throws Throwab le { | 131 public void testOnPageFinishedNotCalledForValidSubresources() throws Throwab le { |
| 78 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = | 132 TestCallbackHelperContainer.OnPageFinishedHelper onPageFinishedHelper = |
| 79 mContentsClient.getOnPageFinishedHelper(); | 133 mContentsClient.getOnPageFinishedHelper(); |
| 80 | 134 |
| 81 TestWebServer webServer = null; | 135 TestWebServer webServer = null; |
| 82 try { | 136 try { |
| 83 webServer = new TestWebServer(false); | 137 webServer = new TestWebServer(false); |
| 84 | 138 |
| 85 final String testHtml = "<html><head>Header</head><body>Body</body>< /html>"; | 139 final String testHtml = "<html><head>Header</head><body>Body</body>< /html>"; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, | 260 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, |
| 207 "window.history.go(-1)"); | 261 "window.history.go(-1)"); |
| 208 | 262 |
| 209 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount); | 263 onPageFinishedHelper.waitForCallback(onPageFinishedCallCount); |
| 210 assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCoun t()); | 264 assertEquals(onPageStartedCallCount, onPageStartedHelper.getCallCoun t()); |
| 211 } finally { | 265 } finally { |
| 212 if (webServer != null) webServer.shutdown(); | 266 if (webServer != null) webServer.shutdown(); |
| 213 } | 267 } |
| 214 } | 268 } |
| 215 } | 269 } |
| OLD | NEW |