UiAutomator-将小部件添加到主屏幕 [英] UiAutomator -- Add Widget to Home Screen
问题描述
我在Google的uiautomator方面拥有丰富的经验;但是,在将小部件添加到手机主屏幕时,我似乎很沮丧.现在,让它保持简单,并假设要添加小部件的屏幕为空.考虑的过程将是打开应用程序抽屉>单击窗口小部件选项卡>找到要添加的窗口小部件>长按并将窗口小部件拖动到主屏幕.似乎小部件不是长期可点击的".任何想法/建议/解决方案将不胜感激.我实现的代码如下.
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);
}
推荐答案
使用Anders的想法,我设法长按了该小部件并将其拖到家中的某个位置,但随后在回到小部件之前我先简要地看到了我的配置活动清单:((科林码)
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
这篇关于UiAutomator-将小部件添加到主屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!