waterui-testing 0.3.0

Headless testing helpers for WaterUI
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
use core::ops::Index;
use std::collections::BTreeMap;

use accesskit::{
    Node as AccessibilityNode, NodeId as AccessibilityNodeId, Rect as AccessibilityRect,
    Role as AccessibilityRole, Toggled as AccessibilityToggled,
    TreeUpdate as AccessibilityTreeUpdate,
};

use crate::selector::{ScopeRelation, Selector};

/// Stable role wrapper exposed by the testing API.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Role(AccessibilityRole);

impl Role {
    /// Button role.
    pub const BUTTON: Self = Self(AccessibilityRole::Button);
    /// Static label role.
    pub const LABEL: Self = Self(AccessibilityRole::Label);
    /// Editable text input role.
    pub const TEXT_INPUT: Self = Self(AccessibilityRole::TextInput);
    /// Secure text input role.
    pub const PASSWORD_INPUT: Self = Self(AccessibilityRole::PasswordInput);
    /// Checkbox role.
    pub const CHECKBOX: Self = Self(AccessibilityRole::CheckBox);
    /// Switch role.
    pub const SWITCH: Self = Self(AccessibilityRole::Switch);
    /// Slider role.
    pub const SLIDER: Self = Self(AccessibilityRole::Slider);
    /// Image role.
    pub const IMAGE: Self = Self(AccessibilityRole::Image);
    /// Scroll view role.
    pub const SCROLL_VIEW: Self = Self(AccessibilityRole::ScrollView);
    /// List role.
    pub const LIST: Self = Self(AccessibilityRole::List);
    /// List item role.
    pub const LIST_ITEM: Self = Self(AccessibilityRole::ListItem);
    /// Tab role.
    pub const TAB: Self = Self(AccessibilityRole::Tab);
    /// Tab list role.
    pub const TAB_LIST: Self = Self(AccessibilityRole::TabList);
    /// Combo box role.
    pub const COMBOBOX: Self = Self(AccessibilityRole::ComboBox);
    /// Selectable option role.
    pub const OPTION: Self = Self(AccessibilityRole::ListBoxOption);
    /// Multiline text input role.
    pub const MULTILINE_TEXT_INPUT: Self = Self(AccessibilityRole::MultilineTextInput);
    /// Link role.
    pub const LINK: Self = Self(AccessibilityRole::Link);
    /// Section heading role.
    pub const HEADER: Self = Self(AccessibilityRole::Header);
    /// Section footer role.
    pub const FOOTER: Self = Self(AccessibilityRole::Footer);
    /// Progress indicator role.
    pub const PROGRESS_INDICATOR: Self = Self(AccessibilityRole::ProgressIndicator);
    /// Stepper / spin button role.
    pub const SPIN_BUTTON: Self = Self(AccessibilityRole::SpinButton);
    /// Radio button role.
    pub const RADIO_BUTTON: Self = Self(AccessibilityRole::RadioButton);
    /// Menu role.
    pub const MENU: Self = Self(AccessibilityRole::Menu);
    /// Menu bar role.
    pub const MENU_BAR: Self = Self(AccessibilityRole::MenuBar);
    /// Menu item role.
    pub const MENU_ITEM: Self = Self(AccessibilityRole::MenuItem);
    /// Checkbox-style menu item role.
    pub const MENU_ITEM_CHECKBOX: Self = Self(AccessibilityRole::MenuItemCheckBox);
    /// Radio-style menu item role.
    pub const MENU_ITEM_RADIO: Self = Self(AccessibilityRole::MenuItemRadio);
    /// Tab panel role.
    pub const TAB_PANEL: Self = Self(AccessibilityRole::TabPanel);
    /// Table role.
    pub const TABLE: Self = Self(AccessibilityRole::Table);
    /// Table cell role.
    pub const CELL: Self = Self(AccessibilityRole::Cell);
    /// Column header role.
    pub const COLUMN_HEADER: Self = Self(AccessibilityRole::ColumnHeader);
    /// Logical grouping role.
    pub const GROUP: Self = Self(AccessibilityRole::Group);
    /// Window root role.
    pub const WINDOW: Self = Self(AccessibilityRole::Window);
    /// Main landmark role.
    pub const MAIN: Self = Self(AccessibilityRole::Main);
    /// Navigation landmark role.
    pub const NAVIGATION: Self = Self(AccessibilityRole::Navigation);
    /// Search landmark role.
    pub const SEARCH: Self = Self(AccessibilityRole::Search);
    /// Article role.
    pub const ARTICLE: Self = Self(AccessibilityRole::Article);
    /// Section role.
    pub const SECTION: Self = Self(AccessibilityRole::Section);

    /// Wraps any AccessKit role, covering roles without a named constant.
    #[must_use]
    pub const fn new(role: AccessibilityRole) -> Self {
        Self(role)
    }

