Rename PositionType and Position in accordance to specs (#295)
1#[test]
2fn absolute_layout_within_border() {
3 use slotmap::Key;
4 #[allow(unused_imports)]
5 use taffy::{layout::Layout, prelude::*};
6 let mut taffy = taffy::Taffy::new();
7 let node0 = taffy
8 .new_leaf(taffy::style::Style {
9 position: taffy::style::Position::Absolute,
10 size: taffy::geometry::Size {
11 width: taffy::style::Dimension::Points(50f32),
12 height: taffy::style::Dimension::Points(50f32),
13 },
14 inset: taffy::geometry::Rect {
15 left: taffy::style::LengthPercentageAuto::Points(0f32),
16 right: auto(),
17 top: taffy::style::LengthPercentageAuto::Points(0f32),
18 bottom: auto(),
19 },
20 ..Default::default()
21 })
22 .unwrap();
23 let node1 = taffy
24 .new_leaf(taffy::style::Style {
25 position: taffy::style::Position::Absolute,
26 size: taffy::geometry::Size {
27 width: taffy::style::Dimension::Points(50f32),
28 height: taffy::style::Dimension::Points(50f32),
29 },
30 inset: taffy::geometry::Rect {
31 left: auto(),
32 right: taffy::style::LengthPercentageAuto::Points(0f32),
33 top: auto(),
34 bottom: taffy::style::LengthPercentageAuto::Points(0f32),
35 },
36 ..Default::default()
37 })
38 .unwrap();
39 let node2 = taffy
40 .new_leaf(taffy::style::Style {
41 position: taffy::style::Position::Absolute,
42 size: taffy::geometry::Size {
43 width: taffy::style::Dimension::Points(50f32),
44 height: taffy::style::Dimension::Points(50f32),
45 },
46 margin: taffy::geometry::Rect {
47 left: taffy::style::LengthPercentageAuto::Points(10f32),
48 right: taffy::style::LengthPercentageAuto::Points(10f32),
49 top: taffy::style::LengthPercentageAuto::Points(10f32),
50 bottom: taffy::style::LengthPercentageAuto::Points(10f32),
51 },
52 inset: taffy::geometry::Rect {
53 left: taffy::style::LengthPercentageAuto::Points(0f32),
54 right: auto(),
55 top: taffy::style::LengthPercentageAuto::Points(0f32),
56 bottom: auto(),
57 },
58 ..Default::default()
59 })
60 .unwrap();
61 let node3 = taffy
62 .new_leaf(taffy::style::Style {
63 position: taffy::style::Position::Absolute,
64 size: taffy::geometry::Size {
65 width: taffy::style::Dimension::Points(50f32),
66 height: taffy::style::Dimension::Points(50f32),
67 },
68 margin: taffy::geometry::Rect {
69 left: taffy::style::LengthPercentageAuto::Points(10f32),
70 right: taffy::style::LengthPercentageAuto::Points(10f32),
71 top: taffy::style::LengthPercentageAuto::Points(10f32),
72 bottom: taffy::style::LengthPercentageAuto::Points(10f32),
73 },
74 inset: taffy::geometry::Rect {
75 left: auto(),
76 right: taffy::style::LengthPercentageAuto::Points(0f32),
77 top: auto(),
78 bottom: taffy::style::LengthPercentageAuto::Points(0f32),
79 },
80 ..Default::default()
81 })
82 .unwrap();
83 let node = taffy
84 .new_with_children(
85 taffy::style::Style {
86 size: taffy::geometry::Size {
87 width: taffy::style::Dimension::Points(100f32),
88 height: taffy::style::Dimension::Points(100f32),
89 },
90 padding: taffy::geometry::Rect {
91 left: taffy::style::LengthPercentage::Points(10f32),
92 right: taffy::style::LengthPercentage::Points(10f32),
93 top: taffy::style::LengthPercentage::Points(10f32),
94 bottom: taffy::style::LengthPercentage::Points(10f32),
95 },
96 border: taffy::geometry::Rect {
97 left: taffy::style::LengthPercentage::Points(10f32),
98 right: taffy::style::LengthPercentage::Points(10f32),
99 top: taffy::style::LengthPercentage::Points(10f32),
100 bottom: taffy::style::LengthPercentage::Points(10f32),
101 },
102 ..Default::default()
103 },
104 &[node0, node1, node2, node3],
105 )
106 .unwrap();
107 taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
108 println!("\nComputed tree:");
109 taffy::debug::print_tree(&taffy, node);
110 println!();
111 let Layout { size, location, .. } = taffy.layout(node).unwrap();
112 assert_eq!(size.width, 100f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 100f32, size.width);
113 assert_eq!(size.height, 100f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 100f32, size.height);
114 assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x);
115 assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y);
116 let Layout { size, location, .. } = taffy.layout(node0).unwrap();
117 assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 50f32, size.width);
118 assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 50f32, size.height);
119 assert_eq!(location.x, 10f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 10f32, location.x);
120 assert_eq!(location.y, 10f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 10f32, location.y);
121 let Layout { size, location, .. } = taffy.layout(node1).unwrap();
122 assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node1.data(), 50f32, size.width);
123 assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node1.data(), 50f32, size.height);
124 assert_eq!(location.x, 40f32, "x of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, location.x);
125 assert_eq!(location.y, 40f32, "y of node {:?}. Expected {}. Actual {}", node1.data(), 40f32, location.y);
126 let Layout { size, location, .. } = taffy.layout(node2).unwrap();
127 assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node2.data(), 50f32, size.width);
128 assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node2.data(), 50f32, size.height);
129 assert_eq!(location.x, 20f32, "x of node {:?}. Expected {}. Actual {}", node2.data(), 20f32, location.x);
130 assert_eq!(location.y, 20f32, "y of node {:?}. Expected {}. Actual {}", node2.data(), 20f32, location.y);
131 let Layout { size, location, .. } = taffy.layout(node3).unwrap();
132 assert_eq!(size.width, 50f32, "width of node {:?}. Expected {}. Actual {}", node3.data(), 50f32, size.width);
133 assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node3.data(), 50f32, size.height);
134 assert_eq!(location.x, 30f32, "x of node {:?}. Expected {}. Actual {}", node3.data(), 30f32, location.x);
135 assert_eq!(location.y, 30f32, "y of node {:?}. Expected {}. Actual {}", node3.data(), 30f32, location.y);
136}