|
1 | 1 | import fc from 'fast-check'; |
2 | | -import { attachRefs, type Rect, type SnapshotNode } from '@agent-device/kernel/snapshot'; |
3 | 2 | import type { SelectorKey, SelectorTerm } from '../parse.ts'; |
4 | 3 |
|
5 | | -export const PROPERTY_RUNS = 100; |
6 | | - |
7 | | -// --------------------------------------------------------------------------- |
8 | | -// Rects / viewports |
9 | | -// --------------------------------------------------------------------------- |
10 | | - |
11 | | -export const scrollingContainerTypeArb = fc.constantFrom( |
12 | | - 'XCUIElementTypeScrollView', |
13 | | - 'XCUIElementTypeTable', |
14 | | - 'XCUIElementTypeCollectionView', |
15 | | - 'android.widget.ListView', |
16 | | - 'androidx.recyclerview.widget.RecyclerView', |
17 | | -); |
18 | | - |
19 | | -export const distinctRectPairArb: fc.Arbitrary<{ ancestor: Rect; target: Rect }> = fc |
20 | | - .record({ |
21 | | - x: fc.integer({ min: -200, max: 200 }), |
22 | | - y: fc.integer({ min: -200, max: 200 }), |
23 | | - width: fc.integer({ min: 1, max: 1200 }), |
24 | | - height: fc.integer({ min: 1, max: 1200 }), |
25 | | - }) |
26 | | - .chain((ancestor) => |
27 | | - fc.constantFrom<keyof Rect>('x', 'y', 'width', 'height').map((field) => ({ |
28 | | - ancestor, |
29 | | - target: { ...ancestor, [field]: ancestor[field] + 1 }, |
30 | | - })), |
31 | | - ); |
32 | | - |
33 | | -export type InteractionTouchPointScenario = { |
34 | | - nodes: SnapshotNode[]; |
35 | | - permutedNodes: SnapshotNode[]; |
36 | | - target: SnapshotNode; |
37 | | - bound: Rect; |
38 | | - competitorRects: Rect[]; |
39 | | -}; |
40 | | - |
41 | | -const halfPixel = (value: number): number => value / 2; |
42 | | -const touchPointAxisStepsArb = fc.oneof( |
43 | | - fc.integer({ min: 4, max: 46 }), |
44 | | - fc.integer({ min: 48, max: 800 }), |
45 | | -); |
46 | | - |
47 | | -const touchPointTargetRectArb = fc |
48 | | - .record({ |
49 | | - x: fc.integer({ min: -200, max: 200 }), |
50 | | - y: fc.integer({ min: -200, max: 200 }), |
51 | | - // Exercise dense desktop rows as well as standard mobile touch targets. |
52 | | - width: touchPointAxisStepsArb, |
53 | | - height: touchPointAxisStepsArb, |
54 | | - }) |
55 | | - .map(({ x, y, width, height }) => ({ |
56 | | - x: halfPixel(x), |
57 | | - y: halfPixel(y), |
58 | | - width: halfPixel(width), |
59 | | - height: halfPixel(height), |
60 | | - })); |
61 | | - |
62 | | -function containedRectArb(container: Rect): fc.Arbitrary<Rect> { |
63 | | - const widthSteps = Math.round(container.width * 2); |
64 | | - const heightSteps = Math.round(container.height * 2); |
65 | | - return fc |
66 | | - .record({ |
67 | | - width: fc.integer({ min: 2, max: widthSteps - 2 }), |
68 | | - height: fc.integer({ min: 2, max: heightSteps - 2 }), |
69 | | - }) |
70 | | - .chain(({ width, height }) => |
71 | | - fc |
72 | | - .record({ |
73 | | - x: fc.integer({ min: 0, max: widthSteps - width }), |
74 | | - y: fc.integer({ min: 0, max: heightSteps - height }), |
75 | | - }) |
76 | | - .map(({ x, y }) => ({ |
77 | | - x: container.x + halfPixel(x), |
78 | | - y: container.y + halfPixel(y), |
79 | | - width: halfPixel(width), |
80 | | - height: halfPixel(height), |
81 | | - })), |
82 | | - ); |
83 | | -} |
84 | | - |
85 | | -export const interactionTouchPointScenarioArb: fc.Arbitrary<InteractionTouchPointScenario> = |
86 | | - touchPointTargetRectArb.chain((targetRect) => |
87 | | - fc |
88 | | - .tuple( |
89 | | - fc.array(containedRectArb(targetRect), { minLength: 1, maxLength: 6 }), |
90 | | - containedRectArb(targetRect), |
91 | | - ) |
92 | | - .chain(([competitorRects, bound]) => { |
93 | | - const nodes = attachRefs([ |
94 | | - { |
95 | | - index: 0, |
96 | | - depth: 0, |
97 | | - type: 'Link', |
98 | | - label: 'Generated parent', |
99 | | - rect: targetRect, |
100 | | - hittable: true, |
101 | | - }, |
102 | | - ...competitorRects.map((rect, offset) => ({ |
103 | | - index: offset + 1, |
104 | | - depth: 1, |
105 | | - parentIndex: 0, |
106 | | - type: 'Button', |
107 | | - label: `Generated child ${offset + 1}`, |
108 | | - rect, |
109 | | - hittable: true, |
110 | | - })), |
111 | | - ]); |
112 | | - return fc |
113 | | - .shuffledSubarray(nodes, { minLength: nodes.length, maxLength: nodes.length }) |
114 | | - .map((permutedNodes) => ({ |
115 | | - nodes, |
116 | | - permutedNodes, |
117 | | - target: nodes[0]!, |
118 | | - bound, |
119 | | - competitorRects, |
120 | | - })); |
121 | | - }), |
122 | | - ); |
123 | | - |
124 | 4 | const SELECTOR_KEY_VALUE_KINDS = { |
125 | 5 | id: 'text', |
126 | 6 | role: 'text', |
|
0 commit comments