Add mumd support to settings tests

Update tests to use apis targetting the user being run. Migrated away
from UiScrollables which defaulted to the default display and instead
used UiObject2 which can more easily be used on specific displays.

Bug: 339049586
Test: atest CtsSettingsTestCases && atest --user-type secondary_user_on_secondary_display CtsSettingsTestCases
Change-Id: Ide194f89fde992141b4306b879bb9ba74f959745
diff --git a/tests/tests/settings/Android.bp b/tests/tests/settings/Android.bp
index 219d3ca..44ee043 100644
--- a/tests/tests/settings/Android.bp
+++ b/tests/tests/settings/Android.bp
@@ -27,6 +27,7 @@
         "junit",
         "truth",
         "telephony_flags_core_java_lib",
+        "camera_platform_flags_java_lib",
     ],
 
     srcs: ["src/**/*.java"],
diff --git a/tests/tests/settings/AndroidTest.xml b/tests/tests/settings/AndroidTest.xml
index 6e9c34f..9ac99b0 100644
--- a/tests/tests/settings/AndroidTest.xml
+++ b/tests/tests/settings/AndroidTest.xml
@@ -23,6 +23,7 @@
     <option name="config-descriptor:metadata" key="parameter" value="multiuser" />
     <option name="config-descriptor:metadata" key="parameter" value="no_foldable_states" />
     <option name="config-descriptor:metadata" key="parameter" value="run_on_sdk_sandbox" />
+    <option name="config-descriptor:metadata" key="parameter" value="secondary_user_on_secondary_display" />
 
     <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
         <option name="cleanup-apks" value="true" />
diff --git a/tests/tests/settings/src/android/settings/cts/AutoPrivacySettingsTest.java b/tests/tests/settings/src/android/settings/cts/AutoPrivacySettingsTest.java
index 1d143a2..4c1ddd7 100644
--- a/tests/tests/settings/src/android/settings/cts/AutoPrivacySettingsTest.java
+++ b/tests/tests/settings/src/android/settings/cts/AutoPrivacySettingsTest.java
@@ -24,30 +24,36 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
-import android.support.test.uiautomator.By;
-import android.support.test.uiautomator.BySelector;
-import android.support.test.uiautomator.UiDevice;
-import android.support.test.uiautomator.UiObject2;
-import android.support.test.uiautomator.UiObjectNotFoundException;
-import android.support.test.uiautomator.UiScrollable;
-import android.support.test.uiautomator.UiSelector;
-import android.support.test.uiautomator.Until;
+import android.os.UserManager;
+import android.platform.test.annotations.RequiresFlagsEnabled;
 import android.widget.Switch;
 
 import androidx.annotation.Nullable;
+import androidx.recyclerview.widget.RecyclerView;
 import androidx.test.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
+import androidx.test.uiautomator.By;
+import androidx.test.uiautomator.BySelector;
+import androidx.test.uiautomator.Condition;
+import androidx.test.uiautomator.Direction;
+import androidx.test.uiautomator.UiDevice;
+import androidx.test.uiautomator.UiObject2;
+import androidx.test.uiautomator.Until;
 
+import com.android.bedstead.harrier.annotations.RequireRunNotOnVisibleBackgroundNonProfileUser;
 import com.android.compatibility.common.util.CddTest;
+import com.android.internal.camera.flags.Flags;
 import com.android.modules.utils.build.SdkLevel;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.util.List;
+
 @RunWith(AndroidJUnit4.class)
 public class AutoPrivacySettingsTest {
-    private static final int TIMEOUT_MS = 2000;
+    private static final int TIMEOUT_MS = 10000;
     private static final String ACTION_SETTINGS = "android.settings.SETTINGS";
     private static final String PRIVACY = "Privacy";
     private static final String MICROPHONE = "Microphone";
@@ -71,20 +77,40 @@
 
     private final Context mContext = InstrumentationRegistry.getContext();
     private final UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
+    private int mDisplayId;
+
+    private Condition mSearchCondition = new Condition<UiDevice, Boolean>() {
+
+        @Override
+        public Boolean apply(UiDevice device) {
+            return device.findObjects(By.clazz(RecyclerView.class)).size() > 1;
+        }
+    };
+
 
     @Before
     public void setUp() {
         assumeFalse("Skipping test: Requirements only apply to Auto",
                 !mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE));
+        UserManager userManager = mContext.getSystemService(UserManager.class);
+        mDisplayId = userManager.getMainDisplayIdAssignedToUser();
     }
 
     /**
-     * MUST provide a user affordance to do microphone toggle in the following location:Settings
-     * > Privacy.
+     * MUST provide a user affordance to do microphone toggle in the following location:Settings >
+     * Privacy.
+     *
+     * This test is not enabled on visible background users.
      */
     @CddTest(requirement = "9.8.2/A-1-3")
     @Test
