Implement logic for new Geolocation delayed permission LayoutTests

Bug: 2914450
Change-Id: Ic0be3120e88efe9199f7719b9ccb1090baaabed6
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java b/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java
index 784a6d5..9f580a3 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/FileFilter.java
@@ -89,8 +89,6 @@
         // This first block of tests are for features for which Android
         // should pass all tests. They are skipped only temporarily.
         // TODO: Fix these failing tests and remove them from this list.
-        ignoreResultList.add("fast/dom/Geolocation/delayed-permission-allowed.html"); // requires layoutTestController.permissionSet
-        ignoreResultList.add("fast/dom/Geolocation/delayed-permission-denied.html"); // requires layoutTestController.permissionSet
         ignoreResultList.add("fast/events/touch/basic-multi-touch-events.html"); // Requires multi-touch
         ignoreResultList.add("fast/events/touch/touch-target.html"); // Requires multi-touch
         ignoreResultList.add("http/tests/appcache/empty-manifest.html"); // flaky
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java b/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
index db076da..40af8c0 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
@@ -57,6 +57,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import java.util.Vector;
 
@@ -493,8 +494,19 @@
      * Sets the Geolocation permission state to be used for all future requests.
      */
     public void setGeolocationPermission(boolean allow) {
-        mGeolocationPermissionSet = true;
+        mIsGeolocationPermissionSet = true;
         mGeolocationPermission = allow;
+
+        if (mPendingGeolocationPermissionCallbacks != null) {
+            Iterator iter = mPendingGeolocationPermissionCallbacks.keySet().iterator();
+            while (iter.hasNext()) {
+                GeolocationPermissions.Callback callback =
+                        (GeolocationPermissions.Callback) iter.next();
+                String origin = (String) mPendingGeolocationPermissionCallbacks.get(callback);
+                callback.invoke(origin, mGeolocationPermission, false);
+            }
+            mPendingGeolocationPermissionCallbacks = null;
+        }
     }
 
     public void setMockDeviceOrientation(boolean canProvideAlpha, double alpha,
@@ -697,9 +709,15 @@
         @Override
         public void onGeolocationPermissionsShowPrompt(String origin,
                 GeolocationPermissions.Callback callback) {
-            if (mGeolocationPermissionSet) {
+            if (mIsGeolocationPermissionSet) {
                 callback.invoke(origin, mGeolocationPermission, false);
+                return;
             }
+            if (mPendingGeolocationPermissionCallbacks == null) {
+                mPendingGeolocationPermissionCallbacks =
+                        new HashMap<GeolocationPermissions.Callback, String>();
+            }
+            mPendingGeolocationPermissionCallbacks.put(callback, origin);
         }
 
         @Override
@@ -785,6 +803,8 @@
         mGetDrawtime = false;
         mSaveImagePath = null;
         setDefaultWebSettings(mWebView);
+        mIsGeolocationPermissionSet = false;
+        mPendingGeolocationPermissionCallbacks = null;
     }
 
     private long[] getDrawWebViewTime(WebView view, int count) {
@@ -920,6 +940,7 @@
     static final String DRAW_TIME_LOG = Environment.getExternalStorageDirectory() +
         "/android/page_draw_time.txt";
 
-    private boolean mGeolocationPermissionSet;
+    private boolean mIsGeolocationPermissionSet;
     private boolean mGeolocationPermission;
+    private Map mPendingGeolocationPermissionCallbacks;
 }
diff --git a/tests/DumpRenderTree2/src/com/android/dumprendertree2/LayoutTestsExecutor.java b/tests/DumpRenderTree2/src/com/android/dumprendertree2/LayoutTestsExecutor.java
index 8cc4921..4737657 100644
--- a/tests/DumpRenderTree2/src/com/android/dumprendertree2/LayoutTestsExecutor.java
+++ b/tests/DumpRenderTree2/src/com/android/dumprendertree2/LayoutTestsExecutor.java
@@ -40,11 +40,14 @@
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
-import android.webkit.GeolocationPermissions.Callback;
+import android.webkit.GeolocationPermissions;
 import android.webkit.WebStorage.QuotaUpdater;
 
 import java.io.File;
+import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 /**
  * This activity executes the test. It contains WebView and logic of LayoutTestController
@@ -107,8 +110,9 @@
     private LayoutTestController mLayoutTestController = new LayoutTestController(this);
     private boolean mCanOpenWindows;
     private boolean mDumpDatabaseCallbacks;
-    private boolean mSetGeolocationPermissionCalled;
+    private boolean mIsGeolocationPermissionSet;
     private boolean mGeolocationPermission;
+    private Map mPendingGeolocationPermissionCallbacks;
 
     private EventSender mEventSender = new EventSender();
 
@@ -230,10 +234,17 @@
         }
 
         @Override
-        public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
-            if (mSetGeolocationPermissionCalled) {
+        public void onGeolocationPermissionsShowPrompt(String origin,
+                GeolocationPermissions.Callback callback) {
+            if (mIsGeolocationPermissionSet) {
                 callback.invoke(origin, mGeolocationPermission, false);
+                return;
             }
+            if (mPendingGeolocationPermissionCallbacks == null) {
+                mPendingGeolocationPermissionCallbacks =
+                        new HashMap<GeolocationPermissions.Callback, String>();
+            }
+            mPendingGeolocationPermissionCallbacks.put(callback, origin);
         }
     };
 
@@ -465,7 +476,7 @@
     private static final int MSG_DUMP_CHILD_FRAMES_AS_TEXT = 3;
     private static final int MSG_SET_CAN_OPEN_WINDOWS = 4;
     private static final int MSG_DUMP_DATABASE_CALLBACKS = 5;
-    private static final int MSG_SET_GEOLOCATION_PREMISSION = 6;
+    private static final int MSG_SET_GEOLOCATION_PERMISSION = 6;
 
     Handler mLayoutTestControllerHandler = new Handler() {
         @Override
@@ -513,9 +524,20 @@
                     mDumpDatabaseCallbacks = true;
                     break;
 
-                case MSG_SET_GEOLOCATION_PREMISSION:
-                    mSetGeolocationPermissionCalled = true;
+                case MSG_SET_GEOLOCATION_PERMISSION:
+                    mIsGeolocationPermissionSet = true;
                     mGeolocationPermission = msg.arg1 == 1;
+
+                    if (mPendingGeolocationPermissionCallbacks != null) {
+                        Iterator iter = mPendingGeolocationPermissionCallbacks.keySet().iterator();
+                        while (iter.hasNext()) {
+                            GeolocationPermissions.Callback callback =
+                                    (GeolocationPermissions.Callback) iter.next();
+                            String origin = (String) mPendingGeolocationPermissionCallbacks.get(callback);
+                            callback.invoke(origin, mGeolocationPermission, false);
+                        }
+                        mPendingGeolocationPermissionCallbacks = null;
+                    }
                     break;
 
                 default:
@@ -528,8 +550,8 @@
     private void resetLayoutTestController() {
         mCanOpenWindows = false;
         mDumpDatabaseCallbacks = false;
-        mSetGeolocationPermissionCalled = false;
-        mGeolocationPermission = false;
+        mIsGeolocationPermissionSet = false;
+        mPendingGeolocationPermissionCallbacks = null;
     }
 
     public void waitUntilDone() {
@@ -568,7 +590,7 @@
 
     public void setGeolocationPermission(boolean allow) {
         Log.w(LOG_TAG + "::setGeolocationPermission", "called");
-        Message msg = mLayoutTestControllerHandler.obtainMessage(MSG_SET_GEOLOCATION_PREMISSION);
+        Message msg = mLayoutTestControllerHandler.obtainMessage(MSG_SET_GEOLOCATION_PERMISSION);
         msg.arg1 = allow ? 1 : 0;
         msg.sendToTarget();
     }