Add CTS tests for WebSQL database.

Add CTS tests for WebView's WebSQL database (for testing if the
database can be enabled or disabled).

Change-Id: Icaa516fd2bca43fd40899b684030f68c44683e58
diff --git a/tests/assets/webkit/test_databaseaccess.html b/tests/assets/webkit/test_databaseaccess.html
new file mode 100644
index 0000000..dcf7b098
--- /dev/null
+++ b/tests/assets/webkit/test_databaseaccess.html
@@ -0,0 +1,27 @@
+<!-- Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<html>
+    <head><title>None</title><script>
+        function checkDatabase() {
+            if ('openDatabase' in window) {
+                var db = window.openDatabase('test', '1.0', 'results', 1*1024*1024);
+            }
+            document.title = db ? "Has database" : "No database";
+        }
+    </script></head>
+    <body onload='checkDatabase()'>
+    </body>
+</html>
diff --git a/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java b/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
index 63354d4..18107f8 100644
--- a/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
+++ b/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
@@ -62,6 +62,7 @@
     public static final String ANCHOR_ASSET_URL = "webkit/test_anchor.html";
     public static final String IMAGE_ACCESS_URL = "webkit/test_imageaccess.html";
     public static final String IFRAME_ACCESS_URL = "webkit/test_iframeaccess.html";
+    public static final String DATABASE_ACCESS_URL = "webkit/test_databaseaccess.html";
 
     // Must match the title of the page at
     // android/frameworks/base/core/res/res/raw/loaderror.html
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java b/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java
index 73a2d40..9f8712e 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebSettingsTest.java
@@ -25,6 +25,7 @@
 import android.webkit.WebIconDatabase;
 import android.webkit.WebSettings;
 import android.webkit.WebSettings.TextSize;
+import android.webkit.WebStorage;
 import android.webkit.WebView;
 import android.webkit.cts.WebViewOnUiThread.WaitForProgressClient;
 import java.io.FileOutputStream;
@@ -575,6 +576,52 @@
         }.run();
     }
 
+    // Ideally the test cases that test if the database can be enabled and disabled
+    // properly should be combined into one. However, it seems that for some
+    // non-obvious reason the webview does not support such a sequence reliably (in
+    // particular it seems to fail when database is first disabled explicitly and
+    // then after loading the page it is enabled and another url is loaded).
+    // Also loading as data rather than using URL should work, but it causes a
+    // security exception in JS, most likely due to cross domain access. So we load
+    // using a URL. Finally, it looks like enabling database requires creating a
+    // webChromeClient and listening to Quota callbacks, which is not documented.
+    public void testDatabaseEnabled() throws Throwable {
+        // Verify that websql database works when enabled.
+        startWebServer();
+
+        mOnUiThread.setWebChromeClient(new ChromeClient(mOnUiThread) {
+           @Override
+           public void onExceededDatabaseQuota(String url, String databaseId, long quota,
+                long estimatedSize, long total, WebStorage.QuotaUpdater updater) {
+                updater.updateQuota(estimatedSize);
+            }
+        });
+        mSettings.setJavaScriptEnabled(true);
+        mSettings.setDatabaseEnabled(true);
+        final String url = mWebServer.getAssetUrl(TestHtmlConstants.DATABASE_ACCESS_URL);
+        mSettings.setDatabasePath(getActivity().getDir("db", 0).getPath());
+        mOnUiThread.loadUrlAndWaitForCompletion(url);
+        assertEquals("Has database", mOnUiThread.getTitle());
+    }
+
+    public void testDatabaseDisabled() throws Throwable {
+        // Verify that websql database does not work when disabled.
+        startWebServer();
+
+        mOnUiThread.setWebChromeClient(new ChromeClient(mOnUiThread) {
+            @Override
+            public void onExceededDatabaseQuota(String url, String databaseId, long quota,
+                long estimatedSize, long total, WebStorage.QuotaUpdater updater) {
+                updater.updateQuota(estimatedSize);
+            }
+        });
+        mSettings.setJavaScriptEnabled(true);
+        mSettings.setDatabaseEnabled(false);
+        final String url = mWebServer.getAssetUrl(TestHtmlConstants.DATABASE_ACCESS_URL);
+        mOnUiThread.loadUrlAndWaitForCompletion(url);
+        assertEquals("No database", mOnUiThread.getTitle());
+    }
+
     public void testLoadsImagesAutomatically() throws Throwable {
         assertTrue(mSettings.getLoadsImagesAutomatically());