As 3 years passed since this question: UiAutomator -- Add Widget to Home Screen
I'd like to make an updated one.
So I want to test my widget and the only way to do it seems to be using UiAutomator.
Using Anders idea in the previous question (usage of swipe(…) to do a long press), I managed to go to the widgets list, scrolling looking for my widget, long press to trigger the widget setup and drag it to somewhere on the home but then, the configuration activity briefly appears before going back to the widgets list :( Here is the code (Kotlin):
@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
Aucun commentaire:
Enregistrer un commentaire