Merge "Add test team members as owners of integration tests."
diff --git a/nearby/tests/integration/privileged/AndroidManifest.xml b/nearby/tests/integration/privileged/AndroidManifest.xml
index c8fac0a..00845f1 100644
--- a/nearby/tests/integration/privileged/AndroidManifest.xml
+++ b/nearby/tests/integration/privileged/AndroidManifest.xml
@@ -18,6 +18,8 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
           package="android.nearby.integration.privileged">
 
+    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
+
     <instrumentation
         android:name="androidx.test.runner.AndroidJUnitRunner"
         android:targetPackage="android.nearby.integration.privileged"
diff --git a/nearby/tests/integration/privileged/src/android/nearby/integration/privileged/FastPairSettingsProviderTest.kt b/nearby/tests/integration/privileged/src/android/nearby/integration/privileged/FastPairSettingsProviderTest.kt
new file mode 100644
index 0000000..761aaf6
--- /dev/null
+++ b/nearby/tests/integration/privileged/src/android/nearby/integration/privileged/FastPairSettingsProviderTest.kt
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2022 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.nearby.integration.privileged
+
+import android.content.Context
+import android.provider.Settings
+import androidx.test.core.app.ApplicationProvider
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.Parameterized
+import org.junit.runners.Parameterized.Parameters
+
+data class FastPairSettingsFlag(val name: String, val value: Int) {
+    override fun toString() = name
+}
+
+@RunWith(Parameterized::class)
+class FastPairSettingsProviderTest(private val flag: FastPairSettingsFlag) {
+
+    /** Verify privileged app can enable/disable Fast Pair scan. */
+    @Test
+    fun testSettingsFastPairScan_fromPrivilegedApp() {
+        val appContext = ApplicationProvider.getApplicationContext<Context>()
+        val contentResolver = appContext.contentResolver
+
+        Settings.Secure.putInt(contentResolver, Settings.Secure.FAST_PAIR_SCAN_ENABLED, flag.value)
+
+        val actualValue = Settings.Secure.getInt(
+                contentResolver, Settings.Secure.FAST_PAIR_SCAN_ENABLED, /* default value */ -1)
+        assertThat(actualValue).isEqualTo(flag.value)
+    }
+
+    companion object {
+        @JvmStatic
+        @Parameters(name = "{0}Succeed")
+        fun fastPairScanFlags() = listOf(
+            FastPairSettingsFlag(name = "disable", value = 0),
+            FastPairSettingsFlag(name = "enable", value = 1),
+        )
+    }
+}
diff --git a/nearby/tests/integration/untrusted/Android.bp b/nearby/tests/integration/untrusted/Android.bp
index 5736dde..53dbfb7 100644
--- a/nearby/tests/integration/untrusted/Android.bp
+++ b/nearby/tests/integration/untrusted/Android.bp
@@ -26,6 +26,7 @@
         "androidx.test.ext.junit",
         "androidx.test.rules",
         "junit",
+        "kotlin-test",
         "truth-prebuilt",
     ],
     test_suites: ["device-tests"],
diff --git a/nearby/tests/integration/untrusted/src/android/nearby/integration/untrusted/FastPairSettingsProviderTest.kt b/nearby/tests/integration/untrusted/src/android/nearby/integration/untrusted/FastPairSettingsProviderTest.kt
new file mode 100644
index 0000000..b73e737
--- /dev/null
+++ b/nearby/tests/integration/untrusted/src/android/nearby/integration/untrusted/FastPairSettingsProviderTest.kt
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2022 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.nearby.integration.untrusted
+
+import android.content.Context
+import android.content.ContentResolver
+import android.provider.Settings
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import kotlin.test.assertFailsWith
+
+
+@RunWith(AndroidJUnit4::class)
+class FastPairSettingsProviderTest {
+    private lateinit var contentResolver: ContentResolver
+
+    @Before
+    fun setUp() {
+        contentResolver = ApplicationProvider.getApplicationContext<Context>().contentResolver
+    }
+
+    /** Verify untrusted app can read Fast Pair scan enabled setting. */
+    @Test
+    fun testSettingsFastPairScan_fromUnTrustedApp_readsSucceed() {
+        Settings.Secure.getInt(contentResolver,
+                Settings.Secure.FAST_PAIR_SCAN_ENABLED, /* default value */ -1)
+    }
+
+    /** Verify untrusted app can't write Fast Pair scan enabled setting. */
+    @Test
+    fun testSettingsFastPairScan_fromUnTrustedApp_writesFailed() {
+        assertFailsWith<SecurityException> {
+            Settings.Secure.putInt(contentResolver, Settings.Secure.FAST_PAIR_SCAN_ENABLED, 1)
+        }
+    }
+}
diff --git a/nearby/tests/robotests/src/com/android/libraries/testing/deviceshadower/shadows/bluetooth/ShadowBluetoothAdapter.java b/nearby/tests/robotests/src/com/android/libraries/testing/deviceshadower/shadows/bluetooth/ShadowBluetoothAdapter.java
index c9f83a6..394afbc 100644
--- a/nearby/tests/robotests/src/com/android/libraries/testing/deviceshadower/shadows/bluetooth/ShadowBluetoothAdapter.java
+++ b/nearby/tests/robotests/src/com/android/libraries/testing/deviceshadower/shadows/bluetooth/ShadowBluetoothAdapter.java
@@ -17,6 +17,7 @@
 package com.android.libraries.testing.deviceshadower.shadows.bluetooth;
 
 import android.bluetooth.BluetoothAdapter;
+import android.content.AttributionSource;
 
 import com.android.libraries.testing.deviceshadower.internal.DeviceShadowEnvironmentImpl;
 import com.android.libraries.testing.deviceshadower.internal.bluetooth.BlueletImpl;
@@ -49,4 +50,16 @@
         BlueletImpl localBluelet = DeviceShadowEnvironmentImpl.getLocalBlueletImpl();
         return localBluelet.getAdapter();
     }
+
+    @Implementation
+    public static BluetoothAdapter createAdapter(AttributionSource attributionSource) {
+        // Add a device and set local devicelet in case no local bluelet set
+        if (!DeviceShadowEnvironmentImpl.hasLocalDeviceletImpl()) {
+            String address = MacAddressGenerator.get().generateMacAddress();
+            DeviceShadowEnvironmentImpl.addDevice(address);
+            DeviceShadowEnvironmentImpl.setLocalDevice(address);
+        }
+        BlueletImpl localBluelet = DeviceShadowEnvironmentImpl.getLocalBlueletImpl();
+        return localBluelet.getAdapter();
+    }
 }