1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
#[allow(clippy::float_cmp)] // exact-value assertions on computed layout floats
5
#[allow(clippy::module_inception)] // this file IS the `tests` module of hit_test; the inner mod is
6
                                   // the cfg(test) convention
7
mod tests {
8
    use super::*;
9

            
10
    #[test]
11
1
    fn test_dom_node_tag_roundtrip() {
12
1
        let tag = HitTestTag::DomNode {
13
1
            tag_id: TagId { inner: 42 },
14
1
        };
15
1
        let item_tag = tag.to_item_tag();
16
1
        let decoded = HitTestTag::from_item_tag(item_tag).unwrap();
17
1
        assert_eq!(tag, decoded);
18
1
    }
19

            
20
    #[test]
21
1
    fn test_scrollbar_tag_roundtrip() {
22
1
        let tag = HitTestTag::Scrollbar {
23
1
            dom_id: DomId { inner: 1 },
24
1
            node_id: NodeId::new(123),
25
1
            component: ScrollbarComponent::VerticalThumb,
26
1
        };
27
1
        let item_tag = tag.to_item_tag();
28
1
        let decoded = HitTestTag::from_item_tag(item_tag).unwrap();
29
1
        assert_eq!(tag, decoded);
30
1
    }
31

            
32
    #[test]
33
1
    fn test_dom_node_tag_not_confused_with_scrollbar() {
34
        // A DOM node tag with value 673 should NOT be decoded as a scrollbar
35
1
        let dom_tag = HitTestTag::DomNode {
36
1
            tag_id: TagId { inner: 673 },
37
1
        };
38
1
        let item_tag = dom_tag.to_item_tag();
39

            
40
        // Verify it has the correct type marker
41
1
        assert_eq!(item_tag.1, TAG_TYPE_DOM_NODE);
42

            
43
        // Verify it decodes correctly
44
1
        let decoded = HitTestTag::from_item_tag(item_tag).unwrap();
45
1
        assert!(decoded.is_dom_node());
46
1
        assert!(!decoded.is_scrollbar());
47
1
    }
48

            
49
    #[test]
50
1
    fn test_legacy_tag_compatibility() {
51
        // Old format tags had tag.1 == 0
52
        // They should be treated as DOM node tags for backwards compatibility
53
1
        let legacy_tag = (42u64, 0u16);
54
1
        let decoded = HitTestTag::from_item_tag(legacy_tag).unwrap();
55
1
        assert!(decoded.is_dom_node());
56
1
        assert_eq!(decoded.as_dom_node().unwrap().inner, 42);
57
1
    }
58

            
59
5
    fn rect(x: f32, y: f32, w: f32, h: f32) -> LogicalRect {
60
5
        LogicalRect::new(LogicalPosition::new(x, y), LogicalSize::new(w, h))
61
5
    }
62

            
63
    #[test]
64
1
    fn scroll_state_clamps_to_content_minus_viewport() {
65
        // content 300 tall, viewport 100 tall -> max scroll offset = 200
66
1
        let max = max_scroll_rect(&OverflowingScrollNode {
67
1
            parent_rect: rect(0.0, 0.0, 100.0, 100.0),
68
1
            child_rect: rect(0.0, 0.0, 100.0, 300.0),
69
1
            ..Default::default()
70
1
        });
71
1
        assert_eq!(max.size.height, 200.0);
72

            
73
1
        let mut st = ScrollState::default();
74
1
        st.set(0.0, 999.0, &max);
75
1
        assert_eq!(st.scroll_position.y, 200.0); // not 300 (content would fully scroll away)
76
1
    }
77

            
78
    #[test]
79
1
    fn scroll_state_no_scroll_when_content_fits() {
80
        // content == viewport -> zero scroll range
81
1
        let max = max_scroll_rect(&OverflowingScrollNode {
82
1
            parent_rect: rect(0.0, 0.0, 100.0, 100.0),
83
1
            child_rect: rect(0.0, 0.0, 100.0, 100.0),
84
1
            ..Default::default()
85
1
        });
86
1
        let mut st = ScrollState::default();
87
1
        st.add(50.0, 50.0, &max);
88
1
        assert_eq!(st.scroll_position, LogicalPosition::zero());
89
1
    }
90

            
91
    #[test]
92
1
    fn scroll_state_nan_delta_does_not_poison() {
93
1
        let max = rect(0.0, 0.0, 100.0, 200.0);
94
1
        let mut st = ScrollState::default();
95
1
        st.add(f32::NAN, f32::NAN, &max);
96
1
        assert_eq!(st.scroll_position, LogicalPosition::zero());
97
1
    }
98

            
99
    #[test]
100
1
    fn selection_tag_out_of_range_domid_is_clamped_not_corrupting() {
101
        // A DomId > 0xFFFF must not bleed into the NodeId field. The masked
102
        // encode/decode round-trips within the 16-bit DomId window.
103
1
        let tag = HitTestTag::Selection {
104
1
            dom_id: DomId { inner: 0x1_0007 }, // exceeds 16 bits
105
1
            container_node_id: NodeId::new(5),
106
1
            text_run_index: 9,
107
1
        };
108
1
        let (value, ty) = tag.to_item_tag();
109
1
        assert_eq!(ty, TAG_TYPE_SELECTION);
110
        // NodeId field (bits 16..48) is exactly 5, uncorrupted by the overflow.
111
1
        assert_eq!((value >> 16) & 0xFFFF_FFFF, 5);
112
        // DomId field is the low 16 bits of the input (0x0007).
113
1
        assert_eq!(value >> 48, 0x0007);
114
1
    }
115
}
116

            
117
#[cfg(test)]
118
#[allow(
119
    clippy::float_cmp,
120
    clippy::cast_lossless,
121
    clippy::cast_possible_truncation,
122
    clippy::unreadable_literal
