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.content.Context; | 7 import android.content.Context; |
8 import android.os.Build; | 8 import android.os.Build; |
9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
10 import android.util.Pair; | |
10 | 11 |
11 import org.chromium.base.test.util.Feature; | 12 import org.chromium.base.test.util.Feature; |
12 import org.chromium.base.test.util.TestFileUtil; | 13 import org.chromium.base.test.util.TestFileUtil; |
13 import org.chromium.base.test.util.UrlUtils; | 14 import org.chromium.base.test.util.UrlUtils; |
14 import org.chromium.content.browser.ContentSettings; | 15 import org.chromium.content.browser.ContentSettings; |
15 import org.chromium.content.browser.ContentViewCore; | 16 import org.chromium.content.browser.ContentViewCore; |
16 import org.chromium.content.browser.test.util.CallbackHelper; | 17 import org.chromium.content.browser.test.util.CallbackHelper; |
18 import org.chromium.content.browser.test.util.Criteria; | |
19 import org.chromium.content.browser.test.util.CriteriaHelper; | |
17 import org.chromium.content.browser.test.util.HistoryUtils; | 20 import org.chromium.content.browser.test.util.HistoryUtils; |
18 | 21 |
19 import java.util.regex.Matcher; | 22 import java.util.regex.Matcher; |
20 import java.util.regex.Pattern; | 23 import java.util.regex.Pattern; |
24 import java.util.ArrayList; | |
25 import java.util.List; | |
26 | |
21 | 27 |
22 /** | 28 /** |
23 * A test suite for ContentSettings class. The key objective is to verify that e ach | 29 * A test suite for ContentSettings class. The key objective is to verify that e ach |
24 * settings applies either to each individual view or to all views of the | 30 * settings applies either to each individual view or to all views of the |
25 * application. | 31 * application. |
26 */ | 32 */ |
27 public class AwSettingsTest extends AndroidWebViewTestBase { | 33 public class AwSettingsTest extends AndroidWebViewTestBase { |
34 private static final int CHECK_INTERVAL = 100; | |
35 | |
28 private static final boolean ENABLED = true; | 36 private static final boolean ENABLED = true; |
29 private static final boolean DISABLED = false; | 37 private static final boolean DISABLED = false; |
30 | 38 |
31 /** | 39 /** |
32 * A helper class for testing a particular preference from ContentSettings. | 40 * A helper class for testing a particular preference from ContentSettings. |
33 * The generic type T is the type of the setting. Usually, to test an | 41 * The generic type T is the type of the setting. Usually, to test an |
34 * effect of the preference, JS code is executed that sets document's title. | 42 * effect of the preference, JS code is executed that sets document's title. |
35 * In this case, requiresJsEnabled constructor argument must be set to true. | 43 * In this case, requiresJsEnabled constructor argument must be set to true. |
36 */ | 44 */ |
37 abstract class AwSettingsTestHelper<T> { | 45 abstract class AwSettingsTestHelper<T> { |
(...skipping 1289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1327 } | 1335 } |
1328 | 1336 |
1329 private void resetResourceRequestCountInContentProvider(String resource) { | 1337 private void resetResourceRequestCountInContentProvider(String resource) { |
1330 Context context = getInstrumentation().getTargetContext(); | 1338 Context context = getInstrumentation().getTargetContext(); |
1331 TestContentProvider.resetResourceRequestCount(context, resource); | 1339 TestContentProvider.resetResourceRequestCount(context, resource); |
1332 } | 1340 } |
1333 | 1341 |
1334 private String createContentUrl(final String target) { | 1342 private String createContentUrl(final String target) { |
1335 return TestContentProvider.createContentUrl(target); | 1343 return TestContentProvider.createContentUrl(target); |
1336 } | 1344 } |
1345 | |
1346 private final String IMAGE_DATA = "iVBORw0KGgoAAA" + | |
1347 "ANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAAAXNSR0IArs4c6QAAAA1JREFUCB0BAgD9/wAAA AIAAc3j" + | |
1348 "0SsAAAAASUVORK5CYII="; | |
1349 | |
1350 private final String DATA_URL_IMAGE_HTML = "<html>" + | |
1351 "<head><script>function updateTitle(){" + | |
1352 "document.title=document.getElementById('img').naturalHeight;}</script>< /head>" + | |
1353 "<body onload='updateTitle()'>" + | |
1354 "<img id='img' onload='updateTitle()' src='data:image/png;base64," + IMA GE_DATA + | |
1355 "'></body></html>"; | |
1356 | |
1357 @SmallTest | |
joth
2012/09/25 20:28:21
both these tests load pages and wait for load to c
boliu
2012/09/25 20:52:28
Pretty much every test in this file fits that desc
| |
1358 @Feature({"Android-WebView", "Preferences"}) | |
1359 public void testBlockNetworkImagesDoesNotBlockDataUrlImage() throws Throwabl e { | |
1360 final TestAwContentsClient contentClient = new TestAwContentsClient(); | |
1361 final ContentViewCore contentView = | |
1362 createAwTestContainerViewOnMainSync(false, contentClient).getCon tentViewCore(); | |
1363 final ContentSettings settings = getContentSettingsOnUiThread(contentVie w); | |
1364 | |
1365 settings.setJavaScriptEnabled(true); | |
1366 | |
1367 settings.setImagesEnabled(false); | |
1368 loadDataSync(contentView, | |
1369 contentClient.getOnPageFinishedHelper(), | |
1370 DATA_URL_IMAGE_HTML, | |
1371 "text/html", | |
1372 false); | |
1373 assertEquals("1", getTitleOnUiThread(contentView)); | |
1374 } | |
1375 | |
1376 @SmallTest | |
1377 @Feature({"Android-WebView", "Preferences"}) | |
1378 public void testBlockNetworkImagesBlocksNetworkImageAndReloadInPlace() throw s Throwable { | |
1379 final TestAwContentsClient contentClient = new TestAwContentsClient(); | |
1380 final ContentViewCore contentView = | |
1381 createAwTestContainerViewOnMainSync(false, contentClient).getCon tentViewCore(); | |
1382 final ContentSettings settings = getContentSettingsOnUiThread(contentVie w); | |
1383 settings.setJavaScriptEnabled(true); | |
1384 | |
1385 TestWebServer webServer = null; | |
1386 try { | |
1387 webServer = new TestWebServer(false); | |
1388 List<Pair<String, String>> imageHeaders = new ArrayList<Pair<String, String>>(); | |
1389 imageHeaders.add(Pair.create("Content-Type", "image/png")); | |
1390 final String imagePath = "/image.png"; | |
1391 webServer.setResponseBase64(imagePath, IMAGE_DATA, imageHeaders); | |
1392 | |
1393 final String pagePath = "/html_image.html"; | |
1394 final String httpUrlImageHtml = "<html>" + | |
1395 "<head><script>" + | |
1396 "function updateTitle(){" + | |
1397 "document.title=document.getElementById('img').naturalHeight;}" + | |
1398 "</script></head>" + | |
1399 "<body onload='updateTitle()'>" + | |
1400 "<img id='img' onload='updateTitle()' src='" + imagePath + | |
1401 "'></body></html>"; | |
1402 final String httpImageUrl = webServer.setResponse(pagePath, httpUrlI mageHtml, null); | |
1403 | |
1404 settings.setImagesEnabled(false); | |
1405 loadUrlSync(contentView, contentClient.getOnPageFinishedHelper(), ht tpImageUrl); | |
1406 assertEquals("0", getTitleOnUiThread(contentView)); | |
1407 | |
1408 settings.setImagesEnabled(true); | |
1409 assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { | |
1410 @Override | |
1411 public boolean isSatisfied() { | |
1412 try { | |
1413 return "0".equals(getTitleOnUiThread(contentView)); | |
1414 } catch (Throwable t) { | |
1415 t.printStackTrace(); | |
1416 fail("Failed to getTitleOnUIThread: " + t.toString()); | |
1417 return false; | |
1418 } | |
1419 } | |
1420 }, WAIT_TIMEOUT_SECONDS * 1000, CHECK_INTERVAL)); | |
1421 } finally { | |
1422 if (webServer != null) webServer.shutdown(); | |
1423 } | |
1424 } | |
1337 } | 1425 } |
OLD | NEW |