Merge "Fix Launcher3 issues due to hardcoded default user" into udc-qpr-dev am: fbb34d205a am: 09e2dc9d6c

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/23765918

Change-Id: I292111cf0d35846f7390257d6a3c4cd90e10d16c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/tests/src/com/android/launcher3/util/TestUtil.java b/tests/src/com/android/launcher3/util/TestUtil.java
index 7332b83..4cd6c53 100644
--- a/tests/src/com/android/launcher3/util/TestUtil.java
+++ b/tests/src/com/android/launcher3/util/TestUtil.java
@@ -67,11 +67,11 @@
     private static final String TAG = "TestUtil";
 
     public static final String DUMMY_PACKAGE = "com.example.android.aardwolf";
-    public static final int DEFAULT_USER_ID = 0;
     public static final long DEFAULT_UI_TIMEOUT = 10000;
 
     public static void installDummyApp() throws IOException {
-        installDummyAppForUser(DEFAULT_USER_ID);
+        final int defaultUserId = getMainUserId();
+        installDummyAppForUser(defaultUserId);
     }
 
     public static void installDummyAppForUser(int userId) throws IOException {
@@ -107,6 +107,23 @@
     }
 
     /**
+     * Returns the main user ID. NOTE: For headless system it is NOT 0. Returns 0 by default, if
+     * there is no main user.
+     *
+     * @return a main user ID
+     */
+    public static int getMainUserId() throws IOException {
+        Instrumentation instrumentation = getInstrumentation();
+        final String result = UiDevice.getInstance(instrumentation)
+                .executeShellCommand("cmd user get-main-user");
+        try {
+            return Integer.parseInt(result.trim());
+        } catch (NumberFormatException e) {
+            return 0;
+        }
+    }
+
+    /**
      * Utility class to override a boolean flag during test. Note that the returned SafeCloseable
      * must be closed to restore the original state
      */