diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRule.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRule.kt index 60c8deca8d..2d26bbfe16 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRule.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRule.kt @@ -16,11 +16,15 @@ package com.android.launcher3.taskbar.rules +import android.platform.test.flag.junit.FlagsParameterization +import android.platform.test.flag.junit.SetFlagsRule import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import com.android.launcher3.ConstantItem +import com.android.launcher3.Flags.FLAG_ENABLE_TASKBAR_PINNING import com.android.launcher3.LauncherPrefs import com.android.launcher3.LauncherPrefs.Companion.TASKBAR_PINNING import com.android.launcher3.LauncherPrefs.Companion.TASKBAR_PINNING_IN_DESKTOP_MODE +import com.android.launcher3.util.DisplayController import kotlin.reflect.KProperty import org.junit.rules.TestRule import org.junit.runner.Description @@ -40,13 +44,20 @@ import org.junit.runners.model.Statement class TaskbarPinningPreferenceRule(context: TaskbarWindowSandboxContext) : TestRule { private val prefs = LauncherPrefs.get(context) + private val setFlagsRule = + SetFlagsRule(FlagsParameterization(mapOf(FLAG_ENABLE_TASKBAR_PINNING to true))) var isPinned by PinningPreference(TASKBAR_PINNING) var isPinnedInDesktopMode by PinningPreference(TASKBAR_PINNING_IN_DESKTOP_MODE) override fun apply(base: Statement, description: Description): Statement { + return setFlagsRule.apply(createStatement(base), description) + } + + private fun createStatement(base: Statement): Statement { return object : Statement() { override fun evaluate() { + DisplayController.enableTaskbarModePreferenceForTests(true) val wasPinned = isPinned val wasPinnedInDesktopMode = isPinnedInDesktopMode try { @@ -54,6 +65,7 @@ class TaskbarPinningPreferenceRule(context: TaskbarWindowSandboxContext) : TestR } finally { isPinned = wasPinned isPinnedInDesktopMode = wasPinnedInDesktopMode + DisplayController.enableTaskbarModePreferenceForTests(false) } } } diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRuleTest.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRuleTest.kt index 3f0a23886f..a709133b34 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRuleTest.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRuleTest.kt @@ -19,6 +19,8 @@ package com.android.launcher3.taskbar.rules import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import com.android.launcher3.util.DisplayController import com.android.launcher3.util.LauncherMultivalentJUnit +import com.android.launcher3.util.window.WindowManagerProxy +import com.google.android.apps.nexuslauncher.deviceemulator.TestWindowManagerProxy import com.google.common.truth.Truth.assertThat import org.junit.Test import org.junit.runner.Description @@ -51,6 +53,11 @@ class TaskbarPinningPreferenceRuleTest { @Test fun testEnableDesktopPinning_verifyDisplayController() { + context.applicationContext.putObject( + WindowManagerProxy.INSTANCE, + TestWindowManagerProxy(context).apply { isInDesktopMode = true }, + ) + onSetup { preferenceRule.isPinned = false preferenceRule.isPinnedInDesktopMode = true @@ -60,6 +67,11 @@ class TaskbarPinningPreferenceRuleTest { @Test fun testDisableDesktopPinning_verifyDisplayController() { + context.applicationContext.putObject( + WindowManagerProxy.INSTANCE, + TestWindowManagerProxy(context).apply { isInDesktopMode = true }, + ) + onSetup { preferenceRule.isPinned = false preferenceRule.isPinnedInDesktopMode = false @@ -83,12 +95,14 @@ class TaskbarPinningPreferenceRuleTest { /** Executes [runTest] after the [preferenceRule] setup phase completes. */ private fun onSetup(runTest: () -> Unit) { - preferenceRule.apply( - object : Statement() { - override fun evaluate() = runTest() - }, - DESCRIPTION, - ) + preferenceRule + .apply( + object : Statement() { + override fun evaluate() = runTest() + }, + DESCRIPTION, + ) + .evaluate() } private companion object { diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarWindowSandboxContext.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarWindowSandboxContext.kt index 321e7a93de..ee21df8f25 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarWindowSandboxContext.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarWindowSandboxContext.kt @@ -50,7 +50,7 @@ private constructor(private val application: SandboxContext, base: Context) : Co ) } - override fun getApplicationContext() = application + override fun getApplicationContext(): SandboxContext = application companion object { /** Creates a [TaskbarWindowSandboxContext] to sandbox [base] for Taskbar tests. */ diff --git a/src/com/android/launcher3/util/DisplayController.java b/src/com/android/launcher3/util/DisplayController.java index 3dcc663c91..5b2f737895 100644 --- a/src/com/android/launcher3/util/DisplayController.java +++ b/src/com/android/launcher3/util/DisplayController.java @@ -76,6 +76,7 @@ public class DisplayController implements ComponentCallbacks, SafeCloseable { private static final String TAG = "DisplayController"; private static final boolean DEBUG = false; + private static boolean sTaskbarModePreferenceStatusForTests = false; private static boolean sTransientTaskbarStatusForTests = true; // TODO(b/254119092) remove all logs with this tag @@ -202,6 +203,14 @@ public class DisplayController implements ComponentCallbacks, SafeCloseable { sTransientTaskbarStatusForTests = enable; } + /** + * Enables respecting taskbar mode preference during test. + */ + @VisibleForTesting + public static void enableTaskbarModePreferenceForTests(boolean enable) { + sTaskbarModePreferenceStatusForTests = enable; + } + /** * Returns whether the taskbar is pinned in gesture navigation mode. */ @@ -461,7 +470,7 @@ public class DisplayController implements ComponentCallbacks, SafeCloseable { if (navigationMode != NavigationMode.NO_BUTTON) { return false; } - if (Utilities.isRunningInTestHarness()) { + if (Utilities.isRunningInTestHarness() && !sTaskbarModePreferenceStatusForTests) { // TODO(b/258604917): Once ENABLE_TASKBAR_PINNING is enabled, remove usage of // sTransientTaskbarStatusForTests and update test to directly // toggle shared preference to switch transient taskbar on/off.