OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.BitmapFactory; | 8 import android.graphics.BitmapFactory; |
9 import android.os.Handler; | 9 import android.os.Handler; |
10 import android.os.Looper; | 10 import android.os.Looper; |
11 import android.os.Message; | 11 import android.os.Message; |
12 import android.test.UiThreadTest; | 12 import android.test.UiThreadTest; |
13 import android.test.suitebuilder.annotation.SmallTest; | 13 import android.test.suitebuilder.annotation.SmallTest; |
14 import android.util.Pair; | 14 import android.util.Pair; |
15 | 15 |
16 import org.chromium.android_webview.AwContents; | 16 import org.chromium.android_webview.AwContents; |
17 import org.chromium.android_webview.test.util.CommonResources; | 17 import org.chromium.android_webview.test.util.CommonResources; |
18 import org.chromium.android_webview.test.util.TestWebServer; | 18 import org.chromium.android_webview.test.util.TestWebServer; |
19 import org.chromium.base.test.util.DisabledTest; | 19 import org.chromium.base.test.util.DisabledTest; |
20 import org.chromium.base.test.util.Feature; | 20 import org.chromium.base.test.util.Feature; |
21 import org.chromium.base.test.util.UrlUtils; | 21 import org.chromium.base.test.util.UrlUtils; |
22 import org.chromium.content.browser.ContentViewCore; | |
23 import org.chromium.content.browser.test.util.CallbackHelper; | 22 import org.chromium.content.browser.test.util.CallbackHelper; |
24 import org.chromium.content.browser.test.util.Criteria; | 23 import org.chromium.content.browser.test.util.Criteria; |
25 import org.chromium.content.browser.test.util.CriteriaHelper; | 24 import org.chromium.content.browser.test.util.CriteriaHelper; |
26 | 25 |
27 import java.io.InputStream; | 26 import java.io.InputStream; |
28 import java.net.URL; | 27 import java.net.URL; |
29 import java.util.ArrayList; | 28 import java.util.ArrayList; |
30 import java.util.concurrent.Semaphore; | 29 import java.util.concurrent.Semaphore; |
31 import java.util.concurrent.TimeUnit; | 30 import java.util.concurrent.TimeUnit; |
32 import java.util.concurrent.atomic.AtomicInteger; | 31 import java.util.concurrent.atomic.AtomicInteger; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mCont
entsClient); | 75 AwTestContainerView testView = createAwTestContainerViewOnMainSync(mCont
entsClient); |
77 AwContents awContents = testView.getAwContents(); | 76 AwContents awContents = testView.getAwContents(); |
78 | 77 |
79 final CallbackHelper loadHelper = mContentsClient.getOnPageFinishedHelpe
r(); | 78 final CallbackHelper loadHelper = mContentsClient.getOnPageFinishedHelpe
r(); |
80 | 79 |
81 final String mime = "text/html"; | 80 final String mime = "text/html"; |
82 final String emptyDoc = "<head/><body/>"; | 81 final String emptyDoc = "<head/><body/>"; |
83 final String imageDoc = "<head/><body><img/><img/></body>"; | 82 final String imageDoc = "<head/><body><img/><img/></body>"; |
84 | 83 |
85 // Make sure a document that does not have images returns 0 | 84 // Make sure a document that does not have images returns 0 |
86 loadDataSync(awContents.getContentViewCore(), loadHelper, emptyDoc, mime
, false); | 85 loadDataSync(awContents, loadHelper, emptyDoc, mime, false); |
87 int result = callDocumentHasImagesSync(awContents); | 86 int result = callDocumentHasImagesSync(awContents); |
88 assertEquals(0, result); | 87 assertEquals(0, result); |
89 | 88 |
90 // Make sure a document that does have images returns 1 | 89 // Make sure a document that does have images returns 1 |
91 loadDataSync(awContents.getContentViewCore(), loadHelper, imageDoc, mime
, false); | 90 loadDataSync(awContents, loadHelper, imageDoc, mime, false); |
92 result = callDocumentHasImagesSync(awContents); | 91 result = callDocumentHasImagesSync(awContents); |
93 assertEquals(1, result); | 92 assertEquals(1, result); |
94 } | 93 } |
95 | 94 |
96 private void clearCacheOnUiThread(final AwContents awContents, | 95 private void clearCacheOnUiThread(final AwContents awContents, |
97 final boolean includeDiskFiles) throws
Throwable { | 96 final boolean includeDiskFiles) throws
Throwable { |
98 runTestOnUiThread(new Runnable() { | 97 runTestOnUiThread(new Runnable() { |
99 @Override | 98 @Override |
100 public void run() { | 99 public void run() { |
101 awContents.clearCache(includeDiskFiles); | 100 awContents.clearCache(includeDiskFiles); |
102 } | 101 } |
103 }); | 102 }); |
104 } | 103 } |
105 | 104 |
106 @SmallTest | 105 @SmallTest |
107 @Feature({"Android-WebView"}) | 106 @Feature({"Android-WebView"}) |
108 public void testClearCacheMemoryAndDisk() throws Throwable { | 107 public void testClearCacheMemoryAndDisk() throws Throwable { |
109 final TestAwContentsClient contentClient = new TestAwContentsClient(); | 108 final TestAwContentsClient contentClient = new TestAwContentsClient(); |
110 final AwTestContainerView testContainer = | 109 final AwTestContainerView testContainer = |
111 createAwTestContainerViewOnMainSync(false, contentClient); | 110 createAwTestContainerViewOnMainSync(false, contentClient); |
112 final ContentViewCore contentView = testContainer.getContentViewCore(); | |
113 final AwContents awContents = testContainer.getAwContents(); | 111 final AwContents awContents = testContainer.getAwContents(); |
114 | 112 |
115 TestWebServer webServer = null; | 113 TestWebServer webServer = null; |
116 try { | 114 try { |
117 webServer = new TestWebServer(false); | 115 webServer = new TestWebServer(false); |
118 final String pagePath = "/clear_cache_test.html"; | 116 final String pagePath = "/clear_cache_test.html"; |
119 List<Pair<String, String>> headers = new ArrayList<Pair<String, Stri
ng>>(); | 117 List<Pair<String, String>> headers = new ArrayList<Pair<String, Stri
ng>>(); |
120 // Set Cache-Control headers to cache this request. One century shou
ld be long enough. | 118 // Set Cache-Control headers to cache this request. One century shou
ld be long enough. |
121 headers.add(Pair.create("Cache-Control", "max-age=3153600000")); | 119 headers.add(Pair.create("Cache-Control", "max-age=3153600000")); |
122 headers.add(Pair.create("Last-Modified", "Wed, 3 Oct 2012 00:00:00 G
MT")); | 120 headers.add(Pair.create("Last-Modified", "Wed, 3 Oct 2012 00:00:00 G
MT")); |
123 final String pageUrl = webServer.setResponse( | 121 final String pageUrl = webServer.setResponse( |
124 pagePath, "<html><body>foo</body></html>", headers); | 122 pagePath, "<html><body>foo</body></html>", headers); |
125 | 123 |
126 // First load to populate cache. | 124 // First load to populate cache. |
127 clearCacheOnUiThread(awContents, true); | 125 clearCacheOnUiThread(awContents, true); |
128 loadUrlSync(contentView, | 126 loadUrlSync(awContents, |
129 contentClient.getOnPageFinishedHelper(), | 127 contentClient.getOnPageFinishedHelper(), |
130 pageUrl); | 128 pageUrl); |
131 assertEquals(1, webServer.getRequestCount(pagePath)); | 129 assertEquals(1, webServer.getRequestCount(pagePath)); |
132 | 130 |
133 // Load about:blank so next load is not treated as reload by webkit
and force | 131 // Load about:blank so next load is not treated as reload by webkit
and force |
134 // revalidate with the server. | 132 // revalidate with the server. |
135 loadUrlSync(contentView, | 133 loadUrlSync(awContents, |
136 contentClient.getOnPageFinishedHelper(), | 134 contentClient.getOnPageFinishedHelper(), |
137 "about:blank"); | 135 "about:blank"); |
138 | 136 |
139 // No clearCache call, so should be loaded from cache. | 137 // No clearCache call, so should be loaded from cache. |
140 loadUrlSync(contentView, | 138 loadUrlSync(awContents, |
141 contentClient.getOnPageFinishedHelper(), | 139 contentClient.getOnPageFinishedHelper(), |
142 pageUrl); | 140 pageUrl); |
143 assertEquals(1, webServer.getRequestCount(pagePath)); | 141 assertEquals(1, webServer.getRequestCount(pagePath)); |
144 | 142 |
145 // Same as above. | 143 // Same as above. |
146 loadUrlSync(contentView, | 144 loadUrlSync(awContents, |
147 contentClient.getOnPageFinishedHelper(), | 145 contentClient.getOnPageFinishedHelper(), |
148 "about:blank"); | 146 "about:blank"); |
149 | 147 |
150 // Clear cache, so should hit server again. | 148 // Clear cache, so should hit server again. |
151 clearCacheOnUiThread(awContents, true); | 149 clearCacheOnUiThread(awContents, true); |
152 loadUrlSync(contentView, | 150 loadUrlSync(awContents, |
153 contentClient.getOnPageFinishedHelper(), | 151 contentClient.getOnPageFinishedHelper(), |
154 pageUrl); | 152 pageUrl); |
155 assertEquals(2, webServer.getRequestCount(pagePath)); | 153 assertEquals(2, webServer.getRequestCount(pagePath)); |
156 } finally { | 154 } finally { |
157 if (webServer != null) webServer.shutdown(); | 155 if (webServer != null) webServer.shutdown(); |
158 } | 156 } |
159 } | 157 } |
160 | 158 |
161 @SmallTest | 159 @SmallTest |
162 @Feature({"Android-WebView"}) | 160 @Feature({"Android-WebView"}) |
(...skipping 17 matching lines...) Expand all Loading... |
180 | 178 |
181 /** | 179 /** |
182 * @SmallTest | 180 * @SmallTest |
183 * @Feature({"Android-WebView"}) | 181 * @Feature({"Android-WebView"}) |
184 * BUG 6094807 | 182 * BUG 6094807 |
185 */ | 183 */ |
186 @DisabledTest | 184 @DisabledTest |
187 public void testGetFavicon() throws Throwable { | 185 public void testGetFavicon() throws Throwable { |
188 final AwTestContainerView testView = createAwTestContainerViewOnMainSync
(mContentsClient); | 186 final AwTestContainerView testView = createAwTestContainerViewOnMainSync
(mContentsClient); |
189 final AwContents awContents = testView.getAwContents(); | 187 final AwContents awContents = testView.getAwContents(); |
190 final ContentViewCore contentViewCore = testView.getContentViewCore(); | |
191 | 188 |
192 TestWebServer webServer = null; | 189 TestWebServer webServer = null; |
193 try { | 190 try { |
194 webServer = new TestWebServer(false); | 191 webServer = new TestWebServer(false); |
195 | 192 |
196 final String faviconUrl = webServer.setResponseBase64( | 193 final String faviconUrl = webServer.setResponseBase64( |
197 "/" + CommonResources.FAVICON_FILENAME, CommonResources.FAVI
CON_DATA_BASE64, | 194 "/" + CommonResources.FAVICON_FILENAME, CommonResources.FAVI
CON_DATA_BASE64, |
198 CommonResources.getImagePngHeaders(false)); | 195 CommonResources.getImagePngHeaders(false)); |
199 final String pageUrl = webServer.setResponse("/favicon.html", | 196 final String pageUrl = webServer.setResponse("/favicon.html", |
200 CommonResources.FAVICON_STATIC_HTML, null); | 197 CommonResources.FAVICON_STATIC_HTML, null); |
201 | 198 |
202 // The getFavicon will return the right icon a certain time after | 199 // The getFavicon will return the right icon a certain time after |
203 // the page load completes which makes it slightly hard to test. | 200 // the page load completes which makes it slightly hard to test. |
204 final Bitmap defaultFavicon = awContents.getFavicon(); | 201 final Bitmap defaultFavicon = awContents.getFavicon(); |
205 | 202 |
206 getContentSettingsOnUiThread(contentViewCore).setImagesEnabled(true)
; | 203 getContentSettingsOnUiThread(awContents).setImagesEnabled(true); |
207 loadUrlSync(contentViewCore, mContentsClient.getOnPageFinishedHelper
(), pageUrl); | 204 loadUrlSync(awContents, mContentsClient.getOnPageFinishedHelper(), p
ageUrl); |
208 | 205 |
209 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | 206 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
210 @Override | 207 @Override |
211 public boolean isSatisfied() { | 208 public boolean isSatisfied() { |
212 return awContents.getFavicon() != null && | 209 return awContents.getFavicon() != null && |
213 !awContents.getFavicon().sameAs(defaultFavicon); | 210 !awContents.getFavicon().sameAs(defaultFavicon); |
214 } | 211 } |
215 }, TEST_TIMEOUT, CHECK_INTERVAL)); | 212 }, TEST_TIMEOUT, CHECK_INTERVAL)); |
216 | 213 |
217 final Object originalFaviconSource = (new URL(faviconUrl)).getConten
t(); | 214 final Object originalFaviconSource = (new URL(faviconUrl)).getConten
t(); |
218 final Bitmap originalFavicon = | 215 final Bitmap originalFavicon = |
219 BitmapFactory.decodeStream((InputStream)originalFaviconSource); | 216 BitmapFactory.decodeStream((InputStream)originalFaviconSource); |
220 assertNotNull(originalFavicon); | 217 assertNotNull(originalFavicon); |
221 | 218 |
222 assertTrue(awContents.getFavicon().sameAs(originalFavicon)); | 219 assertTrue(awContents.getFavicon().sameAs(originalFavicon)); |
223 | 220 |
224 } finally { | 221 } finally { |
225 if (webServer != null) webServer.shutdown(); | 222 if (webServer != null) webServer.shutdown(); |
226 } | 223 } |
227 } | 224 } |
228 } | 225 } |
OLD | NEW |