diff options
author | mnaganov@chromium.org <mnaganov@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-04 22:55:43 +0000 |
---|---|---|
committer | mnaganov@chromium.org <mnaganov@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-04 22:55:43 +0000 |
commit | 2908f2cef8990d5c01dfc1d871df56cd1fae2ce4 (patch) | |
tree | 7aa820c0732b1f3112b0c5c0c71d3e43219d632e /android_webview/javatests | |
parent | 383ca80a4d2389c328044d5c98162101b80dd297 (diff) | |
download | chromium_src-2908f2cef8990d5c01dfc1d871df56cd1fae2ce4.zip chromium_src-2908f2cef8990d5c01dfc1d871df56cd1fae2ce4.tar.gz chromium_src-2908f2cef8990d5c01dfc1d871df56cd1fae2ce4.tar.bz2 |
[Android] Implement WebSettings.setAppCache{Enabled|Path}
AppCacheEnabled is mapped onto WebPreferences.application_cache_enabled, which goes directly into WebKit Settings.
AppCachePath is only used as a flag to enable AppCache. We can't make use of the full path given, because in Chromium the Application Cache directory lives inside the browser context (profile).
The tests added trigger a DCHECK in disk cache, unless the profile is empty when the test starts. This makes impossible to run them both now, so only one of them is enabled for now.
Android CTS tests WebSettings.testAppCache{Disabled|Enabled} are passing with this patch.
Review URL: https://chromiumcodereview.appspot.com/11411229
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171074 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'android_webview/javatests')
3 files changed, 171 insertions, 11 deletions
diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewTestBase.java b/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewTestBase.java index b7fc439..cd9802e 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewTestBase.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AndroidWebViewTestBase.java @@ -290,6 +290,10 @@ public class AndroidWebViewTestBase }); } + /** + * Clears the resource cache. Note that the cache is per-application, so this will clear the + * cache for all WebViews used. + */ protected void clearCacheOnUiThread( final AwContents awContents, final boolean includeDiskFiles) throws Throwable { diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java index c0853f1b..e5e353e 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java @@ -16,6 +16,7 @@ import org.chromium.android_webview.AwContents; import org.chromium.android_webview.AwSettings; import org.chromium.android_webview.test.util.CommonResources; import org.chromium.android_webview.test.util.ImagePageGenerator; +import org.chromium.base.test.util.DisabledTest; import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.TestFileUtil; import org.chromium.base.test.util.UrlUtils; @@ -32,7 +33,6 @@ import java.util.regex.Pattern; import java.util.ArrayList; import java.util.List; - /** * A test suite for ContentSettings class. The key objective is to verify that each * settings applies either to each individual view or to all views of the @@ -1807,7 +1807,7 @@ public class AwSettingsTest extends AndroidWebViewTestBase { return false; } } - }, WAIT_TIMEOUT_SECONDS * 1000, CHECK_INTERVAL)); + }, TEST_TIMEOUT, CHECK_INTERVAL)); } finally { if (webServer != null) webServer.shutdown(); } @@ -2161,6 +2161,163 @@ public class AwSettingsTest extends AndroidWebViewTestBase { } } + static class ManifestTestHelper { + private final TestWebServer mWebServer; + private final String mHtmlPath; + private final String mHtmlUrl; + private final String mManifestPath; + + ManifestTestHelper( + TestWebServer webServer, String htmlPageName, String manifestName) { + mWebServer = webServer; + mHtmlPath = "/" + htmlPageName; + mHtmlUrl = webServer.setResponse( + mHtmlPath, "<html manifest=\"" + manifestName + "\"></html>", null); + mManifestPath = "/" + manifestName; + webServer.setResponse( + mManifestPath, + "CACHE MANIFEST", + CommonResources.getContentTypeAndCacheHeaders("text/cache-manifest", false)); + } + + String getHtmlPath() { + return mHtmlPath; + } + + String getHtmlUrl() { + return mHtmlUrl; + } + + String getManifestPath() { + return mManifestPath; + } + + int waitUntilHtmlIsRequested(final int initialRequestCount) throws InterruptedException { + return waitUntilResourceIsRequested(mHtmlPath, initialRequestCount); + } + + int waitUntilManifestIsRequested(final int initialRequestCount) + throws InterruptedException { + return waitUntilResourceIsRequested(mManifestPath, initialRequestCount); + } + + private int waitUntilResourceIsRequested( + final String path, final int initialRequestCount) throws InterruptedException { + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { + @Override + public boolean isSatisfied() { + return mWebServer.getRequestCount(path) > initialRequestCount; + } + }, TEST_TIMEOUT, CHECK_INTERVAL)); + return mWebServer.getRequestCount(path); + } + } + + @SmallTest + @Feature({"AndroidWebView", "Preferences", "AppCache"}) + public void testAppCache() throws Throwable { + final TestAwContentsClient contentClient = new TestAwContentsClient(); + final AwTestContainerView testContainer = + createAwTestContainerViewOnMainSync(false, contentClient); + final AwContents awContents = testContainer.getAwContents(); + final ContentSettings settings = getContentSettingsOnUiThread(awContents); + // Not sure why, but I'm experiencing DCHECK failures at net/disk_cache/in_flight_io.cc:98 + // w/o this line. crbug.com/163383 + clearCacheOnUiThread(awContents, true); + settings.setJavaScriptEnabled(true); + // Note that the cache isn't actually enabled until the call to setAppCachePath. + settings.setAppCacheEnabled(true); + + TestWebServer webServer = null; + try { + webServer = new TestWebServer(false); + ManifestTestHelper helper = new ManifestTestHelper( + webServer, "testAppCache.html", "appcache.manifest"); + loadUrlSync( + awContents, + contentClient.getOnPageFinishedHelper(), + helper.getHtmlUrl()); + helper.waitUntilHtmlIsRequested(0); + // Unfortunately, there is no other good way of verifying that AppCache is + // disabled, other than checking that it didn't try to fetch the manifest. + Thread.sleep(1000); + assertEquals(0, webServer.getRequestCount(helper.getManifestPath())); + settings.setAppCachePath("whatever"); // Enables AppCache. + loadUrlSync( + awContents, + contentClient.getOnPageFinishedHelper(), + helper.getHtmlUrl()); + helper.waitUntilManifestIsRequested(0); + } finally { + if (webServer != null) webServer.shutdown(); + } + } + + /* + * @SmallTest + * @Feature({"AndroidWebView", "Preferences", "AppCache"}) + * BUG=crbug.com/163383 + * If you run this test with a non-empty profile, it will crash, + * unless you delete the test's data directory. + */ + @DisabledTest + public void testAppCacheNormal() throws Throwable { + // We don't use the test helper here, because making sure that AppCache + // is disabled takes a lot of time, so running through the usual drill + // will take about 20 seconds. + ViewPair views = createViews(NORMAL_VIEW, NORMAL_VIEW); + ContentSettings settings0 = getContentSettingsOnUiThread(views.getContents0()); + settings0.setJavaScriptEnabled(true); + settings0.setAppCachePath("whatever"); + settings0.setAppCacheEnabled(true); + ContentSettings settings1 = getContentSettingsOnUiThread(views.getContents1()); + settings1.setJavaScriptEnabled(true); + // AppCachePath setting is global, no need to set it for the second view. + settings1.setAppCacheEnabled(true); + + clearCacheOnUiThread(views.getContents0(), true); + + TestWebServer webServer = null; + try { + webServer = new TestWebServer(false); + ManifestTestHelper helper0 = new ManifestTestHelper( + webServer, "testAppCache_0.html", "appcache.manifest_0"); + loadUrlSync( + views.getContents0(), + views.getClient0().getOnPageFinishedHelper(), + helper0.getHtmlUrl()); + int manifestRequests0 = helper0.waitUntilManifestIsRequested(0); + ManifestTestHelper helper1 = new ManifestTestHelper( + webServer, "testAppCache_1.html", "appcache.manifest_1"); + loadUrlSync( + views.getContents1(), + views.getClient1().getOnPageFinishedHelper(), + helper1.getHtmlUrl()); + helper1.waitUntilManifestIsRequested(0); + settings1.setAppCacheEnabled(false); + loadUrlSync( + views.getContents0(), + views.getClient0().getOnPageFinishedHelper(), + helper0.getHtmlUrl()); + helper0.waitUntilManifestIsRequested(manifestRequests0); + final int prevManifestRequestCount = + webServer.getRequestCount(helper1.getManifestPath()); + int htmlRequests1 = webServer.getRequestCount(helper1.getHtmlPath()); + loadUrlSync( + views.getContents1(), + views.getClient1().getOnPageFinishedHelper(), + helper1.getHtmlUrl()); + helper1.waitUntilHtmlIsRequested(htmlRequests1); + // Unfortunately, there is no other good way of verifying that AppCache is + // disabled, other than checking that it didn't try to fetch the manifest. + Thread.sleep(1000); + assertEquals( + prevManifestRequestCount, webServer.getRequestCount(helper1.getManifestPath())); + } finally { + if (webServer != null) webServer.shutdown(); + } + } + class ViewPair { private final AwContents contents0; private final TestAwContentsClient client0; diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/util/CommonResources.java b/android_webview/javatests/src/org/chromium/android_webview/test/util/CommonResources.java index 421f5da..293d86e 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/util/CommonResources.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/util/CommonResources.java @@ -13,24 +13,23 @@ public class CommonResources { // Content-type headers used for HTML code. public static List<Pair<String, String>> getTextHtmlHeaders(boolean disableCache) { - List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>(); - headers.add(Pair.create("Content-Type", "text/html")); - if (disableCache) headers.add(Pair.create("Cache-Control", "no-store")); - return headers; + return getContentTypeAndCacheHeaders("text/html", disableCache); } // Content-type headers used for javascript code. public static List<Pair<String, String>> getTextJavascriptHeaders(boolean disableCache) { - List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>(); - headers.add(Pair.create("Content-Type", "text/javascript")); - if (disableCache) headers.add(Pair.create("Cache-Control", "no-store")); - return headers; + return getContentTypeAndCacheHeaders("text/javascript", disableCache); } // Content-type headers used for png images. public static List<Pair<String, String>> getImagePngHeaders(boolean disableCache) { + return getContentTypeAndCacheHeaders("image/png", disableCache); + } + + public static List<Pair<String, String>> getContentTypeAndCacheHeaders( + String contentType, boolean disableCache) { List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>(); - headers.add(Pair.create("Content-Type", "image/png")); + headers.add(Pair.create("Content-Type", contentType)); if (disableCache) headers.add(Pair.create("Cache-Control", "no-store")); return headers; } |