Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / android_webview / javatests / src / org / chromium / android_webview / test / AwQuotaManagerBridgeTest.java
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.
4
5 package org.chromium.android_webview.test;
6
7 import android.test.suitebuilder.annotation.LargeTest;
8 import android.util.Pair;
9 import android.webkit.ValueCallback;
10
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;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.concurrent.Callable;
22
23 /**
24  * Tests for the AwQuotaManagerBridge.
25  */
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;
32
33     @Override
34     public void setUp() throws Exception {
35         super.setUp();
36         mContentsClient = new TestAwContentsClient();
37         mTestView = createAwTestContainerViewOnMainSync(mContentsClient);
38         mAwContents = mTestView.getAwContents();
39         mWebServer = TestWebServer.start();
40         mOrigin = mWebServer.getBaseUrl();
41
42         AwSettings settings = getAwSettingsOnUiThread(mAwContents);
43         settings.setJavaScriptEnabled(true);
44         settings.setDomStorageEnabled(true);
45         settings.setAppCacheEnabled(true);
46         settings.setAppCachePath("whatever");  // Enables AppCache.
47     }
48
49     @Override
50     public void tearDown() throws Exception {
51         deleteAllData();
52         if (mWebServer != null) {
53             mWebServer.shutdown();
54         }
55         super.tearDown();
56     }
57
58     private void deleteAllData() throws Exception {
59         final AwQuotaManagerBridge bridge =
60                 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
61         getInstrumentation().runOnMainSync(new Runnable() {
62             @Override
63             public void run() {
64                 bridge.deleteAllData();
65             }
66         });
67     }
68
69     private void deleteOrigin(final String origin) throws Exception {
70         final AwQuotaManagerBridge bridge =
71                 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
72         getInstrumentation().runOnMainSync(new Runnable() {
73             @Override
74             public void run() {
75                 bridge.deleteOrigin(origin);
76             }
77         });
78     }
79
80     private static class LongValueCallbackHelper extends CallbackHelper {
81         private long mValue;
82
83         public void notifyCalled(long value) {
84             mValue = value;
85             notifyCalled();
86         }
87
88         public long getValue() {
89             assert getCallCount() > 0;
90             return mValue;
91         }
92     }
93
94     private long getQuotaForOrigin(final String origin) throws Exception {
95         final LongValueCallbackHelper callbackHelper = new LongValueCallbackHelper();
96         final AwQuotaManagerBridge bridge =
97                 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
98
99         int callCount = callbackHelper.getCallCount();
100         getInstrumentation().runOnMainSync(new Runnable() {
101             @Override
102             public void run() {
103                 bridge.getQuotaForOrigin("foo.com",
104                         new ValueCallback<Long>() {
105                             @Override
106                             public void onReceiveValue(Long quota) {
107                                 callbackHelper.notifyCalled(quota);
108                             }
109                         });
110             }
111         });
112         callbackHelper.waitForCallback(callCount);
113
114         return callbackHelper.getValue();
115     }
116
117     private long getUsageForOrigin(final String origin) throws Exception {
118         final LongValueCallbackHelper callbackHelper = new LongValueCallbackHelper();
119         final AwQuotaManagerBridge bridge =
120                 AwQuotaManagerBridgeTestUtil.getQuotaManagerBridge(this);
121
122         int callCount = callbackHelper.getCallCount();
123         getInstrumentation().runOnMainSync(new Runnable() {
124             @Override
125             public void run() {
126                 bridge.getUsageForOrigin(origin,
127                         new ValueCallback<Long>() {
128                             @Override
129                             public void onReceiveValue(Long usage) {
130                                 callbackHelper.notifyCalled(usage);
131                             }
132                         });
133             }
134         });
135         callbackHelper.waitForCallback(callCount);
136
137         return callbackHelper.getValue();
138     }
139
140     private void useAppCache() throws Exception {
141         final String cachedFilePath = "/foo.js";
142         final String cachedFileContents = "1 + 1;";
143         mWebServer.setResponse(cachedFilePath, cachedFileContents, null);
144
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);
150
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);
155
156         loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
157         executeJavaScriptAndWaitForResult(mAwContents, mContentsClient,
158                 "window.applicationCache.update();");
159     }
160
161     @LargeTest
162     @Feature({"AndroidWebView", "WebStore"})
163     public void testDeleteAllWithAppCache() throws Exception {
164         final long initialUsage = getUsageForOrigin(mOrigin);
165
166         useAppCache();
167         poll(new Callable<Boolean>() {
168             @Override
169             public Boolean call() throws Exception {
170                 return getUsageForOrigin(mOrigin) > initialUsage;
171             }
172         });
173
174         deleteAllData();
175         poll(new Callable<Boolean>() {
176             @Override
177             public Boolean call() throws Exception {
178                 return getUsageForOrigin(mOrigin) == 0;
179             }
180         });
181     }
182
183     @LargeTest
184     @Feature({"AndroidWebView", "WebStore"})
185     public void testDeleteOriginWithAppCache() throws Exception {
186         final long initialUsage = getUsageForOrigin(mOrigin);
187
188         useAppCache();
189         poll(new Callable<Boolean>() {
190             @Override
191             public Boolean call() throws Exception {
192                 return getUsageForOrigin(mOrigin) > initialUsage;
193             }
194         });
195
196         deleteOrigin(mOrigin);
197         poll(new Callable<Boolean>() {
198             @Override
199             public Boolean call() throws Exception {
200                 return getUsageForOrigin(mOrigin) == 0;
201             }
202         });
203     }
204
205     @LargeTest
206     @Feature({"AndroidWebView", "WebStore"})
207     public void testGetResultsMatch() throws Exception {
208         useAppCache();
209
210         poll(new Callable<Boolean>() {
211             @Override
212             public Boolean call() throws Exception {
213                 return AwQuotaManagerBridgeTestUtil.getOrigins(
214                             AwQuotaManagerBridgeTest.this).mOrigins.length > 0;
215             }
216         });
217
218         AwQuotaManagerBridge.Origins origins = AwQuotaManagerBridgeTestUtil.getOrigins(this);
219         assertEquals(origins.mOrigins.length, origins.mUsages.length);
220         assertEquals(origins.mOrigins.length, origins.mQuotas.length);
221
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]));
225         }
226     }
227 }