1 // Copyright 2012 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.
5 package org.chromium.android_webview.test;
7 import android.content.Context;
8 import android.graphics.Point;
9 import android.os.Build;
10 import android.os.SystemClock;
11 import android.test.suitebuilder.annotation.MediumTest;
12 import android.test.suitebuilder.annotation.SmallTest;
13 import android.view.WindowManager;
14 import android.webkit.JavascriptInterface;
15 import android.webkit.WebSettings;
17 import org.apache.http.Header;
18 import org.apache.http.HttpRequest;
19 import org.chromium.android_webview.AwContents;
20 import org.chromium.android_webview.AwSettings;
21 import org.chromium.android_webview.AwSettings.LayoutAlgorithm;
22 import org.chromium.android_webview.InterceptedRequestData;
23 import org.chromium.android_webview.test.util.CommonResources;
24 import org.chromium.android_webview.test.util.ImagePageGenerator;
25 import org.chromium.android_webview.test.util.JavascriptEventObserver;
26 import org.chromium.android_webview.test.util.VideoTestWebServer;
27 import org.chromium.base.test.util.DisabledTest;
28 import org.chromium.base.test.util.Feature;
29 import org.chromium.base.test.util.TestFileUtil;
30 import org.chromium.base.test.util.UrlUtils;
31 import org.chromium.content.browser.ContentViewCore;
32 import org.chromium.content.browser.test.util.CallbackHelper;
33 import org.chromium.content.browser.test.util.HistoryUtils;
34 import org.chromium.net.test.util.TestWebServer;
35 import org.chromium.ui.gfx.DeviceDisplayInfo;
38 import java.util.concurrent.Callable;
39 import java.util.concurrent.TimeUnit;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
44 * A test suite for AwSettings class. The key objective is to verify that each
45 * settings applies either to each individual view or to all views of the
48 public class AwSettingsTest extends AwTestBase {
49 private static final boolean ENABLED = true;
50 private static final boolean DISABLED = false;
53 * A helper class for testing a particular preference from AwSettings.
54 * The generic type T is the type of the setting. Usually, to test an
55 * effect of the preference, JS code is executed that sets document's title.
56 * In this case, requiresJsEnabled constructor argument must be set to true.
58 abstract class AwSettingsTestHelper<T> {
59 protected final AwContents mAwContents;
60 protected final TestAwContentsClient mContentViewClient;
61 protected final AwSettings mAwSettings;
63 AwSettingsTestHelper(AwContents awContents,
64 TestAwContentsClient contentViewClient,
65 boolean requiresJsEnabled) throws Throwable {
66 mAwContents = awContents;
67 mContentViewClient = contentViewClient;
68 mAwSettings = AwSettingsTest.this.getAwSettingsOnUiThread(awContents);
69 if (requiresJsEnabled) {
70 mAwSettings.setJavaScriptEnabled(true);
74 void ensureSettingHasAlteredValue() throws Throwable {
75 ensureSettingHasValue(getAlteredValue());
78 void ensureSettingHasInitialValue() throws Throwable {
79 ensureSettingHasValue(getInitialValue());
82 void setAlteredSettingValue() throws Throwable {
83 setCurrentValue(getAlteredValue());
86 void setInitialSettingValue() throws Throwable {
87 setCurrentValue(getInitialValue());
90 protected abstract T getAlteredValue();
92 protected abstract T getInitialValue();
94 protected abstract T getCurrentValue();
96 protected abstract void setCurrentValue(T value) throws Throwable;
98 protected abstract void doEnsureSettingHasValue(T value) throws Throwable;
100 protected String getTitleOnUiThread() throws Exception {
101 return AwSettingsTest.this.getTitleOnUiThread(mAwContents);
104 protected void loadDataSync(String data) throws Throwable {
105 AwSettingsTest.this.loadDataSync(
107 mContentViewClient.getOnPageFinishedHelper(),
113 protected void loadUrlSync(String url) throws Throwable {
114 AwSettingsTest.this.loadUrlSync(
116 mContentViewClient.getOnPageFinishedHelper(),
120 protected void loadUrlSyncAndExpectError(String url) throws Throwable {
121 AwSettingsTest.this.loadUrlSyncAndExpectError(
123 mContentViewClient.getOnPageFinishedHelper(),
124 mContentViewClient.getOnReceivedErrorHelper(),
128 protected String executeJavaScriptAndWaitForResult(String script) throws Exception {
129 return AwSettingsTest.this.executeJavaScriptAndWaitForResult(
130 mAwContents, mContentViewClient, script);
133 private void ensureSettingHasValue(T value) throws Throwable {
134 assertEquals(value, getCurrentValue());
135 doEnsureSettingHasValue(value);
139 class AwSettingsJavaScriptTestHelper extends AwSettingsTestHelper<Boolean> {
140 private static final String JS_ENABLED_STRING = "JS Enabled";
141 private static final String JS_DISABLED_STRING = "JS Disabled";
143 AwSettingsJavaScriptTestHelper(AwContents awContents,
144 TestAwContentsClient contentViewClient) throws Throwable {
145 super(awContents, contentViewClient, false);
149 protected Boolean getAlteredValue() {
154 protected Boolean getInitialValue() {
159 protected Boolean getCurrentValue() {
160 return mAwSettings.getJavaScriptEnabled();
164 protected void setCurrentValue(Boolean value) {
165 mAwSettings.setJavaScriptEnabled(value);
169 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
170 loadDataSync(getData());
172 value == ENABLED ? JS_ENABLED_STRING : JS_DISABLED_STRING,
173 getTitleOnUiThread());
176 private String getData() {
177 return "<html><head><title>" + JS_DISABLED_STRING + "</title>"
178 + "</head><body onload=\"document.title='" + JS_ENABLED_STRING
179 + "';\"></body></html>";
183 // In contrast to AwSettingsJavaScriptTestHelper, doesn't reload the page when testing
185 class AwSettingsJavaScriptDynamicTestHelper extends AwSettingsJavaScriptTestHelper {
186 AwSettingsJavaScriptDynamicTestHelper(
187 AwContents awContents,
188 TestAwContentsClient contentViewClient) throws Throwable {
189 super(awContents, contentViewClient);
191 super.doEnsureSettingHasValue(getInitialValue());
195 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
196 String oldTitle = getTitleOnUiThread();
197 String newTitle = oldTitle + "_modified";
198 executeJavaScriptAndWaitForResult(getScript(newTitle));
199 assertEquals(value == ENABLED ? newTitle : oldTitle, getTitleOnUiThread());
202 private String getScript(String title) {
203 return "document.title='" + title + "';";
207 class AwSettingsPluginsTestHelper extends AwSettingsTestHelper<Boolean> {
208 private static final String PLUGINS_ENABLED_STRING = "Embed";
209 private static final String PLUGINS_DISABLED_STRING = "NoEmbed";
211 AwSettingsPluginsTestHelper(AwContents awContents,
212 TestAwContentsClient contentViewClient) throws Throwable {
213 super(awContents, contentViewClient, true);
217 protected Boolean getAlteredValue() {
222 protected Boolean getInitialValue() {
227 protected Boolean getCurrentValue() {
228 return mAwSettings.getPluginsEnabled();
232 protected void setCurrentValue(Boolean value) {
233 mAwSettings.setPluginsEnabled(value);
237 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
238 loadDataSync(getData());
240 value == ENABLED ? PLUGINS_ENABLED_STRING : PLUGINS_DISABLED_STRING,
241 getTitleOnUiThread());
244 private String getData() {
245 return "<html><body onload=\"document.title = document.body.innerText;\">"
246 + "<noembed>No</noembed><span>Embed</span></body></html>";
250 class AwSettingsStandardFontFamilyTestHelper extends AwSettingsTestHelper<String> {
251 AwSettingsStandardFontFamilyTestHelper(
252 AwContents awContents,
253 TestAwContentsClient contentViewClient) throws Throwable {
254 super(awContents, contentViewClient, true);
258 protected String getAlteredValue() {
263 protected String getInitialValue() {
268 protected String getCurrentValue() {
269 return mAwSettings.getStandardFontFamily();
273 protected void setCurrentValue(String value) {
274 mAwSettings.setStandardFontFamily(value);
278 protected void doEnsureSettingHasValue(String value) throws Throwable {
279 loadDataSync(getData());
280 assertEquals(value, getTitleOnUiThread());
283 private String getData() {
284 return "<html><body onload=\"document.title = " +
285 "getComputedStyle(document.body).getPropertyValue('font-family');\">"
290 class AwSettingsDefaultFontSizeTestHelper extends AwSettingsTestHelper<Integer> {
291 AwSettingsDefaultFontSizeTestHelper(
292 AwContents awContents,
293 TestAwContentsClient contentViewClient) throws Throwable {
294 super(awContents, contentViewClient, true);
298 protected Integer getAlteredValue() {
303 protected Integer getInitialValue() {
308 protected Integer getCurrentValue() {
309 return mAwSettings.getDefaultFontSize();
313 protected void setCurrentValue(Integer value) {
314 mAwSettings.setDefaultFontSize(value);
318 protected void doEnsureSettingHasValue(Integer value) throws Throwable {
319 loadDataSync(getData());
320 assertEquals(value.toString() + "px", getTitleOnUiThread());
323 private String getData() {
324 return "<html><body onload=\"document.title = " +
325 "getComputedStyle(document.body).getPropertyValue('font-size');\">"
330 class AwSettingsLoadImagesAutomaticallyTestHelper extends AwSettingsTestHelper<Boolean> {
331 private ImagePageGenerator mGenerator;
333 AwSettingsLoadImagesAutomaticallyTestHelper(
334 AwContents awContents,
335 TestAwContentsClient contentViewClient,
336 ImagePageGenerator generator) throws Throwable {
337 super(awContents, contentViewClient, true);
338 mGenerator = generator;
342 protected Boolean getAlteredValue() {
347 protected Boolean getInitialValue() {
352 protected Boolean getCurrentValue() {
353 return mAwSettings.getLoadsImagesAutomatically();
357 protected void setCurrentValue(Boolean value) {
358 mAwSettings.setLoadsImagesAutomatically(value);
362 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
363 loadDataSync(mGenerator.getPageSource());
364 assertEquals(value == ENABLED ?
365 ImagePageGenerator.IMAGE_LOADED_STRING :
366 ImagePageGenerator.IMAGE_NOT_LOADED_STRING,
367 getTitleOnUiThread());
372 class AwSettingsImagesEnabledHelper extends AwSettingsTestHelper<Boolean> {
374 AwSettingsImagesEnabledHelper(
375 AwContents awContents,
376 TestAwContentsClient contentViewClient,
377 TestWebServer webServer,
378 ImagePageGenerator generator) throws Throwable {
379 super(awContents, contentViewClient, true);
380 mWebServer = webServer;
381 mGenerator = generator;
385 protected Boolean getAlteredValue() {
390 protected Boolean getInitialValue() {
395 protected Boolean getCurrentValue() {
396 return mAwSettings.getImagesEnabled();
400 protected void setCurrentValue(Boolean value) {
401 mAwSettings.setImagesEnabled(value);
405 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
406 final String httpImageUrl = mGenerator.getPageUrl(mWebServer);
407 AwSettingsTest.this.loadUrlSync(
409 mContentViewClient.getOnPageFinishedHelper(),
411 assertEquals(value == ENABLED ?
412 ImagePageGenerator.IMAGE_LOADED_STRING :
413 ImagePageGenerator.IMAGE_NOT_LOADED_STRING,
414 getTitleOnUiThread());
417 private TestWebServer mWebServer;
418 private ImagePageGenerator mGenerator;
421 class AwSettingsDefaultTextEncodingTestHelper extends AwSettingsTestHelper<String> {
422 AwSettingsDefaultTextEncodingTestHelper(
423 AwContents awContents,
424 TestAwContentsClient contentViewClient) throws Throwable {
425 super(awContents, contentViewClient, true);
429 protected String getAlteredValue() {
434 protected String getInitialValue() {
439 protected String getCurrentValue() {
440 return mAwSettings.getDefaultTextEncodingName();
444 protected void setCurrentValue(String value) {
445 mAwSettings.setDefaultTextEncodingName(value);
449 protected void doEnsureSettingHasValue(String value) throws Throwable {
450 loadDataSync(getData());
451 assertEquals(value, getTitleOnUiThread());
454 private String getData() {
455 return "<html><body onload='document.title=document.defaultCharset'></body></html>";
459 class AwSettingsUserAgentStringTestHelper extends AwSettingsTestHelper<String> {
460 private final String mDefaultUa;
461 private static final String DEFAULT_UA = "";
462 private static final String CUSTOM_UA = "ChromeViewTest";
464 AwSettingsUserAgentStringTestHelper(
465 AwContents awContents,
466 TestAwContentsClient contentViewClient) throws Throwable {
467 super(awContents, contentViewClient, true);
468 mDefaultUa = mAwSettings.getUserAgentString();
472 protected String getAlteredValue() {
477 protected String getInitialValue() {
482 protected String getCurrentValue() {
483 // The test framework expects that getXXX() == Z after setXXX(Z).
484 // But setUserAgentString("" / null) resets the UA string to default,
485 // and getUserAgentString returns the default UA string afterwards.
486 // To align with the framework, we return an empty string instead of
488 String currentUa = mAwSettings.getUserAgentString();
489 return mDefaultUa.equals(currentUa) ? DEFAULT_UA : currentUa;
493 protected void setCurrentValue(String value) {
494 mAwSettings.setUserAgentString(value);
498 protected void doEnsureSettingHasValue(String value) throws Throwable {
499 loadDataSync(getData());
501 DEFAULT_UA.equals(value) ? mDefaultUa : value,
502 getTitleOnUiThread());
505 private String getData() {
506 return "<html><body onload='document.title=navigator.userAgent'></body></html>";
510 class AwSettingsDomStorageEnabledTestHelper extends AwSettingsTestHelper<Boolean> {
511 private static final String TEST_FILE = "webview/localStorage.html";
512 private static final String NO_LOCAL_STORAGE = "No localStorage";
513 private static final String HAS_LOCAL_STORAGE = "Has localStorage";
515 AwSettingsDomStorageEnabledTestHelper(
516 AwContents awContents,
517 TestAwContentsClient contentViewClient) throws Throwable {
518 super(awContents, contentViewClient, true);
519 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_FILE));
523 protected Boolean getAlteredValue() {
528 protected Boolean getInitialValue() {
533 protected Boolean getCurrentValue() {
534 return mAwSettings.getDomStorageEnabled();
538 protected void setCurrentValue(Boolean value) {
539 mAwSettings.setDomStorageEnabled(value);
543 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
544 // It is not permitted to access localStorage from data URLs in WebKit,
545 // that is why a standalone page must be used.
546 loadUrlSync(UrlUtils.getTestFileUrl(TEST_FILE));
548 value == ENABLED ? HAS_LOCAL_STORAGE : NO_LOCAL_STORAGE,
549 getTitleOnUiThread());
553 class AwSettingsDatabaseTestHelper extends AwSettingsTestHelper<Boolean> {
554 private static final String TEST_FILE = "webview/database_access.html";
555 private static final String NO_DATABASE = "No database";
556 private static final String HAS_DATABASE = "Has database";
558 AwSettingsDatabaseTestHelper(
559 AwContents awContents,
560 TestAwContentsClient contentViewClient) throws Throwable {
561 super(awContents, contentViewClient, true);
562 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_FILE));
566 protected Boolean getAlteredValue() {
571 protected Boolean getInitialValue() {
576 protected Boolean getCurrentValue() {
577 return mAwSettings.getDatabaseEnabled();
581 protected void setCurrentValue(Boolean value) {
582 mAwSettings.setDatabaseEnabled(value);
586 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
587 // It seems accessing the database through a data scheme is not
588 // supported, and fails with a DOM exception (likely a cross-domain
590 loadUrlSync(UrlUtils.getTestFileUrl(TEST_FILE));
592 value == ENABLED ? HAS_DATABASE : NO_DATABASE,
593 getTitleOnUiThread());
597 class AwSettingsUniversalAccessFromFilesTestHelper extends AwSettingsTestHelper<Boolean> {
598 private static final String TEST_CONTAINER_FILE = "webview/iframe_access.html";
599 private static final String TEST_FILE = "webview/hello_world.html";
600 private static final String ACCESS_DENIED_TITLE = "Exception";
602 AwSettingsUniversalAccessFromFilesTestHelper(
603 AwContents awContents,
604 TestAwContentsClient contentViewClient) throws Throwable {
605 super(awContents, contentViewClient, true);
606 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_CONTAINER_FILE));
607 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_FILE));
608 mIframeContainerUrl = UrlUtils.getTestFileUrl(TEST_CONTAINER_FILE);
609 mIframeUrl = UrlUtils.getTestFileUrl(TEST_FILE);
610 // The value of the setting depends on the SDK version.
611 mAwSettings.setAllowUniversalAccessFromFileURLs(false);
612 // If universal access is true, the value of file access doesn't
613 // matter. While if universal access is false, having file access
614 // enabled will allow file loading.
615 mAwSettings.setAllowFileAccessFromFileURLs(false);
619 protected Boolean getAlteredValue() {
624 protected Boolean getInitialValue() {
629 protected Boolean getCurrentValue() {
630 return mAwSettings.getAllowUniversalAccessFromFileURLs();
634 protected void setCurrentValue(Boolean value) {
635 mAwSettings.setAllowUniversalAccessFromFileURLs(value);
639 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
640 loadUrlSync(mIframeContainerUrl);
642 value == ENABLED ? mIframeUrl : ACCESS_DENIED_TITLE,
643 getTitleOnUiThread());
646 private final String mIframeContainerUrl;
647 private final String mIframeUrl;
650 class AwSettingsFileAccessFromFilesIframeTestHelper extends AwSettingsTestHelper<Boolean> {
651 private static final String TEST_CONTAINER_FILE = "webview/iframe_access.html";
652 private static final String TEST_FILE = "webview/hello_world.html";
653 private static final String ACCESS_DENIED_TITLE = "Exception";
655 AwSettingsFileAccessFromFilesIframeTestHelper(
656 AwContents awContents,
657 TestAwContentsClient contentViewClient) throws Throwable {
658 super(awContents, contentViewClient, true);
659 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_CONTAINER_FILE));
660 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_FILE));
661 mIframeContainerUrl = UrlUtils.getTestFileUrl(TEST_CONTAINER_FILE);
662 mIframeUrl = UrlUtils.getTestFileUrl(TEST_FILE);
663 mAwSettings.setAllowUniversalAccessFromFileURLs(false);
664 // The value of the setting depends on the SDK version.
665 mAwSettings.setAllowFileAccessFromFileURLs(false);
669 protected Boolean getAlteredValue() {
674 protected Boolean getInitialValue() {
679 protected Boolean getCurrentValue() {
680 return mAwSettings.getAllowFileAccessFromFileURLs();
684 protected void setCurrentValue(Boolean value) {
685 mAwSettings.setAllowFileAccessFromFileURLs(value);
689 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
690 loadUrlSync(mIframeContainerUrl);
692 value == ENABLED ? mIframeUrl : ACCESS_DENIED_TITLE,
693 getTitleOnUiThread());
696 private final String mIframeContainerUrl;
697 private final String mIframeUrl;
700 class AwSettingsFileAccessFromFilesXhrTestHelper extends AwSettingsTestHelper<Boolean> {
701 private static final String TEST_FILE = "webview/xhr_access.html";
702 private static final String ACCESS_GRANTED_TITLE = "Hello, World!";
703 private static final String ACCESS_DENIED_TITLE = "Exception";
705 AwSettingsFileAccessFromFilesXhrTestHelper(
706 AwContents awContents,
707 TestAwContentsClient contentViewClient) throws Throwable {
708 super(awContents, contentViewClient, true);
709 assertFileIsReadable(UrlUtils.getTestFilePath(TEST_FILE));
710 mXhrContainerUrl = UrlUtils.getTestFileUrl(TEST_FILE);
711 mAwSettings.setAllowUniversalAccessFromFileURLs(false);
712 // The value of the setting depends on the SDK version.
713 mAwSettings.setAllowFileAccessFromFileURLs(false);
717 protected Boolean getAlteredValue() {
722 protected Boolean getInitialValue() {
727 protected Boolean getCurrentValue() {
728 return mAwSettings.getAllowFileAccessFromFileURLs();
732 protected void setCurrentValue(Boolean value) {
733 mAwSettings.setAllowFileAccessFromFileURLs(value);
737 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
738 loadUrlSync(mXhrContainerUrl);
740 value == ENABLED ? ACCESS_GRANTED_TITLE : ACCESS_DENIED_TITLE,
741 getTitleOnUiThread());
744 private final String mXhrContainerUrl;
747 class AwSettingsFileUrlAccessTestHelper extends AwSettingsTestHelper<Boolean> {
748 private static final String TEST_FILE = "webview/hello_world.html";
749 private static final String ACCESS_GRANTED_TITLE = "Hello, World!";
751 AwSettingsFileUrlAccessTestHelper(
752 AwContents awContents,
753 TestAwContentsClient contentViewClient,
754 int startIndex) throws Throwable {
755 super(awContents, contentViewClient, true);
757 AwSettingsTest.assertFileIsReadable(UrlUtils.getTestFilePath(TEST_FILE));
761 protected Boolean getAlteredValue() {
766 protected Boolean getInitialValue() {
771 protected Boolean getCurrentValue() {
772 return mAwSettings.getAllowFileAccess();
776 protected void setCurrentValue(Boolean value) {
777 mAwSettings.setAllowFileAccess(value);
781 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
782 // Use query parameters to avoid hitting a cached page.
783 String fileUrl = UrlUtils.getTestFileUrl(TEST_FILE + "?id=" + mIndex);
785 if (value == ENABLED) {
786 loadUrlSync(fileUrl);
787 assertEquals(ACCESS_GRANTED_TITLE, getTitleOnUiThread());
789 loadUrlSyncAndExpectError(fileUrl);
796 class AwSettingsContentUrlAccessTestHelper extends AwSettingsTestHelper<Boolean> {
798 AwSettingsContentUrlAccessTestHelper(
799 AwContents awContents,
800 TestAwContentsClient contentViewClient,
801 int index) throws Throwable {
802 super(awContents, contentViewClient, true);
803 mTarget = "content_access_" + index;
807 protected Boolean getAlteredValue() {
812 protected Boolean getInitialValue() {
817 protected Boolean getCurrentValue() {
818 return mAwSettings.getAllowContentAccess();
822 protected void setCurrentValue(Boolean value) {
823 mAwSettings.setAllowContentAccess(value);
827 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
828 AwSettingsTest.this.resetResourceRequestCountInContentProvider(mTarget);
829 loadUrlSync(AwSettingsTest.this.createContentUrl(mTarget));
830 if (value == ENABLED) {
831 AwSettingsTest.this.ensureResourceRequestCountInContentProvider(mTarget, 1);
833 AwSettingsTest.this.ensureResourceRequestCountInContentProvider(mTarget, 0);
837 private final String mTarget;
840 class AwSettingsContentUrlAccessFromFileTestHelper extends AwSettingsTestHelper<Boolean> {
841 private static final String TARGET = "content_from_file";
843 AwSettingsContentUrlAccessFromFileTestHelper(
844 AwContents awContents,
845 TestAwContentsClient contentViewClient,
846 int index) throws Throwable {
847 super(awContents, contentViewClient, true);
849 mTempDir = getInstrumentation().getTargetContext().getCacheDir().getPath();
853 protected Boolean getAlteredValue() {
858 protected Boolean getInitialValue() {
863 protected Boolean getCurrentValue() {
864 return mAwSettings.getAllowContentAccess();
868 protected void setCurrentValue(Boolean value) {
869 mAwSettings.setAllowContentAccess(value);
873 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
874 AwSettingsTest.this.resetResourceRequestCountInContentProvider(TARGET);
875 final String fileName = mTempDir + "/" + TARGET + ".html";
877 TestFileUtil.createNewHtmlFile(fileName,
880 // Adding a query avoids hitting a cached image, and also verifies
881 // that content URL query parameters are ignored when accessing
882 // a content provider.
883 AwSettingsTest.this.createContentUrl(TARGET + "?id=" + mIndex) + "\">");
885 loadUrlSync("file://" + fileName);
886 if (value == ENABLED) {
887 AwSettingsTest.this.ensureResourceRequestCountInContentProvider(TARGET, 1);
889 AwSettingsTest.this.ensureResourceRequestCountInContentProvider(TARGET, 0);
892 TestFileUtil.deleteFile(fileName);
897 private String mTempDir;
900 // This class provides helper methods for testing of settings related to
901 // the text autosizing feature.
902 abstract class AwSettingsTextAutosizingTestHelper<T> extends AwSettingsTestHelper<T> {
903 protected static final float PARAGRAPH_FONT_SIZE = 14.0f;
905 AwSettingsTextAutosizingTestHelper(
906 AwContents awContents,
907 TestAwContentsClient contentViewClient) throws Throwable {
908 super(awContents, contentViewClient, true);
909 mNeedToWaitForFontSizeChange = false;
910 loadDataSync(getData());
914 protected void setCurrentValue(T value) throws Throwable {
915 mNeedToWaitForFontSizeChange = false;
916 if (value != getCurrentValue()) {
917 mOldFontSize = getActualFontSize();
918 mNeedToWaitForFontSizeChange = true;
922 protected float getActualFontSize() throws Throwable {
923 if (!mNeedToWaitForFontSizeChange) {
924 executeJavaScriptAndWaitForResult("setTitleToActualFontSize()");
926 final float oldFontSize = mOldFontSize;
927 poll(new Callable<Boolean>() {
929 public Boolean call() throws Exception {
930 executeJavaScriptAndWaitForResult("setTitleToActualFontSize()");
931 float newFontSize = Float.parseFloat(getTitleOnUiThread());
932 return newFontSize != oldFontSize;
935 mNeedToWaitForFontSizeChange = false;
937 return Float.parseFloat(getTitleOnUiThread());
940 protected String getData() {
941 StringBuilder sb = new StringBuilder();
944 "function setTitleToActualFontSize() {" +
945 // parseFloat is used to trim out the "px" suffix.
946 " document.title = parseFloat(getComputedStyle(" +
947 " document.getElementById('par')).getPropertyValue('font-size'));" +
948 "}</script></head>" +
950 "<p id=\"par\" style=\"font-size:");
951 sb.append(PARAGRAPH_FONT_SIZE);
953 // Make the paragraph wide enough for being processed by the font autosizer.
954 for (int i = 0; i < 100; i++) {
955 sb.append("Hello, World! ");
957 sb.append("</p></body></html>");
958 return sb.toString();
961 private boolean mNeedToWaitForFontSizeChange;
962 private float mOldFontSize;
965 class AwSettingsLayoutAlgorithmTestHelper extends
966 AwSettingsTextAutosizingTestHelper<LayoutAlgorithm> {
968 AwSettingsLayoutAlgorithmTestHelper(
969 AwContents awContents,
970 TestAwContentsClient contentViewClient) throws Throwable {
971 super(awContents, contentViewClient);
972 // Font autosizing doesn't step in for narrow layout widths.
973 mAwSettings.setUseWideViewPort(true);
977 protected LayoutAlgorithm getAlteredValue() {
978 return LayoutAlgorithm.TEXT_AUTOSIZING;
982 protected LayoutAlgorithm getInitialValue() {
983 return LayoutAlgorithm.NARROW_COLUMNS;
987 protected LayoutAlgorithm getCurrentValue() {
988 return mAwSettings.getLayoutAlgorithm();
992 protected void setCurrentValue(LayoutAlgorithm value) throws Throwable {
993 super.setCurrentValue(value);
994 mAwSettings.setLayoutAlgorithm(value);
998 protected void doEnsureSettingHasValue(LayoutAlgorithm value) throws Throwable {
999 final float actualFontSize = getActualFontSize();
1000 if (value == LayoutAlgorithm.TEXT_AUTOSIZING) {
1001 assertFalse("Actual font size: " + actualFontSize,
1002 actualFontSize == PARAGRAPH_FONT_SIZE);
1004 assertTrue("Actual font size: " + actualFontSize,
1005 actualFontSize == PARAGRAPH_FONT_SIZE);
1010 class AwSettingsTextZoomTestHelper extends AwSettingsTextAutosizingTestHelper<Integer> {
1011 private static final int INITIAL_TEXT_ZOOM = 100;
1012 private final float mInitialActualFontSize;
1014 AwSettingsTextZoomTestHelper(
1015 AwContents awContents,
1016 TestAwContentsClient contentViewClient) throws Throwable {
1017 super(awContents, contentViewClient);
1018 mInitialActualFontSize = getActualFontSize();
1022 protected Integer getAlteredValue() {
1023 return INITIAL_TEXT_ZOOM * 2;
1027 protected Integer getInitialValue() {
1028 return INITIAL_TEXT_ZOOM;
1032 protected Integer getCurrentValue() {
1033 return mAwSettings.getTextZoom();
1037 protected void setCurrentValue(Integer value) throws Throwable {
1038 super.setCurrentValue(value);
1039 mAwSettings.setTextZoom(value);
1043 protected void doEnsureSettingHasValue(Integer value) throws Throwable {
1044 final float actualFontSize = getActualFontSize();
1045 // Ensure that actual vs. initial font size ratio is similar to actual vs. initial
1046 // text zoom values ratio.
1047 final float ratiosDelta = Math.abs(
1048 (actualFontSize / mInitialActualFontSize) -
1049 (value / (float) INITIAL_TEXT_ZOOM));
1051 "|(" + actualFontSize + " / " + mInitialActualFontSize + ") - (" +
1052 value + " / " + INITIAL_TEXT_ZOOM + ")| = " + ratiosDelta,
1053 ratiosDelta <= 0.2f);
1057 class AwSettingsTextZoomAutosizingTestHelper
1058 extends AwSettingsTextAutosizingTestHelper<Integer> {
1059 private static final int INITIAL_TEXT_ZOOM = 100;
1060 private final float mInitialActualFontSize;
1062 AwSettingsTextZoomAutosizingTestHelper(
1063 AwContents awContents,
1064 TestAwContentsClient contentViewClient) throws Throwable {
1065 super(awContents, contentViewClient);
1066 mAwSettings.setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING);
1067 // The initial font size can be adjusted by font autosizer depending on the page's
1069 mInitialActualFontSize = getActualFontSize();
1073 protected Integer getAlteredValue() {
1074 return INITIAL_TEXT_ZOOM * 2;
1078 protected Integer getInitialValue() {
1079 return INITIAL_TEXT_ZOOM;
1083 protected Integer getCurrentValue() {
1084 return mAwSettings.getTextZoom();
1088 protected void setCurrentValue(Integer value) throws Throwable {
1089 super.setCurrentValue(value);
1090 mAwSettings.setTextZoom(value);
1094 protected void doEnsureSettingHasValue(Integer value) throws Throwable {
1095 final float actualFontSize = getActualFontSize();
1096 // Ensure that actual vs. initial font size ratio is similar to actual vs. initial
1097 // text zoom values ratio.
1098 final float ratiosDelta = Math.abs(
1099 (actualFontSize / mInitialActualFontSize) -
1100 (value / (float) INITIAL_TEXT_ZOOM));
1102 "|(" + actualFontSize + " / " + mInitialActualFontSize + ") - (" +
1103 value + " / " + INITIAL_TEXT_ZOOM + ")| = " + ratiosDelta,
1104 ratiosDelta <= 0.2f);
1108 class AwSettingsJavaScriptPopupsTestHelper extends AwSettingsTestHelper<Boolean> {
1109 private static final String POPUP_ENABLED = "Popup enabled";
1110 private static final String POPUP_BLOCKED = "Popup blocked";
1112 AwSettingsJavaScriptPopupsTestHelper(
1113 AwContents awContents,
1114 TestAwContentsClient contentViewClient) throws Throwable {
1115 super(awContents, contentViewClient, true);
1119 protected Boolean getAlteredValue() {
1124 protected Boolean getInitialValue() {
1129 protected Boolean getCurrentValue() {
1130 return mAwSettings.getJavaScriptCanOpenWindowsAutomatically();
1134 protected void setCurrentValue(Boolean value) {
1135 mAwSettings.setJavaScriptCanOpenWindowsAutomatically(value);
1139 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
1140 loadDataSync(getData());
1141 final boolean expectPopupEnabled = value;
1142 poll(new Callable<Boolean>() {
1144 public Boolean call() throws Exception {
1145 String title = getTitleOnUiThread();
1146 return expectPopupEnabled ? POPUP_ENABLED.equals(title) :
1147 POPUP_BLOCKED.equals(title);
1150 assertEquals(value ? POPUP_ENABLED : POPUP_BLOCKED, getTitleOnUiThread());
1153 private String getData() {
1154 return "<html><head>" +
1156 " function tryOpenWindow() {" +
1157 " var newWindow = window.open(" +
1158 " 'data:text/html;charset=utf-8," +
1159 " <html><head><title>" + POPUP_ENABLED + "</title></head></html>');" +
1160 " if (!newWindow) document.title = '" + POPUP_BLOCKED + "';" +
1162 "</script></head>" +
1163 "<body onload='tryOpenWindow()'></body></html>";
1167 class AwSettingsCacheModeTestHelper extends AwSettingsTestHelper<Integer> {
1169 AwSettingsCacheModeTestHelper(
1170 AwContents awContents,
1171 TestAwContentsClient contentViewClient,
1173 TestWebServer webServer) throws Throwable {
1174 super(awContents, contentViewClient, true);
1176 mWebServer = webServer;
1180 protected Integer getAlteredValue() {
1181 // We use the value that results in a behaviour completely opposite to default.
1182 return WebSettings.LOAD_CACHE_ONLY;
1186 protected Integer getInitialValue() {
1187 return WebSettings.LOAD_DEFAULT;
1191 protected Integer getCurrentValue() {
1192 return mAwSettings.getCacheMode();
1196 protected void setCurrentValue(Integer value) {
1197 mAwSettings.setCacheMode(value);
1201 protected void doEnsureSettingHasValue(Integer value) throws Throwable {
1202 final String htmlPath = "/cache_mode_" + mIndex + ".html";
1204 final String url = mWebServer.setResponse(htmlPath, "response", null);
1205 assertEquals(0, mWebServer.getRequestCount(htmlPath));
1206 if (value == WebSettings.LOAD_DEFAULT) {
1208 assertEquals(1, mWebServer.getRequestCount(htmlPath));
1210 loadUrlSyncAndExpectError(url);
1211 assertEquals(0, mWebServer.getRequestCount(htmlPath));
1216 private TestWebServer mWebServer;
1219 // To verify whether UseWideViewport works, we check, if the page width specified
1220 // in the "meta viewport" tag is applied. When UseWideViewport is turned off, the
1221 // "viewport" tag is ignored, and the layout width is set to device width in DIP pixels.
1222 // We specify a very high width value to make sure that it doesn't intersect with
1223 // device screen widths (in DIP pixels).
1224 class AwSettingsUseWideViewportTestHelper extends AwSettingsTestHelper<Boolean> {
1225 private static final String VIEWPORT_TAG_LAYOUT_WIDTH = "3000";
1227 AwSettingsUseWideViewportTestHelper(
1228 AwContents awContents,
1229 TestAwContentsClient contentViewClient) throws Throwable {
1230 super(awContents, contentViewClient, true);
1234 protected Boolean getAlteredValue() {
1239 protected Boolean getInitialValue() {
1244 protected Boolean getCurrentValue() {
1245 return mAwSettings.getUseWideViewPort();
1249 protected void setCurrentValue(Boolean value) {
1250 mAwSettings.setUseWideViewPort(value);
1254 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
1255 loadDataSync(getData());
1256 final String bodyWidth = getTitleOnUiThread();
1258 assertTrue(bodyWidth, VIEWPORT_TAG_LAYOUT_WIDTH.equals(bodyWidth));
1260 assertFalse(bodyWidth, VIEWPORT_TAG_LAYOUT_WIDTH.equals(bodyWidth));
1264 private String getData() {
1265 return "<html><head>" +
1266 "<meta name='viewport' content='width=" + VIEWPORT_TAG_LAYOUT_WIDTH + "' />" +
1268 "<body onload='document.title=document.body.clientWidth'></body></html>";
1272 class AwSettingsLoadWithOverviewModeTestHelper extends AwSettingsTestHelper<Boolean> {
1273 private static final float DEFAULT_PAGE_SCALE = 1.0f;
1275 AwSettingsLoadWithOverviewModeTestHelper(
1276 AwContents awContents,
1277 TestAwContentsClient contentViewClient,
1278 boolean withViewPortTag) throws Throwable {
1279 super(awContents, contentViewClient, true);
1280 mWithViewPortTag = withViewPortTag;
1281 mAwSettings.setUseWideViewPort(true);
1285 protected Boolean getAlteredValue() {
1290 protected Boolean getInitialValue() {
1295 protected Boolean getCurrentValue() {
1296 return mAwSettings.getLoadWithOverviewMode();
1300 protected void setCurrentValue(Boolean value) {
1301 mExpectScaleChange = mAwSettings.getLoadWithOverviewMode() != value;
1302 if (mExpectScaleChange) {
1303 mOnScaleChangedCallCount =
1304 mContentViewClient.getOnScaleChangedHelper().getCallCount();
1306 mAwSettings.setLoadWithOverviewMode(value);
1310 protected void doEnsureSettingHasValue(Boolean value) throws Throwable {
1311 loadDataSync(getData());
1312 if (mExpectScaleChange) {
1313 mContentViewClient.getOnScaleChangedHelper().
1314 waitForCallback(mOnScaleChangedCallCount);
1315 mExpectScaleChange = false;
1317 float currentScale = AwSettingsTest.this.getScaleOnUiThread(mAwContents);
1319 assertTrue("Expected: " + currentScale + " < " + DEFAULT_PAGE_SCALE,
1320 currentScale < DEFAULT_PAGE_SCALE);
1322 assertEquals(DEFAULT_PAGE_SCALE, currentScale);
1326 private String getData() {
1327 return "<html><head>" +
1328 (mWithViewPortTag ? "<meta name='viewport' content='width=3000' />" : "") +
1330 "<body></body></html>";
1333 private final boolean mWithViewPortTag;
1334 private boolean mExpectScaleChange;
1335 private int mOnScaleChangedCallCount;
1338 // The test verifies that JavaScript is disabled upon WebView
1339 // creation without accessing AwSettings. If the test passes,
1340 // it means that WebView-specific web preferences configuration
1341 // is applied on WebView creation. JS state is used, because it is
1342 // enabled by default in Chrome, but must be disabled by default
1345 @Feature({"AndroidWebView", "Preferences"})
1346 public void testJavaScriptDisabledByDefault() throws Throwable {
1347 final String JS_ENABLED_STRING = "JS has run";
1348 final String JS_DISABLED_STRING = "JS has not run";
1349 final String TEST_PAGE_HTML =
1350 "<html><head><title>" + JS_DISABLED_STRING + "</title>"
1351 + "</head><body onload=\"document.title='" + JS_ENABLED_STRING
1352 + "';\"></body></html>";
1353 final TestAwContentsClient contentClient = new TestAwContentsClient();
1354 final AwTestContainerView testContainerView =
1355 createAwTestContainerViewOnMainSync(contentClient);
1356 final AwContents awContents = testContainerView.getAwContents();
1359 contentClient.getOnPageFinishedHelper(),
1363 assertEquals(JS_DISABLED_STRING, getTitleOnUiThread(awContents));
1367 @Feature({"AndroidWebView", "Preferences"})
1368 public void testJavaScriptEnabledWithTwoViews() throws Throwable {
1369 ViewPair views = createViews();
1370 runPerViewSettingsTest(
1371 new AwSettingsJavaScriptTestHelper(views.getContents0(), views.getClient0()),
1372 new AwSettingsJavaScriptTestHelper(views.getContents1(), views.getClient1()));
1376 @Feature({"AndroidWebView", "Preferences"})
1377 public void testJavaScriptEnabledDynamicWithTwoViews() throws Throwable {
1378 ViewPair views = createViews();
1379 runPerViewSettingsTest(
1380 new AwSettingsJavaScriptDynamicTestHelper(views.getContents0(), views.getClient0()),
1381 new AwSettingsJavaScriptDynamicTestHelper(views.getContents1(), views.getClient1()));
1385 @Feature({"AndroidWebView", "Preferences"})
1386 public void testPluginsEnabledWithTwoViews() throws Throwable {
1387 ViewPair views = createViews();
1388 runPerViewSettingsTest(
1389 new AwSettingsPluginsTestHelper(views.getContents0(), views.getClient0()),
1390 new AwSettingsPluginsTestHelper(views.getContents1(), views.getClient1()));
1394 @Feature({"AndroidWebView", "Preferences"})
1395 public void testStandardFontFamilyWithTwoViews() throws Throwable {
1396 ViewPair views = createViews();
1397 runPerViewSettingsTest(
1398 new AwSettingsStandardFontFamilyTestHelper(views.getContents0(), views.getClient0()),
1399 new AwSettingsStandardFontFamilyTestHelper(views.getContents1(), views.getClient1()));
1403 @Feature({"AndroidWebView", "Preferences"})
1404 public void testDefaultFontSizeWithTwoViews() throws Throwable {
1405 ViewPair views = createViews();
1406 runPerViewSettingsTest(
1407 new AwSettingsDefaultFontSizeTestHelper(views.getContents0(), views.getClient0()),
1408 new AwSettingsDefaultFontSizeTestHelper(views.getContents1(), views.getClient1()));
1411 // The test verifies that after changing the LoadsImagesAutomatically
1412 // setting value from false to true previously skipped images are
1413 // automatically loaded.
1415 @Feature({"AndroidWebView", "Preferences"})
1416 public void testLoadsImagesAutomaticallyNoPageReload() throws Throwable {
1417 final TestAwContentsClient contentClient = new TestAwContentsClient();
1418 final AwTestContainerView testContainerView =
1419 createAwTestContainerViewOnMainSync(contentClient);
1420 final AwContents awContents = testContainerView.getAwContents();
1421 AwSettings settings = getAwSettingsOnUiThread(awContents);
1422 settings.setJavaScriptEnabled(true);
1423 ImagePageGenerator generator = new ImagePageGenerator(0, false);
1424 settings.setLoadsImagesAutomatically(false);
1425 loadDataSync(awContents,
1426 contentClient.getOnPageFinishedHelper(),
1427 generator.getPageSource(),
1428 "text/html", false);
1429 assertEquals(ImagePageGenerator.IMAGE_NOT_LOADED_STRING,
1430 getTitleOnUiThread(awContents));
1431 settings.setLoadsImagesAutomatically(true);
1432 poll(new Callable<Boolean>() {
1434 public Boolean call() throws Exception {
1435 return !ImagePageGenerator.IMAGE_NOT_LOADED_STRING.equals(
1436 getTitleOnUiThread(awContents));
1439 assertEquals(ImagePageGenerator.IMAGE_LOADED_STRING, getTitleOnUiThread(awContents));
1444 @Feature({"AndroidWebView", "Preferences"})
1445 public void testLoadsImagesAutomaticallyWithTwoViews() throws Throwable {
1446 ViewPair views = createViews();
1447 runPerViewSettingsTest(
1448 new AwSettingsLoadImagesAutomaticallyTestHelper(
1449 views.getContents0(), views.getClient0(), new ImagePageGenerator(0, true)),
1450 new AwSettingsLoadImagesAutomaticallyTestHelper(
1451 views.getContents1(), views.getClient1(), new ImagePageGenerator(1, true)));
1455 @Feature({"AndroidWebView", "Preferences"})
1456 public void testDefaultTextEncodingWithTwoViews() throws Throwable {
1457 ViewPair views = createViews();
1458 runPerViewSettingsTest(
1459 new AwSettingsDefaultTextEncodingTestHelper(views.getContents0(), views.getClient0()),
1460 new AwSettingsDefaultTextEncodingTestHelper(views.getContents1(), views.getClient1()));
1463 // The test verifies that the default user agent string follows the format
1464 // defined in Android CTS tests:
1466 // Mozilla/5.0 (Linux;[ U;] Android <version>;[ <language>-<country>;]
1467 // [<devicemodel>;] Build/<buildID>) AppleWebKit/<major>.<minor> (KHTML, like Gecko)
1468 // Version/<major>.<minor>[ Mobile] Safari/<major>.<minor>
1470 @Feature({"AndroidWebView", "Preferences"})
1471 public void testUserAgentStringDefault() throws Throwable {
1472 final TestAwContentsClient contentClient = new TestAwContentsClient();
1473 final AwTestContainerView testContainerView =
1474 createAwTestContainerViewOnMainSync(contentClient);
1475 final AwContents awContents = testContainerView.getAwContents();
1476 AwSettings settings = getAwSettingsOnUiThread(awContents);
1477 final String actualUserAgentString = settings.getUserAgentString();
1478 assertEquals(actualUserAgentString, AwSettings.getDefaultUserAgent());
1479 final String patternString =
1480 "Mozilla/5\\.0 \\(Linux;( U;)? Android ([^;]+);( (\\w+)-(\\w+);)?" +
1481 "\\s?(.*)\\sBuild/(.+)\\) AppleWebKit/(\\d+)\\.(\\d+) \\(KHTML, like Gecko\\) " +
1482 "Version/\\d+\\.\\d Chrome/\\d+\\.\\d+\\.\\d+\\.\\d+" +
1483 "( Mobile)? Safari/(\\d+)\\.(\\d+)";
1484 final Pattern userAgentExpr = Pattern.compile(patternString);
1485 Matcher patternMatcher = userAgentExpr.matcher(actualUserAgentString);
1486 assertTrue(String.format("User agent string did not match expected pattern. %nExpected " +
1487 "pattern:%n%s%nActual:%n%s", patternString, actualUserAgentString),
1488 patternMatcher.find());
1489 // No country-language code token.
1490 assertEquals(null, patternMatcher.group(3));
1491 if ("REL".equals(Build.VERSION.CODENAME)) {
1492 // Model is only added in release builds
1493 assertEquals(Build.MODEL, patternMatcher.group(6));
1494 // Release version is valid only in release builds
1495 assertEquals(Build.VERSION.RELEASE, patternMatcher.group(2));
1497 assertEquals(Build.ID, patternMatcher.group(7));
1501 @Feature({"AndroidWebView", "Preferences"})
1502 public void testUserAgentStringOverride() throws Throwable {
1503 final TestAwContentsClient contentClient = new TestAwContentsClient();
1504 final AwTestContainerView testContainerView =
1505 createAwTestContainerViewOnMainSync(contentClient);
1506 final AwContents awContents = testContainerView.getAwContents();
1507 AwSettings settings = getAwSettingsOnUiThread(awContents);
1508 final String defaultUserAgentString = settings.getUserAgentString();
1510 // Check that an attempt to reset the default UA string has no effect.
1511 settings.setUserAgentString(null);
1512 assertEquals(defaultUserAgentString, settings.getUserAgentString());
1513 settings.setUserAgentString("");
1514 assertEquals(defaultUserAgentString, settings.getUserAgentString());
1516 // Check that we can also set the default value.
1517 settings.setUserAgentString(defaultUserAgentString);
1518 assertEquals(defaultUserAgentString, settings.getUserAgentString());
1520 // Set a custom UA string, verify that it can be reset back to default.
1521 final String customUserAgentString = "AwSettingsTest";
1522 settings.setUserAgentString(customUserAgentString);
1523 assertEquals(customUserAgentString, settings.getUserAgentString());
1524 settings.setUserAgentString(null);
1525 assertEquals(defaultUserAgentString, settings.getUserAgentString());
1528 // Verify that the current UA override setting has a priority over UA
1529 // overrides in navigation history entries.
1531 @Feature({"AndroidWebView", "Preferences"})
1532 public void testUserAgentStringOverrideForHistory() throws Throwable {
1533 final TestAwContentsClient contentClient = new TestAwContentsClient();
1534 final AwTestContainerView testContainerView =
1535 createAwTestContainerViewOnMainSync(contentClient);
1536 final AwContents awContents = testContainerView.getAwContents();
1537 final ContentViewCore contentView = testContainerView.getContentViewCore();
1538 CallbackHelper onPageFinishedHelper = contentClient.getOnPageFinishedHelper();
1539 AwSettings settings = getAwSettingsOnUiThread(awContents);
1540 settings.setJavaScriptEnabled(true);
1541 final String defaultUserAgentString = settings.getUserAgentString();
1542 final String customUserAgentString = "AwSettingsTest";
1543 // We are using different page titles to make sure that we are really
1544 // going back and forward between them.
1545 final String pageTemplate =
1546 "<html><head><title>%s</title></head>" +
1547 "<body onload='document.title+=navigator.userAgent'></body>" +
1549 final String page1Title = "Page1";
1550 final String page2Title = "Page2";
1551 final String page1 = String.format(pageTemplate, page1Title);
1552 final String page2 = String.format(pageTemplate, page2Title);
1553 settings.setUserAgentString(customUserAgentString);
1555 awContents, onPageFinishedHelper, page1, "text/html", false);
1556 assertEquals(page1Title + customUserAgentString, getTitleOnUiThread(awContents));
1558 awContents, onPageFinishedHelper, page2, "text/html", false);
1559 assertEquals(page2Title + customUserAgentString, getTitleOnUiThread(awContents));
1560 settings.setUserAgentString(null);
1561 // Must not cause any changes until the next page loading.
1562 assertEquals(page2Title + customUserAgentString, getTitleOnUiThread(awContents));
1563 HistoryUtils.goBackSync(getInstrumentation(), contentView, onPageFinishedHelper);
1564 assertEquals(page1Title + defaultUserAgentString, getTitleOnUiThread(awContents));
1565 HistoryUtils.goForwardSync(getInstrumentation(), contentView,
1566 onPageFinishedHelper);
1567 assertEquals(page2Title + defaultUserAgentString, getTitleOnUiThread(awContents));
1571 @Feature({"AndroidWebView", "Preferences"})
1572 public void testUserAgentStringWithTwoViews() throws Throwable {
1573 ViewPair views = createViews();
1574 runPerViewSettingsTest(
1575 new AwSettingsUserAgentStringTestHelper(views.getContents0(), views.getClient0()),
1576 new AwSettingsUserAgentStringTestHelper(views.getContents1(), views.getClient1()));
1580 @Feature({"AndroidWebView", "Preferences"})
1581 public void testUserAgentWithTestServer() throws Throwable {
1582 final TestAwContentsClient contentClient = new TestAwContentsClient();
1583 final AwTestContainerView testContainerView =
1584 createAwTestContainerViewOnMainSync(contentClient);
1585 AwContents awContents = testContainerView.getAwContents();
1586 AwSettings settings = getAwSettingsOnUiThread(awContents);
1587 final String customUserAgentString =
1588 "testUserAgentWithTestServerUserAgent";
1590 TestWebServer webServer = null;
1591 String fileName = null;
1593 webServer = new TestWebServer(false);
1594 final String httpPath = "/testUserAgentWithTestServer.html";
1595 final String url = webServer.setResponse(httpPath, "foo", null);
1597 settings.setUserAgentString(customUserAgentString);
1598 loadUrlSync(awContents,
1599 contentClient.getOnPageFinishedHelper(),
1602 assertEquals(1, webServer.getRequestCount(httpPath));
1603 HttpRequest request = webServer.getLastRequest(httpPath);
1604 Header[] matchingHeaders = request.getHeaders("User-Agent");
1605 assertEquals(1, matchingHeaders.length);
1607 Header header = matchingHeaders[0];
1608 assertEquals(customUserAgentString, header.getValue());
1610 if (webServer != null) webServer.shutdown();
1615 @Feature({"AndroidWebView", "Preferences"})
1616 public void testDomStorageEnabledWithTwoViews() throws Throwable {
1617 ViewPair views = createViews();
1618 runPerViewSettingsTest(
1619 new AwSettingsDomStorageEnabledTestHelper(views.getContents0(), views.getClient0()),
1620 new AwSettingsDomStorageEnabledTestHelper(views.getContents1(), views.getClient1()));
1623 // Ideally, these three tests below should be combined into one, or tested using
1624 // runPerViewSettingsTest. However, it seems the database setting cannot be toggled
1625 // once set. Filed b/8186497.
1627 @Feature({"AndroidWebView", "Preferences"})
1628 public void testDatabaseInitialValue() throws Throwable {
1629 TestAwContentsClient client = new TestAwContentsClient();
1630 final AwTestContainerView testContainerView =
1631 createAwTestContainerViewOnMainSync(client);
1632 final AwContents awContents = testContainerView.getAwContents();
1633 AwSettingsDatabaseTestHelper helper = new AwSettingsDatabaseTestHelper(awContents, client);
1634 helper.ensureSettingHasInitialValue();
1638 @Feature({"AndroidWebView", "Preferences"})
1639 public void testDatabaseEnabled() throws Throwable {
1640 TestAwContentsClient client = new TestAwContentsClient();
1641 final AwTestContainerView testContainerView =
1642 createAwTestContainerViewOnMainSync(client);
1643 final AwContents awContents = testContainerView.getAwContents();
1644 AwSettingsDatabaseTestHelper helper = new AwSettingsDatabaseTestHelper(awContents, client);
1645 helper.setAlteredSettingValue();
1646 helper.ensureSettingHasAlteredValue();
1650 @Feature({"AndroidWebView", "Preferences"})
1651 public void testDatabaseDisabled() throws Throwable {
1652 TestAwContentsClient client = new TestAwContentsClient();
1653 final AwTestContainerView testContainerView =
1654 createAwTestContainerViewOnMainSync(client);
1655 final AwContents awContents = testContainerView.getAwContents();
1656 AwSettingsDatabaseTestHelper helper = new AwSettingsDatabaseTestHelper(awContents, client);
1657 helper.setInitialSettingValue();
1658 helper.ensureSettingHasInitialValue();
1662 @Feature({"AndroidWebView", "Preferences"})
1663 public void testUniversalAccessFromFilesWithTwoViews() throws Throwable {
1664 ViewPair views = createViews();
1665 runPerViewSettingsTest(
1666 new AwSettingsUniversalAccessFromFilesTestHelper(views.getContents0(),
1667 views.getClient0()),
1668 new AwSettingsUniversalAccessFromFilesTestHelper(views.getContents1(),
1669 views.getClient1()));
1672 // This test verifies that local image resources can be loaded from file:
1673 // URLs regardless of file access state.
1675 @Feature({"AndroidWebView", "Preferences"})
1676 public void testFileAccessFromFilesImage() throws Throwable {
1677 final String testFile = "webview/image_access.html";
1678 assertFileIsReadable(UrlUtils.getTestFilePath(testFile));
1679 final String imageContainerUrl = UrlUtils.getTestFileUrl(testFile);
1680 final String imageHeight = "16";
1681 final TestAwContentsClient contentClient = new TestAwContentsClient();
1682 final AwTestContainerView testContainerView =
1683 createAwTestContainerViewOnMainSync(contentClient);
1684 final AwContents awContents = testContainerView.getAwContents();
1685 AwSettings settings = getAwSettingsOnUiThread(awContents);
1686 settings.setJavaScriptEnabled(true);
1687 settings.setAllowUniversalAccessFromFileURLs(false);
1688 settings.setAllowFileAccessFromFileURLs(false);
1689 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), imageContainerUrl);
1690 assertEquals(imageHeight, getTitleOnUiThread(awContents));
1694 @Feature({"AndroidWebView", "Preferences"})
1695 public void testFileAccessFromFilesIframeWithTwoViews() throws Throwable {
1696 ViewPair views = createViews();
1697 runPerViewSettingsTest(
1698 new AwSettingsFileAccessFromFilesIframeTestHelper(
1699 views.getContents0(), views.getClient0()),
1700 new AwSettingsFileAccessFromFilesIframeTestHelper(
1701 views.getContents1(), views.getClient1()));
1705 @Feature({"AndroidWebView", "Preferences"})
1706 public void testFileAccessFromFilesXhrWithTwoViews() throws Throwable {
1707 ViewPair views = createViews();
1708 runPerViewSettingsTest(
1709 new AwSettingsFileAccessFromFilesXhrTestHelper(views.getContents0(),
1710 views.getClient0()),
1711 new AwSettingsFileAccessFromFilesXhrTestHelper(views.getContents1(),
1712 views.getClient1()));
1716 @Feature({"AndroidWebView", "Preferences"})
1717 public void testFileUrlAccessWithTwoViews() throws Throwable {
1718 ViewPair views = createViews();
1719 runPerViewSettingsTest(
1720 new AwSettingsFileUrlAccessTestHelper(views.getContents0(), views.getClient0(), 0),
1721 new AwSettingsFileUrlAccessTestHelper(views.getContents1(), views.getClient1(), 1));
1725 @Feature({"AndroidWebView", "Preferences"})
1726 public void testContentUrlAccessWithTwoViews() throws Throwable {
1727 ViewPair views = createViews();
1728 runPerViewSettingsTest(
1729 new AwSettingsContentUrlAccessTestHelper(views.getContents0(), views.getClient0(), 0),
1730 new AwSettingsContentUrlAccessTestHelper(views.getContents1(), views.getClient1(), 1));
1734 @Feature({"AndroidWebView", "Preferences", "Navigation"})
1735 public void testBlockingContentUrlsFromDataUrls() throws Throwable {
1736 final TestAwContentsClient contentClient = new TestAwContentsClient();
1737 final AwTestContainerView testContainerView =
1738 createAwTestContainerViewOnMainSync(contentClient);
1739 final AwContents awContents = testContainerView.getAwContents();
1740 final String target = "content_from_data";
1741 final String page = "<html><body>" +
1743 createContentUrl(target) + "\">" +
1745 resetResourceRequestCountInContentProvider(target);
1748 contentClient.getOnPageFinishedHelper(),
1752 ensureResourceRequestCountInContentProvider(target, 0);
1756 @Feature({"AndroidWebView", "Preferences", "Navigation"})
1757 public void testContentUrlFromFileWithTwoViews() throws Throwable {
1758 ViewPair views = createViews();
1759 runPerViewSettingsTest(
1760 new AwSettingsContentUrlAccessFromFileTestHelper(
1761 views.getContents0(), views.getClient0(), 0),
1762 new AwSettingsContentUrlAccessFromFileTestHelper(
1763 views.getContents1(), views.getClient1(), 1));
1767 @Feature({"AndroidWebView", "Preferences"})
1768 public void testBlockNetworkImagesDoesNotBlockDataUrlImage() throws Throwable {
1769 final TestAwContentsClient contentClient = new TestAwContentsClient();
1770 final AwTestContainerView testContainerView =
1771 createAwTestContainerViewOnMainSync(contentClient);
1772 final AwContents awContents = testContainerView.getAwContents();
1773 final AwSettings settings = getAwSettingsOnUiThread(awContents);
1774 ImagePageGenerator generator = new ImagePageGenerator(0, false);
1776 settings.setJavaScriptEnabled(true);
1777 settings.setImagesEnabled(false);
1778 loadDataSync(awContents,
1779 contentClient.getOnPageFinishedHelper(),
1780 generator.getPageSource(),
1783 assertEquals(ImagePageGenerator.IMAGE_LOADED_STRING, getTitleOnUiThread(awContents));
1787 @Feature({"AndroidWebView", "Preferences"})
1788 public void testBlockNetworkImagesBlocksNetworkImageAndReloadInPlace() throws Throwable {
1789 final TestAwContentsClient contentClient = new TestAwContentsClient();
1790 final AwTestContainerView testContainerView =
1791 createAwTestContainerViewOnMainSync(contentClient);
1792 final AwContents awContents = testContainerView.getAwContents();
1793 final AwSettings settings = getAwSettingsOnUiThread(awContents);
1794 settings.setJavaScriptEnabled(true);
1795 ImagePageGenerator generator = new ImagePageGenerator(0, false);
1797 TestWebServer webServer = null;
1799 webServer = new TestWebServer(false);
1800 final String httpImageUrl = generator.getPageUrl(webServer);
1802 settings.setImagesEnabled(false);
1803 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), httpImageUrl);
1804 assertEquals(ImagePageGenerator.IMAGE_NOT_LOADED_STRING,
1805 getTitleOnUiThread(awContents));
1807 settings.setImagesEnabled(true);
1808 poll(new Callable<Boolean>() {
1810 public Boolean call() throws Exception {
1811 return ImagePageGenerator.IMAGE_LOADED_STRING.equals(
1812 getTitleOnUiThread(awContents));
1816 if (webServer != null) webServer.shutdown();
1821 @Feature({"AndroidWebView", "Preferences"})
1822 public void testBlockNetworkImagesWithTwoViews() throws Throwable {
1823 ViewPair views = createViews();
1824 TestWebServer webServer = null;
1826 webServer = new TestWebServer(false);
1827 runPerViewSettingsTest(
1828 new AwSettingsImagesEnabledHelper(
1829 views.getContents0(),
1832 new ImagePageGenerator(0, true)),
1833 new AwSettingsImagesEnabledHelper(
1834 views.getContents1(),
1837 new ImagePageGenerator(1, true)));
1839 if (webServer != null) webServer.shutdown();
1844 @Feature({"AndroidWebView", "Preferences"})
1845 public void testBlockNetworkLoadsWithHttpResources() throws Throwable {
1846 final TestAwContentsClient contentClient = new TestAwContentsClient();
1847 final AwTestContainerView testContainer =
1848 createAwTestContainerViewOnMainSync(contentClient);
1849 final AwContents awContents = testContainer.getAwContents();
1850 final AwSettings awSettings = getAwSettingsOnUiThread(awContents);
1851 awSettings.setJavaScriptEnabled(true);
1852 ImagePageGenerator generator = new ImagePageGenerator(0, false);
1854 TestWebServer webServer = null;
1855 String fileName = null;
1857 // Set up http image.
1858 webServer = new TestWebServer(false);
1859 final String httpPath = "/image.png";
1860 final String imageUrl = webServer.setResponseBase64(
1861 httpPath, generator.getImageSourceNoAdvance(),
1862 CommonResources.getImagePngHeaders(true));
1864 // Set up file html that loads http iframe.
1865 String pageHtml = "<img src='" + imageUrl + "' " +
1866 "onload=\"document.title='img_onload_fired';\" " +
1867 "onerror=\"document.title='img_onerror_fired';\" />";
1868 Context context = getInstrumentation().getTargetContext();
1869 fileName = context.getCacheDir() + "/block_network_loads_test.html";
1870 TestFileUtil.deleteFile(fileName); // Remove leftover file if any.
1871 TestFileUtil.createNewHtmlFile(fileName, "unset", pageHtml);
1873 // Actual test. Blocking should trigger onerror handler.
1874 awSettings.setBlockNetworkLoads(true);
1877 contentClient.getOnPageFinishedHelper(),
1878 "file:///" + fileName);
1879 assertEquals(0, webServer.getRequestCount(httpPath));
1880 assertEquals("img_onerror_fired", getTitleOnUiThread(awContents));
1882 // Unblock should load normally.
1883 awSettings.setBlockNetworkLoads(false);
1886 contentClient.getOnPageFinishedHelper(),
1887 "file:///" + fileName);
1888 assertEquals(1, webServer.getRequestCount(httpPath));
1889 assertEquals("img_onload_fired", getTitleOnUiThread(awContents));
1891 if (fileName != null) TestFileUtil.deleteFile(fileName);
1892 if (webServer != null) webServer.shutdown();
1896 private static class AudioEvent {
1897 private CallbackHelper mCallback;
1898 public AudioEvent(CallbackHelper callback) {
1899 mCallback = callback;
1902 @JavascriptInterface
1903 public void onCanPlay() {
1904 mCallback.notifyCalled();
1907 @JavascriptInterface
1908 public void onError() {
1909 mCallback.notifyCalled();
1914 @Feature({"AndroidWebView", "Preferences"})
1915 public void testBlockNetworkLoadsWithAudio() throws Throwable {
1916 final TestAwContentsClient contentClient = new TestAwContentsClient();
1917 final AwTestContainerView testContainer =
1918 createAwTestContainerViewOnMainSync(contentClient);
1919 final AwContents awContents = testContainer.getAwContents();
1920 final AwSettings awSettings = getAwSettingsOnUiThread(awContents);
1921 CallbackHelper callback = new CallbackHelper();
1922 awSettings.setJavaScriptEnabled(true);
1924 TestWebServer webServer = null;
1926 webServer = new TestWebServer(false);
1927 final String httpPath = "/audio.mp3";
1928 // Don't care about the response is correct or not, just want
1929 // to know whether Url is accessed.
1930 final String audioUrl = webServer.setResponse(httpPath, "1", null);
1932 String pageHtml = "<html><body><audio controls src='" + audioUrl + "' " +
1933 "oncanplay=\"AudioEvent.onCanPlay();\" " +
1934 "onerror=\"AudioEvent.onError();\" /> </body></html>";
1935 // Actual test. Blocking should trigger onerror handler.
1936 awSettings.setBlockNetworkLoads(true);
1937 awContents.addPossiblyUnsafeJavascriptInterface(
1938 new AudioEvent(callback), "AudioEvent", null);
1939 int count = callback.getCallCount();
1940 loadDataSync(awContents, contentClient.getOnPageFinishedHelper(), pageHtml,
1941 "text/html", false);
1942 callback.waitForCallback(count, 1);
1943 assertEquals(0, webServer.getRequestCount(httpPath));
1945 // The below test failed in Nexus Galaxy.
1946 // See https://code.google.com/p/chromium/issues/detail?id=313463
1947 // Unblock should load normally.
1949 awSettings.setBlockNetworkLoads(false);
1950 count = callback.getCallCount();
1951 loadDataSync(awContents, contentClient.getOnPageFinishedHelper(), pageHtml,
1952 "text/html", false);
1953 callback.waitForCallback(count, 1);
1954 assertTrue(0 != webServer.getRequestCount(httpPath));
1957 if (webServer != null) webServer.shutdown();
1961 // Test an assert URL (file:///android_asset/)
1963 @Feature({"AndroidWebView", "Navigation"})
1964 public void testAssetUrl() throws Throwable {
1965 // Note: this text needs to be kept in sync with the contents of the html file referenced
1967 final String expectedTitle = "Asset File";
1968 final TestAwContentsClient contentClient = new TestAwContentsClient();
1969 final AwTestContainerView testContainerView =
1970 createAwTestContainerViewOnMainSync(contentClient);
1971 final AwContents awContents = testContainerView.getAwContents();
1972 loadUrlSync(awContents,
1973 contentClient.getOnPageFinishedHelper(),
1974 "file:///android_asset/asset_file.html");
1975 assertEquals(expectedTitle, getTitleOnUiThread(awContents));
1978 // Test a resource URL (file:///android_res/).
1980 @Feature({"AndroidWebView", "Navigation"})
1981 public void testResourceUrl() throws Throwable {
1982 // Note: this text needs to be kept in sync with the contents of the html file referenced
1984 final String expectedTitle = "Resource File";
1985 final TestAwContentsClient contentClient = new TestAwContentsClient();
1986 final AwTestContainerView testContainerView =
1987 createAwTestContainerViewOnMainSync(contentClient);
1988 final AwContents awContents = testContainerView.getAwContents();
1989 loadUrlSync(awContents,
1990 contentClient.getOnPageFinishedHelper(),
1991 "file:///android_res/raw/resource_file.html");
1992 assertEquals(expectedTitle, getTitleOnUiThread(awContents));
1995 // Test that the file URL access toggle does not affect asset URLs.
1997 @Feature({"AndroidWebView", "Navigation"})
1998 public void testFileUrlAccessToggleDoesNotBlockAssetUrls() throws Throwable {
1999 // Note: this text needs to be kept in sync with the contents of the html file referenced
2001 final String expectedTitle = "Asset File";
2002 final TestAwContentsClient contentClient = new TestAwContentsClient();
2003 final AwTestContainerView testContainerView =
2004 createAwTestContainerViewOnMainSync(contentClient);
2005 final AwContents awContents = testContainerView.getAwContents();
2006 final AwSettings settings = getAwSettingsOnUiThread(awContents);
2007 settings.setAllowFileAccess(false);
2008 loadUrlSync(awContents,
2009 contentClient.getOnPageFinishedHelper(),
2010 "file:///android_asset/asset_file.html");
2011 assertEquals(expectedTitle, getTitleOnUiThread(awContents));
2014 // Test that the file URL access toggle does not affect resource URLs.
2016 @Feature({"AndroidWebView", "Navigation"})
2017 public void testFileUrlAccessToggleDoesNotBlockResourceUrls() throws Throwable {
2018 // Note: this text needs to be kept in sync with the contents of the html file referenced
2020 final String expectedTitle = "Resource File";
2021 final TestAwContentsClient contentClient = new TestAwContentsClient();
2022 final AwTestContainerView testContainerView =
2023 createAwTestContainerViewOnMainSync(contentClient);
2024 final AwContents awContents = testContainerView.getAwContents();
2025 final AwSettings settings = getAwSettingsOnUiThread(awContents);
2026 settings.setAllowFileAccess(false);
2027 loadUrlSync(awContents,
2028 contentClient.getOnPageFinishedHelper(),
2029 "file:///android_res/raw/resource_file.html");
2030 assertEquals(expectedTitle, getTitleOnUiThread(awContents));
2034 @Feature({"AndroidWebView", "Preferences"})
2035 public void testLayoutAlgorithmWithTwoViews() throws Throwable {
2036 ViewPair views = createViews();
2037 runPerViewSettingsTest(
2038 new AwSettingsLayoutAlgorithmTestHelper(views.getContents0(), views.getClient0()),
2039 new AwSettingsLayoutAlgorithmTestHelper(views.getContents1(), views.getClient1()));
2043 @Feature({"AndroidWebView", "Preferences"})
2044 public void testTextZoomWithTwoViews() throws Throwable {
2045 ViewPair views = createViews();
2046 runPerViewSettingsTest(
2047 new AwSettingsTextZoomTestHelper(views.getContents0(), views.getClient0()),
2048 new AwSettingsTextZoomTestHelper(views.getContents1(), views.getClient1()));
2052 @Feature({"AndroidWebView", "Preferences"})
2053 public void testTextZoomAutosizingWithTwoViews() throws Throwable {
2054 ViewPair views = createViews();
2055 runPerViewSettingsTest(
2056 new AwSettingsTextZoomAutosizingTestHelper(views.getContents0(), views.getClient0()),
2057 new AwSettingsTextZoomAutosizingTestHelper(views.getContents1(), views.getClient1()));
2061 @Feature({"AndroidWebView", "Preferences"})
2062 public void testJavaScriptPopupsWithTwoViews() throws Throwable {
2063 ViewPair views = createViews();
2064 runPerViewSettingsTest(
2065 new AwSettingsJavaScriptPopupsTestHelper(views.getContents0(), views.getClient0()),
2066 new AwSettingsJavaScriptPopupsTestHelper(views.getContents1(), views.getClient1()));
2070 @Feature({"AndroidWebView", "Preferences"})
2071 public void testCacheMode() throws Throwable {
2072 final TestAwContentsClient contentClient = new TestAwContentsClient();
2073 final AwTestContainerView testContainer =
2074 createAwTestContainerViewOnMainSync(contentClient);
2075 final AwContents awContents = testContainer.getAwContents();
2076 final AwSettings awSettings = getAwSettingsOnUiThread(testContainer.getAwContents());
2077 clearCacheOnUiThread(awContents, true);
2079 assertEquals(WebSettings.LOAD_DEFAULT, awSettings.getCacheMode());
2080 TestWebServer webServer = null;
2082 webServer = new TestWebServer(false);
2083 final String htmlPath = "/testCacheMode.html";
2084 final String url = webServer.setResponse(htmlPath, "response", null);
2085 awSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
2086 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url);
2087 assertEquals(1, webServer.getRequestCount(htmlPath));
2088 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url);
2089 assertEquals(1, webServer.getRequestCount(htmlPath));
2091 awSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
2092 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url);
2093 assertEquals(2, webServer.getRequestCount(htmlPath));
2094 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url);
2095 assertEquals(3, webServer.getRequestCount(htmlPath));
2097 awSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
2098 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url);
2099 assertEquals(3, webServer.getRequestCount(htmlPath));
2100 loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), url);
2101 assertEquals(3, webServer.getRequestCount(htmlPath));
2103 final String htmlNotInCachePath = "/testCacheMode-not-in-cache.html";
2104 final String urlNotInCache = webServer.setResponse(htmlNotInCachePath, "", null);
2105 loadUrlSyncAndExpectError(awContents,
2106 contentClient.getOnPageFinishedHelper(),
2107 contentClient.getOnReceivedErrorHelper(),
2109 assertEquals(0, webServer.getRequestCount(htmlNotInCachePath));
2111 if (webServer != null) webServer.shutdown();
2116 @Feature({"AndroidWebView", "Preferences"})
2117 // As our implementation of network loads blocking uses the same net::URLRequest settings, make
2118 // sure that setting cache mode doesn't accidentally enable network loads. The reference
2119 // behaviour is that when network loads are blocked, setting cache mode has no effect.
2120 public void testCacheModeWithBlockedNetworkLoads() throws Throwable {
2121 final TestAwContentsClient contentClient = new TestAwContentsClient();
2122 final AwTestContainerView testContainer =
2123 createAwTestContainerViewOnMainSync(contentClient);
2124 final AwContents awContents = testContainer.getAwContents();
2125 final AwSettings awSettings = getAwSettingsOnUiThread(testContainer.getAwContents());
2126 clearCacheOnUiThread(awContents, true);
2128 assertEquals(WebSettings.LOAD_DEFAULT, awSettings.getCacheMode());
2129 awSettings.setBlockNetworkLoads(true);
2130 TestWebServer webServer = null;
2132 webServer = new TestWebServer(false);
2133 final String htmlPath = "/testCacheModeWithBlockedNetworkLoads.html";
2134 final String url = webServer.setResponse(htmlPath, "response", null);
2135 loadUrlSyncAndExpectError(awContents,
2136 contentClient.getOnPageFinishedHelper(),
2137 contentClient.getOnReceivedErrorHelper(),
2139 assertEquals(0, webServer.getRequestCount(htmlPath));
2141 awSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
2142 loadUrlSyncAndExpectError(awContents,
2143 contentClient.getOnPageFinishedHelper(),
2144 contentClient.getOnReceivedErrorHelper(),
2146 assertEquals(0, webServer.getRequestCount(htmlPath));
2148 awSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
2149 loadUrlSyncAndExpectError(awContents,
2150 contentClient.getOnPageFinishedHelper(),
2151 contentClient.getOnReceivedErrorHelper(),
2153 assertEquals(0, webServer.getRequestCount(htmlPath));
2155 awSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
2156 loadUrlSyncAndExpectError(awContents,
2157 contentClient.getOnPageFinishedHelper(),
2158 contentClient.getOnReceivedErrorHelper(),
2160 assertEquals(0, webServer.getRequestCount(htmlPath));
2162 if (webServer != null) webServer.shutdown();
2167 @Feature({"AndroidWebView", "Preferences"})
2168 public void testCacheModeWithTwoViews() throws Throwable {
2169 ViewPair views = createViews();
2170 TestWebServer webServer = null;
2172 webServer = new TestWebServer(false);
2173 runPerViewSettingsTest(
2174 new AwSettingsCacheModeTestHelper(
2175 views.getContents0(), views.getClient0(), 0, webServer),
2176 new AwSettingsCacheModeTestHelper(
2177 views.getContents1(), views.getClient1(), 1, webServer));
2179 if (webServer != null) webServer.shutdown();
2183 static class ManifestTestHelper {
2184 private final TestWebServer mWebServer;
2185 private final String mHtmlPath;
2186 private final String mHtmlUrl;
2187 private final String mManifestPath;
2190 TestWebServer webServer, String htmlPageName, String manifestName) {
2191 mWebServer = webServer;
2192 mHtmlPath = "/" + htmlPageName;
2193 mHtmlUrl = webServer.setResponse(
2194 mHtmlPath, "<html manifest=\"" + manifestName + "\"></html>", null);
2195 mManifestPath = "/" + manifestName;
2196 webServer.setResponse(
2199 CommonResources.getContentTypeAndCacheHeaders("text/cache-manifest", false));
2202 String getHtmlPath() {
2206 String getHtmlUrl() {
2210 String getManifestPath() {
2211 return mManifestPath;
2214 int waitUntilHtmlIsRequested(final int initialRequestCount) throws Exception {
2215 return waitUntilResourceIsRequested(mHtmlPath, initialRequestCount);
2218 int waitUntilManifestIsRequested(final int initialRequestCount) throws Exception {
2219 return waitUntilResourceIsRequested(mManifestPath, initialRequestCount);
2222 private int waitUntilResourceIsRequested(
2223 final String path, final int initialRequestCount) throws Exception {
2224 poll(new Callable<Boolean>() {
2226 public Boolean call() throws Exception {
2227 return mWebServer.getRequestCount(path) > initialRequestCount;
2230 return mWebServer.getRequestCount(path);
2235 @Feature({"AndroidWebView", "Preferences", "AppCache"})
2236 public void testAppCache() throws Throwable {
2237 final TestAwContentsClient contentClient = new TestAwContentsClient();
2238 final AwTestContainerView testContainer =
2239 createAwTestContainerViewOnMainSync(contentClient);
2240 final AwContents awContents = testContainer.getAwContents();
2241 final AwSettings settings = getAwSettingsOnUiThread(awContents);
2242 settings.setJavaScriptEnabled(true);
2243 // Note that the cache isn't actually enabled until the call to setAppCachePath.
2244 settings.setAppCacheEnabled(true);
2246 TestWebServer webServer = null;
2248 webServer = new TestWebServer(false);
2249 ManifestTestHelper helper = new ManifestTestHelper(
2250 webServer, "testAppCache.html", "appcache.manifest");
2253 contentClient.getOnPageFinishedHelper(),
2254 helper.getHtmlUrl());
2255 helper.waitUntilHtmlIsRequested(0);
2256 // Unfortunately, there is no other good way of verifying that AppCache is
2257 // disabled, other than checking that it didn't try to fetch the manifest.
2259 assertEquals(0, webServer.getRequestCount(helper.getManifestPath()));
2260 settings.setAppCachePath("whatever"); // Enables AppCache.
2263 contentClient.getOnPageFinishedHelper(),
2264 helper.getHtmlUrl());
2265 helper.waitUntilManifestIsRequested(0);
2267 if (webServer != null) webServer.shutdown();
2272 @Feature({"AndroidWebView", "Preferences", "AppCache"})
2273 public void testAppCacheWithTwoViews() throws Throwable {
2274 // We don't use the test helper here, because making sure that AppCache
2275 // is disabled takes a lot of time, so running through the usual drill
2276 // will take about 20 seconds.
2277 ViewPair views = createViews();
2279 AwSettings settings0 = getAwSettingsOnUiThread(views.getContents0());
2280 settings0.setJavaScriptEnabled(true);
2281 settings0.setAppCachePath("whatever");
2282 settings0.setAppCacheEnabled(true);
2283 AwSettings settings1 = getAwSettingsOnUiThread(views.getContents1());
2284 settings1.setJavaScriptEnabled(true);
2285 // AppCachePath setting is global, no need to set it for the second view.
2286 settings1.setAppCacheEnabled(true);
2288 TestWebServer webServer = null;
2290 webServer = new TestWebServer(false);
2291 ManifestTestHelper helper0 = new ManifestTestHelper(
2292 webServer, "testAppCache_0.html", "appcache.manifest_0");
2294 views.getContents0(),
2295 views.getClient0().getOnPageFinishedHelper(),
2296 helper0.getHtmlUrl());
2297 int manifestRequests0 = helper0.waitUntilManifestIsRequested(0);
2298 ManifestTestHelper helper1 = new ManifestTestHelper(
2299 webServer, "testAppCache_1.html", "appcache.manifest_1");
2301 views.getContents1(),
2302 views.getClient1().getOnPageFinishedHelper(),
2303 helper1.getHtmlUrl());
2304 helper1.waitUntilManifestIsRequested(0);
2305 settings1.setAppCacheEnabled(false);
2307 views.getContents0(),
2308 views.getClient0().getOnPageFinishedHelper(),
2309 helper0.getHtmlUrl());
2310 helper0.waitUntilManifestIsRequested(manifestRequests0);
2311 final int prevManifestRequestCount =
2312 webServer.getRequestCount(helper1.getManifestPath());
2313 int htmlRequests1 = webServer.getRequestCount(helper1.getHtmlPath());
2315 views.getContents1(),
2316 views.getClient1().getOnPageFinishedHelper(),
2317 helper1.getHtmlUrl());
2318 helper1.waitUntilHtmlIsRequested(htmlRequests1);
2319 // Unfortunately, there is no other good way of verifying that AppCache is
2320 // disabled, other than checking that it didn't try to fetch the manifest.
2323 prevManifestRequestCount, webServer.getRequestCount(helper1.getManifestPath()));
2325 if (webServer != null) webServer.shutdown();
2330 @Feature({"AndroidWebView", "Preferences"})
2331 public void testUseWideViewportWithTwoViews() throws Throwable {
2332 ViewPair views = createViews();
2333 runPerViewSettingsTest(
2334 new AwSettingsUseWideViewportTestHelper(views.getContents0(), views.getClient0()),
2335 new AwSettingsUseWideViewportTestHelper(views.getContents1(), views.getClient1()));
2339 @Feature({"AndroidWebView", "Preferences"})
2340 public void testUseWideViewportWithTwoViewsNoQuirks() throws Throwable {
2341 ViewPair views = createViews(false);
2342 runPerViewSettingsTest(
2343 new AwSettingsUseWideViewportTestHelper(views.getContents0(), views.getClient0()),
2344 new AwSettingsUseWideViewportTestHelper(views.getContents1(), views.getClient1()));
2347 private void useWideViewportLayoutWidthTest(
2348 final AwContents awContents, CallbackHelper onPageFinishedHelper) throws Throwable {
2349 AwSettings settings = getAwSettingsOnUiThread(awContents);
2351 final String pageTemplate = "<html><head>%s</head>" +
2352 "<body onload='document.title=document.body.clientWidth'></body></html>";
2353 final String pageNoViewport = String.format(pageTemplate, "");
2354 final String pageViewportDeviceWidth = String.format(
2356 "<meta name='viewport' content='width=device-width' />");
2357 final String viewportTagSpecifiedWidth = "3000";
2358 final String pageViewportSpecifiedWidth = String.format(
2360 "<meta name='viewport' content='width=" + viewportTagSpecifiedWidth + "' />");
2362 DeviceDisplayInfo deviceInfo =
2363 DeviceDisplayInfo.create(getInstrumentation().getTargetContext());
2364 int displayWidth = (int) (deviceInfo.getDisplayWidth() / deviceInfo.getDIPScale());
2366 settings.setJavaScriptEnabled(true);
2367 assertFalse(settings.getUseWideViewPort());
2368 // When UseWideViewPort is off, "width" setting of "meta viewport"
2369 // tags is ignored, and the layout width is set to device width in CSS pixels.
2370 // Thus, all 3 pages will have the same body width.
2371 loadDataSync(awContents, onPageFinishedHelper, pageNoViewport, "text/html", false);
2372 int actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
2373 // Avoid rounding errors.
2374 assertTrue("Expected: " + displayWidth + ", Actual: " + actualWidth,
2375 Math.abs(displayWidth - actualWidth) <= 1);
2376 loadDataSync(awContents, onPageFinishedHelper, pageViewportDeviceWidth, "text/html", false);
2377 actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
2378 assertTrue("Expected: " + displayWidth + ", Actual: " + actualWidth,
2379 Math.abs(displayWidth - actualWidth) <= 1);
2381 awContents, onPageFinishedHelper, pageViewportSpecifiedWidth, "text/html", false);
2382 actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
2383 assertTrue("Expected: " + displayWidth + ", Actual: " + actualWidth,
2384 Math.abs(displayWidth - actualWidth) <= 1);
2386 settings.setUseWideViewPort(true);
2387 // When UseWideViewPort is on, "meta viewport" tag is used.
2388 // If there is no viewport tag, or width isn't specified,
2389 // then layout width is set to max(980, <device-width-in-DIP-pixels>)
2390 loadDataSync(awContents, onPageFinishedHelper, pageNoViewport, "text/html", false);
2391 actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
2392 assertTrue("Expected: >= 980 , Actual: " + actualWidth, actualWidth >= 980);
2393 loadDataSync(awContents, onPageFinishedHelper, pageViewportDeviceWidth, "text/html", false);
2394 actualWidth = Integer.parseInt(getTitleOnUiThread(awContents));
2395 assertTrue("Expected: " + displayWidth + ", Actual: " + actualWidth,
2396 Math.abs(displayWidth - actualWidth) <= 1);
2398 awContents, onPageFinishedHelper, pageViewportSpecifiedWidth, "text/html", false);
2399 assertEquals(viewportTagSpecifiedWidth, getTitleOnUiThread(awContents));
2403 @Feature({"AndroidWebView", "Preferences"})
2404 public void testUseWideViewportLayoutWidth() throws Throwable {
2405 TestAwContentsClient contentClient = new TestAwContentsClient();
2406 AwTestContainerView testContainerView =
2407 createAwTestContainerViewOnMainSync(contentClient);
2408 useWideViewportLayoutWidthTest(testContainerView.getAwContents(),
2409 contentClient.getOnPageFinishedHelper());
2413 @Feature({"AndroidWebView", "Preferences"})
2414 public void testUseWideViewportLayoutWidthNoQuirks() throws Throwable {
2415 TestAwContentsClient contentClient = new TestAwContentsClient();
2416 AwTestContainerView testContainerView =
2417 createAwTestContainerViewOnMainSync(contentClient, false);
2418 useWideViewportLayoutWidthTest(testContainerView.getAwContents(),
2419 contentClient.getOnPageFinishedHelper());
2423 @Feature({"AndroidWebView", "Preferences"})
2424 public void testUseWideViewportControlsDoubleTabToZoom() throws Throwable {
2425 final TestAwContentsClient contentClient = new TestAwContentsClient();
2426 final AwTestContainerView testContainerView =
2427 createAwTestContainerViewOnMainSync(contentClient);
2428 final AwContents awContents = testContainerView.getAwContents();
2429 CallbackHelper onPageFinishedHelper = contentClient.getOnPageFinishedHelper();
2430 AwSettings settings = getAwSettingsOnUiThread(awContents);
2431 settings.setBuiltInZoomControls(true);
2433 final String page = "<html><body>Page Text</body></html>";
2434 assertFalse(settings.getUseWideViewPort());
2435 loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false);
2436 final float initialScale = getScaleOnUiThread(awContents);
2437 simulateDoubleTapCenterOfWebViewOnUiThread(testContainerView);
2439 assertEquals(initialScale, getScaleOnUiThread(awContents));
2441 settings.setUseWideViewPort(true);
2442 loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false);
2443 int onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount();
2444 simulateDoubleTapCenterOfWebViewOnUiThread(testContainerView);
2445 contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount);
2446 final float zoomedOutScale = getScaleOnUiThread(awContents);
2447 assertTrue("zoomedOut: " + zoomedOutScale + ", initial: " + initialScale,
2448 zoomedOutScale < initialScale);
2452 @Feature({"AndroidWebView", "Preferences"})
2453 public void testLoadWithOverviewModeWithTwoViews() throws Throwable {
2454 ViewPair views = createViews();
2455 runPerViewSettingsTest(
2456 new AwSettingsLoadWithOverviewModeTestHelper(
2457 views.getContents0(), views.getClient0(), false),
2458 new AwSettingsLoadWithOverviewModeTestHelper(
2459 views.getContents1(), views.getClient1(), false));
2463 @Feature({"AndroidWebView", "Preferences"})
2464 public void testLoadWithOverviewModeViewportTagWithTwoViews() throws Throwable {
2465 ViewPair views = createViews();
2466 runPerViewSettingsTest(
2467 new AwSettingsLoadWithOverviewModeTestHelper(
2468 views.getContents0(), views.getClient0(), true),
2469 new AwSettingsLoadWithOverviewModeTestHelper(
2470 views.getContents1(), views.getClient1(), true));
2474 @Feature({"AndroidWebView", "Preferences"})
2475 public void testSetInitialScale() throws Throwable {
2476 final TestAwContentsClient contentClient = new TestAwContentsClient();
2477 final AwTestContainerView testContainerView =
2478 createAwTestContainerViewOnMainSync(contentClient);
2479 final AwContents awContents = testContainerView.getAwContents();
2480 final AwSettings awSettings = getAwSettingsOnUiThread(awContents);
2481 CallbackHelper onPageFinishedHelper = contentClient.getOnPageFinishedHelper();
2483 WindowManager wm = (WindowManager) getInstrumentation().getTargetContext()
2484 .getSystemService(Context.WINDOW_SERVICE);
2485 Point screenSize = new Point();
2486 wm.getDefaultDisplay().getSize(screenSize);
2487 // Make sure after 50% scale, page width still larger than screen.
2488 int height = screenSize.y * 2 + 1;
2489 int width = screenSize.x * 2 + 1;
2490 final String page = "<html><body>" +
2491 "<p style='height:" + height + "px;width:" + width + "px'>" +
2492 "testSetInitialScale</p></body></html>";
2493 final float defaultScale =
2494 getInstrumentation().getTargetContext().getResources().getDisplayMetrics().density;
2496 assertEquals(defaultScale, getPixelScaleOnUiThread(awContents), .01f);
2497 loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false);
2498 assertEquals(defaultScale, getPixelScaleOnUiThread(awContents), .01f);
2500 int onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount();
2501 awSettings.setInitialPageScale(50);
2502 loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false);
2503 contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount);
2504 assertEquals(0.5f, getPixelScaleOnUiThread(awContents), .01f);
2506 onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount();
2507 awSettings.setInitialPageScale(500);
2508 loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false);
2509 contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount);
2510 assertEquals(5.0f, getPixelScaleOnUiThread(awContents), .01f);
2512 onScaleChangedCallCount = contentClient.getOnScaleChangedHelper().getCallCount();
2513 awSettings.setInitialPageScale(0);
2514 loadDataSync(awContents, onPageFinishedHelper, page, "text/html", false);
2515 contentClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount);
2516 assertEquals(defaultScale, getPixelScaleOnUiThread(awContents), .01f);
2521 * @param requiredUserGesture the settings of MediaPlaybackRequiresUserGesture.
2522 * @param waitTime time for waiting event happen, -1 means forever.
2523 * @return true if the event happened,
2524 * @throws Throwable throw exception if timeout.
2526 private boolean runVideoTest(final boolean requiredUserGesture, long waitTime)
2528 final JavascriptEventObserver observer = new JavascriptEventObserver();
2529 TestAwContentsClient client = new TestAwContentsClient();
2530 final AwContents awContents = createAwTestContainerViewOnMainSync(client).getAwContents();
2531 getInstrumentation().runOnMainSync(new Runnable() {
2534 AwSettings awSettings = awContents.getSettings();
2535 awSettings.setJavaScriptEnabled(true);
2536 awSettings.setMediaPlaybackRequiresUserGesture(requiredUserGesture);
2537 observer.register(awContents.getContentViewCore(), "javaObserver");
2540 VideoTestWebServer webServer = new VideoTestWebServer(getActivity());
2542 String data = "<html><head><script>" +
2543 "addEventListener('DOMContentLoaded', function() { " +
2544 " document.getElementById('video').addEventListener('play', function() { " +
2545 " javaObserver.notifyJava(); " +
2548 "</script></head><body>" +
2549 "<video id='video' autoplay control src='" +
2550 webServer.getOnePixelOneFrameWebmURL() + "' /> </body></html>";
2551 loadDataAsync(awContents, data, "text/html", false);
2552 if (waitTime == -1) {
2553 observer.waitForEvent();
2556 return observer.waitForEvent(waitTime);
2559 if (webServer != null && webServer.getTestWebServer() != null)
2560 webServer.getTestWebServer().shutdown();
2566 @Feature({"AndroidWebView", "Preferences"})
2567 http://crbug.com/304549
2570 public void testMediaPlaybackWithoutUserGesture() throws Throwable {
2571 assertTrue(runVideoTest(false, -1));
2575 @Feature({"AndroidWebView", "Preferences"})
2576 public void testMediaPlaybackWithUserGesture() throws Throwable {
2577 // Wait for 5 second to see if video played.
2578 assertFalse(runVideoTest(true, 5000));
2582 @Feature({"AndroidWebView", "Preferences"})
2583 public void testDefaultVideoPosterURL() throws Throwable {
2584 final CallbackHelper videoPosterAccessedCallbackHelper = new CallbackHelper();
2585 final String DEFAULT_VIDEO_POSTER_URL = "http://default_video_poster/";
2586 TestAwContentsClient client = new TestAwContentsClient() {
2588 public InterceptedRequestData shouldInterceptRequest(String url) {
2589 if (url.equals(DEFAULT_VIDEO_POSTER_URL)) {
2590 videoPosterAccessedCallbackHelper.notifyCalled();
2595 final AwContents awContents = createAwTestContainerViewOnMainSync(client).getAwContents();
2596 getInstrumentation().runOnMainSync(new Runnable() {
2599 AwSettings awSettings = awContents.getSettings();
2600 awSettings.setDefaultVideoPosterURL(DEFAULT_VIDEO_POSTER_URL);
2603 VideoTestWebServer webServer = new VideoTestWebServer(
2604 getInstrumentation().getTargetContext());
2606 String data = "<html><head><body>" +
2607 "<video id='video' control src='" +
2608 webServer.getOnePixelOneFrameWebmURL() + "' /> </body></html>";
2609 loadDataAsync(awContents, data, "text/html", false);
2610 videoPosterAccessedCallbackHelper.waitForCallback(0, 1, 20, TimeUnit.SECONDS);
2612 if (webServer.getTestWebServer() != null)
2613 webServer.getTestWebServer().shutdown();
2617 static class ViewPair {
2618 private final AwContents contents0;
2619 private final TestAwContentsClient client0;
2620 private final AwContents contents1;
2621 private final TestAwContentsClient client1;
2623 ViewPair(AwContents contents0, TestAwContentsClient client0,
2624 AwContents contents1, TestAwContentsClient client1) {
2625 this.contents0 = contents0;
2626 this.client0 = client0;
2627 this.contents1 = contents1;
2628 this.client1 = client1;
2631 AwContents getContents0() {
2635 TestAwContentsClient getClient0() {
2639 AwContents getContents1() {
2643 TestAwContentsClient getClient1() {
2649 * Verifies the following statements about a setting:
2650 * - initially, the setting has a default value;
2651 * - the setting can be switched to an alternate value and back;
2652 * - switching a setting in the first WebView doesn't affect the setting
2653 * state in the second WebView and vice versa.
2655 * @param helper0 Test helper for the first ContentView
2656 * @param helper1 Test helper for the second ContentView
2659 private void runPerViewSettingsTest(AwSettingsTestHelper helper0,
2660 AwSettingsTestHelper helper1) throws Throwable {
2661 helper0.ensureSettingHasInitialValue();
2662 helper1.ensureSettingHasInitialValue();
2664 helper1.setAlteredSettingValue();
2665 helper0.ensureSettingHasInitialValue();
2666 helper1.ensureSettingHasAlteredValue();
2668 helper1.setInitialSettingValue();
2669 helper0.ensureSettingHasInitialValue();
2670 helper1.ensureSettingHasInitialValue();
2672 helper0.setAlteredSettingValue();
2673 helper0.ensureSettingHasAlteredValue();
2674 helper1.ensureSettingHasInitialValue();
2676 helper0.setInitialSettingValue();
2677 helper0.ensureSettingHasInitialValue();
2678 helper1.ensureSettingHasInitialValue();
2680 helper0.setAlteredSettingValue();
2681 helper0.ensureSettingHasAlteredValue();
2682 helper1.ensureSettingHasInitialValue();
2684 helper1.setAlteredSettingValue();
2685 helper0.ensureSettingHasAlteredValue();
2686 helper1.ensureSettingHasAlteredValue();
2688 helper0.setInitialSettingValue();
2689 helper0.ensureSettingHasInitialValue();
2690 helper1.ensureSettingHasAlteredValue();
2692 helper1.setInitialSettingValue();
2693 helper0.ensureSettingHasInitialValue();
2694 helper1.ensureSettingHasInitialValue();
2697 private ViewPair createViews() throws Throwable {
2698 return createViews(true);
2701 private ViewPair createViews(boolean supportsLegacyQuirks) throws Throwable {
2702 TestAwContentsClient client0 = new TestAwContentsClient();
2703 TestAwContentsClient client1 = new TestAwContentsClient();
2704 return new ViewPair(
2705 createAwTestContainerViewOnMainSync(client0, supportsLegacyQuirks).getAwContents(),
2707 createAwTestContainerViewOnMainSync(client1, supportsLegacyQuirks).getAwContents(),
2711 static void assertFileIsReadable(String filePath) {
2712 File file = new File(filePath);
2714 assertTrue("Test file \"" + filePath + "\" is not readable." +
2715 "Please make sure that files from android_webview/test/data/device_files/ " +
2716 "has been pushed to the device before testing",
2718 } catch (SecurityException e) {
2719 fail("Got a SecurityException for \"" + filePath + "\": " + e.toString());
2724 * Verifies the number of resource requests made to the content provider.
2725 * @param resource Resource name
2726 * @param expectedCount Expected resource requests count
2728 private void ensureResourceRequestCountInContentProvider(String resource, int expectedCount) {
2729 Context context = getInstrumentation().getTargetContext();
2730 int actualCount = TestContentProvider.getResourceRequestCount(context, resource);
2731 assertEquals(expectedCount, actualCount);
2734 private void resetResourceRequestCountInContentProvider(String resource) {
2735 Context context = getInstrumentation().getTargetContext();
2736 TestContentProvider.resetResourceRequestCount(context, resource);
2739 private String createContentUrl(final String target) {
2740 return TestContentProvider.createContentUrl(target);
2743 private void simulateDoubleTapCenterOfWebViewOnUiThread(final AwTestContainerView webView)
2745 final int x = (webView.getRight() - webView.getLeft()) / 2;
2746 final int y = (webView.getBottom() - webView.getTop()) / 2;
2747 final AwContents awContents = webView.getAwContents();
2748 runTestOnUiThread(new Runnable() {
2751 awContents.getContentViewCore().sendDoubleTapForTest(
2752 SystemClock.uptimeMillis(), x, y);