Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0b20a11d authored by vadimt's avatar vadimt Committed by Vadim Tryshev
Browse files

Converting even more tests to TAPL

Bug: 131116002
Bug: 115582915
Change-Id: Id36fca232b5aff5f6692142ce4a0c937d0531f4e
parent 7926791f
Loading
Loading
Loading
Loading
+0 −31
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@ import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import static java.lang.System.exit;

@@ -38,10 +37,7 @@ import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.Direction;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

import com.android.launcher3.Launcher;
@@ -50,7 +46,6 @@ import com.android.launcher3.LauncherModel;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.LauncherState;
import com.android.launcher3.MainThreadExecutor;
import com.android.launcher3.ResourceUtils;
import com.android.launcher3.Utilities;
import com.android.launcher3.compat.LauncherAppsCompat;
import com.android.launcher3.model.AppLaunchTracker;
@@ -86,7 +81,6 @@ public abstract class AbstractLauncherUiTest {
    public static final long DEFAULT_ACTIVITY_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
    public static final long DEFAULT_BROADCAST_TIMEOUT_SECS = 5;

    public static final long SHORT_UI_TIMEOUT = 300;
    public static final long DEFAULT_UI_TIMEOUT = 10000;
    private static final String TAG = "AbstractLauncherUiTest";

@@ -180,31 +174,6 @@ public abstract class AbstractLauncherUiTest {
        }
    }

    /**
     * Scrolls the {@param container} until it finds an object matching {@param condition}.
     *
     * @return the matching object.
     */
    protected UiObject2 scrollAndFind(UiObject2 container, BySelector condition) {
        final int margin = ResourceUtils.getNavbarSize(
                ResourceUtils.NAVBAR_BOTTOM_GESTURE_SIZE, mLauncher.getResources()) + 1;
        container.setGestureMargins(0, 0, 0, margin);

        int i = 0;
        for (; ; ) {
            // findObject can only execute after spring settles.
            mDevice.wait(Until.findObject(condition), SHORT_UI_TIMEOUT);
            UiObject2 widget = container.findObject(condition);
            if (widget != null && widget.getVisibleBounds().intersects(
                    0, 0, mDevice.getDisplayWidth(),
                    mDevice.getDisplayHeight() - margin)) {
                return widget;
            }
            if (++i > 40) fail("Too many attempts");
            container.scroll(Direction.DOWN, 1f);
        }
    }

    /**
     * Removes all icons from homescreen and hotseat.
     */
