对控件的操作,主要在UiObject中。例如各种点击事件。
以长按事件来论述详细的流程。
UiObject的longClick方法如下,
public boolean longClick() throws UiObjectNotFoundException {
Tracer.trace();
AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
if(node == null) {
throw new UiObjectNotFoundException(getSelector().toString());
}
Rect rect = getVisibleBounds(node);
return getInteractionController().longTapNoSync(rect.centerX(), rect.centerY());
}
首先调用findAccessibilityNodeInfo方法在当前屏幕重新查找该控件,
然后调用InteractionController 的longTapNoSync方法写入事件。
1.1 findAccessibilityNodeInfo
protected AccessibilityNodeInfo findAccessibilityNodeInfo(long timeout) {
AccessibilityNodeInfo node = null;
long startMills = SystemClock.uptimeMillis();
long currentMills = 0;
while (currentMills <= timeout) {
node = getQueryController().findAccessibilityNodeInfo(getSelector());
if (node != null) {
break;
} else {
// does nothing if we're reentering another runWatchers()
UiDevice.getInstance().runWatchers();
}
currentMills = SystemClock.uptimeMillis() - startMills;
if(timeout > 0) {
SystemClock.sleep(WAIT_FOR_SELECTOR_POLL);
}
}
return node;
}
getQueryController方法返回的是QueryController对象,然后调用findAccessibilityNodeInfo,