From ed04537faa5025e06462823f1b33490483e5540e Mon Sep 17 00:00:00 2001 From: Wen-Chien Wang Date: Tue, 18 Mar 2025 19:02:41 +0000 Subject: [PATCH] pin-shortcut: Add tests to verify the behavior Bug: 375648361 Test: This is the cl for tests Flag: EXEMPT tests Change-Id: I7209cebcc7e7c34d5f1362121fea5e062e3170de --- .../taskbar/TaskbarPopupController.java | 4 +- .../launcher3/taskbar/TaskbarOverflowTest.kt | 164 ++++++++++++++++++ .../launcher3/taskbar/TaskbarViewTestUtil.kt | 20 ++- 3 files changed, 179 insertions(+), 9 deletions(-) diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java index f8240720ce..b92357bddc 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarPopupController.java @@ -32,6 +32,7 @@ import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; import com.android.internal.logging.InstanceId; import com.android.launcher3.AbstractFloatingView; @@ -222,7 +223,8 @@ public class TaskbarPopupController implements TaskbarControllers.LoggableTaskba } @Nullable - private SystemShortcut createPinShortcut(BaseTaskbarContext target, ItemInfo itemInfo, + @VisibleForTesting + SystemShortcut createPinShortcut(BaseTaskbarContext target, ItemInfo itemInfo, BubbleTextView originalView) { // Predicted items use {@code HotseatPredictionController.PinPrediction} shortcut to pin. if (itemInfo.isPredictedItem()) { diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarOverflowTest.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarOverflowTest.kt index dd8d73118e..9966bef7b0 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarOverflowTest.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarOverflowTest.kt @@ -25,11 +25,17 @@ import android.platform.test.annotations.EnableFlags import android.platform.test.flag.junit.SetFlagsRule import android.view.Display.DEFAULT_DISPLAY import androidx.test.core.app.ApplicationProvider +import com.android.launcher3.BubbleTextView import com.android.launcher3.Flags.FLAG_ENABLE_MULTI_INSTANCE_MENU_TASKBAR import com.android.launcher3.Flags.FLAG_TASKBAR_OVERFLOW import com.android.launcher3.R import com.android.launcher3.dagger.LauncherAppSingleton +import com.android.launcher3.dagger.LauncherComponentProvider.appComponent +import com.android.launcher3.model.BgDataModel import com.android.launcher3.model.data.ItemInfo +import com.android.launcher3.model.data.TaskItemInfo +import com.android.launcher3.model.data.WorkspaceItemInfo +import com.android.launcher3.popup.SystemShortcut import com.android.launcher3.taskbar.TaskbarControllerTestUtil.runOnMainSync import com.android.launcher3.taskbar.TaskbarViewTestUtil.createHotseatItems import com.android.launcher3.taskbar.bubbles.BubbleBarViewController @@ -50,18 +56,24 @@ import com.android.launcher3.util.AllModulesForTest import com.android.launcher3.util.FakePrefsModule import com.android.launcher3.util.LauncherMultivalentJUnit import com.android.launcher3.util.LauncherMultivalentJUnit.EmulatedDevices +import com.android.launcher3.util.Preconditions.assertNotNull import com.android.launcher3.util.TestUtil.getOnUiThread import com.android.quickstep.RecentsModel import com.android.quickstep.SystemUiProxy import com.android.quickstep.util.DesktopTask +import com.android.quickstep.util.GroupTask +import com.android.quickstep.util.SingleTask +import com.android.quickstep.util.SingleTask.Companion.createTaskItemInfo import com.android.systemui.shared.recents.model.Task import com.android.window.flags.Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MODE import com.android.window.flags.Flags.FLAG_ENABLE_DESKTOP_WINDOWING_TASKBAR_RUNNING_APPS +import com.android.window.flags.Flags.FLAG_ENABLE_PINNING_APP_WITH_CONTEXT_MENU import com.android.wm.shell.Flags.FLAG_ENABLE_BUBBLE_BAR import com.android.wm.shell.desktopmode.IDesktopTaskListener import com.google.common.truth.Truth.assertThat import dagger.BindsInstance import dagger.Component +import java.util.function.Predicate import org.junit.Before import org.junit.Rule import org.junit.Test @@ -117,6 +129,10 @@ class TaskbarOverflowTest { @InjectController lateinit var keyboardQuickSwitchController: KeyboardQuickSwitchController private var desktopTaskListener: IDesktopTaskListener? = null + private val modelCallback = ModelCallbacks() + + private val taskbarContext: TaskbarActivityContext + get() = taskbarUnitTestRule.activityContext private var currentControllerInitCallback: () -> Unit = {} set(value) { @@ -431,6 +447,126 @@ class TaskbarOverflowTest { .containsExactlyElementsIn(listOf(0) + (2..? = null + var hotseatIcon: BubbleTextView? = null + runOnMainSync { + val taskbarView = setUpTaskbarAndModelCallback(hotseatItems) + hotseatIcon = + taskbarView.iconViews.filterIsInstance().first { + it.tag is WorkspaceItemInfo + } + shortcut = + taskbarContext.controllers.taskbarPopupController.createPinShortcut( + taskbarContext, + hotseatIcon!!.tag as ItemInfo, + hotseatIcon, + ) as SystemShortcut<*> + } + assertNotNull(shortcut) + runOnMainSync { shortcut?.onClick(hotseatIcon) } + // After unpinning the first item, only the second app is left. + assertThat(modelCallback.hotseatItems.map { info -> info.title }) + .isEqualTo(listOf("Test App 1")) + // The unpinned app doesn't have a task so the shown tasks won't change. + assertThat(recentAppsController.shownTasks.map { it.tasks[0].key.id }) + .isEqualTo(listOf(0, 1)) + } + + @Test + @TaskbarMode(PINNED) + @EnableFlags(FLAG_ENABLE_PINNING_APP_WITH_CONTEXT_MENU) + fun pinToTaskbarShortcut_unpinPinnedItemWithTask() { + // Create two hotseat items with a task for both of them respectively. + var hotseatItems = + createHotseatItems(2).mapIndexed { idx, item -> TaskItemInfo(idx, item) }.toTypedArray() + createDesktopTaskWithTasksFromPackages(hotseatItems.mapNotNull { it.targetPackage }) + var shortcut: SystemShortcut<*>? = null + var hotseatIcon: BubbleTextView? = null + runOnMainSync { + val taskbarView = setUpTaskbarAndModelCallback(hotseatItems.map { it }.toTypedArray()) + hotseatIcon = + taskbarView.iconViews.filterIsInstance().first { + it.tag is WorkspaceItemInfo + } + shortcut = + taskbarContext.controllers.taskbarPopupController.createPinShortcut( + taskbarContext, + hotseatIcon!!.tag as ItemInfo, + hotseatIcon, + ) as SystemShortcut<*> + } + // Before unpinning the app, both of the apps should be pinned and no shown task available. + assertThat(modelCallback.hotseatItems.map { info -> info.title }) + .isEqualTo(listOf("Test App 0", "Test App 1")) + assertThat(recentAppsController.shownTasks.map { it.tasks[0].key.id }) + .isEqualTo(emptyList()) + assertNotNull(shortcut) + runOnMainSync { shortcut?.onClick(hotseatIcon) } + // After unpinning the app, app 0 is removed and its task is shown as a recent task. + assertThat(modelCallback.hotseatItems.map { info -> info.title }) + .isEqualTo(listOf("Test App 1")) + assertThat(recentAppsController.shownTasks.map { it.tasks[0].key.id }).isEqualTo(listOf(0)) + } + + @Test + @TaskbarMode(PINNED) + @EnableFlags(FLAG_ENABLE_PINNING_APP_WITH_CONTEXT_MENU) + fun pinToTaskbarShortcut_pinRecentTask() { + // Create two tasks and two pinned items. + createDesktopTask(2) + val hotseatItems = createHotseatItems(2) + + var shortcut: SystemShortcut<*>? = null + var recentTaskIcon: BubbleTextView? = null + runOnMainSync { + val taskbarView = setUpTaskbarAndModelCallback(hotseatItems) + // Get the first recent task icon + recentTaskIcon = + taskbarView.iconViews.filterIsInstance().first { + it.tag is GroupTask + } + val recentTaskInfo = + createTaskItemInfo( + recentTaskIcon!!.tag as SingleTask, + WorkspaceItemInfo().apply { + title = "Test App 2" + intent = Intent().apply { `package` = "fake" } + }, + ) + shortcut = + taskbarContext.controllers.taskbarPopupController.createPinShortcut( + taskbarContext, + recentTaskInfo, + recentTaskIcon, + ) as SystemShortcut<*> + } + assertNotNull(shortcut) + runOnMainSync { shortcut?.onClick(recentTaskIcon) } + + // After pinning the recent task, it should be included in the hotseat items. + assertThat(modelCallback.hotseatItems.map { info -> info.title }) + .isEqualTo(listOf("Test App 0", "Test App 1", "Test App 2")) + // As the task is pinned, the shown tasks should remove it from the list + assertThat(recentAppsController.shownTasks.map { it.tasks[0].key.id }).isEqualTo(listOf(1)) + } + + private fun setUpTaskbarAndModelCallback(hotseatItems: Array): TaskbarView { + val taskbarView: TaskbarView = + taskbarUnitTestRule.activityContext.dragLayer.findViewById(R.id.taskbar_view) + taskbarView.updateItems(hotseatItems, recentAppsController.shownTasks) + modelCallback.recentAppsController = recentAppsController + context.baseContext.appComponent.launcherAppState.model.addCallbacksAndLoad(modelCallback) + modelCallback.bindItems(hotseatItems.toList(), false) + return taskbarView + } + private fun createDesktopTask(tasksToAdd: Int) { createDesktopTaskWithTasksFromPackages((0..() + var recentAppsController: TaskbarRecentAppsController? = null + + override fun bindItems(shortcuts: List, forceAnimateIcons: Boolean) { + runOnMainSync { + shortcuts + .filter { item -> + item is WorkspaceItemInfo && + !hotseatItems.any { it.targetPackage == item.targetPackage } + } + .forEach { item -> hotseatItems.add(item as WorkspaceItemInfo) } + recentAppsController?.updateHotseatItemInfos(hotseatItems.toTypedArray()) + } + } + + override fun bindWorkspaceComponentsRemoved(matcher: Predicate) { + runOnMainSync { + for (i in hotseatItems.size - 1 downTo 0) { + if (matcher.test(hotseatItems[i])) { + hotseatItems.removeAt(i) + } + } + recentAppsController?.updateHotseatItemInfos(hotseatItems.toTypedArray()) + } + } + } } /** TaskbarOverflowComponent used to bind the RecentsModel. */ diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarViewTestUtil.kt b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarViewTestUtil.kt index 8f6f02e7e4..faba297512 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarViewTestUtil.kt +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarViewTestUtil.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.graphics.Bitmap import android.graphics.Bitmap.createBitmap import android.os.Process +import com.android.launcher3.LauncherSettings.Favorites.CONTAINER_HOTSEAT import com.android.launcher3.icons.BitmapInfo import com.android.launcher3.model.data.AppInfo import com.android.launcher3.model.data.AppPairInfo @@ -50,16 +51,17 @@ object TaskbarViewTestUtil { } /** Creates an array of fake hotseat items. */ - fun createHotseatItems(size: Int): Array { + fun createHotseatItems(size: Int): Array { return Array(size) { createHotseatWorkspaceItem(it) } } fun createHotseatWorkspaceItem(id: Int = 0): WorkspaceItemInfo { return WorkspaceItemInfo( - AppInfo(TEST_COMPONENT, "Test App $id", Process.myUserHandle(), Intent()) + AppInfo(testComponent(id), "Test App $id", Process.myUserHandle(), testIntent(id)) ) .apply { this.id = id + container = CONTAINER_HOTSEAT // Create a placeholder icon so that the test doesn't try to load a high-res icon. this.bitmap = BitmapInfo.fromBitmap(createBitmap(1, 1, Bitmap.Config.ALPHA_8)) } @@ -93,8 +95,8 @@ object TaskbarViewTestUtil { TaskKey( id, 5, - TEST_INTENT, - TEST_COMPONENT, + testIntent(id), + testComponent(id), Process.myUserHandle().identifier, System.currentTimeMillis(), ) @@ -149,8 +151,10 @@ enum class TaskbarIconType { } private const val TEST_PACKAGE = "com.android.launcher3.taskbar" -private val TEST_COMPONENT = ComponentName(TEST_PACKAGE, "Activity") -private val TEST_INTENT = Intent().apply { - `package` = TEST_PACKAGE - component = TEST_COMPONENT +private val testComponent = { i: Int -> ComponentName(TEST_PACKAGE, "Activity $i") } +private val testIntent = { i: Int -> + Intent().apply { + `package` = TEST_PACKAGE + component = testComponent(i) + } }