⎇ 41b87b578c1918acbd991665fc37cd61e5d24f22
tests/generated.rs
Ñawpa willay
Pi imata llamkarqan
ch'uya
1#[cfg(test)]
2mod generated {
3 #[test]
4 fn justify_content_row_space_around() {
5 let layout = stretch::compute(&stretch::style::Node {
6 justify_content: stretch::style::JustifyContent::SpaceAround,
7 width: stretch::style::Dimension::Points(100.0),
8 height: stretch::style::Dimension::Points(100.0),
9 children: vec![
10 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
11 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
12 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
13 ],
14 ..Default::default()
15 });
16
17 assert_eq!(layout.width, 100.0);
18 assert_eq!(layout.height, 100.0);
19 assert_eq!(layout.x, 0.0);
20 assert_eq!(layout.y, 0.0);
21
22 assert_eq!(layout.children[0].width, 10.0);
23 assert_eq!(layout.children[0].height, 100.0);
24 assert_eq!(layout.children[0].x, 12.0);
25 assert_eq!(layout.children[0].y, 0.0);
26
27 assert_eq!(layout.children[1].width, 10.0);
28 assert_eq!(layout.children[1].height, 100.0);
29 assert_eq!(layout.children[1].x, 45.0);
30 assert_eq!(layout.children[1].y, 0.0);
31
32 assert_eq!(layout.children[2].width, 10.0);
33 assert_eq!(layout.children[2].height, 100.0);
34 assert_eq!(layout.children[2].x, 78.0);
35 assert_eq!(layout.children[2].y, 0.0);
36 }
37
38 #[test]
39 fn align_items_center() {
40 let layout = stretch::compute(&stretch::style::Node {
41 align_items: stretch::style::AlignItems::Center,
42 width: stretch::style::Dimension::Points(100.0),
43 height: stretch::style::Dimension::Points(100.0),
44 children: vec![stretch::style::Node {
45 width: stretch::style::Dimension::Points(10.0),
46 height: stretch::style::Dimension::Points(10.0),
47 ..Default::default()
48 }],
49 ..Default::default()
50 });
51
52 assert_eq!(layout.width, 100.0);
53 assert_eq!(layout.height, 100.0);
54 assert_eq!(layout.x, 0.0);
55 assert_eq!(layout.y, 0.0);
56
57 assert_eq!(layout.children[0].width, 10.0);
58 assert_eq!(layout.children[0].height, 10.0);
59 assert_eq!(layout.children[0].x, 0.0);
60 assert_eq!(layout.children[0].y, 45.0);
61 }
62
63 #[test]
64 fn absolute_layout_justify_content_center() {
65 let layout = stretch::compute(&stretch::style::Node {
66 justify_content: stretch::style::JustifyContent::Center,
67 width: stretch::style::Dimension::Points(110.0),
68 height: stretch::style::Dimension::Points(100.0),
69 children: vec![stretch::style::Node {
70 position: stretch::style::Position::Absolute,
71 width: stretch::style::Dimension::Points(60.0),
72 height: stretch::style::Dimension::Points(40.0),
73 ..Default::default()
74 }],
75 ..Default::default()
76 });
77
78 assert_eq!(layout.width, 110.0);
79 assert_eq!(layout.height, 100.0);
80 assert_eq!(layout.x, 0.0);
81 assert_eq!(layout.y, 0.0);
82
83 assert_eq!(layout.children[0].width, 60.0);
84 assert_eq!(layout.children[0].height, 40.0);
85 assert_eq!(layout.children[0].x, 25.0);
86 assert_eq!(layout.children[0].y, 0.0);
87 }
88
89 #[test]
90 fn margin_auto_bottom_and_top() {
91 let layout = stretch::compute(&stretch::style::Node {
92 align_items: stretch::style::AlignItems::Center,
93 width: stretch::style::Dimension::Points(200.0),
94 height: stretch::style::Dimension::Points(200.0),
95 children: vec![
96 stretch::style::Node {
97 width: stretch::style::Dimension::Points(50.0),
98 height: stretch::style::Dimension::Points(50.0),
99 margin: stretch::style::Edges {
100 top: stretch::style::Dimension::Auto,
101 bottom: stretch::style::Dimension::Auto,
102 ..Default::default()
103 },
104 ..Default::default()
105 },
106 stretch::style::Node {
107 width: stretch::style::Dimension::Points(50.0),
108 height: stretch::style::Dimension::Points(50.0),
109 ..Default::default()
110 },
111 ],
112 ..Default::default()
113 });
114
115 assert_eq!(layout.width, 200.0);
116 assert_eq!(layout.height, 200.0);
117 assert_eq!(layout.x, 0.0);
118 assert_eq!(layout.y, 0.0);
119
120 assert_eq!(layout.children[0].width, 50.0);
121 assert_eq!(layout.children[0].height, 50.0);
122 assert_eq!(layout.children[0].x, 0.0);
123 assert_eq!(layout.children[0].y, 75.0);
124
125 assert_eq!(layout.children[1].width, 50.0);
126 assert_eq!(layout.children[1].height, 50.0);
127 assert_eq!(layout.children[1].x, 50.0);
128 assert_eq!(layout.children[1].y, 75.0);
129 }
130
131 #[test]
132 fn margin_auto_left_and_right_strech() {
133 let layout = stretch::compute(&stretch::style::Node {
134 width: stretch::style::Dimension::Points(200.0),
135 height: stretch::style::Dimension::Points(200.0),
136 children: vec![
137 stretch::style::Node {
138 width: stretch::style::Dimension::Points(50.0),
139 height: stretch::style::Dimension::Points(50.0),
140 margin: stretch::style::Edges {
141 start: stretch::style::Dimension::Auto,
142 end: stretch::style::Dimension::Auto,
143 ..Default::default()
144 },
145 ..Default::default()
146 },
147 stretch::style::Node {
148 width: stretch::style::Dimension::Points(50.0),
149 height: stretch::style::Dimension::Points(50.0),
150 ..Default::default()
151 },
152 ],
153 ..Default::default()
154 });
155
156 assert_eq!(layout.width, 200.0);
157 assert_eq!(layout.height, 200.0);
158 assert_eq!(layout.x, 0.0);
159 assert_eq!(layout.y, 0.0);
160
161 assert_eq!(layout.children[0].width, 50.0);
162 assert_eq!(layout.children[0].height, 50.0);
163 assert_eq!(layout.children[0].x, 50.0);
164 assert_eq!(layout.children[0].y, 0.0);
165
166 assert_eq!(layout.children[1].width, 50.0);
167 assert_eq!(layout.children[1].height, 50.0);
168 assert_eq!(layout.children[1].x, 150.0);
169 assert_eq!(layout.children[1].y, 0.0);
170 }
171
172 #[test]
173 fn rounding_flex_basis_overrides_main_size() {
174 let layout = stretch::compute(&stretch::style::Node {
175 flex_direction: stretch::style::FlexDirection::Column,
176 width: stretch::style::Dimension::Points(100.0),
177 height: stretch::style::Dimension::Points(113.0),
178 children: vec![
179 stretch::style::Node {
180 flex_grow: 1.0,
181 flex_basis: stretch::style::Dimension::Points(50.0),
182 height: stretch::style::Dimension::Points(20.0),
183 ..Default::default()
184 },
185 stretch::style::Node {
186 flex_grow: 1.0,
187 height: stretch::style::Dimension::Points(10.0),
188 ..Default::default()
189 },
190 stretch::style::Node {
191 flex_grow: 1.0,
192 height: stretch::style::Dimension::Points(10.0),
193 ..Default::default()
194 },
195 ],
196 ..Default::default()
197 });
198
199 assert_eq!(layout.width, 100.0);
200 assert_eq!(layout.height, 113.0);
201 assert_eq!(layout.x, 0.0);
202 assert_eq!(layout.y, 0.0);
203
204 assert_eq!(layout.children[0].width, 100.0);
205 assert_eq!(layout.children[0].height, 64.0);
206 assert_eq!(layout.children[0].x, 0.0);
207 assert_eq!(layout.children[0].y, 0.0);
208
209 assert_eq!(layout.children[1].width, 100.0);
210 assert_eq!(layout.children[1].height, 25.0);
211 assert_eq!(layout.children[1].x, 0.0);
212 assert_eq!(layout.children[1].y, 64.0);
213
214 assert_eq!(layout.children[2].width, 100.0);
215 assert_eq!(layout.children[2].height, 24.0);
216 assert_eq!(layout.children[2].x, 0.0);
217 assert_eq!(layout.children[2].y, 89.0);
218 }
219
220 #[test]
221 fn percentage_flex_basis() {
222 let layout = stretch::compute(&stretch::style::Node {
223 width: stretch::style::Dimension::Points(200.0),
224 height: stretch::style::Dimension::Points(200.0),
225 children: vec![
226 stretch::style::Node {
227 flex_grow: 1.0,
228 flex_basis: stretch::style::Dimension::Percent(0.5),
229 ..Default::default()
230 },
231 stretch::style::Node {
232 flex_grow: 1.0,
233 flex_basis: stretch::style::Dimension::Percent(0.2),
234 ..Default::default()
235 },
236 ],
237 ..Default::default()
238 });
239
240 assert_eq!(layout.width, 200.0);
241 assert_eq!(layout.height, 200.0);
242 assert_eq!(layout.x, 0.0);
243 assert_eq!(layout.y, 0.0);
244
245 assert_eq!(layout.children[0].width, 125.0);
246 assert_eq!(layout.children[0].height, 200.0);
247 assert_eq!(layout.children[0].x, 0.0);
248 assert_eq!(layout.children[0].y, 0.0);
249
250 assert_eq!(layout.children[1].width, 75.0);
251 assert_eq!(layout.children[1].height, 200.0);
252 assert_eq!(layout.children[1].x, 125.0);
253 assert_eq!(layout.children[1].y, 0.0);
254 }
255
256 #[test]
257 fn rounding_fractial_input_3() {
258 let layout = stretch::compute(&stretch::style::Node {
259 flex_direction: stretch::style::FlexDirection::Column,
260 width: stretch::style::Dimension::Points(100.0),
261 height: stretch::style::Dimension::Points(113.4),
262 children: vec![
263 stretch::style::Node {
264 flex_grow: 1.0,
265 flex_basis: stretch::style::Dimension::Points(50.0),
266 height: stretch::style::Dimension::Points(20.0),
267 ..Default::default()
268 },
269 stretch::style::Node {
270 flex_grow: 1.0,
271 height: stretch::style::Dimension::Points(10.0),
272 ..Default::default()
273 },
274 stretch::style::Node {
275 flex_grow: 1.0,
276 height: stretch::style::Dimension::Points(10.0),
277 ..Default::default()
278 },
279 ],
280 ..Default::default()
281 });
282
283 assert_eq!(layout.width, 100.0);
284 assert_eq!(layout.height, 113.0);
285 assert_eq!(layout.x, 0.0);
286 assert_eq!(layout.y, 0.0);
287
288 assert_eq!(layout.children[0].width, 100.0);
289 assert_eq!(layout.children[0].height, 64.0);
290 assert_eq!(layout.children[0].x, 0.0);
291 assert_eq!(layout.children[0].y, 0.0);
292
293 assert_eq!(layout.children[1].width, 100.0);
294 assert_eq!(layout.children[1].height, 25.0);
295 assert_eq!(layout.children[1].x, 0.0);
296 assert_eq!(layout.children[1].y, 64.0);
297
298 assert_eq!(layout.children[2].width, 100.0);
299 assert_eq!(layout.children[2].height, 24.0);
300 assert_eq!(layout.children[2].x, 0.0);
301 assert_eq!(layout.children[2].y, 89.0);
302 }
303
304 #[test]
305 fn margin_and_stretch_column() {
306 let layout = stretch::compute(&stretch::style::Node {
307 flex_direction: stretch::style::FlexDirection::Column,
308 width: stretch::style::Dimension::Points(100.0),
309 height: stretch::style::Dimension::Points(100.0),
310 children: vec![stretch::style::Node {
311 flex_grow: 1.0,
312 margin: stretch::style::Edges {
313 start: stretch::style::Dimension::Points(10.0),
314 end: stretch::style::Dimension::Points(10.0),
315 ..Default::default()
316 },
317 ..Default::default()
318 }],
319 ..Default::default()
320 });
321
322 assert_eq!(layout.width, 100.0);
323 assert_eq!(layout.height, 100.0);
324 assert_eq!(layout.x, 0.0);
325 assert_eq!(layout.y, 0.0);
326
327 assert_eq!(layout.children[0].width, 80.0);
328 assert_eq!(layout.children[0].height, 100.0);
329 assert_eq!(layout.children[0].x, 10.0);
330 assert_eq!(layout.children[0].y, 0.0);
331 }
332
333 #[test]
334 fn wrapped_column_max_height_flex() {
335 let layout = stretch::compute(&stretch::style::Node {
336 flex_direction: stretch::style::FlexDirection::Column,
337 align_items: stretch::style::AlignItems::Center,
338 align_content: stretch::style::AlignContent::Center,
339 justify_content: stretch::style::JustifyContent::Center,
340 width: stretch::style::Dimension::Points(700.0),
341 height: stretch::style::Dimension::Points(500.0),
342 children: vec![
343 stretch::style::Node {
344 flex_grow: 1.0,
345 flex_shrink: 1.0,
346 flex_basis: stretch::style::Dimension::Percent(0.0),
347 width: stretch::style::Dimension::Points(100.0),
348 height: stretch::style::Dimension::Points(500.0),
349 max_height: stretch::style::Dimension::Points(200.0),
350 ..Default::default()
351 },
352 stretch::style::Node {
353 flex_grow: 1.0,
354 flex_shrink: 1.0,
355 flex_basis: stretch::style::Dimension::Percent(0.0),
356 width: stretch::style::Dimension::Points(200.0),
357 height: stretch::style::Dimension::Points(200.0),
358 margin: stretch::style::Edges {
359 start: stretch::style::Dimension::Points(20.0),
360 end: stretch::style::Dimension::Points(20.0),
361 top: stretch::style::Dimension::Points(20.0),
362 bottom: stretch::style::Dimension::Points(20.0),
363 ..Default::default()
364 },
365 ..Default::default()
366 },
367 stretch::style::Node {
368 width: stretch::style::Dimension::Points(100.0),
369 height: stretch::style::Dimension::Points(100.0),
370 ..Default::default()
371 },
372 ],
373 ..Default::default()
374 });
375
376 assert_eq!(layout.width, 700.0);
377 assert_eq!(layout.height, 500.0);
378 assert_eq!(layout.x, 0.0);
379 assert_eq!(layout.y, 0.0);
380
381 assert_eq!(layout.children[0].width, 100.0);
382 assert_eq!(layout.children[0].height, 180.0);
383 assert_eq!(layout.children[0].x, 300.0);
384 assert_eq!(layout.children[0].y, 0.0);
385
386 assert_eq!(layout.children[1].width, 200.0);
387 assert_eq!(layout.children[1].height, 180.0);
388 assert_eq!(layout.children[1].x, 250.0);
389 assert_eq!(layout.children[1].y, 200.0);
390
391 assert_eq!(layout.children[2].width, 100.0);
392 assert_eq!(layout.children[2].height, 100.0);
393 assert_eq!(layout.children[2].x, 300.0);
394 assert_eq!(layout.children[2].y, 400.0);
395 }
396
397 #[test]
398 fn align_baseline_nested_child() {
399 let layout = stretch::compute(&stretch::style::Node {
400 align_items: stretch::style::AlignItems::Baseline,
401 width: stretch::style::Dimension::Points(100.0),
402 height: stretch::style::Dimension::Points(100.0),
403 children: vec![
404 stretch::style::Node {
405 width: stretch::style::Dimension::Points(50.0),
406 height: stretch::style::Dimension::Points(50.0),
407 ..Default::default()
408 },
409 stretch::style::Node {
410 flex_direction: stretch::style::FlexDirection::Column,
411 width: stretch::style::Dimension::Points(50.0),
412 height: stretch::style::Dimension::Points(20.0),
413 children: vec![stretch::style::Node {
414 width: stretch::style::Dimension::Points(50.0),
415 height: stretch::style::Dimension::Points(10.0),
416 ..Default::default()
417 }],
418 ..Default::default()
419 },
420 ],
421 ..Default::default()
422 });
423
424 assert_eq!(layout.width, 100.0);
425 assert_eq!(layout.height, 100.0);
426 assert_eq!(layout.x, 0.0);
427 assert_eq!(layout.y, 0.0);
428
429 assert_eq!(layout.children[0].width, 50.0);
430 assert_eq!(layout.children[0].height, 50.0);
431 assert_eq!(layout.children[0].x, 0.0);
432 assert_eq!(layout.children[0].y, 0.0);
433
434 assert_eq!(layout.children[1].width, 50.0);
435 assert_eq!(layout.children[1].height, 20.0);
436 assert_eq!(layout.children[1].x, 50.0);
437 assert_eq!(layout.children[1].y, 40.0);
438
439 assert_eq!(layout.children[1].children[0].width, 50.0);
440 assert_eq!(layout.children[1].children[0].height, 10.0);
441 assert_eq!(layout.children[1].children[0].x, 0.0);
442 assert_eq!(layout.children[1].children[0].y, 0.0);
443 }
444
445 #[test]
446 fn max_height_overrides_height_on_root() {
447 let layout = stretch::compute(&stretch::style::Node {
448 height: stretch::style::Dimension::Points(200.0),
449 max_height: stretch::style::Dimension::Points(100.0),
450 ..Default::default()
451 });
452
453 assert_eq!(layout.width, 0.0);
454 assert_eq!(layout.height, 100.0);
455 assert_eq!(layout.x, 0.0);
456 assert_eq!(layout.y, 0.0);
457 }
458
459 #[test]
460 fn padding_center_child() {
461 let layout = stretch::compute(&stretch::style::Node {
462 align_items: stretch::style::AlignItems::Center,
463 justify_content: stretch::style::JustifyContent::Center,
464 width: stretch::style::Dimension::Points(100.0),
465 height: stretch::style::Dimension::Points(100.0),
466 padding: stretch::style::Edges {
467 start: stretch::style::Dimension::Points(10.0),
468 end: stretch::style::Dimension::Points(20.0),
469 top: stretch::style::Dimension::Points(10.0),
470 bottom: stretch::style::Dimension::Points(20.0),
471 ..Default::default()
472 },
473 children: vec![stretch::style::Node {
474 width: stretch::style::Dimension::Points(10.0),
475 height: stretch::style::Dimension::Points(10.0),
476 ..Default::default()
477 }],
478 ..Default::default()
479 });
480
481 assert_eq!(layout.width, 100.0);
482 assert_eq!(layout.height, 100.0);
483 assert_eq!(layout.x, 0.0);
484 assert_eq!(layout.y, 0.0);
485
486 assert_eq!(layout.children[0].width, 10.0);
487 assert_eq!(layout.children[0].height, 10.0);
488 assert_eq!(layout.children[0].x, 40.0);
489 assert_eq!(layout.children[0].y, 40.0);
490 }
491
492 #[test]
493 fn align_flex_start_with_stretching_children() {
494 let layout = stretch::compute(&stretch::style::Node {
495 width: stretch::style::Dimension::Points(500.0),
496 height: stretch::style::Dimension::Points(500.0),
497 children: vec![stretch::style::Node {
498 children: vec![stretch::style::Node {
499 flex_grow: 1.0,
500 flex_shrink: 1.0,
501 children: vec![stretch::style::Node { flex_grow: 1.0, flex_shrink: 1.0, ..Default::default() }],
502 ..Default::default()
503 }],
504 ..Default::default()
505 }],
506 ..Default::default()
507 });
508
509 assert_eq!(layout.width, 500.0);
510 assert_eq!(layout.height, 500.0);
511 assert_eq!(layout.x, 0.0);
512 assert_eq!(layout.y, 0.0);
513
514 assert_eq!(layout.children[0].width, 0.0);
515 assert_eq!(layout.children[0].height, 500.0);
516 assert_eq!(layout.children[0].x, 0.0);
517 assert_eq!(layout.children[0].y, 0.0);
518
519 assert_eq!(layout.children[0].children[0].width, 0.0);
520 assert_eq!(layout.children[0].children[0].height, 500.0);
521 assert_eq!(layout.children[0].children[0].x, 0.0);
522 assert_eq!(layout.children[0].children[0].y, 0.0);
523
524 assert_eq!(layout.children[0].children[0].children[0].width, 0.0);
525 assert_eq!(layout.children[0].children[0].children[0].height, 500.0);
526 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
527 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
528 }
529
530 #[test]
531 fn min_width_overrides_width() {
532 let layout = stretch::compute(&stretch::style::Node {
533 children: vec![stretch::style::Node {
534 width: stretch::style::Dimension::Points(50.0),
535 min_width: stretch::style::Dimension::Points(100.0),
536 ..Default::default()
537 }],
538 ..Default::default()
539 });
540
541 assert_eq!(layout.width, 100.0);
542 assert_eq!(layout.height, 0.0);
543 assert_eq!(layout.x, 0.0);
544 assert_eq!(layout.y, 0.0);
545
546 assert_eq!(layout.children[0].width, 100.0);
547 assert_eq!(layout.children[0].height, 0.0);
548 assert_eq!(layout.children[0].x, 0.0);
549 assert_eq!(layout.children[0].y, 0.0);
550 }
551
552 #[test]
553 fn flex_wrap_align_stretch_fits_one_row() {
554 let layout = stretch::compute(&stretch::style::Node {
555 width: stretch::style::Dimension::Points(150.0),
556 height: stretch::style::Dimension::Points(100.0),
557 children: vec![
558 stretch::style::Node { width: stretch::style::Dimension::Points(50.0), ..Default::default() },
559 stretch::style::Node { width: stretch::style::Dimension::Points(50.0), ..Default::default() },
560 ],
561 ..Default::default()
562 });
563
564 assert_eq!(layout.width, 150.0);
565 assert_eq!(layout.height, 100.0);
566 assert_eq!(layout.x, 0.0);
567 assert_eq!(layout.y, 0.0);
568
569 assert_eq!(layout.children[0].width, 50.0);
570 assert_eq!(layout.children[0].height, 100.0);
571 assert_eq!(layout.children[0].x, 0.0);
572 assert_eq!(layout.children[0].y, 0.0);
573
574 assert_eq!(layout.children[1].width, 50.0);
575 assert_eq!(layout.children[1].height, 100.0);
576 assert_eq!(layout.children[1].x, 50.0);
577 assert_eq!(layout.children[1].y, 0.0);
578 }
579
580 #[test]
581 fn justify_content_row_space_evenly() {
582 let layout = stretch::compute(&stretch::style::Node {
583 justify_content: stretch::style::JustifyContent::SpaceEvenly,
584 width: stretch::style::Dimension::Points(100.0),
585 height: stretch::style::Dimension::Points(100.0),
586 children: vec![
587 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
588 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
589 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
590 ],
591 ..Default::default()
592 });
593
594 assert_eq!(layout.width, 100.0);
595 assert_eq!(layout.height, 100.0);
596 assert_eq!(layout.x, 0.0);
597 assert_eq!(layout.y, 0.0);
598
599 assert_eq!(layout.children[0].width, 0.0);
600 assert_eq!(layout.children[0].height, 10.0);
601 assert_eq!(layout.children[0].x, 25.0);
602 assert_eq!(layout.children[0].y, 0.0);
603
604 assert_eq!(layout.children[1].width, 0.0);
605 assert_eq!(layout.children[1].height, 10.0);
606 assert_eq!(layout.children[1].x, 50.0);
607 assert_eq!(layout.children[1].y, 0.0);
608
609 assert_eq!(layout.children[2].width, 0.0);
610 assert_eq!(layout.children[2].height, 10.0);
611 assert_eq!(layout.children[2].x, 75.0);
612 assert_eq!(layout.children[2].y, 0.0);
613 }
614
615 #[test]
616 fn absolute_layout_percentage_bottom_based_on_parent_height() {
617 let layout = stretch::compute(&stretch::style::Node {
618 width: stretch::style::Dimension::Points(100.0),
619 height: stretch::style::Dimension::Points(200.0),
620 children: vec![
621 stretch::style::Node {
622 position: stretch::style::Position::Absolute,
623 width: stretch::style::Dimension::Points(10.0),
624 height: stretch::style::Dimension::Points(10.0),
625 top: stretch::style::Dimension::Percent(0.5),
626 ..Default::default()
627 },
628 stretch::style::Node {
629 position: stretch::style::Position::Absolute,
630 width: stretch::style::Dimension::Points(10.0),
631 height: stretch::style::Dimension::Points(10.0),
632 bottom: stretch::style::Dimension::Percent(0.5),
633 ..Default::default()
634 },
635 stretch::style::Node {
636 position: stretch::style::Position::Absolute,
637 width: stretch::style::Dimension::Points(10.0),
638 top: stretch::style::Dimension::Percent(0.1),
639 bottom: stretch::style::Dimension::Percent(0.1),
640 ..Default::default()
641 },
642 ],
643 ..Default::default()
644 });
645
646 assert_eq!(layout.width, 100.0);
647 assert_eq!(layout.height, 200.0);
648 assert_eq!(layout.x, 0.0);
649 assert_eq!(layout.y, 0.0);
650
651 assert_eq!(layout.children[0].width, 10.0);
652 assert_eq!(layout.children[0].height, 10.0);
653 assert_eq!(layout.children[0].x, 0.0);
654 assert_eq!(layout.children[0].y, 100.0);
655
656 assert_eq!(layout.children[1].width, 10.0);
657 assert_eq!(layout.children[1].height, 10.0);
658 assert_eq!(layout.children[1].x, 0.0);
659 assert_eq!(layout.children[1].y, 90.0);
660
661 assert_eq!(layout.children[2].width, 10.0);
662 assert_eq!(layout.children[2].height, 160.0);
663 assert_eq!(layout.children[2].x, 0.0);
664 assert_eq!(layout.children[2].y, 20.0);
665 }
666
667 #[test]
668 fn absolute_layout_align_items_and_justify_content_flex_end() {
669 let layout = stretch::compute(&stretch::style::Node {
670 align_items: stretch::style::AlignItems::FlexEnd,
671 justify_content: stretch::style::JustifyContent::FlexEnd,
672 width: stretch::style::Dimension::Points(110.0),
673 height: stretch::style::Dimension::Points(100.0),
674 children: vec![stretch::style::Node {
675 position: stretch::style::Position::Absolute,
676 width: stretch::style::Dimension::Points(60.0),
677 height: stretch::style::Dimension::Points(40.0),
678 ..Default::default()
679 }],
680 ..Default::default()
681 });
682
683 assert_eq!(layout.width, 110.0);
684 assert_eq!(layout.height, 100.0);
685 assert_eq!(layout.x, 0.0);
686 assert_eq!(layout.y, 0.0);
687
688 assert_eq!(layout.children[0].width, 60.0);
689 assert_eq!(layout.children[0].height, 40.0);
690 assert_eq!(layout.children[0].x, 50.0);
691 assert_eq!(layout.children[0].y, 60.0);
692 }
693
694 #[test]
695 fn nested_overflowing_child() {
696 let layout = stretch::compute(&stretch::style::Node {
697 width: stretch::style::Dimension::Points(100.0),
698 height: stretch::style::Dimension::Points(100.0),
699 children: vec![stretch::style::Node {
700 children: vec![stretch::style::Node {
701 width: stretch::style::Dimension::Points(200.0),
702 height: stretch::style::Dimension::Points(200.0),
703 ..Default::default()
704 }],
705 ..Default::default()
706 }],
707 ..Default::default()
708 });
709
710 assert_eq!(layout.width, 100.0);
711 assert_eq!(layout.height, 100.0);
712 assert_eq!(layout.x, 0.0);
713 assert_eq!(layout.y, 0.0);
714
715 assert_eq!(layout.children[0].width, 200.0);
716 assert_eq!(layout.children[0].height, 100.0);
717 assert_eq!(layout.children[0].x, 0.0);
718 assert_eq!(layout.children[0].y, 0.0);
719
720 assert_eq!(layout.children[0].children[0].width, 200.0);
721 assert_eq!(layout.children[0].children[0].height, 200.0);
722 assert_eq!(layout.children[0].children[0].x, 0.0);
723 assert_eq!(layout.children[0].children[0].y, 0.0);
724 }
725
726 #[test]
727 fn flex_direction_row() {
728 let layout = stretch::compute(&stretch::style::Node {
729 width: stretch::style::Dimension::Points(100.0),
730 height: stretch::style::Dimension::Points(100.0),
731 children: vec![
732 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
733 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
734 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
735 ],
736 ..Default::default()
737 });
738
739 assert_eq!(layout.width, 100.0);
740 assert_eq!(layout.height, 100.0);
741 assert_eq!(layout.x, 0.0);
742 assert_eq!(layout.y, 0.0);
743
744 assert_eq!(layout.children[0].width, 10.0);
745 assert_eq!(layout.children[0].height, 100.0);
746 assert_eq!(layout.children[0].x, 0.0);
747 assert_eq!(layout.children[0].y, 0.0);
748
749 assert_eq!(layout.children[1].width, 10.0);
750 assert_eq!(layout.children[1].height, 100.0);
751 assert_eq!(layout.children[1].x, 10.0);
752 assert_eq!(layout.children[1].y, 0.0);
753
754 assert_eq!(layout.children[2].width, 10.0);
755 assert_eq!(layout.children[2].height, 100.0);
756 assert_eq!(layout.children[2].x, 20.0);
757 assert_eq!(layout.children[2].y, 0.0);
758 }
759
760 #[test]
761 fn child_min_max_width_flexing() {
762 let layout = stretch::compute(&stretch::style::Node {
763 width: stretch::style::Dimension::Points(120.0),
764 height: stretch::style::Dimension::Points(50.0),
765 children: vec![
766 stretch::style::Node {
767 flex_grow: 1.0,
768 flex_shrink: 0.0,
769 flex_basis: stretch::style::Dimension::Points(0.0),
770 min_width: stretch::style::Dimension::Points(60.0),
771 ..Default::default()
772 },
773 stretch::style::Node {
774 flex_grow: 1.0,
775 flex_shrink: 0.0,
776 flex_basis: stretch::style::Dimension::Percent(0.5),
777 max_width: stretch::style::Dimension::Points(20.0),
778 ..Default::default()
779 },
780 ],
781 ..Default::default()
782 });
783
784 assert_eq!(layout.width, 120.0);
785 assert_eq!(layout.height, 50.0);
786 assert_eq!(layout.x, 0.0);
787 assert_eq!(layout.y, 0.0);
788
789 assert_eq!(layout.children[0].width, 100.0);
790 assert_eq!(layout.children[0].height, 50.0);
791 assert_eq!(layout.children[0].x, 0.0);
792 assert_eq!(layout.children[0].y, 0.0);
793
794 assert_eq!(layout.children[1].width, 20.0);
795 assert_eq!(layout.children[1].height, 50.0);
796 assert_eq!(layout.children[1].x, 100.0);
797 assert_eq!(layout.children[1].y, 0.0);
798 }
799
800 #[test]
801 fn absolute_layout_in_wrap_reverse_row_container_flex_end() {
802 let layout = stretch::compute(&stretch::style::Node {
803 width: stretch::style::Dimension::Points(100.0),
804 height: stretch::style::Dimension::Points(100.0),
805 children: vec![stretch::style::Node {
806 position: stretch::style::Position::Absolute,
807 align_self: stretch::style::AlignSelf::FlexEnd,
808 width: stretch::style::Dimension::Points(20.0),
809 height: stretch::style::Dimension::Points(20.0),
810 ..Default::default()
811 }],
812 ..Default::default()
813 });
814
815 assert_eq!(layout.width, 100.0);
816 assert_eq!(layout.height, 100.0);
817 assert_eq!(layout.x, 0.0);
818 assert_eq!(layout.y, 0.0);
819
820 assert_eq!(layout.children[0].width, 20.0);
821 assert_eq!(layout.children[0].height, 20.0);
822 assert_eq!(layout.children[0].x, 0.0);
823 assert_eq!(layout.children[0].y, 0.0);
824 }
825
826 #[test]
827 fn margin_and_flex_column() {
828 let layout = stretch::compute(&stretch::style::Node {
829 flex_direction: stretch::style::FlexDirection::Column,
830 width: stretch::style::Dimension::Points(100.0),
831 height: stretch::style::Dimension::Points(100.0),
832 children: vec![stretch::style::Node {
833 flex_grow: 1.0,
834 margin: stretch::style::Edges {
835 top: stretch::style::Dimension::Points(10.0),
836 bottom: stretch::style::Dimension::Points(10.0),
837 ..Default::default()
838 },
839 ..Default::default()
840 }],
841 ..Default::default()
842 });
843
844 assert_eq!(layout.width, 100.0);
845 assert_eq!(layout.height, 100.0);
846 assert_eq!(layout.x, 0.0);
847 assert_eq!(layout.y, 0.0);
848
849 assert_eq!(layout.children[0].width, 100.0);
850 assert_eq!(layout.children[0].height, 80.0);
851 assert_eq!(layout.children[0].x, 0.0);
852 assert_eq!(layout.children[0].y, 10.0);
853 }
854
855 #[test]
856 fn flex_basis_flex_shrink_row() {
857 let layout = stretch::compute(&stretch::style::Node {
858 width: stretch::style::Dimension::Points(100.0),
859 height: stretch::style::Dimension::Points(100.0),
860 children: vec![
861 stretch::style::Node { flex_basis: stretch::style::Dimension::Points(100.0), ..Default::default() },
862 stretch::style::Node { flex_basis: stretch::style::Dimension::Points(50.0), ..Default::default() },
863 ],
864 ..Default::default()
865 });
866
867 assert_eq!(layout.width, 100.0);
868 assert_eq!(layout.height, 100.0);
869 assert_eq!(layout.x, 0.0);
870 assert_eq!(layout.y, 0.0);
871
872 assert_eq!(layout.children[0].width, 67.0);
873 assert_eq!(layout.children[0].height, 100.0);
874 assert_eq!(layout.children[0].x, 0.0);
875 assert_eq!(layout.children[0].y, 0.0);
876
877 assert_eq!(layout.children[1].width, 33.0);
878 assert_eq!(layout.children[1].height, 100.0);
879 assert_eq!(layout.children[1].x, 67.0);
880 assert_eq!(layout.children[1].y, 0.0);
881 }
882
883 #[test]
884 fn justify_content_overflow_min_max() {
885 let layout = stretch::compute(&stretch::style::Node {
886 flex_direction: stretch::style::FlexDirection::Column,
887 justify_content: stretch::style::JustifyContent::Center,
888 min_height: stretch::style::Dimension::Points(100.0),
889 max_height: stretch::style::Dimension::Points(110.0),
890 children: vec![
891 stretch::style::Node {
892 width: stretch::style::Dimension::Points(50.0),
893 height: stretch::style::Dimension::Points(50.0),
894 ..Default::default()
895 },
896 stretch::style::Node {
897 width: stretch::style::Dimension::Points(50.0),
898 height: stretch::style::Dimension::Points(50.0),
899 ..Default::default()
900 },
901 stretch::style::Node {
902 width: stretch::style::Dimension::Points(50.0),
903 height: stretch::style::Dimension::Points(50.0),
904 ..Default::default()
905 },
906 ],
907 ..Default::default()
908 });
909
910 assert_eq!(layout.width, 50.0);
911 assert_eq!(layout.height, 110.0);
912 assert_eq!(layout.x, 0.0);
913 assert_eq!(layout.y, 0.0);
914
915 assert_eq!(layout.children[0].width, 50.0);
916 assert_eq!(layout.children[0].height, 37.0);
917 assert_eq!(layout.children[0].x, 0.0);
918 assert_eq!(layout.children[0].y, 0.0);
919
920 assert_eq!(layout.children[1].width, 50.0);
921 assert_eq!(layout.children[1].height, 36.0);
922 assert_eq!(layout.children[1].x, 0.0);
923 assert_eq!(layout.children[1].y, 37.0);
924
925 assert_eq!(layout.children[2].width, 50.0);
926 assert_eq!(layout.children[2].height, 37.0);
927 assert_eq!(layout.children[2].x, 0.0);
928 assert_eq!(layout.children[2].y, 73.0);
929 }
930
931 #[test]
932 fn wrap_reverse_column_fixed_size() {
933 let layout = stretch::compute(&stretch::style::Node {
934 flex_direction: stretch::style::FlexDirection::Column,
935 align_items: stretch::style::AlignItems::Center,
936 width: stretch::style::Dimension::Points(200.0),
937 height: stretch::style::Dimension::Points(100.0),
938 children: vec![
939 stretch::style::Node {
940 width: stretch::style::Dimension::Points(30.0),
941 height: stretch::style::Dimension::Points(10.0),
942 ..Default::default()
943 },
944 stretch::style::Node {
945 width: stretch::style::Dimension::Points(30.0),
946 height: stretch::style::Dimension::Points(20.0),
947 ..Default::default()
948 },
949 stretch::style::Node {
950 width: stretch::style::Dimension::Points(30.0),
951 height: stretch::style::Dimension::Points(30.0),
952 ..Default::default()
953 },
954 stretch::style::Node {
955 width: stretch::style::Dimension::Points(30.0),
956 height: stretch::style::Dimension::Points(40.0),
957 ..Default::default()
958 },
959 stretch::style::Node {
960 width: stretch::style::Dimension::Points(30.0),
961 height: stretch::style::Dimension::Points(50.0),
962 ..Default::default()
963 },
964 ],
965 ..Default::default()
966 });
967
968 assert_eq!(layout.width, 200.0);
969 assert_eq!(layout.height, 100.0);
970 assert_eq!(layout.x, 0.0);
971 assert_eq!(layout.y, 0.0);
972
973 assert_eq!(layout.children[0].width, 30.0);
974 assert_eq!(layout.children[0].height, 10.0);
975 assert_eq!(layout.children[0].x, 135.0);
976 assert_eq!(layout.children[0].y, 0.0);
977
978 assert_eq!(layout.children[1].width, 30.0);
979 assert_eq!(layout.children[1].height, 20.0);
980 assert_eq!(layout.children[1].x, 135.0);
981 assert_eq!(layout.children[1].y, 10.0);
982
983 assert_eq!(layout.children[2].width, 30.0);
984 assert_eq!(layout.children[2].height, 30.0);
985 assert_eq!(layout.children[2].x, 135.0);
986 assert_eq!(layout.children[2].y, 30.0);
987
988 assert_eq!(layout.children[3].width, 30.0);
989 assert_eq!(layout.children[3].height, 40.0);
990 assert_eq!(layout.children[3].x, 135.0);
991 assert_eq!(layout.children[3].y, 60.0);
992
993 assert_eq!(layout.children[4].width, 30.0);
994 assert_eq!(layout.children[4].height, 50.0);
995 assert_eq!(layout.children[4].x, 35.0);
996 assert_eq!(layout.children[4].y, 0.0);
997 }
998
999 #[test]
1000 fn max_width() {
1001 let layout = stretch::compute(&stretch::style::Node {
1002 flex_direction: stretch::style::FlexDirection::Column,
1003 width: stretch::style::Dimension::Points(100.0),
1004 height: stretch::style::Dimension::Points(100.0),
1005 children: vec![stretch::style::Node {
1006 max_width: stretch::style::Dimension::Points(50.0),
1007 height: stretch::style::Dimension::Points(10.0),
1008 ..Default::default()
1009 }],
1010 ..Default::default()
1011 });
1012
1013 assert_eq!(layout.width, 100.0);
1014 assert_eq!(layout.height, 100.0);
1015 assert_eq!(layout.x, 0.0);
1016 assert_eq!(layout.y, 0.0);
1017
1018 assert_eq!(layout.children[0].width, 50.0);
1019 assert_eq!(layout.children[0].height, 10.0);
1020 assert_eq!(layout.children[0].x, 0.0);
1021 assert_eq!(layout.children[0].y, 0.0);
1022 }
1023
1024 #[test]
1025 fn justify_content_row_center() {
1026 let layout = stretch::compute(&stretch::style::Node {
1027 justify_content: stretch::style::JustifyContent::Center,
1028 width: stretch::style::Dimension::Points(100.0),
1029 height: stretch::style::Dimension::Points(100.0),
1030 children: vec![
1031 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
1032 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
1033 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
1034 ],
1035 ..Default::default()
1036 });
1037
1038 assert_eq!(layout.width, 100.0);
1039 assert_eq!(layout.height, 100.0);
1040 assert_eq!(layout.x, 0.0);
1041 assert_eq!(layout.y, 0.0);
1042
1043 assert_eq!(layout.children[0].width, 10.0);
1044 assert_eq!(layout.children[0].height, 100.0);
1045 assert_eq!(layout.children[0].x, 35.0);
1046 assert_eq!(layout.children[0].y, 0.0);
1047
1048 assert_eq!(layout.children[1].width, 10.0);
1049 assert_eq!(layout.children[1].height, 100.0);
1050 assert_eq!(layout.children[1].x, 45.0);
1051 assert_eq!(layout.children[1].y, 0.0);
1052
1053 assert_eq!(layout.children[2].width, 10.0);
1054 assert_eq!(layout.children[2].height, 100.0);
1055 assert_eq!(layout.children[2].x, 55.0);
1056 assert_eq!(layout.children[2].y, 0.0);
1057 }
1058
1059 #[test]
1060 fn flex_grow_root_minimized() {
1061 let layout = stretch::compute(&stretch::style::Node {
1062 flex_direction: stretch::style::FlexDirection::Column,
1063 width: stretch::style::Dimension::Points(100.0),
1064 min_height: stretch::style::Dimension::Points(100.0),
1065 max_height: stretch::style::Dimension::Points(500.0),
1066 children: vec![stretch::style::Node {
1067 flex_direction: stretch::style::FlexDirection::Column,
1068 flex_grow: 1.0,
1069 min_height: stretch::style::Dimension::Points(100.0),
1070 max_height: stretch::style::Dimension::Points(500.0),
1071 children: vec![
1072 stretch::style::Node {
1073 flex_grow: 1.0,
1074 flex_basis: stretch::style::Dimension::Points(200.0),
1075 ..Default::default()
1076 },
1077 stretch::style::Node { height: stretch::style::Dimension::Points(100.0), ..Default::default() },
1078 ],
1079 ..Default::default()
1080 }],
1081 ..Default::default()
1082 });
1083
1084 assert_eq!(layout.width, 100.0);
1085 assert_eq!(layout.height, 300.0);
1086 assert_eq!(layout.x, 0.0);
1087 assert_eq!(layout.y, 0.0);
1088
1089 assert_eq!(layout.children[0].width, 100.0);
1090 assert_eq!(layout.children[0].height, 300.0);
1091 assert_eq!(layout.children[0].x, 0.0);
1092 assert_eq!(layout.children[0].y, 0.0);
1093
1094 assert_eq!(layout.children[0].children[0].width, 100.0);
1095 assert_eq!(layout.children[0].children[0].height, 200.0);
1096 assert_eq!(layout.children[0].children[0].x, 0.0);
1097 assert_eq!(layout.children[0].children[0].y, 0.0);
1098
1099 assert_eq!(layout.children[0].children[1].width, 100.0);
1100 assert_eq!(layout.children[0].children[1].height, 100.0);
1101 assert_eq!(layout.children[0].children[1].x, 0.0);
1102 assert_eq!(layout.children[0].children[1].y, 200.0);
1103 }
1104
1105 #[test]
1106 fn container_with_unsized_child() {
1107 let layout = stretch::compute(&stretch::style::Node {
1108 width: stretch::style::Dimension::Points(100.0),
1109 height: stretch::style::Dimension::Points(100.0),
1110 children: vec![stretch::style::Node { ..Default::default() }],
1111 ..Default::default()
1112 });
1113
1114 assert_eq!(layout.width, 100.0);
1115 assert_eq!(layout.height, 100.0);
1116 assert_eq!(layout.x, 0.0);
1117 assert_eq!(layout.y, 0.0);
1118
1119 assert_eq!(layout.children[0].width, 0.0);
1120 assert_eq!(layout.children[0].height, 100.0);
1121 assert_eq!(layout.children[0].x, 0.0);
1122 assert_eq!(layout.children[0].y, 0.0);
1123 }
1124
1125 #[test]
1126 fn flex_grow_within_constrained_min_row() {
1127 let layout = stretch::compute(&stretch::style::Node {
1128 min_width: stretch::style::Dimension::Points(100.0),
1129 height: stretch::style::Dimension::Points(100.0),
1130 children: vec![
1131 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
1132 stretch::style::Node { width: stretch::style::Dimension::Points(50.0), ..Default::default() },
1133 ],
1134 ..Default::default()
1135 });
1136
1137 assert_eq!(layout.width, 100.0);
1138 assert_eq!(layout.height, 100.0);
1139 assert_eq!(layout.x, 0.0);
1140 assert_eq!(layout.y, 0.0);
1141
1142 assert_eq!(layout.children[0].width, 50.0);
1143 assert_eq!(layout.children[0].height, 100.0);
1144 assert_eq!(layout.children[0].x, 0.0);
1145 assert_eq!(layout.children[0].y, 0.0);
1146
1147 assert_eq!(layout.children[1].width, 50.0);
1148 assert_eq!(layout.children[1].height, 100.0);
1149 assert_eq!(layout.children[1].x, 50.0);
1150 assert_eq!(layout.children[1].y, 0.0);
1151 }
1152
1153 #[test]
1154 fn absolute_layout_width_height_start_top_end_bottom() {
1155 let layout = stretch::compute(&stretch::style::Node {
1156 width: stretch::style::Dimension::Points(100.0),
1157 height: stretch::style::Dimension::Points(100.0),
1158 children: vec![stretch::style::Node {
1159 position: stretch::style::Position::Absolute,
1160 width: stretch::style::Dimension::Points(10.0),
1161 height: stretch::style::Dimension::Points(10.0),
1162 start: stretch::style::Dimension::Points(10.0),
1163 end: stretch::style::Dimension::Points(10.0),
1164 top: stretch::style::Dimension::Points(10.0),
1165 bottom: stretch::style::Dimension::Points(10.0),
1166 ..Default::default()
1167 }],
1168 ..Default::default()
1169 });
1170
1171 assert_eq!(layout.width, 100.0);
1172 assert_eq!(layout.height, 100.0);
1173 assert_eq!(layout.x, 0.0);
1174 assert_eq!(layout.y, 0.0);
1175
1176 assert_eq!(layout.children[0].width, 10.0);
1177 assert_eq!(layout.children[0].height, 10.0);
1178 assert_eq!(layout.children[0].x, 10.0);
1179 assert_eq!(layout.children[0].y, 10.0);
1180 }
1181
1182 #[test]
1183 fn overflow_main_axis() {
1184 let layout = stretch::compute(&stretch::style::Node {
1185 width: stretch::style::Dimension::Points(100.0),
1186 height: stretch::style::Dimension::Points(100.0),
1187 children: vec![stretch::style::Node {
1188 flex_shrink: 0.0,
1189 width: stretch::style::Dimension::Points(200.0),
1190 ..Default::default()
1191 }],
1192 ..Default::default()
1193 });
1194
1195 assert_eq!(layout.width, 100.0);
1196 assert_eq!(layout.height, 100.0);
1197 assert_eq!(layout.x, 0.0);
1198 assert_eq!(layout.y, 0.0);
1199
1200 assert_eq!(layout.children[0].width, 200.0);
1201 assert_eq!(layout.children[0].height, 100.0);
1202 assert_eq!(layout.children[0].x, 0.0);
1203 assert_eq!(layout.children[0].y, 0.0);
1204 }
1205
1206 #[test]
1207 fn flex_grow_to_min() {
1208 let layout = stretch::compute(&stretch::style::Node {
1209 flex_direction: stretch::style::FlexDirection::Column,
1210 width: stretch::style::Dimension::Points(100.0),
1211 min_height: stretch::style::Dimension::Points(100.0),
1212 max_height: stretch::style::Dimension::Points(500.0),
1213 children: vec![
1214 stretch::style::Node { flex_grow: 1.0, flex_shrink: 1.0, ..Default::default() },
1215 stretch::style::Node { height: stretch::style::Dimension::Points(50.0), ..Default::default() },
1216 ],
1217 ..Default::default()
1218 });
1219
1220 assert_eq!(layout.width, 100.0);
1221 assert_eq!(layout.height, 100.0);
1222 assert_eq!(layout.x, 0.0);
1223 assert_eq!(layout.y, 0.0);
1224
1225 assert_eq!(layout.children[0].width, 100.0);
1226 assert_eq!(layout.children[0].height, 50.0);
1227 assert_eq!(layout.children[0].x, 0.0);
1228 assert_eq!(layout.children[0].y, 0.0);
1229
1230 assert_eq!(layout.children[1].width, 100.0);
1231 assert_eq!(layout.children[1].height, 50.0);
1232 assert_eq!(layout.children[1].x, 0.0);
1233 assert_eq!(layout.children[1].y, 50.0);
1234 }
1235
1236 #[test]
1237 fn absolute_layout_align_items_and_justify_content_center_and_top_position() {
1238 let layout = stretch::compute(&stretch::style::Node {
1239 align_items: stretch::style::AlignItems::Center,
1240 justify_content: stretch::style::JustifyContent::Center,
1241 width: stretch::style::Dimension::Points(110.0),
1242 height: stretch::style::Dimension::Points(100.0),
1243 children: vec![stretch::style::Node {
1244 position: stretch::style::Position::Absolute,
1245 width: stretch::style::Dimension::Points(60.0),
1246 height: stretch::style::Dimension::Points(40.0),
1247 top: stretch::style::Dimension::Points(10.0),
1248 ..Default::default()
1249 }],
1250 ..Default::default()
1251 });
1252
1253 assert_eq!(layout.width, 110.0);
1254 assert_eq!(layout.height, 100.0);
1255 assert_eq!(layout.x, 0.0);
1256 assert_eq!(layout.y, 0.0);
1257
1258 assert_eq!(layout.children[0].width, 60.0);
1259 assert_eq!(layout.children[0].height, 40.0);
1260 assert_eq!(layout.children[0].x, 25.0);
1261 assert_eq!(layout.children[0].y, 10.0);
1262 }
1263
1264 #[test]
1265 fn min_height() {
1266 let layout = stretch::compute(&stretch::style::Node {
1267 flex_direction: stretch::style::FlexDirection::Column,
1268 width: stretch::style::Dimension::Points(100.0),
1269 height: stretch::style::Dimension::Points(100.0),
1270 children: vec![
1271 stretch::style::Node {
1272 flex_grow: 1.0,
1273 min_height: stretch::style::Dimension::Points(60.0),
1274 ..Default::default()
1275 },
1276 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
1277 ],
1278 ..Default::default()
1279 });
1280
1281 assert_eq!(layout.width, 100.0);
1282 assert_eq!(layout.height, 100.0);
1283 assert_eq!(layout.x, 0.0);
1284 assert_eq!(layout.y, 0.0);
1285
1286 assert_eq!(layout.children[0].width, 100.0);
1287 assert_eq!(layout.children[0].height, 80.0);
1288 assert_eq!(layout.children[0].x, 0.0);
1289 assert_eq!(layout.children[0].y, 0.0);
1290
1291 assert_eq!(layout.children[1].width, 100.0);
1292 assert_eq!(layout.children[1].height, 20.0);
1293 assert_eq!(layout.children[1].x, 0.0);
1294 assert_eq!(layout.children[1].y, 80.0);
1295 }
1296
1297 #[test]
1298 fn justify_content_column_flex_start() {
1299 let layout = stretch::compute(&stretch::style::Node {
1300 flex_direction: stretch::style::FlexDirection::Column,
1301 width: stretch::style::Dimension::Points(100.0),
1302 height: stretch::style::Dimension::Points(100.0),
1303 children: vec![
1304 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
1305 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
1306 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
1307 ],
1308 ..Default::default()
1309 });
1310
1311 assert_eq!(layout.width, 100.0);
1312 assert_eq!(layout.height, 100.0);
1313 assert_eq!(layout.x, 0.0);
1314 assert_eq!(layout.y, 0.0);
1315
1316 assert_eq!(layout.children[0].width, 100.0);
1317 assert_eq!(layout.children[0].height, 10.0);
1318 assert_eq!(layout.children[0].x, 0.0);
1319 assert_eq!(layout.children[0].y, 0.0);
1320
1321 assert_eq!(layout.children[1].width, 100.0);
1322 assert_eq!(layout.children[1].height, 10.0);
1323 assert_eq!(layout.children[1].x, 0.0);
1324 assert_eq!(layout.children[1].y, 10.0);
1325
1326 assert_eq!(layout.children[2].width, 100.0);
1327 assert_eq!(layout.children[2].height, 10.0);
1328 assert_eq!(layout.children[2].x, 0.0);
1329 assert_eq!(layout.children[2].y, 20.0);
1330 }
1331
1332 #[test]
1333 fn absolute_layout_in_wrap_reverse_column_container() {
1334 let layout = stretch::compute(&stretch::style::Node {
1335 flex_direction: stretch::style::FlexDirection::Column,
1336 width: stretch::style::Dimension::Points(100.0),
1337 height: stretch::style::Dimension::Points(100.0),
1338 children: vec![stretch::style::Node {
1339 position: stretch::style::Position::Absolute,
1340 width: stretch::style::Dimension::Points(20.0),
1341 height: stretch::style::Dimension::Points(20.0),
1342 ..Default::default()
1343 }],
1344 ..Default::default()
1345 });
1346
1347 assert_eq!(layout.width, 100.0);
1348 assert_eq!(layout.height, 100.0);
1349 assert_eq!(layout.x, 0.0);
1350 assert_eq!(layout.y, 0.0);
1351
1352 assert_eq!(layout.children[0].width, 20.0);
1353 assert_eq!(layout.children[0].height, 20.0);
1354 assert_eq!(layout.children[0].x, 80.0);
1355 assert_eq!(layout.children[0].y, 0.0);
1356 }
1357
1358 #[test]
1359 fn margin_should_not_be_part_of_max_width() {
1360 let layout = stretch::compute(&stretch::style::Node {
1361 width: stretch::style::Dimension::Points(250.0),
1362 height: stretch::style::Dimension::Points(250.0),
1363 children: vec![stretch::style::Node {
1364 width: stretch::style::Dimension::Points(100.0),
1365 max_width: stretch::style::Dimension::Points(100.0),
1366 height: stretch::style::Dimension::Points(100.0),
1367 margin: stretch::style::Edges { start: stretch::style::Dimension::Points(20.0), ..Default::default() },
1368 ..Default::default()
1369 }],
1370 ..Default::default()
1371 });
1372
1373 assert_eq!(layout.width, 250.0);
1374 assert_eq!(layout.height, 250.0);
1375 assert_eq!(layout.x, 0.0);
1376 assert_eq!(layout.y, 0.0);
1377
1378 assert_eq!(layout.children[0].width, 100.0);
1379 assert_eq!(layout.children[0].height, 100.0);
1380 assert_eq!(layout.children[0].x, 20.0);
1381 assert_eq!(layout.children[0].y, 0.0);
1382 }
1383
1384 #[test]
1385 fn border_flex_child() {
1386 let layout = stretch::compute(&stretch::style::Node {
1387 width: stretch::style::Dimension::Points(100.0),
1388 height: stretch::style::Dimension::Points(100.0),
1389 border: stretch::style::Edges {
1390 start: stretch::style::Dimension::Points(10.0),
1391 end: stretch::style::Dimension::Points(10.0),
1392 top: stretch::style::Dimension::Points(10.0),
1393 bottom: stretch::style::Dimension::Points(10.0),
1394 ..Default::default()
1395 },
1396 children: vec![stretch::style::Node {
1397 flex_grow: 1.0,
1398 width: stretch::style::Dimension::Points(10.0),
1399 ..Default::default()
1400 }],
1401 ..Default::default()
1402 });
1403
1404 assert_eq!(layout.width, 100.0);
1405 assert_eq!(layout.height, 100.0);
1406 assert_eq!(layout.x, 0.0);
1407 assert_eq!(layout.y, 0.0);
1408
1409 assert_eq!(layout.children[0].width, 80.0);
1410 assert_eq!(layout.children[0].height, 80.0);
1411 assert_eq!(layout.children[0].x, 10.0);
1412 assert_eq!(layout.children[0].y, 10.0);
1413 }
1414
1415 #[test]
1416 fn margin_right() {
1417 let layout = stretch::compute(&stretch::style::Node {
1418 justify_content: stretch::style::JustifyContent::FlexEnd,
1419 width: stretch::style::Dimension::Points(100.0),
1420 height: stretch::style::Dimension::Points(100.0),
1421 children: vec![stretch::style::Node {
1422 width: stretch::style::Dimension::Points(10.0),
1423 margin: stretch::style::Edges { end: stretch::style::Dimension::Points(10.0), ..Default::default() },
1424 ..Default::default()
1425 }],
1426 ..Default::default()
1427 });
1428
1429 assert_eq!(layout.width, 100.0);
1430 assert_eq!(layout.height, 100.0);
1431 assert_eq!(layout.x, 0.0);
1432 assert_eq!(layout.y, 0.0);
1433
1434 assert_eq!(layout.children[0].width, 10.0);
1435 assert_eq!(layout.children[0].height, 100.0);
1436 assert_eq!(layout.children[0].x, 80.0);
1437 assert_eq!(layout.children[0].y, 0.0);
1438 }
1439
1440 #[test]
1441 fn align_flex_start_with_shrinking_children_with_stretch() {
1442 let layout = stretch::compute(&stretch::style::Node {
1443 width: stretch::style::Dimension::Points(500.0),
1444 height: stretch::style::Dimension::Points(500.0),
1445 children: vec![stretch::style::Node {
1446 align_items: stretch::style::AlignItems::FlexStart,
1447 children: vec![stretch::style::Node {
1448 flex_grow: 1.0,
1449 flex_shrink: 1.0,
1450 children: vec![stretch::style::Node { flex_grow: 1.0, flex_shrink: 1.0, ..Default::default() }],
1451 ..Default::default()
1452 }],
1453 ..Default::default()
1454 }],
1455 ..Default::default()
1456 });
1457
1458 assert_eq!(layout.width, 500.0);
1459 assert_eq!(layout.height, 500.0);
1460 assert_eq!(layout.x, 0.0);
1461 assert_eq!(layout.y, 0.0);
1462
1463 assert_eq!(layout.children[0].width, 0.0);
1464 assert_eq!(layout.children[0].height, 500.0);
1465 assert_eq!(layout.children[0].x, 0.0);
1466 assert_eq!(layout.children[0].y, 0.0);
1467
1468 assert_eq!(layout.children[0].children[0].width, 0.0);
1469 assert_eq!(layout.children[0].children[0].height, 0.0);
1470 assert_eq!(layout.children[0].children[0].x, 0.0);
1471 assert_eq!(layout.children[0].children[0].y, 0.0);
1472
1473 assert_eq!(layout.children[0].children[0].children[0].width, 0.0);
1474 assert_eq!(layout.children[0].children[0].children[0].height, 0.0);
1475 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
1476 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
1477 }
1478
1479 #[test]
1480 fn align_items_stretch() {
1481 let layout = stretch::compute(&stretch::style::Node {
1482 width: stretch::style::Dimension::Points(100.0),
1483 height: stretch::style::Dimension::Points(100.0),
1484 children: vec![stretch::style::Node {
1485 width: stretch::style::Dimension::Points(10.0),
1486 ..Default::default()
1487 }],
1488 ..Default::default()
1489 });
1490
1491 assert_eq!(layout.width, 100.0);
1492 assert_eq!(layout.height, 100.0);
1493 assert_eq!(layout.x, 0.0);
1494 assert_eq!(layout.y, 0.0);
1495
1496 assert_eq!(layout.children[0].width, 10.0);
1497 assert_eq!(layout.children[0].height, 100.0);
1498 assert_eq!(layout.children[0].x, 0.0);
1499 assert_eq!(layout.children[0].y, 0.0);
1500 }
1501
1502 #[test]
1503 fn parent_wrap_child_size_overflowing_parent() {
1504 let layout = stretch::compute(&stretch::style::Node {
1505 width: stretch::style::Dimension::Points(100.0),
1506 height: stretch::style::Dimension::Points(100.0),
1507 children: vec![stretch::style::Node {
1508 width: stretch::style::Dimension::Points(100.0),
1509 children: vec![stretch::style::Node {
1510 width: stretch::style::Dimension::Points(100.0),
1511 height: stretch::style::Dimension::Points(200.0),
1512 ..Default::default()
1513 }],
1514 ..Default::default()
1515 }],
1516 ..Default::default()
1517 });
1518
1519 assert_eq!(layout.width, 100.0);
1520 assert_eq!(layout.height, 100.0);
1521 assert_eq!(layout.x, 0.0);
1522 assert_eq!(layout.y, 0.0);
1523
1524 assert_eq!(layout.children[0].width, 100.0);
1525 assert_eq!(layout.children[0].height, 100.0);
1526 assert_eq!(layout.children[0].x, 0.0);
1527 assert_eq!(layout.children[0].y, 0.0);
1528
1529 assert_eq!(layout.children[0].children[0].width, 100.0);
1530 assert_eq!(layout.children[0].children[0].height, 200.0);
1531 assert_eq!(layout.children[0].children[0].x, 0.0);
1532 assert_eq!(layout.children[0].children[0].y, 0.0);
1533 }
1534
1535 #[test]
1536 fn padding_stretch_child() {
1537 let layout = stretch::compute(&stretch::style::Node {
1538 width: stretch::style::Dimension::Points(100.0),
1539 height: stretch::style::Dimension::Points(100.0),
1540 padding: stretch::style::Edges {
1541 start: stretch::style::Dimension::Points(10.0),
1542 end: stretch::style::Dimension::Points(10.0),
1543 top: stretch::style::Dimension::Points(10.0),
1544 bottom: stretch::style::Dimension::Points(10.0),
1545 ..Default::default()
1546 },
1547 children: vec![stretch::style::Node {
1548 height: stretch::style::Dimension::Points(10.0),
1549 ..Default::default()
1550 }],
1551 ..Default::default()
1552 });
1553
1554 assert_eq!(layout.width, 100.0);
1555 assert_eq!(layout.height, 100.0);
1556 assert_eq!(layout.x, 0.0);
1557 assert_eq!(layout.y, 0.0);
1558
1559 assert_eq!(layout.children[0].width, 0.0);
1560 assert_eq!(layout.children[0].height, 10.0);
1561 assert_eq!(layout.children[0].x, 10.0);
1562 assert_eq!(layout.children[0].y, 10.0);
1563 }
1564
1565 #[test]
1566 fn wrap_nodes_with_content_sizing_overflowing_margin() {
1567 let layout = stretch::compute(&stretch::style::Node {
1568 flex_direction: stretch::style::FlexDirection::Column,
1569 width: stretch::style::Dimension::Points(500.0),
1570 height: stretch::style::Dimension::Points(500.0),
1571 children: vec![stretch::style::Node {
1572 width: stretch::style::Dimension::Points(85.0),
1573 children: vec![
1574 stretch::style::Node {
1575 flex_direction: stretch::style::FlexDirection::Column,
1576 children: vec![stretch::style::Node {
1577 width: stretch::style::Dimension::Points(40.0),
1578 height: stretch::style::Dimension::Points(40.0),
1579 ..Default::default()
1580 }],
1581 ..Default::default()
1582 },
1583 stretch::style::Node {
1584 flex_direction: stretch::style::FlexDirection::Column,
1585 margin: stretch::style::Edges {
1586 end: stretch::style::Dimension::Points(10.0),
1587 ..Default::default()
1588 },
1589 children: vec![stretch::style::Node {
1590 width: stretch::style::Dimension::Points(40.0),
1591 height: stretch::style::Dimension::Points(40.0),
1592 ..Default::default()
1593 }],
1594 ..Default::default()
1595 },
1596 ],
1597 ..Default::default()
1598 }],
1599 ..Default::default()
1600 });
1601
1602 assert_eq!(layout.width, 500.0);
1603 assert_eq!(layout.height, 500.0);
1604 assert_eq!(layout.x, 0.0);
1605 assert_eq!(layout.y, 0.0);
1606
1607 assert_eq!(layout.children[0].width, 85.0);
1608 assert_eq!(layout.children[0].height, 80.0);
1609 assert_eq!(layout.children[0].x, 0.0);
1610 assert_eq!(layout.children[0].y, 0.0);
1611
1612 assert_eq!(layout.children[0].children[0].width, 40.0);
1613 assert_eq!(layout.children[0].children[0].height, 40.0);
1614 assert_eq!(layout.children[0].children[0].x, 0.0);
1615 assert_eq!(layout.children[0].children[0].y, 0.0);
1616
1617 assert_eq!(layout.children[0].children[0].children[0].width, 40.0);
1618 assert_eq!(layout.children[0].children[0].children[0].height, 40.0);
1619 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
1620 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
1621
1622 assert_eq!(layout.children[0].children[1].width, 40.0);
1623 assert_eq!(layout.children[0].children[1].height, 40.0);
1624 assert_eq!(layout.children[0].children[1].x, 0.0);
1625 assert_eq!(layout.children[0].children[1].y, 40.0);
1626
1627 assert_eq!(layout.children[0].children[1].children[0].width, 40.0);
1628 assert_eq!(layout.children[0].children[1].children[0].height, 40.0);
1629 assert_eq!(layout.children[0].children[1].children[0].x, 0.0);
1630 assert_eq!(layout.children[0].children[1].children[0].y, 0.0);
1631 }
1632
1633 #[test]
1634 fn flex_shrink_flex_grow_row() {
1635 let layout = stretch::compute(&stretch::style::Node {
1636 width: stretch::style::Dimension::Points(500.0),
1637 height: stretch::style::Dimension::Points(500.0),
1638 children: vec![
1639 stretch::style::Node {
1640 flex_grow: 0.0,
1641 flex_shrink: 1.0,
1642 width: stretch::style::Dimension::Points(500.0),
1643 height: stretch::style::Dimension::Points(100.0),
1644 ..Default::default()
1645 },
1646 stretch::style::Node {
1647 flex_grow: 0.0,
1648 flex_shrink: 1.0,
1649 width: stretch::style::Dimension::Points(500.0),
1650 height: stretch::style::Dimension::Points(100.0),
1651 ..Default::default()
1652 },
1653 ],
1654 ..Default::default()
1655 });
1656
1657 assert_eq!(layout.width, 500.0);
1658 assert_eq!(layout.height, 500.0);
1659 assert_eq!(layout.x, 0.0);
1660 assert_eq!(layout.y, 0.0);
1661
1662 assert_eq!(layout.children[0].width, 250.0);
1663 assert_eq!(layout.children[0].height, 100.0);
1664 assert_eq!(layout.children[0].x, 0.0);
1665 assert_eq!(layout.children[0].y, 0.0);
1666
1667 assert_eq!(layout.children[1].width, 250.0);
1668 assert_eq!(layout.children[1].height, 100.0);
1669 assert_eq!(layout.children[1].x, 250.0);
1670 assert_eq!(layout.children[1].y, 0.0);
1671 }
1672
1673 #[test]
1674 fn flex_basis_flex_shrink_column() {
1675 let layout = stretch::compute(&stretch::style::Node {
1676 flex_direction: stretch::style::FlexDirection::Column,
1677 width: stretch::style::Dimension::Points(100.0),
1678 height: stretch::style::Dimension::Points(100.0),
1679 children: vec![
1680 stretch::style::Node { flex_basis: stretch::style::Dimension::Points(100.0), ..Default::default() },
1681 stretch::style::Node { flex_basis: stretch::style::Dimension::Points(50.0), ..Default::default() },
1682 ],
1683 ..Default::default()
1684 });
1685
1686 assert_eq!(layout.width, 100.0);
1687 assert_eq!(layout.height, 100.0);
1688 assert_eq!(layout.x, 0.0);
1689 assert_eq!(layout.y, 0.0);
1690
1691 assert_eq!(layout.children[0].width, 100.0);
1692 assert_eq!(layout.children[0].height, 67.0);
1693 assert_eq!(layout.children[0].x, 0.0);
1694 assert_eq!(layout.children[0].y, 0.0);
1695
1696 assert_eq!(layout.children[1].width, 100.0);
1697 assert_eq!(layout.children[1].height, 33.0);
1698 assert_eq!(layout.children[1].x, 0.0);
1699 assert_eq!(layout.children[1].y, 67.0);
1700 }
1701
1702 #[test]
1703 fn align_items_min_max() {
1704 let layout = stretch::compute(&stretch::style::Node {
1705 flex_direction: stretch::style::FlexDirection::Column,
1706 align_items: stretch::style::AlignItems::Center,
1707 min_width: stretch::style::Dimension::Points(100.0),
1708 max_width: stretch::style::Dimension::Points(200.0),
1709 height: stretch::style::Dimension::Points(100.0),
1710 children: vec![stretch::style::Node {
1711 width: stretch::style::Dimension::Points(60.0),
1712 height: stretch::style::Dimension::Points(60.0),
1713 ..Default::default()
1714 }],
1715 ..Default::default()
1716 });
1717
1718 assert_eq!(layout.width, 100.0);
1719 assert_eq!(layout.height, 100.0);
1720 assert_eq!(layout.x, 0.0);
1721 assert_eq!(layout.y, 0.0);
1722
1723 assert_eq!(layout.children[0].width, 60.0);
1724 assert_eq!(layout.children[0].height, 60.0);
1725 assert_eq!(layout.children[0].x, 20.0);
1726 assert_eq!(layout.children[0].y, 0.0);
1727 }
1728
1729 #[test]
1730 fn justify_content_row_min_width_and_margin() {
1731 let layout = stretch::compute(&stretch::style::Node {
1732 justify_content: stretch::style::JustifyContent::Center,
1733 min_width: stretch::style::Dimension::Points(50.0),
1734 margin: stretch::style::Edges { start: stretch::style::Dimension::Points(100.0), ..Default::default() },
1735 children: vec![stretch::style::Node {
1736 width: stretch::style::Dimension::Points(20.0),
1737 height: stretch::style::Dimension::Points(20.0),
1738 ..Default::default()
1739 }],
1740 ..Default::default()
1741 });
1742
1743 assert_eq!(layout.width, 50.0);
1744 assert_eq!(layout.height, 20.0);
1745 assert_eq!(layout.x, 100.0);
1746 assert_eq!(layout.y, 0.0);
1747
1748 assert_eq!(layout.children[0].width, 20.0);
1749 assert_eq!(layout.children[0].height, 20.0);
1750 assert_eq!(layout.children[0].x, 15.0);
1751 assert_eq!(layout.children[0].y, 0.0);
1752 }
1753
1754 #[test]
1755 fn align_self_flex_start() {
1756 let layout = stretch::compute(&stretch::style::Node {
1757 width: stretch::style::Dimension::Points(100.0),
1758 height: stretch::style::Dimension::Points(100.0),
1759 children: vec![stretch::style::Node {
1760 align_self: stretch::style::AlignSelf::FlexStart,
1761 width: stretch::style::Dimension::Points(10.0),
1762 height: stretch::style::Dimension::Points(10.0),
1763 ..Default::default()
1764 }],
1765 ..Default::default()
1766 });
1767
1768 assert_eq!(layout.width, 100.0);
1769 assert_eq!(layout.height, 100.0);
1770 assert_eq!(layout.x, 0.0);
1771 assert_eq!(layout.y, 0.0);
1772
1773 assert_eq!(layout.children[0].width, 10.0);
1774 assert_eq!(layout.children[0].height, 10.0);
1775 assert_eq!(layout.children[0].x, 0.0);
1776 assert_eq!(layout.children[0].y, 0.0);
1777 }
1778
1779 #[test]
1780 fn percentage_position_left_top() {
1781 let layout = stretch::compute(&stretch::style::Node {
1782 width: stretch::style::Dimension::Points(400.0),
1783 height: stretch::style::Dimension::Points(400.0),
1784 children: vec![stretch::style::Node {
1785 width: stretch::style::Dimension::Percent(0.4),
1786 height: stretch::style::Dimension::Percent(0.6),
1787 start: stretch::style::Dimension::Percent(0.1),
1788 top: stretch::style::Dimension::Percent(0.2),
1789 ..Default::default()
1790 }],
1791 ..Default::default()
1792 });
1793
1794 assert_eq!(layout.width, 400.0);
1795 assert_eq!(layout.height, 400.0);
1796 assert_eq!(layout.x, 0.0);
1797 assert_eq!(layout.y, 0.0);
1798
1799 assert_eq!(layout.children[0].width, 180.0);
1800 assert_eq!(layout.children[0].height, 220.0);
1801 assert_eq!(layout.children[0].x, 40.0);
1802 assert_eq!(layout.children[0].y, 80.0);
1803 }
1804
1805 #[test]
1806 fn border_stretch_child() {
1807 let layout = stretch::compute(&stretch::style::Node {
1808 width: stretch::style::Dimension::Points(100.0),
1809 height: stretch::style::Dimension::Points(100.0),
1810 border: stretch::style::Edges {
1811 start: stretch::style::Dimension::Points(10.0),
1812 end: stretch::style::Dimension::Points(10.0),
1813 top: stretch::style::Dimension::Points(10.0),
1814 bottom: stretch::style::Dimension::Points(10.0),
1815 ..Default::default()
1816 },
1817 children: vec![stretch::style::Node {
1818 width: stretch::style::Dimension::Points(10.0),
1819 ..Default::default()
1820 }],
1821 ..Default::default()
1822 });
1823
1824 assert_eq!(layout.width, 100.0);
1825 assert_eq!(layout.height, 100.0);
1826 assert_eq!(layout.x, 0.0);
1827 assert_eq!(layout.y, 0.0);
1828
1829 assert_eq!(layout.children[0].width, 10.0);
1830 assert_eq!(layout.children[0].height, 80.0);
1831 assert_eq!(layout.children[0].x, 10.0);
1832 assert_eq!(layout.children[0].y, 10.0);
1833 }
1834
1835 #[test]
1836 fn rounding_fractial_input_2() {
1837 let layout = stretch::compute(&stretch::style::Node {
1838 flex_direction: stretch::style::FlexDirection::Column,
1839 width: stretch::style::Dimension::Points(100.0),
1840 height: stretch::style::Dimension::Points(113.6),
1841 children: vec![
1842 stretch::style::Node {
1843 flex_grow: 1.0,
1844 flex_basis: stretch::style::Dimension::Points(50.0),
1845 height: stretch::style::Dimension::Points(20.0),
1846 ..Default::default()
1847 },
1848 stretch::style::Node {
1849 flex_grow: 1.0,
1850 height: stretch::style::Dimension::Points(10.0),
1851 ..Default::default()
1852 },
1853 stretch::style::Node {
1854 flex_grow: 1.0,
1855 height: stretch::style::Dimension::Points(10.0),
1856 ..Default::default()
1857 },
1858 ],
1859 ..Default::default()
1860 });
1861
1862 assert_eq!(layout.width, 100.0);
1863 assert_eq!(layout.height, 114.0);
1864 assert_eq!(layout.x, 0.0);
1865 assert_eq!(layout.y, 0.0);
1866
1867 assert_eq!(layout.children[0].width, 100.0);
1868 assert_eq!(layout.children[0].height, 65.0);
1869 assert_eq!(layout.children[0].x, 0.0);
1870 assert_eq!(layout.children[0].y, 0.0);
1871
1872 assert_eq!(layout.children[1].width, 100.0);
1873 assert_eq!(layout.children[1].height, 24.0);
1874 assert_eq!(layout.children[1].x, 0.0);
1875 assert_eq!(layout.children[1].y, 65.0);
1876
1877 assert_eq!(layout.children[2].width, 100.0);
1878 assert_eq!(layout.children[2].height, 25.0);
1879 assert_eq!(layout.children[2].x, 0.0);
1880 assert_eq!(layout.children[2].y, 89.0);
1881 }
1882
1883 #[test]
1884 fn percentage_flex_basis_main_max_width() {
1885 let layout = stretch::compute(&stretch::style::Node {
1886 width: stretch::style::Dimension::Points(200.0),
1887 height: stretch::style::Dimension::Points(200.0),
1888 children: vec![
1889 stretch::style::Node {
1890 flex_grow: 1.0,
1891 flex_basis: stretch::style::Dimension::Percent(0.2),
1892 max_width: stretch::style::Dimension::Percent(0.6),
1893 ..Default::default()
1894 },
1895 stretch::style::Node {
1896 flex_grow: 4.0,
1897 flex_basis: stretch::style::Dimension::Percent(0.1),
1898 max_width: stretch::style::Dimension::Percent(0.2),
1899 ..Default::default()
1900 },
1901 ],
1902 ..Default::default()
1903 });
1904
1905 assert_eq!(layout.width, 200.0);
1906 assert_eq!(layout.height, 200.0);
1907 assert_eq!(layout.x, 0.0);
1908 assert_eq!(layout.y, 0.0);
1909
1910 assert_eq!(layout.children[0].width, 120.0);
1911 assert_eq!(layout.children[0].height, 200.0);
1912 assert_eq!(layout.children[0].x, 0.0);
1913 assert_eq!(layout.children[0].y, 0.0);
1914
1915 assert_eq!(layout.children[1].width, 40.0);
1916 assert_eq!(layout.children[1].height, 200.0);
1917 assert_eq!(layout.children[1].x, 120.0);
1918 assert_eq!(layout.children[1].y, 0.0);
1919 }
1920
1921 #[test]
1922 fn margin_auto_top_and_bottom_strech() {
1923 let layout = stretch::compute(&stretch::style::Node {
1924 flex_direction: stretch::style::FlexDirection::Column,
1925 width: stretch::style::Dimension::Points(200.0),
1926 height: stretch::style::Dimension::Points(200.0),
1927 children: vec![
1928 stretch::style::Node {
1929 width: stretch::style::Dimension::Points(50.0),
1930 height: stretch::style::Dimension::Points(50.0),
1931 margin: stretch::style::Edges {
1932 top: stretch::style::Dimension::Auto,
1933 bottom: stretch::style::Dimension::Auto,
1934 ..Default::default()
1935 },
1936 ..Default::default()
1937 },
1938 stretch::style::Node {
1939 width: stretch::style::Dimension::Points(50.0),
1940 height: stretch::style::Dimension::Points(50.0),
1941 ..Default::default()
1942 },
1943 ],
1944 ..Default::default()
1945 });
1946
1947 assert_eq!(layout.width, 200.0);
1948 assert_eq!(layout.height, 200.0);
1949 assert_eq!(layout.x, 0.0);
1950 assert_eq!(layout.y, 0.0);
1951
1952 assert_eq!(layout.children[0].width, 50.0);
1953 assert_eq!(layout.children[0].height, 50.0);
1954 assert_eq!(layout.children[0].x, 0.0);
1955 assert_eq!(layout.children[0].y, 50.0);
1956
1957 assert_eq!(layout.children[1].width, 50.0);
1958 assert_eq!(layout.children[1].height, 50.0);
1959 assert_eq!(layout.children[1].x, 0.0);
1960 assert_eq!(layout.children[1].y, 150.0);
1961 }
1962
1963 #[test]
1964 fn percentage_multiple_nested_with_padding_margin_and_percentage_values() {
1965 let layout = stretch::compute(&stretch::style::Node {
1966 flex_direction: stretch::style::FlexDirection::Column,
1967 width: stretch::style::Dimension::Points(200.0),
1968 height: stretch::style::Dimension::Points(200.0),
1969 children: vec![
1970 stretch::style::Node {
1971 flex_direction: stretch::style::FlexDirection::Column,
1972 flex_grow: 1.0,
1973 flex_basis: stretch::style::Dimension::Percent(0.1),
1974 min_width: stretch::style::Dimension::Percent(0.6),
1975 margin: stretch::style::Edges {
1976 start: stretch::style::Dimension::Points(5.0),
1977 end: stretch::style::Dimension::Points(5.0),
1978 top: stretch::style::Dimension::Points(5.0),
1979 bottom: stretch::style::Dimension::Points(5.0),
1980 ..Default::default()
1981 },
1982 padding: stretch::style::Edges {
1983 start: stretch::style::Dimension::Points(3.0),
1984 end: stretch::style::Dimension::Points(3.0),
1985 top: stretch::style::Dimension::Points(3.0),
1986 bottom: stretch::style::Dimension::Points(3.0),
1987 ..Default::default()
1988 },
1989 children: vec![stretch::style::Node {
1990 flex_direction: stretch::style::FlexDirection::Column,
1991 width: stretch::style::Dimension::Percent(0.5),
1992 margin: stretch::style::Edges {
1993 start: stretch::style::Dimension::Points(5.0),
1994 end: stretch::style::Dimension::Points(5.0),
1995 top: stretch::style::Dimension::Points(5.0),
1996 bottom: stretch::style::Dimension::Points(5.0),
1997 ..Default::default()
1998 },
1999 padding: stretch::style::Edges {
2000 start: stretch::style::Dimension::Percent(0.0),
2001 end: stretch::style::Dimension::Percent(0.0),
2002 top: stretch::style::Dimension::Percent(0.0),
2003 bottom: stretch::style::Dimension::Percent(0.0),
2004 ..Default::default()
2005 },
2006 children: vec![stretch::style::Node {
2007 width: stretch::style::Dimension::Percent(0.4),
2008 margin: stretch::style::Edges {
2009 start: stretch::style::Dimension::Percent(0.1),
2010 end: stretch::style::Dimension::Percent(0.1),
2011 top: stretch::style::Dimension::Percent(0.1),
2012 bottom: stretch::style::Dimension::Percent(0.1),
2013 ..Default::default()
2014 },
2015 padding: stretch::style::Edges {
2016 start: stretch::style::Dimension::Points(3.0),
2017 end: stretch::style::Dimension::Points(3.0),
2018 top: stretch::style::Dimension::Points(3.0),
2019 bottom: stretch::style::Dimension::Points(3.0),
2020 ..Default::default()
2021 },
2022 ..Default::default()
2023 }],
2024 ..Default::default()
2025 }],
2026 ..Default::default()
2027 },
2028 stretch::style::Node {
2029 flex_grow: 4.0,
2030 flex_basis: stretch::style::Dimension::Percent(0.2),
2031 min_width: stretch::style::Dimension::Percent(0.2),
2032 ..Default::default()
2033 },
2034 ],
2035 ..Default::default()
2036 });
2037
2038 assert_eq!(layout.width, 200.0);
2039 assert_eq!(layout.height, 200.0);
2040 assert_eq!(layout.x, 0.0);
2041 assert_eq!(layout.y, 0.0);
2042
2043 assert_eq!(layout.children[0].width, 190.0);
2044 assert_eq!(layout.children[0].height, 48.0);
2045 assert_eq!(layout.children[0].x, 5.0);
2046 assert_eq!(layout.children[0].y, 5.0);
2047
2048 assert_eq!(layout.children[0].children[0].width, 92.0);
2049 assert_eq!(layout.children[0].children[0].height, 25.0);
2050 assert_eq!(layout.children[0].children[0].x, 8.0);
2051 assert_eq!(layout.children[0].children[0].y, 8.0);
2052
2053 assert_eq!(layout.children[0].children[0].children[0].width, 36.0);
2054 assert_eq!(layout.children[0].children[0].children[0].height, 6.0);
2055 assert_eq!(layout.children[0].children[0].children[0].x, 10.0);
2056 assert_eq!(layout.children[0].children[0].children[0].y, 10.0);
2057
2058 assert_eq!(layout.children[1].width, 200.0);
2059 assert_eq!(layout.children[1].height, 142.0);
2060 assert_eq!(layout.children[1].x, 0.0);
2061 assert_eq!(layout.children[1].y, 58.0);
2062 }
2063
2064 #[test]
2065 fn flex_grow_within_constrained_min_column() {
2066 let layout = stretch::compute(&stretch::style::Node {
2067 flex_direction: stretch::style::FlexDirection::Column,
2068 min_height: stretch::style::Dimension::Points(100.0),
2069 children: vec![
2070 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
2071 stretch::style::Node { height: stretch::style::Dimension::Points(50.0), ..Default::default() },
2072 ],
2073 ..Default::default()
2074 });
2075
2076 assert_eq!(layout.width, 0.0);
2077 assert_eq!(layout.height, 100.0);
2078 assert_eq!(layout.x, 0.0);
2079 assert_eq!(layout.y, 0.0);
2080
2081 assert_eq!(layout.children[0].width, 0.0);
2082 assert_eq!(layout.children[0].height, 50.0);
2083 assert_eq!(layout.children[0].x, 0.0);
2084 assert_eq!(layout.children[0].y, 0.0);
2085
2086 assert_eq!(layout.children[1].width, 0.0);
2087 assert_eq!(layout.children[1].height, 50.0);
2088 assert_eq!(layout.children[1].x, 0.0);
2089 assert_eq!(layout.children[1].y, 50.0);
2090 }
2091
2092 #[test]
2093 fn flex_grow_in_at_most_container() {
2094 let layout = stretch::compute(&stretch::style::Node {
2095 align_items: stretch::style::AlignItems::FlexStart,
2096 width: stretch::style::Dimension::Points(100.0),
2097 height: stretch::style::Dimension::Points(100.0),
2098 children: vec![stretch::style::Node {
2099 children: vec![stretch::style::Node {
2100 flex_grow: 1.0,
2101 flex_basis: stretch::style::Dimension::Points(0.0),
2102 ..Default::default()
2103 }],
2104 ..Default::default()
2105 }],
2106 ..Default::default()
2107 });
2108
2109 assert_eq!(layout.width, 100.0);
2110 assert_eq!(layout.height, 100.0);
2111 assert_eq!(layout.x, 0.0);
2112 assert_eq!(layout.y, 0.0);
2113
2114 assert_eq!(layout.children[0].width, 0.0);
2115 assert_eq!(layout.children[0].height, 0.0);
2116 assert_eq!(layout.children[0].x, 0.0);
2117 assert_eq!(layout.children[0].y, 0.0);
2118
2119 assert_eq!(layout.children[0].children[0].width, 0.0);
2120 assert_eq!(layout.children[0].children[0].height, 0.0);
2121 assert_eq!(layout.children[0].children[0].x, 0.0);
2122 assert_eq!(layout.children[0].children[0].y, 0.0);
2123 }
2124
2125 #[test]
2126 fn margin_auto_left() {
2127 let layout = stretch::compute(&stretch::style::Node {
2128 align_items: stretch::style::AlignItems::Center,
2129 width: stretch::style::Dimension::Points(200.0),
2130 height: stretch::style::Dimension::Points(200.0),
2131 children: vec![
2132 stretch::style::Node {
2133 width: stretch::style::Dimension::Points(50.0),
2134 height: stretch::style::Dimension::Points(50.0),
2135 margin: stretch::style::Edges { start: stretch::style::Dimension::Auto, ..Default::default() },
2136 ..Default::default()
2137 },
2138 stretch::style::Node {
2139 width: stretch::style::Dimension::Points(50.0),
2140 height: stretch::style::Dimension::Points(50.0),
2141 ..Default::default()
2142 },
2143 ],
2144 ..Default::default()
2145 });
2146
2147 assert_eq!(layout.width, 200.0);
2148 assert_eq!(layout.height, 200.0);
2149 assert_eq!(layout.x, 0.0);
2150 assert_eq!(layout.y, 0.0);
2151
2152 assert_eq!(layout.children[0].width, 50.0);
2153 assert_eq!(layout.children[0].height, 50.0);
2154 assert_eq!(layout.children[0].x, 100.0);
2155 assert_eq!(layout.children[0].y, 75.0);
2156
2157 assert_eq!(layout.children[1].width, 50.0);
2158 assert_eq!(layout.children[1].height, 50.0);
2159 assert_eq!(layout.children[1].x, 150.0);
2160 assert_eq!(layout.children[1].y, 75.0);
2161 }
2162
2163 #[test]
2164 fn percentage_flex_basis_cross() {
2165 let layout = stretch::compute(&stretch::style::Node {
2166 flex_direction: stretch::style::FlexDirection::Column,
2167 width: stretch::style::Dimension::Points(200.0),
2168 height: stretch::style::Dimension::Points(200.0),
2169 children: vec![
2170 stretch::style::Node {
2171 flex_grow: 1.0,
2172 flex_basis: stretch::style::Dimension::Percent(0.5),
2173 ..Default::default()
2174 },
2175 stretch::style::Node {
2176 flex_grow: 1.0,
2177 flex_basis: stretch::style::Dimension::Percent(0.2),
2178 ..Default::default()
2179 },
2180 ],
2181 ..Default::default()
2182 });
2183
2184 assert_eq!(layout.width, 200.0);
2185 assert_eq!(layout.height, 200.0);
2186 assert_eq!(layout.x, 0.0);
2187 assert_eq!(layout.y, 0.0);
2188
2189 assert_eq!(layout.children[0].width, 200.0);
2190 assert_eq!(layout.children[0].height, 125.0);
2191 assert_eq!(layout.children[0].x, 0.0);
2192 assert_eq!(layout.children[0].y, 0.0);
2193
2194 assert_eq!(layout.children[1].width, 200.0);
2195 assert_eq!(layout.children[1].height, 75.0);
2196 assert_eq!(layout.children[1].x, 0.0);
2197 assert_eq!(layout.children[1].y, 125.0);
2198 }
2199
2200 #[test]
2201 fn percentage_absolute_position() {
2202 let layout = stretch::compute(&stretch::style::Node {
2203 flex_direction: stretch::style::FlexDirection::Column,
2204 width: stretch::style::Dimension::Points(200.0),
2205 height: stretch::style::Dimension::Points(100.0),
2206 children: vec![stretch::style::Node {
2207 position: stretch::style::Position::Absolute,
2208 width: stretch::style::Dimension::Points(10.0),
2209 height: stretch::style::Dimension::Points(10.0),
2210 start: stretch::style::Dimension::Percent(0.3),
2211 top: stretch::style::Dimension::Percent(0.1),
2212 ..Default::default()
2213 }],
2214 ..Default::default()
2215 });
2216
2217 assert_eq!(layout.width, 200.0);
2218 assert_eq!(layout.height, 100.0);
2219 assert_eq!(layout.x, 0.0);
2220 assert_eq!(layout.y, 0.0);
2221
2222 assert_eq!(layout.children[0].width, 10.0);
2223 assert_eq!(layout.children[0].height, 10.0);
2224 assert_eq!(layout.children[0].x, 60.0);
2225 assert_eq!(layout.children[0].y, 10.0);
2226 }
2227
2228 #[test]
2229 fn flex_basis_flex_grow_row() {
2230 let layout = stretch::compute(&stretch::style::Node {
2231 width: stretch::style::Dimension::Points(100.0),
2232 height: stretch::style::Dimension::Points(100.0),
2233 children: vec![
2234 stretch::style::Node {
2235 flex_grow: 1.0,
2236 flex_basis: stretch::style::Dimension::Points(50.0),
2237 ..Default::default()
2238 },
2239 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
2240 ],
2241 ..Default::default()
2242 });
2243
2244 assert_eq!(layout.width, 100.0);
2245 assert_eq!(layout.height, 100.0);
2246 assert_eq!(layout.x, 0.0);
2247 assert_eq!(layout.y, 0.0);
2248
2249 assert_eq!(layout.children[0].width, 75.0);
2250 assert_eq!(layout.children[0].height, 100.0);
2251 assert_eq!(layout.children[0].x, 0.0);
2252 assert_eq!(layout.children[0].y, 0.0);
2253
2254 assert_eq!(layout.children[1].width, 25.0);
2255 assert_eq!(layout.children[1].height, 100.0);
2256 assert_eq!(layout.children[1].x, 75.0);
2257 assert_eq!(layout.children[1].y, 0.0);
2258 }
2259
2260 #[test]
2261 fn absolute_layout_in_wrap_reverse_column_container_flex_end() {
2262 let layout = stretch::compute(&stretch::style::Node {
2263 flex_direction: stretch::style::FlexDirection::Column,
2264 width: stretch::style::Dimension::Points(100.0),
2265 height: stretch::style::Dimension::Points(100.0),
2266 children: vec![stretch::style::Node {
2267 position: stretch::style::Position::Absolute,
2268 align_self: stretch::style::AlignSelf::FlexEnd,
2269 width: stretch::style::Dimension::Points(20.0),
2270 height: stretch::style::Dimension::Points(20.0),
2271 ..Default::default()
2272 }],
2273 ..Default::default()
2274 });
2275
2276 assert_eq!(layout.width, 100.0);
2277 assert_eq!(layout.height, 100.0);
2278 assert_eq!(layout.x, 0.0);
2279 assert_eq!(layout.y, 0.0);
2280
2281 assert_eq!(layout.children[0].width, 20.0);
2282 assert_eq!(layout.children[0].height, 20.0);
2283 assert_eq!(layout.children[0].x, 0.0);
2284 assert_eq!(layout.children[0].y, 0.0);
2285 }
2286
2287 #[test]
2288 fn justify_content_row_flex_start() {
2289 let layout = stretch::compute(&stretch::style::Node {
2290 width: stretch::style::Dimension::Points(100.0),
2291 height: stretch::style::Dimension::Points(100.0),
2292 children: vec![
2293 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
2294 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
2295 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
2296 ],
2297 ..Default::default()
2298 });
2299
2300 assert_eq!(layout.width, 100.0);
2301 assert_eq!(layout.height, 100.0);
2302 assert_eq!(layout.x, 0.0);
2303 assert_eq!(layout.y, 0.0);
2304
2305 assert_eq!(layout.children[0].width, 10.0);
2306 assert_eq!(layout.children[0].height, 100.0);
2307 assert_eq!(layout.children[0].x, 0.0);
2308 assert_eq!(layout.children[0].y, 0.0);
2309
2310 assert_eq!(layout.children[1].width, 10.0);
2311 assert_eq!(layout.children[1].height, 100.0);
2312 assert_eq!(layout.children[1].x, 10.0);
2313 assert_eq!(layout.children[1].y, 0.0);
2314
2315 assert_eq!(layout.children[2].width, 10.0);
2316 assert_eq!(layout.children[2].height, 100.0);
2317 assert_eq!(layout.children[2].x, 20.0);
2318 assert_eq!(layout.children[2].y, 0.0);
2319 }
2320
2321 #[test]
2322 fn margin_auto_left_right_child_bigger_than_parent() {
2323 let layout = stretch::compute(&stretch::style::Node {
2324 justify_content: stretch::style::JustifyContent::Center,
2325 width: stretch::style::Dimension::Points(52.0),
2326 height: stretch::style::Dimension::Points(52.0),
2327 children: vec![stretch::style::Node {
2328 width: stretch::style::Dimension::Points(72.0),
2329 height: stretch::style::Dimension::Points(72.0),
2330 margin: stretch::style::Edges {
2331 start: stretch::style::Dimension::Auto,
2332 end: stretch::style::Dimension::Auto,
2333 ..Default::default()
2334 },
2335 ..Default::default()
2336 }],
2337 ..Default::default()
2338 });
2339
2340 assert_eq!(layout.width, 52.0);
2341 assert_eq!(layout.height, 52.0);
2342 assert_eq!(layout.x, 0.0);
2343 assert_eq!(layout.y, 0.0);
2344
2345 assert_eq!(layout.children[0].width, 52.0);
2346 assert_eq!(layout.children[0].height, 72.0);
2347 assert_eq!(layout.children[0].x, 0.0);
2348 assert_eq!(layout.children[0].y, 0.0);
2349 }
2350
2351 #[test]
2352 fn margin_fix_left_auto_right_child_bigger_than_parent() {
2353 let layout = stretch::compute(&stretch::style::Node {
2354 justify_content: stretch::style::JustifyContent::Center,
2355 width: stretch::style::Dimension::Points(52.0),
2356 height: stretch::style::Dimension::Points(52.0),
2357 children: vec![stretch::style::Node {
2358 width: stretch::style::Dimension::Points(72.0),
2359 height: stretch::style::Dimension::Points(72.0),
2360 margin: stretch::style::Edges {
2361 start: stretch::style::Dimension::Points(10.0),
2362 end: stretch::style::Dimension::Auto,
2363 ..Default::default()
2364 },
2365 ..Default::default()
2366 }],
2367 ..Default::default()
2368 });
2369
2370 assert_eq!(layout.width, 52.0);
2371 assert_eq!(layout.height, 52.0);
2372 assert_eq!(layout.x, 0.0);
2373 assert_eq!(layout.y, 0.0);
2374
2375 assert_eq!(layout.children[0].width, 42.0);
2376 assert_eq!(layout.children[0].height, 72.0);
2377 assert_eq!(layout.children[0].x, 10.0);
2378 assert_eq!(layout.children[0].y, 0.0);
2379 }
2380
2381 #[test]
2382 fn percentage_container_in_wrapping_container() {
2383 let layout = stretch::compute(&stretch::style::Node {
2384 flex_direction: stretch::style::FlexDirection::Column,
2385 align_items: stretch::style::AlignItems::Center,
2386 justify_content: stretch::style::JustifyContent::Center,
2387 width: stretch::style::Dimension::Points(200.0),
2388 height: stretch::style::Dimension::Points(200.0),
2389 children: vec![stretch::style::Node {
2390 flex_direction: stretch::style::FlexDirection::Column,
2391 children: vec![stretch::style::Node {
2392 justify_content: stretch::style::JustifyContent::Center,
2393 width: stretch::style::Dimension::Percent(1.0),
2394 children: vec![
2395 stretch::style::Node {
2396 width: stretch::style::Dimension::Points(50.0),
2397 height: stretch::style::Dimension::Points(50.0),
2398 ..Default::default()
2399 },
2400 stretch::style::Node {
2401 width: stretch::style::Dimension::Points(50.0),
2402 height: stretch::style::Dimension::Points(50.0),
2403 ..Default::default()
2404 },
2405 ],
2406 ..Default::default()
2407 }],
2408 ..Default::default()
2409 }],
2410 ..Default::default()
2411 });
2412
2413 assert_eq!(layout.width, 200.0);
2414 assert_eq!(layout.height, 200.0);
2415 assert_eq!(layout.x, 0.0);
2416 assert_eq!(layout.y, 0.0);
2417
2418 assert_eq!(layout.children[0].width, 100.0);
2419 assert_eq!(layout.children[0].height, 50.0);
2420 assert_eq!(layout.children[0].x, 50.0);
2421 assert_eq!(layout.children[0].y, 75.0);
2422
2423 assert_eq!(layout.children[0].children[0].width, 100.0);
2424 assert_eq!(layout.children[0].children[0].height, 50.0);
2425 assert_eq!(layout.children[0].children[0].x, 0.0);
2426 assert_eq!(layout.children[0].children[0].y, 0.0);
2427
2428 assert_eq!(layout.children[0].children[0].children[0].width, 50.0);
2429 assert_eq!(layout.children[0].children[0].children[0].height, 50.0);
2430 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
2431 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
2432
2433 assert_eq!(layout.children[0].children[0].children[1].width, 50.0);
2434 assert_eq!(layout.children[0].children[0].children[1].height, 50.0);
2435 assert_eq!(layout.children[0].children[0].children[1].x, 50.0);
2436 assert_eq!(layout.children[0].children[0].children[1].y, 0.0);
2437 }
2438
2439 #[test]
2440 fn margin_auto_left_stretching_child() {
2441 let layout = stretch::compute(&stretch::style::Node {
2442 align_items: stretch::style::AlignItems::Center,
2443 width: stretch::style::Dimension::Points(200.0),
2444 height: stretch::style::Dimension::Points(200.0),
2445 children: vec![
2446 stretch::style::Node {
2447 flex_grow: 1.0,
2448 flex_shrink: 1.0,
2449 flex_basis: stretch::style::Dimension::Percent(0.0),
2450 margin: stretch::style::Edges { start: stretch::style::Dimension::Auto, ..Default::default() },
2451 ..Default::default()
2452 },
2453 stretch::style::Node {
2454 width: stretch::style::Dimension::Points(50.0),
2455 height: stretch::style::Dimension::Points(50.0),
2456 ..Default::default()
2457 },
2458 ],
2459 ..Default::default()
2460 });
2461
2462 assert_eq!(layout.width, 200.0);
2463 assert_eq!(layout.height, 200.0);
2464 assert_eq!(layout.x, 0.0);
2465 assert_eq!(layout.y, 0.0);
2466
2467 assert_eq!(layout.children[0].width, 150.0);
2468 assert_eq!(layout.children[0].height, 0.0);
2469 assert_eq!(layout.children[0].x, 0.0);
2470 assert_eq!(layout.children[0].y, 100.0);
2471
2472 assert_eq!(layout.children[1].width, 50.0);
2473 assert_eq!(layout.children[1].height, 50.0);
2474 assert_eq!(layout.children[1].x, 150.0);
2475 assert_eq!(layout.children[1].y, 75.0);
2476 }
2477
2478 #[test]
2479 fn percentage_flex_basis_cross_max_width() {
2480 let layout = stretch::compute(&stretch::style::Node {
2481 flex_direction: stretch::style::FlexDirection::Column,
2482 width: stretch::style::Dimension::Points(200.0),
2483 height: stretch::style::Dimension::Points(200.0),
2484 children: vec![
2485 stretch::style::Node {
2486 flex_grow: 1.0,
2487 flex_basis: stretch::style::Dimension::Percent(0.1),
2488 max_width: stretch::style::Dimension::Percent(0.6),
2489 ..Default::default()
2490 },
2491 stretch::style::Node {
2492 flex_grow: 4.0,
2493 flex_basis: stretch::style::Dimension::Percent(0.2),
2494 max_width: stretch::style::Dimension::Percent(0.2),
2495 ..Default::default()
2496 },
2497 ],
2498 ..Default::default()
2499 });
2500
2501 assert_eq!(layout.width, 200.0);
2502 assert_eq!(layout.height, 200.0);
2503 assert_eq!(layout.x, 0.0);
2504 assert_eq!(layout.y, 0.0);
2505
2506 assert_eq!(layout.children[0].width, 120.0);
2507 assert_eq!(layout.children[0].height, 50.0);
2508 assert_eq!(layout.children[0].x, 0.0);
2509 assert_eq!(layout.children[0].y, 0.0);
2510
2511 assert_eq!(layout.children[1].width, 40.0);
2512 assert_eq!(layout.children[1].height, 150.0);
2513 assert_eq!(layout.children[1].x, 0.0);
2514 assert_eq!(layout.children[1].y, 50.0);
2515 }
2516
2517 #[test]
2518 fn align_items_flex_end_child_without_margin_bigger_than_parent() {
2519 let layout = stretch::compute(&stretch::style::Node {
2520 align_items: stretch::style::AlignItems::Center,
2521 justify_content: stretch::style::JustifyContent::Center,
2522 width: stretch::style::Dimension::Points(52.0),
2523 height: stretch::style::Dimension::Points(52.0),
2524 children: vec![stretch::style::Node {
2525 align_items: stretch::style::AlignItems::FlexEnd,
2526 children: vec![stretch::style::Node {
2527 width: stretch::style::Dimension::Points(72.0),
2528 height: stretch::style::Dimension::Points(72.0),
2529 ..Default::default()
2530 }],
2531 ..Default::default()
2532 }],
2533 ..Default::default()
2534 });
2535
2536 assert_eq!(layout.width, 52.0);
2537 assert_eq!(layout.height, 52.0);
2538 assert_eq!(layout.x, 0.0);
2539 assert_eq!(layout.y, 0.0);
2540
2541 assert_eq!(layout.children[0].width, 72.0);
2542 assert_eq!(layout.children[0].height, 72.0);
2543 assert_eq!(layout.children[0].x, -10.0);
2544 assert_eq!(layout.children[0].y, -10.0);
2545
2546 assert_eq!(layout.children[0].children[0].width, 72.0);
2547 assert_eq!(layout.children[0].children[0].height, 72.0);
2548 assert_eq!(layout.children[0].children[0].x, 0.0);
2549 assert_eq!(layout.children[0].children[0].y, 0.0);
2550 }
2551
2552 #[test]
2553 fn size_defined_by_child_with_padding() {
2554 let layout = stretch::compute(&stretch::style::Node {
2555 padding: stretch::style::Edges {
2556 start: stretch::style::Dimension::Points(10.0),
2557 end: stretch::style::Dimension::Points(10.0),
2558 top: stretch::style::Dimension::Points(10.0),
2559 bottom: stretch::style::Dimension::Points(10.0),
2560 ..Default::default()
2561 },
2562 children: vec![stretch::style::Node {
2563 width: stretch::style::Dimension::Points(10.0),
2564 height: stretch::style::Dimension::Points(10.0),
2565 ..Default::default()
2566 }],
2567 ..Default::default()
2568 });
2569
2570 assert_eq!(layout.width, 30.0);
2571 assert_eq!(layout.height, 30.0);
2572 assert_eq!(layout.x, 0.0);
2573 assert_eq!(layout.y, 0.0);
2574
2575 assert_eq!(layout.children[0].width, 10.0);
2576 assert_eq!(layout.children[0].height, 10.0);
2577 assert_eq!(layout.children[0].x, 10.0);
2578 assert_eq!(layout.children[0].y, 10.0);
2579 }
2580
2581 #[test]
2582 fn absolute_layout_within_border() {
2583 let layout = stretch::compute(&stretch::style::Node {
2584 width: stretch::style::Dimension::Points(100.0),
2585 height: stretch::style::Dimension::Points(100.0),
2586 padding: stretch::style::Edges {
2587 start: stretch::style::Dimension::Points(10.0),
2588 end: stretch::style::Dimension::Points(10.0),
2589 top: stretch::style::Dimension::Points(10.0),
2590 bottom: stretch::style::Dimension::Points(10.0),
2591 ..Default::default()
2592 },
2593 border: stretch::style::Edges {
2594 start: stretch::style::Dimension::Points(10.0),
2595 end: stretch::style::Dimension::Points(10.0),
2596 top: stretch::style::Dimension::Points(10.0),
2597 bottom: stretch::style::Dimension::Points(10.0),
2598 ..Default::default()
2599 },
2600 children: vec![
2601 stretch::style::Node {
2602 position: stretch::style::Position::Absolute,
2603 width: stretch::style::Dimension::Points(50.0),
2604 height: stretch::style::Dimension::Points(50.0),
2605 start: stretch::style::Dimension::Points(0.0),
2606 top: stretch::style::Dimension::Points(0.0),
2607 ..Default::default()
2608 },
2609 stretch::style::Node {
2610 position: stretch::style::Position::Absolute,
2611 width: stretch::style::Dimension::Points(50.0),
2612 height: stretch::style::Dimension::Points(50.0),
2613 end: stretch::style::Dimension::Points(0.0),
2614 bottom: stretch::style::Dimension::Points(0.0),
2615 ..Default::default()
2616 },
2617 stretch::style::Node {
2618 position: stretch::style::Position::Absolute,
2619 width: stretch::style::Dimension::Points(50.0),
2620 height: stretch::style::Dimension::Points(50.0),
2621 margin: stretch::style::Edges {
2622 start: stretch::style::Dimension::Points(10.0),
2623 end: stretch::style::Dimension::Points(10.0),
2624 top: stretch::style::Dimension::Points(10.0),
2625 bottom: stretch::style::Dimension::Points(10.0),
2626 ..Default::default()
2627 },
2628 start: stretch::style::Dimension::Points(0.0),
2629 top: stretch::style::Dimension::Points(0.0),
2630 ..Default::default()
2631 },
2632 stretch::style::Node {
2633 position: stretch::style::Position::Absolute,
2634 width: stretch::style::Dimension::Points(50.0),
2635 height: stretch::style::Dimension::Points(50.0),
2636 margin: stretch::style::Edges {
2637 start: stretch::style::Dimension::Points(10.0),
2638 end: stretch::style::Dimension::Points(10.0),
2639 top: stretch::style::Dimension::Points(10.0),
2640 bottom: stretch::style::Dimension::Points(10.0),
2641 ..Default::default()
2642 },
2643 end: stretch::style::Dimension::Points(0.0),
2644 bottom: stretch::style::Dimension::Points(0.0),
2645 ..Default::default()
2646 },
2647 ],
2648 ..Default::default()
2649 });
2650
2651 assert_eq!(layout.width, 100.0);
2652 assert_eq!(layout.height, 100.0);
2653 assert_eq!(layout.x, 0.0);
2654 assert_eq!(layout.y, 0.0);
2655
2656 assert_eq!(layout.children[0].width, 50.0);
2657 assert_eq!(layout.children[0].height, 50.0);
2658 assert_eq!(layout.children[0].x, 10.0);
2659 assert_eq!(layout.children[0].y, 10.0);
2660
2661 assert_eq!(layout.children[1].width, 50.0);
2662 assert_eq!(layout.children[1].height, 50.0);
2663 assert_eq!(layout.children[1].x, 40.0);
2664 assert_eq!(layout.children[1].y, 40.0);
2665
2666 assert_eq!(layout.children[2].width, 50.0);
2667 assert_eq!(layout.children[2].height, 50.0);
2668 assert_eq!(layout.children[2].x, 20.0);
2669 assert_eq!(layout.children[2].y, 20.0);
2670
2671 assert_eq!(layout.children[3].width, 50.0);
2672 assert_eq!(layout.children[3].height, 50.0);
2673 assert_eq!(layout.children[3].x, 30.0);
2674 assert_eq!(layout.children[3].y, 30.0);
2675 }
2676
2677 #[test]
2678 fn margin_auto_left_fix_right_child_bigger_than_parent() {
2679 let layout = stretch::compute(&stretch::style::Node {
2680 justify_content: stretch::style::JustifyContent::Center,
2681 width: stretch::style::Dimension::Points(52.0),
2682 height: stretch::style::Dimension::Points(52.0),
2683 children: vec![stretch::style::Node {
2684 width: stretch::style::Dimension::Points(72.0),
2685 height: stretch::style::Dimension::Points(72.0),
2686 margin: stretch::style::Edges {
2687 start: stretch::style::Dimension::Auto,
2688 end: stretch::style::Dimension::Points(10.0),
2689 ..Default::default()
2690 },
2691 ..Default::default()
2692 }],
2693 ..Default::default()
2694 });
2695
2696 assert_eq!(layout.width, 52.0);
2697 assert_eq!(layout.height, 52.0);
2698 assert_eq!(layout.x, 0.0);
2699 assert_eq!(layout.y, 0.0);
2700
2701 assert_eq!(layout.children[0].width, 42.0);
2702 assert_eq!(layout.children[0].height, 72.0);
2703 assert_eq!(layout.children[0].x, 0.0);
2704 assert_eq!(layout.children[0].y, 0.0);
2705 }
2706
2707 #[test]
2708 fn justify_content_row_flex_end() {
2709 let layout = stretch::compute(&stretch::style::Node {
2710 justify_content: stretch::style::JustifyContent::FlexEnd,
2711 width: stretch::style::Dimension::Points(100.0),
2712 height: stretch::style::Dimension::Points(100.0),
2713 children: vec![
2714 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
2715 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
2716 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
2717 ],
2718 ..Default::default()
2719 });
2720
2721 assert_eq!(layout.width, 100.0);
2722 assert_eq!(layout.height, 100.0);
2723 assert_eq!(layout.x, 0.0);
2724 assert_eq!(layout.y, 0.0);
2725
2726 assert_eq!(layout.children[0].width, 10.0);
2727 assert_eq!(layout.children[0].height, 100.0);
2728 assert_eq!(layout.children[0].x, 70.0);
2729 assert_eq!(layout.children[0].y, 0.0);
2730
2731 assert_eq!(layout.children[1].width, 10.0);
2732 assert_eq!(layout.children[1].height, 100.0);
2733 assert_eq!(layout.children[1].x, 80.0);
2734 assert_eq!(layout.children[1].y, 0.0);
2735
2736 assert_eq!(layout.children[2].width, 10.0);
2737 assert_eq!(layout.children[2].height, 100.0);
2738 assert_eq!(layout.children[2].x, 90.0);
2739 assert_eq!(layout.children[2].y, 0.0);
2740 }
2741
2742 #[test]
2743 fn margin_bottom() {
2744 let layout = stretch::compute(&stretch::style::Node {
2745 flex_direction: stretch::style::FlexDirection::Column,
2746 justify_content: stretch::style::JustifyContent::FlexEnd,
2747 width: stretch::style::Dimension::Points(100.0),
2748 height: stretch::style::Dimension::Points(100.0),
2749 children: vec![stretch::style::Node {
2750 height: stretch::style::Dimension::Points(10.0),
2751 margin: stretch::style::Edges { bottom: stretch::style::Dimension::Points(10.0), ..Default::default() },
2752 ..Default::default()
2753 }],
2754 ..Default::default()
2755 });
2756
2757 assert_eq!(layout.width, 100.0);
2758 assert_eq!(layout.height, 100.0);
2759 assert_eq!(layout.x, 0.0);
2760 assert_eq!(layout.y, 0.0);
2761
2762 assert_eq!(layout.children[0].width, 100.0);
2763 assert_eq!(layout.children[0].height, 10.0);
2764 assert_eq!(layout.children[0].x, 0.0);
2765 assert_eq!(layout.children[0].y, 80.0);
2766 }
2767
2768 #[test]
2769 fn margin_should_not_be_part_of_max_height() {
2770 let layout = stretch::compute(&stretch::style::Node {
2771 width: stretch::style::Dimension::Points(250.0),
2772 height: stretch::style::Dimension::Points(250.0),
2773 children: vec![stretch::style::Node {
2774 width: stretch::style::Dimension::Points(100.0),
2775 height: stretch::style::Dimension::Points(100.0),
2776 max_height: stretch::style::Dimension::Points(100.0),
2777 margin: stretch::style::Edges { top: stretch::style::Dimension::Points(20.0), ..Default::default() },
2778 ..Default::default()
2779 }],
2780 ..Default::default()
2781 });
2782
2783 assert_eq!(layout.width, 250.0);
2784 assert_eq!(layout.height, 250.0);
2785 assert_eq!(layout.x, 0.0);
2786 assert_eq!(layout.y, 0.0);
2787
2788 assert_eq!(layout.children[0].width, 100.0);
2789 assert_eq!(layout.children[0].height, 100.0);
2790 assert_eq!(layout.children[0].x, 0.0);
2791 assert_eq!(layout.children[0].y, 20.0);
2792 }
2793
2794 #[test]
2795 fn percentage_flex_basis_cross_min_height() {
2796 let layout = stretch::compute(&stretch::style::Node {
2797 flex_direction: stretch::style::FlexDirection::Column,
2798 width: stretch::style::Dimension::Points(200.0),
2799 height: stretch::style::Dimension::Points(200.0),
2800 children: vec![
2801 stretch::style::Node {
2802 flex_grow: 1.0,
2803 min_height: stretch::style::Dimension::Percent(0.6),
2804 ..Default::default()
2805 },
2806 stretch::style::Node {
2807 flex_grow: 2.0,
2808 min_height: stretch::style::Dimension::Percent(0.1),
2809 ..Default::default()
2810 },
2811 ],
2812 ..Default::default()
2813 });
2814
2815 assert_eq!(layout.width, 200.0);
2816 assert_eq!(layout.height, 200.0);
2817 assert_eq!(layout.x, 0.0);
2818 assert_eq!(layout.y, 0.0);
2819
2820 assert_eq!(layout.children[0].width, 200.0);
2821 assert_eq!(layout.children[0].height, 140.0);
2822 assert_eq!(layout.children[0].x, 0.0);
2823 assert_eq!(layout.children[0].y, 0.0);
2824
2825 assert_eq!(layout.children[1].width, 200.0);
2826 assert_eq!(layout.children[1].height, 60.0);
2827 assert_eq!(layout.children[1].x, 0.0);
2828 assert_eq!(layout.children[1].y, 140.0);
2829 }
2830
2831 #[test]
2832 fn nested_overflowing_child_in_constraint_parent() {
2833 let layout = stretch::compute(&stretch::style::Node {
2834 width: stretch::style::Dimension::Points(100.0),
2835 height: stretch::style::Dimension::Points(100.0),
2836 children: vec![stretch::style::Node {
2837 width: stretch::style::Dimension::Points(100.0),
2838 height: stretch::style::Dimension::Points(100.0),
2839 children: vec![stretch::style::Node {
2840 width: stretch::style::Dimension::Points(200.0),
2841 height: stretch::style::Dimension::Points(200.0),
2842 ..Default::default()
2843 }],
2844 ..Default::default()
2845 }],
2846 ..Default::default()
2847 });
2848
2849 assert_eq!(layout.width, 100.0);
2850 assert_eq!(layout.height, 100.0);
2851 assert_eq!(layout.x, 0.0);
2852 assert_eq!(layout.y, 0.0);
2853
2854 assert_eq!(layout.children[0].width, 100.0);
2855 assert_eq!(layout.children[0].height, 100.0);
2856 assert_eq!(layout.children[0].x, 0.0);
2857 assert_eq!(layout.children[0].y, 0.0);
2858
2859 assert_eq!(layout.children[0].children[0].width, 100.0);
2860 assert_eq!(layout.children[0].children[0].height, 200.0);
2861 assert_eq!(layout.children[0].children[0].x, 0.0);
2862 assert_eq!(layout.children[0].children[0].y, 0.0);
2863 }
2864
2865 #[test]
2866 fn wrapped_row_within_align_items_center() {
2867 let layout = stretch::compute(&stretch::style::Node {
2868 flex_direction: stretch::style::FlexDirection::Column,
2869 align_items: stretch::style::AlignItems::Center,
2870 width: stretch::style::Dimension::Points(200.0),
2871 height: stretch::style::Dimension::Points(200.0),
2872 children: vec![stretch::style::Node {
2873 children: vec![
2874 stretch::style::Node {
2875 width: stretch::style::Dimension::Points(150.0),
2876 height: stretch::style::Dimension::Points(80.0),
2877 ..Default::default()
2878 },
2879 stretch::style::Node {
2880 width: stretch::style::Dimension::Points(80.0),
2881 height: stretch::style::Dimension::Points(80.0),
2882 ..Default::default()
2883 },
2884 ],
2885 ..Default::default()
2886 }],
2887 ..Default::default()
2888 });
2889
2890 assert_eq!(layout.width, 200.0);
2891 assert_eq!(layout.height, 200.0);
2892 assert_eq!(layout.x, 0.0);
2893 assert_eq!(layout.y, 0.0);
2894
2895 assert_eq!(layout.children[0].width, 200.0);
2896 assert_eq!(layout.children[0].height, 160.0);
2897 assert_eq!(layout.children[0].x, 0.0);
2898 assert_eq!(layout.children[0].y, 0.0);
2899
2900 assert_eq!(layout.children[0].children[0].width, 150.0);
2901 assert_eq!(layout.children[0].children[0].height, 80.0);
2902 assert_eq!(layout.children[0].children[0].x, 0.0);
2903 assert_eq!(layout.children[0].children[0].y, 0.0);
2904
2905 assert_eq!(layout.children[0].children[1].width, 80.0);
2906 assert_eq!(layout.children[0].children[1].height, 80.0);
2907 assert_eq!(layout.children[0].children[1].x, 0.0);
2908 assert_eq!(layout.children[0].children[1].y, 80.0);
2909 }
2910
2911 #[test]
2912 fn justify_content_column_center() {
2913 let layout = stretch::compute(&stretch::style::Node {
2914 flex_direction: stretch::style::FlexDirection::Column,
2915 justify_content: stretch::style::JustifyContent::Center,
2916 width: stretch::style::Dimension::Points(100.0),
2917 height: stretch::style::Dimension::Points(100.0),
2918 children: vec![
2919 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
2920 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
2921 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
2922 ],
2923 ..Default::default()
2924 });
2925
2926 assert_eq!(layout.width, 100.0);
2927 assert_eq!(layout.height, 100.0);
2928 assert_eq!(layout.x, 0.0);
2929 assert_eq!(layout.y, 0.0);
2930
2931 assert_eq!(layout.children[0].width, 100.0);
2932 assert_eq!(layout.children[0].height, 10.0);
2933 assert_eq!(layout.children[0].x, 0.0);
2934 assert_eq!(layout.children[0].y, 35.0);
2935
2936 assert_eq!(layout.children[1].width, 100.0);
2937 assert_eq!(layout.children[1].height, 10.0);
2938 assert_eq!(layout.children[1].x, 0.0);
2939 assert_eq!(layout.children[1].y, 45.0);
2940
2941 assert_eq!(layout.children[2].width, 100.0);
2942 assert_eq!(layout.children[2].height, 10.0);
2943 assert_eq!(layout.children[2].x, 0.0);
2944 assert_eq!(layout.children[2].y, 55.0);
2945 }
2946
2947 #[test]
2948 fn align_items_center_child_with_margin_bigger_than_parent() {
2949 let layout = stretch::compute(&stretch::style::Node {
2950 align_items: stretch::style::AlignItems::Center,
2951 justify_content: stretch::style::JustifyContent::Center,
2952 width: stretch::style::Dimension::Points(52.0),
2953 height: stretch::style::Dimension::Points(52.0),
2954 children: vec![stretch::style::Node {
2955 align_items: stretch::style::AlignItems::Center,
2956 children: vec![stretch::style::Node {
2957 width: stretch::style::Dimension::Points(52.0),
2958 height: stretch::style::Dimension::Points(52.0),
2959 margin: stretch::style::Edges {
2960 start: stretch::style::Dimension::Points(10.0),
2961 end: stretch::style::Dimension::Points(10.0),
2962 ..Default::default()
2963 },
2964 ..Default::default()
2965 }],
2966 ..Default::default()
2967 }],
2968 ..Default::default()
2969 });
2970
2971 assert_eq!(layout.width, 52.0);
2972 assert_eq!(layout.height, 52.0);
2973 assert_eq!(layout.x, 0.0);
2974 assert_eq!(layout.y, 0.0);
2975
2976 assert_eq!(layout.children[0].width, 72.0);
2977 assert_eq!(layout.children[0].height, 52.0);
2978 assert_eq!(layout.children[0].x, -10.0);
2979 assert_eq!(layout.children[0].y, 0.0);
2980
2981 assert_eq!(layout.children[0].children[0].width, 52.0);
2982 assert_eq!(layout.children[0].children[0].height, 52.0);
2983 assert_eq!(layout.children[0].children[0].x, 10.0);
2984 assert_eq!(layout.children[0].children[0].y, 0.0);
2985 }
2986
2987 #[test]
2988 fn percentage_width_height() {
2989 let layout = stretch::compute(&stretch::style::Node {
2990 width: stretch::style::Dimension::Points(200.0),
2991 height: stretch::style::Dimension::Points(200.0),
2992 children: vec![stretch::style::Node {
2993 width: stretch::style::Dimension::Percent(0.3),
2994 height: stretch::style::Dimension::Percent(0.3),
2995 ..Default::default()
2996 }],
2997 ..Default::default()
2998 });
2999
3000 assert_eq!(layout.width, 200.0);
3001 assert_eq!(layout.height, 200.0);
3002 assert_eq!(layout.x, 0.0);
3003 assert_eq!(layout.y, 0.0);
3004
3005 assert_eq!(layout.children[0].width, 60.0);
3006 assert_eq!(layout.children[0].height, 60.0);
3007 assert_eq!(layout.children[0].x, 0.0);
3008 assert_eq!(layout.children[0].y, 0.0);
3009 }
3010
3011 #[test]
3012 fn margin_auto_mutiple_children_column() {
3013 let layout = stretch::compute(&stretch::style::Node {
3014 flex_direction: stretch::style::FlexDirection::Column,
3015 align_items: stretch::style::AlignItems::Center,
3016 width: stretch::style::Dimension::Points(200.0),
3017 height: stretch::style::Dimension::Points(200.0),
3018 children: vec![
3019 stretch::style::Node {
3020 width: stretch::style::Dimension::Points(50.0),
3021 height: stretch::style::Dimension::Points(50.0),
3022 margin: stretch::style::Edges { top: stretch::style::Dimension::Auto, ..Default::default() },
3023 ..Default::default()
3024 },
3025 stretch::style::Node {
3026 width: stretch::style::Dimension::Points(50.0),
3027 height: stretch::style::Dimension::Points(50.0),
3028 margin: stretch::style::Edges { top: stretch::style::Dimension::Auto, ..Default::default() },
3029 ..Default::default()
3030 },
3031 stretch::style::Node {
3032 width: stretch::style::Dimension::Points(50.0),
3033 height: stretch::style::Dimension::Points(50.0),
3034 ..Default::default()
3035 },
3036 ],
3037 ..Default::default()
3038 });
3039
3040 assert_eq!(layout.width, 200.0);
3041 assert_eq!(layout.height, 200.0);
3042 assert_eq!(layout.x, 0.0);
3043 assert_eq!(layout.y, 0.0);
3044
3045 assert_eq!(layout.children[0].width, 50.0);
3046 assert_eq!(layout.children[0].height, 50.0);
3047 assert_eq!(layout.children[0].x, 75.0);
3048 assert_eq!(layout.children[0].y, 25.0);
3049
3050 assert_eq!(layout.children[1].width, 50.0);
3051 assert_eq!(layout.children[1].height, 50.0);
3052 assert_eq!(layout.children[1].x, 75.0);
3053 assert_eq!(layout.children[1].y, 100.0);
3054
3055 assert_eq!(layout.children[2].width, 50.0);
3056 assert_eq!(layout.children[2].height, 50.0);
3057 assert_eq!(layout.children[2].x, 75.0);
3058 assert_eq!(layout.children[2].y, 150.0);
3059 }
3060
3061 #[test]
3062 fn min_width_overrides_width_on_root() {
3063 let layout = stretch::compute(&stretch::style::Node {
3064 width: stretch::style::Dimension::Points(50.0),
3065 min_width: stretch::style::Dimension::Points(100.0),
3066 ..Default::default()
3067 });
3068
3069 assert_eq!(layout.width, 100.0);
3070 assert_eq!(layout.height, 0.0);
3071 assert_eq!(layout.x, 0.0);
3072 assert_eq!(layout.y, 0.0);
3073 }
3074
3075 #[test]
3076 fn flex_wrap_wrap_to_child_height() {
3077 let layout = stretch::compute(&stretch::style::Node {
3078 flex_direction: stretch::style::FlexDirection::Column,
3079 children: vec![
3080 stretch::style::Node {
3081 align_items: stretch::style::AlignItems::FlexStart,
3082 children: vec![stretch::style::Node {
3083 flex_direction: stretch::style::FlexDirection::Column,
3084 width: stretch::style::Dimension::Points(100.0),
3085 children: vec![stretch::style::Node {
3086 width: stretch::style::Dimension::Points(100.0),
3087 height: stretch::style::Dimension::Points(100.0),
3088 ..Default::default()
3089 }],
3090 ..Default::default()
3091 }],
3092 ..Default::default()
3093 },
3094 stretch::style::Node {
3095 width: stretch::style::Dimension::Points(100.0),
3096 height: stretch::style::Dimension::Points(100.0),
3097 ..Default::default()
3098 },
3099 ],
3100 ..Default::default()
3101 });
3102
3103 assert_eq!(layout.width, 100.0);
3104 assert_eq!(layout.height, 200.0);
3105 assert_eq!(layout.x, 0.0);
3106 assert_eq!(layout.y, 0.0);
3107
3108 assert_eq!(layout.children[0].width, 100.0);
3109 assert_eq!(layout.children[0].height, 100.0);
3110 assert_eq!(layout.children[0].x, 0.0);
3111 assert_eq!(layout.children[0].y, 0.0);
3112
3113 assert_eq!(layout.children[0].children[0].width, 100.0);
3114 assert_eq!(layout.children[0].children[0].height, 100.0);
3115 assert_eq!(layout.children[0].children[0].x, 0.0);
3116 assert_eq!(layout.children[0].children[0].y, 0.0);
3117
3118 assert_eq!(layout.children[0].children[0].children[0].width, 100.0);
3119 assert_eq!(layout.children[0].children[0].children[0].height, 100.0);
3120 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
3121 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
3122
3123 assert_eq!(layout.children[1].width, 100.0);
3124 assert_eq!(layout.children[1].height, 100.0);
3125 assert_eq!(layout.children[1].x, 0.0);
3126 assert_eq!(layout.children[1].y, 100.0);
3127 }
3128
3129 #[test]
3130 fn wrap_nodes_with_content_sizing_margin_cross() {
3131 let layout = stretch::compute(&stretch::style::Node {
3132 flex_direction: stretch::style::FlexDirection::Column,
3133 width: stretch::style::Dimension::Points(500.0),
3134 height: stretch::style::Dimension::Points(500.0),
3135 children: vec![stretch::style::Node {
3136 width: stretch::style::Dimension::Points(70.0),
3137 children: vec![
3138 stretch::style::Node {
3139 flex_direction: stretch::style::FlexDirection::Column,
3140 children: vec![stretch::style::Node {
3141 width: stretch::style::Dimension::Points(40.0),
3142 height: stretch::style::Dimension::Points(40.0),
3143 ..Default::default()
3144 }],
3145 ..Default::default()
3146 },
3147 stretch::style::Node {
3148 flex_direction: stretch::style::FlexDirection::Column,
3149 margin: stretch::style::Edges {
3150 top: stretch::style::Dimension::Points(10.0),
3151 ..Default::default()
3152 },
3153 children: vec![stretch::style::Node {
3154 width: stretch::style::Dimension::Points(40.0),
3155 height: stretch::style::Dimension::Points(40.0),
3156 ..Default::default()
3157 }],
3158 ..Default::default()
3159 },
3160 ],
3161 ..Default::default()
3162 }],
3163 ..Default::default()
3164 });
3165
3166 assert_eq!(layout.width, 500.0);
3167 assert_eq!(layout.height, 500.0);
3168 assert_eq!(layout.x, 0.0);
3169 assert_eq!(layout.y, 0.0);
3170
3171 assert_eq!(layout.children[0].width, 70.0);
3172 assert_eq!(layout.children[0].height, 90.0);
3173 assert_eq!(layout.children[0].x, 0.0);
3174 assert_eq!(layout.children[0].y, 0.0);
3175
3176 assert_eq!(layout.children[0].children[0].width, 40.0);
3177 assert_eq!(layout.children[0].children[0].height, 40.0);
3178 assert_eq!(layout.children[0].children[0].x, 0.0);
3179 assert_eq!(layout.children[0].children[0].y, 0.0);
3180
3181 assert_eq!(layout.children[0].children[0].children[0].width, 40.0);
3182 assert_eq!(layout.children[0].children[0].children[0].height, 40.0);
3183 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
3184 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
3185
3186 assert_eq!(layout.children[0].children[1].width, 40.0);
3187 assert_eq!(layout.children[0].children[1].height, 40.0);
3188 assert_eq!(layout.children[0].children[1].x, 0.0);
3189 assert_eq!(layout.children[0].children[1].y, 50.0);
3190
3191 assert_eq!(layout.children[0].children[1].children[0].width, 40.0);
3192 assert_eq!(layout.children[0].children[1].children[0].height, 40.0);
3193 assert_eq!(layout.children[0].children[1].children[0].x, 0.0);
3194 assert_eq!(layout.children[0].children[1].children[0].y, 0.0);
3195 }
3196
3197 #[test]
3198 fn align_baseline() {
3199 let layout = stretch::compute(&stretch::style::Node {
3200 align_items: stretch::style::AlignItems::Baseline,
3201 width: stretch::style::Dimension::Points(100.0),
3202 height: stretch::style::Dimension::Points(100.0),
3203 children: vec![
3204 stretch::style::Node {
3205 width: stretch::style::Dimension::Points(50.0),
3206 height: stretch::style::Dimension::Points(50.0),
3207 ..Default::default()
3208 },
3209 stretch::style::Node {
3210 width: stretch::style::Dimension::Points(50.0),
3211 height: stretch::style::Dimension::Points(20.0),
3212 ..Default::default()
3213 },
3214 ],
3215 ..Default::default()
3216 });
3217
3218 assert_eq!(layout.width, 100.0);
3219 assert_eq!(layout.height, 100.0);
3220 assert_eq!(layout.x, 0.0);
3221 assert_eq!(layout.y, 0.0);
3222
3223 assert_eq!(layout.children[0].width, 50.0);
3224 assert_eq!(layout.children[0].height, 50.0);
3225 assert_eq!(layout.children[0].x, 0.0);
3226 assert_eq!(layout.children[0].y, 0.0);
3227
3228 assert_eq!(layout.children[1].width, 50.0);
3229 assert_eq!(layout.children[1].height, 20.0);
3230 assert_eq!(layout.children[1].x, 50.0);
3231 assert_eq!(layout.children[1].y, 30.0);
3232 }
3233
3234 #[test]
3235 fn margin_with_sibling_row() {
3236 let layout = stretch::compute(&stretch::style::Node {
3237 width: stretch::style::Dimension::Points(100.0),
3238 height: stretch::style::Dimension::Points(100.0),
3239 children: vec![
3240 stretch::style::Node {
3241 flex_grow: 1.0,
3242 margin: stretch::style::Edges {
3243 end: stretch::style::Dimension::Points(10.0),
3244 ..Default::default()
3245 },
3246 ..Default::default()
3247 },
3248 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
3249 ],
3250 ..Default::default()
3251 });
3252
3253 assert_eq!(layout.width, 100.0);
3254 assert_eq!(layout.height, 100.0);
3255 assert_eq!(layout.x, 0.0);
3256 assert_eq!(layout.y, 0.0);
3257
3258 assert_eq!(layout.children[0].width, 45.0);
3259 assert_eq!(layout.children[0].height, 100.0);
3260 assert_eq!(layout.children[0].x, 0.0);
3261 assert_eq!(layout.children[0].y, 0.0);
3262
3263 assert_eq!(layout.children[1].width, 45.0);
3264 assert_eq!(layout.children[1].height, 100.0);
3265 assert_eq!(layout.children[1].x, 55.0);
3266 assert_eq!(layout.children[1].y, 0.0);
3267 }
3268
3269 #[test]
3270 fn wrap_reverse_row_align_content_space_around() {
3271 let layout = stretch::compute(&stretch::style::Node {
3272 align_content: stretch::style::AlignContent::SpaceAround,
3273 width: stretch::style::Dimension::Points(100.0),
3274 children: vec![
3275 stretch::style::Node {
3276 width: stretch::style::Dimension::Points(30.0),
3277 height: stretch::style::Dimension::Points(10.0),
3278 ..Default::default()
3279 },
3280 stretch::style::Node {
3281 width: stretch::style::Dimension::Points(30.0),
3282 height: stretch::style::Dimension::Points(20.0),
3283 ..Default::default()
3284 },
3285 stretch::style::Node {
3286 width: stretch::style::Dimension::Points(30.0),
3287 height: stretch::style::Dimension::Points(30.0),
3288 ..Default::default()
3289 },
3290 stretch::style::Node {
3291 width: stretch::style::Dimension::Points(30.0),
3292 height: stretch::style::Dimension::Points(40.0),
3293 ..Default::default()
3294 },
3295 stretch::style::Node {
3296 width: stretch::style::Dimension::Points(30.0),
3297 height: stretch::style::Dimension::Points(50.0),
3298 ..Default::default()
3299 },
3300 ],
3301 ..Default::default()
3302 });
3303
3304 assert_eq!(layout.width, 100.0);
3305 assert_eq!(layout.height, 80.0);
3306 assert_eq!(layout.x, 0.0);
3307 assert_eq!(layout.y, 0.0);
3308
3309 assert_eq!(layout.children[0].width, 30.0);
3310 assert_eq!(layout.children[0].height, 10.0);
3311 assert_eq!(layout.children[0].x, 0.0);
3312 assert_eq!(layout.children[0].y, 70.0);
3313
3314 assert_eq!(layout.children[1].width, 30.0);
3315 assert_eq!(layout.children[1].height, 20.0);
3316 assert_eq!(layout.children[1].x, 30.0);
3317 assert_eq!(layout.children[1].y, 60.0);
3318
3319 assert_eq!(layout.children[2].width, 30.0);
3320 assert_eq!(layout.children[2].height, 30.0);
3321 assert_eq!(layout.children[2].x, 60.0);
3322 assert_eq!(layout.children[2].y, 50.0);
3323
3324 assert_eq!(layout.children[3].width, 30.0);
3325 assert_eq!(layout.children[3].height, 40.0);
3326 assert_eq!(layout.children[3].x, 0.0);
3327 assert_eq!(layout.children[3].y, 10.0);
3328
3329 assert_eq!(layout.children[4].width, 30.0);
3330 assert_eq!(layout.children[4].height, 50.0);
3331 assert_eq!(layout.children[4].x, 30.0);
3332 assert_eq!(layout.children[4].y, 0.0);
3333 }
3334
3335 #[test]
3336 fn align_items_center_with_child_margin() {
3337 let layout = stretch::compute(&stretch::style::Node {
3338 align_items: stretch::style::AlignItems::Center,
3339 width: stretch::style::Dimension::Points(100.0),
3340 height: stretch::style::Dimension::Points(100.0),
3341 children: vec![stretch::style::Node {
3342 width: stretch::style::Dimension::Points(10.0),
3343 height: stretch::style::Dimension::Points(10.0),
3344 margin: stretch::style::Edges { top: stretch::style::Dimension::Points(10.0), ..Default::default() },
3345 ..Default::default()
3346 }],
3347 ..Default::default()
3348 });
3349
3350 assert_eq!(layout.width, 100.0);
3351 assert_eq!(layout.height, 100.0);
3352 assert_eq!(layout.x, 0.0);
3353 assert_eq!(layout.y, 0.0);
3354
3355 assert_eq!(layout.children[0].width, 10.0);
3356 assert_eq!(layout.children[0].height, 10.0);
3357 assert_eq!(layout.children[0].x, 0.0);
3358 assert_eq!(layout.children[0].y, 50.0);
3359 }
3360
3361 #[test]
3362 fn flex_direction_column_reverse() {
3363 let layout = stretch::compute(&stretch::style::Node {
3364 flex_direction: stretch::style::FlexDirection::ColumnReverse,
3365 width: stretch::style::Dimension::Points(100.0),
3366 height: stretch::style::Dimension::Points(100.0),
3367 children: vec![
3368 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
3369 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
3370 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
3371 ],
3372 ..Default::default()
3373 });
3374
3375 assert_eq!(layout.width, 100.0);
3376 assert_eq!(layout.height, 100.0);
3377 assert_eq!(layout.x, 0.0);
3378 assert_eq!(layout.y, 0.0);
3379
3380 assert_eq!(layout.children[0].width, 100.0);
3381 assert_eq!(layout.children[0].height, 10.0);
3382 assert_eq!(layout.children[0].x, 0.0);
3383 assert_eq!(layout.children[0].y, 90.0);
3384
3385 assert_eq!(layout.children[1].width, 100.0);
3386 assert_eq!(layout.children[1].height, 10.0);
3387 assert_eq!(layout.children[1].x, 0.0);
3388 assert_eq!(layout.children[1].y, 80.0);
3389
3390 assert_eq!(layout.children[2].width, 100.0);
3391 assert_eq!(layout.children[2].height, 10.0);
3392 assert_eq!(layout.children[2].x, 0.0);
3393 assert_eq!(layout.children[2].y, 70.0);
3394 }
3395
3396 #[test]
3397 fn flex_direction_column() {
3398 let layout = stretch::compute(&stretch::style::Node {
3399 flex_direction: stretch::style::FlexDirection::Column,
3400 width: stretch::style::Dimension::Points(100.0),
3401 height: stretch::style::Dimension::Points(100.0),
3402 children: vec![
3403 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
3404 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
3405 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
3406 ],
3407 ..Default::default()
3408 });
3409
3410 assert_eq!(layout.width, 100.0);
3411 assert_eq!(layout.height, 100.0);
3412 assert_eq!(layout.x, 0.0);
3413 assert_eq!(layout.y, 0.0);
3414
3415 assert_eq!(layout.children[0].width, 100.0);
3416 assert_eq!(layout.children[0].height, 10.0);
3417 assert_eq!(layout.children[0].x, 0.0);
3418 assert_eq!(layout.children[0].y, 0.0);
3419
3420 assert_eq!(layout.children[1].width, 100.0);
3421 assert_eq!(layout.children[1].height, 10.0);
3422 assert_eq!(layout.children[1].x, 0.0);
3423 assert_eq!(layout.children[1].y, 10.0);
3424
3425 assert_eq!(layout.children[2].width, 100.0);
3426 assert_eq!(layout.children[2].height, 10.0);
3427 assert_eq!(layout.children[2].x, 0.0);
3428 assert_eq!(layout.children[2].y, 20.0);
3429 }
3430
3431 #[test]
3432 fn align_items_center_child_without_margin_bigger_than_parent() {
3433 let layout = stretch::compute(&stretch::style::Node {
3434 align_items: stretch::style::AlignItems::Center,
3435 justify_content: stretch::style::JustifyContent::Center,
3436 width: stretch::style::Dimension::Points(52.0),
3437 height: stretch::style::Dimension::Points(52.0),
3438 children: vec![stretch::style::Node {
3439 align_items: stretch::style::AlignItems::Center,
3440 children: vec![stretch::style::Node {
3441 width: stretch::style::Dimension::Points(72.0),
3442 height: stretch::style::Dimension::Points(72.0),
3443 ..Default::default()
3444 }],
3445 ..Default::default()
3446 }],
3447 ..Default::default()
3448 });
3449
3450 assert_eq!(layout.width, 52.0);
3451 assert_eq!(layout.height, 52.0);
3452 assert_eq!(layout.x, 0.0);
3453 assert_eq!(layout.y, 0.0);
3454
3455 assert_eq!(layout.children[0].width, 72.0);
3456 assert_eq!(layout.children[0].height, 72.0);
3457 assert_eq!(layout.children[0].x, -10.0);
3458 assert_eq!(layout.children[0].y, -10.0);
3459
3460 assert_eq!(layout.children[0].children[0].width, 72.0);
3461 assert_eq!(layout.children[0].children[0].height, 72.0);
3462 assert_eq!(layout.children[0].children[0].x, 0.0);
3463 assert_eq!(layout.children[0].children[0].y, 0.0);
3464 }
3465
3466 #[test]
3467 fn align_self_flex_end_override_flex_start() {
3468 let layout = stretch::compute(&stretch::style::Node {
3469 align_items: stretch::style::AlignItems::FlexStart,
3470 width: stretch::style::Dimension::Points(100.0),
3471 height: stretch::style::Dimension::Points(100.0),
3472 children: vec![stretch::style::Node {
3473 align_self: stretch::style::AlignSelf::FlexEnd,
3474 width: stretch::style::Dimension::Points(10.0),
3475 height: stretch::style::Dimension::Points(10.0),
3476 ..Default::default()
3477 }],
3478 ..Default::default()
3479 });
3480
3481 assert_eq!(layout.width, 100.0);
3482 assert_eq!(layout.height, 100.0);
3483 assert_eq!(layout.x, 0.0);
3484 assert_eq!(layout.y, 0.0);
3485
3486 assert_eq!(layout.children[0].width, 10.0);
3487 assert_eq!(layout.children[0].height, 10.0);
3488 assert_eq!(layout.children[0].x, 0.0);
3489 assert_eq!(layout.children[0].y, 90.0);
3490 }
3491
3492 #[test]
3493 fn rounding_total_fractial() {
3494 let layout = stretch::compute(&stretch::style::Node {
3495 flex_direction: stretch::style::FlexDirection::Column,
3496 width: stretch::style::Dimension::Points(87.4),
3497 height: stretch::style::Dimension::Points(113.4),
3498 children: vec![
3499 stretch::style::Node {
3500 flex_grow: 0.7,
3501 flex_basis: stretch::style::Dimension::Points(50.3),
3502 height: stretch::style::Dimension::Points(20.3),
3503 ..Default::default()
3504 },
3505 stretch::style::Node {
3506 flex_grow: 1.6,
3507 height: stretch::style::Dimension::Points(10.0),
3508 ..Default::default()
3509 },
3510 stretch::style::Node {
3511 flex_grow: 1.1,
3512 height: stretch::style::Dimension::Points(10.7),
3513 ..Default::default()
3514 },
3515 ],
3516 ..Default::default()
3517 });
3518
3519 assert_eq!(layout.width, 87.0);
3520 assert_eq!(layout.height, 113.0);
3521 assert_eq!(layout.x, 0.0);
3522 assert_eq!(layout.y, 0.0);
3523
3524 assert_eq!(layout.children[0].width, 87.0);
3525 assert_eq!(layout.children[0].height, 59.0);
3526 assert_eq!(layout.children[0].x, 0.0);
3527 assert_eq!(layout.children[0].y, 0.0);
3528
3529 assert_eq!(layout.children[1].width, 87.0);
3530 assert_eq!(layout.children[1].height, 30.0);
3531 assert_eq!(layout.children[1].x, 0.0);
3532 assert_eq!(layout.children[1].y, 59.0);
3533
3534 assert_eq!(layout.children[2].width, 87.0);
3535 assert_eq!(layout.children[2].height, 24.0);
3536 assert_eq!(layout.children[2].x, 0.0);
3537 assert_eq!(layout.children[2].y, 89.0);
3538 }
3539
3540 #[test]
3541 fn align_baseline_child_multiline() {
3542 let layout = stretch::compute(&stretch::style::Node {
3543 align_items: stretch::style::AlignItems::Baseline,
3544 width: stretch::style::Dimension::Points(100.0),
3545 children: vec![
3546 stretch::style::Node {
3547 width: stretch::style::Dimension::Points(50.0),
3548 height: stretch::style::Dimension::Points(60.0),
3549 ..Default::default()
3550 },
3551 stretch::style::Node {
3552 width: stretch::style::Dimension::Points(50.0),
3553 children: vec![
3554 stretch::style::Node {
3555 width: stretch::style::Dimension::Points(25.0),
3556 height: stretch::style::Dimension::Points(20.0),
3557 ..Default::default()
3558 },
3559 stretch::style::Node {
3560 width: stretch::style::Dimension::Points(25.0),
3561 height: stretch::style::Dimension::Points(10.0),
3562 ..Default::default()
3563 },
3564 stretch::style::Node {
3565 width: stretch::style::Dimension::Points(25.0),
3566 height: stretch::style::Dimension::Points(20.0),
3567 ..Default::default()
3568 },
3569 stretch::style::Node {
3570 width: stretch::style::Dimension::Points(25.0),
3571 height: stretch::style::Dimension::Points(10.0),
3572 ..Default::default()
3573 },
3574 ],
3575 ..Default::default()
3576 },
3577 ],
3578 ..Default::default()
3579 });
3580
3581 assert_eq!(layout.width, 100.0);
3582 assert_eq!(layout.height, 80.0);
3583 assert_eq!(layout.x, 0.0);
3584 assert_eq!(layout.y, 0.0);
3585
3586 assert_eq!(layout.children[0].width, 50.0);
3587 assert_eq!(layout.children[0].height, 60.0);
3588 assert_eq!(layout.children[0].x, 0.0);
3589 assert_eq!(layout.children[0].y, 0.0);
3590
3591 assert_eq!(layout.children[1].width, 50.0);
3592 assert_eq!(layout.children[1].height, 40.0);
3593 assert_eq!(layout.children[1].x, 50.0);
3594 assert_eq!(layout.children[1].y, 40.0);
3595
3596 assert_eq!(layout.children[1].children[0].width, 25.0);
3597 assert_eq!(layout.children[1].children[0].height, 20.0);
3598 assert_eq!(layout.children[1].children[0].x, 0.0);
3599 assert_eq!(layout.children[1].children[0].y, 0.0);
3600
3601 assert_eq!(layout.children[1].children[1].width, 25.0);
3602 assert_eq!(layout.children[1].children[1].height, 10.0);
3603 assert_eq!(layout.children[1].children[1].x, 25.0);
3604 assert_eq!(layout.children[1].children[1].y, 0.0);
3605
3606 assert_eq!(layout.children[1].children[2].width, 25.0);
3607 assert_eq!(layout.children[1].children[2].height, 20.0);
3608 assert_eq!(layout.children[1].children[2].x, 0.0);
3609 assert_eq!(layout.children[1].children[2].y, 20.0);
3610
3611 assert_eq!(layout.children[1].children[3].width, 25.0);
3612 assert_eq!(layout.children[1].children[3].height, 10.0);
3613 assert_eq!(layout.children[1].children[3].x, 25.0);
3614 assert_eq!(layout.children[1].children[3].y, 20.0);
3615 }
3616
3617 #[test]
3618 fn absolute_layout_align_items_center_on_child_only() {
3619 let layout = stretch::compute(&stretch::style::Node {
3620 width: stretch::style::Dimension::Points(110.0),
3621 height: stretch::style::Dimension::Points(100.0),
3622 children: vec![stretch::style::Node {
3623 position: stretch::style::Position::Absolute,
3624 align_self: stretch::style::AlignSelf::Center,
3625 width: stretch::style::Dimension::Points(60.0),
3626 height: stretch::style::Dimension::Points(40.0),
3627 ..Default::default()
3628 }],
3629 ..Default::default()
3630 });
3631
3632 assert_eq!(layout.width, 110.0);
3633 assert_eq!(layout.height, 100.0);
3634 assert_eq!(layout.x, 0.0);
3635 assert_eq!(layout.y, 0.0);
3636
3637 assert_eq!(layout.children[0].width, 60.0);
3638 assert_eq!(layout.children[0].height, 40.0);
3639 assert_eq!(layout.children[0].x, 0.0);
3640 assert_eq!(layout.children[0].y, 30.0);
3641 }
3642
3643 #[test]
3644 fn wrap_reverse_row_single_line_different_size() {
3645 let layout = stretch::compute(&stretch::style::Node {
3646 align_content: stretch::style::AlignContent::FlexStart,
3647 width: stretch::style::Dimension::Points(300.0),
3648 children: vec![
3649 stretch::style::Node {
3650 width: stretch::style::Dimension::Points(30.0),
3651 height: stretch::style::Dimension::Points(10.0),
3652 ..Default::default()
3653 },
3654 stretch::style::Node {
3655 width: stretch::style::Dimension::Points(30.0),
3656 height: stretch::style::Dimension::Points(20.0),
3657 ..Default::default()
3658 },
3659 stretch::style::Node {
3660 width: stretch::style::Dimension::Points(30.0),
3661 height: stretch::style::Dimension::Points(30.0),
3662 ..Default::default()
3663 },
3664 stretch::style::Node {
3665 width: stretch::style::Dimension::Points(30.0),
3666 height: stretch::style::Dimension::Points(40.0),
3667 ..Default::default()
3668 },
3669 stretch::style::Node {
3670 width: stretch::style::Dimension::Points(30.0),
3671 height: stretch::style::Dimension::Points(50.0),
3672 ..Default::default()
3673 },
3674 ],
3675 ..Default::default()
3676 });
3677
3678 assert_eq!(layout.width, 300.0);
3679 assert_eq!(layout.height, 50.0);
3680 assert_eq!(layout.x, 0.0);
3681 assert_eq!(layout.y, 0.0);
3682
3683 assert_eq!(layout.children[0].width, 30.0);
3684 assert_eq!(layout.children[0].height, 10.0);
3685 assert_eq!(layout.children[0].x, 0.0);
3686 assert_eq!(layout.children[0].y, 40.0);
3687
3688 assert_eq!(layout.children[1].width, 30.0);
3689 assert_eq!(layout.children[1].height, 20.0);
3690 assert_eq!(layout.children[1].x, 30.0);
3691 assert_eq!(layout.children[1].y, 30.0);
3692
3693 assert_eq!(layout.children[2].width, 30.0);
3694 assert_eq!(layout.children[2].height, 30.0);
3695 assert_eq!(layout.children[2].x, 60.0);
3696 assert_eq!(layout.children[2].y, 20.0);
3697
3698 assert_eq!(layout.children[3].width, 30.0);
3699 assert_eq!(layout.children[3].height, 40.0);
3700 assert_eq!(layout.children[3].x, 90.0);
3701 assert_eq!(layout.children[3].y, 10.0);
3702
3703 assert_eq!(layout.children[4].width, 30.0);
3704 assert_eq!(layout.children[4].height, 50.0);
3705 assert_eq!(layout.children[4].x, 120.0);
3706 assert_eq!(layout.children[4].y, 0.0);
3707 }
3708
3709 #[test]
3710 fn rounding_fractial_input_4() {
3711 let layout = stretch::compute(&stretch::style::Node {
3712 flex_direction: stretch::style::FlexDirection::Column,
3713 width: stretch::style::Dimension::Points(100.0),
3714 height: stretch::style::Dimension::Points(113.4),
3715 children: vec![
3716 stretch::style::Node {
3717 flex_grow: 1.0,
3718 flex_basis: stretch::style::Dimension::Points(50.0),
3719 height: stretch::style::Dimension::Points(20.0),
3720 ..Default::default()
3721 },
3722 stretch::style::Node {
3723 flex_grow: 1.0,
3724 height: stretch::style::Dimension::Points(10.0),
3725 ..Default::default()
3726 },
3727 stretch::style::Node {
3728 flex_grow: 1.0,
3729 height: stretch::style::Dimension::Points(10.0),
3730 ..Default::default()
3731 },
3732 ],
3733 ..Default::default()
3734 });
3735
3736 assert_eq!(layout.width, 100.0);
3737 assert_eq!(layout.height, 113.0);
3738 assert_eq!(layout.x, 0.0);
3739 assert_eq!(layout.y, 0.0);
3740
3741 assert_eq!(layout.children[0].width, 100.0);
3742 assert_eq!(layout.children[0].height, 64.0);
3743 assert_eq!(layout.children[0].x, 0.0);
3744 assert_eq!(layout.children[0].y, 0.0);
3745
3746 assert_eq!(layout.children[1].width, 100.0);
3747 assert_eq!(layout.children[1].height, 25.0);
3748 assert_eq!(layout.children[1].x, 0.0);
3749 assert_eq!(layout.children[1].y, 64.0);
3750
3751 assert_eq!(layout.children[2].width, 100.0);
3752 assert_eq!(layout.children[2].height, 24.0);
3753 assert_eq!(layout.children[2].x, 0.0);
3754 assert_eq!(layout.children[2].y, 89.0);
3755 }
3756
3757 #[test]
3758 fn padding_flex_child() {
3759 let layout = stretch::compute(&stretch::style::Node {
3760 width: stretch::style::Dimension::Points(100.0),
3761 height: stretch::style::Dimension::Points(100.0),
3762 padding: stretch::style::Edges {
3763 start: stretch::style::Dimension::Points(10.0),
3764 end: stretch::style::Dimension::Points(10.0),
3765 top: stretch::style::Dimension::Points(10.0),
3766 bottom: stretch::style::Dimension::Points(10.0),
3767 ..Default::default()
3768 },
3769 children: vec![stretch::style::Node {
3770 flex_grow: 1.0,
3771 width: stretch::style::Dimension::Points(10.0),
3772 ..Default::default()
3773 }],
3774 ..Default::default()
3775 });
3776
3777 assert_eq!(layout.width, 100.0);
3778 assert_eq!(layout.height, 100.0);
3779 assert_eq!(layout.x, 0.0);
3780 assert_eq!(layout.y, 0.0);
3781
3782 assert_eq!(layout.children[0].width, 80.0);
3783 assert_eq!(layout.children[0].height, 80.0);
3784 assert_eq!(layout.children[0].x, 10.0);
3785 assert_eq!(layout.children[0].y, 10.0);
3786 }
3787
3788 #[test]
3789 fn flex_direction_row_reverse() {
3790 let layout = stretch::compute(&stretch::style::Node {
3791 flex_direction: stretch::style::FlexDirection::RowReverse,
3792 width: stretch::style::Dimension::Points(100.0),
3793 height: stretch::style::Dimension::Points(100.0),
3794 children: vec![
3795 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
3796 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
3797 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
3798 ],
3799 ..Default::default()
3800 });
3801
3802 assert_eq!(layout.width, 100.0);
3803 assert_eq!(layout.height, 100.0);
3804 assert_eq!(layout.x, 0.0);
3805 assert_eq!(layout.y, 0.0);
3806
3807 assert_eq!(layout.children[0].width, 10.0);
3808 assert_eq!(layout.children[0].height, 100.0);
3809 assert_eq!(layout.children[0].x, 90.0);
3810 assert_eq!(layout.children[0].y, 0.0);
3811
3812 assert_eq!(layout.children[1].width, 10.0);
3813 assert_eq!(layout.children[1].height, 100.0);
3814 assert_eq!(layout.children[1].x, 80.0);
3815 assert_eq!(layout.children[1].y, 0.0);
3816
3817 assert_eq!(layout.children[2].width, 10.0);
3818 assert_eq!(layout.children[2].height, 100.0);
3819 assert_eq!(layout.children[2].x, 70.0);
3820 assert_eq!(layout.children[2].y, 0.0);
3821 }
3822
3823 #[test]
3824 fn wrap_reverse_row_align_content_stretch() {
3825 let layout = stretch::compute(&stretch::style::Node {
3826 width: stretch::style::Dimension::Points(100.0),
3827 children: vec![
3828 stretch::style::Node {
3829 width: stretch::style::Dimension::Points(30.0),
3830 height: stretch::style::Dimension::Points(10.0),
3831 ..Default::default()
3832 },
3833 stretch::style::Node {
3834 width: stretch::style::Dimension::Points(30.0),
3835 height: stretch::style::Dimension::Points(20.0),
3836 ..Default::default()
3837 },
3838 stretch::style::Node {
3839 width: stretch::style::Dimension::Points(30.0),
3840 height: stretch::style::Dimension::Points(30.0),
3841 ..Default::default()
3842 },
3843 stretch::style::Node {
3844 width: stretch::style::Dimension::Points(30.0),
3845 height: stretch::style::Dimension::Points(40.0),
3846 ..Default::default()
3847 },
3848 stretch::style::Node {
3849 width: stretch::style::Dimension::Points(30.0),
3850 height: stretch::style::Dimension::Points(50.0),
3851 ..Default::default()
3852 },
3853 ],
3854 ..Default::default()
3855 });
3856
3857 assert_eq!(layout.width, 100.0);
3858 assert_eq!(layout.height, 80.0);
3859 assert_eq!(layout.x, 0.0);
3860 assert_eq!(layout.y, 0.0);
3861
3862 assert_eq!(layout.children[0].width, 30.0);
3863 assert_eq!(layout.children[0].height, 10.0);
3864 assert_eq!(layout.children[0].x, 0.0);
3865 assert_eq!(layout.children[0].y, 70.0);
3866
3867 assert_eq!(layout.children[1].width, 30.0);
3868 assert_eq!(layout.children[1].height, 20.0);
3869 assert_eq!(layout.children[1].x, 30.0);
3870 assert_eq!(layout.children[1].y, 60.0);
3871
3872 assert_eq!(layout.children[2].width, 30.0);
3873 assert_eq!(layout.children[2].height, 30.0);
3874 assert_eq!(layout.children[2].x, 60.0);
3875 assert_eq!(layout.children[2].y, 50.0);
3876
3877 assert_eq!(layout.children[3].width, 30.0);
3878 assert_eq!(layout.children[3].height, 40.0);
3879 assert_eq!(layout.children[3].x, 0.0);
3880 assert_eq!(layout.children[3].y, 10.0);
3881
3882 assert_eq!(layout.children[4].width, 30.0);
3883 assert_eq!(layout.children[4].height, 50.0);
3884 assert_eq!(layout.children[4].x, 30.0);
3885 assert_eq!(layout.children[4].y, 0.0);
3886 }
3887
3888 #[test]
3889 fn margin_auto_top() {
3890 let layout = stretch::compute(&stretch::style::Node {
3891 align_items: stretch::style::AlignItems::Center,
3892 width: stretch::style::Dimension::Points(200.0),
3893 height: stretch::style::Dimension::Points(200.0),
3894 children: vec![
3895 stretch::style::Node {
3896 width: stretch::style::Dimension::Points(50.0),
3897 height: stretch::style::Dimension::Points(50.0),
3898 margin: stretch::style::Edges { top: stretch::style::Dimension::Auto, ..Default::default() },
3899 ..Default::default()
3900 },
3901 stretch::style::Node {
3902 width: stretch::style::Dimension::Points(50.0),
3903 height: stretch::style::Dimension::Points(50.0),
3904 ..Default::default()
3905 },
3906 ],
3907 ..Default::default()
3908 });
3909
3910 assert_eq!(layout.width, 200.0);
3911 assert_eq!(layout.height, 200.0);
3912 assert_eq!(layout.x, 0.0);
3913 assert_eq!(layout.y, 0.0);
3914
3915 assert_eq!(layout.children[0].width, 50.0);
3916 assert_eq!(layout.children[0].height, 50.0);
3917 assert_eq!(layout.children[0].x, 0.0);
3918 assert_eq!(layout.children[0].y, 150.0);
3919
3920 assert_eq!(layout.children[1].width, 50.0);
3921 assert_eq!(layout.children[1].height, 50.0);
3922 assert_eq!(layout.children[1].x, 50.0);
3923 assert_eq!(layout.children[1].y, 75.0);
3924 }
3925
3926 #[test]
3927 fn flex_grow_within_constrained_max_row() {
3928 let layout = stretch::compute(&stretch::style::Node {
3929 flex_direction: stretch::style::FlexDirection::Column,
3930 width: stretch::style::Dimension::Points(200.0),
3931 children: vec![stretch::style::Node {
3932 max_width: stretch::style::Dimension::Points(100.0),
3933 height: stretch::style::Dimension::Points(100.0),
3934 children: vec![
3935 stretch::style::Node {
3936 flex_shrink: 1.0,
3937 flex_basis: stretch::style::Dimension::Points(100.0),
3938 ..Default::default()
3939 },
3940 stretch::style::Node { width: stretch::style::Dimension::Points(50.0), ..Default::default() },
3941 ],
3942 ..Default::default()
3943 }],
3944 ..Default::default()
3945 });
3946
3947 assert_eq!(layout.width, 200.0);
3948 assert_eq!(layout.height, 100.0);
3949 assert_eq!(layout.x, 0.0);
3950 assert_eq!(layout.y, 0.0);
3951
3952 assert_eq!(layout.children[0].width, 100.0);
3953 assert_eq!(layout.children[0].height, 100.0);
3954 assert_eq!(layout.children[0].x, 0.0);
3955 assert_eq!(layout.children[0].y, 0.0);
3956
3957 assert_eq!(layout.children[0].children[0].width, 67.0);
3958 assert_eq!(layout.children[0].children[0].height, 100.0);
3959 assert_eq!(layout.children[0].children[0].x, 0.0);
3960 assert_eq!(layout.children[0].children[0].y, 0.0);
3961
3962 assert_eq!(layout.children[0].children[1].width, 33.0);
3963 assert_eq!(layout.children[0].children[1].height, 100.0);
3964 assert_eq!(layout.children[0].children[1].x, 67.0);
3965 assert_eq!(layout.children[0].children[1].y, 0.0);
3966 }
3967
3968 #[test]
3969 fn absolute_layout_align_items_and_justify_content_center() {
3970 let layout = stretch::compute(&stretch::style::Node {
3971 align_items: stretch::style::AlignItems::Center,
3972 justify_content: stretch::style::JustifyContent::Center,
3973 width: stretch::style::Dimension::Points(110.0),
3974 height: stretch::style::Dimension::Points(100.0),
3975 children: vec![stretch::style::Node {
3976 position: stretch::style::Position::Absolute,
3977 width: stretch::style::Dimension::Points(60.0),
3978 height: stretch::style::Dimension::Points(40.0),
3979 ..Default::default()
3980 }],
3981 ..Default::default()
3982 });
3983
3984 assert_eq!(layout.width, 110.0);
3985 assert_eq!(layout.height, 100.0);
3986 assert_eq!(layout.x, 0.0);
3987 assert_eq!(layout.y, 0.0);
3988
3989 assert_eq!(layout.children[0].width, 60.0);
3990 assert_eq!(layout.children[0].height, 40.0);
3991 assert_eq!(layout.children[0].x, 25.0);
3992 assert_eq!(layout.children[0].y, 30.0);
3993 }
3994
3995 #[test]
3996 fn align_self_flex_end() {
3997 let layout = stretch::compute(&stretch::style::Node {
3998 width: stretch::style::Dimension::Points(100.0),
3999 height: stretch::style::Dimension::Points(100.0),
4000 children: vec![stretch::style::Node {
4001 align_self: stretch::style::AlignSelf::FlexEnd,
4002 width: stretch::style::Dimension::Points(10.0),
4003 height: stretch::style::Dimension::Points(10.0),
4004 ..Default::default()
4005 }],
4006 ..Default::default()
4007 });
4008
4009 assert_eq!(layout.width, 100.0);
4010 assert_eq!(layout.height, 100.0);
4011 assert_eq!(layout.x, 0.0);
4012 assert_eq!(layout.y, 0.0);
4013
4014 assert_eq!(layout.children[0].width, 10.0);
4015 assert_eq!(layout.children[0].height, 10.0);
4016 assert_eq!(layout.children[0].x, 0.0);
4017 assert_eq!(layout.children[0].y, 90.0);
4018 }
4019
4020 #[test]
4021 fn margin_auto_left_child_bigger_than_parent() {
4022 let layout = stretch::compute(&stretch::style::Node {
4023 justify_content: stretch::style::JustifyContent::Center,
4024 width: stretch::style::Dimension::Points(52.0),
4025 height: stretch::style::Dimension::Points(52.0),
4026 children: vec![stretch::style::Node {
4027 width: stretch::style::Dimension::Points(72.0),
4028 height: stretch::style::Dimension::Points(72.0),
4029 margin: stretch::style::Edges { start: stretch::style::Dimension::Auto, ..Default::default() },
4030 ..Default::default()
4031 }],
4032 ..Default::default()
4033 });
4034
4035 assert_eq!(layout.width, 52.0);
4036 assert_eq!(layout.height, 52.0);
4037 assert_eq!(layout.x, 0.0);
4038 assert_eq!(layout.y, 0.0);
4039
4040 assert_eq!(layout.children[0].width, 52.0);
4041 assert_eq!(layout.children[0].height, 72.0);
4042 assert_eq!(layout.children[0].x, 0.0);
4043 assert_eq!(layout.children[0].y, 0.0);
4044 }
4045
4046 #[test]
4047 fn wrapped_row_within_align_items_flex_end() {
4048 let layout = stretch::compute(&stretch::style::Node {
4049 flex_direction: stretch::style::FlexDirection::Column,
4050 align_items: stretch::style::AlignItems::FlexEnd,
4051 width: stretch::style::Dimension::Points(200.0),
4052 height: stretch::style::Dimension::Points(200.0),
4053 children: vec![stretch::style::Node {
4054 children: vec![
4055 stretch::style::Node {
4056 width: stretch::style::Dimension::Points(150.0),
4057 height: stretch::style::Dimension::Points(80.0),
4058 ..Default::default()
4059 },
4060 stretch::style::Node {
4061 width: stretch::style::Dimension::Points(80.0),
4062 height: stretch::style::Dimension::Points(80.0),
4063 ..Default::default()
4064 },
4065 ],
4066 ..Default::default()
4067 }],
4068 ..Default::default()
4069 });
4070
4071 assert_eq!(layout.width, 200.0);
4072 assert_eq!(layout.height, 200.0);
4073 assert_eq!(layout.x, 0.0);
4074 assert_eq!(layout.y, 0.0);
4075
4076 assert_eq!(layout.children[0].width, 200.0);
4077 assert_eq!(layout.children[0].height, 160.0);
4078 assert_eq!(layout.children[0].x, 0.0);
4079 assert_eq!(layout.children[0].y, 0.0);
4080
4081 assert_eq!(layout.children[0].children[0].width, 150.0);
4082 assert_eq!(layout.children[0].children[0].height, 80.0);
4083 assert_eq!(layout.children[0].children[0].x, 0.0);
4084 assert_eq!(layout.children[0].children[0].y, 0.0);
4085
4086 assert_eq!(layout.children[0].children[1].width, 80.0);
4087 assert_eq!(layout.children[0].children[1].height, 80.0);
4088 assert_eq!(layout.children[0].children[1].x, 0.0);
4089 assert_eq!(layout.children[0].children[1].y, 80.0);
4090 }
4091
4092 #[test]
4093 fn min_height_overrides_height() {
4094 let layout = stretch::compute(&stretch::style::Node {
4095 children: vec![stretch::style::Node {
4096 height: stretch::style::Dimension::Points(50.0),
4097 min_height: stretch::style::Dimension::Points(100.0),
4098 ..Default::default()
4099 }],
4100 ..Default::default()
4101 });
4102
4103 assert_eq!(layout.width, 0.0);
4104 assert_eq!(layout.height, 100.0);
4105 assert_eq!(layout.x, 0.0);
4106 assert_eq!(layout.y, 0.0);
4107
4108 assert_eq!(layout.children[0].width, 0.0);
4109 assert_eq!(layout.children[0].height, 100.0);
4110 assert_eq!(layout.children[0].x, 0.0);
4111 assert_eq!(layout.children[0].y, 0.0);
4112 }
4113
4114 #[test]
4115 fn align_strech_should_size_based_on_parent() {
4116 let layout = stretch::compute(&stretch::style::Node {
4117 width: stretch::style::Dimension::Points(100.0),
4118 height: stretch::style::Dimension::Points(100.0),
4119 children: vec![stretch::style::Node {
4120 justify_content: stretch::style::JustifyContent::Center,
4121 flex_grow: 0.0,
4122 flex_shrink: 1.0,
4123 children: vec![stretch::style::Node {
4124 flex_grow: 1.0,
4125 flex_shrink: 1.0,
4126 children: vec![stretch::style::Node {
4127 width: stretch::style::Dimension::Points(20.0),
4128 height: stretch::style::Dimension::Points(20.0),
4129 ..Default::default()
4130 }],
4131 ..Default::default()
4132 }],
4133 ..Default::default()
4134 }],
4135 ..Default::default()
4136 });
4137
4138 assert_eq!(layout.width, 100.0);
4139 assert_eq!(layout.height, 100.0);
4140 assert_eq!(layout.x, 0.0);
4141 assert_eq!(layout.y, 0.0);
4142
4143 assert_eq!(layout.children[0].width, 20.0);
4144 assert_eq!(layout.children[0].height, 100.0);
4145 assert_eq!(layout.children[0].x, 0.0);
4146 assert_eq!(layout.children[0].y, 0.0);
4147
4148 assert_eq!(layout.children[0].children[0].width, 20.0);
4149 assert_eq!(layout.children[0].children[0].height, 100.0);
4150 assert_eq!(layout.children[0].children[0].x, 0.0);
4151 assert_eq!(layout.children[0].children[0].y, 0.0);
4152
4153 assert_eq!(layout.children[0].children[0].children[0].width, 20.0);
4154 assert_eq!(layout.children[0].children[0].children[0].height, 20.0);
4155 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
4156 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
4157 }
4158
4159 #[test]
4160 fn wrap_row_align_items_flex_end() {
4161 let layout = stretch::compute(&stretch::style::Node {
4162 align_items: stretch::style::AlignItems::FlexEnd,
4163 width: stretch::style::Dimension::Points(100.0),
4164 children: vec![
4165 stretch::style::Node {
4166 width: stretch::style::Dimension::Points(30.0),
4167 height: stretch::style::Dimension::Points(10.0),
4168 ..Default::default()
4169 },
4170 stretch::style::Node {
4171 width: stretch::style::Dimension::Points(30.0),
4172 height: stretch::style::Dimension::Points(20.0),
4173 ..Default::default()
4174 },
4175 stretch::style::Node {
4176 width: stretch::style::Dimension::Points(30.0),
4177 height: stretch::style::Dimension::Points(30.0),
4178 ..Default::default()
4179 },
4180 stretch::style::Node {
4181 width: stretch::style::Dimension::Points(30.0),
4182 height: stretch::style::Dimension::Points(30.0),
4183 ..Default::default()
4184 },
4185 ],
4186 ..Default::default()
4187 });
4188
4189 assert_eq!(layout.width, 100.0);
4190 assert_eq!(layout.height, 60.0);
4191 assert_eq!(layout.x, 0.0);
4192 assert_eq!(layout.y, 0.0);
4193
4194 assert_eq!(layout.children[0].width, 30.0);
4195 assert_eq!(layout.children[0].height, 10.0);
4196 assert_eq!(layout.children[0].x, 0.0);
4197 assert_eq!(layout.children[0].y, 20.0);
4198
4199 assert_eq!(layout.children[1].width, 30.0);
4200 assert_eq!(layout.children[1].height, 20.0);
4201 assert_eq!(layout.children[1].x, 30.0);
4202 assert_eq!(layout.children[1].y, 10.0);
4203
4204 assert_eq!(layout.children[2].width, 30.0);
4205 assert_eq!(layout.children[2].height, 30.0);
4206 assert_eq!(layout.children[2].x, 60.0);
4207 assert_eq!(layout.children[2].y, 0.0);
4208
4209 assert_eq!(layout.children[3].width, 30.0);
4210 assert_eq!(layout.children[3].height, 30.0);
4211 assert_eq!(layout.children[3].x, 0.0);
4212 assert_eq!(layout.children[3].y, 30.0);
4213 }
4214
4215 #[test]
4216 fn size_defined_by_child_with_border() {
4217 let layout = stretch::compute(&stretch::style::Node {
4218 border: stretch::style::Edges {
4219 start: stretch::style::Dimension::Points(10.0),
4220 end: stretch::style::Dimension::Points(10.0),
4221 top: stretch::style::Dimension::Points(10.0),
4222 bottom: stretch::style::Dimension::Points(10.0),
4223 ..Default::default()
4224 },
4225 children: vec![stretch::style::Node {
4226 width: stretch::style::Dimension::Points(10.0),
4227 height: stretch::style::Dimension::Points(10.0),
4228 ..Default::default()
4229 }],
4230 ..Default::default()
4231 });
4232
4233 assert_eq!(layout.width, 30.0);
4234 assert_eq!(layout.height, 30.0);
4235 assert_eq!(layout.x, 0.0);
4236 assert_eq!(layout.y, 0.0);
4237
4238 assert_eq!(layout.children[0].width, 10.0);
4239 assert_eq!(layout.children[0].height, 10.0);
4240 assert_eq!(layout.children[0].x, 10.0);
4241 assert_eq!(layout.children[0].y, 10.0);
4242 }
4243
4244 #[test]
4245 fn margin_auto_bottom_and_top_justify_center() {
4246 let layout = stretch::compute(&stretch::style::Node {
4247 justify_content: stretch::style::JustifyContent::Center,
4248 width: stretch::style::Dimension::Points(200.0),
4249 height: stretch::style::Dimension::Points(200.0),
4250 children: vec![
4251 stretch::style::Node {
4252 width: stretch::style::Dimension::Points(50.0),
4253 height: stretch::style::Dimension::Points(50.0),
4254 margin: stretch::style::Edges {
4255 top: stretch::style::Dimension::Auto,
4256 bottom: stretch::style::Dimension::Auto,
4257 ..Default::default()
4258 },
4259 ..Default::default()
4260 },
4261 stretch::style::Node {
4262 width: stretch::style::Dimension::Points(50.0),
4263 height: stretch::style::Dimension::Points(50.0),
4264 ..Default::default()
4265 },
4266 ],
4267 ..Default::default()
4268 });
4269
4270 assert_eq!(layout.width, 200.0);
4271 assert_eq!(layout.height, 200.0);
4272 assert_eq!(layout.x, 0.0);
4273 assert_eq!(layout.y, 0.0);
4274
4275 assert_eq!(layout.children[0].width, 50.0);
4276 assert_eq!(layout.children[0].height, 50.0);
4277 assert_eq!(layout.children[0].x, 50.0);
4278 assert_eq!(layout.children[0].y, 75.0);
4279
4280 assert_eq!(layout.children[1].width, 50.0);
4281 assert_eq!(layout.children[1].height, 50.0);
4282 assert_eq!(layout.children[1].x, 100.0);
4283 assert_eq!(layout.children[1].y, 0.0);
4284 }
4285
4286 #[test]
4287 fn flex_basis_overrides_main_size() {
4288 let layout = stretch::compute(&stretch::style::Node {
4289 width: stretch::style::Dimension::Points(100.0),
4290 height: stretch::style::Dimension::Points(100.0),
4291 children: vec![
4292 stretch::style::Node {
4293 flex_grow: 1.0,
4294 flex_basis: stretch::style::Dimension::Points(50.0),
4295 width: stretch::style::Dimension::Points(20.0),
4296 ..Default::default()
4297 },
4298 stretch::style::Node {
4299 flex_grow: 1.0,
4300 width: stretch::style::Dimension::Points(10.0),
4301 ..Default::default()
4302 },
4303 stretch::style::Node {
4304 flex_grow: 1.0,
4305 width: stretch::style::Dimension::Points(10.0),
4306 ..Default::default()
4307 },
4308 ],
4309 ..Default::default()
4310 });
4311
4312 assert_eq!(layout.width, 100.0);
4313 assert_eq!(layout.height, 100.0);
4314 assert_eq!(layout.x, 0.0);
4315 assert_eq!(layout.y, 0.0);
4316
4317 assert_eq!(layout.children[0].width, 60.0);
4318 assert_eq!(layout.children[0].height, 100.0);
4319 assert_eq!(layout.children[0].x, 0.0);
4320 assert_eq!(layout.children[0].y, 0.0);
4321
4322 assert_eq!(layout.children[1].width, 20.0);
4323 assert_eq!(layout.children[1].height, 100.0);
4324 assert_eq!(layout.children[1].x, 60.0);
4325 assert_eq!(layout.children[1].y, 0.0);
4326
4327 assert_eq!(layout.children[2].width, 20.0);
4328 assert_eq!(layout.children[2].height, 100.0);
4329 assert_eq!(layout.children[2].x, 80.0);
4330 assert_eq!(layout.children[2].y, 0.0);
4331 }
4332
4333 #[test]
4334 fn wrapped_row_within_align_items_flex_start() {
4335 let layout = stretch::compute(&stretch::style::Node {
4336 flex_direction: stretch::style::FlexDirection::Column,
4337 align_items: stretch::style::AlignItems::FlexStart,
4338 width: stretch::style::Dimension::Points(200.0),
4339 height: stretch::style::Dimension::Points(200.0),
4340 children: vec![stretch::style::Node {
4341 children: vec![
4342 stretch::style::Node {
4343 width: stretch::style::Dimension::Points(150.0),
4344 height: stretch::style::Dimension::Points(80.0),
4345 ..Default::default()
4346 },
4347 stretch::style::Node {
4348 width: stretch::style::Dimension::Points(80.0),
4349 height: stretch::style::Dimension::Points(80.0),
4350 ..Default::default()
4351 },
4352 ],
4353 ..Default::default()
4354 }],
4355 ..Default::default()
4356 });
4357
4358 assert_eq!(layout.width, 200.0);
4359 assert_eq!(layout.height, 200.0);
4360 assert_eq!(layout.x, 0.0);
4361 assert_eq!(layout.y, 0.0);
4362
4363 assert_eq!(layout.children[0].width, 200.0);
4364 assert_eq!(layout.children[0].height, 160.0);
4365 assert_eq!(layout.children[0].x, 0.0);
4366 assert_eq!(layout.children[0].y, 0.0);
4367
4368 assert_eq!(layout.children[0].children[0].width, 150.0);
4369 assert_eq!(layout.children[0].children[0].height, 80.0);
4370 assert_eq!(layout.children[0].children[0].x, 0.0);
4371 assert_eq!(layout.children[0].children[0].y, 0.0);
4372
4373 assert_eq!(layout.children[0].children[1].width, 80.0);
4374 assert_eq!(layout.children[0].children[1].height, 80.0);
4375 assert_eq!(layout.children[0].children[1].x, 0.0);
4376 assert_eq!(layout.children[0].children[1].y, 80.0);
4377 }
4378
4379 #[test]
4380 fn align_center_should_size_based_on_content() {
4381 let layout = stretch::compute(&stretch::style::Node {
4382 align_items: stretch::style::AlignItems::Center,
4383 width: stretch::style::Dimension::Points(100.0),
4384 height: stretch::style::Dimension::Points(100.0),
4385 margin: stretch::style::Edges { top: stretch::style::Dimension::Points(20.0), ..Default::default() },
4386 children: vec![stretch::style::Node {
4387 justify_content: stretch::style::JustifyContent::Center,
4388 flex_grow: 0.0,
4389 flex_shrink: 1.0,
4390 children: vec![stretch::style::Node {
4391 flex_grow: 1.0,
4392 flex_shrink: 1.0,
4393 children: vec![stretch::style::Node {
4394 width: stretch::style::Dimension::Points(20.0),
4395 height: stretch::style::Dimension::Points(20.0),
4396 ..Default::default()
4397 }],
4398 ..Default::default()
4399 }],
4400 ..Default::default()
4401 }],
4402 ..Default::default()
4403 });
4404
4405 assert_eq!(layout.width, 100.0);
4406 assert_eq!(layout.height, 100.0);
4407 assert_eq!(layout.x, 0.0);
4408 assert_eq!(layout.y, 20.0);
4409
4410 assert_eq!(layout.children[0].width, 20.0);
4411 assert_eq!(layout.children[0].height, 20.0);
4412 assert_eq!(layout.children[0].x, 0.0);
4413 assert_eq!(layout.children[0].y, 40.0);
4414
4415 assert_eq!(layout.children[0].children[0].width, 20.0);
4416 assert_eq!(layout.children[0].children[0].height, 20.0);
4417 assert_eq!(layout.children[0].children[0].x, 0.0);
4418 assert_eq!(layout.children[0].children[0].y, 0.0);
4419
4420 assert_eq!(layout.children[0].children[0].children[0].width, 20.0);
4421 assert_eq!(layout.children[0].children[0].children[0].height, 20.0);
4422 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
4423 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
4424 }
4425
4426 #[test]
4427 fn margin_auto_right() {
4428 let layout = stretch::compute(&stretch::style::Node {
4429 align_items: stretch::style::AlignItems::Center,
4430 width: stretch::style::Dimension::Points(200.0),
4431 height: stretch::style::Dimension::Points(200.0),
4432 children: vec![
4433 stretch::style::Node {
4434 width: stretch::style::Dimension::Points(50.0),
4435 height: stretch::style::Dimension::Points(50.0),
4436 margin: stretch::style::Edges { end: stretch::style::Dimension::Auto, ..Default::default() },
4437 ..Default::default()
4438 },
4439 stretch::style::Node {
4440 width: stretch::style::Dimension::Points(50.0),
4441 height: stretch::style::Dimension::Points(50.0),
4442 ..Default::default()
4443 },
4444 ],
4445 ..Default::default()
4446 });
4447
4448 assert_eq!(layout.width, 200.0);
4449 assert_eq!(layout.height, 200.0);
4450 assert_eq!(layout.x, 0.0);
4451 assert_eq!(layout.y, 0.0);
4452
4453 assert_eq!(layout.children[0].width, 50.0);
4454 assert_eq!(layout.children[0].height, 50.0);
4455 assert_eq!(layout.children[0].x, 0.0);
4456 assert_eq!(layout.children[0].y, 75.0);
4457
4458 assert_eq!(layout.children[1].width, 50.0);
4459 assert_eq!(layout.children[1].height, 50.0);
4460 assert_eq!(layout.children[1].x, 150.0);
4461 assert_eq!(layout.children[1].y, 75.0);
4462 }
4463
4464 #[test]
4465 fn wrap_reverse_row_align_content_center() {
4466 let layout = stretch::compute(&stretch::style::Node {
4467 align_content: stretch::style::AlignContent::Center,
4468 width: stretch::style::Dimension::Points(100.0),
4469 children: vec![
4470 stretch::style::Node {
4471 width: stretch::style::Dimension::Points(30.0),
4472 height: stretch::style::Dimension::Points(10.0),
4473 ..Default::default()
4474 },
4475 stretch::style::Node {
4476 width: stretch::style::Dimension::Points(30.0),
4477 height: stretch::style::Dimension::Points(20.0),
4478 ..Default::default()
4479 },
4480 stretch::style::Node {
4481 width: stretch::style::Dimension::Points(30.0),
4482 height: stretch::style::Dimension::Points(30.0),
4483 ..Default::default()
4484 },
4485 stretch::style::Node {
4486 width: stretch::style::Dimension::Points(30.0),
4487 height: stretch::style::Dimension::Points(40.0),
4488 ..Default::default()
4489 },
4490 stretch::style::Node {
4491 width: stretch::style::Dimension::Points(30.0),
4492 height: stretch::style::Dimension::Points(50.0),
4493 ..Default::default()
4494 },
4495 ],
4496 ..Default::default()
4497 });
4498
4499 assert_eq!(layout.width, 100.0);
4500 assert_eq!(layout.height, 80.0);
4501 assert_eq!(layout.x, 0.0);
4502 assert_eq!(layout.y, 0.0);
4503
4504 assert_eq!(layout.children[0].width, 30.0);
4505 assert_eq!(layout.children[0].height, 10.0);
4506 assert_eq!(layout.children[0].x, 0.0);
4507 assert_eq!(layout.children[0].y, 70.0);
4508
4509 assert_eq!(layout.children[1].width, 30.0);
4510 assert_eq!(layout.children[1].height, 20.0);
4511 assert_eq!(layout.children[1].x, 30.0);
4512 assert_eq!(layout.children[1].y, 60.0);
4513
4514 assert_eq!(layout.children[2].width, 30.0);
4515 assert_eq!(layout.children[2].height, 30.0);
4516 assert_eq!(layout.children[2].x, 60.0);
4517 assert_eq!(layout.children[2].y, 50.0);
4518
4519 assert_eq!(layout.children[3].width, 30.0);
4520 assert_eq!(layout.children[3].height, 40.0);
4521 assert_eq!(layout.children[3].x, 0.0);
4522 assert_eq!(layout.children[3].y, 10.0);
4523
4524 assert_eq!(layout.children[4].width, 30.0);
4525 assert_eq!(layout.children[4].height, 50.0);
4526 assert_eq!(layout.children[4].x, 30.0);
4527 assert_eq!(layout.children[4].y, 0.0);
4528 }
4529
4530 #[test]
4531 fn padding_no_child() {
4532 let layout = stretch::compute(&stretch::style::Node {
4533 padding: stretch::style::Edges {
4534 start: stretch::style::Dimension::Points(10.0),
4535 end: stretch::style::Dimension::Points(10.0),
4536 top: stretch::style::Dimension::Points(10.0),
4537 bottom: stretch::style::Dimension::Points(10.0),
4538 ..Default::default()
4539 },
4540 ..Default::default()
4541 });
4542
4543 assert_eq!(layout.width, 20.0);
4544 assert_eq!(layout.height, 20.0);
4545 assert_eq!(layout.x, 0.0);
4546 assert_eq!(layout.y, 0.0);
4547 }
4548
4549 #[test]
4550 fn margin_with_sibling_column() {
4551 let layout = stretch::compute(&stretch::style::Node {
4552 flex_direction: stretch::style::FlexDirection::Column,
4553 width: stretch::style::Dimension::Points(100.0),
4554 height: stretch::style::Dimension::Points(100.0),
4555 children: vec![
4556 stretch::style::Node {
4557 flex_grow: 1.0,
4558 margin: stretch::style::Edges {
4559 bottom: stretch::style::Dimension::Points(10.0),
4560 ..Default::default()
4561 },
4562 ..Default::default()
4563 },
4564 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
4565 ],
4566 ..Default::default()
4567 });
4568
4569 assert_eq!(layout.width, 100.0);
4570 assert_eq!(layout.height, 100.0);
4571 assert_eq!(layout.x, 0.0);
4572 assert_eq!(layout.y, 0.0);
4573
4574 assert_eq!(layout.children[0].width, 100.0);
4575 assert_eq!(layout.children[0].height, 45.0);
4576 assert_eq!(layout.children[0].x, 0.0);
4577 assert_eq!(layout.children[0].y, 0.0);
4578
4579 assert_eq!(layout.children[1].width, 100.0);
4580 assert_eq!(layout.children[1].height, 45.0);
4581 assert_eq!(layout.children[1].x, 0.0);
4582 assert_eq!(layout.children[1].y, 55.0);
4583 }
4584
4585 #[test]
4586 fn flex_direction_row_no_width() {
4587 let layout = stretch::compute(&stretch::style::Node {
4588 height: stretch::style::Dimension::Points(100.0),
4589 children: vec![
4590 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
4591 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
4592 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
4593 ],
4594 ..Default::default()
4595 });
4596
4597 assert_eq!(layout.width, 30.0);
4598 assert_eq!(layout.height, 100.0);
4599 assert_eq!(layout.x, 0.0);
4600 assert_eq!(layout.y, 0.0);
4601
4602 assert_eq!(layout.children[0].width, 10.0);
4603 assert_eq!(layout.children[0].height, 100.0);
4604 assert_eq!(layout.children[0].x, 0.0);
4605 assert_eq!(layout.children[0].y, 0.0);
4606
4607 assert_eq!(layout.children[1].width, 10.0);
4608 assert_eq!(layout.children[1].height, 100.0);
4609 assert_eq!(layout.children[1].x, 10.0);
4610 assert_eq!(layout.children[1].y, 0.0);
4611
4612 assert_eq!(layout.children[2].width, 10.0);
4613 assert_eq!(layout.children[2].height, 100.0);
4614 assert_eq!(layout.children[2].x, 20.0);
4615 assert_eq!(layout.children[2].y, 0.0);
4616 }
4617
4618 #[test]
4619 fn max_width_overrides_width_on_root() {
4620 let layout = stretch::compute(&stretch::style::Node {
4621 width: stretch::style::Dimension::Points(200.0),
4622 max_width: stretch::style::Dimension::Points(100.0),
4623 ..Default::default()
4624 });
4625
4626 assert_eq!(layout.width, 100.0);
4627 assert_eq!(layout.height, 0.0);
4628 assert_eq!(layout.x, 0.0);
4629 assert_eq!(layout.y, 0.0);
4630 }
4631
4632 #[test]
4633 fn margin_auto_left_and_right() {
4634 let layout = stretch::compute(&stretch::style::Node {
4635 width: stretch::style::Dimension::Points(200.0),
4636 height: stretch::style::Dimension::Points(200.0),
4637 children: vec![
4638 stretch::style::Node {
4639 width: stretch::style::Dimension::Points(50.0),
4640 height: stretch::style::Dimension::Points(50.0),
4641 margin: stretch::style::Edges {
4642 start: stretch::style::Dimension::Auto,
4643 end: stretch::style::Dimension::Auto,
4644 ..Default::default()
4645 },
4646 ..Default::default()
4647 },
4648 stretch::style::Node {
4649 width: stretch::style::Dimension::Points(50.0),
4650 height: stretch::style::Dimension::Points(50.0),
4651 ..Default::default()
4652 },
4653 ],
4654 ..Default::default()
4655 });
4656
4657 assert_eq!(layout.width, 200.0);
4658 assert_eq!(layout.height, 200.0);
4659 assert_eq!(layout.x, 0.0);
4660 assert_eq!(layout.y, 0.0);
4661
4662 assert_eq!(layout.children[0].width, 50.0);
4663 assert_eq!(layout.children[0].height, 50.0);
4664 assert_eq!(layout.children[0].x, 50.0);
4665 assert_eq!(layout.children[0].y, 0.0);
4666
4667 assert_eq!(layout.children[1].width, 50.0);
4668 assert_eq!(layout.children[1].height, 50.0);
4669 assert_eq!(layout.children[1].x, 150.0);
4670 assert_eq!(layout.children[1].y, 0.0);
4671 }
4672
4673 #[test]
4674 fn flex_direction_column_no_height() {
4675 let layout = stretch::compute(&stretch::style::Node {
4676 flex_direction: stretch::style::FlexDirection::Column,
4677 width: stretch::style::Dimension::Points(100.0),
4678 children: vec![
4679 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
4680 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
4681 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
4682 ],
4683 ..Default::default()
4684 });
4685
4686 assert_eq!(layout.width, 100.0);
4687 assert_eq!(layout.height, 30.0);
4688 assert_eq!(layout.x, 0.0);
4689 assert_eq!(layout.y, 0.0);
4690
4691 assert_eq!(layout.children[0].width, 100.0);
4692 assert_eq!(layout.children[0].height, 10.0);
4693 assert_eq!(layout.children[0].x, 0.0);
4694 assert_eq!(layout.children[0].y, 0.0);
4695
4696 assert_eq!(layout.children[1].width, 100.0);
4697 assert_eq!(layout.children[1].height, 10.0);
4698 assert_eq!(layout.children[1].x, 0.0);
4699 assert_eq!(layout.children[1].y, 10.0);
4700
4701 assert_eq!(layout.children[2].width, 100.0);
4702 assert_eq!(layout.children[2].height, 10.0);
4703 assert_eq!(layout.children[2].x, 0.0);
4704 assert_eq!(layout.children[2].y, 20.0);
4705 }
4706
4707 #[test]
4708 fn absolute_layout_in_wrap_reverse_row_container() {
4709 let layout = stretch::compute(&stretch::style::Node {
4710 width: stretch::style::Dimension::Points(100.0),
4711 height: stretch::style::Dimension::Points(100.0),
4712 children: vec![stretch::style::Node {
4713 position: stretch::style::Position::Absolute,
4714 width: stretch::style::Dimension::Points(20.0),
4715 height: stretch::style::Dimension::Points(20.0),
4716 ..Default::default()
4717 }],
4718 ..Default::default()
4719 });
4720
4721 assert_eq!(layout.width, 100.0);
4722 assert_eq!(layout.height, 100.0);
4723 assert_eq!(layout.x, 0.0);
4724 assert_eq!(layout.y, 0.0);
4725
4726 assert_eq!(layout.children[0].width, 20.0);
4727 assert_eq!(layout.children[0].height, 20.0);
4728 assert_eq!(layout.children[0].x, 0.0);
4729 assert_eq!(layout.children[0].y, 80.0);
4730 }
4731
4732 #[test]
4733 fn percentage_margin_should_calculate_based_only_on_width() {
4734 let layout = stretch::compute(&stretch::style::Node {
4735 flex_direction: stretch::style::FlexDirection::Column,
4736 width: stretch::style::Dimension::Points(200.0),
4737 height: stretch::style::Dimension::Points(100.0),
4738 children: vec![stretch::style::Node {
4739 flex_direction: stretch::style::FlexDirection::Column,
4740 flex_grow: 1.0,
4741 margin: stretch::style::Edges {
4742 start: stretch::style::Dimension::Percent(0.1),
4743 end: stretch::style::Dimension::Percent(0.1),
4744 top: stretch::style::Dimension::Percent(0.1),
4745 bottom: stretch::style::Dimension::Percent(0.1),
4746 ..Default::default()
4747 },
4748 children: vec![stretch::style::Node {
4749 width: stretch::style::Dimension::Points(10.0),
4750 height: stretch::style::Dimension::Points(10.0),
4751 ..Default::default()
4752 }],
4753 ..Default::default()
4754 }],
4755 ..Default::default()
4756 });
4757
4758 assert_eq!(layout.width, 200.0);
4759 assert_eq!(layout.height, 100.0);
4760 assert_eq!(layout.x, 0.0);
4761 assert_eq!(layout.y, 0.0);
4762
4763 assert_eq!(layout.children[0].width, 160.0);
4764 assert_eq!(layout.children[0].height, 60.0);
4765 assert_eq!(layout.children[0].x, 20.0);
4766 assert_eq!(layout.children[0].y, 20.0);
4767
4768 assert_eq!(layout.children[0].children[0].width, 10.0);
4769 assert_eq!(layout.children[0].children[0].height, 10.0);
4770 assert_eq!(layout.children[0].children[0].x, 0.0);
4771 assert_eq!(layout.children[0].children[0].y, 0.0);
4772 }
4773
4774 #[test]
4775 fn absolute_layout_start_top_end_bottom() {
4776 let layout = stretch::compute(&stretch::style::Node {
4777 width: stretch::style::Dimension::Points(100.0),
4778 height: stretch::style::Dimension::Points(100.0),
4779 children: vec![stretch::style::Node {
4780 position: stretch::style::Position::Absolute,
4781 start: stretch::style::Dimension::Points(10.0),
4782 end: stretch::style::Dimension::Points(10.0),
4783 top: stretch::style::Dimension::Points(10.0),
4784 bottom: stretch::style::Dimension::Points(10.0),
4785 ..Default::default()
4786 }],
4787 ..Default::default()
4788 });
4789
4790 assert_eq!(layout.width, 100.0);
4791 assert_eq!(layout.height, 100.0);
4792 assert_eq!(layout.x, 0.0);
4793 assert_eq!(layout.y, 0.0);
4794
4795 assert_eq!(layout.children[0].width, 80.0);
4796 assert_eq!(layout.children[0].height, 80.0);
4797 assert_eq!(layout.children[0].x, 10.0);
4798 assert_eq!(layout.children[0].y, 10.0);
4799 }
4800
4801 #[test]
4802 fn min_width() {
4803 let layout = stretch::compute(&stretch::style::Node {
4804 width: stretch::style::Dimension::Points(100.0),
4805 height: stretch::style::Dimension::Points(100.0),
4806 children: vec![
4807 stretch::style::Node {
4808 flex_grow: 1.0,
4809 min_width: stretch::style::Dimension::Points(60.0),
4810 ..Default::default()
4811 },
4812 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
4813 ],
4814 ..Default::default()
4815 });
4816
4817 assert_eq!(layout.width, 100.0);
4818 assert_eq!(layout.height, 100.0);
4819 assert_eq!(layout.x, 0.0);
4820 assert_eq!(layout.y, 0.0);
4821
4822 assert_eq!(layout.children[0].width, 80.0);
4823 assert_eq!(layout.children[0].height, 100.0);
4824 assert_eq!(layout.children[0].x, 0.0);
4825 assert_eq!(layout.children[0].y, 0.0);
4826
4827 assert_eq!(layout.children[1].width, 20.0);
4828 assert_eq!(layout.children[1].height, 100.0);
4829 assert_eq!(layout.children[1].x, 80.0);
4830 assert_eq!(layout.children[1].y, 0.0);
4831 }
4832
4833 #[test]
4834 fn min_height_overrides_height_on_root() {
4835 let layout = stretch::compute(&stretch::style::Node {
4836 height: stretch::style::Dimension::Points(50.0),
4837 min_height: stretch::style::Dimension::Points(100.0),
4838 ..Default::default()
4839 });
4840
4841 assert_eq!(layout.width, 0.0);
4842 assert_eq!(layout.height, 100.0);
4843 assert_eq!(layout.x, 0.0);
4844 assert_eq!(layout.y, 0.0);
4845 }
4846
4847 #[test]
4848 fn absolute_layout_width_height_end_bottom() {
4849 let layout = stretch::compute(&stretch::style::Node {
4850 width: stretch::style::Dimension::Points(100.0),
4851 height: stretch::style::Dimension::Points(100.0),
4852 children: vec![stretch::style::Node {
4853 position: stretch::style::Position::Absolute,
4854 width: stretch::style::Dimension::Points(10.0),
4855 height: stretch::style::Dimension::Points(10.0),
4856 end: stretch::style::Dimension::Points(10.0),
4857 bottom: stretch::style::Dimension::Points(10.0),
4858 ..Default::default()
4859 }],
4860 ..Default::default()
4861 });
4862
4863 assert_eq!(layout.width, 100.0);
4864 assert_eq!(layout.height, 100.0);
4865 assert_eq!(layout.x, 0.0);
4866 assert_eq!(layout.y, 0.0);
4867
4868 assert_eq!(layout.children[0].width, 10.0);
4869 assert_eq!(layout.children[0].height, 10.0);
4870 assert_eq!(layout.children[0].x, 80.0);
4871 assert_eq!(layout.children[0].y, 80.0);
4872 }
4873
4874 #[test]
4875 fn align_self_center() {
4876 let layout = stretch::compute(&stretch::style::Node {
4877 width: stretch::style::Dimension::Points(100.0),
4878 height: stretch::style::Dimension::Points(100.0),
4879 children: vec![stretch::style::Node {
4880 align_self: stretch::style::AlignSelf::Center,
4881 width: stretch::style::Dimension::Points(10.0),
4882 height: stretch::style::Dimension::Points(10.0),
4883 ..Default::default()
4884 }],
4885 ..Default::default()
4886 });
4887
4888 assert_eq!(layout.width, 100.0);
4889 assert_eq!(layout.height, 100.0);
4890 assert_eq!(layout.x, 0.0);
4891 assert_eq!(layout.y, 0.0);
4892
4893 assert_eq!(layout.children[0].width, 10.0);
4894 assert_eq!(layout.children[0].height, 10.0);
4895 assert_eq!(layout.children[0].x, 0.0);
4896 assert_eq!(layout.children[0].y, 45.0);
4897 }
4898
4899 #[test]
4900 fn wrapped_column_max_height() {
4901 let layout = stretch::compute(&stretch::style::Node {
4902 flex_direction: stretch::style::FlexDirection::Column,
4903 align_items: stretch::style::AlignItems::Center,
4904 align_content: stretch::style::AlignContent::Center,
4905 justify_content: stretch::style::JustifyContent::Center,
4906 width: stretch::style::Dimension::Points(700.0),
4907 height: stretch::style::Dimension::Points(500.0),
4908 children: vec![
4909 stretch::style::Node {
4910 width: stretch::style::Dimension::Points(100.0),
4911 height: stretch::style::Dimension::Points(500.0),
4912 max_height: stretch::style::Dimension::Points(200.0),
4913 ..Default::default()
4914 },
4915 stretch::style::Node {
4916 width: stretch::style::Dimension::Points(200.0),
4917 height: stretch::style::Dimension::Points(200.0),
4918 margin: stretch::style::Edges {
4919 start: stretch::style::Dimension::Points(20.0),
4920 end: stretch::style::Dimension::Points(20.0),
4921 top: stretch::style::Dimension::Points(20.0),
4922 bottom: stretch::style::Dimension::Points(20.0),
4923 ..Default::default()
4924 },
4925 ..Default::default()
4926 },
4927 stretch::style::Node {
4928 width: stretch::style::Dimension::Points(100.0),
4929 height: stretch::style::Dimension::Points(100.0),
4930 ..Default::default()
4931 },
4932 ],
4933 ..Default::default()
4934 });
4935
4936 assert_eq!(layout.width, 700.0);
4937 assert_eq!(layout.height, 500.0);
4938 assert_eq!(layout.x, 0.0);
4939 assert_eq!(layout.y, 0.0);
4940
4941 assert_eq!(layout.children[0].width, 100.0);
4942 assert_eq!(layout.children[0].height, 200.0);
4943 assert_eq!(layout.children[0].x, 250.0);
4944 assert_eq!(layout.children[0].y, 30.0);
4945
4946 assert_eq!(layout.children[1].width, 200.0);
4947 assert_eq!(layout.children[1].height, 200.0);
4948 assert_eq!(layout.children[1].x, 200.0);
4949 assert_eq!(layout.children[1].y, 250.0);
4950
4951 assert_eq!(layout.children[2].width, 100.0);
4952 assert_eq!(layout.children[2].height, 100.0);
4953 assert_eq!(layout.children[2].x, 420.0);
4954 assert_eq!(layout.children[2].y, 200.0);
4955 }
4956
4957 #[test]
4958 fn wrap_row() {
4959 let layout = stretch::compute(&stretch::style::Node {
4960 width: stretch::style::Dimension::Points(100.0),
4961 children: vec![
4962 stretch::style::Node {
4963 width: stretch::style::Dimension::Points(30.0),
4964 height: stretch::style::Dimension::Points(30.0),
4965 ..Default::default()
4966 },
4967 stretch::style::Node {
4968 width: stretch::style::Dimension::Points(30.0),
4969 height: stretch::style::Dimension::Points(30.0),
4970 ..Default::default()
4971 },
4972 stretch::style::Node {
4973 width: stretch::style::Dimension::Points(30.0),
4974 height: stretch::style::Dimension::Points(30.0),
4975 ..Default::default()
4976 },
4977 stretch::style::Node {
4978 width: stretch::style::Dimension::Points(30.0),
4979 height: stretch::style::Dimension::Points(30.0),
4980 ..Default::default()
4981 },
4982 ],
4983 ..Default::default()
4984 });
4985
4986 assert_eq!(layout.width, 100.0);
4987 assert_eq!(layout.height, 60.0);
4988 assert_eq!(layout.x, 0.0);
4989 assert_eq!(layout.y, 0.0);
4990
4991 assert_eq!(layout.children[0].width, 30.0);
4992 assert_eq!(layout.children[0].height, 30.0);
4993 assert_eq!(layout.children[0].x, 0.0);
4994 assert_eq!(layout.children[0].y, 0.0);
4995
4996 assert_eq!(layout.children[1].width, 30.0);
4997 assert_eq!(layout.children[1].height, 30.0);
4998 assert_eq!(layout.children[1].x, 30.0);
4999 assert_eq!(layout.children[1].y, 0.0);
5000
5001 assert_eq!(layout.children[2].width, 30.0);
5002 assert_eq!(layout.children[2].height, 30.0);
5003 assert_eq!(layout.children[2].x, 60.0);
5004 assert_eq!(layout.children[2].y, 0.0);
5005
5006 assert_eq!(layout.children[3].width, 30.0);
5007 assert_eq!(layout.children[3].height, 30.0);
5008 assert_eq!(layout.children[3].x, 0.0);
5009 assert_eq!(layout.children[3].y, 30.0);
5010 }
5011
5012 #[test]
5013 fn max_height() {
5014 let layout = stretch::compute(&stretch::style::Node {
5015 width: stretch::style::Dimension::Points(100.0),
5016 height: stretch::style::Dimension::Points(100.0),
5017 children: vec![stretch::style::Node {
5018 width: stretch::style::Dimension::Points(10.0),
5019 max_height: stretch::style::Dimension::Points(50.0),
5020 ..Default::default()
5021 }],
5022 ..Default::default()
5023 });
5024
5025 assert_eq!(layout.width, 100.0);
5026 assert_eq!(layout.height, 100.0);
5027 assert_eq!(layout.x, 0.0);
5028 assert_eq!(layout.y, 0.0);
5029
5030 assert_eq!(layout.children[0].width, 10.0);
5031 assert_eq!(layout.children[0].height, 50.0);
5032 assert_eq!(layout.children[0].x, 0.0);
5033 assert_eq!(layout.children[0].y, 0.0);
5034 }
5035
5036 #[test]
5037 fn flex_grow_child() {
5038 let layout = stretch::compute(&stretch::style::Node {
5039 children: vec![stretch::style::Node {
5040 flex_grow: 1.0,
5041 flex_basis: stretch::style::Dimension::Points(0.0),
5042 height: stretch::style::Dimension::Points(100.0),
5043 ..Default::default()
5044 }],
5045 ..Default::default()
5046 });
5047
5048 assert_eq!(layout.width, 0.0);
5049 assert_eq!(layout.height, 100.0);
5050 assert_eq!(layout.x, 0.0);
5051 assert_eq!(layout.y, 0.0);
5052
5053 assert_eq!(layout.children[0].width, 0.0);
5054 assert_eq!(layout.children[0].height, 100.0);
5055 assert_eq!(layout.children[0].x, 0.0);
5056 assert_eq!(layout.children[0].y, 0.0);
5057 }
5058
5059 #[test]
5060 fn align_items_flex_end_child_with_margin_bigger_than_parent() {
5061 let layout = stretch::compute(&stretch::style::Node {
5062 align_items: stretch::style::AlignItems::Center,
5063 justify_content: stretch::style::JustifyContent::Center,
5064 width: stretch::style::Dimension::Points(52.0),
5065 height: stretch::style::Dimension::Points(52.0),
5066 children: vec![stretch::style::Node {
5067 align_items: stretch::style::AlignItems::FlexEnd,
5068 children: vec![stretch::style::Node {
5069 width: stretch::style::Dimension::Points(52.0),
5070 height: stretch::style::Dimension::Points(52.0),
5071 margin: stretch::style::Edges {
5072 start: stretch::style::Dimension::Points(10.0),
5073 end: stretch::style::Dimension::Points(10.0),
5074 ..Default::default()
5075 },
5076 ..Default::default()
5077 }],
5078 ..Default::default()
5079 }],
5080 ..Default::default()
5081 });
5082
5083 assert_eq!(layout.width, 52.0);
5084 assert_eq!(layout.height, 52.0);
5085 assert_eq!(layout.x, 0.0);
5086 assert_eq!(layout.y, 0.0);
5087
5088 assert_eq!(layout.children[0].width, 72.0);
5089 assert_eq!(layout.children[0].height, 52.0);
5090 assert_eq!(layout.children[0].x, -10.0);
5091 assert_eq!(layout.children[0].y, 0.0);
5092
5093 assert_eq!(layout.children[0].children[0].width, 52.0);
5094 assert_eq!(layout.children[0].children[0].height, 52.0);
5095 assert_eq!(layout.children[0].children[0].x, 10.0);
5096 assert_eq!(layout.children[0].children[0].y, 0.0);
5097 }
5098
5099 #[test]
5100 fn justify_content_row_max_width_and_margin() {
5101 let layout = stretch::compute(&stretch::style::Node {
5102 justify_content: stretch::style::JustifyContent::Center,
5103 width: stretch::style::Dimension::Points(100.0),
5104 max_width: stretch::style::Dimension::Points(80.0),
5105 margin: stretch::style::Edges { start: stretch::style::Dimension::Points(100.0), ..Default::default() },
5106 children: vec![stretch::style::Node {
5107 width: stretch::style::Dimension::Points(20.0),
5108 height: stretch::style::Dimension::Points(20.0),
5109 ..Default::default()
5110 }],
5111 ..Default::default()
5112 });
5113
5114 assert_eq!(layout.width, 80.0);
5115 assert_eq!(layout.height, 20.0);
5116 assert_eq!(layout.x, 100.0);
5117 assert_eq!(layout.y, 0.0);
5118
5119 assert_eq!(layout.children[0].width, 20.0);
5120 assert_eq!(layout.children[0].height, 20.0);
5121 assert_eq!(layout.children[0].x, 30.0);
5122 assert_eq!(layout.children[0].y, 0.0);
5123 }
5124
5125 #[test]
5126 fn margin_left() {
5127 let layout = stretch::compute(&stretch::style::Node {
5128 width: stretch::style::Dimension::Points(100.0),
5129 height: stretch::style::Dimension::Points(100.0),
5130 children: vec![stretch::style::Node {
5131 width: stretch::style::Dimension::Points(10.0),
5132 margin: stretch::style::Edges { start: stretch::style::Dimension::Points(10.0), ..Default::default() },
5133 ..Default::default()
5134 }],
5135 ..Default::default()
5136 });
5137
5138 assert_eq!(layout.width, 100.0);
5139 assert_eq!(layout.height, 100.0);
5140 assert_eq!(layout.x, 0.0);
5141 assert_eq!(layout.y, 0.0);
5142
5143 assert_eq!(layout.children[0].width, 10.0);
5144 assert_eq!(layout.children[0].height, 100.0);
5145 assert_eq!(layout.children[0].x, 10.0);
5146 assert_eq!(layout.children[0].y, 0.0);
5147 }
5148
5149 #[test]
5150 fn size_defined_by_child() {
5151 let layout = stretch::compute(&stretch::style::Node {
5152 children: vec![stretch::style::Node {
5153 width: stretch::style::Dimension::Points(100.0),
5154 height: stretch::style::Dimension::Points(100.0),
5155 ..Default::default()
5156 }],
5157 ..Default::default()
5158 });
5159
5160 assert_eq!(layout.width, 100.0);
5161 assert_eq!(layout.height, 100.0);
5162 assert_eq!(layout.x, 0.0);
5163 assert_eq!(layout.y, 0.0);
5164
5165 assert_eq!(layout.children[0].width, 100.0);
5166 assert_eq!(layout.children[0].height, 100.0);
5167 assert_eq!(layout.children[0].x, 0.0);
5168 assert_eq!(layout.children[0].y, 0.0);
5169 }
5170
5171 #[test]
5172 fn rounding_total_fractial_nested() {
5173 let layout = stretch::compute(&stretch::style::Node {
5174 flex_direction: stretch::style::FlexDirection::Column,
5175 width: stretch::style::Dimension::Points(87.4),
5176 height: stretch::style::Dimension::Points(113.4),
5177 children: vec![
5178 stretch::style::Node {
5179 flex_direction: stretch::style::FlexDirection::Column,
5180 flex_grow: 0.7,
5181 flex_basis: stretch::style::Dimension::Points(50.3),
5182 height: stretch::style::Dimension::Points(20.3),
5183 children: vec![
5184 stretch::style::Node {
5185 flex_grow: 1.0,
5186 flex_basis: stretch::style::Dimension::Points(0.3),
5187 height: stretch::style::Dimension::Points(9.9),
5188 bottom: stretch::style::Dimension::Points(13.3),
5189 ..Default::default()
5190 },
5191 stretch::style::Node {
5192 flex_grow: 4.0,
5193 flex_basis: stretch::style::Dimension::Points(0.3),
5194 height: stretch::style::Dimension::Points(1.1),
5195 top: stretch::style::Dimension::Points(13.3),
5196 ..Default::default()
5197 },
5198 ],
5199 ..Default::default()
5200 },
5201 stretch::style::Node {
5202 flex_grow: 1.6,
5203 height: stretch::style::Dimension::Points(10.0),
5204 ..Default::default()
5205 },
5206 stretch::style::Node {
5207 flex_grow: 1.1,
5208 height: stretch::style::Dimension::Points(10.7),
5209 ..Default::default()
5210 },
5211 ],
5212 ..Default::default()
5213 });
5214
5215 assert_eq!(layout.width, 87.0);
5216 assert_eq!(layout.height, 113.0);
5217 assert_eq!(layout.x, 0.0);
5218 assert_eq!(layout.y, 0.0);
5219
5220 assert_eq!(layout.children[0].width, 87.0);
5221 assert_eq!(layout.children[0].height, 59.0);
5222 assert_eq!(layout.children[0].x, 0.0);
5223 assert_eq!(layout.children[0].y, 0.0);
5224
5225 assert_eq!(layout.children[0].children[0].width, 87.0);
5226 assert_eq!(layout.children[0].children[0].height, 12.0);
5227 assert_eq!(layout.children[0].children[0].x, 0.0);
5228 assert_eq!(layout.children[0].children[0].y, -13.0);
5229
5230 assert_eq!(layout.children[0].children[1].width, 87.0);
5231 assert_eq!(layout.children[0].children[1].height, 47.0);
5232 assert_eq!(layout.children[0].children[1].x, 0.0);
5233 assert_eq!(layout.children[0].children[1].y, 25.0);
5234
5235 assert_eq!(layout.children[1].width, 87.0);
5236 assert_eq!(layout.children[1].height, 30.0);
5237 assert_eq!(layout.children[1].x, 0.0);
5238 assert_eq!(layout.children[1].y, 59.0);
5239
5240 assert_eq!(layout.children[2].width, 87.0);
5241 assert_eq!(layout.children[2].height, 24.0);
5242 assert_eq!(layout.children[2].x, 0.0);
5243 assert_eq!(layout.children[2].y, 89.0);
5244 }
5245
5246 #[test]
5247 fn min_max_percent_no_width_height() {
5248 let layout = stretch::compute(&stretch::style::Node {
5249 flex_direction: stretch::style::FlexDirection::Column,
5250 align_items: stretch::style::AlignItems::FlexStart,
5251 width: stretch::style::Dimension::Points(100.0),
5252 height: stretch::style::Dimension::Points(100.0),
5253 children: vec![stretch::style::Node {
5254 min_width: stretch::style::Dimension::Percent(0.1),
5255 max_width: stretch::style::Dimension::Percent(0.1),
5256 min_height: stretch::style::Dimension::Percent(0.1),
5257 max_height: stretch::style::Dimension::Percent(0.1),
5258 ..Default::default()
5259 }],
5260 ..Default::default()
5261 });
5262
5263 assert_eq!(layout.width, 100.0);
5264 assert_eq!(layout.height, 100.0);
5265 assert_eq!(layout.x, 0.0);
5266 assert_eq!(layout.y, 0.0);
5267
5268 assert_eq!(layout.children[0].width, 10.0);
5269 assert_eq!(layout.children[0].height, 10.0);
5270 assert_eq!(layout.children[0].x, 0.0);
5271 assert_eq!(layout.children[0].y, 0.0);
5272 }
5273
5274 #[test]
5275 fn percent_within_flex_grow() {
5276 let layout = stretch::compute(&stretch::style::Node {
5277 width: stretch::style::Dimension::Points(350.0),
5278 height: stretch::style::Dimension::Points(100.0),
5279 children: vec![
5280 stretch::style::Node { width: stretch::style::Dimension::Points(100.0), ..Default::default() },
5281 stretch::style::Node {
5282 flex_direction: stretch::style::FlexDirection::Column,
5283 flex_grow: 1.0,
5284 children: vec![stretch::style::Node {
5285 width: stretch::style::Dimension::Percent(1.0),
5286 ..Default::default()
5287 }],
5288 ..Default::default()
5289 },
5290 stretch::style::Node { width: stretch::style::Dimension::Points(100.0), ..Default::default() },
5291 ],
5292 ..Default::default()
5293 });
5294
5295 assert_eq!(layout.width, 350.0);
5296 assert_eq!(layout.height, 100.0);
5297 assert_eq!(layout.x, 0.0);
5298 assert_eq!(layout.y, 0.0);
5299
5300 assert_eq!(layout.children[0].width, 100.0);
5301 assert_eq!(layout.children[0].height, 100.0);
5302 assert_eq!(layout.children[0].x, 0.0);
5303 assert_eq!(layout.children[0].y, 0.0);
5304
5305 assert_eq!(layout.children[1].width, 150.0);
5306 assert_eq!(layout.children[1].height, 100.0);
5307 assert_eq!(layout.children[1].x, 100.0);
5308 assert_eq!(layout.children[1].y, 0.0);
5309
5310 assert_eq!(layout.children[1].children[0].width, 150.0);
5311 assert_eq!(layout.children[1].children[0].height, 0.0);
5312 assert_eq!(layout.children[1].children[0].x, 0.0);
5313 assert_eq!(layout.children[1].children[0].y, 0.0);
5314
5315 assert_eq!(layout.children[2].width, 100.0);
5316 assert_eq!(layout.children[2].height, 100.0);
5317 assert_eq!(layout.children[2].x, 250.0);
5318 assert_eq!(layout.children[2].y, 0.0);
5319 }
5320
5321 #[test]
5322 fn margin_and_stretch_row() {
5323 let layout = stretch::compute(&stretch::style::Node {
5324 width: stretch::style::Dimension::Points(100.0),
5325 height: stretch::style::Dimension::Points(100.0),
5326 children: vec![stretch::style::Node {
5327 flex_grow: 1.0,
5328 margin: stretch::style::Edges {
5329 top: stretch::style::Dimension::Points(10.0),
5330 bottom: stretch::style::Dimension::Points(10.0),
5331 ..Default::default()
5332 },
5333 ..Default::default()
5334 }],
5335 ..Default::default()
5336 });
5337
5338 assert_eq!(layout.width, 100.0);
5339 assert_eq!(layout.height, 100.0);
5340 assert_eq!(layout.x, 0.0);
5341 assert_eq!(layout.y, 0.0);
5342
5343 assert_eq!(layout.children[0].width, 100.0);
5344 assert_eq!(layout.children[0].height, 80.0);
5345 assert_eq!(layout.children[0].x, 0.0);
5346 assert_eq!(layout.children[0].y, 10.0);
5347 }
5348
5349 #[test]
5350 fn margin_top() {
5351 let layout = stretch::compute(&stretch::style::Node {
5352 flex_direction: stretch::style::FlexDirection::Column,
5353 width: stretch::style::Dimension::Points(100.0),
5354 height: stretch::style::Dimension::Points(100.0),
5355 children: vec![stretch::style::Node {
5356 height: stretch::style::Dimension::Points(10.0),
5357 margin: stretch::style::Edges { top: stretch::style::Dimension::Points(10.0), ..Default::default() },
5358 ..Default::default()
5359 }],
5360 ..Default::default()
5361 });
5362
5363 assert_eq!(layout.width, 100.0);
5364 assert_eq!(layout.height, 100.0);
5365 assert_eq!(layout.x, 0.0);
5366 assert_eq!(layout.y, 0.0);
5367
5368 assert_eq!(layout.children[0].width, 100.0);
5369 assert_eq!(layout.children[0].height, 10.0);
5370 assert_eq!(layout.children[0].x, 0.0);
5371 assert_eq!(layout.children[0].y, 10.0);
5372 }
5373
5374 #[test]
5375 fn percentage_flex_basis_main_max_height() {
5376 let layout = stretch::compute(&stretch::style::Node {
5377 width: stretch::style::Dimension::Points(200.0),
5378 height: stretch::style::Dimension::Points(200.0),
5379 children: vec![
5380 stretch::style::Node {
5381 flex_grow: 1.0,
5382 flex_basis: stretch::style::Dimension::Percent(0.1),
5383 max_height: stretch::style::Dimension::Percent(0.6),
5384 ..Default::default()
5385 },
5386 stretch::style::Node {
5387 flex_grow: 4.0,
5388 flex_basis: stretch::style::Dimension::Percent(0.1),
5389 max_height: stretch::style::Dimension::Percent(0.2),
5390 ..Default::default()
5391 },
5392 ],
5393 ..Default::default()
5394 });
5395
5396 assert_eq!(layout.width, 200.0);
5397 assert_eq!(layout.height, 200.0);
5398 assert_eq!(layout.x, 0.0);
5399 assert_eq!(layout.y, 0.0);
5400
5401 assert_eq!(layout.children[0].width, 52.0);
5402 assert_eq!(layout.children[0].height, 120.0);
5403 assert_eq!(layout.children[0].x, 0.0);
5404 assert_eq!(layout.children[0].y, 0.0);
5405
5406 assert_eq!(layout.children[1].width, 148.0);
5407 assert_eq!(layout.children[1].height, 40.0);
5408 assert_eq!(layout.children[1].x, 52.0);
5409 assert_eq!(layout.children[1].y, 0.0);
5410 }
5411
5412 #[test]
5413 fn margin_auto_left_and_right_column() {
5414 let layout = stretch::compute(&stretch::style::Node {
5415 align_items: stretch::style::AlignItems::Center,
5416 width: stretch::style::Dimension::Points(200.0),
5417 height: stretch::style::Dimension::Points(200.0),
5418 children: vec![
5419 stretch::style::Node {
5420 width: stretch::style::Dimension::Points(50.0),
5421 height: stretch::style::Dimension::Points(50.0),
5422 margin: stretch::style::Edges {
5423 start: stretch::style::Dimension::Auto,
5424 end: stretch::style::Dimension::Auto,
5425 ..Default::default()
5426 },
5427 ..Default::default()
5428 },
5429 stretch::style::Node {
5430 width: stretch::style::Dimension::Points(50.0),
5431 height: stretch::style::Dimension::Points(50.0),
5432 ..Default::default()
5433 },
5434 ],
5435 ..Default::default()
5436 });
5437
5438 assert_eq!(layout.width, 200.0);
5439 assert_eq!(layout.height, 200.0);
5440 assert_eq!(layout.x, 0.0);
5441 assert_eq!(layout.y, 0.0);
5442
5443 assert_eq!(layout.children[0].width, 50.0);
5444 assert_eq!(layout.children[0].height, 50.0);
5445 assert_eq!(layout.children[0].x, 50.0);
5446 assert_eq!(layout.children[0].y, 75.0);
5447
5448 assert_eq!(layout.children[1].width, 50.0);
5449 assert_eq!(layout.children[1].height, 50.0);
5450 assert_eq!(layout.children[1].x, 150.0);
5451 assert_eq!(layout.children[1].y, 75.0);
5452 }
5453
5454 #[test]
5455 fn margin_and_flex_row() {
5456 let layout = stretch::compute(&stretch::style::Node {
5457 width: stretch::style::Dimension::Points(100.0),
5458 height: stretch::style::Dimension::Points(100.0),
5459 children: vec![stretch::style::Node {
5460 flex_grow: 1.0,
5461 margin: stretch::style::Edges {
5462 start: stretch::style::Dimension::Points(10.0),
5463 end: stretch::style::Dimension::Points(10.0),
5464 ..Default::default()
5465 },
5466 ..Default::default()
5467 }],
5468 ..Default::default()
5469 });
5470
5471 assert_eq!(layout.width, 100.0);
5472 assert_eq!(layout.height, 100.0);
5473 assert_eq!(layout.x, 0.0);
5474 assert_eq!(layout.y, 0.0);
5475
5476 assert_eq!(layout.children[0].width, 80.0);
5477 assert_eq!(layout.children[0].height, 100.0);
5478 assert_eq!(layout.children[0].x, 10.0);
5479 assert_eq!(layout.children[0].y, 0.0);
5480 }
5481
5482 #[test]
5483 fn flex_wrap_children_with_min_main_overriding_flex_basis() {
5484 let layout = stretch::compute(&stretch::style::Node {
5485 width: stretch::style::Dimension::Points(100.0),
5486 children: vec![
5487 stretch::style::Node {
5488 flex_basis: stretch::style::Dimension::Points(50.0),
5489 min_width: stretch::style::Dimension::Points(55.0),
5490 height: stretch::style::Dimension::Points(50.0),
5491 ..Default::default()
5492 },
5493 stretch::style::Node {
5494 flex_basis: stretch::style::Dimension::Points(50.0),
5495 min_width: stretch::style::Dimension::Points(55.0),
5496 height: stretch::style::Dimension::Points(50.0),
5497 ..Default::default()
5498 },
5499 ],
5500 ..Default::default()
5501 });
5502
5503 assert_eq!(layout.width, 100.0);
5504 assert_eq!(layout.height, 100.0);
5505 assert_eq!(layout.x, 0.0);
5506 assert_eq!(layout.y, 0.0);
5507
5508 assert_eq!(layout.children[0].width, 55.0);
5509 assert_eq!(layout.children[0].height, 50.0);
5510 assert_eq!(layout.children[0].x, 0.0);
5511 assert_eq!(layout.children[0].y, 0.0);
5512
5513 assert_eq!(layout.children[1].width, 55.0);
5514 assert_eq!(layout.children[1].height, 50.0);
5515 assert_eq!(layout.children[1].x, 0.0);
5516 assert_eq!(layout.children[1].y, 50.0);
5517 }
5518
5519 #[test]
5520 fn wrap_row_align_items_center() {
5521 let layout = stretch::compute(&stretch::style::Node {
5522 align_items: stretch::style::AlignItems::Center,
5523 width: stretch::style::Dimension::Points(100.0),
5524 children: vec![
5525 stretch::style::Node {
5526 width: stretch::style::Dimension::Points(30.0),
5527 height: stretch::style::Dimension::Points(10.0),
5528 ..Default::default()
5529 },
5530 stretch::style::Node {
5531 width: stretch::style::Dimension::Points(30.0),
5532 height: stretch::style::Dimension::Points(20.0),
5533 ..Default::default()
5534 },
5535 stretch::style::Node {
5536 width: stretch::style::Dimension::Points(30.0),
5537 height: stretch::style::Dimension::Points(30.0),
5538 ..Default::default()
5539 },
5540 stretch::style::Node {
5541 width: stretch::style::Dimension::Points(30.0),
5542 height: stretch::style::Dimension::Points(30.0),
5543 ..Default::default()
5544 },
5545 ],
5546 ..Default::default()
5547 });
5548
5549 assert_eq!(layout.width, 100.0);
5550 assert_eq!(layout.height, 60.0);
5551 assert_eq!(layout.x, 0.0);
5552 assert_eq!(layout.y, 0.0);
5553
5554 assert_eq!(layout.children[0].width, 30.0);
5555 assert_eq!(layout.children[0].height, 10.0);
5556 assert_eq!(layout.children[0].x, 0.0);
5557 assert_eq!(layout.children[0].y, 10.0);
5558
5559 assert_eq!(layout.children[1].width, 30.0);
5560 assert_eq!(layout.children[1].height, 20.0);
5561 assert_eq!(layout.children[1].x, 30.0);
5562 assert_eq!(layout.children[1].y, 5.0);
5563
5564 assert_eq!(layout.children[2].width, 30.0);
5565 assert_eq!(layout.children[2].height, 30.0);
5566 assert_eq!(layout.children[2].x, 60.0);
5567 assert_eq!(layout.children[2].y, 0.0);
5568
5569 assert_eq!(layout.children[3].width, 30.0);
5570 assert_eq!(layout.children[3].height, 30.0);
5571 assert_eq!(layout.children[3].x, 0.0);
5572 assert_eq!(layout.children[3].y, 30.0);
5573 }
5574
5575 #[test]
5576 fn justify_content_colunn_max_height_and_margin() {
5577 let layout = stretch::compute(&stretch::style::Node {
5578 flex_direction: stretch::style::FlexDirection::Column,
5579 justify_content: stretch::style::JustifyContent::Center,
5580 height: stretch::style::Dimension::Points(100.0),
5581 max_height: stretch::style::Dimension::Points(80.0),
5582 margin: stretch::style::Edges { top: stretch::style::Dimension::Points(100.0), ..Default::default() },
5583 children: vec![stretch::style::Node {
5584 width: stretch::style::Dimension::Points(20.0),
5585 height: stretch::style::Dimension::Points(20.0),
5586 ..Default::default()
5587 }],
5588 ..Default::default()
5589 });
5590
5591 assert_eq!(layout.width, 20.0);
5592 assert_eq!(layout.height, 80.0);
5593 assert_eq!(layout.x, 0.0);
5594 assert_eq!(layout.y, 100.0);
5595
5596 assert_eq!(layout.children[0].width, 20.0);
5597 assert_eq!(layout.children[0].height, 20.0);
5598 assert_eq!(layout.children[0].x, 0.0);
5599 assert_eq!(layout.children[0].y, 30.0);
5600 }
5601
5602 #[test]
5603 fn flex_basis_flex_grow_column() {
5604 let layout = stretch::compute(&stretch::style::Node {
5605 flex_direction: stretch::style::FlexDirection::Column,
5606 width: stretch::style::Dimension::Points(100.0),
5607 height: stretch::style::Dimension::Points(100.0),
5608 children: vec![
5609 stretch::style::Node {
5610 flex_grow: 1.0,
5611 flex_basis: stretch::style::Dimension::Points(50.0),
5612 ..Default::default()
5613 },
5614 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
5615 ],
5616 ..Default::default()
5617 });
5618
5619 assert_eq!(layout.width, 100.0);
5620 assert_eq!(layout.height, 100.0);
5621 assert_eq!(layout.x, 0.0);
5622 assert_eq!(layout.y, 0.0);
5623
5624 assert_eq!(layout.children[0].width, 100.0);
5625 assert_eq!(layout.children[0].height, 75.0);
5626 assert_eq!(layout.children[0].x, 0.0);
5627 assert_eq!(layout.children[0].y, 0.0);
5628
5629 assert_eq!(layout.children[1].width, 100.0);
5630 assert_eq!(layout.children[1].height, 25.0);
5631 assert_eq!(layout.children[1].x, 0.0);
5632 assert_eq!(layout.children[1].y, 75.0);
5633 }
5634
5635 #[test]
5636 fn absolute_layout_align_items_and_justify_content_center_and_right_position() {
5637 let layout = stretch::compute(&stretch::style::Node {
5638 align_items: stretch::style::AlignItems::Center,
5639 justify_content: stretch::style::JustifyContent::Center,
5640 width: stretch::style::Dimension::Points(110.0),
5641 height: stretch::style::Dimension::Points(100.0),
5642 children: vec![stretch::style::Node {
5643 position: stretch::style::Position::Absolute,
5644 width: stretch::style::Dimension::Points(60.0),
5645 height: stretch::style::Dimension::Points(40.0),
5646 end: stretch::style::Dimension::Points(5.0),
5647 ..Default::default()
5648 }],
5649 ..Default::default()
5650 });
5651
5652 assert_eq!(layout.width, 110.0);
5653 assert_eq!(layout.height, 100.0);
5654 assert_eq!(layout.x, 0.0);
5655 assert_eq!(layout.y, 0.0);
5656
5657 assert_eq!(layout.children[0].width, 60.0);
5658 assert_eq!(layout.children[0].height, 40.0);
5659 assert_eq!(layout.children[0].x, 45.0);
5660 assert_eq!(layout.children[0].y, 30.0);
5661 }
5662
5663 #[test]
5664 fn overflow_cross_axis() {
5665 let layout = stretch::compute(&stretch::style::Node {
5666 width: stretch::style::Dimension::Points(100.0),
5667 height: stretch::style::Dimension::Points(100.0),
5668 children: vec![stretch::style::Node {
5669 width: stretch::style::Dimension::Points(100.0),
5670 height: stretch::style::Dimension::Points(200.0),
5671 ..Default::default()
5672 }],
5673 ..Default::default()
5674 });
5675
5676 assert_eq!(layout.width, 100.0);
5677 assert_eq!(layout.height, 100.0);
5678 assert_eq!(layout.x, 0.0);
5679 assert_eq!(layout.y, 0.0);
5680
5681 assert_eq!(layout.children[0].width, 100.0);
5682 assert_eq!(layout.children[0].height, 200.0);
5683 assert_eq!(layout.children[0].x, 0.0);
5684 assert_eq!(layout.children[0].y, 0.0);
5685 }
5686
5687 #[test]
5688 fn wrap_column() {
5689 let layout = stretch::compute(&stretch::style::Node {
5690 flex_direction: stretch::style::FlexDirection::Column,
5691 height: stretch::style::Dimension::Points(100.0),
5692 children: vec![
5693 stretch::style::Node {
5694 width: stretch::style::Dimension::Points(30.0),
5695 height: stretch::style::Dimension::Points(30.0),
5696 ..Default::default()
5697 },
5698 stretch::style::Node {
5699 width: stretch::style::Dimension::Points(30.0),
5700 height: stretch::style::Dimension::Points(30.0),
5701 ..Default::default()
5702 },
5703 stretch::style::Node {
5704 width: stretch::style::Dimension::Points(30.0),
5705 height: stretch::style::Dimension::Points(30.0),
5706 ..Default::default()
5707 },
5708 stretch::style::Node {
5709 width: stretch::style::Dimension::Points(30.0),
5710 height: stretch::style::Dimension::Points(30.0),
5711 ..Default::default()
5712 },
5713 ],
5714 ..Default::default()
5715 });
5716
5717 assert_eq!(layout.width, 30.0);
5718 assert_eq!(layout.height, 100.0);
5719 assert_eq!(layout.x, 0.0);
5720 assert_eq!(layout.y, 0.0);
5721
5722 assert_eq!(layout.children[0].width, 30.0);
5723 assert_eq!(layout.children[0].height, 30.0);
5724 assert_eq!(layout.children[0].x, 0.0);
5725 assert_eq!(layout.children[0].y, 0.0);
5726
5727 assert_eq!(layout.children[1].width, 30.0);
5728 assert_eq!(layout.children[1].height, 30.0);
5729 assert_eq!(layout.children[1].x, 0.0);
5730 assert_eq!(layout.children[1].y, 30.0);
5731
5732 assert_eq!(layout.children[2].width, 30.0);
5733 assert_eq!(layout.children[2].height, 30.0);
5734 assert_eq!(layout.children[2].x, 0.0);
5735 assert_eq!(layout.children[2].y, 60.0);
5736
5737 assert_eq!(layout.children[3].width, 30.0);
5738 assert_eq!(layout.children[3].height, 30.0);
5739 assert_eq!(layout.children[3].x, 30.0);
5740 assert_eq!(layout.children[3].y, 0.0);
5741 }
5742
5743 #[test]
5744 fn justify_content_column_space_evenly() {
5745 let layout = stretch::compute(&stretch::style::Node {
5746 flex_direction: stretch::style::FlexDirection::Column,
5747 justify_content: stretch::style::JustifyContent::SpaceEvenly,
5748 width: stretch::style::Dimension::Points(100.0),
5749 height: stretch::style::Dimension::Points(100.0),
5750 children: vec![
5751 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
5752 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
5753 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
5754 ],
5755 ..Default::default()
5756 });
5757
5758 assert_eq!(layout.width, 100.0);
5759 assert_eq!(layout.height, 100.0);
5760 assert_eq!(layout.x, 0.0);
5761 assert_eq!(layout.y, 0.0);
5762
5763 assert_eq!(layout.children[0].width, 100.0);
5764 assert_eq!(layout.children[0].height, 10.0);
5765 assert_eq!(layout.children[0].x, 0.0);
5766 assert_eq!(layout.children[0].y, 18.0);
5767
5768 assert_eq!(layout.children[1].width, 100.0);
5769 assert_eq!(layout.children[1].height, 10.0);
5770 assert_eq!(layout.children[1].x, 0.0);
5771 assert_eq!(layout.children[1].y, 45.0);
5772
5773 assert_eq!(layout.children[2].width, 100.0);
5774 assert_eq!(layout.children[2].height, 10.0);
5775 assert_eq!(layout.children[2].x, 0.0);
5776 assert_eq!(layout.children[2].y, 73.0);
5777 }
5778
5779 #[test]
5780 fn flex_grow_within_constrained_max_column() {
5781 let layout = stretch::compute(&stretch::style::Node {
5782 flex_direction: stretch::style::FlexDirection::Column,
5783 width: stretch::style::Dimension::Points(100.0),
5784 max_height: stretch::style::Dimension::Points(100.0),
5785 children: vec![
5786 stretch::style::Node {
5787 flex_shrink: 1.0,
5788 flex_basis: stretch::style::Dimension::Points(100.0),
5789 ..Default::default()
5790 },
5791 stretch::style::Node { height: stretch::style::Dimension::Points(50.0), ..Default::default() },
5792 ],
5793 ..Default::default()
5794 });
5795
5796 assert_eq!(layout.width, 100.0);
5797 assert_eq!(layout.height, 100.0);
5798 assert_eq!(layout.x, 0.0);
5799 assert_eq!(layout.y, 0.0);
5800
5801 assert_eq!(layout.children[0].width, 100.0);
5802 assert_eq!(layout.children[0].height, 67.0);
5803 assert_eq!(layout.children[0].x, 0.0);
5804 assert_eq!(layout.children[0].y, 0.0);
5805
5806 assert_eq!(layout.children[1].width, 100.0);
5807 assert_eq!(layout.children[1].height, 33.0);
5808 assert_eq!(layout.children[1].x, 0.0);
5809 assert_eq!(layout.children[1].y, 67.0);
5810 }
5811
5812 #[test]
5813 fn justify_content_column_space_around() {
5814 let layout = stretch::compute(&stretch::style::Node {
5815 flex_direction: stretch::style::FlexDirection::Column,
5816 justify_content: stretch::style::JustifyContent::SpaceAround,
5817 width: stretch::style::Dimension::Points(100.0),
5818 height: stretch::style::Dimension::Points(100.0),
5819 children: vec![
5820 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
5821 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
5822 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
5823 ],
5824 ..Default::default()
5825 });
5826
5827 assert_eq!(layout.width, 100.0);
5828 assert_eq!(layout.height, 100.0);
5829 assert_eq!(layout.x, 0.0);
5830 assert_eq!(layout.y, 0.0);
5831
5832 assert_eq!(layout.children[0].width, 100.0);
5833 assert_eq!(layout.children[0].height, 10.0);
5834 assert_eq!(layout.children[0].x, 0.0);
5835 assert_eq!(layout.children[0].y, 12.0);
5836
5837 assert_eq!(layout.children[1].width, 100.0);
5838 assert_eq!(layout.children[1].height, 10.0);
5839 assert_eq!(layout.children[1].x, 0.0);
5840 assert_eq!(layout.children[1].y, 45.0);
5841
5842 assert_eq!(layout.children[2].width, 100.0);
5843 assert_eq!(layout.children[2].height, 10.0);
5844 assert_eq!(layout.children[2].x, 0.0);
5845 assert_eq!(layout.children[2].y, 78.0);
5846 }
5847
5848 #[test]
5849 fn absolute_layout_align_items_and_justify_content_center_and_left_position() {
5850 let layout = stretch::compute(&stretch::style::Node {
5851 align_items: stretch::style::AlignItems::Center,
5852 justify_content: stretch::style::JustifyContent::Center,
5853 width: stretch::style::Dimension::Points(110.0),
5854 height: stretch::style::Dimension::Points(100.0),
5855 children: vec![stretch::style::Node {
5856 position: stretch::style::Position::Absolute,
5857 width: stretch::style::Dimension::Points(60.0),
5858 height: stretch::style::Dimension::Points(40.0),
5859 start: stretch::style::Dimension::Points(5.0),
5860 ..Default::default()
5861 }],
5862 ..Default::default()
5863 });
5864
5865 assert_eq!(layout.width, 110.0);
5866 assert_eq!(layout.height, 100.0);
5867 assert_eq!(layout.x, 0.0);
5868 assert_eq!(layout.y, 0.0);
5869
5870 assert_eq!(layout.children[0].width, 60.0);
5871 assert_eq!(layout.children[0].height, 40.0);
5872 assert_eq!(layout.children[0].x, 5.0);
5873 assert_eq!(layout.children[0].y, 30.0);
5874 }
5875
5876 #[test]
5877 fn percentage_flex_basis_main_min_width() {
5878 let layout = stretch::compute(&stretch::style::Node {
5879 width: stretch::style::Dimension::Points(200.0),
5880 height: stretch::style::Dimension::Points(200.0),
5881 children: vec![
5882 stretch::style::Node {
5883 flex_grow: 1.0,
5884 flex_basis: stretch::style::Dimension::Percent(0.2),
5885 min_width: stretch::style::Dimension::Percent(0.6),
5886 ..Default::default()
5887 },
5888 stretch::style::Node {
5889 flex_grow: 4.0,
5890 flex_basis: stretch::style::Dimension::Percent(0.1),
5891 min_width: stretch::style::Dimension::Percent(0.2),
5892 ..Default::default()
5893 },
5894 ],
5895 ..Default::default()
5896 });
5897
5898 assert_eq!(layout.width, 200.0);
5899 assert_eq!(layout.height, 200.0);
5900 assert_eq!(layout.x, 0.0);
5901 assert_eq!(layout.y, 0.0);
5902
5903 assert_eq!(layout.children[0].width, 120.0);
5904 assert_eq!(layout.children[0].height, 200.0);
5905 assert_eq!(layout.children[0].x, 0.0);
5906 assert_eq!(layout.children[0].y, 0.0);
5907
5908 assert_eq!(layout.children[1].width, 80.0);
5909 assert_eq!(layout.children[1].height, 200.0);
5910 assert_eq!(layout.children[1].x, 120.0);
5911 assert_eq!(layout.children[1].y, 0.0);
5912 }
5913
5914 #[test]
5915 fn margin_auto_bottom() {
5916 let layout = stretch::compute(&stretch::style::Node {
5917 align_items: stretch::style::AlignItems::Center,
5918 width: stretch::style::Dimension::Points(200.0),
5919 height: stretch::style::Dimension::Points(200.0),
5920 children: vec![
5921 stretch::style::Node {
5922 width: stretch::style::Dimension::Points(50.0),
5923 height: stretch::style::Dimension::Points(50.0),
5924 margin: stretch::style::Edges { bottom: stretch::style::Dimension::Auto, ..Default::default() },
5925 ..Default::default()
5926 },
5927 stretch::style::Node {
5928 width: stretch::style::Dimension::Points(50.0),
5929 height: stretch::style::Dimension::Points(50.0),
5930 ..Default::default()
5931 },
5932 ],
5933 ..Default::default()
5934 });
5935
5936 assert_eq!(layout.width, 200.0);
5937 assert_eq!(layout.height, 200.0);
5938 assert_eq!(layout.x, 0.0);
5939 assert_eq!(layout.y, 0.0);
5940
5941 assert_eq!(layout.children[0].width, 50.0);
5942 assert_eq!(layout.children[0].height, 50.0);
5943 assert_eq!(layout.children[0].x, 0.0);
5944 assert_eq!(layout.children[0].y, 0.0);
5945
5946 assert_eq!(layout.children[1].width, 50.0);
5947 assert_eq!(layout.children[1].height, 50.0);
5948 assert_eq!(layout.children[1].x, 50.0);
5949 assert_eq!(layout.children[1].y, 75.0);
5950 }
5951
5952 #[test]
5953 fn flex_grow_shrink_at_most() {
5954 let layout = stretch::compute(&stretch::style::Node {
5955 width: stretch::style::Dimension::Points(100.0),
5956 height: stretch::style::Dimension::Points(100.0),
5957 children: vec![stretch::style::Node {
5958 children: vec![stretch::style::Node { flex_grow: 1.0, flex_shrink: 1.0, ..Default::default() }],
5959 ..Default::default()
5960 }],
5961 ..Default::default()
5962 });
5963
5964 assert_eq!(layout.width, 100.0);
5965 assert_eq!(layout.height, 100.0);
5966 assert_eq!(layout.x, 0.0);
5967 assert_eq!(layout.y, 0.0);
5968
5969 assert_eq!(layout.children[0].width, 0.0);
5970 assert_eq!(layout.children[0].height, 100.0);
5971 assert_eq!(layout.children[0].x, 0.0);
5972 assert_eq!(layout.children[0].y, 0.0);
5973
5974 assert_eq!(layout.children[0].children[0].width, 0.0);
5975 assert_eq!(layout.children[0].children[0].height, 100.0);
5976 assert_eq!(layout.children[0].children[0].x, 0.0);
5977 assert_eq!(layout.children[0].children[0].y, 0.0);
5978 }
5979
5980 #[test]
5981 fn percentage_padding_should_calculate_based_only_on_width() {
5982 let layout = stretch::compute(&stretch::style::Node {
5983 flex_direction: stretch::style::FlexDirection::Column,
5984 width: stretch::style::Dimension::Points(200.0),
5985 height: stretch::style::Dimension::Points(100.0),
5986 children: vec![stretch::style::Node {
5987 flex_direction: stretch::style::FlexDirection::Column,
5988 flex_grow: 1.0,
5989 padding: stretch::style::Edges {
5990 start: stretch::style::Dimension::Percent(0.1),
5991 end: stretch::style::Dimension::Percent(0.1),
5992 top: stretch::style::Dimension::Percent(0.1),
5993 bottom: stretch::style::Dimension::Percent(0.1),
5994 ..Default::default()
5995 },
5996 children: vec![stretch::style::Node {
5997 width: stretch::style::Dimension::Points(10.0),
5998 height: stretch::style::Dimension::Points(10.0),
5999 ..Default::default()
6000 }],
6001 ..Default::default()
6002 }],
6003 ..Default::default()
6004 });
6005
6006 assert_eq!(layout.width, 200.0);
6007 assert_eq!(layout.height, 100.0);
6008 assert_eq!(layout.x, 0.0);
6009 assert_eq!(layout.y, 0.0);
6010
6011 assert_eq!(layout.children[0].width, 200.0);
6012 assert_eq!(layout.children[0].height, 100.0);
6013 assert_eq!(layout.children[0].x, 0.0);
6014 assert_eq!(layout.children[0].y, 0.0);
6015
6016 assert_eq!(layout.children[0].children[0].width, 10.0);
6017 assert_eq!(layout.children[0].children[0].height, 10.0);
6018 assert_eq!(layout.children[0].children[0].x, 20.0);
6019 assert_eq!(layout.children[0].children[0].y, 20.0);
6020 }
6021
6022 #[test]
6023 fn flex_shrink_to_zero() {
6024 let layout = stretch::compute(&stretch::style::Node {
6025 width: stretch::style::Dimension::Points(75.0),
6026 children: vec![
6027 stretch::style::Node {
6028 flex_shrink: 0.0,
6029 width: stretch::style::Dimension::Points(50.0),
6030 height: stretch::style::Dimension::Points(50.0),
6031 ..Default::default()
6032 },
6033 stretch::style::Node {
6034 flex_shrink: 1.0,
6035 width: stretch::style::Dimension::Points(50.0),
6036 height: stretch::style::Dimension::Points(50.0),
6037 ..Default::default()
6038 },
6039 stretch::style::Node {
6040 flex_shrink: 0.0,
6041 width: stretch::style::Dimension::Points(50.0),
6042 height: stretch::style::Dimension::Points(50.0),
6043 ..Default::default()
6044 },
6045 ],
6046 ..Default::default()
6047 });
6048
6049 assert_eq!(layout.width, 75.0);
6050 assert_eq!(layout.height, 50.0);
6051 assert_eq!(layout.x, 0.0);
6052 assert_eq!(layout.y, 0.0);
6053
6054 assert_eq!(layout.children[0].width, 50.0);
6055 assert_eq!(layout.children[0].height, 50.0);
6056 assert_eq!(layout.children[0].x, 0.0);
6057 assert_eq!(layout.children[0].y, 0.0);
6058
6059 assert_eq!(layout.children[1].width, 0.0);
6060 assert_eq!(layout.children[1].height, 50.0);
6061 assert_eq!(layout.children[1].x, 50.0);
6062 assert_eq!(layout.children[1].y, 0.0);
6063
6064 assert_eq!(layout.children[2].width, 50.0);
6065 assert_eq!(layout.children[2].height, 50.0);
6066 assert_eq!(layout.children[2].x, 50.0);
6067 assert_eq!(layout.children[2].y, 0.0);
6068 }
6069
6070 #[test]
6071 fn margin_auto_mutiple_children_row() {
6072 let layout = stretch::compute(&stretch::style::Node {
6073 align_items: stretch::style::AlignItems::Center,
6074 width: stretch::style::Dimension::Points(200.0),
6075 height: stretch::style::Dimension::Points(200.0),
6076 children: vec![
6077 stretch::style::Node {
6078 width: stretch::style::Dimension::Points(50.0),
6079 height: stretch::style::Dimension::Points(50.0),
6080 margin: stretch::style::Edges { end: stretch::style::Dimension::Auto, ..Default::default() },
6081 ..Default::default()
6082 },
6083 stretch::style::Node {
6084 width: stretch::style::Dimension::Points(50.0),
6085 height: stretch::style::Dimension::Points(50.0),
6086 margin: stretch::style::Edges { end: stretch::style::Dimension::Auto, ..Default::default() },
6087 ..Default::default()
6088 },
6089 stretch::style::Node {
6090 width: stretch::style::Dimension::Points(50.0),
6091 height: stretch::style::Dimension::Points(50.0),
6092 ..Default::default()
6093 },
6094 ],
6095 ..Default::default()
6096 });
6097
6098 assert_eq!(layout.width, 200.0);
6099 assert_eq!(layout.height, 200.0);
6100 assert_eq!(layout.x, 0.0);
6101 assert_eq!(layout.y, 0.0);
6102
6103 assert_eq!(layout.children[0].width, 50.0);
6104 assert_eq!(layout.children[0].height, 50.0);
6105 assert_eq!(layout.children[0].x, 0.0);
6106 assert_eq!(layout.children[0].y, 75.0);
6107
6108 assert_eq!(layout.children[1].width, 50.0);
6109 assert_eq!(layout.children[1].height, 50.0);
6110 assert_eq!(layout.children[1].x, 75.0);
6111 assert_eq!(layout.children[1].y, 75.0);
6112
6113 assert_eq!(layout.children[2].width, 50.0);
6114 assert_eq!(layout.children[2].height, 50.0);
6115 assert_eq!(layout.children[2].x, 150.0);
6116 assert_eq!(layout.children[2].y, 75.0);
6117 }
6118
6119 #[test]
6120 fn justify_content_min_width_with_padding_child_width_lower_than_parent() {
6121 let layout = stretch::compute(&stretch::style::Node {
6122 flex_direction: stretch::style::FlexDirection::Column,
6123 width: stretch::style::Dimension::Points(1080.0),
6124 height: stretch::style::Dimension::Points(1584.0),
6125 children: vec![stretch::style::Node {
6126 children: vec![stretch::style::Node {
6127 justify_content: stretch::style::JustifyContent::Center,
6128 min_width: stretch::style::Dimension::Points(400.0),
6129 padding: stretch::style::Edges {
6130 start: stretch::style::Dimension::Points(100.0),
6131 end: stretch::style::Dimension::Points(100.0),
6132 ..Default::default()
6133 },
6134 children: vec![stretch::style::Node {
6135 width: stretch::style::Dimension::Points(199.0),
6136 height: stretch::style::Dimension::Points(100.0),
6137 ..Default::default()
6138 }],
6139 ..Default::default()
6140 }],
6141 ..Default::default()
6142 }],
6143 ..Default::default()
6144 });
6145
6146 assert_eq!(layout.width, 1080.0);
6147 assert_eq!(layout.height, 1584.0);
6148 assert_eq!(layout.x, 0.0);
6149 assert_eq!(layout.y, 0.0);
6150
6151 assert_eq!(layout.children[0].width, 1080.0);
6152 assert_eq!(layout.children[0].height, 100.0);
6153 assert_eq!(layout.children[0].x, 0.0);
6154 assert_eq!(layout.children[0].y, 0.0);
6155
6156 assert_eq!(layout.children[0].children[0].width, 400.0);
6157 assert_eq!(layout.children[0].children[0].height, 100.0);
6158 assert_eq!(layout.children[0].children[0].x, 0.0);
6159 assert_eq!(layout.children[0].children[0].y, 0.0);
6160
6161 assert_eq!(layout.children[0].children[0].children[0].width, 199.0);
6162 assert_eq!(layout.children[0].children[0].children[0].height, 100.0);
6163 assert_eq!(layout.children[0].children[0].children[0].x, 101.0);
6164 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
6165 }
6166
6167 #[test]
6168 fn size_defined_by_grand_child() {
6169 let layout = stretch::compute(&stretch::style::Node {
6170 children: vec![stretch::style::Node {
6171 children: vec![stretch::style::Node {
6172 width: stretch::style::Dimension::Points(100.0),
6173 height: stretch::style::Dimension::Points(100.0),
6174 ..Default::default()
6175 }],
6176 ..Default::default()
6177 }],
6178 ..Default::default()
6179 });
6180
6181 assert_eq!(layout.width, 100.0);
6182 assert_eq!(layout.height, 100.0);
6183 assert_eq!(layout.x, 0.0);
6184 assert_eq!(layout.y, 0.0);
6185
6186 assert_eq!(layout.children[0].width, 100.0);
6187 assert_eq!(layout.children[0].height, 100.0);
6188 assert_eq!(layout.children[0].x, 0.0);
6189 assert_eq!(layout.children[0].y, 0.0);
6190
6191 assert_eq!(layout.children[0].children[0].width, 100.0);
6192 assert_eq!(layout.children[0].children[0].height, 100.0);
6193 assert_eq!(layout.children[0].children[0].x, 0.0);
6194 assert_eq!(layout.children[0].children[0].y, 0.0);
6195 }
6196
6197 #[test]
6198 fn absolute_layout_align_items_and_justify_content_center_and_bottom_position() {
6199 let layout = stretch::compute(&stretch::style::Node {
6200 align_items: stretch::style::AlignItems::Center,
6201 justify_content: stretch::style::JustifyContent::Center,
6202 width: stretch::style::Dimension::Points(110.0),
6203 height: stretch::style::Dimension::Points(100.0),
6204 children: vec![stretch::style::Node {
6205 position: stretch::style::Position::Absolute,
6206 width: stretch::style::Dimension::Points(60.0),
6207 height: stretch::style::Dimension::Points(40.0),
6208 bottom: stretch::style::Dimension::Points(10.0),
6209 ..Default::default()
6210 }],
6211 ..Default::default()
6212 });
6213
6214 assert_eq!(layout.width, 110.0);
6215 assert_eq!(layout.height, 100.0);
6216 assert_eq!(layout.x, 0.0);
6217 assert_eq!(layout.y, 0.0);
6218
6219 assert_eq!(layout.children[0].width, 60.0);
6220 assert_eq!(layout.children[0].height, 40.0);
6221 assert_eq!(layout.children[0].x, 25.0);
6222 assert_eq!(layout.children[0].y, 50.0);
6223 }
6224
6225 #[test]
6226 fn justify_content_min_max() {
6227 let layout = stretch::compute(&stretch::style::Node {
6228 flex_direction: stretch::style::FlexDirection::Column,
6229 justify_content: stretch::style::JustifyContent::Center,
6230 width: stretch::style::Dimension::Points(100.0),
6231 min_height: stretch::style::Dimension::Points(100.0),
6232 max_height: stretch::style::Dimension::Points(200.0),
6233 children: vec![stretch::style::Node {
6234 width: stretch::style::Dimension::Points(60.0),
6235 height: stretch::style::Dimension::Points(60.0),
6236 ..Default::default()
6237 }],
6238 ..Default::default()
6239 });
6240
6241 assert_eq!(layout.width, 100.0);
6242 assert_eq!(layout.height, 100.0);
6243 assert_eq!(layout.x, 0.0);
6244 assert_eq!(layout.y, 0.0);
6245
6246 assert_eq!(layout.children[0].width, 60.0);
6247 assert_eq!(layout.children[0].height, 60.0);
6248 assert_eq!(layout.children[0].x, 0.0);
6249 assert_eq!(layout.children[0].y, 20.0);
6250 }
6251
6252 #[test]
6253 fn padding_align_end_child() {
6254 let layout = stretch::compute(&stretch::style::Node {
6255 align_items: stretch::style::AlignItems::FlexEnd,
6256 justify_content: stretch::style::JustifyContent::FlexEnd,
6257 width: stretch::style::Dimension::Points(200.0),
6258 height: stretch::style::Dimension::Points(200.0),
6259 children: vec![stretch::style::Node {
6260 width: stretch::style::Dimension::Points(100.0),
6261 height: stretch::style::Dimension::Points(100.0),
6262 padding: stretch::style::Edges {
6263 start: stretch::style::Dimension::Points(20.0),
6264 end: stretch::style::Dimension::Points(20.0),
6265 top: stretch::style::Dimension::Points(20.0),
6266 bottom: stretch::style::Dimension::Points(20.0),
6267 ..Default::default()
6268 },
6269 ..Default::default()
6270 }],
6271 ..Default::default()
6272 });
6273
6274 assert_eq!(layout.width, 200.0);
6275 assert_eq!(layout.height, 200.0);
6276 assert_eq!(layout.x, 0.0);
6277 assert_eq!(layout.y, 0.0);
6278
6279 assert_eq!(layout.children[0].width, 100.0);
6280 assert_eq!(layout.children[0].height, 100.0);
6281 assert_eq!(layout.children[0].x, 100.0);
6282 assert_eq!(layout.children[0].y, 100.0);
6283 }
6284
6285 #[test]
6286 fn justify_content_row_space_between() {
6287 let layout = stretch::compute(&stretch::style::Node {
6288 justify_content: stretch::style::JustifyContent::SpaceBetween,
6289 width: stretch::style::Dimension::Points(100.0),
6290 height: stretch::style::Dimension::Points(100.0),
6291 children: vec![
6292 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
6293 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
6294 stretch::style::Node { width: stretch::style::Dimension::Points(10.0), ..Default::default() },
6295 ],
6296 ..Default::default()
6297 });
6298
6299 assert_eq!(layout.width, 100.0);
6300 assert_eq!(layout.height, 100.0);
6301 assert_eq!(layout.x, 0.0);
6302 assert_eq!(layout.y, 0.0);
6303
6304 assert_eq!(layout.children[0].width, 10.0);
6305 assert_eq!(layout.children[0].height, 100.0);
6306 assert_eq!(layout.children[0].x, 0.0);
6307 assert_eq!(layout.children[0].y, 0.0);
6308
6309 assert_eq!(layout.children[1].width, 10.0);
6310 assert_eq!(layout.children[1].height, 100.0);
6311 assert_eq!(layout.children[1].x, 45.0);
6312 assert_eq!(layout.children[1].y, 0.0);
6313
6314 assert_eq!(layout.children[2].width, 10.0);
6315 assert_eq!(layout.children[2].height, 100.0);
6316 assert_eq!(layout.children[2].x, 90.0);
6317 assert_eq!(layout.children[2].y, 0.0);
6318 }
6319
6320 #[test]
6321 fn align_self_baseline() {
6322 let layout = stretch::compute(&stretch::style::Node {
6323 width: stretch::style::Dimension::Points(100.0),
6324 height: stretch::style::Dimension::Points(100.0),
6325 children: vec![
6326 stretch::style::Node {
6327 align_self: stretch::style::AlignSelf::Baseline,
6328 width: stretch::style::Dimension::Points(50.0),
6329 height: stretch::style::Dimension::Points(50.0),
6330 ..Default::default()
6331 },
6332 stretch::style::Node {
6333 align_self: stretch::style::AlignSelf::Baseline,
6334 width: stretch::style::Dimension::Points(50.0),
6335 height: stretch::style::Dimension::Points(20.0),
6336 children: vec![stretch::style::Node {
6337 width: stretch::style::Dimension::Points(50.0),
6338 height: stretch::style::Dimension::Points(10.0),
6339 ..Default::default()
6340 }],
6341 ..Default::default()
6342 },
6343 ],
6344 ..Default::default()
6345 });
6346
6347 assert_eq!(layout.width, 100.0);
6348 assert_eq!(layout.height, 100.0);
6349 assert_eq!(layout.x, 0.0);
6350 assert_eq!(layout.y, 0.0);
6351
6352 assert_eq!(layout.children[0].width, 50.0);
6353 assert_eq!(layout.children[0].height, 50.0);
6354 assert_eq!(layout.children[0].x, 0.0);
6355 assert_eq!(layout.children[0].y, 0.0);
6356
6357 assert_eq!(layout.children[1].width, 50.0);
6358 assert_eq!(layout.children[1].height, 20.0);
6359 assert_eq!(layout.children[1].x, 50.0);
6360 assert_eq!(layout.children[1].y, 40.0);
6361
6362 assert_eq!(layout.children[1].children[0].width, 50.0);
6363 assert_eq!(layout.children[1].children[0].height, 10.0);
6364 assert_eq!(layout.children[1].children[0].x, 0.0);
6365 assert_eq!(layout.children[1].children[0].y, 0.0);
6366 }
6367
6368 #[test]
6369 fn rounding_fractial_input_1() {
6370 let layout = stretch::compute(&stretch::style::Node {
6371 flex_direction: stretch::style::FlexDirection::Column,
6372 width: stretch::style::Dimension::Points(100.0),
6373 height: stretch::style::Dimension::Points(113.4),
6374 children: vec![
6375 stretch::style::Node {
6376 flex_grow: 1.0,
6377 flex_basis: stretch::style::Dimension::Points(50.0),
6378 height: stretch::style::Dimension::Points(20.0),
6379 ..Default::default()
6380 },
6381 stretch::style::Node {
6382 flex_grow: 1.0,
6383 height: stretch::style::Dimension::Points(10.0),
6384 ..Default::default()
6385 },
6386 stretch::style::Node {
6387 flex_grow: 1.0,
6388 height: stretch::style::Dimension::Points(10.0),
6389 ..Default::default()
6390 },
6391 ],
6392 ..Default::default()
6393 });
6394
6395 assert_eq!(layout.width, 100.0);
6396 assert_eq!(layout.height, 113.0);
6397 assert_eq!(layout.x, 0.0);
6398 assert_eq!(layout.y, 0.0);
6399
6400 assert_eq!(layout.children[0].width, 100.0);
6401 assert_eq!(layout.children[0].height, 64.0);
6402 assert_eq!(layout.children[0].x, 0.0);
6403 assert_eq!(layout.children[0].y, 0.0);
6404
6405 assert_eq!(layout.children[1].width, 100.0);
6406 assert_eq!(layout.children[1].height, 25.0);
6407 assert_eq!(layout.children[1].x, 0.0);
6408 assert_eq!(layout.children[1].y, 64.0);
6409
6410 assert_eq!(layout.children[2].width, 100.0);
6411 assert_eq!(layout.children[2].height, 24.0);
6412 assert_eq!(layout.children[2].x, 0.0);
6413 assert_eq!(layout.children[2].y, 89.0);
6414 }
6415
6416 #[test]
6417 fn absolute_layout_align_items_center() {
6418 let layout = stretch::compute(&stretch::style::Node {
6419 align_items: stretch::style::AlignItems::Center,
6420 width: stretch::style::Dimension::Points(110.0),
6421 height: stretch::style::Dimension::Points(100.0),
6422 children: vec![stretch::style::Node {
6423 position: stretch::style::Position::Absolute,
6424 width: stretch::style::Dimension::Points(60.0),
6425 height: stretch::style::Dimension::Points(40.0),
6426 ..Default::default()
6427 }],
6428 ..Default::default()
6429 });
6430
6431 assert_eq!(layout.width, 110.0);
6432 assert_eq!(layout.height, 100.0);
6433 assert_eq!(layout.x, 0.0);
6434 assert_eq!(layout.y, 0.0);
6435
6436 assert_eq!(layout.children[0].width, 60.0);
6437 assert_eq!(layout.children[0].height, 40.0);
6438 assert_eq!(layout.children[0].x, 0.0);
6439 assert_eq!(layout.children[0].y, 30.0);
6440 }
6441
6442 #[test]
6443 fn percentage_flex_basis_cross_min_width() {
6444 let layout = stretch::compute(&stretch::style::Node {
6445 flex_direction: stretch::style::FlexDirection::Column,
6446 width: stretch::style::Dimension::Points(200.0),
6447 height: stretch::style::Dimension::Points(200.0),
6448 children: vec![
6449 stretch::style::Node {
6450 flex_grow: 1.0,
6451 flex_basis: stretch::style::Dimension::Percent(0.1),
6452 min_width: stretch::style::Dimension::Percent(0.6),
6453 ..Default::default()
6454 },
6455 stretch::style::Node {
6456 flex_grow: 4.0,
6457 flex_basis: stretch::style::Dimension::Percent(0.2),
6458 min_width: stretch::style::Dimension::Percent(0.2),
6459 ..Default::default()
6460 },
6461 ],
6462 ..Default::default()
6463 });
6464
6465 assert_eq!(layout.width, 200.0);
6466 assert_eq!(layout.height, 200.0);
6467 assert_eq!(layout.x, 0.0);
6468 assert_eq!(layout.y, 0.0);
6469
6470 assert_eq!(layout.children[0].width, 200.0);
6471 assert_eq!(layout.children[0].height, 50.0);
6472 assert_eq!(layout.children[0].x, 0.0);
6473 assert_eq!(layout.children[0].y, 0.0);
6474
6475 assert_eq!(layout.children[1].width, 200.0);
6476 assert_eq!(layout.children[1].height, 150.0);
6477 assert_eq!(layout.children[1].x, 0.0);
6478 assert_eq!(layout.children[1].y, 50.0);
6479 }
6480
6481 #[test]
6482 fn margin_auto_left_and_right_column_and_center() {
6483 let layout = stretch::compute(&stretch::style::Node {
6484 align_items: stretch::style::AlignItems::Center,
6485 width: stretch::style::Dimension::Points(200.0),
6486 height: stretch::style::Dimension::Points(200.0),
6487 children: vec![
6488 stretch::style::Node {
6489 width: stretch::style::Dimension::Points(50.0),
6490 height: stretch::style::Dimension::Points(50.0),
6491 margin: stretch::style::Edges {
6492 start: stretch::style::Dimension::Auto,
6493 end: stretch::style::Dimension::Auto,
6494 ..Default::default()
6495 },
6496 ..Default::default()
6497 },
6498 stretch::style::Node {
6499 width: stretch::style::Dimension::Points(50.0),
6500 height: stretch::style::Dimension::Points(50.0),
6501 ..Default::default()
6502 },
6503 ],
6504 ..Default::default()
6505 });
6506
6507 assert_eq!(layout.width, 200.0);
6508 assert_eq!(layout.height, 200.0);
6509 assert_eq!(layout.x, 0.0);
6510 assert_eq!(layout.y, 0.0);
6511
6512 assert_eq!(layout.children[0].width, 50.0);
6513 assert_eq!(layout.children[0].height, 50.0);
6514 assert_eq!(layout.children[0].x, 50.0);
6515 assert_eq!(layout.children[0].y, 75.0);
6516
6517 assert_eq!(layout.children[1].width, 50.0);
6518 assert_eq!(layout.children[1].height, 50.0);
6519 assert_eq!(layout.children[1].x, 150.0);
6520 assert_eq!(layout.children[1].y, 75.0);
6521 }
6522
6523 #[test]
6524 fn flex_grow_less_than_factor_one() {
6525 let layout = stretch::compute(&stretch::style::Node {
6526 width: stretch::style::Dimension::Points(500.0),
6527 height: stretch::style::Dimension::Points(200.0),
6528 children: vec![
6529 stretch::style::Node {
6530 flex_grow: 0.2,
6531 flex_shrink: 0.0,
6532 flex_basis: stretch::style::Dimension::Points(40.0),
6533 ..Default::default()
6534 },
6535 stretch::style::Node { flex_grow: 0.2, flex_shrink: 0.0, ..Default::default() },
6536 stretch::style::Node { flex_grow: 0.4, flex_shrink: 0.0, ..Default::default() },
6537 ],
6538 ..Default::default()
6539 });
6540
6541 assert_eq!(layout.width, 500.0);
6542 assert_eq!(layout.height, 200.0);
6543 assert_eq!(layout.x, 0.0);
6544 assert_eq!(layout.y, 0.0);
6545
6546 assert_eq!(layout.children[0].width, 132.0);
6547 assert_eq!(layout.children[0].height, 200.0);
6548 assert_eq!(layout.children[0].x, 0.0);
6549 assert_eq!(layout.children[0].y, 0.0);
6550
6551 assert_eq!(layout.children[1].width, 92.0);
6552 assert_eq!(layout.children[1].height, 200.0);
6553 assert_eq!(layout.children[1].x, 132.0);
6554 assert_eq!(layout.children[1].y, 0.0);
6555
6556 assert_eq!(layout.children[2].width, 184.0);
6557 assert_eq!(layout.children[2].height, 200.0);
6558 assert_eq!(layout.children[2].x, 224.0);
6559 assert_eq!(layout.children[2].y, 0.0);
6560 }
6561
6562 #[test]
6563 fn flex_grow_within_constrained_min_max_column() {
6564 let layout = stretch::compute(&stretch::style::Node {
6565 min_height: stretch::style::Dimension::Points(100.0),
6566 children: vec![
6567 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6568 stretch::style::Node { height: stretch::style::Dimension::Points(50.0), ..Default::default() },
6569 ],
6570 ..Default::default()
6571 });
6572
6573 assert_eq!(layout.width, 0.0);
6574 assert_eq!(layout.height, 100.0);
6575 assert_eq!(layout.x, 0.0);
6576 assert_eq!(layout.y, 0.0);
6577
6578 assert_eq!(layout.children[0].width, 0.0);
6579 assert_eq!(layout.children[0].height, 100.0);
6580 assert_eq!(layout.children[0].x, 0.0);
6581 assert_eq!(layout.children[0].y, 0.0);
6582
6583 assert_eq!(layout.children[1].width, 0.0);
6584 assert_eq!(layout.children[1].height, 50.0);
6585 assert_eq!(layout.children[1].x, 0.0);
6586 assert_eq!(layout.children[1].y, 0.0);
6587 }
6588
6589 #[test]
6590 fn percentage_flex_basis_cross_max_height() {
6591 let layout = stretch::compute(&stretch::style::Node {
6592 flex_direction: stretch::style::FlexDirection::Column,
6593 width: stretch::style::Dimension::Points(200.0),
6594 height: stretch::style::Dimension::Points(200.0),
6595 children: vec![
6596 stretch::style::Node {
6597 flex_grow: 1.0,
6598 flex_basis: stretch::style::Dimension::Percent(0.1),
6599 max_height: stretch::style::Dimension::Percent(0.6),
6600 ..Default::default()
6601 },
6602 stretch::style::Node {
6603 flex_grow: 4.0,
6604 flex_basis: stretch::style::Dimension::Percent(0.1),
6605 max_height: stretch::style::Dimension::Percent(0.2),
6606 ..Default::default()
6607 },
6608 ],
6609 ..Default::default()
6610 });
6611
6612 assert_eq!(layout.width, 200.0);
6613 assert_eq!(layout.height, 200.0);
6614 assert_eq!(layout.x, 0.0);
6615 assert_eq!(layout.y, 0.0);
6616
6617 assert_eq!(layout.children[0].width, 200.0);
6618 assert_eq!(layout.children[0].height, 120.0);
6619 assert_eq!(layout.children[0].x, 0.0);
6620 assert_eq!(layout.children[0].y, 0.0);
6621
6622 assert_eq!(layout.children[1].width, 200.0);
6623 assert_eq!(layout.children[1].height, 40.0);
6624 assert_eq!(layout.children[1].x, 0.0);
6625 assert_eq!(layout.children[1].y, 120.0);
6626 }
6627
6628 #[test]
6629 fn justify_content_column_flex_end() {
6630 let layout = stretch::compute(&stretch::style::Node {
6631 flex_direction: stretch::style::FlexDirection::Column,
6632 justify_content: stretch::style::JustifyContent::FlexEnd,
6633 width: stretch::style::Dimension::Points(100.0),
6634 height: stretch::style::Dimension::Points(100.0),
6635 children: vec![
6636 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
6637 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
6638 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
6639 ],
6640 ..Default::default()
6641 });
6642
6643 assert_eq!(layout.width, 100.0);
6644 assert_eq!(layout.height, 100.0);
6645 assert_eq!(layout.x, 0.0);
6646 assert_eq!(layout.y, 0.0);
6647
6648 assert_eq!(layout.children[0].width, 100.0);
6649 assert_eq!(layout.children[0].height, 10.0);
6650 assert_eq!(layout.children[0].x, 0.0);
6651 assert_eq!(layout.children[0].y, 70.0);
6652
6653 assert_eq!(layout.children[1].width, 100.0);
6654 assert_eq!(layout.children[1].height, 10.0);
6655 assert_eq!(layout.children[1].x, 0.0);
6656 assert_eq!(layout.children[1].y, 80.0);
6657
6658 assert_eq!(layout.children[2].width, 100.0);
6659 assert_eq!(layout.children[2].height, 10.0);
6660 assert_eq!(layout.children[2].x, 0.0);
6661 assert_eq!(layout.children[2].y, 90.0);
6662 }
6663
6664 #[test]
6665 fn rounding_flex_basis_flex_grow_row_width_of_100() {
6666 let layout = stretch::compute(&stretch::style::Node {
6667 width: stretch::style::Dimension::Points(100.0),
6668 height: stretch::style::Dimension::Points(100.0),
6669 children: vec![
6670 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6671 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6672 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6673 ],
6674 ..Default::default()
6675 });
6676
6677 assert_eq!(layout.width, 100.0);
6678 assert_eq!(layout.height, 100.0);
6679 assert_eq!(layout.x, 0.0);
6680 assert_eq!(layout.y, 0.0);
6681
6682 assert_eq!(layout.children[0].width, 33.0);
6683 assert_eq!(layout.children[0].height, 100.0);
6684 assert_eq!(layout.children[0].x, 0.0);
6685 assert_eq!(layout.children[0].y, 0.0);
6686
6687 assert_eq!(layout.children[1].width, 34.0);
6688 assert_eq!(layout.children[1].height, 100.0);
6689 assert_eq!(layout.children[1].x, 33.0);
6690 assert_eq!(layout.children[1].y, 0.0);
6691
6692 assert_eq!(layout.children[2].width, 33.0);
6693 assert_eq!(layout.children[2].height, 100.0);
6694 assert_eq!(layout.children[2].x, 67.0);
6695 assert_eq!(layout.children[2].y, 0.0);
6696 }
6697
6698 #[test]
6699 fn wrap_reverse_row_align_content_flex_start() {
6700 let layout = stretch::compute(&stretch::style::Node {
6701 align_content: stretch::style::AlignContent::FlexStart,
6702 width: stretch::style::Dimension::Points(100.0),
6703 children: vec![
6704 stretch::style::Node {
6705 width: stretch::style::Dimension::Points(30.0),
6706 height: stretch::style::Dimension::Points(10.0),
6707 ..Default::default()
6708 },
6709 stretch::style::Node {
6710 width: stretch::style::Dimension::Points(30.0),
6711 height: stretch::style::Dimension::Points(20.0),
6712 ..Default::default()
6713 },
6714 stretch::style::Node {
6715 width: stretch::style::Dimension::Points(30.0),
6716 height: stretch::style::Dimension::Points(30.0),
6717 ..Default::default()
6718 },
6719 stretch::style::Node {
6720 width: stretch::style::Dimension::Points(30.0),
6721 height: stretch::style::Dimension::Points(40.0),
6722 ..Default::default()
6723 },
6724 stretch::style::Node {
6725 width: stretch::style::Dimension::Points(30.0),
6726 height: stretch::style::Dimension::Points(50.0),
6727 ..Default::default()
6728 },
6729 ],
6730 ..Default::default()
6731 });
6732
6733 assert_eq!(layout.width, 100.0);
6734 assert_eq!(layout.height, 80.0);
6735 assert_eq!(layout.x, 0.0);
6736 assert_eq!(layout.y, 0.0);
6737
6738 assert_eq!(layout.children[0].width, 30.0);
6739 assert_eq!(layout.children[0].height, 10.0);
6740 assert_eq!(layout.children[0].x, 0.0);
6741 assert_eq!(layout.children[0].y, 70.0);
6742
6743 assert_eq!(layout.children[1].width, 30.0);
6744 assert_eq!(layout.children[1].height, 20.0);
6745 assert_eq!(layout.children[1].x, 30.0);
6746 assert_eq!(layout.children[1].y, 60.0);
6747
6748 assert_eq!(layout.children[2].width, 30.0);
6749 assert_eq!(layout.children[2].height, 30.0);
6750 assert_eq!(layout.children[2].x, 60.0);
6751 assert_eq!(layout.children[2].y, 50.0);
6752
6753 assert_eq!(layout.children[3].width, 30.0);
6754 assert_eq!(layout.children[3].height, 40.0);
6755 assert_eq!(layout.children[3].x, 0.0);
6756 assert_eq!(layout.children[3].y, 10.0);
6757
6758 assert_eq!(layout.children[4].width, 30.0);
6759 assert_eq!(layout.children[4].height, 50.0);
6760 assert_eq!(layout.children[4].x, 30.0);
6761 assert_eq!(layout.children[4].y, 0.0);
6762 }
6763
6764 #[test]
6765 fn flex_shrink_flex_grow_child_flex_shrink_other_child() {
6766 let layout = stretch::compute(&stretch::style::Node {
6767 width: stretch::style::Dimension::Points(500.0),
6768 height: stretch::style::Dimension::Points(500.0),
6769 children: vec![
6770 stretch::style::Node {
6771 flex_grow: 0.0,
6772 flex_shrink: 1.0,
6773 width: stretch::style::Dimension::Points(500.0),
6774 height: stretch::style::Dimension::Points(100.0),
6775 ..Default::default()
6776 },
6777 stretch::style::Node {
6778 flex_grow: 1.0,
6779 flex_shrink: 1.0,
6780 width: stretch::style::Dimension::Points(500.0),
6781 height: stretch::style::Dimension::Points(100.0),
6782 ..Default::default()
6783 },
6784 ],
6785 ..Default::default()
6786 });
6787
6788 assert_eq!(layout.width, 500.0);
6789 assert_eq!(layout.height, 500.0);
6790 assert_eq!(layout.x, 0.0);
6791 assert_eq!(layout.y, 0.0);
6792
6793 assert_eq!(layout.children[0].width, 250.0);
6794 assert_eq!(layout.children[0].height, 100.0);
6795 assert_eq!(layout.children[0].x, 0.0);
6796 assert_eq!(layout.children[0].y, 0.0);
6797
6798 assert_eq!(layout.children[1].width, 250.0);
6799 assert_eq!(layout.children[1].height, 100.0);
6800 assert_eq!(layout.children[1].x, 250.0);
6801 assert_eq!(layout.children[1].y, 0.0);
6802 }
6803
6804 #[test]
6805 fn align_items_flex_end() {
6806 let layout = stretch::compute(&stretch::style::Node {
6807 align_items: stretch::style::AlignItems::FlexEnd,
6808 width: stretch::style::Dimension::Points(100.0),
6809 height: stretch::style::Dimension::Points(100.0),
6810 children: vec![stretch::style::Node {
6811 width: stretch::style::Dimension::Points(10.0),
6812 height: stretch::style::Dimension::Points(10.0),
6813 ..Default::default()
6814 }],
6815 ..Default::default()
6816 });
6817
6818 assert_eq!(layout.width, 100.0);
6819 assert_eq!(layout.height, 100.0);
6820 assert_eq!(layout.x, 0.0);
6821 assert_eq!(layout.y, 0.0);
6822
6823 assert_eq!(layout.children[0].width, 10.0);
6824 assert_eq!(layout.children[0].height, 10.0);
6825 assert_eq!(layout.children[0].x, 0.0);
6826 assert_eq!(layout.children[0].y, 90.0);
6827 }
6828
6829 #[test]
6830 fn absolute_layout_width_height_start_top() {
6831 let layout = stretch::compute(&stretch::style::Node {
6832 width: stretch::style::Dimension::Points(100.0),
6833 height: stretch::style::Dimension::Points(100.0),
6834 children: vec![stretch::style::Node {
6835 position: stretch::style::Position::Absolute,
6836 width: stretch::style::Dimension::Points(10.0),
6837 height: stretch::style::Dimension::Points(10.0),
6838 start: stretch::style::Dimension::Points(10.0),
6839 top: stretch::style::Dimension::Points(10.0),
6840 ..Default::default()
6841 }],
6842 ..Default::default()
6843 });
6844
6845 assert_eq!(layout.width, 100.0);
6846 assert_eq!(layout.height, 100.0);
6847 assert_eq!(layout.x, 0.0);
6848 assert_eq!(layout.y, 0.0);
6849
6850 assert_eq!(layout.children[0].width, 10.0);
6851 assert_eq!(layout.children[0].height, 10.0);
6852 assert_eq!(layout.children[0].x, 10.0);
6853 assert_eq!(layout.children[0].y, 10.0);
6854 }
6855
6856 #[test]
6857 fn rounding_flex_basis_flex_shrink_row() {
6858 let layout = stretch::compute(&stretch::style::Node {
6859 width: stretch::style::Dimension::Points(101.0),
6860 height: stretch::style::Dimension::Points(100.0),
6861 children: vec![
6862 stretch::style::Node {
6863 flex_shrink: 1.0,
6864 flex_basis: stretch::style::Dimension::Points(100.0),
6865 ..Default::default()
6866 },
6867 stretch::style::Node { flex_basis: stretch::style::Dimension::Points(25.0), ..Default::default() },
6868 stretch::style::Node { flex_basis: stretch::style::Dimension::Points(25.0), ..Default::default() },
6869 ],
6870 ..Default::default()
6871 });
6872
6873 assert_eq!(layout.width, 101.0);
6874 assert_eq!(layout.height, 100.0);
6875 assert_eq!(layout.x, 0.0);
6876 assert_eq!(layout.y, 0.0);
6877
6878 assert_eq!(layout.children[0].width, 67.0);
6879 assert_eq!(layout.children[0].height, 100.0);
6880 assert_eq!(layout.children[0].x, 0.0);
6881 assert_eq!(layout.children[0].y, 0.0);
6882
6883 assert_eq!(layout.children[1].width, 17.0);
6884 assert_eq!(layout.children[1].height, 100.0);
6885 assert_eq!(layout.children[1].x, 67.0);
6886 assert_eq!(layout.children[1].y, 0.0);
6887
6888 assert_eq!(layout.children[2].width, 17.0);
6889 assert_eq!(layout.children[2].height, 100.0);
6890 assert_eq!(layout.children[2].x, 84.0);
6891 assert_eq!(layout.children[2].y, 0.0);
6892 }
6893
6894 #[test]
6895 fn border_center_child() {
6896 let layout = stretch::compute(&stretch::style::Node {
6897 align_items: stretch::style::AlignItems::Center,
6898 justify_content: stretch::style::JustifyContent::Center,
6899 width: stretch::style::Dimension::Points(100.0),
6900 height: stretch::style::Dimension::Points(100.0),
6901 border: stretch::style::Edges {
6902 top: stretch::style::Dimension::Points(10.0),
6903 bottom: stretch::style::Dimension::Points(20.0),
6904 ..Default::default()
6905 },
6906 children: vec![stretch::style::Node {
6907 width: stretch::style::Dimension::Points(10.0),
6908 height: stretch::style::Dimension::Points(10.0),
6909 ..Default::default()
6910 }],
6911 ..Default::default()
6912 });
6913
6914 assert_eq!(layout.width, 100.0);
6915 assert_eq!(layout.height, 100.0);
6916 assert_eq!(layout.x, 0.0);
6917 assert_eq!(layout.y, 0.0);
6918
6919 assert_eq!(layout.children[0].width, 10.0);
6920 assert_eq!(layout.children[0].height, 10.0);
6921 assert_eq!(layout.children[0].x, 45.0);
6922 assert_eq!(layout.children[0].y, 40.0);
6923 }
6924
6925 #[test]
6926 fn align_items_flex_start() {
6927 let layout = stretch::compute(&stretch::style::Node {
6928 align_items: stretch::style::AlignItems::FlexStart,
6929 width: stretch::style::Dimension::Points(100.0),
6930 height: stretch::style::Dimension::Points(100.0),
6931 children: vec![stretch::style::Node {
6932 width: stretch::style::Dimension::Points(10.0),
6933 height: stretch::style::Dimension::Points(10.0),
6934 ..Default::default()
6935 }],
6936 ..Default::default()
6937 });
6938
6939 assert_eq!(layout.width, 100.0);
6940 assert_eq!(layout.height, 100.0);
6941 assert_eq!(layout.x, 0.0);
6942 assert_eq!(layout.y, 0.0);
6943
6944 assert_eq!(layout.children[0].width, 10.0);
6945 assert_eq!(layout.children[0].height, 10.0);
6946 assert_eq!(layout.children[0].x, 0.0);
6947 assert_eq!(layout.children[0].y, 0.0);
6948 }
6949
6950 #[test]
6951 fn border_no_child() {
6952 let layout = stretch::compute(&stretch::style::Node {
6953 border: stretch::style::Edges {
6954 start: stretch::style::Dimension::Points(10.0),
6955 end: stretch::style::Dimension::Points(10.0),
6956 top: stretch::style::Dimension::Points(10.0),
6957 bottom: stretch::style::Dimension::Points(10.0),
6958 ..Default::default()
6959 },
6960 ..Default::default()
6961 });
6962
6963 assert_eq!(layout.width, 20.0);
6964 assert_eq!(layout.height, 20.0);
6965 assert_eq!(layout.x, 0.0);
6966 assert_eq!(layout.y, 0.0);
6967 }
6968
6969 #[test]
6970 fn rounding_flex_basis_flex_grow_row_prime_number_width() {
6971 let layout = stretch::compute(&stretch::style::Node {
6972 width: stretch::style::Dimension::Points(113.0),
6973 height: stretch::style::Dimension::Points(100.0),
6974 children: vec![
6975 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6976 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6977 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6978 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6979 stretch::style::Node { flex_grow: 1.0, ..Default::default() },
6980 ],
6981 ..Default::default()
6982 });
6983
6984 assert_eq!(layout.width, 113.0);
6985 assert_eq!(layout.height, 100.0);
6986 assert_eq!(layout.x, 0.0);
6987 assert_eq!(layout.y, 0.0);
6988
6989 assert_eq!(layout.children[0].width, 23.0);
6990 assert_eq!(layout.children[0].height, 100.0);
6991 assert_eq!(layout.children[0].x, 0.0);
6992 assert_eq!(layout.children[0].y, 0.0);
6993
6994 assert_eq!(layout.children[1].width, 22.0);
6995 assert_eq!(layout.children[1].height, 100.0);
6996 assert_eq!(layout.children[1].x, 23.0);
6997 assert_eq!(layout.children[1].y, 0.0);
6998
6999 assert_eq!(layout.children[2].width, 23.0);
7000 assert_eq!(layout.children[2].height, 100.0);
7001 assert_eq!(layout.children[2].x, 45.0);
7002 assert_eq!(layout.children[2].y, 0.0);
7003
7004 assert_eq!(layout.children[3].width, 22.0);
7005 assert_eq!(layout.children[3].height, 100.0);
7006 assert_eq!(layout.children[3].x, 68.0);
7007 assert_eq!(layout.children[3].y, 0.0);
7008
7009 assert_eq!(layout.children[4].width, 23.0);
7010 assert_eq!(layout.children[4].height, 100.0);
7011 assert_eq!(layout.children[4].x, 90.0);
7012 assert_eq!(layout.children[4].y, 0.0);
7013 }
7014
7015 #[test]
7016 fn align_items_center_with_child_top() {
7017 let layout = stretch::compute(&stretch::style::Node {
7018 align_items: stretch::style::AlignItems::Center,
7019 width: stretch::style::Dimension::Points(100.0),
7020 height: stretch::style::Dimension::Points(100.0),
7021 children: vec![stretch::style::Node {
7022 width: stretch::style::Dimension::Points(10.0),
7023 height: stretch::style::Dimension::Points(10.0),
7024 top: stretch::style::Dimension::Points(10.0),
7025 ..Default::default()
7026 }],
7027 ..Default::default()
7028 });
7029
7030 assert_eq!(layout.width, 100.0);
7031 assert_eq!(layout.height, 100.0);
7032 assert_eq!(layout.x, 0.0);
7033 assert_eq!(layout.y, 0.0);
7034
7035 assert_eq!(layout.children[0].width, 10.0);
7036 assert_eq!(layout.children[0].height, 10.0);
7037 assert_eq!(layout.children[0].x, 0.0);
7038 assert_eq!(layout.children[0].y, 55.0);
7039 }
7040
7041 #[test]
7042 fn percentage_width_height_undefined_parent_size() {
7043 let layout = stretch::compute(&stretch::style::Node {
7044 flex_direction: stretch::style::FlexDirection::Column,
7045 children: vec![stretch::style::Node {
7046 width: stretch::style::Dimension::Percent(0.5),
7047 height: stretch::style::Dimension::Percent(0.5),
7048 ..Default::default()
7049 }],
7050 ..Default::default()
7051 });
7052
7053 assert_eq!(layout.width, 0.0);
7054 assert_eq!(layout.height, 0.0);
7055 assert_eq!(layout.x, 0.0);
7056 assert_eq!(layout.y, 0.0);
7057
7058 assert_eq!(layout.children[0].width, 0.0);
7059 assert_eq!(layout.children[0].height, 0.0);
7060 assert_eq!(layout.children[0].x, 0.0);
7061 assert_eq!(layout.children[0].y, 0.0);
7062 }
7063
7064 #[test]
7065 fn flex_grow_within_max_width() {
7066 let layout = stretch::compute(&stretch::style::Node {
7067 flex_direction: stretch::style::FlexDirection::Column,
7068 width: stretch::style::Dimension::Points(200.0),
7069 height: stretch::style::Dimension::Points(100.0),
7070 children: vec![stretch::style::Node {
7071 max_width: stretch::style::Dimension::Points(100.0),
7072 children: vec![stretch::style::Node {
7073 flex_grow: 1.0,
7074 height: stretch::style::Dimension::Points(20.0),
7075 ..Default::default()
7076 }],
7077 ..Default::default()
7078 }],
7079 ..Default::default()
7080 });
7081
7082 assert_eq!(layout.width, 200.0);
7083 assert_eq!(layout.height, 100.0);
7084 assert_eq!(layout.x, 0.0);
7085 assert_eq!(layout.y, 0.0);
7086
7087 assert_eq!(layout.children[0].width, 100.0);
7088 assert_eq!(layout.children[0].height, 20.0);
7089 assert_eq!(layout.children[0].x, 0.0);
7090 assert_eq!(layout.children[0].y, 0.0);
7091
7092 assert_eq!(layout.children[0].children[0].width, 100.0);
7093 assert_eq!(layout.children[0].children[0].height, 20.0);
7094 assert_eq!(layout.children[0].children[0].x, 0.0);
7095 assert_eq!(layout.children[0].children[0].y, 0.0);
7096 }
7097
7098 #[test]
7099 fn flex_grow_height_maximized() {
7100 let layout = stretch::compute(&stretch::style::Node {
7101 flex_direction: stretch::style::FlexDirection::Column,
7102 width: stretch::style::Dimension::Points(100.0),
7103 height: stretch::style::Dimension::Points(500.0),
7104 children: vec![stretch::style::Node {
7105 flex_direction: stretch::style::FlexDirection::Column,
7106 flex_grow: 1.0,
7107 min_height: stretch::style::Dimension::Points(100.0),
7108 max_height: stretch::style::Dimension::Points(500.0),
7109 children: vec![
7110 stretch::style::Node {
7111 flex_grow: 1.0,
7112 flex_basis: stretch::style::Dimension::Points(200.0),
7113 ..Default::default()
7114 },
7115 stretch::style::Node { height: stretch::style::Dimension::Points(100.0), ..Default::default() },
7116 ],
7117 ..Default::default()
7118 }],
7119 ..Default::default()
7120 });
7121
7122 assert_eq!(layout.width, 100.0);
7123 assert_eq!(layout.height, 500.0);
7124 assert_eq!(layout.x, 0.0);
7125 assert_eq!(layout.y, 0.0);
7126
7127 assert_eq!(layout.children[0].width, 100.0);
7128 assert_eq!(layout.children[0].height, 500.0);
7129 assert_eq!(layout.children[0].x, 0.0);
7130 assert_eq!(layout.children[0].y, 0.0);
7131
7132 assert_eq!(layout.children[0].children[0].width, 100.0);
7133 assert_eq!(layout.children[0].children[0].height, 400.0);
7134 assert_eq!(layout.children[0].children[0].x, 0.0);
7135 assert_eq!(layout.children[0].children[0].y, 0.0);
7136
7137 assert_eq!(layout.children[0].children[1].width, 100.0);
7138 assert_eq!(layout.children[0].children[1].height, 100.0);
7139 assert_eq!(layout.children[0].children[1].x, 0.0);
7140 assert_eq!(layout.children[0].children[1].y, 400.0);
7141 }
7142
7143 #[test]
7144 fn max_height_overrides_height() {
7145 let layout = stretch::compute(&stretch::style::Node {
7146 children: vec![stretch::style::Node {
7147 height: stretch::style::Dimension::Points(200.0),
7148 max_height: stretch::style::Dimension::Points(100.0),
7149 ..Default::default()
7150 }],
7151 ..Default::default()
7152 });
7153
7154 assert_eq!(layout.width, 0.0);
7155 assert_eq!(layout.height, 100.0);
7156 assert_eq!(layout.x, 0.0);
7157 assert_eq!(layout.y, 0.0);
7158
7159 assert_eq!(layout.children[0].width, 0.0);
7160 assert_eq!(layout.children[0].height, 100.0);
7161 assert_eq!(layout.children[0].x, 0.0);
7162 assert_eq!(layout.children[0].y, 0.0);
7163 }
7164
7165 #[test]
7166 fn justify_content_min_width_with_padding_child_width_greater_than_parent() {
7167 let layout = stretch::compute(&stretch::style::Node {
7168 flex_direction: stretch::style::FlexDirection::Column,
7169 width: stretch::style::Dimension::Points(1000.0),
7170 height: stretch::style::Dimension::Points(1584.0),
7171 children: vec![stretch::style::Node {
7172 children: vec![stretch::style::Node {
7173 justify_content: stretch::style::JustifyContent::Center,
7174 min_width: stretch::style::Dimension::Points(400.0),
7175 padding: stretch::style::Edges {
7176 start: stretch::style::Dimension::Points(100.0),
7177 end: stretch::style::Dimension::Points(100.0),
7178 ..Default::default()
7179 },
7180 children: vec![stretch::style::Node {
7181 width: stretch::style::Dimension::Points(300.0),
7182 height: stretch::style::Dimension::Points(100.0),
7183 ..Default::default()
7184 }],
7185 ..Default::default()
7186 }],
7187 ..Default::default()
7188 }],
7189 ..Default::default()
7190 });
7191
7192 assert_eq!(layout.width, 1000.0);
7193 assert_eq!(layout.height, 1584.0);
7194 assert_eq!(layout.x, 0.0);
7195 assert_eq!(layout.y, 0.0);
7196
7197 assert_eq!(layout.children[0].width, 1000.0);
7198 assert_eq!(layout.children[0].height, 100.0);
7199 assert_eq!(layout.children[0].x, 0.0);
7200 assert_eq!(layout.children[0].y, 0.0);
7201
7202 assert_eq!(layout.children[0].children[0].width, 500.0);
7203 assert_eq!(layout.children[0].children[0].height, 100.0);
7204 assert_eq!(layout.children[0].children[0].x, 0.0);
7205 assert_eq!(layout.children[0].children[0].y, 0.0);
7206
7207 assert_eq!(layout.children[0].children[0].children[0].width, 300.0);
7208 assert_eq!(layout.children[0].children[0].children[0].height, 100.0);
7209 assert_eq!(layout.children[0].children[0].children[0].x, 100.0);
7210 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
7211 }
7212
7213 #[test]
7214 fn percent_absolute_position() {
7215 let layout = stretch::compute(&stretch::style::Node {
7216 flex_direction: stretch::style::FlexDirection::Column,
7217 width: stretch::style::Dimension::Points(60.0),
7218 height: stretch::style::Dimension::Points(50.0),
7219 children: vec![stretch::style::Node {
7220 position: stretch::style::Position::Absolute,
7221 width: stretch::style::Dimension::Percent(1.0),
7222 height: stretch::style::Dimension::Points(50.0),
7223 start: stretch::style::Dimension::Percent(0.5),
7224 children: vec![
7225 stretch::style::Node { width: stretch::style::Dimension::Percent(1.0), ..Default::default() },
7226 stretch::style::Node { width: stretch::style::Dimension::Percent(1.0), ..Default::default() },
7227 ],
7228 ..Default::default()
7229 }],
7230 ..Default::default()
7231 });
7232
7233 assert_eq!(layout.width, 60.0);
7234 assert_eq!(layout.height, 50.0);
7235 assert_eq!(layout.x, 0.0);
7236 assert_eq!(layout.y, 0.0);
7237
7238 assert_eq!(layout.children[0].width, 60.0);
7239 assert_eq!(layout.children[0].height, 50.0);
7240 assert_eq!(layout.children[0].x, 30.0);
7241 assert_eq!(layout.children[0].y, 0.0);
7242
7243 assert_eq!(layout.children[0].children[0].width, 30.0);
7244 assert_eq!(layout.children[0].children[0].height, 50.0);
7245 assert_eq!(layout.children[0].children[0].x, 0.0);
7246 assert_eq!(layout.children[0].children[0].y, 0.0);
7247
7248 assert_eq!(layout.children[0].children[1].width, 30.0);
7249 assert_eq!(layout.children[0].children[1].height, 50.0);
7250 assert_eq!(layout.children[0].children[1].x, 30.0);
7251 assert_eq!(layout.children[0].children[1].y, 0.0);
7252 }
7253
7254 #[test]
7255 fn flex_root_ignored() {
7256 let layout = stretch::compute(&stretch::style::Node {
7257 flex_direction: stretch::style::FlexDirection::Column,
7258 width: stretch::style::Dimension::Points(100.0),
7259 min_height: stretch::style::Dimension::Points(100.0),
7260 max_height: stretch::style::Dimension::Points(500.0),
7261 children: vec![
7262 stretch::style::Node {
7263 flex_grow: 1.0,
7264 flex_basis: stretch::style::Dimension::Points(200.0),
7265 ..Default::default()
7266 },
7267 stretch::style::Node { height: stretch::style::Dimension::Points(100.0), ..Default::default() },
7268 ],
7269 ..Default::default()
7270 });
7271
7272 assert_eq!(layout.width, 100.0);
7273 assert_eq!(layout.height, 300.0);
7274 assert_eq!(layout.x, 0.0);
7275 assert_eq!(layout.y, 0.0);
7276
7277 assert_eq!(layout.children[0].width, 100.0);
7278 assert_eq!(layout.children[0].height, 200.0);
7279 assert_eq!(layout.children[0].x, 0.0);
7280 assert_eq!(layout.children[0].y, 0.0);
7281
7282 assert_eq!(layout.children[1].width, 100.0);
7283 assert_eq!(layout.children[1].height, 100.0);
7284 assert_eq!(layout.children[1].x, 0.0);
7285 assert_eq!(layout.children[1].y, 200.0);
7286 }
7287
7288 #[test]
7289 fn justify_content_column_space_between() {
7290 let layout = stretch::compute(&stretch::style::Node {
7291 flex_direction: stretch::style::FlexDirection::Column,
7292 justify_content: stretch::style::JustifyContent::SpaceBetween,
7293 width: stretch::style::Dimension::Points(100.0),
7294 height: stretch::style::Dimension::Points(100.0),
7295 children: vec![
7296 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
7297 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
7298 stretch::style::Node { height: stretch::style::Dimension::Points(10.0), ..Default::default() },
7299 ],
7300 ..Default::default()
7301 });
7302
7303 assert_eq!(layout.width, 100.0);
7304 assert_eq!(layout.height, 100.0);
7305 assert_eq!(layout.x, 0.0);
7306 assert_eq!(layout.y, 0.0);
7307
7308 assert_eq!(layout.children[0].width, 100.0);
7309 assert_eq!(layout.children[0].height, 10.0);
7310 assert_eq!(layout.children[0].x, 0.0);
7311 assert_eq!(layout.children[0].y, 0.0);
7312
7313 assert_eq!(layout.children[1].width, 100.0);
7314 assert_eq!(layout.children[1].height, 10.0);
7315 assert_eq!(layout.children[1].x, 0.0);
7316 assert_eq!(layout.children[1].y, 45.0);
7317
7318 assert_eq!(layout.children[2].width, 100.0);
7319 assert_eq!(layout.children[2].height, 10.0);
7320 assert_eq!(layout.children[2].x, 0.0);
7321 assert_eq!(layout.children[2].y, 90.0);
7322 }
7323
7324 #[test]
7325 fn flex_grow_within_constrained_max_width() {
7326 let layout = stretch::compute(&stretch::style::Node {
7327 flex_direction: stretch::style::FlexDirection::Column,
7328 width: stretch::style::Dimension::Points(200.0),
7329 height: stretch::style::Dimension::Points(100.0),
7330 children: vec![stretch::style::Node {
7331 max_width: stretch::style::Dimension::Points(300.0),
7332 children: vec![stretch::style::Node {
7333 flex_grow: 1.0,
7334 height: stretch::style::Dimension::Points(20.0),
7335 ..Default::default()
7336 }],
7337 ..Default::default()
7338 }],
7339 ..Default::default()
7340 });
7341
7342 assert_eq!(layout.width, 200.0);
7343 assert_eq!(layout.height, 100.0);
7344 assert_eq!(layout.x, 0.0);
7345 assert_eq!(layout.y, 0.0);
7346
7347 assert_eq!(layout.children[0].width, 200.0);
7348 assert_eq!(layout.children[0].height, 20.0);
7349 assert_eq!(layout.children[0].x, 0.0);
7350 assert_eq!(layout.children[0].y, 0.0);
7351
7352 assert_eq!(layout.children[0].children[0].width, 200.0);
7353 assert_eq!(layout.children[0].children[0].height, 20.0);
7354 assert_eq!(layout.children[0].children[0].x, 0.0);
7355 assert_eq!(layout.children[0].children[0].y, 0.0);
7356 }
7357
7358 #[test]
7359 fn max_width_overrides_width() {
7360 let layout = stretch::compute(&stretch::style::Node {
7361 children: vec![stretch::style::Node {
7362 width: stretch::style::Dimension::Points(200.0),
7363 max_width: stretch::style::Dimension::Points(100.0),
7364 ..Default::default()
7365 }],
7366 ..Default::default()
7367 });
7368
7369 assert_eq!(layout.width, 100.0);
7370 assert_eq!(layout.height, 0.0);
7371 assert_eq!(layout.x, 0.0);
7372 assert_eq!(layout.y, 0.0);
7373
7374 assert_eq!(layout.children[0].width, 100.0);
7375 assert_eq!(layout.children[0].height, 0.0);
7376 assert_eq!(layout.children[0].x, 0.0);
7377 assert_eq!(layout.children[0].y, 0.0);
7378 }
7379
7380 #[test]
7381 fn percentage_position_bottom_right() {
7382 let layout = stretch::compute(&stretch::style::Node {
7383 width: stretch::style::Dimension::Points(500.0),
7384 height: stretch::style::Dimension::Points(500.0),
7385 children: vec![stretch::style::Node {
7386 width: stretch::style::Dimension::Percent(0.6),
7387 height: stretch::style::Dimension::Percent(0.2),
7388 end: stretch::style::Dimension::Percent(0.2),
7389 bottom: stretch::style::Dimension::Percent(0.1),
7390 ..Default::default()
7391 }],
7392 ..Default::default()
7393 });
7394
7395 assert_eq!(layout.width, 500.0);
7396 assert_eq!(layout.height, 500.0);
7397 assert_eq!(layout.x, 0.0);
7398 assert_eq!(layout.y, 0.0);
7399
7400 assert_eq!(layout.children[0].width, 275.0);
7401 assert_eq!(layout.children[0].height, 75.0);
7402 assert_eq!(layout.children[0].x, -100.0);
7403 assert_eq!(layout.children[0].y, -50.0);
7404 }
7405
7406 #[test]
7407 fn margin_auto_top_stretching_child() {
7408 let layout = stretch::compute(&stretch::style::Node {
7409 align_items: stretch::style::AlignItems::Center,
7410 width: stretch::style::Dimension::Points(200.0),
7411 height: stretch::style::Dimension::Points(200.0),
7412 children: vec![
7413 stretch::style::Node {
7414 flex_grow: 1.0,
7415 flex_shrink: 1.0,
7416 flex_basis: stretch::style::Dimension::Percent(0.0),
7417 margin: stretch::style::Edges { top: stretch::style::Dimension::Auto, ..Default::default() },
7418 ..Default::default()
7419 },
7420 stretch::style::Node {
7421 width: stretch::style::Dimension::Points(50.0),
7422 height: stretch::style::Dimension::Points(50.0),
7423 ..Default::default()
7424 },
7425 ],
7426 ..Default::default()
7427 });
7428
7429 assert_eq!(layout.width, 200.0);
7430 assert_eq!(layout.height, 200.0);
7431 assert_eq!(layout.x, 0.0);
7432 assert_eq!(layout.y, 0.0);
7433
7434 assert_eq!(layout.children[0].width, 150.0);
7435 assert_eq!(layout.children[0].height, 0.0);
7436 assert_eq!(layout.children[0].x, 0.0);
7437 assert_eq!(layout.children[0].y, 200.0);
7438
7439 assert_eq!(layout.children[1].width, 50.0);
7440 assert_eq!(layout.children[1].height, 50.0);
7441 assert_eq!(layout.children[1].x, 150.0);
7442 assert_eq!(layout.children[1].y, 75.0);
7443 }
7444
7445 #[test]
7446 fn justify_content_column_min_height_and_margin() {
7447 let layout = stretch::compute(&stretch::style::Node {
7448 flex_direction: stretch::style::FlexDirection::Column,
7449 justify_content: stretch::style::JustifyContent::Center,
7450 min_height: stretch::style::Dimension::Points(50.0),
7451 margin: stretch::style::Edges { top: stretch::style::Dimension::Points(100.0), ..Default::default() },
7452 children: vec![stretch::style::Node {
7453 width: stretch::style::Dimension::Points(20.0),
7454 height: stretch::style::Dimension::Points(20.0),
7455 ..Default::default()
7456 }],
7457 ..Default::default()
7458 });
7459
7460 assert_eq!(layout.width, 20.0);
7461 assert_eq!(layout.height, 50.0);
7462 assert_eq!(layout.x, 0.0);
7463 assert_eq!(layout.y, 100.0);
7464
7465 assert_eq!(layout.children[0].width, 20.0);
7466 assert_eq!(layout.children[0].height, 20.0);
7467 assert_eq!(layout.children[0].x, 0.0);
7468 assert_eq!(layout.children[0].y, 15.0);
7469 }
7470
7471 #[test]
7472 fn align_flex_start_with_shrinking_children() {
7473 let layout = stretch::compute(&stretch::style::Node {
7474 width: stretch::style::Dimension::Points(500.0),
7475 height: stretch::style::Dimension::Points(500.0),
7476 children: vec![stretch::style::Node {
7477 align_items: stretch::style::AlignItems::FlexStart,
7478 children: vec![stretch::style::Node {
7479 flex_grow: 1.0,
7480 flex_shrink: 1.0,
7481 children: vec![stretch::style::Node { flex_grow: 1.0, flex_shrink: 1.0, ..Default::default() }],
7482 ..Default::default()
7483 }],
7484 ..Default::default()
7485 }],
7486 ..Default::default()
7487 });
7488
7489 assert_eq!(layout.width, 500.0);
7490 assert_eq!(layout.height, 500.0);
7491 assert_eq!(layout.x, 0.0);
7492 assert_eq!(layout.y, 0.0);
7493
7494 assert_eq!(layout.children[0].width, 0.0);
7495 assert_eq!(layout.children[0].height, 500.0);
7496 assert_eq!(layout.children[0].x, 0.0);
7497 assert_eq!(layout.children[0].y, 0.0);
7498
7499 assert_eq!(layout.children[0].children[0].width, 0.0);
7500 assert_eq!(layout.children[0].children[0].height, 0.0);
7501 assert_eq!(layout.children[0].children[0].x, 0.0);
7502 assert_eq!(layout.children[0].children[0].y, 0.0);
7503
7504 assert_eq!(layout.children[0].children[0].children[0].width, 0.0);
7505 assert_eq!(layout.children[0].children[0].children[0].height, 0.0);
7506 assert_eq!(layout.children[0].children[0].children[0].x, 0.0);
7507 assert_eq!(layout.children[0].children[0].children[0].y, 0.0);
7508 }
7509
7510}