|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { buildUiHierarchySnapshot, parseUiHierarchyTree } from '../ui-hierarchy.ts'; |
| 3 | + |
| 4 | +// The bottom-tab shape from #2462: sibling tabs whose type, label, and identifier are all |
| 5 | +// present and differ only in which one the app marked selected. |
| 6 | +function tabBarXml(homeSelected: boolean, localSelected: boolean): string { |
| 7 | + return `<hierarchy> |
| 8 | + <node class="android.widget.FrameLayout" resource-id="tab-bar" bounds="[0,724][400,800]" |
| 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.view.View" resource-id="home-tab" content-desc="Home" |
| 12 | + bounds="[0,724][200,800]" clickable="true" enabled="true" visible-to-user="true" |
| 13 | + selected="${homeSelected}" /> |
| 14 | + <node class="android.view.View" resource-id="local-tab" content-desc="Local" |
| 15 | + bounds="[200,724][400,800]" clickable="true" enabled="true" visible-to-user="true" |
| 16 | + selected="${localSelected}" /> |
| 17 | + </node> |
| 18 | + </hierarchy>`; |
| 19 | +} |
| 20 | + |
| 21 | +// A helper older than the `selected` attribute, or any producer that omits it. |
| 22 | +const UNREPORTED_SELECTION_XML = |
| 23 | + '<hierarchy><node class="android.view.View" resource-id="legacy-tab" content-desc="Home"' + |
| 24 | + ' bounds="[0,724][200,800]" clickable="true" enabled="true" visible-to-user="true" /></hierarchy>'; |
| 25 | + |
| 26 | +function tabNodes(raw: boolean, interactiveOnly = false) { |
| 27 | + const { nodes } = buildUiHierarchySnapshot( |
| 28 | + parseUiHierarchyTree(tabBarXml(true, false)), |
| 29 | + undefined, |
| 30 | + { raw, interactiveOnly }, |
| 31 | + ); |
| 32 | + return { |
| 33 | + home: nodes.find((node) => node.identifier === 'home-tab'), |
| 34 | + local: nodes.find((node) => node.identifier === 'local-tab'), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function unreportedSelectionNode() { |
| 39 | + return buildUiHierarchySnapshot(parseUiHierarchyTree(UNREPORTED_SELECTION_XML), undefined, { |
| 40 | + raw: false, |
| 41 | + }).nodes.find((node) => node.identifier === 'legacy-tab'); |
| 42 | +} |
| 43 | + |
| 44 | +test.each([ |
| 45 | + { raw: false, interactiveOnly: false }, |
| 46 | + { raw: false, interactiveOnly: true }, |
| 47 | + { raw: true, interactiveOnly: false }, |
| 48 | + { raw: true, interactiveOnly: true }, |
| 49 | +])( |
| 50 | + 'accessibility selection reaches snapshot nodes in every projection (raw=$raw, -i=$interactiveOnly)', |
| 51 | + ({ raw, interactiveOnly }) => { |
| 52 | + const { home, local } = tabNodes(raw, interactiveOnly); |
| 53 | + expect(home?.selected).toBe(true); |
| 54 | + expect(local?.selected).toBe(false); |
| 55 | + }, |
| 56 | +); |
| 57 | + |
| 58 | +test('attrs answer explicit false where an unreported selection answers nothing', () => { |
| 59 | + // Serialized, because that is the answer an agent reads: an unavailable fact drops the key |
| 60 | + // while JSON encodes an observed `false`. |
| 61 | + const serializedLocal = JSON.parse(JSON.stringify(tabNodes(false).local)); |
| 62 | + const serializedLegacy = JSON.parse(JSON.stringify(unreportedSelectionNode())); |
| 63 | + expect(serializedLocal.selected).toBe(false); |
| 64 | + expect(serializedLegacy).not.toHaveProperty('selected'); |
| 65 | +}); |
| 66 | + |
| 67 | +test.each([ |
| 68 | + { homeSelected: true, localSelected: false, selectedTabs: ['home-tab'] }, |
| 69 | + { homeSelected: false, localSelected: true, selectedTabs: ['local-tab'] }, |
| 70 | +])( |
| 71 | + 'selection, not the label, is what names the active tab ($selectedTabs)', |
| 72 | + ({ homeSelected, localSelected, selectedTabs }) => { |
| 73 | + const { nodes } = buildUiHierarchySnapshot( |
| 74 | + parseUiHierarchyTree(tabBarXml(homeSelected, localSelected)), |
| 75 | + undefined, |
| 76 | + { raw: false }, |
| 77 | + ); |
| 78 | + // Both tabs stay addressable by label, so selection is the only thing that can tell them apart. |
| 79 | + expect(nodes.filter((node) => node.selected === true).map((node) => node.identifier)).toEqual( |
| 80 | + selectedTabs, |
| 81 | + ); |
| 82 | + expect(nodes.filter((node) => node.label === 'Home' || node.label === 'Local')).toHaveLength(2); |
| 83 | + }, |
| 84 | +); |
| 85 | + |
| 86 | +test('unreported selection stays unknown instead of becoming false', () => { |
| 87 | + const node = unreportedSelectionNode(); |
| 88 | + expect(node).toBeDefined(); |
| 89 | + expect(node?.selected).toBeUndefined(); |
| 90 | +}); |
0 commit comments