Removing magic constants from TAPL/Widgets

Bug: 123904290
Change-Id: I8abc6cde6be8a51b38782f5c94749fe0c33384c1
diff --git a/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java b/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java
index 3f35a3a..e1b3ede 100644
--- a/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java
+++ b/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java
@@ -41,7 +41,6 @@
 import com.android.launcher3.util.rule.ShellCommandRule;
 
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -53,7 +52,8 @@
 @RunWith(AndroidJUnit4.class)
 public class AddConfigWidgetTest extends AbstractLauncherUiTest {
 
-    @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
+    @Rule
+    public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
 
     private LauncherAppWidgetProviderInfo mWidgetInfo;
     private AppWidgetManager mAppWidgetManager;
@@ -70,14 +70,12 @@
 
     @Test
     @PortraitLandscape
-    @org.junit.Ignore
     public void testWidgetConfig() throws Throwable {
         runTest(true);
     }
 
     @Test
     @PortraitLandscape
-    @org.junit.Ignore
     public void testConfigCancelled() throws Throwable {
         runTest(false);
     }
diff --git a/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java b/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java
index 1edce22..b8ca5de 100644
--- a/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java
+++ b/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java
@@ -30,7 +30,6 @@
 import com.android.launcher3.ui.TestViewHelpers;
 import com.android.launcher3.util.rule.ShellCommandRule;
 
-import org.junit.Ignore;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -42,11 +41,11 @@
 @RunWith(AndroidJUnit4.class)
 public class AddWidgetTest extends AbstractLauncherUiTest {
 
-    @Rule public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
+    @Rule
+    public ShellCommandRule mGrantWidgetRule = ShellCommandRule.grantWidgetBind();
 
     @Test
     @PortraitLandscape
-    @org.junit.Ignore
     public void testDragIcon() throws Throwable {
         clearHomescreen();
         mDevice.pressHome();
diff --git a/tests/tapl/com/android/launcher3/tapl/Widgets.java b/tests/tapl/com/android/launcher3/tapl/Widgets.java
index 2a04d46..0cd6c38 100644
--- a/tests/tapl/com/android/launcher3/tapl/Widgets.java
+++ b/tests/tapl/com/android/launcher3/tapl/Widgets.java
@@ -16,8 +16,6 @@
 
 package com.android.launcher3.tapl;
 
-import static org.junit.Assert.fail;
-
 import android.graphics.Point;
 import android.graphics.Rect;
 
@@ -28,11 +26,13 @@
 
 import com.android.launcher3.ResourceUtils;
 
+import java.util.Collection;
+import java.util.Collections;
+
 /**
  * All widgets container.
  */
 public final class Widgets extends LauncherInstrumentation.VisibleContainer {
-    private static final Rect MARGINS = new Rect(100, 100, 100, 100);
     private static final int FLING_STEPS = 10;
 
     Widgets(LauncherInstrumentation launcher) {
@@ -48,12 +48,12 @@
                 "want to fling forward in widgets")) {
             LauncherInstrumentation.log("Widgets.flingForward enter");
             final UiObject2 widgetsContainer = verifyActiveContainer();
-            final int margin = widgetsContainer.getVisibleBounds().bottom -
-                    mLauncher.getRealDisplaySize().y +
-                    ResourceUtils.getNavbarSize(
-                            ResourceUtils.NAVBAR_BOTTOM_GESTURE_SIZE, mLauncher.getResources());
             mLauncher.scroll(
-                    widgetsContainer, Direction.DOWN, 1f, new Rect(0, 0, 0, margin), FLING_STEPS);
+                    widgetsContainer,
+                    Direction.DOWN,
+                    1f,
+                    new Rect(0, 0, 0, getBottomGestureMargin(widgetsContainer)),
+                    FLING_STEPS);
             try (LauncherInstrumentation.Closable c1 = mLauncher.addContextLayer("flung forward")) {
                 verifyActiveContainer();
             }
@@ -61,6 +61,16 @@
         }
     }
 
+    private int getBottomGestureMargin(UiObject2 widgetsContainer) {
+        return widgetsContainer.getVisibleBounds().bottom - mLauncher.getRealDisplaySize().y +
+                getBottomGestureSize();
+    }
+
+    private int getBottomGestureSize() {
+        return ResourceUtils.getNavbarSize(
+                ResourceUtils.NAVBAR_BOTTOM_GESTURE_SIZE, mLauncher.getResources()) + 1;
+    }
+
     /**
      * Flings backward (up) and waits the fling's end.
      */
@@ -83,32 +93,48 @@
     }
 
     public Widget getWidget(String labelText) {
-        final int margin = ResourceUtils.getNavbarSize(
-                ResourceUtils.NAVBAR_BOTTOM_GESTURE_SIZE, mLauncher.getResources()) + 1;
         final UiObject2 widgetsContainer = verifyActiveContainer();
-        widgetsContainer.setGestureMargins(0, 0, 0, margin);
-
         final Point displaySize = mLauncher.getRealDisplaySize();
+        final BySelector labelSelector = By.clazz("android.widget.TextView").text(labelText);
 
         int i = 0;
-        final BySelector selector = By.clazz("android.widget.TextView").text(labelText);
-
         for (; ; ) {
-            final UiObject2 label = mLauncher.tryWaitForLauncherObject(selector, 300);
-            if (label != null) {
+            final Collection<UiObject2> cells = mLauncher.getObjectsInContainer(
+                    widgetsContainer, "widgets_cell_list_container");
+            mLauncher.assertTrue("Widgets doesn't have 2 rows", cells.size() >= 2);
+            for (UiObject2 cell : cells) {
+                final UiObject2 label = cell.findObject(labelSelector);
+                if (label == null) continue;
+
                 final UiObject2 widget = label.getParent().getParent();
                 mLauncher.assertEquals(
                         "View is not WidgetCell",
                         "com.android.launcher3.widget.WidgetCell",
                         widget.getClassName());
 
-                if (widget.getVisibleBounds().bottom <= displaySize.y - margin) {
+                if (widget.getVisibleBounds().bottom <= displaySize.y - getBottomGestureSize()) {
                     return new Widget(mLauncher, widget);
                 }
             }
 
-            if (++i > 40) fail("Too many attempts");
-            mLauncher.scroll(widgetsContainer, Direction.DOWN, 0.7f, MARGINS, 50);
+            mLauncher.assertTrue("Too many attempts", ++i <= 40);
+            final UiObject2 lowestCell = Collections.max(cells, (c1, c2) ->
+                    Integer.compare(c1.getVisibleBounds().top, c2.getVisibleBounds().top));
+
+            final int gestureStart = lowestCell.getVisibleBounds().top + mLauncher.getTouchSlop();
+            final int distance = gestureStart - widgetsContainer.getVisibleBounds().top;
+            final int bottomMargin = widgetsContainer.getVisibleBounds().height() - distance;
+
+            mLauncher.scroll(
+                    widgetsContainer,
+                    Direction.DOWN,
+                    1f,
+                    new Rect(
+                            0,
+                            0,
+                            0,
+                            Math.max(bottomMargin, getBottomGestureMargin(widgetsContainer))),
+                    150);
         }
     }
 }