    /// The underlying AccessKit role.
    #[must_use]
    pub const fn as_accesskit(self) -> AccessibilityRole {
        self.0
    }
}

/// Semantic checked state exposed by a checkable accessibility node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CheckedState {
    /// The node is not checked.
    False,
    /// The node is checked.
    True,
    /// The node is indeterminate or represents a mixed selection.
    Mixed,
}

impl From<AccessibilityRole> for Role {
    fn from(role: AccessibilityRole) -> Self {
        Self(role)
    }
}

/// Stable node identifier wrapper exposed by the testing API.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct NodeId(AccessibilityNodeId);

impl NodeId {
    /// Returns the numeric AccessKit node id.
    #[must_use]
    pub const fn as_u64(self) -> u64 {
        self.0.0
    }

    pub(crate) const fn as_accesskit(self) -> AccessibilityNodeId {
        self.0
    }
}

impl From<AccessibilityNodeId> for NodeId {
    fn from(value: AccessibilityNodeId) -> Self {
        Self(value)
    }
}

/// Stable node bounds wrapper exposed by the testing API.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NodeBounds {
    x: f32,
    y: f32,
    width: f32,
    height: f32,
}

impl NodeBounds {
    /// Creates node bounds from x/y origin and width/height.
    #[must_use]
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Returns the x origin.
    #[must_use]
    pub const fn x(self) -> f32 {
        self.x
    }

    /// Returns the y origin.
    #[must_use]
    pub const fn y(self) -> f32 {
        self.y
    }

    /// Returns the width.
    #[must_use]
    pub const fn width(self) -> f32 {
        self.width
    }

    /// Returns the height.
    #[must_use]
    pub const fn height(self) -> f32 {
        self.height
    }

    /// Returns the center point.
    #[must_use]
    pub const fn center(self) -> (f32, f32) {
        (
            self.width.mul_add(0.5, self.x),
            self.height.mul_add(0.5, self.y),
        )
    }
}

#[expect(
    clippy::cast_possible_truncation,
    reason = "waterui-testing exposes f32 logical coordinates for pointer synthesis"
)]
fn accesskit_rect_to_node_bounds(rect: AccessibilityRect) -> NodeBounds {
    NodeBounds::new(
        rect.x0 as f32,
        rect.y0 as f32,
        (rect.x1 - rect.x0) as f32,
        (rect.y1 - rect.y0) as f32,
    )
}

/// Immutable accessibility node snapshot used by assertions and queries.
#[derive(Debug, Clone, PartialEq)]
#[expect(
    clippy::struct_excessive_bools,
    reason = "mirrors independent accessibility attributes reported by the platform tree"
)]
pub struct NodeSnapshot {
    pub(crate) id: NodeId,
    pub(crate) role: Role,
    pub(crate) label: Option<String>,
    pub(crate) identifier: Option<String>,
    pub(crate) value: Option<String>,
    pub(crate) bounds: Option<NodeBounds>,
    pub(crate) enabled: bool,
    pub(crate) selected: bool,
    pub(crate) checked: Option<CheckedState>,
    pub(crate) expanded: Option<bool>,
    pub(crate) busy: bool,
    pub(crate) hidden: bool,
    pub(crate) children: Vec<NodeId>,
}

impl NodeSnapshot {
    /// Returns the node id.
    #[must_use]
    pub const fn id(&self) -> NodeId {
        self.id
    }

    /// Returns the accessibility role.
    #[must_use]
    pub const fn role(&self) -> Role {
        self.role
    }

    /// Returns the accessibility label, if any.
    #[must_use]
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// Returns the stable automation identifier (`a11y_id`), if any.
    #[must_use]
    pub fn identifier(&self) -> Option<&str> {
        self.identifier.as_deref()
    }

    /// Returns the accessibility value, if any.
    #[must_use]
    pub fn value(&self) -> Option<&str> {
        self.value.as_deref()
    }

    /// Returns whether the node is enabled.
    #[must_use]
    pub const fn enabled(&self) -> bool {
        self.enabled
    }

    /// Returns whether the node is selected.
    #[must_use]
    pub const fn selected(&self) -> bool {
        self.selected
    }

    /// Returns the checked state, if applicable.
    #[must_use]
    pub const fn checked(&self) -> Option<bool> {
        match self.checked_state() {
            Some(CheckedState::False) => Some(false),
            Some(CheckedState::True) => Some(true),
            Some(CheckedState::Mixed) | None => None,
        }
    }

    /// Returns the complete checked state, preserving an indeterminate value.
    #[must_use]
    pub const fn checked_state(&self) -> Option<CheckedState> {
        self.checked
    }

    /// Returns the expanded state, if applicable.
    #[must_use]
    pub const fn expanded(&self) -> Option<bool> {
        self.expanded
    }

    /// Returns whether the node reports that it is busy.
    #[must_use]
    pub const fn busy(&self) -> bool {
        self.busy
    }

