tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 89Markdown 11TOML 91930 Sutichaykuna

⎇ d803b72f958991514d47fae7f877198e126402d1 tests/hand_written/measure.rs Rust · 371 siq'ikuna · 13.6 KB Ñawpa willay Pi imata llamkarqan ch'uya

Comine integration tests into one binary (#951) d803b72f9 · Nico Burns ·

1#[cfg(test)]
2mod measure {
3    use taffy::prelude::*;
4    use taffy_test_helpers::{new_test_tree, test_measure_function, TestNodeContext};
5
6    const HUNDRED_HUNDRED: TestNodeContext = TestNodeContext::fixed(100.0, 100.0);
7    const HUNDRED_FIFTY: TestNodeContext = TestNodeContext::fixed(100.0, 50.0);
8    const FIFTY_FIFTY: TestNodeContext = TestNodeContext::fixed(50.0, 50.0);
9
10    #[test]
11    fn measure_root() {
12        let mut taffy = new_test_tree();
13        let node = taffy.new_leaf_with_context(Style::default(), HUNDRED_HUNDRED).unwrap();
14        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
15
16        assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
17        assert_eq!(taffy.layout(node).unwrap().size.height, 100.0);
18    }
19
20    #[test]
21    fn measure_child() {
22        let mut taffy = new_test_tree();
23
24        let child = taffy.new_leaf_with_context(Style::default(), HUNDRED_HUNDRED).unwrap();
25
26        let node = taffy.new_with_children(Style::default(), &[child]).unwrap();
27        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
28
29        assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
30        assert_eq!(taffy.layout(node).unwrap().size.height, 100.0);
31
32        assert_eq!(taffy.layout(child).unwrap().size.width, 100.0);
33        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
34    }
35
36    #[test]
37    fn measure_child_constraint() {
38        let mut taffy = new_test_tree();
39        let child = taffy.new_leaf_with_context(Style::default(), HUNDRED_HUNDRED).unwrap();
40
41        let node = taffy
42            .new_with_children(
43                Style { size: Size { width: Dimension::from_length(50.0), height: auto() }, ..Default::default() },
44                &[child],
45            )
46            .unwrap();
47
48        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
49
50        // Parent
51        assert_eq!(taffy.layout(node).unwrap().size.width, 50.0);
52        assert_eq!(taffy.layout(node).unwrap().size.height, 100.0);
53        // Child
54        assert_eq!(taffy.layout(child).unwrap().size.width, 100.0);
55        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
56    }
57
58    #[test]
59    fn measure_child_constraint_padding_parent() {
60        let mut taffy = new_test_tree();
61        let child = taffy.new_leaf_with_context(Style::default(), HUNDRED_HUNDRED).unwrap();
62
63        let node = taffy
64            .new_with_children(
65                Style {
66                    size: Size { width: Dimension::from_length(50.0), height: auto() },
67                    padding: Rect {
68                        left: LengthPercentage::from_length(10.0),
69                        right: LengthPercentage::from_length(10.0),
70                        top: LengthPercentage::from_length(10.0),
71                        bottom: LengthPercentage::from_length(10.0),
72                    },
73                    ..Default::default()
74                },
75                &[child],
76            )
77            .unwrap();
78        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
79
80        assert_eq!(taffy.layout(node).unwrap().location.x, 0.0);
81        assert_eq!(taffy.layout(node).unwrap().location.y, 0.0);
82        assert_eq!(taffy.layout(node).unwrap().size.width, 50.0);
83        assert_eq!(taffy.layout(node).unwrap().size.height, 120.0);
84
85        assert_eq!(taffy.layout(child).unwrap().location.x, 10.0);
86        assert_eq!(taffy.layout(child).unwrap().location.y, 10.0);
87        assert_eq!(taffy.layout(child).unwrap().size.width, 100.0);
88        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
89    }
90
91    #[test]
92    fn measure_child_with_flex_grow() {
93        let mut taffy = new_test_tree();
94        let child0 = taffy
95            .new_leaf(Style {
96                size: Size { width: Dimension::from_length(50.0), height: Dimension::from_length(50.0) },
97                ..Default::default()
98            })
99            .unwrap();
100
101        let child1 = taffy.new_leaf_with_context(Style { flex_grow: 1.0, ..Default::default() }, FIFTY_FIFTY).unwrap();
102
103        let node = taffy
104            .new_with_children(
105                Style { size: Size { width: Dimension::from_length(100.0), height: auto() }, ..Default::default() },
106                &[child0, child1],
107            )
108            .unwrap();
109
110        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
111
112        assert_eq!(taffy.layout(child1).unwrap().size.width, 50.0);
113        assert_eq!(taffy.layout(child1).unwrap().size.height, 50.0);
114    }
115
116    #[test]
117    fn measure_child_with_flex_shrink() {
118        let mut taffy = new_test_tree();
119        let child0 = taffy
120            .new_leaf(Style {
121                size: Size { width: Dimension::from_length(50.0), height: Dimension::from_length(50.0) },
122                flex_shrink: 0.0,
123                ..Default::default()
124            })
125            .unwrap();
126
127        let child1 = taffy.new_leaf_with_context(Style::default(), HUNDRED_FIFTY).unwrap();
128
129        let node = taffy
130            .new_with_children(
131                Style { size: Size { width: Dimension::from_length(100.0), height: auto() }, ..Default::default() },
132                &[child0, child1],
133            )
134            .unwrap();
135
136        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
137
138        assert_eq!(taffy.layout(child1).unwrap().size.width, 100.0);
139        assert_eq!(taffy.layout(child1).unwrap().size.height, 50.0);
140    }
141
142    #[test]
143    fn remeasure_child_after_growing() {
144        let mut taffy = new_test_tree();
145        let child0 = taffy
146            .new_leaf(Style {
147                size: Size { width: Dimension::from_length(50.0), height: Dimension::from_length(50.0) },
148                ..Default::default()
149            })
150            .unwrap();
151
152        let child1 = taffy
153            .new_leaf_with_context(
154                Style { flex_grow: 1.0, ..Default::default() },
155                TestNodeContext::aspect_ratio(10.0, 2.0),
156            )
157            .unwrap();
158
159        let node = taffy
160            .new_with_children(
161                Style {
162                    size: Size { width: Dimension::from_length(100.0), height: auto() },
163                    align_items: Some(AlignItems::Start),
164                    ..Default::default()
165                },
166                &[child0, child1],
167            )
168            .unwrap();
169
170        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
171
172        assert_eq!(taffy.layout(child1).unwrap().size.width, 50.0);
173        assert_eq!(taffy.layout(child1).unwrap().size.height, 100.0);
174    }
175
176    #[test]
177    fn remeasure_child_after_shrinking() {
178        let mut taffy = new_test_tree();
179
180        let child0 = taffy
181            .new_leaf(Style {
182                size: Size { width: Dimension::from_length(50.0), height: Dimension::from_length(50.0) },
183                flex_shrink: 0.0,
184                ..Default::default()
185            })
186            .unwrap();
187
188        let child1 = taffy.new_leaf_with_context(Style::default(), TestNodeContext::aspect_ratio(100.0, 2.0)).unwrap();
189
190        let node = taffy
191            .new_with_children(
192                Style {
193                    size: Size { width: Dimension::from_length(100.0), height: auto() },
194                    align_items: Some(AlignItems::Start),
195                    ..Default::default()
196                },
197                &[child0, child1],
198            )
199            .unwrap();
200
201        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
202
203        assert_eq!(taffy.layout(child1).unwrap().size.width, 100.0);
204        assert_eq!(taffy.layout(child1).unwrap().size.height, 200.0);
205    }
206
207    #[test]
208    fn remeasure_child_after_stretching() {
209        let mut taffy: TaffyTree<()> = TaffyTree::new();
210
211        fn custom_measure_function(
212            known_dimensions: Size<Option<f32>>,
213            _available_space: Size<AvailableSpace>,
214            _node_id: NodeId,
215            _node_context: Option<&mut ()>,
216            _style: &Style,
217        ) -> taffy::geometry::Size<f32> {
218            let height = known_dimensions.height.unwrap_or(50.0);
219            let width = known_dimensions.width.unwrap_or(height);
220            Size { width, height }
221        }
222
223        let child = taffy.new_leaf_with_context(Style::default(), ()).unwrap();
224
225        let node = taffy
226            .new_with_children(
227                Style {
228                    size: Size { width: Dimension::from_length(100.0), height: Dimension::from_length(100.0) },
229                    ..Default::default()
230                },
231                &[child],
232            )
233            .unwrap();
234
235        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, custom_measure_function).unwrap();
236
237        assert_eq!(taffy.layout(child).unwrap().size.width, 100.0);
238        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
239    }
240
241    #[test]
242    fn width_overrides_measure() {
243        let mut taffy = new_test_tree();
244        let child = taffy
245            .new_leaf_with_context(
246                Style { size: Size { width: Dimension::from_length(50.0), height: auto() }, ..Default::default() },
247                HUNDRED_HUNDRED,
248            )
249            .unwrap();
250
251        let node = taffy.new_with_children(Style::default(), &[child]).unwrap();
252        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
253
254        assert_eq!(taffy.layout(child).unwrap().size.width, 50.0);
255        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
256    }
257
258    #[test]
259    fn height_overrides_measure() {
260        let mut taffy = new_test_tree();
261        let child = taffy
262            .new_leaf_with_context(
263                Style { size: Size { width: auto(), height: Dimension::from_length(50.0) }, ..Default::default() },
264                HUNDRED_HUNDRED,
265            )
266            .unwrap();
267
268        let node = taffy.new_with_children(Style::default(), &[child]).unwrap();
269        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
270
271        assert_eq!(taffy.layout(child).unwrap().size.width, 100.0);
272        assert_eq!(taffy.layout(child).unwrap().size.height, 50.0);
273    }
274
275    #[test]
276    fn flex_basis_overrides_measure() {
277        let mut taffy = new_test_tree();
278        let child0 = taffy
279            .new_leaf(Style { flex_basis: Dimension::from_length(50.0), flex_grow: 1.0, ..Default::default() })
280            .unwrap();
281
282        let child1 = taffy
283            .new_leaf_with_context(
284                Style { flex_basis: Dimension::from_length(50.0), flex_grow: 1.0, ..Default::default() },
285                HUNDRED_HUNDRED,
286            )
287            .unwrap();
288
289        let node = taffy
290            .new_with_children(
291                Style {
292                    size: Size { width: Dimension::from_length(200.0), height: Dimension::from_length(100.0) },
293                    ..Default::default()
294                },
295                &[child0, child1],
296            )
297            .unwrap();
298
299        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
300
301        assert_eq!(taffy.layout(child0).unwrap().size.width, 100.0);
302        assert_eq!(taffy.layout(child0).unwrap().size.height, 100.0);
303        assert_eq!(taffy.layout(child1).unwrap().size.width, 100.0);
304        assert_eq!(taffy.layout(child1).unwrap().size.height, 100.0);
305    }
306
307    #[test]
308    fn stretch_overrides_measure() {
309        let mut taffy = new_test_tree();
310        let child = taffy.new_leaf_with_context(Style::default(), FIFTY_FIFTY).unwrap();
311
312        let node = taffy
313            .new_with_children(
314                Style {
315                    size: Size { width: Dimension::from_length(100.0), height: Dimension::from_length(100.0) },
316                    ..Default::default()
317                },
318                &[child],
319            )
320            .unwrap();
321
322        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
323
324        assert_eq!(taffy.layout(child).unwrap().size.width, 50.0);
325        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
326    }
327
328    #[test]
329    fn measure_absolute_child() {
330        let mut taffy = new_test_tree();
331        let child = taffy
332            .new_leaf_with_context(Style { position: Position::Absolute, ..Default::default() }, FIFTY_FIFTY)
333            .unwrap();
334
335        let node = taffy
336            .new_with_children(
337                Style {
338                    size: Size { width: Dimension::from_length(100.0), height: Dimension::from_length(100.0) },
339                    ..Default::default()
340                },
341                &[child],
342            )
343            .unwrap();
344
345        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
346
347        assert_eq!(taffy.layout(child).unwrap().size.width, 50.0);
348        assert_eq!(taffy.layout(child).unwrap().size.height, 50.0);
349    }
350
351    #[test]
352    fn ignore_invalid_measure() {
353        let mut taffy = new_test_tree();
354        let child = taffy.new_leaf(Style { flex_grow: 1.0, ..Default::default() }).unwrap();
355
356        let node = taffy
357            .new_with_children(
358                Style {
359                    size: Size { width: Dimension::from_length(100.0), height: Dimension::from_length(100.0) },
360                    ..Default::default()
361                },
362                &[child],
363            )
364            .unwrap();
365
366        taffy.compute_layout_with_measure(node, Size::MAX_CONTENT, test_measure_function).unwrap();
367
368        assert_eq!(taffy.layout(child).unwrap().size.width, 100.0);
369        assert_eq!(taffy.layout(child).unwrap().size.height, 100.0);
370    }
371}