|
| 1 | +import type { Rect } from '@agent-device/kernel/snapshot'; |
| 2 | +import fc from 'fast-check'; |
| 3 | +import { expect, test } from 'vitest'; |
| 4 | +import { PROPERTY_RUNS } from '../../../__tests__/test-utils/property-arbitraries.ts'; |
| 5 | +import { |
| 6 | + createAndroidSnapshotPresentationNode, |
| 7 | + effectiveAndroidRect, |
| 8 | + serializeAndroidRegularPresentationNode, |
| 9 | +} from '../snapshot-presentation.ts'; |
| 10 | +import { parseUiHierarchy } from './ui-hierarchy-fixtures.ts'; |
| 11 | + |
| 12 | +const rectArb = fc.record({ |
| 13 | + x: fc.integer({ min: -500, max: 500 }), |
| 14 | + y: fc.integer({ min: -500, max: 500 }), |
| 15 | + width: fc.integer({ min: -20, max: 500 }), |
| 16 | + height: fc.integer({ min: -20, max: 500 }), |
| 17 | +}); |
| 18 | + |
| 19 | +const positiveRectArb = fc.record({ |
| 20 | + x: fc.integer({ min: -500, max: 500 }), |
| 21 | + y: fc.integer({ min: -500, max: 500 }), |
| 22 | + width: fc.integer({ min: 1, max: 500 }), |
| 23 | + height: fc.integer({ min: 1, max: 500 }), |
| 24 | +}); |
| 25 | + |
| 26 | +test('regular Android snapshots publish cumulative effective geometry while raw keeps reported bounds', () => { |
| 27 | + const xml = `<hierarchy> |
| 28 | + <node class="android.widget.FrameLayout" bounds="[0,0][400,800]" window-bounds="[0,0][400,800]" visible-to-user="true"> |
| 29 | + <node class="android.widget.ScrollView" bounds="[0,100][300,500]" scrollable="true" visible-to-user="true"> |
| 30 | + <node class="android.widget.ScrollView" bounds="[50,150][250,350]" scrollable="true" visible-to-user="true"> |
| 31 | + <node class="android.widget.Button" text="Partially visible" bounds="[200,300][320,380]" clickable="true" visible-to-user="true" /> |
| 32 | + <node class="android.widget.Button" text="Outside nested scroll" bounds="[260,360][380,420]" clickable="true" visible-to-user="true" /> |
| 33 | + </node> |
| 34 | + </node> |
| 35 | + </node> |
| 36 | +</hierarchy>`; |
| 37 | + |
| 38 | + const regular = parseUiHierarchy(xml, 800, {}); |
| 39 | + const raw = parseUiHierarchy(xml, 800, { raw: true }); |
| 40 | + |
| 41 | + expect(regular.nodes.find((node) => node.label === 'Partially visible')).toMatchObject({ |
| 42 | + rect: { x: 200, y: 300, width: 50, height: 50 }, |
| 43 | + hittable: true, |
| 44 | + }); |
| 45 | + expect(regular.nodes.find((node) => node.label === 'Outside nested scroll')).toMatchObject({ |
| 46 | + rect: { x: 260, y: 360, width: 0, height: 0 }, |
| 47 | + hittable: undefined, |
| 48 | + }); |
| 49 | + expect(raw.nodes.find((node) => node.label === 'Partially visible')).toMatchObject({ |
| 50 | + rect: { x: 200, y: 300, width: 120, height: 80 }, |
| 51 | + hittable: true, |
| 52 | + }); |
| 53 | + expect(raw.nodes.find((node) => node.label === 'Outside nested scroll')).toMatchObject({ |
| 54 | + rect: { x: 260, y: 360, width: 120, height: 60 }, |
| 55 | + hittable: true, |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +test('regular Android snapshots clip descendants to their owning window', () => { |
| 60 | + const xml = `<hierarchy> |
| 61 | + <node class="android.widget.FrameLayout" bounds="[0,0][400,800]" window-bounds="[0,0][400,800]" window-index="0" window-type="1" window-layer="1" window-active="true" window-focused="true" visible-to-user="true"> |
| 62 | + <node class="android.widget.Button" text="Main action" bounds="[20,20][180,80]" clickable="true" visible-to-user="true" /> |
| 63 | + </node> |
| 64 | + <node class="android.widget.FrameLayout" bounds="[100,100][300,300]" window-bounds="[100,100][300,300]" window-index="1" window-type="2" window-layer="2" window-active="true" window-focused="true" visible-to-user="true"> |
| 65 | + <node class="android.widget.Button" text="Outside dialog" bounds="[120,320][180,380]" clickable="true" visible-to-user="true" /> |
| 66 | + </node> |
| 67 | +</hierarchy>`; |
| 68 | + |
| 69 | + const regular = parseUiHierarchy(xml, 800, {}); |
| 70 | + const dialogAction = regular.nodes.find((node) => node.label === 'Outside dialog'); |
| 71 | + |
| 72 | + expect(dialogAction).toMatchObject({ |
| 73 | + rect: { x: 120, y: 320, width: 0, height: 0 }, |
| 74 | + hittable: undefined, |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +test('property: effective Android geometry stays inside every positive clip and never upgrades actionability', () => { |
| 79 | + fc.assert( |
| 80 | + fc.property(rectArb, positiveRectArb, positiveRectArb, (reported, viewport, ancestorClip) => { |
| 81 | + const effective = effectiveAndroidRect(reported, viewport, ancestorClip); |
| 82 | + const raw = { index: 0, depth: 0, rect: reported, hittable: true as const }; |
| 83 | + const carrier = createAndroidSnapshotPresentationNode(raw, effective); |
| 84 | + const regular = serializeAndroidRegularPresentationNode(carrier); |
| 85 | + |
| 86 | + expect(carrier.raw.rect).toEqual(reported); |
| 87 | + expect(regular.rect).toEqual(effective); |
| 88 | + if (effective && effective.width > 0 && effective.height > 0) { |
| 89 | + expect(rectContains(viewport, effective)).toBe(true); |
| 90 | + expect(rectContains(ancestorClip, effective)).toBe(true); |
| 91 | + expect(regular.hittable).toBe(true); |
| 92 | + } else { |
| 93 | + expect(regular.hittable).toBeUndefined(); |
| 94 | + } |
| 95 | + }), |
| 96 | + { numRuns: PROPERTY_RUNS }, |
| 97 | + ); |
| 98 | +}); |
| 99 | + |
| 100 | +function rectContains(outer: Rect, inner: Rect): boolean { |
| 101 | + return ( |
| 102 | + inner.x >= outer.x && |
| 103 | + inner.y >= outer.y && |
| 104 | + inner.x + inner.width <= outer.x + Math.max(0, outer.width) && |
| 105 | + inner.y + inner.height <= outer.y + Math.max(0, outer.height) |
| 106 | + ); |
| 107 | +} |
0 commit comments