    /// Returns node bounds, if present.
    #[must_use]
    pub const fn bounds(&self) -> Option<NodeBounds> {
        self.bounds
    }

    /// Returns whether the node is hidden.
    #[must_use]
    pub const fn hidden(&self) -> bool {
        self.hidden
    }

    /// Returns child node ids.
    #[must_use]
    pub fn children(&self) -> &[NodeId] {
        &self.children
    }

    fn from_accesskit(id: AccessibilityNodeId, node: &AccessibilityNode) -> Self {
        let checked = match node.toggled() {
            Some(AccessibilityToggled::True) => Some(CheckedState::True),
            Some(AccessibilityToggled::False) => Some(CheckedState::False),
            Some(AccessibilityToggled::Mixed) => Some(CheckedState::Mixed),
            None => None,
        };
        let expanded = node.is_expanded();

        let value = node
            .value()
            .map(ToOwned::to_owned)
            .or_else(|| node.numeric_value().map(|v| v.to_string()));

        Self {
            id: NodeId::from(id),
            role: Role(node.role()),
            label: node.label().map(ToOwned::to_owned),
            identifier: node.author_id().map(ToOwned::to_owned),
            value,
            bounds: node.bounds().map(accesskit_rect_to_node_bounds),
            enabled: !node.is_disabled(),
            selected: node.is_selected().unwrap_or(false),
            checked,
            expanded,
            busy: node.is_busy(),
            hidden: node.is_hidden(),
            children: node.children().iter().copied().map(NodeId::from).collect(),
        }
    }
}

/// Immutable accessibility tree snapshot.
#[derive(Debug, Clone)]
pub struct TreeSnapshot {
    pub(crate) revision: u64,
    pub(crate) root: NodeId,
    pub(crate) focus: NodeId,
    pub(crate) nodes: BTreeMap<NodeId, NodeSnapshot>,
}

impl TreeSnapshot {
    pub(crate) fn empty() -> Self {
        let root = NodeId::from(AccessibilityNodeId(0));
        Self {
            revision: 0,
            root,
            focus: root,
            nodes: BTreeMap::new(),
        }
    }

    pub(crate) fn from_update(revision: u64, update: AccessibilityTreeUpdate) -> Self {
        let root = update.tree.as_ref().map_or_else(
            || NodeId::from(AccessibilityNodeId(0)),
            |tree| NodeId::from(tree.root),
        );
        let focus = NodeId::from(update.focus);
        let mut nodes = BTreeMap::new();
        for (id, node) in update.nodes {
            let stable_id = NodeId::from(id);
            nodes.insert(stable_id, NodeSnapshot::from_accesskit(id, &node));
        }

        Self {
            revision,
            root,
            focus,
            nodes,
        }
    }

    /// Returns the monotonically increasing snapshot revision.
    #[must_use]
    pub const fn revision(&self) -> u64 {
        self.revision
    }

    /// Returns the root node id.
    #[must_use]
    pub const fn root(&self) -> NodeId {
        self.root
    }

    /// Returns the accessibility focus node id.
    #[must_use]
    pub const fn focus(&self) -> NodeId {
        self.focus
    }

    /// Returns all nodes keyed by stable id.
    #[must_use]
    pub const fn nodes(&self) -> &BTreeMap<NodeId, NodeSnapshot> {
        &self.nodes
    }

    /// Returns one node by id.
    #[must_use]
    pub fn node(&self, id: NodeId) -> Option<&NodeSnapshot> {
        self.nodes.get(&id)
    }

    pub(crate) fn matching(&self, selector: &Selector) -> Vec<NodeId> {
        self.scoped_ids(selector)
            .into_iter()
            .filter(|id| selector.matches(&self[*id]))
            .collect()
    }

    fn scoped_ids(&self, selector: &Selector) -> Vec<NodeId> {
        let Some(scope) = selector.scope() else {
            return self.nodes.keys().copied().collect();
        };
        match scope.relation() {
            ScopeRelation::Descendants => self.descendants_of(scope.handle().id()),
            ScopeRelation::Children => self[scope.handle().id()].children().to_vec(),
        }
    }

    fn descendants_of(&self, parent: NodeId) -> Vec<NodeId> {
        let mut descendants = Vec::new();
        let mut stack = self[parent]
            .children()
            .iter()
            .rev()
            .copied()
            .collect::<Vec<_>>();
        while let Some(node_id) = stack.pop() {
            descendants.push(node_id);
            stack.extend(self[node_id].children().iter().rev().copied());
        }
        descendants
    }
}

impl Index<NodeId> for TreeSnapshot {
    type Output = NodeSnapshot;

    fn index(&self, index: NodeId) -> &Self::Output {
        self.nodes.get(&index).unwrap_or_else(|| {
            panic!(
                "waterui-testing tree index missing node id {} (revision {})",
                index.as_u64(),
                self.revision
            )
        })
    }
}