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);
112 callbackHelper.waitForCallback(callCount);
114 return callbackHelper.getValue();
117 private long getUsageForOrigin(final String origin) throws Exception {
118 final LongValueCallbackHelper callbackHelper = new LongValueCallbackHelper();
119 final AwQuotaManagerBridge bridge =
120 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
122 int callCount = callbackHelper.getCallCount();
123 getInstrumentation().runOnMainSync(new Runnable() {
126 bridge.getUsageForOrigin(origin,
127 new ValueCallback<Long>() {
129 public void onReceiveValue(Long usage) {
130 callbackHelper.notifyCalled(usage);
135 callbackHelper.waitForCallback(callCount);
137 return callbackHelper.getValue();
140 private void useAppCache() throws Exception {
141 final String cachedFilePath = "/foo.js";
142 final String cachedFileContents = "1 + 1;";
143 mWebServer.setResponse(cachedFilePath, cachedFileContents, null);
145 final String manifestPath = "/foo.manifest";
146 final String manifestContents = "CACHE MANIFEST\nCACHE:\n" + cachedFilePath;
147 List<Pair<String, String>> manifestHeaders = new ArrayList<Pair<String, String>>();
148 manifestHeaders.add(Pair.create("Content-Disposition", "text/cache-manifest"));
149 mWebServer.setResponse(manifestPath, manifestContents, manifestHeaders);
151 final String pagePath = "/appcache.html";
152 final String pageContents = "<html manifest=\"" + manifestPath + "\">" +
153 "<head><script src=\"" + cachedFilePath + "\"></script></head></html>";
154 String url = mWebServer.setResponse(pagePath, pageContents, null);
156 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
157 executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
158 "window.applicationCache.update();");
162 @Feature({"AndroidWebView", "WebStore"})
163 public void testDeleteAllWithAppCache() throws Exception {
164 final long initialUsage = getUsageForOrigin(mOrigin);
167 poll(new Callable<Boolean>() {
169 public Boolean call() throws Exception {
170 return getUsageForOrigin(mOrigin) > initialUsage;
175 poll(new Callable<Boolean>() {
177 public Boolean call() throws Exception {
178 return getUsageForOrigin(mOrigin) == 0;
184 @Feature({"AndroidWebView", "WebStore"})
185 public void testDeleteOriginWithAppCache() throws Exception {
186 final long initialUsage = getUsageForOrigin(mOrigin);
189 poll(new Callable<Boolean>() {
191 public Boolean call() throws Exception {
192 return getUsageForOrigin(mOrigin) > initialUsage;
196 deleteOrigin(mOrigin);
197 poll(new Callable<Boolean>() {
199 public Boolean call() throws Exception {
200 return getUsageForOrigin(mOrigin) == 0;
206 @Feature({"AndroidWebView", "WebStore"})
207 public void testGetResultsMatch() throws Exception {
210 poll(new Callable<Boolean>() {
212 public Boolean call() throws Exception {
213 return AwQuotaManagerBridgeTestUtil.getOrigins(
214 AwQuotaManagerBridgeTest.this).mOrigins.length > 0;
218 AwQuotaManagerBridge.Origins origins = AwQuotaManagerBridgeTestUtil.getOrigins(this);
219 assertEquals(origins.mOrigins.length, origins.mUsages.length);
220 assertEquals(origins.mOrigins.length, origins.mQuotas.length);
222 for (int i = 0; i < origins.mOrigins.length; ++i) {
223 assertEquals(origins.mUsages[i], getUsageForOrigin(origins.mOrigins[i]));
224 assertEquals(origins.mQuotas[i], getQuotaForOrigin(origins.mOrigins[i]));