fret-diag-protocol 0.1.0

Diagnostics protocol types for Fret (bundles, events, and capture metadata).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Typed helpers for building UI diagnostics scripts in Rust.
//!
//! Design goals:
//! - Keep `tools/diag-scripts/*.json` as the portable, reviewable artifact.
//! - Provide a small, type-safe API for generating scripts (e.g. via a scriptgen tool).
//! - Prefer stable selectors (`test_id`, semantics role/name) over pixel coordinates.

use crate::{
    UiActionScriptV2, UiActionStepV2, UiCommandDispatchTraceQueryV1, UiImeEventV1,
    UiIncomingOpenInjectItemV1, UiKeyModifiersV1, UiMouseButtonV1, UiOverlayPlacementTraceQueryV1,
    UiPointerKindV1, UiPredicateV1, UiSelectorV1, UiShortcutRoutingTraceQueryV1, UiWindowTargetV1,
};

pub fn test_id(id: impl Into<String>) -> UiSelectorV1 {
    UiSelectorV1::TestId {
        id: id.into(),
        root_z_index: None,
    }
}

pub fn role_and_name(role: impl Into<String>, name: impl Into<String>) -> UiSelectorV1 {
    UiSelectorV1::RoleAndName {
        role: role.into(),
        name: name.into(),
        root_z_index: None,
    }
}

pub fn exists(target: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::Exists { target }
}

pub fn not_exists(target: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::NotExists { target }
}

pub fn exists_under(scope: UiSelectorV1, target: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::ExistsUnder { scope, target }
}

pub fn not_exists_under(scope: UiSelectorV1, target: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::NotExistsUnder { scope, target }
}

pub fn focus_is(target: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::FocusIs { target }
}

pub fn focused_descendant_is(scope: UiSelectorV1, target: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::FocusedDescendantIs { scope, target }
}

pub fn active_item_is(container: UiSelectorV1, item: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::ActiveItemIs { container, item }
}

pub fn active_item_is_none(container: UiSelectorV1) -> UiPredicateV1 {
    UiPredicateV1::ActiveItemIsNone { container }
}

pub fn selected_is(target: UiSelectorV1, selected: bool) -> UiPredicateV1 {
    UiPredicateV1::SelectedIs { target, selected }
}

pub fn text_composition_is(target: UiSelectorV1, composing: bool) -> UiPredicateV1 {
    UiPredicateV1::TextCompositionIs { target, composing }
}

pub fn ime_cursor_area_is_some(is_some: bool) -> UiPredicateV1 {
    UiPredicateV1::ImeCursorAreaIsSome { is_some }
}

pub fn ime_cursor_area_within_window(padding_px: f32, eps_px: f32) -> UiPredicateV1 {
    UiPredicateV1::ImeCursorAreaWithinWindow {
        padding_px,
        padding_insets_px: None,
        eps_px,
    }
}

pub fn ime_cursor_area_min_size(min_w_px: f32, min_h_px: f32, eps_px: f32) -> UiPredicateV1 {
    UiPredicateV1::ImeCursorAreaMinSize {
        min_w_px,
        min_h_px,
        eps_px,
    }
}

pub fn runner_accessibility_activated() -> UiPredicateV1 {
    UiPredicateV1::RunnerAccessibilityActivated
}

pub fn app_snapshot_field_equals(
    pointer: impl Into<String>,
    value: serde_json::Value,
) -> UiPredicateV1 {
    UiPredicateV1::AppSnapshotFieldEquals {
        pointer: pointer.into(),
        value,
    }
}

pub fn value_equals(target: UiSelectorV1, text: impl Into<String>) -> UiPredicateV1 {
    UiPredicateV1::ValueEquals {
        target,
        text: text.into(),
    }
}

#[derive(Debug, Default, Clone)]
pub struct ScriptV2Builder {
    steps: Vec<UiActionStepV2>,
}

impl ScriptV2Builder {
    pub fn new() -> Self {
        Self { steps: Vec::new() }
    }

