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