Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/platforms/android/__tests__/ui-hierarchy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { buildSnapshotState } from '../../../daemon/handlers/snapshot-capture.ts';
import { isNodeVisibleOnScreen } from '../../../snapshot/mobile-snapshot-semantics.ts';
import { androidUiNodes, parseUiHierarchy } from '../ui-hierarchy.ts';

test('parseUiHierarchy does not truncate when no max node count is requested', () => {
Expand Down Expand Up @@ -85,6 +87,31 @@ test('parseUiHierarchy keeps visible Android nodes with meaningful test identifi
);
});

test('interactive Android snapshots keep a fixed sibling outside filtered scroll content (#1377)', () => {
const xml = `<hierarchy>
<node class="android.widget.FrameLayout" bounds="[0,0][400,800]" visible-to-user="true">
<node class="android.view.View" bounds="[0,0][400,800]" visible-to-user="true">
<node class="android.widget.ScrollView" bounds="[0,100][400,800]" scrollable="true" visible-to-user="true">
<node class="android.widget.TextView" text="Row 1" bounds="[0,100][400,160]" clickable="true" visible-to-user="true"/>
</node>
<node class="android.view.View" bounds="[0,0][400,100]" visible-to-user="true">
<node class="android.view.View" resource-id="header-action" bounds="[340,20][400,80]" clickable="true" visible-to-user="true"/>
</node>
</node>
</node>
</hierarchy>`;

const parsed = parseUiHierarchy(xml, 800, { interactiveOnly: true });
const snapshot = buildSnapshotState(
{ nodes: parsed.nodes, backend: 'android' },
{ snapshotInteractiveOnly: true },
);
const header = snapshot.nodes.find((node) => node.identifier === 'header-action');

assert.equal(header?.parentIndex, undefined);
assert.equal(header && isNodeVisibleOnScreen(header, snapshot.nodes), true);
});

test('parseUiHierarchy reads Android bounds with negative coordinates', () => {
const xml =
'<hierarchy><node class="android.widget.TextView" text="Clipped" bounds="[0,935][-67,994]"/></hierarchy>';
Expand Down
16 changes: 13 additions & 3 deletions src/platforms/android/ui-hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function walkUiHierarchyNode(
const systemChrome =
ancestorSystemChrome || isAndroidSystemChromeWindowResourceId(node.identifier);
const currentIndex = include
? appendAndroidSnapshotNode(state, node, depth, parentIndex, systemChrome)
? appendAndroidSnapshotNode(state, node, parentIndex, systemChrome)
: parentIndex;
const nextAncestorHittable = ancestorHittable || Boolean(node.hittable);
const nextAncestorCollection = ancestorCollection || isCollectionContainerType(node.type);
Expand All @@ -211,11 +211,14 @@ function walkUiHierarchyNode(
function appendAndroidSnapshotNode(
state: AndroidSnapshotBuildState,
node: AndroidNode,
depth: number,
parentIndex: number | undefined,
systemChrome: boolean,
): number {
const currentIndex = state.nodes.length;
// Snapshot filtering removes Compose layout wrappers. Keep depth aligned with
// the retained parent edge, rather than the source tree's depth: otherwise a
// fixed sibling that follows scroll content can be re-parented under the last
// retained row by normalizeSnapshotTree's depth fallback (#1377).
state.sourceNodes.push(node);
state.nodes.push({
index: currentIndex,
Expand All @@ -229,7 +232,7 @@ function appendAndroidSnapshotNode(
focused: node.focused,
visibleToUser: node.visibleToUser,
hittable: node.hittable,
depth,
depth: compactedAndroidNodeDepth(state.nodes, parentIndex),
parentIndex,
...(node.hiddenContentAbove ? { hiddenContentAbove: true } : {}),
...(node.hiddenContentBelow ? { hiddenContentBelow: true } : {}),
Expand All @@ -238,6 +241,13 @@ function appendAndroidSnapshotNode(
return currentIndex;
}

function compactedAndroidNodeDepth(
nodes: AndroidRawSnapshotNode[],
parentIndex: number | undefined,
): number {
return parentIndex === undefined ? 0 : (nodes[parentIndex]?.depth ?? -1) + 1;
}

function hasInteractiveDescendant(state: AndroidSnapshotBuildState, node: AndroidNode): boolean {
const cached = state.interactiveDescendantMemo.get(node);
if (cached !== undefined) return cached;
Expand Down
Loading