diff --git a/packages/platform-harmonyos/src/__tests__/arkui-settings-layout.fixture.ts b/packages/platform-harmonyos/src/__tests__/arkui-settings-layout.fixture.ts new file mode 100644 index 0000000000..cada8ea28a --- /dev/null +++ b/packages/platform-harmonyos/src/__tests__/arkui-settings-layout.fixture.ts @@ -0,0 +1,249 @@ +/** + * Sanitized `uitest dumpLayout` shapes from a real Huawei device (nova 14, OpenHarmony 6.1.1, + * 1084x2412) on Settings > Developer options. Attribute names, spellings, and the nesting that + * produced #4906 and #4907 are kept; bounds and unrelated attributes are trimmed, and no personal + * data is carried. + * + * The two shapes that matter: + * - `SwitchMenuComponent` rows, where a `Toggle` reports only its resource id and the words on the + * screen are a sibling `Text` of the row container, whose own `id` is the same for every row; + * - a merged status-bar window, which `dumpLayout` reports beside the application's own content root. + */ + +const ARK_UI_BOUNDS = { + statusBar: '[0,0][1084,105]', + titleBar: '[0,105][1084,273]', + backButton: '[48,129][168,249]', + titleText: '[192,154][717,224]', + row: '[48,1161][1036,1369]', + rowTextColumn: '[84,1185][868,1345]', + title: '[84,1185][435,1241]', + summary: '[84,1247][743,1345]', + toggle: '[892,1235][1000,1295]', +} as const; + +/** ArkUI reports these fields on every node; the mapping reads only a few of them. */ +function arkUiAttributes( + type: string, + attributes: Record = {}, +): Record { + return { + type, + bounds: ARK_UI_BOUNDS.statusBar, + clickable: 'false', + description: '', + enabled: 'true', + focused: 'false', + id: '', + key: '', + text: '', + originalText: '', + visible: 'true', + ...attributes, + }; +} + +/** The status-bar window of `com.ohos.sceneboard`, merged into the same dump as the app's tree. */ +function statusBarWindow() { + return { + attributes: arkUiAttributes('WindowScene', { + bounds: ARK_UI_BOUNDS.statusBar, + id: 'session24', + key: 'session24', + hostWindowId: '24', + }), + children: [ + { + attributes: arkUiAttributes('__Common__', { + bounds: ARK_UI_BOUNDS.statusBar, + id: 'StatusBarBox', + hostWindowId: '24', + }), + children: [ + { + attributes: arkUiAttributes('Stack', { + bounds: ARK_UI_BOUNDS.statusBar, + hostWindowId: '24', + }), + children: [ + { + // Reports itself clickable and carries no text, which is what made it the nearest + // "label" for the app's back button (#4907). + attributes: arkUiAttributes('__Common__', { + bounds: ARK_UI_BOUNDS.statusBar, + clickable: 'true', + id: 'StatusBarBridgeView', + hostWindowId: '24', + }), + }, + ], + }, + ], + }, + ], + }; +} + +/** One settings row: `SwitchMenuComponent` holds the words, the `Toggle` holds only its id. */ +function switchMenuRow(title: string, toggleId: string) { + return { + attributes: arkUiAttributes('Flex', { + bounds: ARK_UI_BOUNDS.row, + clickable: 'true', + // Every row on this screen reports the same component id, and a padding blank as its + // accessibility description. + description: ' ', + id: 'SwitchMenuComponent', + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('Column', { + bounds: ARK_UI_BOUNDS.rowTextColumn, + text: `${title}, ${title} summary.`, + originalText: `${title}, ${title} summary.`, + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('Text', { + bounds: ARK_UI_BOUNDS.title, + text: title, + originalText: title, + hostWindowId: '179', + }), + }, + { + attributes: arkUiAttributes('Text', { + bounds: ARK_UI_BOUNDS.summary, + text: `${title} summary.`, + originalText: `${title} summary.`, + hostWindowId: '179', + }), + }, + ], + }, + { + attributes: arkUiAttributes('Toggle', { + bounds: ARK_UI_BOUNDS.toggle, + checkable: 'true', + checked: 'true', + clickable: 'true', + id: toggleId, + hostWindowId: '179', + }), + }, + ], + }; +} + +/** A settings row whose container renders no words at all, only the toggle. */ +function textlessSwitchMenuRow(toggleId: string) { + return { + attributes: arkUiAttributes('Flex', { + bounds: ARK_UI_BOUNDS.row, + clickable: 'true', + id: 'SwitchMenuComponent', + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('Toggle', { + bounds: ARK_UI_BOUNDS.toggle, + checkable: 'true', + clickable: 'true', + id: toggleId, + hostWindowId: '179', + }), + }, + ], + }; +} + +/** The navigation bar: an icon-only back `Button` beside the screen title. */ +function titleBar(screenTitle: string) { + return { + attributes: arkUiAttributes('TitleBar', { + bounds: ARK_UI_BOUNDS.titleBar, + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('HdsTitleBar', { + bounds: ARK_UI_BOUNDS.titleBar, + id: 'HdsTitleBar', + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('Button', { + bounds: ARK_UI_BOUNDS.backButton, + clickable: 'true', + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('SymbolGlyph', { + bounds: '[72,153][145,225]', + hostWindowId: '179', + }), + }, + ], + }, + { + attributes: arkUiAttributes('Text', { + bounds: ARK_UI_BOUNDS.titleText, + text: screenTitle, + originalText: screenTitle, + hostWindowId: '179', + }), + }, + ], + }, + ], + }; +} + +/** Developer options: the app window plus the merged status-bar window. */ +export function harmonyDeveloperOptionsLayout() { + return { + attributes: arkUiAttributes('unknown'), + children: [ + { + attributes: arkUiAttributes('root', { bounds: '[0,105][1084,2412]', hostWindowId: '179' }), + children: [ + { + attributes: arkUiAttributes('Navigation', { + bounds: '[0,105][1084,2412]', + hostWindowId: '179', + }), + children: [titleBar('Developer options')], + }, + { + attributes: arkUiAttributes('List', { + bounds: '[48,273][1036,2412]', + hostWindowId: '179', + }), + children: [ + { + attributes: arkUiAttributes('ListItem', { + bounds: ARK_UI_BOUNDS.row, + hostWindowId: '179', + }), + children: [switchMenuRow('USB debugging', 'entry_toggle_usb_debug')], + }, + { + attributes: arkUiAttributes('ListItem', { + bounds: ARK_UI_BOUNDS.row, + hostWindowId: '179', + }), + children: [textlessSwitchMenuRow('entry_toggle_unlabelled')], + }, + ], + }, + ], + }, + statusBarWindow(), + ], + }; +} diff --git a/packages/platform-harmonyos/src/__tests__/snapshot.test.ts b/packages/platform-harmonyos/src/__tests__/snapshot.test.ts index d35bd217fc..56d5509903 100644 --- a/packages/platform-harmonyos/src/__tests__/snapshot.test.ts +++ b/packages/platform-harmonyos/src/__tests__/snapshot.test.ts @@ -1,7 +1,10 @@ import assert from 'node:assert/strict'; import fs from 'node:fs'; import { beforeEach, test } from 'vitest'; +import { attachRefs } from '@agent-device/kernel/snapshot'; +import { resolveRefLabel } from '@agent-device/capture-kit/snapshot-node-lookup'; import { createHarmonyHdcRunnerStub, type HarmonyHdcRunnerStub } from '../toolbox.fixtures.ts'; +import { harmonyDeveloperOptionsLayout } from './arkui-settings-layout.fixture.ts'; import { collectArkUiNodes, @@ -137,3 +140,170 @@ test('snapshotHarmony pulls a uitest layout and reports its analysis', async () ], ); }); + +/** The interactive-only capture `snapshot -i` publishes, from the real-device fixture. */ +function interactiveFixtureNodes() { + const { nodes } = collectArkUiNodes(harmonyDeveloperOptionsLayout(), { + ...UNBOUNDED, + maxNodes: 5_000, + interactiveOnly: true, + }); + return attachRefs(nodes); +} + +test('collectArkUiNodes labels a control with the words its row renders', () => { + const toggle = interactiveFixtureNodes().find( + (node) => node.identifier === 'entry_toggle_usb_debug', + ); + + // ArkUI reports only the resource id on the `Toggle`; "USB debugging" is a sibling `Text` of the + // row. The aggregated screen-reader string ("USB debugging, USB debugging summary.") is not used. + assert.equal(toggle?.label, 'USB debugging'); + assert.equal(toggle?.identifier, 'entry_toggle_usb_debug'); +}); + +test('collectArkUiNodes labels an icon-only control with the title beside it', () => { + const backButton = interactiveFixtureNodes().find((node) => node.type === 'Button'); + + assert.equal(backButton?.label, 'Developer options'); + assert.equal(backButton?.identifier, undefined); +}); + +test('collectArkUiNodes leaves a control whose row renders no words unlabeled', () => { + const toggle = interactiveFixtureNodes().find( + (node) => node.identifier === 'entry_toggle_unlabelled', + ); + + assert.equal(toggle?.label, undefined); + assert.equal(toggle?.identifier, 'entry_toggle_unlabelled'); +}); + +test('collectArkUiNodes does not label a control that contains another control', () => { + // Both nodes of a row answering with the row's own words would make every selector on the screen + // ambiguous, and ArkUI names every such container `SwitchMenuComponent`. + const rows = interactiveFixtureNodes().filter((node) => node.type === 'Flex'); + + assert.equal(rows.length, 2); + assert.deepEqual( + rows.map((node) => node.label), + [undefined, undefined], + ); +}); + +test('collectArkUiNodes gives a lone control on a full-screen page no borrowed label', () => { + // A screen holding exactly one control (an icon-only FAB) makes every ancestor up to the capture + // root a "container with one control". Without a bound on the row, the FAB would borrow the first + // words of the whole document — the headline hundreds of pixels away — and a `label=` selector for + // the headline would match the button as well. + const loneControlLayout = parseHarmonyLayout( + JSON.stringify({ + attributes: { type: 'unknown', bounds: '[0,0][1084,2412]' }, + children: [ + { + attributes: { type: 'root', bounds: '[0,105][1084,2412]' }, + children: [ + { + attributes: { type: 'Stack', bounds: '[0,105][1084,2412]' }, + children: [ + { + attributes: { type: 'Column', bounds: '[0,105][1084,900]' }, + children: [ + { + attributes: { + type: 'Text', + bounds: '[48,180][640,260]', + text: 'Account & security', + }, + }, + ], + }, + { + attributes: { + type: 'Button', + bounds: '[888,2140][1036,2288]', + clickable: 'true', + }, + children: [ + { attributes: { type: 'SymbolGlyph', bounds: '[924,2176][1000,2252]' } }, + ], + }, + ], + }, + ], + }, + ], + }), + ); + + const { nodes } = collectArkUiNodes(loneControlLayout, { ...UNBOUNDED, maxNodes: 5_000 }); + + const fab = nodes.find((node) => node.type === 'Button'); + assert.notEqual(fab, undefined); + assert.equal(fab?.label, undefined); + assert.equal(fab?.hittable, true); + + // The headline names itself through `value`, and nothing on the page borrows it as a `label`, so a + // selector for those words still matches one node. + const headline = nodes.find((node) => node.value === 'Account & security'); + assert.notEqual(headline, undefined); + assert.equal(headline?.label, undefined); + assert.deepEqual( + nodes.filter((node) => node.label === 'Account & security').map((node) => node.type), + [], + ); +}); + +test('collectArkUiNodes lends a control row words through a wrapper that hugs it', () => { + // The bound must not over-trim: a FAB wrapped in a stack that is itself a row still reads the + // words of that row, even when the screen container above it would reach the whole page. + const wrappedControlLayout = parseHarmonyLayout( + JSON.stringify({ + attributes: { type: 'unknown', bounds: '[0,0][1084,2412]' }, + children: [ + { + attributes: { type: 'root', bounds: '[0,105][1084,2412]' }, + children: [ + { + attributes: { type: 'Stack', bounds: '[0,105][1084,2412]' }, + children: [ + { + attributes: { type: 'Row', bounds: '[0,2100][1084,2330]' }, + children: [ + { + attributes: { type: 'Text', bounds: '[48,2160][700,2240]', text: 'Drafts' }, + }, + { + attributes: { type: 'Stack', bounds: '[864,2120][1044,2300]' }, + children: [ + { + attributes: { + type: 'Button', + bounds: '[888,2140][1036,2288]', + clickable: 'true', + }, + }, + ], + }, + ], + }, + ], + }, + ], + }, + ], + }), + ); + + const { nodes } = collectArkUiNodes(wrappedControlLayout, { ...UNBOUNDED, maxNodes: 5_000 }); + + assert.equal(nodes.find((node) => node.type === 'Button')?.label, 'Drafts'); +}); + +test('resolveRefLabel answers a HarmonyOS ref with its own row words', () => { + const nodes = interactiveFixtureNodes(); + const backButton = nodes.find((node) => node.type === 'Button')!; + + // Without a label of its own, the nearest labelled node in the merged capture was the status-bar + // window's bridge view, 136 px above the back button (#4907). + assert.equal(resolveRefLabel(backButton, nodes), 'Developer options'); +}); diff --git a/packages/platform-harmonyos/src/snapshot.ts b/packages/platform-harmonyos/src/snapshot.ts index 3e354f369b..9105ed53d8 100644 --- a/packages/platform-harmonyos/src/snapshot.ts +++ b/packages/platform-harmonyos/src/snapshot.ts @@ -40,6 +40,156 @@ type ArkUiLayoutNode = { children?: ArkUiLayoutNode[]; }; +/** + * ArkUI keeps a control's words OUTSIDE the control. A `Toggle` answers with its resource id + * (`entry_toggle_usb_debug`) and no text; the words on the screen ("USB debugging") are a sibling + * `Text` of the row container, and every such container reports the same `id` (`SwitchMenuComponent`). + * An icon-only `Button` answers with neither. So the words a person reads have to be read off the + * control's row (#4906). + * + * The row of a control is the container around it that holds that control and no other control, and + * hugs it: the settings row of a `Toggle`, the title bar of a back `Button`. The outermost such + * container wins when several hug the control in a chain. Anything wider holds several controls, and + * its words would name all of them. Two bounded passes fold the answers for a capture in O(nodes), + * and nothing outside the row is searched — searching wider is what made an unlabeled back button + * report the label of a node elsewhere on the screen (#4907). + * + * Hugging is what stops the borrow on a screen with a single control: without a size bound, every + * ancestor of an icon-only FAB up to the capture root holds exactly one control, and the FAB reads + * the first words of the whole document — the headline far above it — as its own label. + */ +type ArkUiRowIndex = Readonly<{ + /** The first words the node's subtree renders, in document order. */ + textOfSubtree(node: ArkUiLayoutNode): string | undefined; + /** How many controls (`clickable` nodes) the node's subtree holds. */ + controlsBelow(node: ArkUiLayoutNode): number; + /** The row container the node sits in, or `undefined` when nothing bounds it. */ + rowOf(node: ArkUiLayoutNode): ArkUiLayoutNode | undefined; +}>; + +/** + * How many multiples of a control's own height a container may reach and still count as that + * control's row. A row on a device is one line beside the control: across the real captures this + * shipped against, the container around a leaf control reaches at most 3.5 times the control's + * height, while the column and screen containers a lone control sits under reach ten or more. + */ +const ARK_UI_ROW_MAX_HEIGHT_MULTIPLE = 4; + +/** Whether the container wraps the control the way a row does: it holds it and is barely taller. */ +function isArkUiRowOf(container: ArkUiLayoutNode, control: ArkUiLayoutNode): boolean { + const outer = parseArkUiBounds(stringAttribute(container.attributes ?? {}, 'bounds')); + const inner = parseArkUiBounds(stringAttribute(control.attributes ?? {}, 'bounds')); + if (outer === undefined || inner === undefined) return false; + const insideContainer = + inner.x >= outer.x && + inner.y >= outer.y && + inner.x + inner.width <= outer.x + outer.width && + inner.y + inner.height <= outer.y + outer.height; + return ( + insideContainer && outer.height <= ARK_UI_ROW_MAX_HEIGHT_MULTIPLE * Math.max(inner.height, 1) + ); +} + +function indexArkUiRows(root: ArkUiLayoutNode): ArkUiRowIndex { + const textOfSubtree = new Map(); + const controlsBelow = new Map(); + const rowOf = new Map(); + + // Words below a node beat the node's own `text`, because ArkUI aggregates a whole subtree into a + // container's `text` ("USB debugging, Enable Debug mode when USB is connected.") — what a screen + // reader says, rather than the first line shown. + const fold = (node: ArkUiLayoutNode): string | undefined => { + let textBelow: string | undefined; + let controls = 0; + for (const child of node.children ?? []) { + const childText = fold(child); + textBelow ??= childText; + controls += (controlsBelow.get(child) ?? 0) + (isClickableArkUiNode(child) ? 1 : 0); + } + const candidate = textBelow ?? arkUiBorrowableText(node.attributes ?? {}); + textOfSubtree.set(node, candidate); + controlsBelow.set(node, controls); + return candidate; + }; + fold(root); + + // The row is read from the container around the control outwards, and the climb stops at the first + // container that is not the control's row: one holding another control, or one too tall to be a + // row. Stopping on the height keeps a lone control on a full-height page without a row, so it + // borrows nothing instead of the first words of the whole document. + const assignRows = (node: ArkUiLayoutNode, ancestors: ArkUiLayoutNode[]): void => { + let row: ArkUiLayoutNode | undefined; + for (let index = ancestors.length - 1; index >= 0; index -= 1) { + const container = ancestors[index]!; + if (controlsBelow.get(container) !== 1 || !isArkUiRowOf(container, node)) break; + row = container; + } + rowOf.set(node, row); + ancestors.push(node); + for (const child of node.children ?? []) assignRows(child, ancestors); + ancestors.pop(); + }; + assignRows(root, []); + + return { + textOfSubtree: (node) => textOfSubtree.get(node), + controlsBelow: (node) => controlsBelow.get(node) ?? 0, + rowOf: (node) => rowOf.get(node), + }; +} + +/** + * The words to label an ArkUI node with, or `undefined` when it names itself. Only a control + * (`clickable`) borrows: a plain container or `Text` already reports its own words as its `value`. + * + * A control that holds another control takes no label: those words are the nested control's own label, + * and both answering one selector would make every row on the screen ambiguous. A control that borrows + * reads its own subtree first, then its row. + */ +function arkUiRowLabel(node: ArkUiLayoutNode, rows: ArkUiRowIndex): string | undefined { + if (!isClickableArkUiNode(node) || arkUiNamesItself(node)) return undefined; + if (rows.controlsBelow(node) > 0) return undefined; + const textInside = rows.textOfSubtree(node); + if (textInside !== undefined) return textInside; + const row = rows.rowOf(node); + return row === undefined ? undefined : rows.textOfSubtree(row); +} + +function isClickableArkUiNode(node: ArkUiLayoutNode): boolean { + return node.attributes?.clickable === 'true'; +} + +/** Whether ArkUI already names this node, through its own text or its accessibility description. */ +function arkUiNamesItself(node: ArkUiLayoutNode): boolean { + const attributes = node.attributes ?? {}; + return arkUiRenderedText(attributes) !== undefined || arkUiDescription(attributes) !== undefined; +} + +/** The text ArkUI renders on this node, preferring the spelling it displays. */ +function arkUiRenderedText(attributes: Record): string | undefined { + return arkUiAttributeValue(attributes, 'text') ?? arkUiAttributeValue(attributes, 'originalText'); +} + +/** + * The node's own text when it is worth labeling a neighbouring control with. ArkUI answers clock + * digits (`"17"`), battery percentages and padding blanks through the same field, and none of them + * name anything a person would match. A node's own `value` still reports them unchanged. + */ +function arkUiBorrowableText(attributes: Record): string | undefined { + const text = arkUiRenderedText(attributes); + return text === undefined || !/\p{L}/u.test(text) ? undefined : text; +} + +/** The accessibility description ArkUI renders for this node, when it renders one. */ +function arkUiDescription(attributes: Record): string | undefined { + return arkUiAttributeValue(attributes, 'description'); +} + +function arkUiAttributeValue(attributes: Record, key: string): string | undefined { + const value = stringAttribute(attributes, key)?.trim(); + return value === undefined || value.length === 0 ? undefined : value; +} + export async function snapshotHarmony( hdc: HarmonyHdcRunner, device: DeviceInfo, @@ -168,6 +318,7 @@ export function collectArkUiNodes( let rawNodeCount = 0; let maxDepth = 0; let truncated = false; + const rows = indexArkUiRows(root); const walk = (node: ArkUiLayoutNode, depth: number, parentIndex?: number): void => { rawNodeCount += 1; maxDepth = Math.max(maxDepth, depth); @@ -175,8 +326,13 @@ export function collectArkUiNodes( if (nodes.length >= policy.maxNodes) { truncated = true; } else { - const attributes = node.attributes ?? {}; - const candidate = arkUiNodeFromAttributes(attributes, nodes.length, depth, parentIndex); + const candidate = arkUiNodeFromAttributes( + node.attributes ?? {}, + nodes.length, + depth, + parentIndex, + arkUiRowLabel(node, rows), + ); const include = !policy.interactiveOnly || candidate.hittable === true; currentIndex = include ? nodes.push(candidate) - 1 : parentIndex; } @@ -193,9 +349,10 @@ function arkUiNodeFromAttributes( index: number, depth: number, parentIndex: number | undefined, + rowLabel: string | undefined, ): RawSnapshotNode { const text = stringAttribute(attributes, 'text') ?? stringAttribute(attributes, 'originalText'); - const label = stringAttribute(attributes, 'description'); + const label = arkUiDescription(attributes) ?? rowLabel; const identifier = stringAttribute(attributes, 'id') ?? stringAttribute(attributes, 'key'); const rect = parseArkUiBounds(stringAttribute(attributes, 'bounds')); return {