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.SmallTest; |
8 | |
9 | import org.apache.http.Header; |
10 | import org.apache.http.HttpRequest; |
11 | |
12 | import org.chromium.android_webview.AwContents; |
13 | import org.chromium.base.test.util.Feature; |
14 | import org.chromium.content.browser.LoadUrlParams; |
15 | import org.chromium.content.browser.test.util.CallbackHelper; |
16 | import org.chromium.net.test.util.TestWebServer; |
17 | |
18 | import java.util.concurrent.TimeUnit; |
19 | import java.util.HashMap; |
20 | import java.util.Map; |
21 | |
22 | /** |
23 | * Test suite for loadUrl(). |
24 | */ |
25 | public class LoadUrlTest extends AwTestBase { |
26 | @SmallTest |
27 | @Feature({"AndroidWebView"}) |
28 | public void testDataUrl() throws Throwable { |
29 | final String expectedTitle = "dataUrlTest"; |
30 | final String data = |
31 | "<html><head><title>" + expectedTitle + "</title></head><body>foo</body></html>"; |
32 | |
33 | final TestAwContentsClient contentsClient = new TestAwContentsClient(); |
34 | final AwTestContainerView testContainerView = |
35 | createAwTestContainerViewOnMainSync(contentsClient); |
36 | final AwContents awContents = testContainerView.getAwContents(); |
37 | loadDataSync(awContents, contentsClient.getOnPageFinishedHelper(), data, |
38 | "text/html", false); |
39 | assertEquals(expectedTitle, getTitleOnUiThread(awContents)); |
40 | } |
41 | |
42 | @SmallTest |
43 | @Feature({"AndroidWebView"}) |
44 | public void testDataUrlBase64() throws Throwable { |
45 | final String expectedTitle = "dataUrlTestBase64"; |
46 | final String data = "PGh0bWw+PGhlYWQ+PHRpdGxlPmRhdGFVcmxUZXN0QmFzZTY0PC90aXRsZT48" + |
47 | "L2hlYWQ+PC9odG1sPg=="; |
48 | |
49 | final TestAwContentsClient contentsClient = new TestAwContentsClient(); |
50 | final AwTestContainerView testContainerView = |
51 | createAwTestContainerViewOnMainSync(contentsClient); |
52 | final AwContents awContents = testContainerView.getAwContents(); |
53 | loadDataSync(awContents, contentsClient.getOnPageFinishedHelper(), data, |
54 | "text/html", true); |
55 | assertEquals(expectedTitle, getTitleOnUiThread(awContents)); |
56 | } |
57 | |
58 | @SmallTest |
59 | @Feature({"AndroidWebView"}) |
60 | public void testDataUrlCharset() throws Throwable { |
61 | // Note that the \u00a3 (pound sterling) is the important character in the following |
62 | // string as it's not in the US_ASCII character set. |
63 | final String expectedTitle = "You win \u00a3100!"; |
64 | final String data = |
65 | "<html><head><title>" + expectedTitle + "</title></head><body>foo</body></html>"; |
66 | final TestAwContentsClient contentsClient = new TestAwContentsClient(); |
67 | final AwTestContainerView testContainerView = |
68 | createAwTestContainerViewOnMainSync(contentsClient); |
69 | final AwContents awContents = testContainerView.getAwContents(); |
70 | loadDataSyncWithCharset(awContents, contentsClient.getOnPageFinishedHelper(), data, |
71 | "text/html", false, "UTF-8"); |
72 | assertEquals(expectedTitle, getTitleOnUiThread(awContents)); |
73 | } |
74 | |
75 | /** |
76 | * Loads url on the UI thread and blocks until onPageFinished is called. |
77 | */ |
78 | protected void loadUrlWithExtraHeadersSync( |
79 | final AwContents awContents, |
80 | CallbackHelper onPageFinishedHelper, |
81 | final String url, |
82 | final Map<String, String> extraHeaders) throws Throwable { |
83 | int currentCallCount = onPageFinishedHelper.getCallCount(); |
84 | runTestOnUiThread(new Runnable() { |
85 | @Override |
86 | public void run() { |
87 | LoadUrlParams params = new LoadUrlParams(url); |
88 | params.setExtraHeaders(extraHeaders); |
89 | awContents.loadUrl(params); |
90 | } |
91 | }); |
92 | onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_SECONDS, |
93 | TimeUnit.SECONDS); |
94 | } |
95 | |
96 | @SmallTest |
97 | @Feature({"AndroidWebView"}) |
98 | public void testLoadUrlWithExtraHeaders() throws Throwable { |
99 | final TestAwContentsClient contentsClient = new TestAwContentsClient(); |
100 | final AwTestContainerView testContainerView = |
101 | createAwTestContainerViewOnMainSync(contentsClient); |
102 | final AwContents awContents = testContainerView.getAwContents(); |
103 | |
104 | TestWebServer webServer = null; |
105 | try { |
106 | webServer = new TestWebServer(false); |
107 | final String path = "/load_url_with_extra_headers_test.html"; |
108 | final String url = webServer.setResponse(path, "<html><body>foo</body></html>", null); |
109 | |
110 | String[] headerNames = {"X-ExtraHeaders1", "x-extraHeaders2"}; |
111 | String[] headerValues = {"extra-header-data1", "EXTRA-HEADER-DATA2"}; |
112 | Map<String, String> extraHeaders = new HashMap<String, String>(); |
113 | for (int i = 0; i < headerNames.length; ++i) |
114 | extraHeaders.put(headerNames[i], headerValues[i]); |
115 | |
116 | loadUrlWithExtraHeadersSync(awContents, |
117 | contentsClient.getOnPageFinishedHelper(), |
118 | url, |
119 | extraHeaders); |
120 | |
121 | HttpRequest request = webServer.getLastRequest(path); |
122 | for (int i = 0; i < headerNames.length; ++i) { |
123 | Header[] matchingHeaders = request.getHeaders(headerNames[i]); |
124 | assertEquals(1, matchingHeaders.length); |
125 | |
126 | Header header = matchingHeaders[0]; |
127 | assertEquals(headerNames[i].toLowerCase(), header.getName()); |
128 | assertEquals(headerValues[i], header.getValue()); |
129 | } |
130 | } finally { |
131 | if (webServer != null) webServer.shutdown(); |
132 | } |
133 | } |
134 | } |