Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / xwalk / test / android / core / javatests / src / org / xwalk / core / xwview / test / SetAppCacheEnabledTest.java
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright (c) 2013 Intel Corporation. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 package org.xwalk.core.xwview.test;
7
8 import android.graphics.Bitmap;
9 import android.test.suitebuilder.annotation.SmallTest;
10 import android.util.Pair;
11 import android.webkit.ValueCallback;
12 import android.webkit.WebResourceResponse;
13
14 import java.util.concurrent.atomic.AtomicReference;
15 import java.util.concurrent.Callable;
16
17 import org.chromium.base.test.util.DisabledTest;
18 import org.chromium.base.test.util.Feature;
19 import org.chromium.content.browser.test.util.CallbackHelper;
20 import org.chromium.content.browser.test.util.Criteria;
21 import org.chromium.content.browser.test.util.CriteriaHelper;
22 import org.chromium.net.test.util.TestWebServer;
23 import org.xwalk.core.XWalkClient;
24 import org.xwalk.core.XWalkContent;
25 import org.xwalk.core.XWalkSettings;
26 import org.xwalk.core.XWalkView;
27 import org.xwalk.core.XWalkWebChromeClient;
28 import org.xwalk.core.xwview.test.util.CommonResources;
29
30 /**
31  * Test suite for setAppCacheEnabled().
32  */
33 public class SetAppCacheEnabledTest extends XWalkViewTestBase {
34     private static final long TEST_TIMEOUT = 20000L;
35     private static final int CHECK_INTERVAL = 100;
36     private TestHelperBridge mContentClient;
37     private XWalkSettings mSettings;
38
39     static class ManifestTestHelper {
40         private final TestWebServer mWebServer;
41         private final String mHtmlPath;
42         private final String mHtmlUrl;
43         private final String mManifestPath;
44
45         ManifestTestHelper(TestWebServer webServer, String htmlPageName, String manifestName) {
46             mWebServer = webServer;
47             mHtmlPath = "/" + htmlPageName;
48             mHtmlUrl = webServer.setResponse(
49                     mHtmlPath, "<html manifest=\"" + manifestName + "\"></html>", null);
50             mManifestPath = "/" + manifestName;
51             webServer.setResponse(
52                     mManifestPath,
53                     "CACHE MANIFEST",
54                     CommonResources.getContentTypeAndCacheHeaders("text/cache-manifest", false));
55         }
56
57         String getHtmlPath() {
58             return mHtmlPath;
59         }
60
61         String getHtmlUrl() {
62             return mHtmlUrl;
63         }
64
65         String getManifestPath() {
66             return mManifestPath;
67         }
68
69         int waitUntilHtmlIsRequested(final int initialRequestCount) throws InterruptedException {
70             return waitUntilResourceIsRequested(mHtmlPath, initialRequestCount);
71         }
72
73         int waitUntilManifestIsRequested(final int initialRequestCount)
74                 throws InterruptedException {
75             return waitUntilResourceIsRequested(mManifestPath, initialRequestCount);
76         }
77
78         private int waitUntilResourceIsRequested(
79                 final String path, final int initialRequestCount) throws InterruptedException {
80             boolean result = CriteriaHelper.pollForCriteria(new Criteria() {
81                 @Override
82                 public boolean isSatisfied() {
83                     return mWebServer.getRequestCount(path) > initialRequestCount;
84                 }
85             }, TEST_TIMEOUT, CHECK_INTERVAL);
86             assertTrue(result);
87             return mWebServer.getRequestCount(path);
88         }
89     }
90
91     private XWalkSettings getXWalkSettings(final XWalkView view) {
92         getInstrumentation().runOnMainSync(new Runnable() {
93             @Override
94             public void run() {
95                 mSettings = view.getSettings();
96             }
97         });
98         return mSettings;
99     }
100
101     @SmallTest
102     @Feature({"XWalkView", "Preferences", "AppCache"})
103     public void testAppCache() throws Throwable {
104         final TestHelperBridge helperBridge =
105                 new TestHelperBridge();
106         mContentClient = helperBridge;
107         final XWalkViewTestBase.TestXWalkClientBase client =
108                 new XWalkViewTestBase.TestXWalkClientBase(helperBridge);
109         final XWalkViewTestBase.TestXWalkResourceClientBase resourceClient =
110                 new XWalkViewTestBase.TestXWalkResourceClientBase(helperBridge);
111         final XWalkViewTestBase.TestXWalkWebChromeClientBase chromeClient =
112                 new XWalkViewTestBase.TestXWalkWebChromeClientBase(helperBridge);
113         final XWalkView xWalkView =
114                 createXWalkViewContainerOnMainSync(getActivity(), client,
115                         resourceClient, chromeClient);
116
117         final XWalkContent xWalkContent = getXWalkContentOnMainSync(xWalkView);
118         final XWalkSettings settings = getXWalkSettings(xWalkView);
119         settings.setJavaScriptEnabled(true);
120         settings.setAppCacheEnabled(false);
121
122         TestWebServer webServer = null;
123         try {
124             webServer = new TestWebServer(false);
125             ManifestTestHelper helper = new ManifestTestHelper(
126                     webServer, "testAppCache.html", "appcache.manifest");
127             loadUrlSyncByContent(
128                     xWalkContent,
129                     mContentClient,
130                     helper.getHtmlUrl());
131             helper.waitUntilHtmlIsRequested(0);
132             // Unfortunately, there is no other good way of verifying that AppCache is
133             // disabled, other than checking that it didn't try to fetch the manifest.
134             Thread.sleep(1000);
135             assertEquals(0, webServer.getRequestCount(helper.getManifestPath()));
136             // Enables AppCache. Use the default path if app cache path isn't set.
137             settings.setAppCacheEnabled(true);
138             loadUrlSyncByContent(
139                     xWalkContent,
140                     mContentClient,
141                     helper.getHtmlUrl());
142             helper.waitUntilManifestIsRequested(0);
143         } finally {
144             if (webServer != null) webServer.shutdown();
145         }
146     }
147
148     /*
149      * @SmallTest
150      * @Feature({"XWalkView", "Preferences", "AppCache"})
151      * This test is flaky but the root cause is not found yet. See crbug.com/171765.
152      */
153     @DisabledTest
154     public void testAppCacheWithTwoViews() throws Throwable {
155         // We don't use the test helper here, because making sure that AppCache
156         // is disabled takes a lot of time, so running through the usual drill
157         // will take about 20 seconds.
158         ViewPair views = createViews();
159
160         XWalkSettings settings0 = getXWalkSettingsOnUiThreadByContent(
161                 views.getContent0());
162         settings0.setJavaScriptEnabled(true);
163         settings0.setAppCachePath("whatever");
164         settings0.setAppCacheEnabled(true);
165         XWalkSettings settings1 = getXWalkSettingsOnUiThreadByContent(
166                 views.getContent1());
167         settings1.setJavaScriptEnabled(true);
168         // AppCachePath setting is global, no need to set it for the second view.
169         settings1.setAppCacheEnabled(true);
170
171         TestWebServer webServer = null;
172         try {
173             webServer = new TestWebServer(false);
174             ManifestTestHelper helper0 = new ManifestTestHelper(
175                     webServer, "testAppCache_0.html", "appcache.manifest_0");
176             mContentClient = views.getClient0();
177             loadUrlSyncByContent(
178                     views.getContent0(),
179                     mContentClient,
180                     helper0.getHtmlUrl());
181             int manifestRequests0 = helper0.waitUntilManifestIsRequested(0);
182             ManifestTestHelper helper1 = new ManifestTestHelper(
183                     webServer, "testAppCache_1.html", "appcache.manifest_1");
184             mContentClient = views.getClient1();
185             loadUrlSyncByContent(
186                     views.getContent1(),
187                     mContentClient,
188                     helper1.getHtmlUrl());
189             helper1.waitUntilManifestIsRequested(0);
190             settings1.setAppCacheEnabled(false);
191             mContentClient = views.getClient0();
192             loadUrlSyncByContent(
193                     views.getContent0(),
194                     mContentClient,
195                     helper0.getHtmlUrl());
196             helper0.waitUntilManifestIsRequested(manifestRequests0);
197             final int prevManifestRequestCount =
198                     webServer.getRequestCount(helper1.getManifestPath());
199             int htmlRequests1 = webServer.getRequestCount(helper1.getHtmlPath());
200             mContentClient = views.getClient1();
201             loadUrlSyncByContent(
202                     views.getContent1(),
203                     mContentClient,
204                     helper1.getHtmlUrl());
205             helper1.waitUntilHtmlIsRequested(htmlRequests1);
206             // Unfortunately, there is no other good way of verifying that AppCache is
207             // disabled, other than checking that it didn't try to fetch the manifest.
208             Thread.sleep(1000);
209             assertEquals(
210                     prevManifestRequestCount, webServer.getRequestCount(helper1.getManifestPath()));
211         } finally {
212             if (webServer != null) webServer.shutdown();
213         }
214     }
215 }