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

Commit dc4f5604 authored by Brian Isganitis's avatar Brian Isganitis Committed by Android (Google) Code Review
Browse files

Merge "Migrate to FakeLauncherPrefs for Taskbar unit tests." into main

parents d2867286 a3ec98e0
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -16,10 +16,34 @@

package com.android.launcher3.taskbar

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.android.launcher3.ConstantItem
import com.android.launcher3.LauncherPrefs
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

object TaskbarControllerTestUtil {
    inline fun runOnMainSync(crossinline runTest: () -> Unit) {
        getInstrumentation().runOnMainSync { runTest() }
    }

    /** Returns a property to read/write the value of a [ConstantItem]. */
    fun <T : Any> ConstantItem<T>.asProperty(context: Context): ReadWriteProperty<Any?, T> {
        return TaskbarItemProperty(context, this)
    }

    private class TaskbarItemProperty<T : Any>(
        private val context: Context,
        private val item: ConstantItem<T>,
    ) : ReadWriteProperty<Any?, T> {

        override fun getValue(thisRef: Any?, property: KProperty<*>): T {
            return LauncherPrefs.get(context).get(item)
        }

        override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
            runOnMainSync { LauncherPrefs.get(context).put(item, value) }
        }
    }
}
+17 −25
Original line number Diff line number Diff line
@@ -18,14 +18,13 @@ package com.android.launcher3.taskbar

import android.util.Log
import com.android.launcher3.Utilities
import com.android.launcher3.taskbar.TaskbarControllerTestUtil.asProperty
import com.android.launcher3.taskbar.TaskbarControllerTestUtil.runOnMainSync
import com.android.launcher3.taskbar.rules.TaskbarModeRule
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.PINNED
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.THREE_BUTTONS
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.TRANSIENT
import com.android.launcher3.taskbar.rules.TaskbarModeRule.TaskbarMode
import com.android.launcher3.taskbar.rules.TaskbarPinningPreferenceRule
import com.android.launcher3.taskbar.rules.TaskbarPreferenceRule
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule.InjectController
import com.android.launcher3.taskbar.rules.TaskbarWindowSandboxContext
@@ -47,19 +46,9 @@ class TaskbarEduTooltipControllerTest {

    @get:Rule(order = 0) val context = TaskbarWindowSandboxContext.create()

    @get:Rule(order = 1)
    val tooltipStepPreferenceRule =
        TaskbarPreferenceRule(context, OnboardingPrefs.TASKBAR_EDU_TOOLTIP_STEP.prefItem)
    @get:Rule(order = 1) val taskbarModeRule = TaskbarModeRule(context)

    @get:Rule(order = 2)
    val searchEduPreferenceRule =
        TaskbarPreferenceRule(context, OnboardingPrefs.TASKBAR_SEARCH_EDU_SEEN)

    @get:Rule(order = 3) val taskbarPinningPreferenceRule = TaskbarPinningPreferenceRule(context)

    @get:Rule(order = 4) val taskbarModeRule = TaskbarModeRule(context)

    @get:Rule(order = 5) val taskbarUnitTestRule = TaskbarUnitTestRule(this, context)
    @get:Rule(order = 2) val taskbarUnitTestRule = TaskbarUnitTestRule(this, context)

    @InjectController lateinit var taskbarEduTooltipController: TaskbarEduTooltipController

@@ -68,6 +57,9 @@ class TaskbarEduTooltipControllerTest {

    private val wasInTestHarness = Utilities.isRunningInTestHarness()

    private var tooltipStep by OnboardingPrefs.TASKBAR_EDU_TOOLTIP_STEP.prefItem.asProperty(context)
    private var searchEduSeen by OnboardingPrefs.TASKBAR_SEARCH_EDU_SEEN.asProperty(context)

    @Before
    fun setUp() {
        Log.e("Taskbar", "TaskbarEduTooltipControllerTest test started")
@@ -85,7 +77,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(THREE_BUTTONS)
    fun testMaybeShowSwipeEdu_whenTaskbarIsInThreeButtonMode_doesNotShowSwipeEdu() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_SWIPE
        tooltipStep = TOOLTIP_STEP_SWIPE
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_SWIPE)
        runOnMainSync { taskbarEduTooltipController.maybeShowSwipeEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_SWIPE)
@@ -95,7 +87,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(TRANSIENT)
    fun testMaybeShowSwipeEdu_whenSwipeEduAlreadyShown_doesNotShowSwipeEdu() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_FEATURES
        tooltipStep = TOOLTIP_STEP_FEATURES
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_FEATURES)
        runOnMainSync { taskbarEduTooltipController.maybeShowSwipeEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_FEATURES)
@@ -105,7 +97,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(TRANSIENT)
    fun testMaybeShowSwipeEdu_whenUserHasNotSeen_doesShowSwipeEdu() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_SWIPE
        tooltipStep = TOOLTIP_STEP_SWIPE
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_SWIPE)
        runOnMainSync { taskbarEduTooltipController.maybeShowSwipeEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_FEATURES)
