tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 238Rust 13TOML 3CSS 1146 Definitions

⎇ c2db86dfc600067c6f4d70c8ea398cbcc5d42dbd tests/measure.rs Rust · 449 lines · 15.8 KB History Who touched what raw

Optimise CSS Grid (#338) 6ce28e153 · Nico Burns ·

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