Merge "Fix testDragToFolder()" into main

This commit is contained in:
Federico Baron
2024-02-23 00:41:55 +00:00
committed by Android (Google) Code Review
6 changed files with 74 additions and 20 deletions
@@ -16,6 +16,8 @@
package com.android.launcher3.tapl;
import android.graphics.Rect;
import androidx.annotation.NonNull;
import androidx.test.uiautomator.UiObject2;
@@ -58,4 +60,7 @@ public class Folder {
return mLauncher.getWorkspace();
}
}
Rect getDropLocationBounds() {
return mLauncher.getVisibleBounds(mContainer);
}
}
@@ -26,7 +26,7 @@ import com.android.launcher3.testing.shared.TestProtocol;
/**
* Folder Icon, an app folder in workspace.
*/
public class FolderIcon implements FolderDragTarget {
public class FolderIcon implements IconDragTarget {
protected final UiObject2 mObject;
protected final LauncherInstrumentation mLauncher;
@@ -60,7 +60,7 @@ public class FolderIcon implements FolderDragTarget {
/** This method requires public access, however should not be called in tests. */
@Override
public FolderIcon getTargetFolder(Rect bounds) {
public FolderIcon getTargetIcon(Rect bounds) {
return this;
}
}
@@ -27,7 +27,7 @@ import java.util.function.Supplier;
/**
* App icon on the workspace or all apps.
*/
public abstract class HomeAppIcon extends AppIcon implements FolderDragTarget, WorkspaceDragSource {
public abstract class HomeAppIcon extends AppIcon implements IconDragTarget, WorkspaceDragSource {
private final String mAppName;
@@ -42,7 +42,7 @@ public abstract class HomeAppIcon extends AppIcon implements FolderDragTarget, W
* @param target the destination icon.
*/
@NonNull
public FolderIcon dragToIcon(FolderDragTarget target) {
public FolderIcon dragToIcon(IconDragTarget target) {
try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
LauncherInstrumentation.Closable c = mLauncher.addContextLayer("want to drag icon")) {
final Rect dropBounds = target.getDropLocationBounds();
@@ -51,13 +51,33 @@ public abstract class HomeAppIcon extends AppIcon implements FolderDragTarget, W
() -> {
final Rect bounds = target.getDropLocationBounds();
return new Point(bounds.centerX(), bounds.centerY());
});
FolderIcon result = target.getTargetFolder(dropBounds);
}, false);
FolderIcon result = target.getTargetIcon(dropBounds);
mLauncher.assertTrue("Can't find the target folder.", result != null);
return result;
}
}
/**
* Drag the AppIcon to the given position of a folder icon, and then inside that folder.
*
* @param target the destination folder.
*/
@NonNull
public Folder dragToFolder(IconDragTarget target) {
try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck();
LauncherInstrumentation.Closable c = mLauncher.addContextLayer("want to drag icon")) {
Workspace.dragIconToWorkspace(
mLauncher, this,
() -> {
final Rect bounds = target.getDropLocationBounds();
return new Point(bounds.centerX(), bounds.centerY());
}, /* isDraggingToFolder */ true);
}
return new Folder(mLauncher);
}
/** This method requires public access, however should not be called in tests. */
@Override
public Rect getDropLocationBounds() {
@@ -66,7 +86,7 @@ public abstract class HomeAppIcon extends AppIcon implements FolderDragTarget, W
/** This method requires public access, however should not be called in tests. */
@Override
public FolderIcon getTargetFolder(Rect bounds) {
public FolderIcon getTargetIcon(Rect bounds) {
for (FolderIcon folderIcon : mLauncher.getWorkspace().getFolderIcons()) {
final Rect folderIconBounds = folderIcon.getDropLocationBounds();
if (bounds.contains(folderIconBounds.centerX(), folderIconBounds.centerY())) {
@@ -18,11 +18,11 @@ package com.android.launcher3.tapl;
import android.graphics.Rect;
public interface FolderDragTarget {
public interface IconDragTarget {
/** This method requires public access, however should not be called in tests. */
Rect getDropLocationBounds();
/** This method requires public access, however should not be called in tests. */
FolderIcon getTargetFolder(Rect bounds);
FolderIcon getTargetIcon(Rect bounds);
}
@@ -551,7 +551,8 @@ public final class Workspace extends Home {
* This function expects the launchable is inside the workspace and there is no drop event.
*/
static void dragIconToWorkspace(
LauncherInstrumentation launcher, Launchable launchable, Supplier<Point> destSupplier) {
LauncherInstrumentation launcher, Launchable launchable, Supplier<Point> destSupplier,
boolean isDraggingToFolder) {
dragIconToWorkspace(
launcher,
launchable,
@@ -559,7 +560,8 @@ public final class Workspace extends Home {
/* isDecelerating= */ false,
() -> launcher.expectEvent(TestProtocol.SEQUENCE_MAIN, LONG_CLICK_EVENT),
/* expectDropEvents= */ null,
/* startsActivity = */ false);
/* startsActivity = */ false,
isDraggingToFolder);
}
static void dragIconToWorkspace(
@@ -570,7 +572,8 @@ public final class Workspace extends Home {
@Nullable Runnable expectDropEvents,
boolean startsActivity) {
dragIconToWorkspace(launcher, launchable, dest, /* isDecelerating */ true,
expectLongClickEvents, expectDropEvents, startsActivity);
expectLongClickEvents, expectDropEvents, startsActivity,
/* isDraggingToFolder */ false);
}
static void dragIconToWorkspace(
@@ -580,7 +583,8 @@ public final class Workspace extends Home {
boolean isDecelerating,
Runnable expectLongClickEvents,
@Nullable Runnable expectDropEvents,
boolean startsActivity) {
boolean startsActivity,
boolean isDraggingToFolder) {
try (LauncherInstrumentation.Closable ignored = launcher.addContextLayer(
"want to drag icon to workspace")) {
final long downTime = SystemClock.uptimeMillis();
@@ -607,11 +611,27 @@ public final class Workspace extends Home {
dragStart = screenEdge;
}
// targetDest.x is now between 0 and displayX so we found the target page,
// we just have to put move the icon to the destination and drop it
launcher.movePointer(dragStart, targetDest, DEFAULT_DRAG_STEPS, isDecelerating,
downTime, SystemClock.uptimeMillis(), false,
LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
// targetDest.x is now between 0 and displayX so we found the target page.
// If not a folder, we just have to put move the icon to the destination and drop it.
// If it's a folder we want to drag to the folder icon and then drag to the center of
// that folder when it opens.
if (isDraggingToFolder) {
Point finalDragStart = dragStart;
Point finalTargetDest = targetDest;
Folder folder = executeAndWaitForFolderOpen(launcher, () -> launcher.movePointer(
finalDragStart, finalTargetDest, DEFAULT_DRAG_STEPS, isDecelerating,
downTime, SystemClock.uptimeMillis(), false,
LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER));
Rect dropBounds = folder.getDropLocationBounds();
dragStart = targetDest;
targetDest = new Point(dropBounds.centerX(), dropBounds.centerY());
}
launcher.movePointer(dragStart, targetDest,
DEFAULT_DRAG_STEPS, isDecelerating, downTime, SystemClock.uptimeMillis(),
false, LauncherInstrumentation.GestureScope.DONT_EXPECT_PILFER);
dropDraggedIcon(launcher, targetDest, downTime, expectDropEvents, startsActivity);
}
}
@@ -696,6 +716,16 @@ public final class Workspace extends Home {
() -> "Page scroll didn't happen", "Scrolling page");
}
private static Folder executeAndWaitForFolderOpen(LauncherInstrumentation launcher,
Runnable command) {
launcher.executeAndWaitForEvent(command,
event -> TestProtocol.FOLDER_OPENED_MESSAGE.equals(
event.getClassName().toString()),
() -> "Fail to open folder.",
"open folder");
return new Folder(launcher);
}
static void dragIconToHotseat(
LauncherInstrumentation launcher,
Launchable launchable,
@@ -87,8 +87,7 @@ public class TaplDragTest extends AbstractLauncherUiTest {
PHOTOS_APP_NAME);
final HomeAppIcon mapIcon = createShortcutInCenterIfNotExist(MAPS_APP_NAME);
folderIcon = mapIcon.dragToIcon(folderIcon);
folder = folderIcon.open();
folder = mapIcon.dragToFolder(folderIcon);
folder.getAppIcon(MAPS_APP_NAME);
workspace = folder.close();