Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UiAutomator -- Add Widget to Home Screen

I have a fair amount of experience with Google's uiautomator; however, I seem to be stumped when it comes to adding a widget to the phone's home screen. For now lets keep it simple and assume the screen the widget is being added to is empty. The thought process would be to open the app drawer > click the widgets tab > locate the widget to be added > long click and drag the widget to the home screen. It seems like widgets are not "long clickable" though. Any thoughts/suggestions/solutions will be appreciated. The code I have implemented is below.

@Override
protected void setUp() throws UiObjectNotFoundException {
    getUiDevice().pressHome();

    new UiObject(new UiSelector().className(TEXT_VIEW).description("Apps")).clickAndWaitForNewWindow();
    new UiObject(new UiSelector().className(TEXT_VIEW).text("Widgets")).click();

    UiScrollable widgets = new UiScrollable(new UiSelector().scrollable(true));
    widgets.setAsHorizontalList();
    widgets.flingToEnd(MAX_SWIPES);

    UiObject widget = widgets.getChildByText(
            new UiSelector().className(TEXT_VIEW).resourceId("com.android.launcher:id/widget_name"),
            WIDGET_NAME
    );

    // Returns true
    System.out.println("exists(): " + widget.exists());
    // Returns false...
    System.out.println("longClickable(): " + widget.isLongClickable());

    widget.longClick();

    // Also tried...
    int startX = sonosWidget.getVisibleBounds().centerX();
    int startY = sonosWidget.getVisibleBounds().centerY();
    getUiDevice().drag(startX, startY, 0, 0, 3);
}
like image 823
Justin Papez Avatar asked Nov 27 '22 09:11

Justin Papez


1 Answers

Using Anders idea, I managed to long press the widget and drag it to somewhere on the home but then I see my configuration activity briefly before going back to the widgets list :( (Kolin code)

@Before fun setWidgetOnHome() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
    val screenSize = Point(mDevice.displayWidth, mDevice.displayHeight)
    val screenCenter = Point(screenSize.x / 2, screenSize.y / 2)

    mDevice.pressHome()

    val launcherPackage = mDevice.launcherPackageName!!
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)

    // attempt long press
    mDevice.swipe(arrayOf(screenCenter, screenCenter), 150)
    pauseTest(2000)

    mDevice.findObject(By.text("Widgets")).click()
    mDevice.waitForIdle()

    val y = screenSize.y / 2

    var widget = mDevice.findObject(By.text("Efficio"))
    var additionalSwipe = 1
    while (widget == null || additionalSwipe > 0) {
        mDevice.swipe(screenCenter.x, y, screenCenter.x, 0, 150)
        mDevice.waitForIdle()
        if (widget == null) {
            widget = mDevice.findObject(By.text("Efficio"))
        } else {
            additionalSwipe--
        }
    }
    val b = widget.visibleBounds
    val c = Point(b.left + 150, b.bottom + 150)
    val dest = Point(c.x + 250, c.y + 250)
    mDevice.swipe(arrayOf(c, c, dest), 150)
}

I believe something is happening but what? :-/ it's like a back was clicked

like image 120
Geob-o-matic Avatar answered Dec 06 '22 07:12

Geob-o-matic