Skip to content

Commit 3009716

Browse files
committed
test: refactor aspect-ratio test
1 parent 244379b commit 3009716

File tree

1 file changed

+48
-150
lines changed

1 file changed

+48
-150
lines changed

float-pigment-forest/tests/custom/css_aspect_ratio.rs

Lines changed: 48 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -5,110 +5,53 @@ use float_pigment_layout::{DefLength, OptionNum, OptionSize};
55
unsafe fn as_ref<'a>(node: *mut Node) -> &'a Node {
66
&*node
77
}
8+
89
#[test]
910
pub fn aspect_ratio_in_flex_row() {
10-
unsafe {
11-
let root = as_ref(Node::new_ptr());
12-
13-
let container = as_ref(Node::new_ptr());
14-
container.set_width(DefLength::Points(Len::from_f32(700.)));
15-
container.set_height(DefLength::Points(Len::from_f32(300.)));
16-
container.set_display(Display::Flex);
17-
container.set_flex_direction(FlexDirection::Row);
18-
root.append_child(convert_node_ref_to_ptr(container));
19-
20-
let child_a = as_ref(Node::new_ptr());
21-
child_a.set_width(DefLength::Points(Len::from_f32(100.)));
22-
child_a.set_height(DefLength::Points(Len::from_f32(100.)));
23-
child_a.set_aspect_ratio(Some(0.75 / 1.));
24-
container.append_child(convert_node_ref_to_ptr(child_a));
25-
26-
let child_b = as_ref(Node::new_ptr());
27-
child_b.set_width(DefLength::Auto);
28-
child_b.set_height(DefLength::Points(Len::from_f32(100.)));
29-
child_b.set_aspect_ratio(Some(0.75 / 1.));
30-
container.append_child(convert_node_ref_to_ptr(child_b));
31-
32-
let child_c = as_ref(Node::new_ptr());
33-
child_c.set_width(DefLength::Points(Len::from_f32(100.)));
34-
child_c.set_height(DefLength::Auto);
35-
child_c.set_aspect_ratio(Some(0.75 / 1.));
36-
container.append_child(convert_node_ref_to_ptr(child_c));
37-
38-
let child_d = as_ref(Node::new_ptr());
39-
child_d.set_width(DefLength::Points(Len::from_f32(100.)));
40-
child_d.set_height(DefLength::Auto);
41-
child_d.set_align_self(AlignSelf::FlexStart);
42-
child_d.set_aspect_ratio(Some(0.75 / 1.));
43-
container.append_child(convert_node_ref_to_ptr(child_d));
44-
45-
root.layout(
46-
OptionSize::new(OptionNum::some(Len::from_f32(800.)), OptionNum::none()),
47-
Size::new(Len::from_f32(0.), Len::from_f32(0.)),
48-
);
49-
println!(
50-
"{}",
51-
root.dump_to_html(
52-
DumpOptions {
53-
recursive: true,
54-
layout: true,
55-
style: DumpStyleMode::None
56-
},
57-
0
58-
)
59-
);
60-
61-
assert_eq!(child_a.layout_position().width, 100.);
62-
assert_eq!(child_a.layout_position().height, 100.);
63-
64-
assert_eq!(child_b.layout_position().width, 75.);
65-
assert_eq!(child_b.layout_position().height, 100.);
66-
67-
assert_eq!(child_c.layout_position().width, 100.);
68-
assert_eq!(child_c.layout_position().height, 300.);
69-
70-
assert_eq!(child_d.layout_position().width, 100.);
71-
assert_eq!(child_d.layout_position().height.round(), 133.);
72-
}
11+
assert_xml!(
12+
r#"
13+
<div style="width: 700px; height: 300px; display: flex; flex-direction: row;">
14+
<div style="width: 100px; height: 100px; aspect-ratio: 0.75 / 1;" expect_width="100" expect_height="100"></div>
15+
<div style="width: auto; height: 100px; aspect-ratio: 0.75 / 1;" expect_width="75" expect_height="100"></div>
16+
<div style="width: 100px; height: auto; aspect-ratio: 0.75 / 1;" expect_width="100" expect_height="300"></div>
17+
<div style="width: 100px; height: auto; aspect-ratio: 0.75 / 1; align-self: flex-start" expect_width="100" expect_height="133"></div>
18+
</div>
19+
"#
20+
)
7321
}
7422

