Skip to content

Commit b765db5

Browse files
committed
Adding a method for retching the root node in UiTestAutomationBridge
bug:5974791 Change-Id: I46a8b6a259ba31f27fcd6a71d06dc34efdb4de76
1 parent 064d2d6 commit b765db5

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

core/java/android/accessibilityservice/UiTestAutomationBridge.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class UiTestAutomationBridge {
6363

6464
private AccessibilityEvent mLastEvent;
6565

66+
private AccessibilityEvent mLastWindowStateChangeEvent;
67+
6668
private volatile boolean mWaitingForEventDelivery;
6769

6870
private volatile boolean mUnprocessedEventAvailable;
@@ -138,12 +140,22 @@ public void onInterrupt() {
138140
public void onAccessibilityEvent(AccessibilityEvent event) {
139141
synchronized (mLock) {
140142
while (true) {
143+
mLastEvent = AccessibilityEvent.obtain(event);
144+
145+
final int eventType = event.getEventType();
146+
if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
147+
|| eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
148+
if (mLastWindowStateChangeEvent != null) {
149+
mLastWindowStateChangeEvent.recycle();
150+
}
151+
mLastWindowStateChangeEvent = mLastEvent;
152+
}
153+
141154
if (!mWaitingForEventDelivery) {
142155
break;
143156
}
144157
if (!mUnprocessedEventAvailable) {
145158
mUnprocessedEventAvailable = true;
146-
mLastEvent = AccessibilityEvent.obtain(event);
147159
mLock.notifyAll();
148160
break;
149161
}
@@ -409,6 +421,20 @@ public boolean performAccessibilityAction(int accessibilityWindowId, long access
409421
accessibilityWindowId, accessibilityNodeId, action);
410422
}
411423

424+
/**
425+
* Gets the root {@link AccessibilityNodeInfo} in the active window.
426+
*
427+
* @return The root info.
428+
*/
429+
public AccessibilityNodeInfo getRootAccessibilityNodeInfoInActiveWindow() {
430+
synchronized (mLock) {
431+
if (mLastWindowStateChangeEvent != null) {
432+
return mLastWindowStateChangeEvent.getSource();
433+
}
434+
}
435+
return null;
436+
}
437+
412438
private void ensureValidConnection(int connectionId) {
413439
if (connectionId == AccessibilityInteractionClient.NO_ID) {
414440
throw new IllegalStateException("UiAutomationService not connected."

core/tests/coretests/src/android/accessibilityservice/InterrogationActivityTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,35 @@ public void testObjectContract() throws Exception {
430430
}
431431
}
432432
}
433+
434+
@LargeTest
435+
public void testGetRootAccessibilityNodeInfoInActiveWindow() throws Exception {
436+
final long startTimeMillis = SystemClock.uptimeMillis();
437+
try {
438+
// get the root via the designated API
439+
AccessibilityNodeInfo fetched = mUiTestAutomationBridge
440+
.getRootAccessibilityNodeInfoInActiveWindow();
441+
assertNotNull(fetched);
442+
443+
// get the root via traversal
444+
AccessibilityNodeInfo expected = mUiTestAutomationBridge
445+
.findAccessibilityNodeInfoByViewIdInActiveWindow(R.id.root);
446+
while (true) {
447+
AccessibilityNodeInfo parent = expected.getParent();
448+
if (parent == null) {
449+
break;
450+
}
451+
expected = parent;
452+
}
453+
assertNotNull(expected);
454+
455+
assertEquals("The node with id \"root\" should be the root.", expected, fetched);
456+
} finally {
457+
if (DEBUG) {
458+
final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis;
459+
Log.i(LOG_TAG, "testGetRootAccessibilityNodeInfoInActiveWindow: "
460+
+ elapsedTimeMillis + "ms");
461+
}
462+
}
463+
}
433464
}

0 commit comments

Comments
 (0)