From b5b70930e983fe2d2c7ebd64a2408e0061d060e5 Mon Sep 17 00:00:00 2001 From: Benno Lin Date: Wed, 19 Jan 2022 03:45:46 +0000 Subject: [PATCH 1/6] Enable pressback tests Bug: 205027405 Test: Manual Change-Id: I5bb41fc27db17e95d7a7c91cec2bbf85f33d6dad --- .../android/quickstep/TaplTestsQuickstep.java | 16 ++---------- .../launcher3/ui/TaplTestsLauncher3.java | 12 ++------- .../tapl/LauncherInstrumentation.java | 25 +++++++++++++------ 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java b/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java index 399cd10855..4d38822f26 100644 --- a/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java +++ b/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java @@ -32,7 +32,6 @@ import androidx.test.uiautomator.Until; import com.android.launcher3.Launcher; import com.android.launcher3.LauncherState; -import com.android.launcher3.tapl.HomeAllApps; import com.android.launcher3.tapl.LaunchedAppState; import com.android.launcher3.tapl.LauncherInstrumentation.NavigationModel; import com.android.launcher3.tapl.Overview; @@ -45,7 +44,6 @@ import com.android.quickstep.views.RecentsView; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -295,26 +293,16 @@ public class TaplTestsQuickstep extends AbstractQuickStepTest { getAndAssertLaunchedApp(); } - // TODO(b/204830798): test with all navigation modes(add @NavigationModeSwitch annotation) - // after the bug resolved. - @Ignore("b/205027405") @Test @PortraitLandscape - @ScreenRecord + @NavigationModeSwitch public void testPressBack() throws Exception { mLauncher.getWorkspace().switchToAllApps(); mLauncher.pressBack(); mLauncher.getWorkspace(); waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL); - HomeAllApps allApps = mLauncher.getWorkspace().switchToAllApps(); - allApps.freeze(); - try { - allApps.getAppIcon(APP_NAME).dragToWorkspace(false, false); - } finally { - allApps.unfreeze(); - } - mLauncher.getWorkspace().getWorkspaceAppIcon(APP_NAME).launch(getAppPackageName()); + startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR)); mLauncher.pressBack(); mLauncher.getWorkspace(); waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL); diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java index 73e9823229..f0bef2477f 100644 --- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java +++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import android.content.Intent; import android.graphics.Point; import androidx.test.filters.LargeTest; @@ -50,7 +51,6 @@ import com.android.launcher3.widget.picker.WidgetsFullSheet; import com.android.launcher3.widget.picker.WidgetsRecyclerView; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -391,7 +391,6 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest { folder.close(); } - @Ignore("b/205027405") @Test @PortraitLandscape public void testPressBack() throws Exception { @@ -400,14 +399,7 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest { mLauncher.getWorkspace(); waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL); - HomeAllApps allApps = mLauncher.getWorkspace().switchToAllApps(); - allApps.freeze(); - try { - allApps.getAppIcon(APP_NAME).dragToWorkspace(false, false); - } finally { - allApps.unfreeze(); - } - mLauncher.getWorkspace().getWorkspaceAppIcon(APP_NAME).launch(getAppPackageName()); + startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR)); mLauncher.pressBack(); mLauncher.getWorkspace(); waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL); diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java index 8e0eb7b071..9f0d9720da 100644 --- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java +++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java @@ -113,6 +113,7 @@ public final class LauncherInstrumentation { static final Pattern EVENT_TOUCH_DOWN_TIS = getTouchEventPatternTIS("ACTION_DOWN"); static final Pattern EVENT_TOUCH_UP_TIS = getTouchEventPatternTIS("ACTION_UP"); + static final Pattern EVENT_TOUCH_CANCEL_TIS = getTouchEventPatternTIS("ACTION_CANCEL"); static final Pattern EVENT_KEY_BACK_DOWN = getKeyEventPattern("ACTION_DOWN", "KEYCODE_BACK"); static final Pattern EVENT_KEY_BACK_UP = getKeyEventPattern("ACTION_UP", "KEYCODE_BACK"); @@ -135,6 +136,8 @@ public final class LauncherInstrumentation { public enum GestureScope { OUTSIDE_WITHOUT_PILFER, OUTSIDE_WITH_PILFER, INSIDE, INSIDE_TO_OUTSIDE, INSIDE_TO_OUTSIDE_WITHOUT_PILFER, + INSIDE_TO_OUTSIDE_WITH_KEYCODE, // For gestures that will trigger a keycode from TIS. + OUTSIDE_WITH_KEYCODE, } // Base class for launcher containers. @@ -877,10 +880,9 @@ public final class LauncherInstrumentation { } /** + * @return the Workspace object. * @deprecated use goHome(). * Presses nav bar home button. - * - * @return the Workspace object. */ @Deprecated public Workspace pressHome() { @@ -968,9 +970,11 @@ public final class LauncherInstrumentation { if (getNavigationModel() == NavigationModel.ZERO_BUTTON) { final Point displaySize = getRealDisplaySize(); final GestureScope gestureScope = - launcherVisible ? GestureScope.INSIDE_TO_OUTSIDE_WITHOUT_PILFER - : GestureScope.OUTSIDE_WITHOUT_PILFER; - linearGesture(0, displaySize.y / 2, displaySize.x / 2, displaySize.y / 2, + launcherVisible ? GestureScope.INSIDE_TO_OUTSIDE_WITH_KEYCODE + : GestureScope.OUTSIDE_WITH_KEYCODE; + // TODO(b/225505986): change startY and endY back to displaySize.y / 2 once the + // issue is solved. + linearGesture(0, displaySize.y / 4, displaySize.x / 2, displaySize.y / 4, 10, false, gestureScope); } else { waitForNavigationUiObject("back").click(); @@ -1506,7 +1510,8 @@ public final class LauncherInstrumentation { switch (action) { case MotionEvent.ACTION_DOWN: if (gestureScope != GestureScope.OUTSIDE_WITH_PILFER - && gestureScope != GestureScope.OUTSIDE_WITHOUT_PILFER) { + && gestureScope != GestureScope.OUTSIDE_WITHOUT_PILFER + && gestureScope != GestureScope.OUTSIDE_WITH_KEYCODE) { expectEvent(TestProtocol.SEQUENCE_MAIN, EVENT_TOUCH_DOWN); } if (notLauncher3 && getNavigationModel() != NavigationModel.THREE_BUTTON) { @@ -1521,14 +1526,18 @@ public final class LauncherInstrumentation { expectEvent(TestProtocol.SEQUENCE_PILFER, EVENT_PILFER_POINTERS); } if (gestureScope != GestureScope.OUTSIDE_WITH_PILFER - && gestureScope != GestureScope.OUTSIDE_WITHOUT_PILFER) { + && gestureScope != GestureScope.OUTSIDE_WITHOUT_PILFER + && gestureScope != GestureScope.OUTSIDE_WITH_KEYCODE) { expectEvent(TestProtocol.SEQUENCE_MAIN, gestureScope == GestureScope.INSIDE || gestureScope == GestureScope.OUTSIDE_WITHOUT_PILFER ? EVENT_TOUCH_UP : EVENT_TOUCH_CANCEL); } if (notLauncher3 && getNavigationModel() != NavigationModel.THREE_BUTTON) { - expectEvent(TestProtocol.SEQUENCE_TIS, EVENT_TOUCH_UP_TIS); + expectEvent(TestProtocol.SEQUENCE_TIS, + gestureScope == GestureScope.INSIDE_TO_OUTSIDE_WITH_KEYCODE + || gestureScope == GestureScope.OUTSIDE_WITH_KEYCODE + ? EVENT_TOUCH_CANCEL_TIS : EVENT_TOUCH_UP_TIS); } break; } From 0c3961c367954a2a9719df15818bbbbf244a7d1e Mon Sep 17 00:00:00 2001 From: Luca Zuccarini Date: Wed, 16 Mar 2022 11:09:56 +0000 Subject: [PATCH 2/6] Refactor the floating header's positioning. Maintains the existing behavior while simplifying the overall layout. See go/all-apps-qsb-positioning for details. Bug: 221418522 Test: manual Change-Id: I25a737cdfa431e8eb3a00b639984caed70819252 --- res/values/dimens.xml | 1 + .../allapps/ActivityAllAppsContainerView.java | 49 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/res/values/dimens.xml b/res/values/dimens.xml index 0615053de8..2ee4c45991 100644 --- a/res/values/dimens.xml +++ b/res/values/dimens.xml @@ -109,6 +109,7 @@ 12dp 48dp 2dp + 33dp 36dp 6dp 16dp diff --git a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java index 11e0a1faae..e279f59aaf 100644 --- a/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java +++ b/src/com/android/launcher3/allapps/ActivityAllAppsContainerView.java @@ -175,22 +175,27 @@ public class ActivityAllAppsContainerView Date: Mon, 22 Nov 2021 16:54:54 +0000 Subject: [PATCH 3/6] Separate workspace item finding logic Extract the item finding logic from AddWorkspaceItemsTask to a separate class and write tests. Test: AddWorkspaceItemsTaskTest.kt , WorkspaceItemSpaceFinderTest.kt Bug: 199160559 Change-Id: Ie1bc4fcd4f94cd7cb0601c21bbdf273452b9dd1f --- .../model/AddWorkspaceItemsTask.java | 101 +------- .../model/WorkspaceItemSpaceFinder.java | 115 ++++++++ .../model/AbstractWorkspaceModelTest.kt | 162 ++++++++++++ .../model/AddWorkspaceItemsTaskTest.java | 201 -------------- .../model/AddWorkspaceItemsTaskTest.kt | 245 ++++++++++++++++++ .../model/WorkspaceItemSpaceFinderTest.kt | 171 ++++++++++++ .../launcher3/util/KotlinMockitoHelpers.kt | 117 +++++++++ 7 files changed, 824 insertions(+), 288 deletions(-) create mode 100644 src/com/android/launcher3/model/WorkspaceItemSpaceFinder.java create mode 100644 tests/src/com/android/launcher3/model/AbstractWorkspaceModelTest.kt delete mode 100644 tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java create mode 100644 tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.kt create mode 100644 tests/src/com/android/launcher3/model/WorkspaceItemSpaceFinderTest.kt create mode 100644 tests/src/com/android/launcher3/util/KotlinMockitoHelpers.kt diff --git a/src/com/android/launcher3/model/AddWorkspaceItemsTask.java b/src/com/android/launcher3/model/AddWorkspaceItemsTask.java index 91fb44e84c..ca91296ba6 100644 --- a/src/com/android/launcher3/model/AddWorkspaceItemsTask.java +++ b/src/com/android/launcher3/model/AddWorkspaceItemsTask.java @@ -15,22 +15,17 @@ */ package com.android.launcher3.model; -import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID; - import android.content.Intent; import android.content.pm.LauncherActivityInfo; import android.content.pm.LauncherApps; import android.content.pm.PackageInstaller.SessionInfo; import android.os.UserHandle; import android.util.Log; -import android.util.LongSparseArray; import android.util.Pair; -import com.android.launcher3.InvariantDeviceProfile; import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherModel.CallbackTask; import com.android.launcher3.LauncherSettings; -import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.logging.FileLog; import com.android.launcher3.model.BgDataModel.Callbacks; import com.android.launcher3.model.data.AppInfo; @@ -41,9 +36,7 @@ import com.android.launcher3.model.data.WorkspaceItemInfo; import com.android.launcher3.pm.InstallSessionHelper; import com.android.launcher3.pm.PackageInstallInfo; import com.android.launcher3.testing.TestProtocol; -import com.android.launcher3.util.GridOccupancy; import com.android.launcher3.util.IntArray; -import com.android.launcher3.util.IntSet; import com.android.launcher3.util.PackageManagerHelper; import java.util.ArrayList; @@ -58,11 +51,23 @@ public class AddWorkspaceItemsTask extends BaseModelUpdateTask { private final List> mItemList; + private final WorkspaceItemSpaceFinder mItemSpaceFinder; + /** * @param itemList items to add on the workspace */ public AddWorkspaceItemsTask(List> itemList) { + this(itemList, new WorkspaceItemSpaceFinder()); + } + + /** + * @param itemList items to add on the workspace + * @param itemSpaceFinder inject WorkspaceItemSpaceFinder dependency for testing + */ + public AddWorkspaceItemsTask(List> itemList, + WorkspaceItemSpaceFinder itemSpaceFinder) { mItemList = itemList; + mItemSpaceFinder = itemSpaceFinder; } @Override @@ -74,7 +79,7 @@ public class AddWorkspaceItemsTask extends BaseModelUpdateTask { final ArrayList addedItemsFinal = new ArrayList<>(); final IntArray addedWorkspaceScreensFinal = new IntArray(); - synchronized(dataModel) { + synchronized (dataModel) { IntArray workspaceScreens = dataModel.collectWorkspaceScreens(); List filteredItems = new ArrayList<>(); @@ -117,7 +122,7 @@ public class AddWorkspaceItemsTask extends BaseModelUpdateTask { for (ItemInfo item : filteredItems) { // Find appropriate space for the item. - int[] coords = findSpaceForItem(app, dataModel, workspaceScreens, + int[] coords = mItemSpaceFinder.findSpaceForItem(app, dataModel, workspaceScreens, addedWorkspaceScreensFinal, item.spanX, item.spanY); int screenId = coords[0]; @@ -288,82 +293,4 @@ public class AddWorkspaceItemsTask extends BaseModelUpdateTask { } return false; } - - /** - * Find a position on the screen for the given size or adds a new screen. - * @return screenId and the coordinates for the item in an int array of size 3. - */ - protected int[] findSpaceForItem( LauncherAppState app, BgDataModel dataModel, - IntArray workspaceScreens, IntArray addedWorkspaceScreensFinal, int spanX, int spanY) { - LongSparseArray> screenItems = new LongSparseArray<>(); - - // Use sBgItemsIdMap as all the items are already loaded. - synchronized (dataModel) { - for (ItemInfo info : dataModel.itemsIdMap) { - if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { - ArrayList items = screenItems.get(info.screenId); - if (items == null) { - items = new ArrayList<>(); - screenItems.put(info.screenId, items); - } - items.add(info); - } - } - } - - // Find appropriate space for the item. - int screenId = 0; - int[] coordinates = new int[2]; - boolean found = false; - - int screenCount = workspaceScreens.size(); - // First check the preferred screen. - IntSet screensToExclude = new IntSet(); - if (FeatureFlags.QSB_ON_FIRST_SCREEN) { - screensToExclude.add(FIRST_SCREEN_ID); - } - - for (int screen = 0; screen < screenCount; screen++) { - screenId = workspaceScreens.get(screen); - if (!screensToExclude.contains(screenId) && findNextAvailableIconSpaceInScreen( - app, screenItems.get(screenId), coordinates, spanX, spanY)) { - // We found a space for it - found = true; - break; - } - } - - if (!found) { - // Still no position found. Add a new screen to the end. - screenId = LauncherSettings.Settings.call(app.getContext().getContentResolver(), - LauncherSettings.Settings.METHOD_NEW_SCREEN_ID) - .getInt(LauncherSettings.Settings.EXTRA_VALUE); - - // Save the screen id for binding in the workspace - workspaceScreens.add(screenId); - addedWorkspaceScreensFinal.add(screenId); - - // If we still can't find an empty space, then God help us all!!! - if (!findNextAvailableIconSpaceInScreen( - app, screenItems.get(screenId), coordinates, spanX, spanY)) { - throw new RuntimeException("Can't find space to add the item"); - } - } - return new int[] {screenId, coordinates[0], coordinates[1]}; - } - - private boolean findNextAvailableIconSpaceInScreen( - LauncherAppState app, ArrayList occupiedPos, - int[] xy, int spanX, int spanY) { - InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); - - GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows); - if (occupiedPos != null) { - for (ItemInfo r : occupiedPos) { - occupied.markCells(r, true); - } - } - return occupied.findVacantCell(xy, spanX, spanY); - } - } diff --git a/src/com/android/launcher3/model/WorkspaceItemSpaceFinder.java b/src/com/android/launcher3/model/WorkspaceItemSpaceFinder.java new file mode 100644 index 0000000000..93fc6a539f --- /dev/null +++ b/src/com/android/launcher3/model/WorkspaceItemSpaceFinder.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2022 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.model; + +import static com.android.launcher3.WorkspaceLayoutManager.FIRST_SCREEN_ID; + +import android.util.LongSparseArray; + +import com.android.launcher3.InvariantDeviceProfile; +import com.android.launcher3.LauncherAppState; +import com.android.launcher3.LauncherSettings; +import com.android.launcher3.config.FeatureFlags; +import com.android.launcher3.model.data.ItemInfo; +import com.android.launcher3.util.GridOccupancy; +import com.android.launcher3.util.IntArray; +import com.android.launcher3.util.IntSet; + +import java.util.ArrayList; + +/** + * Utility class to help find space for new workspace items + */ +public class WorkspaceItemSpaceFinder { + + /** + * Find a position on the screen for the given size or adds a new screen. + * + * @return screenId and the coordinates for the item in an int array of size 3. + */ + public int[] findSpaceForItem(LauncherAppState app, BgDataModel dataModel, + IntArray workspaceScreens, IntArray addedWorkspaceScreensFinal, int spanX, int spanY) { + LongSparseArray> screenItems = new LongSparseArray<>(); + + // Use sBgItemsIdMap as all the items are already loaded. + synchronized (dataModel) { + for (ItemInfo info : dataModel.itemsIdMap) { + if (info.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { + ArrayList items = screenItems.get(info.screenId); + if (items == null) { + items = new ArrayList<>(); + screenItems.put(info.screenId, items); + } + items.add(info); + } + } + } + + // Find appropriate space for the item. + int screenId = 0; + int[] coordinates = new int[2]; + boolean found = false; + + int screenCount = workspaceScreens.size(); + // First check the preferred screen. + IntSet screensToExclude = new IntSet(); + if (FeatureFlags.QSB_ON_FIRST_SCREEN) { + screensToExclude.add(FIRST_SCREEN_ID); + } + + for (int screen = 0; screen < screenCount; screen++) { + screenId = workspaceScreens.get(screen); + if (!screensToExclude.contains(screenId) && findNextAvailableIconSpaceInScreen( + app, screenItems.get(screenId), coordinates, spanX, spanY)) { + // We found a space for it + found = true; + break; + } + } + + if (!found) { + // Still no position found. Add a new screen to the end. + screenId = LauncherSettings.Settings.call(app.getContext().getContentResolver(), + LauncherSettings.Settings.METHOD_NEW_SCREEN_ID) + .getInt(LauncherSettings.Settings.EXTRA_VALUE); + + // Save the screen id for binding in the workspace + workspaceScreens.add(screenId); + addedWorkspaceScreensFinal.add(screenId); + + // If we still can't find an empty space, then God help us all!!! + if (!findNextAvailableIconSpaceInScreen( + app, screenItems.get(screenId), coordinates, spanX, spanY)) { + throw new RuntimeException("Can't find space to add the item"); + } + } + return new int[]{screenId, coordinates[0], coordinates[1]}; + } + + private boolean findNextAvailableIconSpaceInScreen( + LauncherAppState app, ArrayList occupiedPos, + int[] xy, int spanX, int spanY) { + InvariantDeviceProfile profile = app.getInvariantDeviceProfile(); + + GridOccupancy occupied = new GridOccupancy(profile.numColumns, profile.numRows); + if (occupiedPos != null) { + for (ItemInfo r : occupiedPos) { + occupied.markCells(r, true); + } + } + return occupied.findVacantCell(xy, spanX, spanY); + } +} diff --git a/tests/src/com/android/launcher3/model/AbstractWorkspaceModelTest.kt b/tests/src/com/android/launcher3/model/AbstractWorkspaceModelTest.kt new file mode 100644 index 0000000000..d26381dc2f --- /dev/null +++ b/tests/src/com/android/launcher3/model/AbstractWorkspaceModelTest.kt @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2022 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.model + +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.graphics.Rect +import com.android.launcher3.InvariantDeviceProfile +import com.android.launcher3.LauncherAppState +import com.android.launcher3.LauncherSettings +import com.android.launcher3.model.data.WorkspaceItemInfo +import com.android.launcher3.util.ContentWriter +import com.android.launcher3.util.GridOccupancy +import com.android.launcher3.util.IntArray +import com.android.launcher3.util.IntSparseArrayMap +import com.android.launcher3.util.LauncherModelHelper +import java.util.UUID + +/** + * Base class for workspace related tests. + */ +abstract class AbstractWorkspaceModelTest { + companion object { + val emptyScreenSpaces = listOf(Rect(0, 0, 5, 5)) + val fullScreenSpaces = emptyList() + val nonEmptyScreenSpaces = listOf(Rect(1, 2, 3, 4)) + } + + protected lateinit var mTargetContext: Context + protected lateinit var mIdp: InvariantDeviceProfile + protected lateinit var mAppState: LauncherAppState + protected lateinit var mModelHelper: LauncherModelHelper + protected lateinit var mExistingScreens: IntArray + protected lateinit var mNewScreens: IntArray + protected lateinit var mScreenOccupancy: IntSparseArrayMap + + open fun setup() { + mModelHelper = LauncherModelHelper() + mTargetContext = mModelHelper.sandboxContext + mIdp = InvariantDeviceProfile.INSTANCE[mTargetContext] + mIdp.numRows = 5 + mIdp.numColumns = mIdp.numRows + mAppState = LauncherAppState.getInstance(mTargetContext) + mExistingScreens = IntArray() + mScreenOccupancy = IntSparseArrayMap() + mNewScreens = IntArray() + } + + open fun tearDown() { + mModelHelper.destroy() + } + + + /** + * Sets up workspaces with the given screen IDs with some items and a 2x2 space. + */ + fun setupWorkspaces(screenIdsWithItems: List) { + var nextItemId = 1 + screenIdsWithItems.forEach { screenId -> + nextItemId = setupWorkspace(nextItemId, screenId, nonEmptyScreenSpaces) + } + } + + /** + * Sets up the given workspaces with the given spaces, and fills the remaining space with items. + */ + fun setupWorkspacesWithSpaces( + screen0: List? = null, + screen1: List? = null, + screen2: List? = null, + screen3: List? = null, + ) = listOf(screen0, screen1, screen2, screen3) + .let(this::setupWithSpaces) + + private fun setupWithSpaces(workspaceSpaces: List?>) { + var nextItemId = 1 + workspaceSpaces.forEachIndexed { screenId, spaces -> + if (spaces != null) { + nextItemId = setupWorkspace(nextItemId, screenId, spaces) + } + } + } + + private fun setupWorkspace(startId: Int, screenId: Int, spaces: List): Int { + return mModelHelper.executeSimpleTask { dataModel -> + writeWorkspaceWithSpaces(dataModel, startId, screenId, spaces) + } + } + + private fun writeWorkspaceWithSpaces( + bgDataModel: BgDataModel, + itemStartId: Int, + screenId: Int, + spaces: List, + ): Int { + var itemId = itemStartId + val occupancy = GridOccupancy(mIdp.numColumns, mIdp.numRows) + occupancy.markCells(0, 0, mIdp.numColumns, mIdp.numRows, true) + spaces.forEach { spaceRect -> + occupancy.markCells(spaceRect, false) + } + mExistingScreens.add(screenId) + mScreenOccupancy.append(screenId, occupancy) + for (x in 0 until mIdp.numColumns) { + for (y in 0 until mIdp.numRows) { + if (!occupancy.cells[x][y]) { + continue + } + val info = getExistingItem() + info.id = itemId++ + info.screenId = screenId + info.cellX = x + info.cellY = y + info.container = LauncherSettings.Favorites.CONTAINER_DESKTOP + bgDataModel.addItem(mTargetContext, info, false) + val writer = ContentWriter(mTargetContext) + info.writeToValues(writer) + writer.put(LauncherSettings.Favorites._ID, info.id) + mTargetContext.contentResolver.insert( + LauncherSettings.Favorites.CONTENT_URI, + writer.getValues(mTargetContext) + ) + } + } + return itemId + } + + fun getExistingItem() = WorkspaceItemInfo() + .apply { intent = Intent().setComponent(ComponentName("a", "b")) } + + fun getNewItem(): WorkspaceItemInfo { + val itemPackage = UUID.randomUUID().toString() + return WorkspaceItemInfo() + .apply { intent = Intent().setComponent(ComponentName(itemPackage, itemPackage)) } + } +} + +data class NewItemSpace( + val screenId: Int, + val cellX: Int, + val cellY: Int +) { + fun toIntArray() = intArrayOf(screenId, cellX, cellY) + + companion object { + fun fromIntArray(array: kotlin.IntArray) = NewItemSpace(array[0], array[1], array[2]) + } +} \ No newline at end of file diff --git a/tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java b/tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java deleted file mode 100644 index 8a4590a388..0000000000 --- a/tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.java +++ /dev/null @@ -1,201 +0,0 @@ -package com.android.launcher3.model; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.graphics.Rect; -import android.util.Pair; - -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.SmallTest; - -import com.android.launcher3.InvariantDeviceProfile; -import com.android.launcher3.LauncherAppState; -import com.android.launcher3.LauncherSettings; -import com.android.launcher3.LauncherSettings.Favorites; -import com.android.launcher3.model.BgDataModel.Callbacks; -import com.android.launcher3.model.data.ItemInfo; -import com.android.launcher3.model.data.WorkspaceItemInfo; -import com.android.launcher3.util.ContentWriter; -import com.android.launcher3.util.Executors; -import com.android.launcher3.util.GridOccupancy; -import com.android.launcher3.util.IntArray; -import com.android.launcher3.util.IntSparseArrayMap; -import com.android.launcher3.util.LauncherModelHelper; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; - -import java.util.ArrayList; -import java.util.List; - -/** - * Tests for {@link AddWorkspaceItemsTask} - */ -@SmallTest -@RunWith(AndroidJUnit4.class) -public class AddWorkspaceItemsTaskTest { - - private final ComponentName mComponent1 = new ComponentName("a", "b"); - private final ComponentName mComponent2 = new ComponentName("b", "b"); - - private Context mTargetContext; - private InvariantDeviceProfile mIdp; - private LauncherAppState mAppState; - private LauncherModelHelper mModelHelper; - - private IntArray mExistingScreens; - private IntArray mNewScreens; - private IntSparseArrayMap mScreenOccupancy; - - @Before - public void setup() { - mModelHelper = new LauncherModelHelper(); - mTargetContext = mModelHelper.sandboxContext; - mIdp = InvariantDeviceProfile.INSTANCE.get(mTargetContext); - mIdp.numColumns = mIdp.numRows = 5; - mAppState = LauncherAppState.getInstance(mTargetContext); - - mExistingScreens = new IntArray(); - mScreenOccupancy = new IntSparseArrayMap<>(); - mNewScreens = new IntArray(); - } - - @After - public void tearDown() { - mModelHelper.destroy(); - } - - private AddWorkspaceItemsTask newTask(ItemInfo... items) { - List> list = new ArrayList<>(); - for (ItemInfo item : items) { - list.add(Pair.create(item, null)); - } - return new AddWorkspaceItemsTask(list); - } - - @Test - public void testFindSpaceForItem_prefers_second() throws Exception { - // First screen has only one hole of size 1 - int nextId = setupWorkspaceWithHoles(1, 1, new Rect(2, 2, 3, 3)); - - // Second screen has 2 holes of sizes 3x2 and 2x3 - setupWorkspaceWithHoles(nextId, 2, new Rect(2, 0, 5, 2), new Rect(0, 2, 2, 5)); - - int[] spaceFound = newTask().findSpaceForItem( - mAppState, mModelHelper.getBgDataModel(), mExistingScreens, mNewScreens, 1, 1); - assertEquals(1, spaceFound[0]); - assertTrue(mScreenOccupancy.get(spaceFound[0]) - .isRegionVacant(spaceFound[1], spaceFound[2], 1, 1)); - - // Find a larger space - spaceFound = newTask().findSpaceForItem( - mAppState, mModelHelper.getBgDataModel(), mExistingScreens, mNewScreens, 2, 3); - assertEquals(2, spaceFound[0]); - assertTrue(mScreenOccupancy.get(spaceFound[0]) - .isRegionVacant(spaceFound[1], spaceFound[2], 2, 3)); - } - - @Test - public void testFindSpaceForItem_adds_new_screen() throws Exception { - // First screen has 2 holes of sizes 3x2 and 2x3 - setupWorkspaceWithHoles(1, 1, new Rect(2, 0, 5, 2), new Rect(0, 2, 2, 5)); - - IntArray oldScreens = mExistingScreens.clone(); - int[] spaceFound = newTask().findSpaceForItem( - mAppState, mModelHelper.getBgDataModel(), mExistingScreens, mNewScreens, 3, 3); - assertFalse(oldScreens.contains(spaceFound[0])); - assertTrue(mNewScreens.contains(spaceFound[0])); - } - - @Test - public void testAddItem_existing_item_ignored() throws Exception { - WorkspaceItemInfo info = new WorkspaceItemInfo(); - info.intent = new Intent().setComponent(mComponent1); - - // Setup a screen with a hole - setupWorkspaceWithHoles(1, 1, new Rect(2, 2, 3, 3)); - - // Nothing was added - assertTrue(mModelHelper.executeTaskForTest(newTask(info)).isEmpty()); - } - - @Test - public void testAddItem_some_items_added() throws Exception { - Callbacks callbacks = mock(Callbacks.class); - Executors.MAIN_EXECUTOR.submit(() -> mModelHelper.getModel().addCallbacks(callbacks)).get(); - - WorkspaceItemInfo info = new WorkspaceItemInfo(); - info.intent = new Intent().setComponent(mComponent1); - - WorkspaceItemInfo info2 = new WorkspaceItemInfo(); - info2.intent = new Intent().setComponent(mComponent2); - - // Setup a screen with a hole - setupWorkspaceWithHoles(1, 1, new Rect(2, 2, 3, 3)); - - mModelHelper.executeTaskForTest(newTask(info, info2)).get(0).run(); - ArgumentCaptor notAnimated = ArgumentCaptor.forClass(ArrayList.class); - ArgumentCaptor animated = ArgumentCaptor.forClass(ArrayList.class); - - // only info2 should be added because info was already added to the workspace - // in setupWorkspaceWithHoles() - verify(callbacks).bindAppsAdded(any(IntArray.class), notAnimated.capture(), - animated.capture()); - assertTrue(notAnimated.getValue().isEmpty()); - - assertEquals(1, animated.getValue().size()); - assertTrue(animated.getValue().contains(info2)); - } - - private int setupWorkspaceWithHoles(int startId, int screenId, Rect... holes) throws Exception { - return mModelHelper.executeSimpleTask( - model -> writeWorkspaceWithHoles(model, startId, screenId, holes)); - } - - private int writeWorkspaceWithHoles( - BgDataModel bgDataModel, int startId, int screenId, Rect... holes) { - GridOccupancy occupancy = new GridOccupancy(mIdp.numColumns, mIdp.numRows); - occupancy.markCells(0, 0, mIdp.numColumns, mIdp.numRows, true); - for (Rect r : holes) { - occupancy.markCells(r, false); - } - - mExistingScreens.add(screenId); - mScreenOccupancy.append(screenId, occupancy); - - for (int x = 0; x < mIdp.numColumns; x++) { - for (int y = 0; y < mIdp.numRows; y++) { - if (!occupancy.cells[x][y]) { - continue; - } - - WorkspaceItemInfo info = new WorkspaceItemInfo(); - info.intent = new Intent().setComponent(mComponent1); - info.id = startId++; - info.screenId = screenId; - info.cellX = x; - info.cellY = y; - info.container = LauncherSettings.Favorites.CONTAINER_DESKTOP; - bgDataModel.addItem(mTargetContext, info, false); - - ContentWriter writer = new ContentWriter(mTargetContext); - info.writeToValues(writer); - writer.put(Favorites._ID, info.id); - mTargetContext.getContentResolver().insert(Favorites.CONTENT_URI, - writer.getValues(mTargetContext)); - } - } - return startId; - } -} diff --git a/tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.kt b/tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.kt new file mode 100644 index 0000000000..65d938b91a --- /dev/null +++ b/tests/src/com/android/launcher3/model/AddWorkspaceItemsTaskTest.kt @@ -0,0 +1,245 @@ +/* + * Copyright (C) 2022 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.model + +import android.util.Pair +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SmallTest +import com.android.launcher3.model.data.ItemInfo +import com.android.launcher3.model.data.WorkspaceItemInfo +import com.android.launcher3.util.Executors +import com.android.launcher3.util.IntArray +import com.android.launcher3.util.same +import com.android.launcher3.util.eq +import com.android.launcher3.util.any +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentCaptor +import org.mockito.Captor +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.Mockito.verify +import org.mockito.Mockito.verifyZeroInteractions +import org.mockito.Mockito.times +import org.mockito.Mockito.`when` as whenever + +/** + * Tests for [AddWorkspaceItemsTask] + */ +@SmallTest +@RunWith(AndroidJUnit4::class) +class AddWorkspaceItemsTaskTest : AbstractWorkspaceModelTest() { + + @Captor + private lateinit var mAnimatedItemArgumentCaptor: ArgumentCaptor> + + @Captor + private lateinit var mNotAnimatedItemArgumentCaptor: ArgumentCaptor> + + @Mock + private lateinit var mDataModelCallbacks: BgDataModel.Callbacks + + @Mock + private lateinit var mWorkspaceItemSpaceFinder: WorkspaceItemSpaceFinder + + + @Before + override fun setup() { + super.setup() + MockitoAnnotations.initMocks(this) + Executors.MAIN_EXECUTOR.submit { mModelHelper.model.addCallbacks(mDataModelCallbacks) } + .get() + } + + @After + override fun tearDown() { + super.tearDown() + } + + @Test + fun givenNewItemAndNonEmptyPages_whenExecuteTask_thenAddNewItem() { + val itemToAdd = getNewItem() + val nonEmptyScreenIds = listOf(0, 1, 2) + givenNewItemSpaces(NewItemSpace(1, 2, 2)) + + val addedItems = testAddItems(nonEmptyScreenIds, itemToAdd) + + assertThat(addedItems.size).isEqualTo(1) + assertThat(addedItems.first().itemInfo.screenId).isEqualTo(1) + assertThat(addedItems.first().isAnimated).isTrue() + verifyItemSpaceFinderCall(nonEmptyScreenIds, numberOfExpectedCall = 1) + } + + @Test + fun givenNewAndExistingItems_whenExecuteTask_thenOnlyAddNewItem() { + val itemsToAdd = arrayOf( + getNewItem(), + getExistingItem() + ) + givenNewItemSpaces(NewItemSpace(1, 0, 0)) + val nonEmptyScreenIds = listOf(0) + + val addedItems = testAddItems(nonEmptyScreenIds, *itemsToAdd) + + assertThat(addedItems.size).isEqualTo(1) + assertThat(addedItems.first().itemInfo.screenId).isEqualTo(1) + assertThat(addedItems.first().isAnimated).isTrue() + verifyItemSpaceFinderCall(nonEmptyScreenIds, numberOfExpectedCall = 1) + } + + @Test + fun givenOnlyExistingItem_whenExecuteTask_thenDoNotAddItem() { + val itemToAdd = getExistingItem() + givenNewItemSpaces(NewItemSpace(1, 0, 0)) + val nonEmptyScreenIds = listOf(0) + + val addedItems = testAddItems(nonEmptyScreenIds, itemToAdd) + + assertThat(addedItems.size).isEqualTo(0) + verifyZeroInteractions(mWorkspaceItemSpaceFinder, mDataModelCallbacks) + } + + @Test + fun givenNonSequentialScreenIds_whenExecuteTask_thenReturnNewScreenId() { + val itemToAdd = getNewItem() + givenNewItemSpaces(NewItemSpace(2, 1, 3)) + val nonEmptyScreenIds = listOf(0, 2, 3) + + val addedItems = testAddItems(nonEmptyScreenIds, itemToAdd) + + assertThat(addedItems.size).isEqualTo(1) + assertThat(addedItems.first().itemInfo.screenId).isEqualTo(2) + assertThat(addedItems.first().isAnimated).isTrue() + verifyItemSpaceFinderCall(nonEmptyScreenIds, numberOfExpectedCall = 1) + } + + @Test + fun givenMultipleItems_whenExecuteTask_thenAddThem() { + val itemsToAdd = arrayOf( + getNewItem(), + getExistingItem(), + getNewItem(), + getNewItem(), + getExistingItem(), + ) + givenNewItemSpaces( + NewItemSpace(1, 3, 3), + NewItemSpace(2, 0, 0), + NewItemSpace(2, 0, 1), + ) + val nonEmptyScreenIds = listOf(0, 1) + + val addedItems = testAddItems(nonEmptyScreenIds, *itemsToAdd) + + // Only the new items should be added + assertThat(addedItems.size).isEqualTo(3) + + // Items that are added to the first screen should not be animated + val itemsAddedToFirstScreen = addedItems.filter { it.itemInfo.screenId == 1 } + assertThat(itemsAddedToFirstScreen.size).isEqualTo(1) + assertThat(itemsAddedToFirstScreen.first().isAnimated).isFalse() + + // Items that are added to the second screen should be animated + val itemsAddedToSecondScreen = addedItems.filter { it.itemInfo.screenId == 2 } + assertThat(itemsAddedToSecondScreen.size).isEqualTo(2) + itemsAddedToSecondScreen.forEach { + assertThat(it.isAnimated).isTrue() + } + verifyItemSpaceFinderCall(nonEmptyScreenIds, numberOfExpectedCall = 3) + } + + /** + * Sets up the item space data that will be returned from WorkspaceItemSpaceFinder. + */ + private fun givenNewItemSpaces(vararg newItemSpaces: NewItemSpace) { + val spaceStack = newItemSpaces.toMutableList() + whenever( + mWorkspaceItemSpaceFinder.findSpaceForItem( + any(), + any(), + any(), + any(), + any(), + any() + ) + ) + .then { spaceStack.removeFirst().toIntArray() } + } + + /** + * Verifies if WorkspaceItemSpaceFinder was called with proper arguments and how many times was + * it called. + */ + private fun verifyItemSpaceFinderCall( + nonEmptyScreenIds: List, + numberOfExpectedCall: Int + ) { + verify(mWorkspaceItemSpaceFinder, times(numberOfExpectedCall)) + .findSpaceForItem( + same(mAppState), same(mModelHelper.bgDataModel), + eq(IntArray.wrap(*nonEmptyScreenIds.toIntArray())), eq(IntArray()), eq(1), eq(1) + ) + } + + /** + * Sets up the workspaces with items, executes the task, collects the added items from the + * model callback then returns it. + */ + private fun testAddItems( + nonEmptyScreenIds: List, + vararg itemsToAdd: WorkspaceItemInfo + ): List { + setupWorkspaces(nonEmptyScreenIds) + val task = newTask(*itemsToAdd) + var updateCount = 0 + mModelHelper.executeTaskForTest(task) + .forEach { + updateCount++ + it.run() + } + + val addedItems = mutableListOf() + if (updateCount > 0) { + verify(mDataModelCallbacks).bindAppsAdded( + any(), + mNotAnimatedItemArgumentCaptor.capture(), mAnimatedItemArgumentCaptor.capture() + ) + addedItems.addAll(mAnimatedItemArgumentCaptor.value.map { AddedItem(it, true) }) + addedItems.addAll(mNotAnimatedItemArgumentCaptor.value.map { AddedItem(it, false) }) + + } + + return addedItems + } + + /** + * Creates the task with the given items and replaces the WorkspaceItemSpaceFinder dependency + * with a mock. + */ + private fun newTask(vararg items: ItemInfo): AddWorkspaceItemsTask = + items.map { Pair.create(it, Any()) } + .toMutableList() + .let { AddWorkspaceItemsTask(it, mWorkspaceItemSpaceFinder) } +} + +private data class AddedItem( + val itemInfo: ItemInfo, + val isAnimated: Boolean +) diff --git a/tests/src/com/android/launcher3/model/WorkspaceItemSpaceFinderTest.kt b/tests/src/com/android/launcher3/model/WorkspaceItemSpaceFinderTest.kt new file mode 100644 index 0000000000..bfb1ac64c4 --- /dev/null +++ b/tests/src/com/android/launcher3/model/WorkspaceItemSpaceFinderTest.kt @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2022 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.model + +import android.graphics.Rect +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SmallTest +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Tests for [WorkspaceItemSpaceFinder] + */ +@SmallTest +@RunWith(AndroidJUnit4::class) +class WorkspaceItemSpaceFinderTest : AbstractWorkspaceModelTest() { + + private val mItemSpaceFinder = WorkspaceItemSpaceFinder() + + @Before + override fun setup() { + super.setup() + } + + @After + override fun tearDown() { + super.tearDown() + } + + private fun findSpace(spanX: Int, spanY: Int): NewItemSpace = + mItemSpaceFinder.findSpaceForItem( + mAppState, mModelHelper.bgDataModel, + mExistingScreens, mNewScreens, spanX, spanY + ) + .let { NewItemSpace.fromIntArray(it) } + + private fun assertRegionVacant(newItemSpace: NewItemSpace, spanX: Int, spanY: Int) { + assertThat( + mScreenOccupancy[newItemSpace.screenId] + .isRegionVacant(newItemSpace.cellX, newItemSpace.cellY, spanX, spanY) + ).isTrue() + } + + @Test + fun justEnoughSpaceOnFirstScreen_whenFindSpaceForItem_thenReturnFirstScreenId() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + screen1 = listOf(Rect(2, 2, 3, 3)), // 1x1 space + // 2 spaces of sizes 3x2 and 2x3 + screen2 = listOf(Rect(2, 0, 5, 2), Rect(0, 2, 2, 5)), + ) + + val spaceFound = findSpace(1, 1) + + assertThat(spaceFound.screenId).isEqualTo(1) + assertRegionVacant(spaceFound, 1, 1) + } + + @Test + fun notEnoughSpaceOnFirstScreen_whenFindSpaceForItem_thenReturnSecondScreenId() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + screen1 = listOf(Rect(2, 2, 3, 3)), // 1x1 space + // 2 spaces of sizes 3x2 and 2x3 + screen2 = listOf(Rect(2, 0, 5, 2), Rect(0, 2, 2, 5)), + ) + + // Find a larger space + val spaceFound = findSpace(2, 3) + + assertThat(spaceFound.screenId).isEqualTo(2) + assertRegionVacant(spaceFound, 2, 3) + } + + @Test + fun notEnoughSpaceOnExistingScreens_returnNewScreenId() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + // 2 spaces of sizes 3x2 and 2x3 + screen1 = listOf(Rect(2, 0, 5, 2), Rect(0, 2, 2, 5)), + // 2 spaces of sizes 1x2 and 2x2 + screen2 = listOf(Rect(1, 0, 2, 2), Rect(3, 2, 5, 4)), + ) + + val oldScreens = mExistingScreens.clone() + val spaceFound = findSpace(3, 3) + + assertThat(oldScreens.contains(spaceFound.screenId)).isFalse() + assertThat(mNewScreens.contains(spaceFound.screenId)).isTrue() + } + + @Test + fun firstScreenIsEmptyButSecondIsNotEmpty_returnSecondScreenId() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + // empty screens are skipped + screen2 = listOf(Rect(2, 0, 5, 2)), // 3x2 space + ) + + val spaceFound = findSpace(2, 1) + + assertThat(spaceFound.screenId).isEqualTo(2) + assertRegionVacant(spaceFound, 2, 1) + } + + @Test + fun twoEmptyMiddleScreens_returnThirdScreen() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + // empty screens are skipped + screen3 = listOf(Rect(1, 1, 4, 4)), // 3x3 space + ) + + val spaceFound = findSpace(2, 3) + + assertThat(spaceFound.screenId).isEqualTo(3) + assertRegionVacant(spaceFound, 2, 3) + } + + @Test + fun allExistingPagesAreFull_returnNewScreenId() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + screen1 = fullScreenSpaces, + screen2 = fullScreenSpaces, + ) + + val spaceFound = findSpace(2, 3) + + assertThat(spaceFound.screenId).isEqualTo(3) + assertThat(mNewScreens.contains(spaceFound.screenId)).isTrue() + } + + @Test + fun firstTwoPagesAreFull_and_ThirdPageIsEmpty_returnThirdPage() { + setupWorkspacesWithSpaces( + // 3x2 space on screen 0, but it should be skipped + screen0 = listOf(Rect(2, 0, 5, 2)), + screen1 = fullScreenSpaces, // full screens are skipped + screen2 = fullScreenSpaces, // full screens are skipped + screen3 = emptyScreenSpaces + ) + + val spaceFound = findSpace(3, 1) + + assertThat(spaceFound.screenId).isEqualTo(3) + assertRegionVacant(spaceFound, 3, 1) + } +} diff --git a/tests/src/com/android/launcher3/util/KotlinMockitoHelpers.kt b/tests/src/com/android/launcher3/util/KotlinMockitoHelpers.kt new file mode 100644 index 0000000000..57db13ac45 --- /dev/null +++ b/tests/src/com/android/launcher3/util/KotlinMockitoHelpers.kt @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2022 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.util + +/** + * Kotlin versions of popular mockito methods that can return null in situations when Kotlin expects + * a non-null value. Kotlin will throw an IllegalStateException when this takes place ("x must not + * be null"). To fix this, we can use methods that modify the return type to be nullable. This + * causes Kotlin to skip the null checks. + */ + +import org.mockito.ArgumentCaptor +import org.mockito.Mockito + +/** + * Returns Mockito.eq() as nullable type to avoid java.lang.IllegalStateException when + * null is returned. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +fun eq(obj: T): T = Mockito.eq(obj) + +/** + * Returns Mockito.same() as nullable type to avoid java.lang.IllegalStateException when + * null is returned. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +fun same(obj: T): T = Mockito.same(obj) + +/** + * Returns Mockito.any() as nullable type to avoid java.lang.IllegalStateException when + * null is returned. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +fun any(type: Class): T = Mockito.any(type) +inline fun any(): T = any(T::class.java) + +/** + * Kotlin type-inferred version of Mockito.nullable() + */ +inline fun nullable(): T? = Mockito.nullable(T::class.java) + +/** + * Returns ArgumentCaptor.capture() as nullable type to avoid java.lang.IllegalStateException + * when null is returned. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +fun capture(argumentCaptor: ArgumentCaptor): T = argumentCaptor.capture() + +/** + * Helper function for creating an argumentCaptor in kotlin. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +inline fun argumentCaptor(): ArgumentCaptor = + ArgumentCaptor.forClass(T::class.java) + +/** + * Helper function for creating new mocks, without the need to pass in a [Class] instance. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +inline fun mock(): T = Mockito.mock(T::class.java) + +/** + * A kotlin implemented wrapper of [ArgumentCaptor] which prevents the following exception when + * kotlin tests are mocking kotlin objects and the methods take non-null parameters: + * + * java.lang.NullPointerException: capture() must not be null + */ +class KotlinArgumentCaptor constructor(clazz: Class) { + private val wrapped: ArgumentCaptor = ArgumentCaptor.forClass(clazz) + fun capture(): T = wrapped.capture() + val value: T + get() = wrapped.value +} + +/** + * Helper function for creating an argumentCaptor in kotlin. + * + * Generic T is nullable because implicitly bounded by Any?. + */ +inline fun kotlinArgumentCaptor(): KotlinArgumentCaptor = + KotlinArgumentCaptor(T::class.java) + +/** + * Helper function for creating and using a single-use ArgumentCaptor in kotlin. + * + * val captor = argumentCaptor() + * verify(...).someMethod(captor.capture()) + * val captured = captor.value + * + * becomes: + * + * val captured = withArgCaptor { verify(...).someMethod(capture()) } + * + * NOTE: this uses the KotlinArgumentCaptor to avoid the NullPointerException. + */ +inline fun withArgCaptor(block: KotlinArgumentCaptor.() -> Unit): T = + kotlinArgumentCaptor().apply { block() }.value From c8aada443303fa8fe6aa2bbf99efc2e2790e1824 Mon Sep 17 00:00:00 2001 From: vadimt Date: Mon, 28 Mar 2022 12:57:38 -0700 Subject: [PATCH 4/6] Moving away from obsolete Display#getRealSize(). Step 1: use both new and old APIs and make sure they return the same values. Bug: 202567877, 227307209 Test: presubmit Change-Id: I86fbb0ca089a88b5ff787a5c353664a192fb7e79 --- .../launcher3/tapl/LauncherInstrumentation.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java index 8e0eb7b071..f270cd55e6 100644 --- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java +++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java @@ -877,10 +877,9 @@ public final class LauncherInstrumentation { } /** + * @return the Workspace object. * @deprecated use goHome(). * Presses nav bar home button. - * - * @return the Workspace object. */ @Deprecated public Workspace pressHome() { @@ -1653,6 +1652,13 @@ public final class LauncherInstrumentation { Point getRealDisplaySize() { final Point size = new Point(); getContext().getSystemService(WindowManager.class).getDefaultDisplay().getRealSize(size); + final Rect winMetricsHeight = getContext().getSystemService(WindowManager.class) + .getMaximumWindowMetrics() + .getBounds(); + final Point winMetricsSize = new Point(winMetricsHeight.width(), winMetricsHeight.height()); + if (!winMetricsSize.equals(size)) { + fail("Display size mismatch: " + size + " vs " + winMetricsSize); + } return size; } From 59cfcd094600774febe69abbb33621bf0c71fc3d Mon Sep 17 00:00:00 2001 From: Sihua Ma Date: Tue, 29 Mar 2022 10:14:09 -0700 Subject: [PATCH 5/6] Updating font to Google Sans for widget dialog when work profile is paused Changing the font family of no_widgets_text to Google Sans. Test: Manual Fix: 226612140 Change-Id: I70d98c9571aa3a62bb61685b10ae46530630bf97 --- res/layout/widgets_full_sheet.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/res/layout/widgets_full_sheet.xml b/res/layout/widgets_full_sheet.xml index 505ecb1a0d..e867405fd6 100644 --- a/res/layout/widgets_full_sheet.xml +++ b/res/layout/widgets_full_sheet.xml @@ -43,7 +43,6 @@ android:layout_height="match_parent" android:gravity="center" android:visibility="gone" - android:fontFamily="sans-serif-medium" android:textSize="20sp" android:layout_below="@id/search_and_recommendations_container" tools:text="No widgets available" /> From 080d890383db046de7587efdca83846892544c13 Mon Sep 17 00:00:00 2001 From: Thales Lima Date: Mon, 28 Mar 2022 15:30:29 +0100 Subject: [PATCH 6/6] Add border space to all apps cells Also remove tablet condition, phones should also follow the workspace size. Fix: 218638090 Test: visual and HSV Change-Id: Ib201011391c28c0f1dcd0ce8645c3191655a7227 --- res/values/attrs.xml | 42 +++++++++++++---- src/com/android/launcher3/DeviceProfile.java | 31 +++++++----- .../launcher3/InvariantDeviceProfile.java | 47 ++++++++++++++----- 3 files changed, 89 insertions(+), 31 deletions(-) diff --git a/res/values/attrs.xml b/res/values/attrs.xml index c96a2284e7..187689f8c3 100644 --- a/res/values/attrs.xml +++ b/res/values/attrs.xml @@ -258,14 +258,6 @@ - - - - - - - - @@ -280,6 +272,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/android/launcher3/DeviceProfile.java b/src/com/android/launcher3/DeviceProfile.java index 0c33bce5e0..7e7e078d89 100644 --- a/src/com/android/launcher3/DeviceProfile.java +++ b/src/com/android/launcher3/DeviceProfile.java @@ -319,7 +319,7 @@ public class DeviceProfile { pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics), pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics)); cellLayoutBorderSpaceOriginalPx = new Point(cellLayoutBorderSpacePx); - folderCellLayoutBorderSpaceOriginalPx = pxFromDp(inv.folderBorderSpace, mMetrics, 1f); + folderCellLayoutBorderSpaceOriginalPx = pxFromDp(inv.folderBorderSpace, mMetrics); folderCellLayoutBorderSpacePx = new Point(folderCellLayoutBorderSpaceOriginalPx, folderCellLayoutBorderSpaceOriginalPx); @@ -525,21 +525,21 @@ public class DeviceProfile { } private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp) { + return getCellLayoutBorderSpace(idp, 1f); + + } + + private Point getCellLayoutBorderSpace(InvariantDeviceProfile idp, float scale) { if (!isScalableGrid) { return new Point(0, 0); } - int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics); - int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics); + int horizontalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].x, mMetrics, scale); + int verticalSpacePx = pxFromDp(idp.borderSpaces[mTypeIndex].y, mMetrics, scale); return new Point(horizontalSpacePx, verticalSpacePx); } - private Point getCellLayoutBorderSpaceScaled(InvariantDeviceProfile idp, float scale) { - Point original = getCellLayoutBorderSpace(idp); - return new Point((int) (original.x * scale), (int) (original.y * scale)); - } - public Info getDisplayInfo() { return mInfo; } @@ -708,7 +708,7 @@ public class DeviceProfile { iconTextSizePx = (int) (pxFromSp(invIconTextSizeSp, mMetrics) * iconScale); iconDrawablePaddingPx = (int) (iconDrawablePaddingOriginalPx * iconScale); - cellLayoutBorderSpacePx = getCellLayoutBorderSpaceScaled(inv, scale); + cellLayoutBorderSpacePx = getCellLayoutBorderSpace(inv, scale); if (isScalableGrid) { cellWidthPx = pxFromDp(inv.minCellSize[mTypeIndex].x, mMetrics, scale); @@ -756,15 +756,22 @@ public class DeviceProfile { * Updates the iconSize for allApps* variants. */ public void updateAllAppsIconSize(float scale, Resources res) { - //TODO(b/218638090): remove the tablet condition once we have phone specs - if (isScalableGrid && isTablet) { + allAppsBorderSpacePx = new Point( + pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].x, mMetrics, scale), + pxFromDp(inv.allAppsBorderSpaces[mTypeIndex].y, mMetrics, scale)); + if (isScalableGrid) { allAppsIconSizePx = pxFromDp(inv.allAppsIconSize[mTypeIndex], mMetrics); allAppsIconTextSizePx = pxFromSp(inv.allAppsIconTextSize[mTypeIndex], mMetrics); allAppsIconDrawablePaddingPx = iconDrawablePaddingOriginalPx; + // AllApps cells don't have real space between cells, + // so we add the border space to the cell height + allAppsCellHeightPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].y, mMetrics, scale) + + allAppsBorderSpacePx.y; + // but width is just the cell, + // the border is added in #updateAllAppsContainerWidth allAppsCellWidthPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].x, mMetrics, scale); - allAppsCellHeightPx = pxFromDp(inv.allAppsCellSize[mTypeIndex].y, mMetrics, scale); } else { float invIconSizeDp = inv.iconSize[mTypeIndex]; float invIconTextSizeSp = inv.iconTextSize[mTypeIndex]; diff --git a/src/com/android/launcher3/InvariantDeviceProfile.java b/src/com/android/launcher3/InvariantDeviceProfile.java index 219ed9e371..59ef28380b 100644 --- a/src/com/android/launcher3/InvariantDeviceProfile.java +++ b/src/com/android/launcher3/InvariantDeviceProfile.java @@ -900,19 +900,44 @@ public class InvariantDeviceProfile { allAppsCellSize[INDEX_DEFAULT].y); allAppsCellSize[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y); - x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpace, - borderSpace); - allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y); - x = y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape, - allAppsBorderSpaces[INDEX_DEFAULT].x); - allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y); - x = y = a.getFloat( + float allAppsBorderSpace = a.getFloat( + R.styleable.ProfileDisplayOption_allAppsBorderSpace, borderSpace); + float allAppsBorderSpaceLandscape = a.getFloat( + R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscape, + allAppsBorderSpace); + float allAppsBorderSpaceTwoPanelPortrait = a.getFloat( R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortrait, - allAppsBorderSpaces[INDEX_DEFAULT].x); - allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y); - x = y = a.getFloat( + allAppsBorderSpace); + float allAppsBorderSpaceTwoPanelLandscape = a.getFloat( R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscape, - allAppsBorderSpaces[INDEX_DEFAULT].x); + allAppsBorderSpace); + + x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceHorizontal, + allAppsBorderSpace); + y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceVertical, + allAppsBorderSpace); + allAppsBorderSpaces[INDEX_DEFAULT] = new PointF(x, y); + + x = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeHorizontal, + allAppsBorderSpaceLandscape); + y = a.getFloat(R.styleable.ProfileDisplayOption_allAppsBorderSpaceLandscapeVertical, + allAppsBorderSpaceLandscape); + allAppsBorderSpaces[INDEX_LANDSCAPE] = new PointF(x, y); + + x = a.getFloat( + R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitHorizontal, + allAppsBorderSpaceTwoPanelPortrait); + y = a.getFloat( + R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelPortraitVertical, + allAppsBorderSpaceTwoPanelPortrait); + allAppsBorderSpaces[INDEX_TWO_PANEL_PORTRAIT] = new PointF(x, y); + + x = a.getFloat( + R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeHorizontal, + allAppsBorderSpaceTwoPanelLandscape); + y = a.getFloat( + R.styleable.ProfileDisplayOption_allAppsBorderSpaceTwoPanelLandscapeVertical, + allAppsBorderSpaceTwoPanelLandscape); allAppsBorderSpaces[INDEX_TWO_PANEL_LANDSCAPE] = new PointF(x, y); iconSizes[INDEX_DEFAULT] =