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