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