123
)]
124
mod autotest_generated {
125
    use super::*;
126

            
127
    // ------------------------------------------------------------------
128
    // helpers
129
    // ------------------------------------------------------------------
130

            
131
    fn r(x: f32, y: f32, w: f32, h: f32) -> LogicalRect {
132
        LogicalRect::new(LogicalPosition::new(x, y), LogicalSize::new(w, h))
133
    }
134

            
135
    /// An `OverflowingScrollNode` with only the two rects that `max_scroll_rect`
136
    /// actually reads set; everything else stays at its `Default`.
137
    fn scroll_node_of(parent: LogicalRect, child: LogicalRect) -> OverflowingScrollNode {
138
        OverflowingScrollNode {
139
            parent_rect: parent,
140
            child_rect: child,
141
            ..Default::default()
142
        }
143
    }
144

            
145
    fn ext_id(raw: u64) -> ExternalScrollId {
146
        ExternalScrollId(raw, PipelineId::DUMMY)
147
    }
148

            
149
    const ALL_CURSORS: [CursorType; 21] = [
150
        CursorType::Default,
151
        CursorType::Pointer,
152
        CursorType::Text,
153
        CursorType::Crosshair,
154
        CursorType::Move,
155
        CursorType::NotAllowed,
156
        CursorType::Grab,
157
        CursorType::Grabbing,
158
        CursorType::EResize,
159
        CursorType::WResize,
160
        CursorType::NResize,
161
        CursorType::SResize,
162
        CursorType::EwResize,
163
        CursorType::NsResize,
164
        CursorType::NeswResize,
165
        CursorType::NwseResize,
166
        CursorType::ColResize,
167
        CursorType::RowResize,
168
        CursorType::Wait,
169
        CursorType::Help,
170
        CursorType::Progress,
171
    ];
172

            
173
    const ALL_SCROLLBAR_COMPONENTS: [ScrollbarComponent; 4] = [
174
        ScrollbarComponent::VerticalTrack,
175
        ScrollbarComponent::VerticalThumb,
176
        ScrollbarComponent::HorizontalTrack,
177
        ScrollbarComponent::HorizontalThumb,
178
    ];
179

            
180
    // ------------------------------------------------------------------
181
    // HitTest / FullHitTest — constructors + predicates
182
    // ------------------------------------------------------------------
183

            
184
    #[test]
185
    fn hit_test_empty_is_neutral_element() {
186
        let h = HitTest::empty();
187
        assert!(h.is_empty());
188
        assert_eq!(h.regular_hit_test_nodes.len(), 0);
189
        assert_eq!(h.scroll_hit_test_nodes.len(), 0);
190
        assert_eq!(h.scrollbar_hit_test_nodes.len(), 0);
191
        assert_eq!(h.cursor_hit_test_nodes.len(), 0);
192
        // repeated construction is stable / equal
193
        assert_eq!(HitTest::empty(), HitTest::empty());
194
    }
195

            
196
    #[test]
197
    fn hit_test_is_empty_false_if_any_single_map_is_populated() {
198
        // Each of the four maps must independently flip `is_empty()` to false,
199
        // otherwise a hit in (say) only the cursor map would be silently dropped.
200
        let mut a = HitTest::empty();
201
        a.regular_hit_test_nodes.insert(
202
            NodeId::ZERO,
203
            HitTestItem {
204
                point_in_viewport: LogicalPosition::zero(),
205
                point_relative_to_item: Default::default(),
206
                is_focusable: false,
207
                is_virtual_view_hit: None,
208
                hit_depth: 0,
209
            },
210
        );
211
        assert!(!a.is_empty());
212

            
213
        let mut b = HitTest::empty();
214
        b.scroll_hit_test_nodes.insert(
215
            NodeId::new(usize::MAX),
216
            ScrollHitTestItem {
217
                point_in_viewport: LogicalPosition::zero(),
218
                point_relative_to_item: Default::default(),
219
                scroll_node: OverflowingScrollNode::default(),
220
            },
221
        );
222
        assert!(!b.is_empty());
223

            
224
        let mut c = HitTest::empty();
225
        c.scrollbar_hit_test_nodes.insert(
226
            ScrollbarHitId::VerticalTrack(DomId::ROOT_ID, NodeId::ZERO),
227
            ScrollbarHitTestItem {
228
                point_in_viewport: LogicalPosition::zero(),
229
                point_relative_to_item: Default::default(),
230
                orientation: ScrollbarOrientation::Vertical,
231
            },
232
        );
233
        assert!(!c.is_empty());
234

            
235
        let mut d = HitTest::empty();
236
        d.cursor_hit_test_nodes.insert(
237
            NodeId::ZERO,
238
            CursorHitTestItem {
239
                cursor_type: CursorType::Default,
240
                hit_depth: u32::MAX,
241
                point_in_viewport: LogicalPosition::new(f32::NAN, f32::INFINITY),
242
            },
243
        );
244
        assert!(!d.is_empty());
245
    }
246

            
247
    #[test]
248
    fn full_hit_test_empty_is_empty_regardless_of_focused_node() {
249
        let none = FullHitTest::empty(None);
250
        assert!(none.is_empty());
251
        assert!(none.focused_node.is_none());
252

            
253
        // `is_empty()` is documented to ignore `focused_node` — a focused node
254
        // must NOT make an unhovered hit test look non-empty.
255
        let focused = FullHitTest::empty(Some(DomNodeId::ROOT));
256
        assert!(focused.is_empty());
257
        assert!(focused.focused_node.is_some());
258
        assert_eq!(focused.hovered_nodes.len(), 0);
259
    }
260

            
261
    #[test]
262
    fn full_hit_test_is_empty_false_once_a_dom_is_hovered() {
263
        let mut f = FullHitTest::empty(None);
264
        f.hovered_nodes.insert(DomId::ROOT_ID, HitTest::empty());
265
        // NOTE: an *empty* HitTest inserted under a DomId still counts as
266
        // "hovered" — `is_empty()` only looks at the outer map's length.
267
        assert!(!f.is_empty());
268
    }
269

            
270
    // ------------------------------------------------------------------
271
    // PipelineId / DocumentId / ExternalScrollId — constructors + serializers
272
    // ------------------------------------------------------------------
273

            
274
    #[test]
275
    fn pipeline_id_new_is_monotonic_and_second_field_is_zero() {
276
        let a = PipelineId::new();
277
        let b = PipelineId::new();
278
        let c = PipelineId::default();
279
        // The counter only ever moves forward (other tests in this binary may
280
        // bump it concurrently, so assert ordering, not adjacency).
281
        assert!(b.0 > a.0);
282
        assert!(c.0 > b.0);
283
        assert_eq!(a.1, 0);
284
        assert_eq!(b.1, 0);
285
        assert_eq!(PipelineId::DUMMY, PipelineId(0, 0));
286
    }
287

            
288
    #[test]
289
    fn pipeline_id_display_and_debug_agree_on_edge_values() {
290
        assert_eq!(alloc::format!("{}", PipelineId::DUMMY), "PipelineId(0, 0)");
291
        let maxed = PipelineId(u32::MAX, u32::MAX);
292
        let shown = alloc::format!("{maxed}");
293
        assert_eq!(shown, "PipelineId(4294967295, 4294967295)");
294
        assert_eq!(alloc::format!("{maxed:?}"), shown);
295
    }
296

            
297
    #[test]
298
    fn document_id_display_handles_min_and_max() {
299
        let zero = DocumentId {
300
            namespace_id: IdNamespace(0),
301
            id: 0,
302
        };
303
        let maxed = DocumentId {
304
            namespace_id: IdNamespace(u32::MAX),
305
            id: u32::MAX,
306
        };
307
        for d in [zero, maxed] {
308
            let shown = alloc::format!("{d}");
309
            assert!(!shown.is_empty());
310
            assert!(shown.starts_with("DocumentId {"));
311
            // Debug delegates to Display, so the two must be byte-identical.
312
            assert_eq!(alloc::format!("{d:?}"), shown);
313
        }
314
        assert!(alloc::format!("{maxed}").contains("4294967295"));
315
    }
316

            
317
    #[test]
318
    fn external_scroll_id_display_omits_the_pipeline_but_keys_stay_distinct() {
319
        let a = ExternalScrollId(7, PipelineId(1, 0));
320
        let b = ExternalScrollId(7, PipelineId(2, 0));
321

            
322
        // Display/Debug only render `.0` — they are deliberately lossy, so two
323
        // *different* IDs can format identically. Guard that this laxness never
324
        // leaks into equality/ordering (which is what BTreeMap keys rely on).
325
        assert_eq!(alloc::format!("{a}"), "ExternalScrollId(7)");
326
        assert_eq!(alloc::format!("{a:?}"), alloc::format!("{b:?}"));
327
        assert_ne!(a, b);
328

            
329
        let mut states = ScrollStates::new();
330
        states.0.insert(a, ScrollState::default());
331
        states.0.insert(b, ScrollState::default());
332
        assert_eq!(states.0.len(), 2);
333
    }
334

            
335
    #[test]
336
    fn external_scroll_id_display_no_panic_at_u64_max() {
337
        let shown = alloc::format!("{}", ExternalScrollId(u64::MAX, PipelineId::DUMMY));
338
        assert_eq!(shown, "ExternalScrollId(18446744073709551615)");
339
    }
340

            
341
    // ------------------------------------------------------------------
342
    // max_scroll_rect — numeric edge cases
343
    // ------------------------------------------------------------------
344

            
345
    #[test]
346
    fn max_scroll_rect_is_content_minus_viewport_and_keeps_origin() {
347
        let max = max_scroll_rect(&scroll_node_of(
348
            r(0.0, 0.0, 100.0, 100.0),
349
            r(-12.5, 7.25, 400.0, 300.0),
350
        ));
351
        assert_eq!(max.size.width, 300.0);
352
        assert_eq!(max.size.height, 200.0);
353
        // documented: the returned rect keeps `child_rect.origin`
354
        assert_eq!(max.origin.x, -12.5);
355
        assert_eq!(max.origin.y, 7.25);
356
    }
357

            
358
    #[test]
359
    fn max_scroll_rect_clamps_negative_range_to_zero() {
360
        // viewport larger than content -> nothing to scroll (must not go negative)
361
        let max = max_scroll_rect(&scroll_node_of(
362
            r(0.0, 0.0, 500.0, 500.0),
363
            r(0.0, 0.0, 100.0, 100.0),
364
        ));
365
        assert_eq!(max.size.width, 0.0);
366
        assert_eq!(max.size.height, 0.0);
367
    }
368

            
369
    #[test]
370
    fn max_scroll_rect_nan_and_infinite_sizes_do_not_produce_nan() {
371
        // NaN content size: `NaN - x` is NaN, and `.max(0.0)` collapses it to 0.0.
372
        let nan = max_scroll_rect(&scroll_node_of(
373
            r(0.0, 0.0, 10.0, 10.0),
374
            r(0.0, 0.0, f32::NAN, f32::NAN),
375
        ));
376
        assert!(!nan.size.width.is_nan());
377
        assert!(!nan.size.height.is_nan());
378
        assert_eq!(nan.size.width, 0.0);
379
        assert_eq!(nan.size.height, 0.0);
380

            
381
        // inf - inf == NaN -> also collapses to 0.0 rather than poisoning scroll.
382
        let both_inf = max_scroll_rect(&scroll_node_of(
383
            r(0.0, 0.0, f32::INFINITY, f32::INFINITY),
384
            r(0.0, 0.0, f32::INFINITY, f32::INFINITY),
385
        ));
386
        assert_eq!(both_inf.size.width, 0.0);
387
        assert_eq!(both_inf.size.height, 0.0);
388

            
389
        // Infinite content over a finite viewport stays infinite (unbounded scroll).
390
        let inf = max_scroll_rect(&scroll_node_of(
391
            r(0.0, 0.0, 10.0, 10.0),
392
            r(0.0, 0.0, f32::INFINITY, f32::INFINITY),
393
        ));
394
        assert!(inf.size.width.is_infinite() && inf.size.width.is_sign_positive());
395
        assert!(inf.size.height.is_infinite() && inf.size.height.is_sign_positive());
396
    }
397

            
398
    #[test]
399
    fn max_scroll_rect_at_float_extremes_does_not_panic() {
400
        let max = max_scroll_rect(&scroll_node_of(
401
            r(f32::MIN, f32::MIN, f32::MIN_POSITIVE, f32::MAX),
402
            r(f32::MAX, f32::MAX, f32::MAX, f32::MIN_POSITIVE),
403
        ));
404
        assert!(max.size.width >= 0.0);
405
        assert!(max.size.height >= 0.0);
406
        assert!(!max.size.width.is_nan());
407
        assert!(!max.size.height.is_nan());
408
    }
409

            
410
    // ------------------------------------------------------------------
411
    // ScrollState — numeric (zero / negative / min-max / overflow / NaN)
412
    // ------------------------------------------------------------------
413

            
414
    #[test]
415
    fn scroll_state_default_and_get_round_trip() {
416
        let st = ScrollState::default();
417
        assert_eq!(st.get(), LogicalPosition::zero());
418

            
419
        let st = ScrollState {
420
            scroll_position: LogicalPosition::new(3.5, -4.25),
421
        };
422
        // `get` is a pure accessor: it must not clamp or normalize.
423
        assert_eq!(st.get().x, 3.5);
424
        assert_eq!(st.get().y, -4.25);
425
    }
426

            
427
    #[test]
428
    fn scroll_state_set_zero_is_identity_within_range() {
429
        let max = r(0.0, 0.0, 100.0, 200.0);
430
        let mut st = ScrollState::default();
431
        st.set(0.0, 0.0, &max);
432
        assert_eq!(st.get(), LogicalPosition::zero());
433

            
434
        st.set(50.0, 150.0, &max);
435
        assert_eq!(st.get().x, 50.0);
436
        assert_eq!(st.get().y, 150.0);
437
    }
438

            
439
    #[test]
440
    fn scroll_state_set_clamps_negative_to_zero_and_overshoot_to_max() {
441
        let max = r(0.0, 0.0, 100.0, 200.0);
442
        let mut st = ScrollState::default();
443

            
444
        st.set(-1.0, -f32::MAX, &max);
445
        assert_eq!(st.get().x, 0.0);
446
        assert_eq!(st.get().y, 0.0);
447

            
448
        st.set(f32::MAX, f32::MAX, &max);
449
        assert_eq!(st.get().x, 100.0);
450
        assert_eq!(st.get().y, 200.0);
451

            
452
        st.set(f32::INFINITY, f32::INFINITY, &max);
453
        assert_eq!(st.get().x, 100.0);
454
        assert_eq!(st.get().y, 200.0);
455

            
456
        st.set(f32::NEG_INFINITY, f32::NEG_INFINITY, &max);
457
        assert_eq!(st.get().x, 0.0);
458
        assert_eq!(st.get().y, 0.0);
459
    }
460

            
461
    #[test]
462
    fn scroll_state_set_nan_position_collapses_to_zero() {
463
        let max = r(0.0, 0.0, 100.0, 200.0);
464
        let mut st = ScrollState {
465
            scroll_position: LogicalPosition::new(40.0, 40.0),
466
        };
467
        st.set(f32::NAN, f32::NAN, &max);
468
        assert!(!st.get().x.is_nan());
469
        assert!(!st.get().y.is_nan());
470
        assert_eq!(st.get(), LogicalPosition::zero());
471
    }
472

            
473
    #[test]
474
    fn scroll_state_set_nan_max_range_collapses_to_zero() {
475
        // A NaN *range* must not leak into the position either: `NaN.max(0.0)`
476
        // is 0.0, so the position clamps to 0 rather than becoming NaN.
477
        let max = r(0.0, 0.0, f32::NAN, f32::NAN);
478
        let mut st = ScrollState::default();
479
        st.set(75.0, 75.0, &max);
480
        assert_eq!(st.get(), LogicalPosition::zero());
481
    }
482

            
483
    #[test]
484
    fn scroll_state_set_negative_max_range_collapses_to_zero() {
485
        let max = r(0.0, 0.0, -50.0, -50.0);
486
        let mut st = ScrollState::default();
487
        st.set(10.0, 10.0, &max);
488
        assert_eq!(st.get(), LogicalPosition::zero());
489
    }
490

            
491
    #[test]
492
    fn scroll_state_add_accumulates_then_saturates_at_the_range() {
493
        let max = r(0.0, 0.0, 100.0, 200.0);
494
        let mut st = ScrollState::default();
495
        st.add(30.0, 30.0, &max);
496
        st.add(30.0, 30.0, &max);
497
        assert_eq!(st.get().x, 60.0);
498
        assert_eq!(st.get().y, 60.0);
499

            
500
        // Deltas far beyond the f32 range must clamp to the max offset, and
501
        // re-applying them must not push the position past it (or to +inf).
502
        st.add(f32::MAX, f32::MAX, &max);
503
        st.add(f32::MAX, f32::MAX, &max);
504
        assert_eq!(st.get().x, 100.0);
505
        assert_eq!(st.get().y, 200.0);
506
        assert!(st.get().x.is_finite() && st.get().y.is_finite());
507
    }
508

            
509
    #[test]
510
    fn scroll_state_add_negative_underflow_clamps_to_zero() {
511
        let max = r(0.0, 0.0, 100.0, 200.0);
512
        let mut st = ScrollState {
513
            scroll_position: LogicalPosition::new(10.0, 10.0),
514
        };
515
        st.add(f32::MIN, f32::MIN, &max);
516
        assert_eq!(st.get(), LogicalPosition::zero());
517

            
518
        st.add(f32::NEG_INFINITY, f32::NEG_INFINITY, &max);
519
        assert_eq!(st.get(), LogicalPosition::zero());
520
        assert!(st.get().x.is_finite() && st.get().y.is_finite());
521
    }
522

            
523
    #[test]
524
    fn scroll_state_add_inf_minus_inf_does_not_poison_position() {
525
        // Worst case: an unbounded scroll range lets the position itself become
526
        // +inf, and the *next* delta is -inf -> `inf + -inf == NaN`. The `.max(0.0)`
527
        // clamp must still collapse that NaN back to a defined 0.0.
528
        let unbounded = r(0.0, 0.0, f32::INFINITY, f32::INFINITY);
529
        let mut st = ScrollState::default();
530
        st.add(f32::INFINITY, f32::INFINITY, &unbounded);
531
        assert!(st.get().x.is_infinite());
532

            
533
        st.add(f32::NEG_INFINITY, f32::NEG_INFINITY, &unbounded);
534
        assert!(!st.get().x.is_nan());
535
        assert!(!st.get().y.is_nan());
536
        assert_eq!(st.get(), LogicalPosition::zero());
537
    }
538

            
539
    #[test]
540
    fn scroll_state_add_nan_delta_from_nonzero_position_resets_to_zero() {
541
        let max = r(0.0, 0.0, 100.0, 200.0);
542
        let mut st = ScrollState {
543
            scroll_position: LogicalPosition::new(50.0, 50.0),
544
        };
545
        st.add(f32::NAN, 0.0, &max);
546
        // `50 + NaN == NaN`, `NaN.max(0.0) == 0.0`: the delta is discarded *and*
547
        // the previously-good X position is lost. Defined, but lossy — pinned here.
548
        assert_eq!(st.get().x, 0.0);
549
        assert_eq!(st.get().y, 50.0);
550
    }
551

            
552
    // ------------------------------------------------------------------
553
    // ScrollStates — map behaviour
554
    // ------------------------------------------------------------------
555

            
556
    #[test]
557
    fn scroll_states_new_is_empty_and_lookup_misses_return_none() {
558
        let states = ScrollStates::new();
559
        assert_eq!(states.0.len(), 0);
560
        assert!(states.get_scroll_position(&ext_id(0)).is_none());
561
        assert!(states.get_scroll_position(&ext_id(u64::MAX)).is_none());
562
    }
563

            
564
    #[test]
565
    fn scroll_states_set_scroll_position_creates_entry_and_clamps() {
566
        let node = scroll_node_of(r(0.0, 0.0, 100.0, 100.0), r(0.0, 0.0, 100.0, 300.0));
567
        let mut states = ScrollStates::new();
568

            
569
        states.set_scroll_position(&node, LogicalPosition::new(999.0, 999.0));
570
        let pos = states
571
            .get_scroll_position(&node.parent_external_scroll_id)
572
            .expect("entry must exist after set_scroll_position");
573
        assert_eq!(states.0.len(), 1);
574
        assert_eq!(pos.x, 0.0); // no horizontal overflow -> zero range
575
        assert_eq!(pos.y, 200.0); // 300 content - 100 viewport
576

            
577
        // Re-setting the same node updates in place rather than adding a key.
578
        states.set_scroll_position(&node, LogicalPosition::new(-5.0, -5.0));
579
        assert_eq!(states.0.len(), 1);
580
        let pos = states
581
            .get_scroll_position(&node.parent_external_scroll_id)
582
            .unwrap();
583
        assert_eq!(pos, LogicalPosition::zero());
584
    }
585

            
586
    #[test]
587
    fn scroll_states_set_scroll_position_with_nan_stays_defined() {
588
        let node = scroll_node_of(r(0.0, 0.0, 100.0, 100.0), r(0.0, 0.0, 100.0, 300.0));
589
        let mut states = ScrollStates::new();
590
        states.set_scroll_position(&node, LogicalPosition::new(f32::NAN, f32::NAN));
591
        let pos = states
592
            .get_scroll_position(&node.parent_external_scroll_id)
593
            .unwrap();
594
        assert!(!pos.x.is_nan() && !pos.y.is_nan());
595
        assert_eq!(pos, LogicalPosition::zero());
596
    }
597

            
598
    #[test]
599
    fn scroll_states_scroll_node_accumulates_and_saturates() {
600
        let node = scroll_node_of(r(0.0, 0.0, 100.0, 100.0), r(0.0, 0.0, 400.0, 300.0));
601
        let mut states = ScrollStates::new();
602

            
603
        states.scroll_node(&node, 0.0, 0.0);
604
        assert_eq!(
605
            states
606
                .get_scroll_position(&node.parent_external_scroll_id)
607
                .unwrap(),
608
            LogicalPosition::zero()
609
        );
610

            
611
        states.scroll_node(&node, 10.0, 10.0);
612
        states.scroll_node(&node, 10.0, 10.0);
613
        let pos = states
614
            .get_scroll_position(&node.parent_external_scroll_id)
615
            .unwrap();
616
        assert_eq!(pos.x, 20.0);
617
        assert_eq!(pos.y, 20.0);
618

            
619
        // Saturate: max range is (400-100, 300-100) = (300, 200).
620
        states.scroll_node(&node, f32::MAX, f32::INFINITY);
621
        let pos = states
622
            .get_scroll_position(&node.parent_external_scroll_id)
623
            .unwrap();
624
        assert_eq!(pos.x, 300.0);
625
        assert_eq!(pos.y, 200.0);
626

            
627
        // ...and NaN deltas never corrupt the stored position.
628
        states.scroll_node(&node, f32::NAN, f32::NAN);
629
        let pos = states
630
            .get_scroll_position(&node.parent_external_scroll_id)
631
            .unwrap();
632
        assert!(!pos.x.is_nan() && !pos.y.is_nan());
633
        assert_eq!(states.0.len(), 1);
634
    }
635

            
636
    #[test]
637
    fn scroll_states_keys_are_pipeline_qualified() {
638
        // Same raw scroll tag, different pipeline => two independent scroll states.
639
        let a = OverflowingScrollNode {
640
            parent_rect: r(0.0, 0.0, 10.0, 10.0),
641
            child_rect: r(0.0, 0.0, 10.0, 100.0),
642
            parent_external_scroll_id: ExternalScrollId(1, PipelineId(1, 0)),
643
            ..Default::default()
644
        };
645
        let b = OverflowingScrollNode {
646
            parent_external_scroll_id: ExternalScrollId(1, PipelineId(2, 0)),
647
            ..a
648
        };
649

            
650
        let mut states = ScrollStates::new();
651
        states.scroll_node(&a, 0.0, 25.0);
652
        states.scroll_node(&b, 0.0, 50.0);
653
        assert_eq!(states.0.len(), 2);
654
        assert_eq!(
655
            states
656
                .get_scroll_position(&a.parent_external_scroll_id)
657
                .unwrap()
658
                .y,
659
            25.0
660
        );
661
        assert_eq!(
662
            states
663
                .get_scroll_position(&b.parent_external_scroll_id)
664
                .unwrap()
665
                .y,
666
            50.0
667
        );
668
    }
669

            
670
    // ------------------------------------------------------------------
671
    // ScrollbarComponent / CursorType — from_u8 over the whole domain
672
    // ------------------------------------------------------------------
673

            
674
    #[test]
675
    fn scrollbar_component_from_u8_covers_all_256_values() {
676
        for v in 0u8..=255 {
677
            match ScrollbarComponent::from_u8(v) {
678
                Some(c) => {
679
                    assert!(v < 4, "value {v} unexpectedly decoded to {c:?}");
680
                    // discriminant round-trips
681
                    assert_eq!(c as u8, v);
682
                }
683
                None => assert!(v >= 4, "value {v} should have decoded"),
684
            }
685
        }
686
        for c in ALL_SCROLLBAR_COMPONENTS {
687
            assert_eq!(ScrollbarComponent::from_u8(c as u8), Some(c));
688
        }
689
    }
690

            
691
    #[test]
692
    fn cursor_type_from_u8_is_total_and_unknown_falls_back_to_default() {
693
        for v in 0u8..=255 {
694
            let c = CursorType::from_u8(v);
695
            if v <= 20 {
696
                assert_eq!(c as u8, v, "known discriminant {v} must round-trip");
697
            } else {
698
                // Out-of-range bytes must degrade to Default, never panic.
699
                assert_eq!(c, CursorType::Default, "unknown byte {v} must be Default");
700
            }
701
        }
702
        assert_eq!(CursorType::default(), CursorType::Default);
703
        for c in ALL_CURSORS {
704
            assert_eq!(CursorType::from_u8(c as u8), c);
705
        }
706
    }
707

            
708
    // ------------------------------------------------------------------
709
    // HitTestTag — encode/decode round-trips
710
    // ------------------------------------------------------------------
711

            
712
    #[test]
713
    fn dom_node_tag_round_trips_at_u64_boundaries() {
714
        for inner in [0u64, 1, 673, u64::from(u32::MAX), u64::MAX] {
715
            let tag = HitTestTag::DomNode {
716
                tag_id: TagId { inner },
717
            };
718
            let item = tag.to_item_tag();
719
            assert_eq!(item, (inner, TAG_TYPE_DOM_NODE));
720
            assert_eq!(HitTestTag::from_item_tag(item), Some(tag));
721
            assert_eq!(tag.as_dom_node().unwrap().inner, inner);
722
        }
723
    }
724

            
725
    #[test]
726
    fn scrollbar_tag_round_trips_for_every_component_at_field_boundaries() {
727
        let ids = [
728
            (0usize, 0usize),
729
            (0, u32::MAX as usize),
730
            (u32::MAX as usize, 0),
731
            (u32::MAX as usize, u32::MAX as usize),
732
        ];
733
        for component in ALL_SCROLLBAR_COMPONENTS {
734
            for (dom, node) in ids {
735
                let tag = HitTestTag::Scrollbar {
736
                    dom_id: DomId { inner: dom },
737
                    node_id: NodeId::new(node),
738
                    component,
739
                };
740
                let item = tag.to_item_tag();
741
                assert_eq!(item.1 & 0xFF00, TAG_TYPE_SCROLLBAR);
742
                assert_eq!(
743
                    HitTestTag::from_item_tag(item),
744
                    Some(tag),
745
                    "scrollbar round-trip failed for dom={dom} node={node} {component:?}"
746
                );
747
            }
748
        }
749
    }
750

            
751
    #[test]
752
    fn cursor_tag_round_trips_for_every_cursor_type() {
753
        for cursor_type in ALL_CURSORS {
754
            let tag = HitTestTag::Cursor {
755
                dom_id: DomId {
756
                    inner: u32::MAX as usize,
757
                },
758
                node_id: NodeId::new(u32::MAX as usize),
759
                cursor_type,
760
            };
761
            let item = tag.to_item_tag();
762
            assert_eq!(item.1 & 0xFF00, TAG_TYPE_CURSOR);
763
            assert_eq!(
764
                HitTestTag::from_item_tag(item),
765
                Some(tag),
766
                "cursor round-trip failed for {cursor_type:?}"
767
            );
768
        }
769
    }
770

            
771
    #[test]
772
    fn selection_tag_round_trips_at_every_field_boundary() {
773
        let cases = [
774
            (0usize, 0usize, 0u16),
775
            (0xFFFF, u32::MAX as usize, u16::MAX),
776
            (1, 1, 1),
777
            (0xFFFF, 0, u16::MAX),
778
        ];
779
        for (dom, node, run) in cases {
780
            let tag = HitTestTag::Selection {
781
                dom_id: DomId { inner: dom },
782
                container_node_id: NodeId::new(node),
783
                text_run_index: run,
784
            };
785
            let item = tag.to_item_tag();
786
            assert_eq!(item.1, TAG_TYPE_SELECTION);
787
            assert_eq!(
788
                HitTestTag::from_item_tag(item),
789
                Some(tag),
790
                "selection round-trip failed for dom={dom} node={node} run={run}"
791
            );
792
        }
793
    }
794

            
795
    #[test]
796
    fn selection_tag_oversized_node_id_is_masked_not_bled_into_dom_id() {
797
        // Mirror of the DomId-overflow guard, from the other side: a
798
        // container_node_id wider than 32 bits must be masked, leaving the
799
        // DomId field (bits 48..64) intact.
800
        let tag = HitTestTag::Selection {
801
            dom_id: DomId { inner: 0x00AB },
802
            container_node_id: NodeId::new(u32::MAX as usize),
803
            text_run_index: 0xBEEF,
804
        };
805
        let (value, _) = tag.to_item_tag();
806
        assert_eq!(value >> 48, 0x00AB);
807
        assert_eq!((value >> 16) & 0xFFFF_FFFF, u64::from(u32::MAX));
808
        assert_eq!(value & 0xFFFF, 0xBEEF);
809
    }
810

            
811
    #[test]
812
    fn tag_namespaces_do_not_collide_on_identical_payloads() {
813
        // The same numeric payload under four different type markers must decode
814
        // to four different variants — that is the whole point of the namespaces.
815
        let payload = 0x0000_0001_0000_0002u64;
816
        let decoded: [HitTestTag; 4] = [
817
            HitTestTag::from_item_tag((payload, TAG_TYPE_DOM_NODE)).unwrap(),
818
            HitTestTag::from_item_tag((payload, TAG_TYPE_SCROLLBAR)).unwrap(),
819
            HitTestTag::from_item_tag((payload, TAG_TYPE_SELECTION)).unwrap(),
820
            HitTestTag::from_item_tag((payload, TAG_TYPE_CURSOR)).unwrap(),
821
        ];
822
        assert!(decoded[0].is_dom_node());
823
        assert!(decoded[1].is_scrollbar());
824
        assert!(decoded[2].is_selection());
825
        assert!(decoded[3].is_cursor());
826
        for (i, a) in decoded.iter().enumerate() {
827
            for b in decoded.iter().skip(i + 1) {
828
                assert_ne!(a, b);
829
            }
830
        }
831
    }
832

            
833
    // ------------------------------------------------------------------
834
    // HitTestTag::from_item_tag — malformed / unknown input
835
    // ------------------------------------------------------------------
836

            
837
    #[test]
838
    fn from_item_tag_sweeps_every_type_marker_without_panicking() {
839
        // Exhaustive sweep of the upper byte x a few lower bytes: every
840
        // combination must either decode or return None — never panic.
841
        for hi in 0u16..=0xFF {
842
            for lo in [0u16, 1, 3, 4, 20, 21, 0xFF] {
843
                let tag_type = (hi << 8) | lo;
844
                let decoded = HitTestTag::from_item_tag((0xDEAD_BEEF_CAFE_BABE, tag_type));
845
                let expected_some = match hi {
846
                    0x00 => tag_type == 0,      // legacy DOM tags only
847
                    0x01 | 0x03 | 0x04 => true, // DomNode / Selection / Cursor
848
                    0x02 => lo < 4,             // Scrollbar: component must be valid
849
                    _ => false,                 // 0x05.. (incl. SCROLL_CONTAINER) is unknown
850
                };
851
                assert_eq!(
852
                    decoded.is_some(),
853
                    expected_some,
854
                    "tag_type {tag_type:#06x} decoded to {decoded:?}"
855
                );
856
            }
857
        }
858
    }
859

            
860
    #[test]
861
    fn from_item_tag_rejects_invalid_scrollbar_components() {
862
        for lo in 4u16..=0xFF {
863
            let item = (0u64, TAG_TYPE_SCROLLBAR | lo);
864
            assert!(
865
                HitTestTag::from_item_tag(item).is_none(),
866
                "scrollbar component byte {lo} should be rejected"
867
            );
868
        }
869
    }
870

            
871
    #[test]
872
    fn from_item_tag_unknown_cursor_byte_degrades_to_default() {
873
        // Unlike scrollbars, an unknown cursor byte is *not* an error: it maps
874
        // to CursorType::Default so a corrupt tag still yields a usable cursor.
875
        for lo in [21u16, 100, 0xFF] {
876
            let decoded = HitTestTag::from_item_tag((0, TAG_TYPE_CURSOR | lo)).unwrap();
877
            assert_eq!(
878
                decoded.as_cursor().unwrap().2,
879
                CursorType::Default,
880
                "cursor byte {lo} should fall back to Default"
881
            );
882
        }
883
    }
884

            
885
    #[test]
886
    fn from_item_tag_dom_node_ignores_the_lower_byte() {
887
        // Only the upper byte selects the namespace; junk in the lower byte of a
888
        // DOM-node tag must not flip it to another variant or drop it.
889
        for lo in [0u16, 1, 0x7F, 0xFF] {
890
            let decoded = HitTestTag::from_item_tag((99, TAG_TYPE_DOM_NODE | lo)).unwrap();
891
            assert!(decoded.is_dom_node());
892
            assert_eq!(decoded.as_dom_node().unwrap().inner, 99);
893
        }
894
    }
895

            
896
    #[test]
897
    fn from_item_tag_scroll_container_marker_is_not_decodable() {
898
        // TAG_TYPE_SCROLL_CONTAINER has no HitTestTag variant — it must be
899
        // rejected rather than silently aliased onto another namespace.
900
        assert!(HitTestTag::from_item_tag((0, TAG_TYPE_SCROLL_CONTAINER)).is_none());
901
        assert!(HitTestTag::from_item_tag((u64::MAX, TAG_TYPE_SCROLL_CONTAINER)).is_none());
902
    }
903

            
904
    #[test]
905
    fn from_item_tag_legacy_zero_type_only_matches_exact_zero() {
906
        // tag_type == 0 is the legacy DOM-node escape hatch...
907
        let legacy = HitTestTag::from_item_tag((u64::MAX, 0)).unwrap();
908
        assert_eq!(legacy.as_dom_node().unwrap().inner, u64::MAX);
909
        // ...but a *nonzero* lower byte with a zero upper byte is not a known
910
        // namespace and must be rejected, not treated as legacy.
911
        for lo in 1u16..=0xFF {
912
            assert!(
913
                HitTestTag::from_item_tag((0, lo)).is_none(),
914
                "tag_type {lo:#06x} must not be treated as a legacy DOM tag"
915
            );
916
        }
917
    }
918

            
919
    #[cfg(target_pointer_width = "64")]
920
    #[test]
921
    fn scrollbar_encode_keeps_decoded_fields_inside_their_bit_windows() {
922
        // A NodeId wider than 32 bits is absurd, but must degrade without
923
        // panicking (no shift/`as` overflow) and must not produce out-of-window
924
        // decoded values. NOTE: unlike `Selection`, the `Scrollbar`/`Cursor`
925
        // encoders do NOT mask their fields, so such an id is lossy — see report.
926
        let tag = HitTestTag::Scrollbar {
927
            dom_id: DomId { inner: 0 },
928
            node_id: NodeId::new(1usize << 32),
929
            component: ScrollbarComponent::VerticalTrack,
930
        };
931
        let (value, ty) = tag.to_item_tag();
932
        assert_eq!(ty & 0xFF00, TAG_TYPE_SCROLLBAR);
933

            
934
        let decoded = HitTestTag::from_item_tag((value, ty)).expect("must still decode");
935
        let (dom, node, component) = decoded.as_scrollbar().unwrap();
936
        assert!(dom.inner <= u32::MAX as usize);
937
        assert!(node.index() <= u32::MAX as usize);
938
        assert_eq!(component, ScrollbarComponent::VerticalTrack);
939
    }
940

            
941
    // ------------------------------------------------------------------
942
    // HitTestTag — predicates + accessors
943
    // ------------------------------------------------------------------
944

            
945
    fn sample_tags() -> [HitTestTag; 4] {
946
        [
947
            HitTestTag::DomNode {
948
                tag_id: TagId { inner: 7 },
949
            },
950
            HitTestTag::Scrollbar {
951
                dom_id: DomId { inner: 1 },
952
                node_id: NodeId::new(2),
953
                component: ScrollbarComponent::HorizontalThumb,
954
            },
955
            HitTestTag::Cursor {
956
                dom_id: DomId { inner: 3 },
957
                node_id: NodeId::new(4),
958
                cursor_type: CursorType::Grabbing,
959
            },
960
            HitTestTag::Selection {
961
                dom_id: DomId { inner: 5 },
962
                container_node_id: NodeId::new(6),
963
                text_run_index: 8,
964
            },
965
        ]
966
    }
967

            
968
    #[test]
969
    fn exactly_one_predicate_is_true_per_variant() {
970
        for tag in sample_tags() {
971
            let flags = [
972
                tag.is_dom_node(),
973
                tag.is_scrollbar(),
974
                tag.is_cursor(),
975
                tag.is_selection(),
976
            ];
977
            assert_eq!(
978
                flags.iter().filter(|b| **b).count(),
979
                1,
980
                "predicates not mutually exclusive for {tag:?}"
981
            );
982
        }
983
    }
984

            
985
    #[test]
986
    fn accessors_agree_with_predicates_and_return_none_otherwise() {
987
        for tag in sample_tags() {
988
            assert_eq!(tag.as_dom_node().is_some(), tag.is_dom_node());
989
            assert_eq!(tag.as_scrollbar().is_some(), tag.is_scrollbar());
990
            assert_eq!(tag.as_cursor().is_some(), tag.is_cursor());
991
            assert_eq!(tag.as_selection().is_some(), tag.is_selection());
992

            
993
            // exactly one accessor yields a value
994
            let some_count = usize::from(tag.as_dom_node().is_some())
995
                + usize::from(tag.as_scrollbar().is_some())
996
                + usize::from(tag.as_cursor().is_some())
997
                + usize::from(tag.as_selection().is_some());
998
            assert_eq!(some_count, 1, "accessor overlap for {tag:?}");
999
        }
    }
    #[test]
    fn accessors_return_the_constructed_payload() {
        let [dom, scrollbar, cursor, selection] = sample_tags();
        assert_eq!(dom.as_dom_node().unwrap().inner, 7);
        let (d, n, c) = scrollbar.as_scrollbar().unwrap();
        assert_eq!((d.inner, n.index()), (1, 2));
        assert_eq!(c, ScrollbarComponent::HorizontalThumb);
        let (d, n, c) = cursor.as_cursor().unwrap();
        assert_eq!((d.inner, n.index()), (3, 4));
        assert_eq!(c, CursorType::Grabbing);
        let (d, n, run) = selection.as_selection().unwrap();
        assert_eq!((d.inner, n.index()), (5, 6));
        assert_eq!(run, 8);
    }
    // ------------------------------------------------------------------
    // HitTestTag — Display
    // ------------------------------------------------------------------
    #[test]
    fn hit_test_tag_display_is_non_empty_and_variant_tagged() {
        let [dom, scrollbar, cursor, selection] = sample_tags();
        for (tag, prefix) in [
            (dom, "DomNode("),
            (scrollbar, "Scrollbar("),
            (cursor, "Cursor("),
            (selection, "Selection("),
        ] {
            let shown = alloc::format!("{tag}");
            assert!(!shown.is_empty());
            assert!(
                shown.starts_with(prefix),
                "expected {shown:?} to start with {prefix:?}"
            );
        }
    }
    #[test]
    fn hit_test_tag_display_handles_extreme_ids() {
        let tags = [
            HitTestTag::DomNode {
                tag_id: TagId { inner: u64::MAX },
            },
            HitTestTag::Scrollbar {
                dom_id: DomId { inner: usize::MAX },
                node_id: NodeId::new(usize::MAX),
                component: ScrollbarComponent::VerticalThumb,
            },
            HitTestTag::Cursor {
                dom_id: DomId { inner: usize::MAX },
                node_id: NodeId::new(usize::MAX),
                cursor_type: CursorType::Progress,
            },
            HitTestTag::Selection {
                dom_id: DomId { inner: usize::MAX },
                container_node_id: NodeId::new(usize::MAX),
                text_run_index: u16::MAX,
            },
        ];
        for tag in tags {
            assert!(!alloc::format!("{tag}").is_empty());
            assert!(!alloc::format!("{tag:?}").is_empty());
            // encoding an extreme tag must not panic either
            let _ = tag.to_item_tag();
        }
    }
}
#[cfg(test)]
mod hovered_node_answers {
    use alloc::{collections::BTreeMap, vec, vec::Vec};
    use crate::{
        dom::{DomId, DomNodeId, NodeId},
        geom::LogicalPosition,
        hit_test::{FullHitTest, HitTest, HitTestItem, OptionDomNodeId},
        styled_dom::NodeHierarchyItemId,
    };
7
    fn item(depth: u32) -> HitTestItem {
7
        HitTestItem {
7
            point_in_viewport: LogicalPosition::zero(),
7
            point_relative_to_item: crate::spaces::ContentBoxLocal::new(LogicalPosition::zero()),
7
            is_focusable: false,
7
            is_virtual_view_hit: None,
7
            hit_depth: depth,
7
        }
7
    }
4
    fn hit_test(nodes: &[(usize, u32)]) -> HitTest {
4
        let mut regular = BTreeMap::new();
11
        for (id, depth) in nodes {
7
            regular.insert(NodeId::new(*id), item(*depth));
7
        }
4
        HitTest {
4
            regular_hit_test_nodes: regular,
4
            scroll_hit_test_nodes: BTreeMap::new(),
4
            scrollbar_hit_test_nodes: BTreeMap::new(),
4
            cursor_hit_test_nodes: BTreeMap::new(),
4
        }
4
    }
9
    fn nid(node: usize) -> DomNodeId {
9
        DomNodeId {
9
            dom: DomId::ROOT_ID,
9
            node: NodeHierarchyItemId::from_crate_internal(Some(NodeId::new(node))),
9
        }
9
    }
3
    fn full(nodes: &[(usize, u32)]) -> FullHitTest {
3
        let mut hovered = BTreeMap::new();
3
        hovered.insert(DomId::ROOT_ID, hit_test(nodes));
3
        FullHitTest {
3
            hovered_nodes: hovered,
3
            focused_node: OptionDomNodeId::None,
3
        }
3
    }
    /// DEPTH ORDERS THE ANSWER, not the node id. Two overlapping absolutely
    /// positioned nodes can have any id relationship, and the one on top is
    /// the one with the lower `hit_depth` - so a call site that took the
    /// largest id (as one in the dll does) can name the node underneath.
    #[test]
1
    fn hovered_nodes_come_back_front_to_back_by_depth_not_by_id() {
        // Node 2 is BEHIND node 9, despite the ids.
1
        let hit = full(&[(2, 5), (9, 0), (4, 2)]);
1
        assert_eq!(hit.hovered_node_ids(), vec![nid(9), nid(4), nid(2)]);
1
        assert_eq!(hit.topmost_node(), Some(nid(9)));
1
    }
    #[test]
1
    fn nothing_under_the_pointer_answers_none() {
1
        let empty = FullHitTest {
1
            hovered_nodes: BTreeMap::new(),
1
            focused_node: OptionDomNodeId::None,
1
        };
1
        assert!(empty.hovered_node_ids().is_empty());
1
        assert_eq!(empty.topmost_node(), None);
        // A dom that was hit-tested but hit nothing is the same answer.
1
        let none_hit = full(&[]);
1
        assert!(none_hit.hovered_node_ids().is_empty());
1
        assert_eq!(none_hit.topmost_node(), None);
1
    }
    /// SCROLLBARS AND CURSOR AREAS ARE NOT "under the pointer" in the sense an
    /// app means - a scrollbar is not the content beneath it - so only the
    /// regular hits are reported.
    #[test]
1
    fn only_regular_hits_count() {
1
        let mut hit = hit_test(&[(1, 0)]);
1
        hit.cursor_hit_test_nodes.insert(
1
            NodeId::new(7),
1
            crate::hit_test::CursorHitTestItem {
1
                cursor_type: crate::hit_test::CursorType::default(),
1
                hit_depth: 0,
1
                point_in_viewport: LogicalPosition::zero(),
1
            },
        );
1
        let mut hovered = BTreeMap::new();
1
        hovered.insert(DomId::ROOT_ID, hit);
1
        let full = FullHitTest {
1
            hovered_nodes: hovered,
1
            focused_node: OptionDomNodeId::None,
1
        };
1
        assert_eq!(full.hovered_node_ids(), vec![nid(1)]);
1
    }
    /// A tie must not flip between frames: the first node at the frontmost
    /// depth wins, and the sort is stable.
    #[test]
1
    fn a_depth_tie_is_resolved_stably() {
1
        let hit = full(&[(1, 0), (2, 0), (3, 0)]);
1
        assert_eq!(hit.topmost_node(), Some(nid(1)));
1
        assert_eq!(hit.hovered_node_ids(), vec![nid(1), nid(2), nid(3)]);
1
    }
}