7523
#[test]
7624
pub fn aspect_ratio_in_flex_column() {
25+
assert_xml!(
26+
r#"
27+
<div style="width: 300px; height: 600px; display: flex; flex-direction: column;">
28+
<div style="width: 100px; height: 100px; aspect-ratio: 0.75 / 1;" expect_width="100" expect_height="100"></div>
29+
<div style="width: auto; height: 100px; aspect-ratio: 0.75 / 1;" expect_width="300" expect_height="100"></div>
30+
<div style="width: auto; height: 100px; aspect-ratio: 0.75 / 1; align-self: flex-start" expect_width="75" expect_height="100"></div>
31+
<div style="width: 100px; height: auto; aspect-ratio: 0.75 / 1;" expect_width="100" expect_height="133"></div>
32+
</div>
33+
"#
34+
)
35+
}
36+
37+
#[test]
38+
pub fn aspect_ratio_with_flex_1() {
7739
unsafe {
7840
let root = as_ref(Node::new_ptr());
7941

8042
let container = as_ref(Node::new_ptr());
81-
container.set_width(DefLength::Points(Len::from_f32(300.)));
82-
container.set_height(DefLength::Points(Len::from_f32(600.)));
43+
container.set_width(DefLength::Points(Len::from_f32(100.)));
8344
container.set_display(Display::Flex);
8445
container.set_flex_direction(FlexDirection::Column);
8546
root.append_child(convert_node_ref_to_ptr(container));
8647

8748
let child_a = as_ref(Node::new_ptr());
88-
child_a.set_width(DefLength::Points(Len::from_f32(100.)));
89-
child_a.set_height(DefLength::Points(Len::from_f32(100.)));
90-
child_a.set_aspect_ratio(Some(0.75 / 1.));
49+
child_a.set_width(DefLength::Points(Len::from_f32(50.)));
50+
child_a.set_min_width(DefLength::Points(Len::from_f32(0.)));
51+
child_a.set_flex_basis(DefLength::Percent(0.));
52+
child_a.set_aspect_ratio(Some(1. / 1.));
9153
container.append_child(convert_node_ref_to_ptr(child_a));
9254

93-
let child_b = as_ref(Node::new_ptr());
94-
child_b.set_width(DefLength::Auto);
95-
child_b.set_height(DefLength::Points(Len::from_f32(100.)));
96-
child_b.set_aspect_ratio(Some(0.75 / 1.));
97-
container.append_child(convert_node_ref_to_ptr(child_b));
98-
99-
let child_c = as_ref(Node::new_ptr());
100-
child_c.set_width(DefLength::Auto);
101-
child_c.set_height(DefLength::Points(Len::from_f32(100.)));
102-
child_c.set_aspect_ratio(Some(0.75 / 1.));
103-
child_c.set_align_self(AlignSelf::FlexStart);
104-
container.append_child(convert_node_ref_to_ptr(child_c));
105-
106-
let child_d = as_ref(Node::new_ptr());
107-
child_d.set_width(DefLength::Points(Len::from_f32(100.)));
108-
child_d.set_height(DefLength::Auto);
109-
child_d.set_aspect_ratio(Some(0.75 / 1.));
110-
container.append_child(convert_node_ref_to_ptr(child_d));
111-
11255
root.layout(
11356
OptionSize::new(OptionNum::some(Len::from_f32(375.)), OptionNum::none()),
11457
Size::new(Len::from_f32(0.), Len::from_f32(0.)),
@@ -125,35 +68,30 @@ pub fn aspect_ratio_in_flex_column() {
12568
)
12669
);
12770

128-
assert_eq!(child_a.layout_position().width, 100.);
129-
assert_eq!(child_a.layout_position().height, 100.);
130-
131-
assert_eq!(child_b.layout_position().width, 300.);
132-
assert_eq!(child_b.layout_position().height, 100.);
133-
134-
assert_eq!(child_c.layout_position().width, 75.);
135-
assert_eq!(child_c.layout_position().height, 100.);
136-
137-
assert_eq!(child_d.layout_position().width, 100.);
138-
assert_eq!(child_d.layout_position().height.round(), 133.);
71+
assert_eq!(child_a.layout_position().width, 50.);
72+
assert_eq!(child_a.layout_position().height, 50.);
13973
}
14074
}
14175