    pub fn push(mut self, step: UiActionStepV2) -> Self {
        self.steps.push(step);
        self
    }

    pub fn extend(mut self, steps: impl IntoIterator<Item = UiActionStepV2>) -> Self {
        self.steps.extend(steps);
        self
    }

    pub fn reset_diagnostics(self) -> Self {
        self.push(UiActionStepV2::ResetDiagnostics)
    }

    pub fn set_base_ref(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::SetBaseRef {
            window: None,
            target,
        })
    }

    pub fn set_base_ref_in_window(self, window: UiWindowTargetV1, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::SetBaseRef {
            window: Some(window),
            target,
        })
    }

    pub fn clear_base_ref(self) -> Self {
        self.push(UiActionStepV2::ClearBaseRef)
    }

    pub fn click(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Click {
            window: None,
            pointer_kind: None,
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: None,
        })
    }

    pub fn click_touch(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Click {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Touch),
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: None,
        })
    }

    pub fn click_pen(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Click {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Pen),
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: None,
        })
    }

    pub fn tap(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Tap {
            window: None,
            pointer_kind: None,
            target,
            modifiers: None,
        })
    }

    pub fn tap_pen(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Tap {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Pen),
            target,
            modifiers: None,
        })
    }

    pub fn long_press(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::LongPress {
            window: None,
            pointer_kind: None,
            target,
            duration_ms: 500,
            modifiers: None,
        })
    }

    pub fn long_press_pen(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::LongPress {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Pen),
            target,
            duration_ms: 500,
            modifiers: None,
        })
    }

    pub fn swipe(self, target: UiSelectorV1, delta_x: f32, delta_y: f32) -> Self {
        self.push(UiActionStepV2::Swipe {
            window: None,
            pointer_kind: None,
            target,
            delta_x,
            delta_y,
            steps: 8,
            modifiers: None,
        })
    }

    pub fn pinch(self, target: UiSelectorV1, delta: f32) -> Self {
        self.push(UiActionStepV2::Pinch {
            window: None,
            pointer_kind: None,
            target,
            delta,
            steps: 8,
            modifiers: None,
        })
    }

    pub fn click_with_modifiers(self, target: UiSelectorV1, modifiers: UiKeyModifiersV1) -> Self {
        self.push(UiActionStepV2::Click {
            window: None,
            pointer_kind: None,
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: Some(modifiers),
        })
    }

    pub fn pointer_down(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::PointerDown {
            window: None,
            pointer_kind: None,
            target,
            button: UiMouseButtonV1::Left,
            modifiers: None,
        })
    }

    pub fn pointer_down_touch(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::PointerDown {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Touch),
            target,
            button: UiMouseButtonV1::Left,
            modifiers: None,
        })
    }

    pub fn pointer_down_pen(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::PointerDown {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Pen),
            target,
            button: UiMouseButtonV1::Left,
            modifiers: None,
        })
    }

    pub fn pointer_down_with_modifiers(
        self,
        target: UiSelectorV1,
        modifiers: UiKeyModifiersV1,
    ) -> Self {
        self.push(UiActionStepV2::PointerDown {
            window: None,
            pointer_kind: None,
            target,
            button: UiMouseButtonV1::Left,
            modifiers: Some(modifiers),
        })
    }

    pub fn pointer_move(self, delta_x: f32, delta_y: f32) -> Self {
        self.push(UiActionStepV2::PointerMove {
            window: None,
            pointer_kind: None,
            delta_x,
            delta_y,
            steps: 8,
        })
    }

    pub fn pointer_up(self) -> Self {
        self.push(UiActionStepV2::PointerUp {
            window: None,
            pointer_kind: None,
            button: None,
        })
    }

    pub fn pointer_cancel(self) -> Self {
        self.push(UiActionStepV2::PointerCancel {
            window: None,
            pointer_kind: None,
        })
    }

    pub fn activate(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Activate {
            window: None,
            target,
        })
    }

    pub fn focus(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::Focus {
            window: None,
            target,
        })
    }

    pub fn click_stable(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::ClickStable {
            window: None,
            pointer_kind: None,
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: None,
            stable_frames: 2,
            max_move_px: 1.0,
            timeout_frames: 180,
        })
    }

    pub fn click_stable_touch(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::ClickStable {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Touch),
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: None,
            stable_frames: 2,
            max_move_px: 1.0,
            timeout_frames: 180,
        })
    }

    pub fn click_selectable_text_span_stable(
        self,
        target: UiSelectorV1,
        tag: impl Into<String>,
    ) -> Self {
        self.push(UiActionStepV2::ClickSelectableTextSpanStable {
            window: None,
            pointer_kind: None,
            target,
            tag: tag.into(),
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: None,
            stable_frames: 2,
            max_move_px: 1.0,
            timeout_frames: 180,
        })
    }

    pub fn click_stable_with_modifiers(
        self,
        target: UiSelectorV1,
        modifiers: UiKeyModifiersV1,
    ) -> Self {
        self.push(UiActionStepV2::ClickStable {
            window: None,
            pointer_kind: None,
            target,
            button: UiMouseButtonV1::Left,
            click_count: 1,
            modifiers: Some(modifiers),
            stable_frames: 2,
            max_move_px: 1.0,
            timeout_frames: 180,
        })
    }

    pub fn wait_bounds_stable(self, target: UiSelectorV1) -> Self {
        self.push(UiActionStepV2::WaitBoundsStable {
            window: None,
            target,
            stable_frames: 2,
            max_move_px: 1.0,
            timeout_frames: 180,
        })
    }

    pub fn press_key(self, key: impl Into<String>) -> Self {
        self.push(UiActionStepV2::PressKey {
            key: key.into(),
            modifiers: UiKeyModifiersV1::default(),
            repeat: false,
        })
    }

    pub fn press_shortcut(self, shortcut: impl Into<String>) -> Self {
        self.push(UiActionStepV2::PressShortcut {
            shortcut: shortcut.into(),
            repeat: false,
        })
    }

    pub fn type_text(self, text: impl Into<String>) -> Self {
        self.push(UiActionStepV2::TypeText { text: text.into() })
    }

    pub fn ime_enabled(self) -> Self {
        self.push(UiActionStepV2::Ime {
            event: UiImeEventV1::Enabled,
        })
    }

    pub fn ime_disabled(self) -> Self {
        self.push(UiActionStepV2::Ime {
            event: UiImeEventV1::Disabled,
        })
    }

    pub fn ime_preedit(self, text: impl Into<String>, cursor_bytes: Option<(u32, u32)>) -> Self {
        self.push(UiActionStepV2::Ime {
            event: UiImeEventV1::Preedit {
                text: text.into(),
                cursor_bytes,
            },
        })
    }

    pub fn ime_commit(self, text: impl Into<String>) -> Self {
        self.push(UiActionStepV2::Ime {
            event: UiImeEventV1::Commit { text: text.into() },
        })
    }

    pub fn ime_delete_surrounding(self, before_bytes: u32, after_bytes: u32) -> Self {
        self.push(UiActionStepV2::Ime {
            event: UiImeEventV1::DeleteSurrounding {
                before_bytes,
                after_bytes,
            },
        })
    }

    pub fn type_text_into(self, target: UiSelectorV1, text: impl Into<String>) -> Self {
        self.push(UiActionStepV2::TypeTextInto {
            window: None,
            pointer_kind: None,
            target,
            text: text.into(),
            clear_before_type: false,
            timeout_frames: 180,
        })
    }

    pub fn type_text_into_touch(self, target: UiSelectorV1, text: impl Into<String>) -> Self {
        self.push(UiActionStepV2::TypeTextInto {
            window: None,
            pointer_kind: Some(UiPointerKindV1::Touch),
            target,
            text: text.into(),
            clear_before_type: false,
            timeout_frames: 180,
        })
    }

    pub fn set_text_value(self, target: UiSelectorV1, text: impl Into<String>) -> Self {
        self.push(UiActionStepV2::SetTextValue {
            window: None,
            target,
            text: text.into(),
            timeout_frames: 180,
        })
    }

    pub fn wait_frames(self, n: u32) -> Self {
        self.push(UiActionStepV2::WaitFrames { window: None, n })
    }

    pub fn wait_ms(self, n_ms: u32) -> Self {
        self.push(UiActionStepV2::WaitMs { window: None, n_ms })
    }

    pub fn wait_until(self, predicate: UiPredicateV1, timeout_frames: u32) -> Self {
        self.push(UiActionStepV2::WaitUntil {
            window: None,
            predicate,
            timeout_frames,
            timeout_ms: None,
        })
    }

    pub fn wait_shortcut_routing_trace(
        self,
        query: UiShortcutRoutingTraceQueryV1,
        timeout_frames: u32,
    ) -> Self {
        self.push(UiActionStepV2::WaitShortcutRoutingTrace {
            query,
            timeout_frames,
            timeout_ms: None,
        })
    }

    pub fn wait_command_dispatch_trace(
        self,
        query: UiCommandDispatchTraceQueryV1,
        timeout_frames: u32,
    ) -> Self {
        self.push(UiActionStepV2::WaitCommandDispatchTrace {
            query,
            timeout_frames,
            timeout_ms: None,
        })
    }

    pub fn wait_overlay_placement_trace(
        self,
        query: UiOverlayPlacementTraceQueryV1,
        timeout_frames: u32,
    ) -> Self {
        self.push(UiActionStepV2::WaitOverlayPlacementTrace {
            query,
            timeout_frames,
            timeout_ms: None,
        })
    }

    pub fn wait_exists(self, target: UiSelectorV1, timeout_frames: u32) -> Self {
        self.wait_until(exists(target), timeout_frames)
    }

    pub fn wait_not_exists(self, target: UiSelectorV1, timeout_frames: u32) -> Self {
        self.wait_until(not_exists(target), timeout_frames)
    }

    pub fn assert(self, predicate: UiPredicateV1) -> Self {
        self.push(UiActionStepV2::Assert {
            window: None,
            predicate,
        })
    }

    pub fn assert_exists(self, target: UiSelectorV1) -> Self {
        self.assert(exists(target))
    }

    pub fn assert_not_exists(self, target: UiSelectorV1) -> Self {
        self.assert(not_exists(target))
    }

    pub fn assert_focus_is(self, target: UiSelectorV1) -> Self {
        self.assert(focus_is(target))
    }

    pub fn capture_bundle(self, label: impl Into<Option<String>>) -> Self {
        self.push(UiActionStepV2::CaptureBundle {
            label: label.into(),
            max_snapshots: None,
        })
    }

    pub fn capture_bundle_with_max_snapshots(
        self,
        label: impl Into<Option<String>>,
        max_snapshots: u32,
    ) -> Self {
        self.push(UiActionStepV2::CaptureBundle {
            label: label.into(),
            max_snapshots: Some(max_snapshots),
        })
    }

    pub fn capture_screenshot(self, label: impl Into<Option<String>>) -> Self {
        self.push(UiActionStepV2::CaptureScreenshot {
            label: label.into(),
            timeout_frames: 300,
            timeout_ms: None,
        })
    }

    pub fn set_clipboard_force_unavailable(self, enabled: bool) -> Self {
        self.push(UiActionStepV2::SetClipboardForceUnavailable { enabled })
    }

    pub fn inject_incoming_open(self, items: Vec<UiIncomingOpenInjectItemV1>) -> Self {
        self.push(UiActionStepV2::InjectIncomingOpen { items })
    }

    pub fn build(self) -> UiActionScriptV2 {
        UiActionScriptV2 {
            schema_version: 2,
            meta: None,
            steps: self.steps,
        }
    }
}