1#[cfg(test)]
2mod caching {
3 use taffy::node::MeasureFunc;
4 use taffy::prelude::*;
5
6 #[test]
7 fn measure_count_flexbox() {
8 use std::sync::atomic::{AtomicU32, Ordering};
9
10 let mut taffy = Taffy::new();
11 static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
12
13 let grandchild = taffy
14 .new_leaf_with_measure(
15 Style { ..Default::default() },
16 MeasureFunc::Raw(|known_dimensions, _available_space| {
17 NUM_MEASURES.fetch_add(1, Ordering::SeqCst);
18 Size {
19 width: known_dimensions.width.unwrap_or(50.0),
20 height: known_dimensions.height.unwrap_or(50.0),
21 }
22 }),
23 )
24 .unwrap();
25
26 let child = taffy.new_with_children(Style::DEFAULT, &[grandchild]).unwrap();
27
28 let node = taffy.new_with_children(Style::DEFAULT, &[child]).unwrap();
29 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
30
31 assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 2);
32 }
33
34 #[test]
35 #[cfg(feature = "grid")]
36 fn measure_count_grid() {
37 use std::sync::atomic::{AtomicU32, Ordering};
38
39 let style = || Style { display: Display::Grid, ..Default::default() };
40
41 let mut taffy = Taffy::new();
42 static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
43
44 let grandchild = taffy
45 .new_leaf_with_measure(
46 style(),
47 MeasureFunc::Raw(|known_dimensions, _available_space| {
48 NUM_MEASURES.fetch_add(1, Ordering::SeqCst);
49 Size {
50 width: known_dimensions.width.unwrap_or(50.0),
51 height: known_dimensions.height.unwrap_or(50.0),
52 }
53 }),
54 )
55 .unwrap();
56
57 let child = taffy.new_with_children(style(), &[grandchild]).unwrap();
58
59 let node = taffy.new_with_children(style(), &[child]).unwrap();
60
61 taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
62 assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 2);
63 }
64}