1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.android_webview.test;
7 import android.test.suitebuilder.annotation.LargeTest;
8 import android.util.Pair;
9 import android.webkit.ValueCallback;
11 import org.chromium.android_webview.AwContents;
12 import org.chromium.android_webview.AwQuotaManagerBridge;
13 import org.chromium.android_webview.AwSettings;
14 import org.chromium.android_webview.test.util.AwQuotaManagerBridgeTestUtil;
15 import org.chromium.base.test.util.Feature;
16 import org.chromium.content.browser.test.util.CallbackHelper;
17 import org.chromium.net.test.util.TestWebServer;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.Callable;
24 * Tests for the AwQuotaManagerBridge.
26 public class AwQuotaManagerBridgeTest extends AwTestBase {
27 private TestAwContentsClient mContentsClient;
28 private AwTestContainerView mTestView;
29 private AwContents mAwContents;
30 private TestWebServer mWebServer;
31 private String mOrigin;
34 public void setUp() throws Exception {
36 mContentsClient = new TestAwContentsClient();
37 mTestView = createAwTestContainerViewOnMainSync(mContentsClient);
38 mAwContents = mTestView.getAwContents();
39 mWebServer = TestWebServer.start();
40 mOrigin = mWebServer.getBaseUrl();
42 AwSettings settings = getAwSettingsOnUiThread(mAwContents);
43 settings.setJavaScriptEnabled(true);
44 settings.setDomStorageEnabled(true);
45 settings.setAppCacheEnabled(true);
46 settings.setAppCachePath("whatever"); // Enables AppCache.
50 public void tearDown() throws Exception {
52 if (mWebServer != null) {
53 mWebServer.shutdown();
58 private void deleteAllData() throws Exception {
59 final AwQuotaManagerBridge bridge =
60 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
61 getInstrumentation().runOnMainSync(new Runnable() {
64 bridge.deleteAllData();
69 private void deleteOrigin(final String origin) throws Exception {
70 final AwQuotaManagerBridge bridge =
71 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
72 getInstrumentation().runOnMainSync(new Runnable() {
75 bridge.deleteOrigin(origin);
80 private static class LongValueCallbackHelper extends CallbackHelper {
83 public void notifyCalled(long value) {
88 public long getValue() {
89 assert getCallCount() > 0;
94 private long getQuotaForOrigin(final String origin) throws Exception {
95 final LongValueCallbackHelper callbackHelper = new LongValueCallbackHelper();
96 final AwQuotaManagerBridge bridge =
97 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
99 int callCount = callbackHelper.getCallCount();
100 getInstrumentation().runOnMainSync(new Runnable() {
103 bridge.getQuotaForOrigin("foo.com",
104 new ValueCallback<Long>() {
106 public void onReceiveValue(Long quota) {
107 callbackHelper.notifyCalled(quota);
113 callbackHelper.waitForCallback(callCount);
115 return callbackHelper.getValue();
118 private long getUsageForOrigin(final String origin) throws Exception {
119 final LongValueCallbackHelper callbackHelper = new LongValueCallbackHelper();
120 final AwQuotaManagerBridge bridge =
121 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
123 int callCount = callbackHelper.getCallCount();
124 getInstrumentation().runOnMainSync(new Runnable() {
127 bridge.getUsageForOrigin(origin,
128 new ValueCallback<Long>() {
130 public void onReceiveValue(Long usage) {
131 callbackHelper.notifyCalled(usage);
137 callbackHelper.waitForCallback(callCount);
139 return callbackHelper.getValue();
142 private void useAppCache() throws Exception {
143 final String cachedFilePath = "/foo.js";
144 final String cachedFileContents = "1 + 1;";
145 mWebServer.setResponse(cachedFilePath, cachedFileContents, null);
147 final String manifestPath = "/foo.manifest";
148 final String manifestContents = "CACHE MANIFEST\nCACHE:\n" + cachedFilePath;
149 List<Pair<String, String>> manifestHeaders = new ArrayList<Pair<String, String>>();
150 manifestHeaders.add(Pair.create("Content-Disposition", "text/cache-manifest"));
151 mWebServer.setResponse(manifestPath, manifestContents, manifestHeaders);
153 final String pagePath = "/appcache.html";
154 final String pageContents = "<html manifest=\"" + manifestPath + "\">" +
155 "<head><script src=\"" + cachedFilePath + "\"></script></head></html>";
156 String url = mWebServer.setResponse(pagePath, pageContents, null);
158 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
159 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
160 "window.applicationCache.update();");
164 @Feature({"AndroidWebView", "WebStore"})
165 public void testDeleteAllWithAppCache() throws Exception {
166 final long initialUsage = getUsageForOrigin(mOrigin);
169 poll(new Callable<Boolean>() {
171 public Boolean call() throws Exception {
172 return getUsageForOrigin(mOrigin) > initialUsage;
177 poll(new Callable<Boolean>() {
179 public Boolean call() throws Exception {
180 return getUsageForOrigin(mOrigin) == 0;
186 @Feature({"AndroidWebView", "WebStore"})
187 public void testDeleteOriginWithAppCache() throws Exception {
188 final long initialUsage = getUsageForOrigin(mOrigin);
191 poll(new Callable<Boolean>() {
193 public Boolean call() throws Exception {
194 return getUsageForOrigin(mOrigin) > initialUsage;
198 deleteOrigin(mOrigin);
199 poll(new Callable<Boolean>() {
201 public Boolean call() throws Exception {
202 return getUsageForOrigin(mOrigin) == 0;
208 @Feature({"AndroidWebView", "WebStore"})
209 public void testGetResultsMatch() throws Exception {
212 poll(new Callable<Boolean>() {
214 public Boolean call() throws Exception {
215 return AwQuotaManagerBridgeTestUtil.getOrigins(
216 AwQuotaManagerBridgeTest.this).mOrigins.length > 0;
220 AwQuotaManagerBridge.Origins origins = AwQuotaManagerBridgeTestUtil.getOrigins(this);
221 assertEquals(origins.mOrigins.length, origins.mUsages.length);
222 assertEquals(origins.mOrigins.length, origins.mQuotas.length);
224 for (int i = 0; i < origins.mOrigins.length; ++i) {
225 assertEquals(origins.mUsages[i], getUsageForOrigin(origins.mOrigins[i]));
226 assertEquals(origins.mQuotas[i], getQuotaForOrigin(origins.mOrigins[i]));