14276
#[test]
143-
pub fn aspect_ratio_with_flex_1() {
77+
pub fn aspect_ratio_with_flex_wrap() {
14478
unsafe {
14579
let root = as_ref(Node::new_ptr());
14680

14781
let container = as_ref(Node::new_ptr());
148-
container.set_width(DefLength::Points(Len::from_f32(100.)));
82+
container.set_height(DefLength::Points(Len::from_f32(100.)));
14983
container.set_display(Display::Flex);
15084
container.set_flex_direction(FlexDirection::Column);
85+
container.set_flex_wrap(FlexWrap::Wrap);
86+
container.set_align_items(AlignItems::Stretch);
15187
root.append_child(convert_node_ref_to_ptr(container));
15288

15389
let child_a = as_ref(Node::new_ptr());
154-
child_a.set_width(DefLength::Points(Len::from_f32(50.)));
90+
child_a.set_height(DefLength::Points(Len::from_f32(50.)));
15591
child_a.set_min_width(DefLength::Points(Len::from_f32(0.)));
15692
child_a.set_flex_basis(DefLength::Percent(0.));
93+
child_a.set_flex_shrink(1.);
94+
child_a.set_flex_grow(1.);
15795
child_a.set_aspect_ratio(Some(1. / 1.));
15896
container.append_child(convert_node_ref_to_ptr(child_a));
15997

@@ -173,30 +111,25 @@ pub fn aspect_ratio_with_flex_1() {
173111
)
174112
);
175113

176-
assert_eq!(child_a.layout_position().width, 50.);
177-
assert_eq!(child_a.layout_position().height, 50.);
114+
assert_eq!(child_a.layout_position().width, 100.);
115+
assert_eq!(child_a.layout_position().height, 100.);
178116
}
179117
}
180118

181119
#[test]
182-
pub fn aspect_ratio_with_flex_wrap() {
120+
pub fn aspect_ratio_with_flex_no_wrap_1() {
183121
unsafe {
184122
let root = as_ref(Node::new_ptr());
185123

186124
let container = as_ref(Node::new_ptr());
187-
container.set_height(DefLength::Points(Len::from_f32(100.)));
125+
container.set_height(DefLength::Points(Len::from_f32(50.)));
188126
container.set_display(Display::Flex);
189127
container.set_flex_direction(FlexDirection::Column);
190-
container.set_flex_wrap(FlexWrap::Wrap);
128+
container.set_flex_wrap(FlexWrap::NoWrap);
191129
container.set_align_items(AlignItems::Stretch);
192130
root.append_child(convert_node_ref_to_ptr(container));
193131

194132
let child_a = as_ref(Node::new_ptr());
195-
child_a.set_height(DefLength::Points(Len::from_f32(50.)));
196-
child_a.set_min_width(DefLength::Points(Len::from_f32(0.)));
197-
child_a.set_flex_basis(DefLength::Percent(0.));
198-
child_a.set_flex_shrink(1.);
199-
child_a.set_flex_grow(1.);
200133
child_a.set_aspect_ratio(Some(1. / 1.));
201134
container.append_child(convert_node_ref_to_ptr(child_a));
202135

@@ -216,48 +149,13 @@ pub fn aspect_ratio_with_flex_wrap() {
216149
)
217150
);
218151

219-
assert_eq!(child_a.layout_position().width, 100.);
220-
assert_eq!(child_a.layout_position().height, 100.);
152+
assert_eq!(child_a.layout_position().width, 375.);
153+
assert_eq!(child_a.layout_position().height, 50.);
221154
}
222155
}
223156

224-
// #[test]
225-
// pub fn aspect_ratio_1() {
226-
// unsafe {
227-
// let root = as_ref(Node::new_ptr(Some(0), None));
228-
229-
// let container = as_ref(Node::new_ptr(Some(1), None));
230-
// container.set_height(DefLength::Points(50.));
231-
// container.set_display(Display::Flex);
232-
// container.set_flex_direction(FlexDirection::Column);
233-
// container.set_flex_wrap(FlexWrap::NoWrap);
234-
// container.set_align_items(AlignItems::Stretch);
235-
// root.append_child(container.self_ptr().unwrap());
236-
237-
// let child_a = as_ref(Node::new_ptr(Some(2), None));
238-
// child_a.set_aspect_ratio(AspectRatio::Ratio(Number::F32(1.), Number::F32(1.)));
239-
// container.append_child(child_a.self_ptr().unwrap());
240-
241-
// root.layout(OptionNum::some(375.), OptionNum::none());
242-
// println!(
243-
// "{}",
244-
// root.dump_to_html(
245-
// DumpOptions {
246-
// recursive: true,
247-
// layout: true,
248-
// style: DumpStyleMode::None
249-
// },
250-
// 0
251-
// )
252-
// );
253-
254-
// assert_eq!(child_a.layout_position().width, 375.);
255-
// assert_eq!(child_a.layout_position().height, 50.);
256-
// }
257-
// }
258-
259157
#[test]
260-
pub fn aspect_ratio_2() {
158+
pub fn aspect_ratio_with_flex_no_wrap_2() {
261159
unsafe {
262160
let root = as_ref(Node::new_ptr());
263161

@@ -659,7 +557,7 @@ pub fn aspect_ratio_block_size_with_box_sizing_and_writing_mode() {
659557
}
660558

661559
#[test]
662-
pub fn apsect_ratio_writing_mode_streched() {
560+
pub fn aspect_ratio_writing_mode_stretched() {
663561
assert_xml!(
664562
r#"
665563
<div>

0 commit comments

Comments
 (0)