Add CTS test for scanning settings

Bug: 121037698
Test: build and run the test case.
Change-Id: Ia44709bbfb6a8c887e9eec88e88e9d0af6c51136
diff --git a/tests/tests/location/src/android/location/cts/ScanningSettingsTest.java b/tests/tests/location/src/android/location/cts/ScanningSettingsTest.java
new file mode 100644
index 0000000..9e35d7e
--- /dev/null
+++ b/tests/tests/location/src/android/location/cts/ScanningSettingsTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2019 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;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.provider.Settings;
+import android.support.test.uiautomator.By;
+import android.support.test.uiautomator.UiDevice;
+import android.support.test.uiautomator.UiObject2;
+import android.support.test.uiautomator.Until;
+import android.test.AndroidTestCase;
+
+import com.android.compatibility.common.util.CddTest;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests if system settings app provides scanning settings.
+ */
+public class ScanningSettingsTest extends AndroidTestCase {
+    private static final int TIMEOUT = 8_000;  // 8 seconds
+    private static final String SETTINGS_PACKAGE = "com.android.settings";
+
+    private static final String WIFI_SCANNING_TITLE_RES =
+            "location_scanning_wifi_always_scanning_title";
+    private static final String BLUETOOTH_SCANNING_TITLE_RES =
+            "location_scanning_bluetooth_always_scanning_title";
+
+    private UiDevice mDevice;
+    private Context mContext;
+    private String mLauncherPackage;
+    private PackageManager mPackageManager;
+
+    @Before
+    public void setUp() {
+        mContext = InstrumentationRegistry.getInstrumentation().getContext();
+        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+
+        mPackageManager = mContext.getPackageManager();
+        final Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
+        launcherIntent.addCategory(Intent.CATEGORY_HOME);
+        mLauncherPackage = mPackageManager.resolveActivity(launcherIntent,
+                PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName;
+    }
+
+    @Test
+    @CddTest(requirement = "7.4.2/C-2-1")
+    public void testWifiScanningSettings() throws PackageManager.NameNotFoundException {
+        launchScanningSettings();
+        toggleSettingAndVerify(WIFI_SCANNING_TITLE_RES, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE);
+    }
+
+    @Test
+    @CddTest(requirement = "7.4.3/C-4-1")
+    public void testBleScanningSettings() throws PackageManager.NameNotFoundException {
+        launchScanningSettings();
+        toggleSettingAndVerify(BLUETOOTH_SCANNING_TITLE_RES,
+                Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE);
+    }
+
+    private void launchScanningSettings() {
+        // Start from the home screen
+        mDevice.pressHome();
+        mDevice.wait(Until.hasObject(By.pkg(mLauncherPackage).depth(0)), TIMEOUT);
+
+        final Intent intent = new Intent(Settings.ACTION_LOCATION_SCANNING_SETTINGS);
+        // Clear out any previous instances
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+        mContext.startActivity(intent);
+
+        // Wait for the app to appear
+        mDevice.wait(Until.hasObject(By.pkg(SETTINGS_PACKAGE).depth(0)), TIMEOUT);
+    }
+
+    private void toggleSettingAndVerify(String prefTitleRes, String settingKey)
+            throws PackageManager.NameNotFoundException {
+        final Resources res = mPackageManager.getResourcesForApplication(SETTINGS_PACKAGE);
+        final int resId = res.getIdentifier(prefTitleRes, "string", SETTINGS_PACKAGE);
+        final UiObject2 pref = mDevice.findObject(By.text(res.getString(resId)));
+        final ContentResolver resolver = mContext.getContentResolver();
+        final boolean checked = Settings.Global.getInt(resolver, settingKey, 0) == 1;
+
+        // Click the preference to toggle the setting.
+        pref.click();
+        mDevice.waitForIdle();
+        assertEquals(!checked, Settings.Global.getInt(resolver, settingKey, 0) == 1);
+
+        // Click the preference again to toggle the setting back.
+        pref.click();
+        mDevice.waitForIdle();
+        assertEquals(checked, Settings.Global.getInt(resolver, settingKey, 0) == 1);
+    }
+}