gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 254C 19JavaScript 81412 Sutichaykuna

⎇ cdac14745f9a689d71afd0dcb843b43d3b8b0f44 tests/node.rs Rust · 192 siq'ikuna · 7.5 KB Ñawpa willay Pi imata llamkarqan ch'uya

Add and cleanup new leaf-nodes methods (#181) cdac14745 · Andreas Weibye ·

1#[cfg(test)]
2mod node {
3    use taffy::geometry::*;
4    use taffy::node::{MeasureFunc, Taffy};
5    use taffy::style::*;
6
7    #[test]
8    fn children() {
9        let mut taffy = Taffy::new();
10        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
11        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
12        let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
13
14        assert_eq!(taffy.child_count(node).unwrap(), 2);
15        assert_eq!(taffy.children(node).unwrap()[0], child1);
16        assert_eq!(taffy.children(node).unwrap()[1], child2);
17    }
18
19    #[test]
20    fn set_measure() {
21        let mut taffy = Taffy::new();
22        let node = taffy
23            .new_leaf_with_measure(FlexboxLayout::default(), MeasureFunc::Raw(|_| Size { width: 200.0, height: 200.0 }))
24            .unwrap();
25        taffy.compute_layout(node, Size::undefined()).unwrap();
26        assert_eq!(taffy.layout(node).unwrap().size.width, 200.0);
27
28        taffy.set_measure(node, Some(MeasureFunc::Raw(|_| Size { width: 100.0, height: 100.0 }))).unwrap();
29        taffy.compute_layout(node, Size::undefined()).unwrap();
30        assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
31    }
32
33    #[test]
34    fn add_child() {
35        let mut taffy = Taffy::new();
36        let node = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
37        assert_eq!(taffy.child_count(node).unwrap(), 0);
38
39        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
40        taffy.add_child(node, child1).unwrap();
41        assert_eq!(taffy.child_count(node).unwrap(), 1);
42
43        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
44        taffy.add_child(node, child2).unwrap();
45        assert_eq!(taffy.child_count(node).unwrap(), 2);
46    }
47
48    #[test]
49    fn remove_child() {
50        let mut taffy = Taffy::new();
51
52        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
53        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
54
55        let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
56        assert_eq!(taffy.child_count(node).unwrap(), 2);
57
58        taffy.remove_child(node, child1).unwrap();
59        assert_eq!(taffy.child_count(node).unwrap(), 1);
60        assert_eq!(taffy.children(node).unwrap()[0], child2);
61
62        taffy.remove_child(node, child2).unwrap();
63        assert_eq!(taffy.child_count(node).unwrap(), 0);
64    }
65
66    #[test]
67    fn remove_child_at_index() {
68        let mut taffy = Taffy::new();
69
70        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
71        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
72
73        let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
74        assert_eq!(taffy.child_count(node).unwrap(), 2);
75
76        taffy.remove_child_at_index(node, 0).unwrap();
77        assert_eq!(taffy.child_count(node).unwrap(), 1);
78        assert_eq!(taffy.children(node).unwrap()[0], child2);
79
80        taffy.remove_child_at_index(node, 0).unwrap();
81        assert_eq!(taffy.child_count(node).unwrap(), 0);
82    }
83
84    #[test]
85    fn replace_child_at_index() {
86        let mut taffy = Taffy::new();
87
88        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
89        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
90
91        let node = taffy.new_with_children(FlexboxLayout::default(), &[child1]).unwrap();
92        assert_eq!(taffy.child_count(node).unwrap(), 1);
93        assert_eq!(taffy.children(node).unwrap()[0], child1);
94
95        taffy.replace_child_at_index(node, 0, child2).unwrap();
96        assert_eq!(taffy.child_count(node).unwrap(), 1);
97        assert_eq!(taffy.children(node).unwrap()[0], child2);
98    }
99
100    #[test]
101    fn remove() {
102        let mut taffy = Taffy::new();
103
104        let style2 = FlexboxLayout { flex_direction: FlexDirection::Column, ..FlexboxLayout::default() };
105
106        // Build a linear tree layout: <0> <- <1> <- <2>
107        let node2 = taffy.new_with_children(style2, &[]).unwrap();
108        let node1 = taffy.new_with_children(FlexboxLayout::default(), &[node2]).unwrap();
109        let node0 = taffy.new_with_children(FlexboxLayout::default(), &[node1]).unwrap();
110
111        assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
112
113        // Disconnect the tree: <0> <2>
114        taffy.remove(node1).unwrap();
115
116        assert!(taffy.style(node1).is_err());
117
118        assert!(taffy.children(node0).unwrap().is_empty());
119        assert!(taffy.children(node2).unwrap().is_empty());
120        assert_eq!(taffy.style(node2).unwrap().flex_direction, style2.flex_direction);
121    }
122
123    #[test]
124    fn set_children() {
125        let mut taffy = Taffy::new();
126
127        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
128        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
129        let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
130
131        assert_eq!(taffy.child_count(node).unwrap(), 2);
132        assert_eq!(taffy.children(node).unwrap()[0], child1);
133        assert_eq!(taffy.children(node).unwrap()[1], child2);
134
135        let child3 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
136        let child4 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
137        taffy.set_children(node, &[child3, child4]).unwrap();
138
139        assert_eq!(taffy.child_count(node).unwrap(), 2);
140        assert_eq!(taffy.children(node).unwrap()[0], child3);
141        assert_eq!(taffy.children(node).unwrap()[1], child4);
142    }
143
144    #[test]
145    fn set_style() {
146        let mut taffy = Taffy::new();
147
148        let node = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
149        assert_eq!(taffy.style(node).unwrap().display, Display::Flex);
150
151        taffy.set_style(node, FlexboxLayout { display: Display::None, ..FlexboxLayout::default() }).unwrap();
152        assert_eq!(taffy.style(node).unwrap().display, Display::None);
153    }
154
155    #[test]
156    fn mark_dirty() {
157        let mut taffy = Taffy::new();
158
159        let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
160        let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
161        let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
162
163        taffy.compute_layout(node, taffy::geometry::Size::undefined()).unwrap();
164
165        assert_eq!(taffy.dirty(child1).unwrap(), false);
166        assert_eq!(taffy.dirty(child2).unwrap(), false);
167        assert_eq!(taffy.dirty(node).unwrap(), false);
168
169        taffy.mark_dirty(node).unwrap();
170        assert_eq!(taffy.dirty(child1).unwrap(), false);
171        assert_eq!(taffy.dirty(child2).unwrap(), false);
172        assert_eq!(taffy.dirty(node).unwrap(), true);
173
174        taffy.compute_layout(node, taffy::geometry::Size::undefined()).unwrap();
175        taffy.mark_dirty(child1).unwrap();
176        assert_eq!(taffy.dirty(child1).unwrap(), true);
177        assert_eq!(taffy.dirty(child2).unwrap(), false);
178        assert_eq!(taffy.dirty(node).unwrap(), true);
179    }
180
181    #[test]
182    fn remove_last_node() {
183        let mut taffy = Taffy::new();
184
185        let parent = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
186        let child = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
187        taffy.add_child(parent, child).unwrap();
188
189        taffy.remove(child).unwrap();
190        taffy.remove(parent).unwrap();
191    }
192}