Write a CTS test to verify shouldOverrideUrlLoading

Bug: 11237523

Write a CTS test to verify shouldOverrideUrlLoading is called when
onCreateWindow is called.

Change-Id: Iac6cc2e29ad4d4e591f773702cfd5b136d6f652c
diff --git a/libs/util/src/android/cts/util/EvaluateJsResultPollingCheck.java b/libs/util/src/android/cts/util/EvaluateJsResultPollingCheck.java
new file mode 100644
index 0000000..17d6a73
--- /dev/null
+++ b/libs/util/src/android/cts/util/EvaluateJsResultPollingCheck.java
@@ -0,0 +1,39 @@
+/*
+ * 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.
+ */
+
+package android.cts.util;
+
+import android.webkit.ValueCallback;
+
+public class EvaluateJsResultPollingCheck  extends PollingCheck
+        implements ValueCallback<String> {
+    private String mActualResult;
+    private String mExpectedResult;
+
+    public EvaluateJsResultPollingCheck(String expected) {
+        mExpectedResult = expected;
+    }
+
+    @Override
+    public synchronized boolean check() {
+        return mExpectedResult.equals(mActualResult);
+    }
+
+    @Override
+    public synchronized void onReceiveValue(String result) {
+        mActualResult = result;
+    }
+}
\ No newline at end of file
diff --git a/tests/assets/webkit/blank_tag.html b/tests/assets/webkit/blank_tag.html
new file mode 100644
index 0000000..628ff28
--- /dev/null
+++ b/tests/assets/webkit/blank_tag.html
@@ -0,0 +1,24 @@
+<!-- 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>
+  <body>
+    <div>
+      <a href="test_hello_world.html" target="_blank" id="link">_blank anchor tag</a><br>
+    </div>
+    <iframe src="iframe_blank_tag.html"></iframe>
+  </body>
+</html>
+
diff --git a/tests/assets/webkit/iframe_blank_tag.html b/tests/assets/webkit/iframe_blank_tag.html
new file mode 100644
index 0000000..55ff410d
--- /dev/null
+++ b/tests/assets/webkit/iframe_blank_tag.html
@@ -0,0 +1,22 @@
+<!-- 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.
+-->
+
+<!DOCTYPE html>
+<html>
+<head>
+  <script type='text/javascript'> window.open('test_hello_world.html'); </script>
+</head>
+</html>
+
diff --git a/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java b/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
index 47ec475..11cc1a5 100644
--- a/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
+++ b/tests/tests/webkit/src/android/webkit/cts/TestHtmlConstants.java
@@ -64,6 +64,7 @@
     public static final String IFRAME_ACCESS_URL = "webkit/test_iframeaccess.html";
     public static final String DATABASE_ACCESS_URL = "webkit/test_databaseaccess.html";
     public static final String STOP_LOADING_URL = "webkit/test_stop_loading.html";
+    public static final String BLANK_TAG_URL = "webkit/blank_tag.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/WebViewClientTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java
index efd3aef..f78c126 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewClientTest.java
@@ -16,12 +16,16 @@
 
 package android.webkit.cts;
 
+import android.cts.util.EvaluateJsResultPollingCheck;
 import android.cts.util.PollingCheck;
 import android.graphics.Bitmap;
 import android.os.Message;
 import android.test.ActivityInstrumentationTestCase2;
 import android.view.KeyEvent;
+import android.view.ViewGroup;
 import android.webkit.HttpAuthHandler;
+import android.webkit.ValueCallback;
+import android.webkit.WebChromeClient;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
@@ -30,6 +34,7 @@
 
 public class WebViewClientTest extends ActivityInstrumentationTestCase2<WebViewStubActivity> {
     private static final long TEST_TIMEOUT = 5000;
+    private static final String TEST_URL = "http://foo.com/";
 
     private WebViewOnUiThread mOnUiThread;
     private CtsTestServer mWebServer;
@@ -60,9 +65,70 @@
         super.tearDown();
     }
 
