From 1a03740ebd5790dc7cad61b761c36a72f294dde4 Mon Sep 17 00:00:00 2001 From: Brian Isganitis Date: Wed, 10 Jul 2024 18:35:43 -0400 Subject: [PATCH 1/3] Inline showTaskbarFromBroadcast. Feels like it should be the activities' responsibility to coordinate the EDU with stashing. All of these methods are also public to TaskbarActivityContext. Test: Compiles Bug: 346394503 Flag: EXEMPT refactor Change-Id: Ibedd8caec59da3b3a14c15d0c948bf1069ca3c6c --- .../launcher3/taskbar/TaskbarActivityContext.java | 7 ++++++- .../launcher3/taskbar/TaskbarStashController.java | 12 ------------ 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarActivityContext.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarActivityContext.java index 1166cf7ea6..7b90aa6e87 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarActivityContext.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarActivityContext.java @@ -456,7 +456,12 @@ public class TaskbarActivityContext extends BaseTaskbarContext { * Show Taskbar upon receiving broadcast */ public void showTaskbarFromBroadcast() { - mControllers.taskbarStashController.showTaskbarFromBroadcast(); + // If user is in middle of taskbar education handle go to next step of education + if (mControllers.taskbarEduTooltipController.isBeforeTooltipFeaturesStep()) { + mControllers.taskbarEduTooltipController.hide(); + mControllers.taskbarEduTooltipController.maybeShowFeaturesEdu(); + } + mControllers.taskbarStashController.updateAndAnimateTransientTaskbar(false); } /** Toggles Taskbar All Apps overlay. */ diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarStashController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarStashController.java index 64fb04b8ad..267e19c7f8 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarStashController.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarStashController.java @@ -285,18 +285,6 @@ public class TaskbarStashController implements TaskbarControllers.LoggableTaskba } } - /** - * Show Taskbar upon receiving broadcast - */ - public void showTaskbarFromBroadcast() { - // If user is in middle of taskbar education handle go to next step of education - if (mControllers.taskbarEduTooltipController.isBeforeTooltipFeaturesStep()) { - mControllers.taskbarEduTooltipController.hide(); - mControllers.taskbarEduTooltipController.maybeShowFeaturesEdu(); - } - updateAndAnimateTransientTaskbar(false); - } - /** * Initializes the controller */ From c79c2fd28d367955716aba453139b9a562c4558c Mon Sep 17 00:00:00 2001 From: Brian Isganitis Date: Fri, 12 Jul 2024 15:34:47 -0400 Subject: [PATCH 2/3] Reinject controllers when Taskbar recreates itself. Test: TaskbarUnitTestRuleTest Bug: 230027385 Flag: TEST_ONLY Change-Id: I939d820271ba7e5426303ac4662d1d621d86c08b --- .../taskbar/rules/TaskbarUnitTestRule.kt | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt index 8a64949f7d..a966d2aedb 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarUnitTestRule.kt @@ -136,13 +136,19 @@ class TaskbarUnitTestRule( taskbarManager = TestUtil.getOnUiThread { - TaskbarManager( - context, - AllAppsActionManager(context, UI_HELPER_EXECUTOR) { - PendingIntent(IIntentSender.Default()) - }, - object : TaskbarNavButtonCallbacks {}, - ) + object : + TaskbarManager( + context, + AllAppsActionManager(context, UI_HELPER_EXECUTOR) { + PendingIntent(IIntentSender.Default()) + }, + object : TaskbarNavButtonCallbacks {}, + ) { + override fun recreateTaskbar() { + super.recreateTaskbar() + if (currentActivityContext != null) injectControllers() + } + } } try { @@ -154,7 +160,6 @@ class TaskbarUnitTestRule( taskbarManager.onUserUnlocked() // Required to complete initialization. } - injectControllers() base.evaluate() } finally { // Revert Taskbar window. @@ -168,10 +173,7 @@ class TaskbarUnitTestRule( } /** Simulates Taskbar recreation lifecycle. */ - fun recreateTaskbar() { - instrumentation.runOnMainSync { taskbarManager.recreateTaskbar() } - injectControllers() - } + fun recreateTaskbar() = instrumentation.runOnMainSync { taskbarManager.recreateTaskbar() } private fun injectControllers() { val controllers = activityContext.controllers From 803c85ac219af125ecaf24930f75aafe6223c5f3 Mon Sep 17 00:00:00 2001 From: Brian Isganitis Date: Thu, 11 Jul 2024 13:45:47 -0400 Subject: [PATCH 3/3] Introduce rule for changing taskbar pinning preferences. Test: TaskbarPinningPreferenceRuleTest Bug: 230027385 Flag: TEST_ONLY Change-Id: I2b87ab8823b17c364b3797a325c8b0d569a1d39a --- .../rules/TaskbarPinningPreferenceRule.kt | 71 ++++++++++++++ .../rules/TaskbarPinningPreferenceRuleTest.kt | 98 +++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRule.kt create mode 100644 quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRuleTest.kt 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 new file mode 100644 index 0000000000..60c8deca8d --- /dev/null +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRule.kt @@ -0,0 +1,71 @@ +/* + * 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 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation +import com.android.launcher3.ConstantItem +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 kotlin.reflect.KProperty +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 prefs = LauncherPrefs.get(context) + + var isPinned by PinningPreference(TASKBAR_PINNING) + var isPinnedInDesktopMode by PinningPreference(TASKBAR_PINNING_IN_DESKTOP_MODE) + + override fun apply(base: Statement, description: Description): Statement { + return object : Statement() { + override fun evaluate() { + val wasPinned = isPinned + val wasPinnedInDesktopMode = isPinnedInDesktopMode + try { + base.evaluate() + } finally { + isPinned = wasPinned + isPinnedInDesktopMode = wasPinnedInDesktopMode + } + } + } + } + + private inner class PinningPreference(private val constantItem: ConstantItem) { + operator fun getValue(thisRef: Any?, property: KProperty<*>): Boolean { + return prefs.get(constantItem) + } + + operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) { + getInstrumentation().runOnMainSync { prefs.put(constantItem, value) } + } + } +} 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 new file mode 100644 index 0000000000..3f0a23886f --- /dev/null +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/rules/TaskbarPinningPreferenceRuleTest.kt @@ -0,0 +1,98 @@ +/* + * 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 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation +import com.android.launcher3.util.DisplayController +import com.android.launcher3.util.LauncherMultivalentJUnit +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.Description +import org.junit.runner.RunWith +import org.junit.runners.model.Statement + +@RunWith(LauncherMultivalentJUnit::class) +class TaskbarPinningPreferenceRuleTest { + private val context = TaskbarWindowSandboxContext.create(getInstrumentation().targetContext) + + private val preferenceRule = TaskbarPinningPreferenceRule(context) + + @Test + fun testEnablePinning_verifyDisplayController() { + onSetup { + preferenceRule.isPinned = true + preferenceRule.isPinnedInDesktopMode = false + assertThat(DisplayController.isPinnedTaskbar(context)).isTrue() + } + } + + @Test + fun testDisablePinning_verifyDisplayController() { + onSetup { + preferenceRule.isPinned = false + preferenceRule.isPinnedInDesktopMode = false + assertThat(DisplayController.isPinnedTaskbar(context)).isFalse() + } + } + + @Test + fun testEnableDesktopPinning_verifyDisplayController() { + onSetup { + preferenceRule.isPinned = false + preferenceRule.isPinnedInDesktopMode = true + assertThat(DisplayController.isPinnedTaskbar(context)).isTrue() + } + } + + @Test + fun testDisableDesktopPinning_verifyDisplayController() { + onSetup { + preferenceRule.isPinned = false + preferenceRule.isPinnedInDesktopMode = false + assertThat(DisplayController.isPinnedTaskbar(context)).isFalse() + } + } + + @Test + fun testTearDown_afterTogglingPinnedPreference_preferenceReset() { + val wasPinned = preferenceRule.isPinned + onSetup { preferenceRule.isPinned = !preferenceRule.isPinned } + assertThat(preferenceRule.isPinned).isEqualTo(wasPinned) + } + + @Test + fun testTearDown_afterTogglingDesktopPreference_preferenceReset() { + val wasPinnedInDesktopMode = preferenceRule.isPinnedInDesktopMode + onSetup { preferenceRule.isPinnedInDesktopMode = !preferenceRule.isPinnedInDesktopMode } + assertThat(preferenceRule.isPinnedInDesktopMode).isEqualTo(wasPinnedInDesktopMode) + } + + /** Executes [runTest] after the [preferenceRule] setup phase completes. */ + private fun onSetup(runTest: () -> Unit) { + preferenceRule.apply( + object : Statement() { + override fun evaluate() = runTest() + }, + DESCRIPTION, + ) + } + + private companion object { + private val DESCRIPTION = + Description.createSuiteDescription(TaskbarPinningPreferenceRule::class.java) + } +}