Add tests for the new flow for requesting bg location

Test: atest CtsPermission3TestCases
Bug: 155881548
Fixes: 156000858
Change-Id: I9b0aaf6add844be072ea3c3b41ee8f4ef61b05ba
diff --git a/tests/tests/permission3/Android.bp b/tests/tests/permission3/Android.bp
index 77e2934..be534b7 100644
--- a/tests/tests/permission3/Android.bp
+++ b/tests/tests/permission3/Android.bp
@@ -38,6 +38,7 @@
         ":CtsUsePermissionApp28",
         ":CtsUsePermissionApp29",
         ":CtsUsePermissionAppLatest",
+        ":CtsUsePermissionAppLatestWithBackground",
     ],
     test_suites: [
         "cts",
diff --git a/tests/tests/permission3/AndroidTest.xml b/tests/tests/permission3/AndroidTest.xml
index 7d80f2c..c4adbfc 100644
--- a/tests/tests/permission3/AndroidTest.xml
+++ b/tests/tests/permission3/AndroidTest.xml
@@ -42,6 +42,7 @@
         <option name="push" value="CtsUsePermissionApp28.apk->/data/local/tmp/cts/permission3/CtsUsePermissionApp28.apk" />
         <option name="push" value="CtsUsePermissionApp29.apk->/data/local/tmp/cts/permission3/CtsUsePermissionApp29.apk" />
         <option name="push" value="CtsUsePermissionAppLatest.apk->/data/local/tmp/cts/permission3/CtsUsePermissionAppLatest.apk" />
+        <option name="push" value="CtsUsePermissionAppLatestWithBackground.apk->/data/local/tmp/cts/permission3/CtsUsePermissionAppLatestWithBackground.apk" />
     </target_preparer>
 
     <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
diff --git a/tests/tests/permission3/UsePermissionAppLatestWithBackground/Android.bp b/tests/tests/permission3/UsePermissionAppLatestWithBackground/Android.bp
new file mode 100644
index 0000000..65bc605
--- /dev/null
+++ b/tests/tests/permission3/UsePermissionAppLatestWithBackground/Android.bp
@@ -0,0 +1,26 @@
+//
+// 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_helper_app {
+    name: "CtsUsePermissionAppLatestWithBackground",
+    srcs: [
+        "src/**/*.kt",
+    ],
+    static_libs: [
+        "kotlin-stdlib",
+    ],
+    certificate: ":cts-testkey2",
+}
diff --git a/tests/tests/permission3/UsePermissionAppLatestWithBackground/AndroidManifest.xml b/tests/tests/permission3/UsePermissionAppLatestWithBackground/AndroidManifest.xml
new file mode 100644
index 0000000..adf2eac
--- /dev/null
+++ b/tests/tests/permission3/UsePermissionAppLatestWithBackground/AndroidManifest.xml
@@ -0,0 +1,29 @@
+<?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.permission3.cts.usepermission">
+
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
+
+    <application>
+        <activity android:name=".RequestPermissionsActivity" android:exported="true" />
+    </application>
+</manifest>
diff --git a/tests/tests/permission3/UsePermissionAppLatestWithBackground/src/android/permission3/cts/usepermission/RequestPermissionsActivity.kt b/tests/tests/permission3/UsePermissionAppLatestWithBackground/src/android/permission3/cts/usepermission/RequestPermissionsActivity.kt
new file mode 100644
index 0000000..1c30c87
--- /dev/null
+++ b/tests/tests/permission3/UsePermissionAppLatestWithBackground/src/android/permission3/cts/usepermission/RequestPermissionsActivity.kt
@@ -0,0 +1,44 @@
+/*
+ * 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.permission3.cts.usepermission
+
+import android.app.Activity
+import android.content.Intent
+import android.os.Bundle
+
+class RequestPermissionsActivity : Activity() {
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+
+        val permissions = intent.getStringArrayExtra("$packageName.PERMISSIONS")!!
+        requestPermissions(permissions, 1)
+    }
+
+    override fun onRequestPermissionsResult(
+        requestCode: Int,
+        permissions: Array<out String>,
+        grantResults: IntArray
+    ) {
+        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
+
+        setResult(RESULT_OK, Intent().apply {
+            putExtra("$packageName.PERMISSIONS", permissions)
+            putExtra("$packageName.GRANT_RESULTS", grantResults)
+        })
+        finish()
+    }
+}
diff --git a/tests/tests/permission3/src/android/permission3/cts/BaseUsePermissionTest.kt b/tests/tests/permission3/src/android/permission3/cts/BaseUsePermissionTest.kt
index e689188..6672f7e 100644
--- a/tests/tests/permission3/src/android/permission3/cts/BaseUsePermissionTest.kt
+++ b/tests/tests/permission3/src/android/permission3/cts/BaseUsePermissionTest.kt
@@ -50,6 +50,8 @@
         const val APP_APK_PATH_28 = "$APK_DIRECTORY/CtsUsePermissionApp28.apk"
         const val APP_APK_PATH_29 = "$APK_DIRECTORY/CtsUsePermissionApp29.apk"
         const val APP_APK_PATH_LATEST = "$APK_DIRECTORY/CtsUsePermissionAppLatest.apk"
+        const val APP_APK_PATH_LATEST_WITH_BACKGROUND =
+                "$APK_DIRECTORY/CtsUsePermissionAppLatestWithBackground.apk"
         const val APP_PACKAGE_NAME = "android.permission3.cts.usepermission"
     }
 
@@ -266,10 +268,14 @@
 
     protected fun clickPermissionRequestSettingsLinkAndAllowAlways() {
         clickPermissionRequestSettingsLink()
-        click(By.res("com.android.permissioncontroller:id/allow_always_radio_button"))
+        clickAllowAlwaysInSettings()
         pressBack()
     }
 
+    protected fun clickAllowAlwaysInSettings() {
+        click(By.res("com.android.permissioncontroller:id/allow_always_radio_button"))
+    }
+
     protected fun clickPermissionRequestAllowForegroundButton() =
         click(By.res("com.android.permissioncontroller:id/permission_allow_foreground_only_button"))
 
diff --git a/tests/tests/permission3/src/android/permission3/cts/PermissionTest30.kt b/tests/tests/permission3/src/android/permission3/cts/PermissionTest30.kt
new file mode 100644
index 0000000..0abae86
--- /dev/null
+++ b/tests/tests/permission3/src/android/permission3/cts/PermissionTest30.kt
@@ -0,0 +1,55 @@
+/*
+ * 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.permission3.cts
+
+import android.Manifest.permission.ACCESS_BACKGROUND_LOCATION
+import android.Manifest.permission.ACCESS_FINE_LOCATION
+import org.junit.Test
+
+/**
+ * Runtime permission behavior apps targeting API 30
+ */
+class PermissionTest30 : BaseUsePermissionTest() {
+
+    @Test
+    fun testCantRequestFgAndBgAtOnce() {
+        installPackage(APP_APK_PATH_LATEST_WITH_BACKGROUND)
+        assertAppHasPermission(ACCESS_FINE_LOCATION, false)
+        assertAppHasPermission(ACCESS_BACKGROUND_LOCATION, false)
+
+        requestAppPermissionsAndAssertResult(ACCESS_FINE_LOCATION to false,
+                ACCESS_BACKGROUND_LOCATION to false) {
+            // Do nothing, should be automatically denied
+        }
+    }
+
+    @Test
+    fun testRequestBothInSequence() {
+        installPackage(APP_APK_PATH_LATEST_WITH_BACKGROUND)
+        assertAppHasPermission(ACCESS_FINE_LOCATION, false)
+        assertAppHasPermission(ACCESS_BACKGROUND_LOCATION, false)
+
+        requestAppPermissionsAndAssertResult(ACCESS_FINE_LOCATION to true) {
+            clickPermissionRequestAllowForegroundButton()
+        }
+
+        requestAppPermissionsAndAssertResult(ACCESS_BACKGROUND_LOCATION to true) {
+            clickAllowAlwaysInSettings()
+            pressBack()
+        }
+    }
+}