From 73a3d95906b14ff2f4ec79f3a4f33294ab66f409 Mon Sep 17 00:00:00 2001 From: Anna Bauza Date: Thu, 22 Jun 2023 14:00:45 +0000 Subject: [PATCH] Fix Launcher3 issues due to hardcoded default user Bug: 288407058 Bug: 288406660 Bug: 288406673 Test: atest NexusLauncherOutOfProcTests:com.android.launcher3.ui.TaplTestsLauncher3#testUninstallFromWorkspace Test: atest NexusLauncherOutOfProcTests:com.android.launcher3.ui.TaplTestsLauncher3#testUninstallFromAllApps Test: atest NexusLauncherOutOfProcTests:com.android.launcher3.ui.TaplTestsLauncher3#getIconsPosition_afterIconRemoved_notContained Change-Id: Ibe0c4d8f21721e08b2496bfb5c470903e785c2c5 --- .../com/android/launcher3/util/TestUtil.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/src/com/android/launcher3/util/TestUtil.java b/tests/src/com/android/launcher3/util/TestUtil.java index 7332b836f8..4cd6c539d1 100644 --- a/tests/src/com/android/launcher3/util/TestUtil.java +++ b/tests/src/com/android/launcher3/util/TestUtil.java @@ -67,11 +67,11 @@ public class TestUtil { 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 { @@ -106,6 +106,23 @@ public class TestUtil { } } + /** + * 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