Add cts test for extra location controller APIs.

Bug: 154347471
Test: atest PrivilegedLocationPermissionTest
Change-Id: I3d1994cb14e9a19f744d592074d56511718f030c
diff --git a/tests/location/location_privileged/Android.bp b/tests/location/location_privileged/Android.bp
new file mode 100644
index 0000000..19ab211b
--- /dev/null
+++ b/tests/location/location_privileged/Android.bp
@@ -0,0 +1,34 @@
+// Copyright (C) 2020 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.
+
+android_test {
+    name: "CtsLocationPrivilegedTestCases",
+    defaults: ["cts_defaults"],
+    static_libs: [
+        "androidx.test.ext.junit",
+        "androidx.test.ext.truth",
+        "androidx.test.rules",
+        "compatibility-device-util-axt",
+        "ctstestrunner-axt",
+        "truth-prebuilt",
+    ],
+    libs: [
+        "android.test.base.stubs",
+    ],
+    srcs: ["src/**/*.java"],
+    test_suites: [
+        "cts",
+        "general-tests",
+    ],
+}
diff --git a/tests/location/location_privileged/AndroidManifest.xml b/tests/location/location_privileged/AndroidManifest.xml
new file mode 100644
index 0000000..29df273
--- /dev/null
+++ b/tests/location/location_privileged/AndroidManifest.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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.
+  -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.location.cts.privileged">
+
+    <uses-permission android:name="android.permission.LOCATION_HARDWARE" />
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+    </application>
+
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+                     android:label="CTS tests for android.location"
+                     android:targetPackage="android.location.cts.privileged" >
+        <meta-data android:name="listener"
+                   android:value="com.android.cts.runner.CtsTestRunListener" />
+    </instrumentation>
+</manifest>
+
diff --git a/tests/location/location_privileged/AndroidTest.xml b/tests/location/location_privileged/AndroidTest.xml
new file mode 100644
index 0000000..9086e27
--- /dev/null
+++ b/tests/location/location_privileged/AndroidTest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2020 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.
+-->
+<configuration description="Config for CTS Location test cases">
+    <option name="test-suite-tag" value="cts" />
+    <option name="config-descriptor:metadata" key="component" value="location" />
+    <option name="config-descriptor:metadata" key="parameter" value="instant_app" />
+    <option name="config-descriptor:metadata" key="parameter" value="multi_abi" />
+    <option name="config-descriptor:metadata" key="parameter" value="secondary_user" />
+    <target_preparer class="com.android.compatibility.common.tradefed.targetprep.LocationCheck" />
+    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="CtsLocationPrivilegedTestCases.apk" />
+    </target_preparer>
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+        <option name="package" value="android.location.cts.privileged" />
+    </test>
+
+</configuration>
diff --git a/tests/location/location_privileged/README.md b/tests/location/location_privileged/README.md
new file mode 100644
index 0000000..98c24c5
--- /dev/null
+++ b/tests/location/location_privileged/README.md
@@ -0,0 +1 @@
+Location CTS tests that require privileged permissions such as LOCATION_HARDWARE.
diff --git a/tests/location/location_privileged/src/android/location/cts/privileged/PrivilegedLocationPermissionTest.java b/tests/location/location_privileged/src/android/location/cts/privileged/PrivilegedLocationPermissionTest.java
new file mode 100644
index 0000000..97c3909
--- /dev/null
+++ b/tests/location/location_privileged/src/android/location/cts/privileged/PrivilegedLocationPermissionTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2020 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.location.cts.privileged;
+
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import static org.junit.Assert.assertNotNull;
+
+import android.Manifest;
+import android.content.Context;
+import android.location.LocationManager;
+import android.os.Build.VERSION;
+import android.os.Build.VERSION_CODES;
+
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+@RunWith(AndroidJUnit4.class)
+public class PrivilegedLocationPermissionTest {
+
+    private Context mContext;
+    private LocationManager mLocationManager;
+
+    @Before
+    public void setUp() throws Exception {
+        mContext = ApplicationProvider.getApplicationContext();
+        mLocationManager = mContext.getSystemService(LocationManager.class);
+        assertNotNull(mLocationManager);
+
+        InstrumentationRegistry.getInstrumentation().getUiAutomation()
+                .adoptShellPermissionIdentity(Manifest.permission.LOCATION_HARDWARE);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        InstrumentationRegistry.getInstrumentation().getUiAutomation()
+                .dropShellPermissionIdentity();
+    }
+
+    @Test
+    public void testExtraLocationControllerPackage() {
+        // Extra location controller API is only available after Q.
+        if (VERSION.SDK_INT < VERSION_CODES.Q) {
+            return;
+        }
+        String originalPackageName = mLocationManager.getExtraLocationControllerPackage();
+        boolean originalPackageEnabeld = mLocationManager.isExtraLocationControllerPackageEnabled();
+
+        // Test setting extra location controller package.
+        String packageName = mContext.getPackageName();
+        mLocationManager.setExtraLocationControllerPackage(packageName);
+        assertWithMessage("Extra location controller package").that(
+                mLocationManager.getExtraLocationControllerPackage()).isEqualTo(packageName);
+
+        // Test enabling extra location controller package.
+        mLocationManager.setExtraLocationControllerPackageEnabled(!originalPackageEnabeld);
+        assertWithMessage("Extra location controller package enabled").that(
+                mLocationManager.isExtraLocationControllerPackageEnabled()).isEqualTo(
+                !originalPackageEnabeld);
+
+        // Reset the original extra location controller package.
+        mLocationManager.setExtraLocationControllerPackage(originalPackageName);
+        mLocationManager.setExtraLocationControllerPackageEnabled(originalPackageEnabeld);
+    }
+}