Skip to content

Commit

Permalink
Defer to real framework code for AccessibilityWindowInfo.getWindowId
Browse files Browse the repository at this point in the history
The Robolectric implementation of returning -1 was incorrect for API levels
pre-O.

PiperOrigin-RevId: 695389549
  • Loading branch information
hoisie committed Nov 15, 2024
1 parent 434e22d commit 68e04b8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public void testConstructor() {
assertThat(secondInfo.getWindowId()).isEqualTo(firstInfo.getWindowId());
}

/** Pre-O, the window id is set to Integer.MAX_VALUE. Post-O, the window id is set to -1. */
@Test
public void obtain_noArgs_windowId() {
assertThat(AccessibilityNodeInfo.obtain().getWindowId())
.isEqualTo(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? -1 : Integer.MAX_VALUE);
}

@Test
public void obtain_withWindow_returnsWindowId() {
try (ActivityScenario<ActivityWithAnotherTheme> scenario =
Expand Down Expand Up @@ -103,6 +110,7 @@ public void directAccessibilityConnection_queryChildCount() {
node.setQueryFromAppProcessEnabled(rootView, true);
assertThat(node.getChildCount()).isEqualTo(1);
assertThat(node.getChild(0)).isNotNull();
assertThat(node.getWindowId()).isEqualTo(-1);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ public void getSourceNodeId_notZero() {
@Test
public void testConstructor() {
AccessibilityNodeInfo node = AccessibilityNodeInfo.obtain();
assertThat(node.getWindowId()).isEqualTo(AccessibilityWindowInfo.UNDEFINED_WINDOW_ID);
assertThat(node.getWindowId())
.isEqualTo(RuntimeEnvironment.getApiLevel() >= O ? -1 : Integer.MAX_VALUE);
if (RuntimeEnvironment.getApiLevel() >= O) {
// This constant does not exists pre-O.
assertThat(node.getSourceNodeId()).isEqualTo(AccessibilityNodeInfo.UNDEFINED_NODE_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,10 @@ protected AccessibilityWindowInfo getWindow() {
/** Returns the id of the window from which the info comes. */
@Implementation
protected int getWindowId() {
if (useRealAni()) {
if (useRealAni() || accessibilityWindowInfo == null) {
return accessibilityNodeInfoReflector.getWindowId();
}
return (accessibilityWindowInfo == null) ? -1 : accessibilityWindowInfo.getId();
return accessibilityWindowInfo.getId();
}

public void setAccessibilityWindowInfo(AccessibilityWindowInfo info) {
Expand Down

0 comments on commit 68e04b8

Please sign in to comment.