1
#[allow(unused_imports)]
2
pub use super::*;
3
#[cfg(test)]
4
mod tests {
5
    use super::*;
6

            
7
14
    const fn p(x: f32, y: f32) -> LogicalPosition {
8
14
        LogicalPosition { x, y }
9
14
    }
10

            
11
    #[test]
12
1
    fn the_full_chain_reproduces_the_raster_equation() {
13
        // The raster paints inline position g at `P + E + g - S - A`.
14
        // Feeding that window position through the chain must return g.
15
1
        let node_origin = p(100.0, 50.0); // P
16
1
        let ancestor = ScrollOffset::new(7.0, 13.0); // A
17
1
        let own = ScrollOffset::new(30.0, 4.0); // S
18
1
        let inset = ContentInset::new(6.0, 3.0); // E
19
1
        let g = p(12.0, 9.0);
20

            
21
1
        let window = WindowPoint::new(p(
22
1
            node_origin.x + inset.left + g.x - own.get().x - ancestor.get().x,
23
1
            node_origin.y + inset.top + g.y - own.get().y - ancestor.get().y,
24
        ));
25

            
26
1
        let content = window
27
1
            .to_static_layout(ancestor)
28
1
            .to_border_box_local(node_origin)
29
1
            .to_content_box_local(inset)
30
1
            .scrolled_by(own);
31

            
32
1
        assert_eq!(content.get(), g);
33
1
    }
34

            
35
    #[test]
36
1
    fn every_conversion_round_trips() {
37
1
        let w = WindowPoint::new(p(3.5, -8.25));
38
1
        let a = ScrollOffset::new(11.0, 2.0);
39
1
        let origin = p(-4.0, 60.0);
40
1
        let inset = ContentInset::new(5.0, 5.0);
41
1
        let own = ScrollOffset::new(90.0, 0.5);
42

            
43
1
        let s = w.to_static_layout(a);
44
1
        assert_eq!(s.to_window(a).get(), w.get());
45

            
46
1
        let b = s.to_border_box_local(origin);
47
1
        assert_eq!(b.to_static_layout(origin).get(), s.get());
48

            
49
1
        let c = b.to_content_box_local(inset);
50
1
        assert_eq!(c.to_border_box_local(inset).get(), b.get());
51

            
52
1
        let sc = c.scrolled_by(own);
53
1
        assert_eq!(sc.unscrolled_by(own).get(), c.get());
54
1
    }
55

            
56
    #[test]
57
1
    fn a_zero_inset_makes_border_and_content_boxes_agree() {
58
        // Why the WebRender/CPU divergence stayed latent: the TextInput's
59
        // value <p> sets neither padding nor border.
60
1
        let b = BorderBoxLocal::new(p(17.0, 4.0));
61
1
        assert_eq!(b.to_content_box_local(ContentInset::ZERO).get(), b.get());
62
1
    }
63

            
64
    #[test]
65
1
    fn own_scroll_shifts_the_content_point_right_by_the_offset() {
66
        // A field scrolled 40px right: the pointer sitting on the box's left
67
        // edge is 40px into the CONTENT.
68
1
        let c = ContentBoxLocal::new(p(0.0, 5.0));
69
1
        let scrolled = c.scrolled_by(ScrollOffset::new(40.0, 0.0));
70
1
        assert_eq!(scrolled.get(), p(40.0, 5.0));
71
1
    }
72

            
73
    #[test]
74
1
    fn scroll_offsets_accumulate() {
75
1
        let total = ScrollOffset::new(1.0, 2.0).plus(ScrollOffset::new(10.0, 20.0));
76
1
        assert_eq!(total.get(), p(11.0, 22.0));
77
1
        assert_eq!(ScrollOffset::zero().plus(total).get(), total.get());
78
1
    }
79

            
80
    #[test]
81
1
    fn clamping_keeps_the_point_inside_the_block() {
82
1
        let sc = ScrolledContentPoint::new(p(-5.0, 900.0));
83
1
        assert_eq!(sc.clamp_to(100.0, 40.0).get(), p(0.0, 40.0));
84
        // A degenerate (negative) size clamps to the origin rather than
85
        // panicking on an inverted range.
86
1
        assert_eq!(sc.clamp_to(-1.0, -1.0).get(), p(0.0, 0.0));
87
1
    }
88

            
89
    #[test]
90
1
    fn inclusivity_is_explicit() {
91
1
        assert!(Inclusivity::SelfAndAncestors.includes_self());
92
1
        assert!(!Inclusivity::AncestorsOnly.includes_self());
93
1
    }
94

            
95
    #[test]
96
1
    fn the_newtypes_are_free() {
97
        use core::mem::{align_of, size_of};
98
1
        assert_eq!(size_of::<WindowPoint>(), size_of::<LogicalPosition>());
99
1
        assert_eq!(size_of::<StaticLayoutPoint>(), size_of::<LogicalPosition>());
100
1
        assert_eq!(size_of::<BorderBoxLocal>(), size_of::<LogicalPosition>());
101
1
        assert_eq!(size_of::<ContentBoxLocal>(), size_of::<LogicalPosition>());
102
1
        assert_eq!(
103
            size_of::<ScrolledContentPoint>(),
104
            size_of::<LogicalPosition>()
105
        );
106
1
        assert_eq!(size_of::<ScrollOffset>(), size_of::<LogicalPosition>());
107
1
        assert_eq!(
108
            align_of::<ScrolledContentPoint>(),
109
            align_of::<LogicalPosition>()
110
        );
111
1
    }
112

            
113
    #[test]
114
1
    fn nan_and_infinity_pass_through_without_panicking() {
115
1
        let w = WindowPoint::new(p(f32::NAN, f32::INFINITY));
116
1
        let out = w
117
1
            .to_static_layout(ScrollOffset::new(1.0, 1.0))
118
1
            .to_border_box_local(p(1.0, 1.0))
119
1
            .to_content_box_local(ContentInset::new(1.0, 1.0))
120
1
            .scrolled_by(ScrollOffset::new(1.0, 1.0));
121
1
        assert!(out.x().is_nan());
122
1
        assert!(out.y().is_infinite());
123
1
    }
124
}