Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e3d3d653 authored by Brian Isganitis's avatar Brian Isganitis
Browse files

Taskbar Pinning Preference Rule fixes.

- Actually evaluate statement in tests.
- Enable aconfig flag for taskbar pinning.
- Allow DisplayController Taskbar mode to change in test harness.
- Make DisplayController think it's in desktop mode when needed.

Flag: TEST_ONLY
Bug: 230027385
Test: TaskbarPinningPreferenceRuleTest

Change-Id: I1c55f73cb5a4bb903cd2d628fa47fbd5e8b1bb86
parent 803c85ac
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -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)
                }
            }
        }
+20 −6
Original line number Diff line number Diff line
@@ -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(
        preferenceRule
            .apply(
                object : Statement() {
                    override fun evaluate() = runTest()
                },
                DESCRIPTION,
            )
            .evaluate()
    }

    private companion object {
+1 −1
Original line number Diff line number Diff line
@@ -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. */
+10 −1
Original line number Diff line number Diff line
@@ -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.