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.
6 package org.xwalk.core.xwview.test;
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;
14 import java.util.concurrent.atomic.AtomicReference;
15 import java.util.concurrent.Callable;
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;
31 * Test suite for setAppCacheEnabled().
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;
39 static class ManifestTestHelper {
40 private final TestWebServer mWebServer;
41 private final String mHtmlPath;
42 private final String mHtmlUrl;
43 private final String mManifestPath;
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(
54 CommonResources.getContentTypeAndCacheHeaders("text/cache-manifest", false));
57 String getHtmlPath() {
65 String getManifestPath() {
69 int waitUntilHtmlIsRequested(final int initialRequestCount) throws InterruptedException {
70 return waitUntilResourceIsRequested(mHtmlPath, initialRequestCount);
73 int waitUntilManifestIsRequested(final int initialRequestCount)
74 throws InterruptedException {
75 return waitUntilResourceIsRequested(mManifestPath, initialRequestCount);
78 private int waitUntilResourceIsRequested(
79 final String path, final int initialRequestCount) throws InterruptedException {
80 boolean result = CriteriaHelper.pollForCriteria(new Criteria() {
82 public boolean isSatisfied() {
83 return mWebServer.getRequestCount(path) > initialRequestCount;
85 }, TEST_TIMEOUT, CHECK_INTERVAL);
87 return mWebServer.getRequestCount(path);
91 private XWalkSettings getXWalkSettings(final XWalkView view) {
92 getInstrumentation().runOnMainSync(new Runnable() {
95 mSettings = view.getSettings();
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);
117 final XWalkContent xWalkContent = getXWalkContentOnMainSync(xWalkView);
118 final XWalkSettings settings = getXWalkSettings(xWalkView);
119 settings.setJavaScriptEnabled(true);
120 settings.setAppCacheEnabled(false);
122 TestWebServer webServer = null;
124 webServer = new TestWebServer(false);
125 ManifestTestHelper helper = new ManifestTestHelper(
126 webServer, "testAppCache.html", "appcache.manifest");
127 loadUrlSyncByContent(
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.
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(
141 helper.getHtmlUrl());
142 helper.waitUntilManifestIsRequested(0);
144 if (webServer != null) webServer.shutdown();
150 * @Feature({"XWalkView", "Preferences", "AppCache"})
151 * This test is flaky but the root cause is not found yet. See crbug.com/171765.
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();
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);
171 TestWebServer webServer = null;
173 webServer = new TestWebServer(false);
174 ManifestTestHelper helper0 = new ManifestTestHelper(
175 webServer, "testAppCache_0.html", "appcache.manifest_0");
176 mContentClient = views.getClient0();
177 loadUrlSyncByContent(
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(
188 helper1.getHtmlUrl());
189 helper1.waitUntilManifestIsRequested(0);
190 settings1.setAppCacheEnabled(false);
191 mContentClient = views.getClient0();
192 loadUrlSyncByContent(
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(
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.
210 prevManifestRequestCount, webServer.getRequestCount(helper1.getManifestPath()));
212 if (webServer != null) webServer.shutdown();