+6 −13
Original line number Diff line number Diff line
@@ -216,7 +216,8 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest {
            final AppIcon app = allApps.getAppIcon("TestActivity7");
            assertNotNull("AppIcon.launch returned null", app.launch(getAppPackageName()));
            test.executeOnLauncher(launcher -> assertTrue(
                    "Launcher activity is the top activity; expecting another activity to be the top "
                    "Launcher activity is the top activity; expecting another activity to be the "
                            + "top "
                            + "one",
                    test.isInBackground(launcher)));
        } finally {
@@ -304,11 +305,8 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest {
                switchToAllApps();
        allApps.freeze();
        try {
            allApps.
                    getAppIcon(APP_NAME).
                    dragToWorkspace().
                    getWorkspaceAppIcon(APP_NAME).
                    launch(getAppPackageName());
            allApps.getAppIcon(APP_NAME).dragToWorkspace();
            mLauncher.getWorkspace().getWorkspaceAppIcon(APP_NAME).launch(getAppPackageName());
        } finally {
            allApps.unfreeze();
        }
@@ -335,13 +333,8 @@ public class TaplTestsLauncher3 extends AbstractLauncherUiTest {
                    getMenuItem(0);
            final String shortcutName = menuItem.getText();

            // 4. Drag the first shortcut to the home screen.
            // 5. Verify that the shortcut works on home screen
            //    (the app opens and has the same text as the shortcut).
            menuItem.
                    dragToWorkspace().
                    getWorkspaceAppIcon(shortcutName).
                    launch(getAppPackageName());
            menuItem.dragToWorkspace();
            mLauncher.getWorkspace().getWorkspaceAppIcon(shortcutName).launch(getAppPackageName());
        } finally {
            allApps.unfreeze();
        }
+0 −114
Original line number Diff line number Diff line
@@ -18,28 +18,15 @@ package com.android.launcher3.ui;
import static androidx.test.InstrumentationRegistry.getInstrumentation;
import static androidx.test.InstrumentationRegistry.getTargetContext;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import android.content.ComponentName;
import android.content.Context;
import android.graphics.Point;
import android.os.Process;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.test.uiautomator.By;
import androidx.test.uiautomator.BySelector;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject2;
import androidx.test.uiautomator.Until;

import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.R;
import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.testcomponent.AppWidgetNoConfig;
import com.android.launcher3.testcomponent.AppWidgetWithConfig;
@@ -50,21 +37,6 @@ import java.util.function.Function;
public class TestViewHelpers {
    private static final String TAG = "TestViewHelpers";

    private static UiDevice getDevice() {
        return UiDevice.getInstance(getInstrumentation());
    }

    public static UiObject2 findViewById(int id) {
        return getDevice().wait(Until.findObject(getSelectorForId(id)),
                AbstractLauncherUiTest.DEFAULT_UI_TIMEOUT);
    }

    public static BySelector getSelectorForId(int id) {
        final Context targetContext = getTargetContext();
        String name = targetContext.getResources().getResourceEntryName(id);
        return By.res(targetContext.getPackageName(), name);
    }

    /**
     * Finds a widget provider which can fit on the home screen.
     *
@@ -91,92 +63,6 @@ public class TestViewHelpers {
        return info;
    }

    /**
     * Drags an icon to the center of homescreen.
     *
     * @param icon object that is either app icon or shortcut icon
     */
    public static void dragToWorkspace(UiObject2 icon, boolean expectedToShowShortcuts) {
        Point center = icon.getVisibleCenter();

        // Action Down
        final long downTime = SystemClock.uptimeMillis();
        sendPointer(downTime, MotionEvent.ACTION_DOWN, center);

        UiObject2 dragLayer = findViewById(R.id.drag_layer);

        if (expectedToShowShortcuts) {
            // Make sure shortcuts show up, and then move a bit to hide them.
            assertNotNull(findViewById(R.id.deep_shortcuts_container));

            Point moveLocation = new Point(center);
            int distanceToMove =
                    getTargetContext().getResources().getDimensionPixelSize(
                            R.dimen.deep_shortcuts_start_drag_threshold) + 50;
            if (moveLocation.y - distanceToMove >= dragLayer.getVisibleBounds().top) {
                moveLocation.y -= distanceToMove;
            } else {
                moveLocation.y += distanceToMove;
            }
            movePointer(downTime, center, moveLocation);

            assertNull(findViewById(R.id.deep_shortcuts_container));
        }

        // Wait until Remove/Delete target is visible
        assertNotNull(findViewById(R.id.delete_target_text));

        Point moveLocation = dragLayer.getVisibleCenter();

        // Move to center
        movePointer(downTime, center, moveLocation);
        sendPointer(downTime, MotionEvent.ACTION_UP, moveLocation);

        // Wait until remove target is gone.
        getDevice().wait(Until.gone(getSelectorForId(R.id.delete_target_text)),
                AbstractLauncherUiTest.DEFAULT_UI_TIMEOUT);
    }

    private static void movePointer(long downTime, Point from, Point to) {
        while (!from.equals(to)) {
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            from.x = getNextMoveValue(to.x, from.x);
            from.y = getNextMoveValue(to.y, from.y);
            sendPointer(downTime, MotionEvent.ACTION_MOVE, from);
        }
    }

    private static int getNextMoveValue(int targetValue, int oldValue) {
        if (targetValue - oldValue > 10) {
            return oldValue + 10;
        } else if (targetValue - oldValue < -10) {
            return oldValue - 10;
        } else {
            return targetValue;
        }
    }

    public static void sendPointer(long downTime, int action, Point point) {
        MotionEvent event = MotionEvent.obtain(downTime,
                SystemClock.uptimeMillis(), action, point.x, point.y, 0);
        getInstrumentation().sendPointerSync(event);
        event.recycle();
    }

    /**
     * Opens widget tray and returns the recycler view.
     */
    public static UiObject2 openWidgetsTray() {
        final UiDevice device = getDevice();
        device.pressKeyCode(KeyEvent.KEYCODE_W, KeyEvent.META_CTRL_ON);
        device.waitForIdle();
        return findViewById(R.id.widgets_list_view);
    }

    public static View findChildView(ViewGroup parent, Function<View, Boolean> condition) {
        for (int i = 0; i < parent.getChildCount(); ++i) {
            final View child = parent.getChildAt(i);
+5 −11
Original line number Diff line number Diff line
@@ -27,20 +27,18 @@ import android.view.View;

import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiObject2;

import com.android.launcher3.ItemInfo;
import com.android.launcher3.LauncherAppWidgetInfo;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.Workspace;
import com.android.launcher3.tapl.Widgets;
import com.android.launcher3.testcomponent.WidgetConfigActivity;
import com.android.launcher3.ui.AbstractLauncherUiTest;
import com.android.launcher3.ui.TestViewHelpers;
import com.android.launcher3.util.Condition;
import com.android.launcher3.util.Wait;
import com.android.launcher3.util.rule.ShellCommandRule;
import com.android.launcher3.widget.WidgetCell;

import org.junit.Before;
import org.junit.Ignore;
@@ -71,7 +69,6 @@ public class AddConfigWidgetTest extends AbstractLauncherUiTest {
    }

    @Test
    // Convert test to TAPL b/131116002
    public void testWidgetConfig() throws Throwable {
        runTest(false, true);
    }
@@ -83,7 +80,6 @@ public class AddConfigWidgetTest extends AbstractLauncherUiTest {
    }

    @Test
    // Convert test to TAPL b/131116002
    public void testConfigCancelled() throws Throwable {
        runTest(false, false);
    }
@@ -104,15 +100,13 @@ public class AddConfigWidgetTest extends AbstractLauncherUiTest {
        clearHomescreen();
        mActivityMonitor.startLauncher();

        // Open widget tray and wait for load complete.
        final UiObject2 widgetContainer = TestViewHelpers.openWidgetsTray();
        Wait.atMost(null, Condition.minChildCount(widgetContainer, 2), DEFAULT_UI_TIMEOUT);
        final Widgets widgets = mLauncher.getWorkspace().openAllWidgets();

        // Drag widget to homescreen
        WidgetConfigStartupMonitor monitor = new WidgetConfigStartupMonitor();
        UiObject2 widget = scrollAndFind(widgetContainer, By.clazz(WidgetCell.class)
                .hasDescendant(By.text(mWidgetInfo.getLabel(mTargetContext.getPackageManager()))));
        TestViewHelpers.dragToWorkspace(widget, false);
        widgets.
                getWidget(mWidgetInfo.getLabel(mTargetContext.getPackageManager())).
                dragToWorkspace();
        // Widget id for which the config activity was opened
        mWidgetId = monitor.getWidgetId();

+8 −24
Original line number Diff line number Diff line
@@ -19,20 +19,12 @@ import static org.junit.Assert.assertTrue;

import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiObject2;
import android.view.View;

import com.android.launcher3.ItemInfo;
import com.android.launcher3.LauncherAppWidgetInfo;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.Workspace.ItemOperator;
import com.android.launcher3.ui.AbstractLauncherUiTest;
import com.android.launcher3.ui.TestViewHelpers;
import com.android.launcher3.util.Condition;
import com.android.launcher3.util.Wait;
import com.android.launcher3.util.rule.ShellCommandRule;
import com.android.launcher3.widget.WidgetCell;

import org.junit.Ignore;
import org.junit.Rule;
@@ -61,7 +53,6 @@ public class AddWidgetTest extends AbstractLauncherUiTest {
        performTest();
    }

    // Convert to TAPL b/131116002
    private void performTest() throws Throwable {
        clearHomescreen();
        mActivityMonitor.startLauncher();
@@ -69,22 +60,15 @@ public class AddWidgetTest extends AbstractLauncherUiTest {
        final LauncherAppWidgetProviderInfo widgetInfo =
                TestViewHelpers.findWidgetProvider(this, false /* hasConfigureScreen */);

        // Open widget tray and wait for load complete.
        final UiObject2 widgetContainer = TestViewHelpers.openWidgetsTray();
        Wait.atMost(null, Condition.minChildCount(widgetContainer, 2), DEFAULT_UI_TIMEOUT);
        mLauncher.
                getWorkspace().
                openAllWidgets().
                getWidget(widgetInfo.getLabel(mTargetContext.getPackageManager())).
                dragToWorkspace();

        // Drag widget to homescreen
        UiObject2 widget = scrollAndFind(widgetContainer, By.clazz(WidgetCell.class)
                .hasDescendant(By.text(widgetInfo.getLabel(mTargetContext.getPackageManager()))));
        TestViewHelpers.dragToWorkspace(widget, false);

        assertTrue(mActivityMonitor.itemExists(new ItemOperator() {
            @Override
            public boolean evaluate(ItemInfo info, View view) {
                return info instanceof LauncherAppWidgetInfo &&
        assertTrue(mActivityMonitor.itemExists(
                (info, view) -> info instanceof LauncherAppWidgetInfo &&
                        ((LauncherAppWidgetInfo) info).providerName.getClassName().equals(
                                widgetInfo.provider.getClassName());
            }
        }).call());
                                widgetInfo.provider.getClassName())).call());
    }
}
Loading