@@ -115,7 +107,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(TRANSIENT)
    fun testMaybeShowFeaturesEdu_whenFeatureEduAlreadyShown_doesNotShowFeatureEdu() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_NONE
        tooltipStep = TOOLTIP_STEP_NONE
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_NONE)
        runOnMainSync { taskbarEduTooltipController.maybeShowFeaturesEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_NONE)
@@ -125,7 +117,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(TRANSIENT)
    fun testMaybeShowFeaturesEdu_whenUserHasNotSeen_doesShowFeatureEdu() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_FEATURES
        tooltipStep = TOOLTIP_STEP_FEATURES
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_FEATURES)
        runOnMainSync { taskbarEduTooltipController.maybeShowFeaturesEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_NONE)
@@ -135,7 +127,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(THREE_BUTTONS)
    fun testMaybeShowPinningEdu_whenTaskbarIsInThreeButtonMode_doesNotShowPinningEdu() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_PINNING
        tooltipStep = TOOLTIP_STEP_PINNING
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_PINNING)
        runOnMainSync { taskbarEduTooltipController.maybeShowFeaturesEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_PINNING)
@@ -146,7 +138,7 @@ class TaskbarEduTooltipControllerTest {
    @TaskbarMode(TRANSIENT)
    fun testMaybeShowPinningEdu_whenUserHasNotSeen_doesShowPinningEdu() {
        // Test standalone pinning edu, where user has seen taskbar edu before, but not pinning edu.
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_PINNING
        tooltipStep = TOOLTIP_STEP_PINNING
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_PINNING)
        runOnMainSync { taskbarEduTooltipController.maybeShowFeaturesEdu() }
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_NONE)
@@ -156,21 +148,21 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(TRANSIENT)
    fun testIsBeforeTooltipFeaturesStep_whenUserHasNotSeenFeatureEdu_shouldReturnTrue() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_SWIPE
        tooltipStep = TOOLTIP_STEP_SWIPE
        assertThat(taskbarEduTooltipController.isBeforeTooltipFeaturesStep).isTrue()
    }

    @Test
    @TaskbarMode(TRANSIENT)
    fun testIsBeforeTooltipFeaturesStep_whenUserHasSeenFeatureEdu_shouldReturnFalse() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_NONE
        tooltipStep = TOOLTIP_STEP_NONE
        assertThat(taskbarEduTooltipController.isBeforeTooltipFeaturesStep).isFalse()
    }

    @Test
    @TaskbarMode(TRANSIENT)
    fun testHide_whenTooltipIsOpen_shouldCloseTooltip() {
        tooltipStepPreferenceRule.value = TOOLTIP_STEP_SWIPE
        tooltipStep = TOOLTIP_STEP_SWIPE
        assertThat(taskbarEduTooltipController.tooltipStep).isEqualTo(TOOLTIP_STEP_SWIPE)
        assertThat(taskbarEduTooltipController.isTooltipOpen).isFalse()
        runOnMainSync { taskbarEduTooltipController.maybeShowSwipeEdu() }
@@ -190,7 +182,7 @@ class TaskbarEduTooltipControllerTest {
    @Test
    @TaskbarMode(PINNED)
    fun testMaybeShowSearchEdu_whenTaskbarIsPinnedAndUserHasSeenSearchEdu_shouldNotShowSearchEdu() {
        searchEduPreferenceRule.value = true
        searchEduSeen = true
        assertThat(taskbarEduTooltipController.userHasSeenSearchEdu).isTrue()
        runOnMainSync { taskbarEduTooltipController.hide() }
        assertThat(taskbarEduTooltipController.isTooltipOpen).isFalse()
+11 −8
Original line number Diff line number Diff line
@@ -18,10 +18,13 @@ package com.android.launcher3.taskbar

import android.animation.AnimatorTestRule
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.android.launcher3.LauncherPrefs.Companion.TASKBAR_PINNING
import com.android.launcher3.R
import com.android.launcher3.taskbar.StashedHandleViewController.ALPHA_INDEX_STASHED
import com.android.launcher3.taskbar.TaskbarAutohideSuspendController.FLAG_AUTOHIDE_SUSPEND_EDU_OPEN
import com.android.launcher3.taskbar.TaskbarControllerTestUtil.asProperty
import com.android.launcher3.taskbar.TaskbarStashController.FLAG_IN_APP
import com.android.launcher3.taskbar.TaskbarStashController.FLAG_IN_OVERVIEW
import com.android.launcher3.taskbar.TaskbarStashController.FLAG_IN_STASHED_LAUNCHER_STATE
@@ -42,7 +45,6 @@ import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.PINNED
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.THREE_BUTTONS
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.TRANSIENT
import com.android.launcher3.taskbar.rules.TaskbarModeRule.TaskbarMode
import com.android.launcher3.taskbar.rules.TaskbarPinningPreferenceRule
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule.InjectController
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule.UserSetupMode
@@ -63,11 +65,11 @@ import org.junit.runner.RunWith
@EnableFlags(FLAG_ENABLE_BUBBLE_BAR)
@EmulatedDevices(["pixelTablet2023"])
class TaskbarStashControllerTest {
    @get:Rule(order = 0) val context = TaskbarWindowSandboxContext.create()
    @get:Rule(order = 1) val taskbarModeRule = TaskbarModeRule(context)
    @get:Rule(order = 2) val taskbarPinningPreferenceRule = TaskbarPinningPreferenceRule(context)
    @get:Rule(order = 3) val animatorTestRule = AnimatorTestRule(this)
    @get:Rule(order = 4) val taskbarUnitTestRule = TaskbarUnitTestRule(this, context)
    @get:Rule(order = 0) val setFlagsRule = SetFlagsRule()
    @get:Rule(order = 1) val context = TaskbarWindowSandboxContext.create()
    @get:Rule(order = 2) val taskbarModeRule = TaskbarModeRule(context)
    @get:Rule(order = 4) val animatorTestRule = AnimatorTestRule(this)
    @get:Rule(order = 5) val taskbarUnitTestRule = TaskbarUnitTestRule(this, context)

    @InjectController lateinit var stashController: TaskbarStashController
    @InjectController lateinit var viewController: TaskbarViewController
@@ -121,10 +123,11 @@ class TaskbarStashControllerTest {

    @Test
    fun testRecreateAsTransient_timeoutStarted() {
        taskbarPinningPreferenceRule.isPinned = true
        var isPinned by TASKBAR_PINNING.asProperty(context)
        isPinned = true
        activityContext.controllers.sharedState?.taskbarWasPinned = true

        taskbarPinningPreferenceRule.isPinned = false
        isPinned = false
        assertThat(stashController.timeoutAlarm.alarmPending()).isTrue()
    }

+5 −7
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import com.android.launcher3.R
import com.android.launcher3.appprediction.AppsDividerView
import com.android.launcher3.appprediction.AppsDividerView.DividerType
import com.android.launcher3.appprediction.PredictionRowView
import com.android.launcher3.taskbar.TaskbarControllerTestUtil.asProperty
import com.android.launcher3.taskbar.TaskbarStashController
import com.android.launcher3.taskbar.TaskbarStashController.FLAG_STASHED_IN_APP_AUTO
import com.android.launcher3.taskbar.allapps.TaskbarAllAppsControllerTest.Companion.TEST_PREDICTED_APPS
@@ -29,7 +30,6 @@ import com.android.launcher3.taskbar.rules.TaskbarModeRule
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.PINNED
import com.android.launcher3.taskbar.rules.TaskbarModeRule.Mode.TRANSIENT
import com.android.launcher3.taskbar.rules.TaskbarModeRule.TaskbarMode
import com.android.launcher3.taskbar.rules.TaskbarPreferenceRule
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule
import com.android.launcher3.taskbar.rules.TaskbarUnitTestRule.InjectController
import com.android.launcher3.taskbar.rules.TaskbarWindowSandboxContext
@@ -49,14 +49,12 @@ class TaskbarAllAppsViewControllerTest {

    @get:Rule(order = 0) val context = TaskbarWindowSandboxContext.create()
    @get:Rule(order = 1) val taskbarModeRule = TaskbarModeRule(context)
    @get:Rule(order = 2)
    val allAppsVisitedPreferenceRule =
        TaskbarPreferenceRule(context, ALL_APPS_VISITED_COUNT.prefItem)
    @get:Rule(order = 3) val taskbarUnitTestRule = TaskbarUnitTestRule(this, context)
    @get:Rule(order = 2) val taskbarUnitTestRule = TaskbarUnitTestRule(this, context)

    @InjectController lateinit var overlayController: TaskbarOverlayController
    @InjectController lateinit var stashController: TaskbarStashController

    private var allAppsVisitedCount by ALL_APPS_VISITED_COUNT.prefItem.asProperty(context)
    private val searchSessionController =
        TestUtil.getOnUiThread { TaskbarSearchSessionController.newInstance(context) }

@@ -102,7 +100,7 @@ class TaskbarAllAppsViewControllerTest {

    @Test
    fun testShow_firstAllAppsVisit_hasAllAppsTextDivider() {
        allAppsVisitedPreferenceRule.value = 0
        allAppsVisitedCount = 0
        val viewController = createViewController()
        getInstrumentation().runOnMainSync { viewController.show(false) }

@@ -120,7 +118,7 @@ class TaskbarAllAppsViewControllerTest {

    @Test
    fun testShow_maxAllAppsVisitedCount_hasLineDivider() {
        allAppsVisitedPreferenceRule.value = ALL_APPS_VISITED_COUNT.maxCount
        allAppsVisitedCount = ALL_APPS_VISITED_COUNT.maxCount
        val viewController = createViewController()
        getInstrumentation().runOnMainSync { viewController.show(false) }

+0 −65
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.taskbar.rules

import android.platform.test.flag.junit.FlagsParameterization
import android.platform.test.flag.junit.SetFlagsRule
import com.android.launcher3.Flags.FLAG_ENABLE_TASKBAR_PINNING
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 org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
 * Rule that allows modifying the Taskbar pinned preferences.
 *
 * The original preference values are restored on teardown.
 *
 * If this rule is being used with [TaskbarUnitTestRule], make sure this rule is applied first.
 *
 * This rule is overkill if a test does not need to change the mode during Taskbar's lifecycle. If
 * the mode is static, use [TaskbarModeRule] instead, which forces the mode. A test can class can
 * declare both this rule and [TaskbarModeRule] but using both for a test method is unsupported.
 */
class TaskbarPinningPreferenceRule(context: TaskbarWindowSandboxContext) : TestRule {

    private val setFlagsRule =
        SetFlagsRule(FlagsParameterization(mapOf(FLAG_ENABLE_TASKBAR_PINNING to true)))
    private val pinningRule = TaskbarPreferenceRule(context, TASKBAR_PINNING)
    private val desktopPinningRule = TaskbarPreferenceRule(context, TASKBAR_PINNING_IN_DESKTOP_MODE)
    private val ruleChain =
        RuleChain.outerRule(setFlagsRule).around(pinningRule).around(desktopPinningRule)

    var isPinned by pinningRule::value
    var isPinnedInDesktopMode by desktopPinningRule::value

    override fun apply(base: Statement, description: Description): Statement {
        return object : Statement() {
            override fun evaluate() {
                DisplayController.enableTaskbarModePreferenceForTests(true)
                try {
                    ruleChain.apply(base, description).evaluate()
                } finally {
                    DisplayController.enableTaskbarModePreferenceForTests(false)
                }
            }
        }
    }
}
Loading