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