Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: android_webview/javatests/src/org/chromium/android_webview/test/AwSettingsTest.java

Issue 11419093: [Android WebView] Implement WebSettings.{get|set}CacheMode (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.webkit.WebSettings;
10 11
11 import org.apache.http.Header; 12 import org.apache.http.Header;
12 import org.apache.http.HttpRequest; 13 import org.apache.http.HttpRequest;
13 import org.chromium.android_webview.AndroidProtocolHandler; 14 import org.chromium.android_webview.AndroidProtocolHandler;
14 import org.chromium.android_webview.AwContents; 15 import org.chromium.android_webview.AwContents;
15 import org.chromium.android_webview.AwSettings; 16 import org.chromium.android_webview.AwSettings;
16 import org.chromium.android_webview.test.util.CommonResources; 17 import org.chromium.android_webview.test.util.CommonResources;
17 import org.chromium.android_webview.test.util.ImagePageGenerator; 18 import org.chromium.android_webview.test.util.ImagePageGenerator;
18 import org.chromium.base.test.util.DisabledTest; 19 import org.chromium.base.test.util.DisabledTest;
19 import org.chromium.base.test.util.Feature; 20 import org.chromium.base.test.util.Feature;
(...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 " var newWindow = window.open(" + 935 " var newWindow = window.open(" +
935 " 'data:text/html;charset=utf-8," + 936 " 'data:text/html;charset=utf-8," +
936 " <html><head><title>" + POPUP_ENABLED + "</title> </head></html>');" + 937 " <html><head><title>" + POPUP_ENABLED + "</title> </head></html>');" +
937 " if (!newWindow) document.title = '" + POPUP_BLOCKED + "';" + 938 " if (!newWindow) document.title = '" + POPUP_BLOCKED + "';" +
938 " }" + 939 " }" +
939 "</script></head>" + 940 "</script></head>" +
940 "<body onload='tryOpenWindow()'></body></html>"; 941 "<body onload='tryOpenWindow()'></body></html>";
941 } 942 }
942 } 943 }
943 944
945 class AwSettingsCacheModeTestHelper extends AwSettingsWithSettingsTestHelper <Integer> {
946
947 AwSettingsCacheModeTestHelper(
948 AwContents awContents,
949 TestAwContentsClient contentViewClient,
950 int index,
951 TestWebServer webServer) throws Throwable {
952 super(awContents, contentViewClient, true);
953 mIndex = index;
954 mWebServer = webServer;
955 runTestOnUiThread(new Runnable() {
956 @Override
957 public void run() {
958 mAwContents.clearCache(true);
959 }
960 });
961 }
962
963 @Override
964 protected Integer getAlteredValue() {
965 // We use the value that results in a behaviour completely opposite to default.
966 return WebSettings.LOAD_CACHE_ONLY;
967 }
968
969 @Override
970 protected Integer getInitialValue() {
971 return WebSettings.LOAD_DEFAULT;
972 }
973
974 @Override
975 protected Integer getCurrentValue() {
976 return mAwSettings.getCacheMode();
977 }
978
979 @Override
980 protected void setCurrentValue(Integer value) {
981 mAwSettings.setCacheMode(value);
982 }
983
984 @Override
985 protected void doEnsureSettingHasValue(Integer value) throws Throwable {
986 final String htmlPath = "/cache_mode_" + mIndex + ".html";
987 mIndex += 2;
988 final String url = mWebServer.setResponse(htmlPath, "response", null );
989 assertEquals(0, mWebServer.getRequestCount(htmlPath));
990 if (value == WebSettings.LOAD_DEFAULT) {
991 loadUrlSync(url);
992 assertEquals(1, mWebServer.getRequestCount(htmlPath));
993 } else {
994 loadUrlSyncAndExpectError(url);
995 assertEquals(0, mWebServer.getRequestCount(htmlPath));
996 }
997 }
998
999 private int mIndex;
1000 private TestWebServer mWebServer;
1001 }
1002
944 // The test verifies that JavaScript is disabled upon WebView 1003 // The test verifies that JavaScript is disabled upon WebView
945 // creation without accessing ContentSettings. If the test passes, 1004 // creation without accessing ContentSettings. If the test passes,
946 // it means that WebView-specific web preferences configuration 1005 // it means that WebView-specific web preferences configuration
947 // is applied on WebView creation. JS state is used, because it is 1006 // is applied on WebView creation. JS state is used, because it is
948 // enabled by default in Chrome, but must be disabled by default 1007 // enabled by default in Chrome, but must be disabled by default
949 // in WebView. 1008 // in WebView.
950 @SmallTest 1009 @SmallTest
951 @Feature({"AndroidWebView", "Preferences"}) 1010 @Feature({"AndroidWebView", "Preferences"})
952 public void testJavaScriptDisabledByDefault() throws Throwable { 1011 public void testJavaScriptDisabledByDefault() throws Throwable {
953 final String JS_ENABLED_STRING = "JS has run"; 1012 final String JS_ENABLED_STRING = "JS has run";
(...skipping 939 matching lines...) Expand 10 before | Expand all | Expand 10 after
1893 new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views .getClient1(), 1)); 1952 new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views .getClient1(), 1));
1894 } 1953 }
1895 1954
1896 public void testJavaScriptPopupsBoth() throws Throwable { 1955 public void testJavaScriptPopupsBoth() throws Throwable {
1897 ViewPair views = createViews(NORMAL_VIEW, INCOGNITO_VIEW); 1956 ViewPair views = createViews(NORMAL_VIEW, INCOGNITO_VIEW);
1898 runPerViewSettingsTest( 1957 runPerViewSettingsTest(
1899 new AwSettingsJavaScriptPopupsTestHelper(views.getContents0(), views .getClient0(), 0), 1958 new AwSettingsJavaScriptPopupsTestHelper(views.getContents0(), views .getClient0(), 0),
1900 new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views .getClient1(), 1)); 1959 new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views .getClient1(), 1));
1901 } 1960 }
1902 1961
1962 @SmallTest
1963 @Feature({"AndroidWebView", "Preferences"})
1964 public void testCacheMode() throws Throwable {
1965 final TestAwContentsClient contentClient = new TestAwContentsClient();
1966 final AwTestContainerView testContainer =
1967 createAwTestContainerViewOnMainSync(false, contentClient);
1968 final AwContents awContents = testContainer.getAwContents();
1969 final AwSettings awSettings = getAwSettingsOnUiThread(testContainer.getA wContents());
1970 runTestOnUiThread(new Runnable() {
1971 @Override
1972 public void run() {
1973 awContents.clearCache(true);
1974 }
1975 });
1976
1977 assertEquals(WebSettings.LOAD_DEFAULT, awSettings.getCacheMode());
1978 TestWebServer webServer = null;
1979 try {
1980 webServer = new TestWebServer(false);
1981 final String htmlPath = "/testCacheMode.html";
1982 final String url = webServer.setResponse(htmlPath, "response", null) ;
1983 awSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
1984 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url );
1985 assertEquals(1, webServer.getRequestCount(htmlPath));
1986 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url );
1987 assertEquals(1, webServer.getRequestCount(htmlPath));
1988
1989 awSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
1990 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url );
1991 assertEquals(2, webServer.getRequestCount(htmlPath));
1992 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url );
1993 assertEquals(3, webServer.getRequestCount(htmlPath));
1994
1995 awSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
1996 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url );
1997 assertEquals(3, webServer.getRequestCount(htmlPath));
1998 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url );
1999 assertEquals(3, webServer.getRequestCount(htmlPath));
2000
2001 final String htmlNotInCachePath = "/testCacheMode-not-in-cache.html" ;
2002 final String urlNotInCache = webServer.setResponse(htmlNotInCachePat h, "", null);
2003 loadUrlSyncAndExpectError(awContents,
2004 contentClient.getOnPageFinishedHelper(),
2005 contentClient.getOnReceivedErrorHelper(),
2006 urlNotInCache);
2007 assertEquals(0, webServer.getRequestCount(htmlNotInCachePath));
2008 } finally {
2009 if (webServer != null) webServer.shutdown();
2010 }
2011 }
2012
2013 @SmallTest
2014 @Feature({"AndroidWebView", "Preferences"})
2015 public void testCacheModeNormal() throws Throwable {
2016 ViewPair views = createViews(NORMAL_VIEW, NORMAL_VIEW);
2017 TestWebServer webServer = null;
2018 try {
2019 webServer = new TestWebServer(false);
2020 runPerViewSettingsTest(
2021 new AwSettingsCacheModeTestHelper(
2022 views.getContents0(), views.getClient0(), 0, webServ er),
2023 new AwSettingsCacheModeTestHelper(
2024 views.getContents1(), views.getClient1(), 1, webServ er));
2025 } finally {
2026 if (webServer != null) webServer.shutdown();
2027 }
2028 }
2029
2030 @SmallTest
2031 @Feature({"AndroidWebView", "Preferences"})
2032 public void testCacheModeIncognito() throws Throwable {
2033 ViewPair views = createViews(INCOGNITO_VIEW, INCOGNITO_VIEW);
2034 TestWebServer webServer = null;
2035 try {
2036 webServer = new TestWebServer(false);
2037 runPerViewSettingsTest(
2038 new AwSettingsCacheModeTestHelper(
2039 views.getContents0(), views.getClient0(), 0, webServ er),
2040 new AwSettingsCacheModeTestHelper(
2041 views.getContents1(), views.getClient1(), 1, webServ er));
2042 } finally {
2043 if (webServer != null) webServer.shutdown();
2044 }
2045 }
2046
2047 @SmallTest
2048 @Feature({"AndroidWebView", "Preferences"})
2049 public void testCacheModeBoth() throws Throwable {
2050 ViewPair views = createViews(NORMAL_VIEW, INCOGNITO_VIEW);
2051 TestWebServer webServer = null;
2052 try {
2053 webServer = new TestWebServer(false);
2054 runPerViewSettingsTest(
2055 new AwSettingsCacheModeTestHelper(
2056 views.getContents0(), views.getClient0(), 0, webServ er),
2057 new AwSettingsCacheModeTestHelper(
2058 views.getContents1(), views.getClient1(), 1, webServ er));
2059 } finally {
2060 if (webServer != null) webServer.shutdown();
2061 }
2062 }
2063
1903 class ViewPair { 2064 class ViewPair {
1904 private final AwContents contents0; 2065 private final AwContents contents0;
1905 private final TestAwContentsClient client0; 2066 private final TestAwContentsClient client0;
1906 private final AwContents contents1; 2067 private final AwContents contents1;
1907 private final TestAwContentsClient client1; 2068 private final TestAwContentsClient client1;
1908 2069
1909 ViewPair(AwContents contents0, TestAwContentsClient client0, 2070 ViewPair(AwContents contents0, TestAwContentsClient client0,
1910 AwContents contents1, TestAwContentsClient client1) { 2071 AwContents contents1, TestAwContentsClient client1) {
1911 this.contents0 = contents0; 2072 this.contents0 = contents0;
1912 this.client0 = client0; 2073 this.client0 = client0;
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
2076 contentClient.getOnPageFinishedHelper(), 2237 contentClient.getOnPageFinishedHelper(),
2077 "file:///" + fileName); 2238 "file:///" + fileName);
2078 assertEquals(1, webServer.getRequestCount(httpPath)); 2239 assertEquals(1, webServer.getRequestCount(httpPath));
2079 assertEquals("img_onload_fired", getTitleOnUiThread(awContents)); 2240 assertEquals("img_onload_fired", getTitleOnUiThread(awContents));
2080 } finally { 2241 } finally {
2081 if (fileName != null) TestFileUtil.deleteFile(fileName); 2242 if (fileName != null) TestFileUtil.deleteFile(fileName);
2082 if (webServer != null) webServer.shutdown(); 2243 if (webServer != null) webServer.shutdown();
2083 } 2244 }
2084 } 2245 }
2085 } 2246 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698