+    @RequireRunNotOnVisibleBackgroundNonProfileUser
     public void testPrivacyMicrophoneSettings() throws Exception {
+        PackageManager pm = mContext.getPackageManager();
+        if (!pm.hasSystemFeature(PackageManager.FEATURE_MICROPHONE)) {
+            // Skip this test if the system does not have a microphone.
+            return;
+        }
         goHome();
 
         launchActionSettings();
@@ -110,14 +136,15 @@
     /**
      * MUST provide a user affordance to do camera toggle in the following location:Settings >
      * Privacy.
+     *
+     * This test is not enabled on visible background users
      */
     @CddTest(requirement = "9.8.2/A-2-3")
     @Test
+    @RequiresFlagsEnabled(Flags.FLAG_CAMERA_PRIVACY_ALLOWLIST)
+    @RequireRunNotOnVisibleBackgroundNonProfileUser
     public void testPrivacyCameraSettings() throws Exception {
         assumeFalse(
-                "Skipping test: Enabling/Disabling Camera is not supported in AAOS",
-                SettingsTestUtils.isAutomotive());
-        assumeFalse(
                 "Skipping test: Enabling/Disabling Camera is not supported in Wear",
                 SettingsTestUtils.isWatch());
 
@@ -162,7 +189,7 @@
      * Find the specified text.
      */
     private UiObject2 assertFind(String text) {
-        UiObject2 obj = mDevice.findObject(By.text(text));
+        UiObject2 obj = mDevice.findObject(By.text(text).displayId(mDisplayId));
         assertNotNull("Failed to find '" + text + "'.", obj);
         return obj;
     }
@@ -181,19 +208,18 @@
     @Nullable
     private UiObject2 scrollToText(String text) {
         UiObject2 foundObject = null;
-        // Iterate through multiple scrollables.
-        for (int i = 0; i < MAX_NUM_SCROLLABLES; i++) {
-            UiScrollable scrollable = new UiScrollable(
-                    new UiSelector().scrollable(true).instance(i));
-            scrollable.setMaxSearchSwipes(10);
-            try {
-                scrollable.scrollTextIntoView(text);
-            } catch (UiObjectNotFoundException e) {
-                // Ignore the exception if there's no scroll bar.
-            }
-            foundObject = mDevice.findObject(By.text(text));
+        // RecyclerViews take longer to load and aren't found even after waiting for idle
+        mDevice.wait(mSearchCondition, TIMEOUT_MS);
+        // Car-ui-lib replaces settings recyclerviews dynamically so cannot find by resource
+        List<UiObject2> recyclerViews =
+                mDevice.findObjects(By.clazz(RecyclerView.class).displayId(mDisplayId));
+        for (UiObject2 recyclerView : recyclerViews) {
+            // Make sure reclerview starts at the top
+            recyclerView.scroll(Direction.UP, 1);
+            recyclerView.scrollUntil(Direction.DOWN, Until.findObject(By.textContains(text)));
+            foundObject = mDevice.findObject(By.text(text).displayId(mDisplayId));
             if (foundObject != null) {
-                // No need to look at other scrollables.
+                // No need to look at other recyclerviews.
                 break;
             }
         }
@@ -209,14 +235,14 @@
     private void launchActionSettings() {
         final Intent intent = new Intent(ACTION_SETTINGS);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
-        mContext.startActivity(intent);
+        mContext.startActivityAsUser(intent, mContext.getUser());
     }
 
     private void goHome() {
         final Intent home = new Intent(Intent.ACTION_MAIN);
         home.addCategory(Intent.CATEGORY_HOME);
         home.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        mContext.startActivity(home);
+        mContext.startActivityAsUser(home, mContext.getUser());
         mDevice.waitForIdle();
     }