+    // Verify that the shouldoverrideurlloading is false by default
+    public void testShouldOverrideUrlLoadingDefault() {
+        final WebViewClient webViewClient = new WebViewClient();
+        assertFalse(webViewClient.shouldOverrideUrlLoading(mOnUiThread.getWebView(), null));
+    }
+
+    // Verify shouldoverrideurlloading called on top level navigation
     public void testShouldOverrideUrlLoading() {
         final MockWebViewClient webViewClient = new MockWebViewClient();
-        assertFalse(webViewClient.shouldOverrideUrlLoading(mOnUiThread.getWebView(), null));
+        mOnUiThread.setWebViewClient(webViewClient);
+        mOnUiThread.getSettings().setJavaScriptEnabled(true);
+        String data = "<html><body>" +
+                "<a href=\"" + TEST_URL + "\" id=\"link\">new page</a>" +
+                "</body></html>";
+        mOnUiThread.loadDataAndWaitForCompletion(data, "text/html", null);
+        clickOnLinkUsingJs("link");
+        assertEquals(TEST_URL, webViewClient.getLastShouldOverrideUrl());
+    }
+
+    // Verify shouldoverrideurlloading called on webview called via onCreateWindow
+    public void testShouldOverrideUrlLoadingOnCreateWindow() throws Exception {
+        mWebServer = new CtsTestServer(getActivity());
+        // WebViewClient for main window
+        final MockWebViewClient mainWebViewClient = new MockWebViewClient();
+        // WebViewClient for child window
+        final MockWebViewClient childWebViewClient = new MockWebViewClient();
+        mOnUiThread.setWebViewClient(mainWebViewClient);
+        mOnUiThread.getSettings().setJavaScriptEnabled(true);
+        mOnUiThread.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
+        mOnUiThread.getSettings().setSupportMultipleWindows(true);
+        mOnUiThread.setWebChromeClient(new WebChromeClient() {
+            @Override
+            public boolean onCreateWindow(
+                WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
+                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
+                WebView childWebView = new WebView(view.getContext());
+                childWebView.setWebViewClient(childWebViewClient);
+                childWebView.getSettings().setJavaScriptEnabled(true);
+                transport.setWebView(childWebView);
+                getActivity().addContentView(childWebView, new ViewGroup.LayoutParams(
+                            ViewGroup.LayoutParams.FILL_PARENT,
+                            ViewGroup.LayoutParams.WRAP_CONTENT));
+                resultMsg.sendToTarget();
+                return true;
+            }
+        });
+        mOnUiThread.loadUrl(mWebServer.getAssetUrl(TestHtmlConstants.BLANK_TAG_URL));
+
+        new PollingCheck(TEST_TIMEOUT) {
+            @Override
+            protected boolean check() {
+                return childWebViewClient.hasOnPageFinishedCalled();
+            }
+        }.run();
+        assertEquals(mWebServer.getAssetUrl(TestHtmlConstants.HELLO_WORLD_URL),
+                childWebViewClient.getLastShouldOverrideUrl());
+    }
+
+    private void clickOnLinkUsingJs(final String linkId) {
+        EvaluateJsResultPollingCheck jsResult = new EvaluateJsResultPollingCheck("null");
+        mOnUiThread.evaluateJavascript(
+                "document.getElementById('" + linkId + "').click();" +
+                "console.log('element with id [" + linkId + "] clicked');", jsResult);
+        jsResult.run();
     }
 
     public void testLoadPage() throws Exception {
@@ -221,6 +287,7 @@
         private boolean mOnReceivedHttpAuthRequestCalled;
         private boolean mOnUnhandledKeyEventCalled;
         private boolean mOnScaleChangedCalled;
+        private String mLastShouldOverrideUrl;
 
         public MockWebViewClient() {
             super(mOnUiThread);
@@ -262,6 +329,10 @@
             return mOnScaleChangedCalled;
         }
 
+        public String getLastShouldOverrideUrl() {
+            return mLastShouldOverrideUrl;
+        }
+
         @Override
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
             super.onPageStarted(view, url, favicon);
@@ -320,5 +391,11 @@
             super.onScaleChanged(view, oldScale, newScale);
             mOnScaleChangedCalled = true;
         }
+
+        @Override
+        public boolean shouldOverrideUrlLoading(WebView view, String url) {
+            mLastShouldOverrideUrl = url;
+            return false;
+        }
     }
 }
diff --git a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
index 7347ae0..1bec05c 100644
--- a/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/WebViewTest.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.content.res.AssetManager;
+import android.cts.util.EvaluateJsResultPollingCheck;
 import android.cts.util.PollingCheck;
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.Config;
@@ -2259,26 +2260,6 @@
         }
     }
 
-    private static class EvaluateJsResultPollingCheck  extends PollingCheck
-            implements ValueCallback<String> {
-        private String mActualResult;
-        private String mExpectedResult;
-
-        public EvaluateJsResultPollingCheck(String expected) {
-            mExpectedResult = expected;
-        }
-
-        @Override
-        public synchronized boolean check() {
-            return mExpectedResult.equals(mActualResult);
-        }
-
-        @Override
-        public synchronized void onReceiveValue(String result) {
-            mActualResult = result;
-        }
-    }
-
     final class ScaleChangedWebViewClient extends WaitForLoadedClient {
         private boolean mOnScaleChangedCalled = false;
         public ScaleChangedWebViewClient() {