|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { buildUiHierarchySnapshot, parseUiHierarchyTree } from '../ui-hierarchy.ts'; |
| 3 | + |
| 4 | +// A React Native screen: `accessibilityRole="header"` is a plain View the helper flags as a heading, |
| 5 | +// a tab bar is a View with the role description the app set, and a label carries neither. |
| 6 | +const ROLE_FACTS_XML = `<hierarchy> |
| 7 | + <node class="android.widget.FrameLayout" resource-id="root" bounds="[0,0][400,800]" |
| 8 | + window-index="0" window-type="1" window-layer="1" window-active="true" window-focused="true" |
| 9 | + window-bounds="[0,0][400,800]" visible-to-user="true" enabled="true"> |
| 10 | + <node class="android.view.View" resource-id="inventory-header" text="Inventory" heading="true" |
| 11 | + bounds="[0,0][400,60]" enabled="true" visible-to-user="true" /> |
| 12 | + <node class="android.view.View" resource-id="section-tabs" role-description="tab list" |
| 13 | + bounds="[0,60][400,120]" enabled="true" visible-to-user="true"> |
| 14 | + <node class="android.view.View" resource-id="tab-fields" text="Fields" role-description="tab" |
| 15 | + bounds="[0,60][200,120]" clickable="true" enabled="true" visible-to-user="true" /> |
| 16 | + </node> |
| 17 | + <node class="android.widget.TextView" resource-id="plain-label" text="Wi-Fi" |
| 18 | + bounds="[0,120][400,180]" enabled="true" visible-to-user="true" /> |
| 19 | + </node> |
| 20 | +</hierarchy>`; |
| 21 | + |
| 22 | +function nodesById(raw: boolean, interactiveOnly = false) { |
| 23 | + const { nodes } = buildUiHierarchySnapshot(parseUiHierarchyTree(ROLE_FACTS_XML), undefined, { |
| 24 | + raw, |
| 25 | + interactiveOnly, |
| 26 | + }); |
| 27 | + return (identifier: string) => nodes.find((node) => node.identifier === identifier); |
| 28 | +} |
| 29 | + |
| 30 | +test.each([ |
| 31 | + { raw: false, interactiveOnly: false }, |
| 32 | + { raw: true, interactiveOnly: false }, |
| 33 | + { raw: true, interactiveOnly: true }, |
| 34 | +])( |
| 35 | + 'the heading flag and the role description reach snapshot nodes (raw=$raw, -i=$interactiveOnly)', |
| 36 | + ({ raw, interactiveOnly }) => { |
| 37 | + const byId = nodesById(raw, interactiveOnly); |
| 38 | + expect(byId('inventory-header')?.heading).toBe(true); |
| 39 | + expect(byId('section-tabs')?.roleDescription).toBe('tab list'); |
| 40 | + expect(byId('tab-fields')?.roleDescription).toBe('tab'); |
| 41 | + }, |
| 42 | +); |
| 43 | + |
| 44 | +test('a node without either fact carries neither key once serialized', () => { |
| 45 | + const byId = nodesById(false); |
| 46 | + const label = JSON.parse(JSON.stringify(byId('plain-label'))); |
| 47 | + expect(label).not.toHaveProperty('heading'); |
| 48 | + expect(label).not.toHaveProperty('roleDescription'); |
| 49 | + // A heading is not a role description and a role description is not a heading. |
| 50 | + expect(JSON.parse(JSON.stringify(byId('inventory-header')))).not.toHaveProperty( |
| 51 | + 'roleDescription', |
| 52 | + ); |
| 53 | + expect(JSON.parse(JSON.stringify(byId('tab-fields')))).not.toHaveProperty('heading'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('the class stays the type: a role description refines nothing on its own', () => { |
| 57 | + const byId = nodesById(false); |
| 58 | + expect(byId('tab-fields')?.type).toBe('android.view.View'); |
| 59 | + expect(byId('inventory-header')?.label).toBe('Inventory'); |
| 60 | +}); |
0 commit comments