|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { buildUiHierarchySnapshot, parseUiHierarchyTree } from '../ui-hierarchy.ts'; |
| 3 | + |
| 4 | +// A settings row: a switch the helper reports as checkable beside a label that is not. The helper |
| 5 | +// writes `checked` only on the checkable node, with both answers. |
| 6 | +function togglesXml(wifiChecked: boolean): string { |
| 7 | + return `<hierarchy> |
| 8 | + <node class="android.widget.LinearLayout" resource-id="toggles" bounds="[0,0][400,200]" |
| 9 | + window-index="0" window-type="1" window-layer="1" window-active="true" window-focused="true" |
| 10 | + window-bounds="[0,0][400,800]" visible-to-user="true" enabled="true"> |
| 11 | + <node class="android.widget.TextView" resource-id="wifi-label" text="Wi-Fi" |
| 12 | + bounds="[0,0][200,100]" enabled="true" visible-to-user="true" /> |
| 13 | + <node class="android.widget.Switch" resource-id="wifi-switch" content-desc="Wi-Fi switch" |
| 14 | + bounds="[200,0][400,100]" clickable="true" enabled="true" visible-to-user="true" |
| 15 | + checked="${wifiChecked}" /> |
| 16 | + <node class="android.widget.RadioButton" resource-id="size-small" text="Small" |
| 17 | + bounds="[0,100][400,200]" clickable="true" enabled="true" visible-to-user="true" |
| 18 | + checked="false" /> |
| 19 | + </node> |
| 20 | + </hierarchy>`; |
| 21 | +} |
| 22 | + |
| 23 | +// A helper older than the `checked` attribute, or a switch it did not report as checkable. |
| 24 | +const UNREPORTED_CHECKED_XML = |
| 25 | + '<hierarchy><node class="android.widget.Switch" resource-id="legacy-switch" content-desc="Wi-Fi"' + |
| 26 | + ' bounds="[200,0][400,100]" clickable="true" enabled="true" visible-to-user="true" /></hierarchy>'; |
| 27 | + |
| 28 | +function toggleNodes(raw: boolean, interactiveOnly = false, wifiChecked = true) { |
| 29 | + const { nodes } = buildUiHierarchySnapshot( |
| 30 | + parseUiHierarchyTree(togglesXml(wifiChecked)), |
| 31 | + undefined, |
| 32 | + { raw, interactiveOnly }, |
| 33 | + ); |
| 34 | + const byId = (identifier: string) => nodes.find((node) => node.identifier === identifier); |
| 35 | + return { label: byId('wifi-label'), wifi: byId('wifi-switch'), small: byId('size-small') }; |
| 36 | +} |
| 37 | + |
| 38 | +test.each([ |
| 39 | + { raw: false, interactiveOnly: false }, |
| 40 | + { raw: false, interactiveOnly: true }, |
| 41 | + { raw: true, interactiveOnly: false }, |
| 42 | + { raw: true, interactiveOnly: true }, |
| 43 | +])( |
| 44 | + 'checked state reaches snapshot nodes in every projection (raw=$raw, -i=$interactiveOnly)', |
| 45 | + ({ raw, interactiveOnly }) => { |
| 46 | + const { wifi, small } = toggleNodes(raw, interactiveOnly); |
| 47 | + expect(wifi?.checked).toBe(true); |
| 48 | + expect(small?.checked).toBe(false); |
| 49 | + }, |
| 50 | +); |
| 51 | + |
| 52 | +test.each([true, false])('the switch answers the state the helper observed (%s)', (wifiChecked) => { |
| 53 | + expect(toggleNodes(false, false, wifiChecked).wifi?.checked).toBe(wifiChecked); |
| 54 | +}); |
| 55 | + |
| 56 | +test('attrs answer explicit false where a node that cannot be checked answers nothing', () => { |
| 57 | + // Serialized, because that is the answer an agent reads: an unavailable fact drops the key |
| 58 | + // while JSON encodes an observed `false`. |
| 59 | + const { label, small } = toggleNodes(false); |
| 60 | + expect(JSON.parse(JSON.stringify(small)).checked).toBe(false); |
| 61 | + expect(JSON.parse(JSON.stringify(label))).not.toHaveProperty('checked'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('an unreported checked state stays unknown instead of becoming false', () => { |
| 65 | + const node = buildUiHierarchySnapshot(parseUiHierarchyTree(UNREPORTED_CHECKED_XML), undefined, { |
| 66 | + raw: false, |
| 67 | + }).nodes.find((node) => node.identifier === 'legacy-switch'); |
| 68 | + expect(node).toBeDefined(); |
| 69 | + expect(node?.checked).toBeUndefined(); |
| 70 | +}); |
0 commit comments