Fix/clean up WebSettingsTest.testAccessAllowFileAccess

- Actually create a file in the test's data dir and use it to check the
  setting has the desired effect. The test previously used an
  android_asset URL in one case (which is not affected by the setting)
  and /etc/hosts in another case (which we shouldn't assume the app can
  access normally).

- Don't attempt to compare against the title of the network error page,
  as this is different in different locales; instead just check that we
  did *not* load the expected content.

- Remove obsolete version check.

Bug: 71925584
Test: atest WebSettingsTest#testAccessAllowFileAccess
Merged-In: Ic1b03b2cadb5ae8e38450f8b094a43f641ca694f
Change-Id: Ic1b03b2cadb5ae8e38450f8b094a43f641ca694f
diff --git a/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java b/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
index 68d944b..7945c0c 100644
--- a/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
+++ b/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
@@ -59,8 +59,6 @@
 
     public static final String EXT_WEB_URL1 = "http://www.example.com/";
 
-    public static final String LOCAL_FILESYSTEM_URL = "file:///etc/hosts";
-
     public static final String PARAM_ASSET_URL = "webkit/test_queryparam.html";
     public static final String ANCHOR_ASSET_URL = "webkit/test_anchor.html";
     public static final String IMAGE_ACCESS_URL = "webkit/test_imageaccess.html";
@@ -73,10 +71,6 @@
     public static final String NON_EXISTENT_PAGE_URL = "webkit/generate_404.html";
     public static final String BAD_IMAGE_PAGE_URL = "webkit/test_bad_image_url.html";
 
-    // Must match the title of the page at
-    // android/frameworks/base/core/res/res/raw/loaderror.html
-    public static final String WEBPAGE_NOT_AVAILABLE_TITLE = "Webpage not available";
-
     public static final String getFileUrl(String assetName) {
         if (assetName.contains(":") || assetName.startsWith("/")) {
             throw new IllegalArgumentException();
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java b/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java
index ecfd2ec..d55e151 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java
@@ -226,32 +226,41 @@
         assertEquals(customUserAgent, mOnUiThread.getTitle());
     }
 
-    public void testAccessAllowFileAccess() {
+    public void testAccessAllowFileAccess() throws Exception {
         if (!NullWebViewUtils.isWebViewAvailable()) {
             return;
         }
-        // This test is not compatible with 4.0.3
-        if ("4.0.3".equals(Build.VERSION.RELEASE)) {
-            return;
-        }
 
-        assertTrue(mSettings.getAllowFileAccess());
+        // prepare an HTML file in the data directory we can access to test the setting.
+        final String dataDirTitle = "Loaded from data dir";
+        final String dataDirFile = "datadir.html";
+        final String dataDirPath = mContext.getFileStreamPath(dataDirFile).getAbsolutePath();
+        final String dataDirUrl = "file://" + dataDirPath;
+        writeFile(dataDirFile, "<html><title>" + dataDirTitle + "</title></html>");
 
-        String fileUrl = TestHtmlConstants.getFileUrl(TestHtmlConstants.HELLO_WORLD_URL);
-        mOnUiThread.loadUrlAndWaitForCompletion(fileUrl);
-        assertEquals(TestHtmlConstants.HELLO_WORLD_TITLE, mOnUiThread.getTitle());
+        assertTrue("File access should be on by default", mSettings.getAllowFileAccess());
 
-        fileUrl = TestHtmlConstants.getFileUrl(TestHtmlConstants.BR_TAG_URL);
         mSettings.setAllowFileAccess(false);
-        assertFalse(mSettings.getAllowFileAccess());
-        mOnUiThread.loadUrlAndWaitForCompletion(fileUrl);
-        // android_asset URLs should still be loaded when even with file access
-        // disabled.
-        assertEquals(TestHtmlConstants.BR_TAG_TITLE, mOnUiThread.getTitle());
+        assertFalse("Explicitly setting file access to false should work",
+                mSettings.getAllowFileAccess());
 
-        // Files on the file system should not be loaded.
-        mOnUiThread.loadUrlAndWaitForCompletion(TestHtmlConstants.LOCAL_FILESYSTEM_URL);
-        assertEquals(TestHtmlConstants.WEBPAGE_NOT_AVAILABLE_TITLE, mOnUiThread.getTitle());
+        String assetUrl = TestHtmlConstants.getFileUrl(TestHtmlConstants.BR_TAG_URL);
+        mOnUiThread.loadUrlAndWaitForCompletion(assetUrl);
+        assertEquals(
+                "android_asset URLs should still be loaded when even with file access disabled",
+                TestHtmlConstants.BR_TAG_TITLE, mOnUiThread.getTitle());
+
+        mOnUiThread.loadUrlAndWaitForCompletion(dataDirUrl);
+        assertFalse("Files on the file system should not be loaded with file access disabled",
+                dataDirTitle.equals(mOnUiThread.getTitle()));
+
+        mSettings.setAllowFileAccess(true);
+        assertTrue("Explicitly setting file access to true should work",
+                mSettings.getAllowFileAccess());
+
+        mOnUiThread.loadUrlAndWaitForCompletion(dataDirUrl);
+        assertEquals("Loading files on the file system should work with file access enabled",
+                dataDirTitle, mOnUiThread.getTitle());
     }
 
     public void testAccessCacheMode() throws Throwable {