Fix GeolocationTest

Accessing mock location is now controlled via app ops. Moved common code to
LocationUtils in deviceutil module.

Bug: 21871002
Change-Id: If75c8ece29f50f7042dd9de646cd0de5953aa441
diff --git a/libs/deviceutil/src/android/cts/util/LocationUtils.java b/libs/deviceutil/src/android/cts/util/LocationUtils.java
new file mode 100644
index 0000000..1f7af86
--- /dev/null
+++ b/libs/deviceutil/src/android/cts/util/LocationUtils.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2015 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.app.Instrumentation;
+import android.util.Log;
+
+import java.io.IOException;
+
+public class LocationUtils {
+    private static String TAG = "LocationUtils";
+
+    public static void registerMockLocationProvider(Instrumentation instrumentation,
+            boolean enable) {
+        StringBuilder command = new StringBuilder();
+        command.append("appops set ");
+        command.append(instrumentation.getContext().getPackageName());
+        command.append(" android:mock_location ");
+        command.append(enable ? "allow" : "deny");
+        try {
+            SystemUtil.runShellCommand(instrumentation, command.toString());
+        } catch (IOException e) {
+            Log.e(TAG, "Error managing mock location app. Command: " + command, e);
+        }
+    }
+}
diff --git a/tests/tests/location/Android.mk b/tests/tests/location/Android.mk
index 02de2f2..62d0d5d 100644
--- a/tests/tests/location/Android.mk
+++ b/tests/tests/location/Android.mk
@@ -21,7 +21,7 @@
 # and when built explicitly put it in the data partition
 LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
 
-LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner
+LOCAL_STATIC_JAVA_LIBRARIES := ctsdeviceutil ctstestrunner
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
diff --git a/tests/tests/location/src/android/location/cts/BaseMockLocationTest.java b/tests/tests/location/src/android/location/cts/BaseMockLocationTest.java
index fbc7d8d..14019be 100644
--- a/tests/tests/location/src/android/location/cts/BaseMockLocationTest.java
+++ b/tests/tests/location/src/android/location/cts/BaseMockLocationTest.java
@@ -16,13 +16,8 @@
 
 package android.location.cts;
 
-import android.os.ParcelFileDescriptor;
+import android.cts.util.LocationUtils;
 import android.test.InstrumentationTestCase;
-import android.util.Log;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
 
 /**
  * Base class for instrumentations tests that use mock location.
@@ -33,31 +28,12 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        setAsMoskLocationProvider(true);
+        LocationUtils.registerMockLocationProvider(getInstrumentation(), true);
     }
 
     @Override
     protected void tearDown() throws Exception {
-        setAsMoskLocationProvider(false);
+        LocationUtils.registerMockLocationProvider(getInstrumentation(), false);
         super.tearDown();
     }
-
-    private void setAsMoskLocationProvider(boolean enable) {
-        StringBuilder command = new StringBuilder();
-        command.append("appops set ");
-        command.append(getInstrumentation().getContext().getPackageName());
-        command.append(" android:mock_location ");
-        command.append(enable ? "allow" : "deny");
-
-        ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
-                .executeShellCommand(command.toString());
-
-        InputStream is = new FileInputStream(pfd.getFileDescriptor());
-        try {
-            final byte[] buffer = new byte[8192];
-            while ((is.read(buffer)) != -1);
-        } catch (IOException e) {
-            Log.e(LOG_TAG, "Error managing mock locaiton app", e);
-        }
-    }
 }
diff --git a/tests/tests/webkit/AndroidManifest.xml b/tests/tests/webkit/AndroidManifest.xml
index 098acd9..fa25824 100644
--- a/tests/tests/webkit/AndroidManifest.xml
+++ b/tests/tests/webkit/AndroidManifest.xml
@@ -20,7 +20,6 @@
 
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
-    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <application android:maxRecents="1">
diff --git a/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java b/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java
index ba0e0e9..32b6167 100644
--- a/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java
+++ b/tests/tests/webkit/src/android/webkit/cts/GeolocationTest.java
@@ -17,6 +17,7 @@
 package android.webkit.cts;
 
 import android.content.Context;
+import android.cts.util.LocationUtils;
 import android.cts.util.NullWebViewUtils;
 import android.cts.util.PollingCheck;
 import android.graphics.Bitmap;
@@ -136,6 +137,7 @@
     protected void setUp() throws Exception {
         super.setUp();
 
+        LocationUtils.registerMockLocationProvider(getInstrumentation(), true);
         WebView webview = getActivity().getWebView();
 
         if (webview != null) {
@@ -185,6 +187,7 @@
                 } catch (IllegalArgumentException e) {} // Not much to do about this
             }
         }
+        LocationUtils.registerMockLocationProvider(getInstrumentation(), false);
 
         if (mOnUiThread != null) {
             mOnUiThread.cleanUp();