use ui_layout::*;
#[test]
fn padding_content_box() {
let mut root = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(15.0),
padding_top: Length::Px(5.0),
padding_bottom: Length::Px(8.0),
..Default::default()
},
..Default::default()
});
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 225.0); assert_eq!(box_model.padding_box.height, 113.0);
assert_eq!(box_model.border_box.width, 225.0);
assert_eq!(box_model.border_box.height, 113.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn border_box_sizing() {
let mut root = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(100.0),
..Default::default()
},
box_sizing: BoxSizing::BorderBox,
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(15.0),
padding_top: Length::Px(5.0),
padding_bottom: Length::Px(8.0),
border_left: Length::Px(2.0),
border_right: Length::Px(3.0),
border_top: Length::Px(1.0),
border_bottom: Length::Px(4.0),
..Default::default()
},
..Default::default()
});
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.width, 200.0);
assert_eq!(box_model.border_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 195.0); assert_eq!(box_model.padding_box.height, 95.0);
assert_eq!(box_model.content_box.width, 170.0); assert_eq!(box_model.content_box.height, 82.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn nested_border_box_sizing() {
let def_style = Style {
spacing: Spacing {
margin_top: Length::Px(10.0),
margin_bottom: Length::Px(10.0),
margin_left: Length::Px(10.0),
margin_right: Length::Px(10.0),
..Default::default()
},
size: SizeStyle {
..Default::default()
},
..Default::default()
};
let mut root = LayoutNode::new(def_style.clone());
fn push_child(parent: &mut LayoutNode, style: Style, max: usize, current: usize) {
if current + 1 < max {
parent.children.push(LayoutNode::new(style.clone()));
push_child(&mut parent.children[0], style.clone(), max, current + 1);
} else {
parent.children.push(LayoutNode::new(Style {
size: SizeStyle {
height: Length::Px(50.0),
..Default::default()
},
..style
}));
}
}
root.children.push(LayoutNode::new(def_style.clone()));
push_child(&mut root.children[0], def_style.clone(), 3, 0);
LayoutEngine::layout(&mut root, 800.0, 600.0);
let child0 = &root.children[0];
match &child0.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.y, 10.0);
assert_eq!(box_model.border_box.width, 780.0);
assert_eq!(box_model.border_box.height, 110.0);
}
_ => panic!("Expected single box model"),
}
let child1 = &child0.children[0];
match &child1.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.y, 10.0);
assert_eq!(box_model.border_box.width, 760.0);
assert_eq!(box_model.border_box.height, 90.0);
}
_ => panic!("Expected single box model"),
}
let child2 = &child1.children[0];
match &child2.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.y, 10.0);
assert_eq!(box_model.border_box.width, 740.0);
assert_eq!(box_model.border_box.height, 70.0);
}
_ => panic!("Expected single box model"),
}
let leaf = &child2.children[0];
match &leaf.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.y, 10.0);
assert_eq!(box_model.border_box.width, 720.0);
assert_eq!(box_model.content_box.height, 50.0);
assert_eq!(box_model.border_box.height, 50.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn margins_affect_positioning() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
margin_left: Length::Px(20.0),
margin_top: Length::Px(10.0),
margin_right: Length::Px(30.0),
margin_bottom: Length::Px(15.0),
..Default::default()
},
..Default::default()
});
let inner = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(300.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![child],
);
let mut root = LayoutNode::with_children(Style::default(), vec![inner]);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.x, 20.0); assert_eq!(box_model.border_box.y, 10.0); assert_eq!(box_model.border_box.width, 100.0);
assert_eq!(box_model.border_box.height, 50.0);
}
_ => panic!("Expected single box model"),
}
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.height, 75.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn min_width_constraint() {
let mut root = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
min_width: Length::Px(150.0),
..Default::default()
},
..Default::default()
});
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 150.0);
assert_eq!(box_model.content_box.height, 50.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn max_width_constraint() {
let mut root = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(50.0),
max_width: Length::Px(150.0),
..Default::default()
},
..Default::default()
});
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 150.0);
assert_eq!(box_model.content_box.height, 50.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn min_max_height_constraints() {
let child1 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(30.0),
min_height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(80.0),
max_height: Length::Px(60.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(300.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![child1, child2],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.height, 50.0);
}
_ => panic!("Expected single box model"),
}
match &root.children[1].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.height, 60.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn percentage_padding() {
let mut root = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Percent(10.0), padding_right: Length::Percent(5.0), padding_top: Length::Percent(10.0), padding_bottom: Length::Percent(5.0), ..Default::default()
},
..Default::default()
});
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 320.0); assert_eq!(box_model.padding_box.height, 220.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn viewport_relative_sizing() {
let inner = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Vw(50.0), height: Length::Vh(25.0), ..Default::default()
},
spacing: Spacing {
margin_left: Length::Vw(5.0), margin_top: Length::Vh(10.0), padding_left: Length::Vw(2.5), padding_top: Length::Vw(2.5), ..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(Style::default(), vec![inner]);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 400.0);
assert_eq!(box_model.content_box.height, 150.0);
assert_eq!(box_model.border_box.x, 40.0);
assert_eq!(box_model.border_box.y, 60.0);
assert_eq!(box_model.padding_box.width, 420.0); assert_eq!(box_model.padding_box.height, 170.0);
assert_eq!(box_model.border_box.width, 420.0);
assert_eq!(box_model.border_box.height, 170.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn complex_spacing_calculation() {
let inner = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(300.0),
height: Length::Px(200.0),
..Default::default()
},
box_sizing: BoxSizing::ContentBox,
spacing: Spacing {
margin_left: Length::Px(5.0),
margin_right: Length::Px(5.0),
margin_top: Length::Px(10.0),
margin_bottom: Length::Px(10.0),
border_left: Length::Px(3.0),
border_right: Length::Px(3.0),
border_top: Length::Px(2.0),
border_bottom: Length::Px(2.0),
padding_left: Length::Px(15.0),
padding_right: Length::Px(20.0),
padding_top: Length::Px(12.0),
padding_bottom: Length::Px(18.0),
},
..Default::default()
});
let mut root = LayoutNode::with_children(Style::default(), vec![inner]);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 300.0);
assert_eq!(box_model.content_box.height, 200.0);
assert_eq!(box_model.padding_box.width, 335.0); assert_eq!(box_model.padding_box.height, 230.0);
assert_eq!(box_model.border_box.width, 341.0); assert_eq!(box_model.border_box.height, 234.0);
assert_eq!(box_model.border_box.x, 5.0);
assert_eq!(box_model.border_box.y, 10.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn flex_with_spacing_constraints() {
let child1 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_grow: 1.0,
..Default::default()
},
spacing: Spacing {
margin_left: Length::Px(5.0),
margin_right: Length::Px(10.0),
padding_left: Length::Px(8.0),
padding_right: Length::Px(12.0),
border_left: Length::Px(2.0),
border_right: Length::Px(2.0),
..Default::default()
},
size: SizeStyle {
min_width: Length::Px(50.0),
max_width: Length::Px(200.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
margin_left: Length::Px(5.0),
margin_right: Length::Px(5.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(400.0),
height: Length::Px(100.0),
..Default::default()
},
column_gap: Length::Px(20.0),
..Default::default()
},
vec![child1, child2],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
let content_width = box_model.content_box.width;
assert!(content_width >= 50.0); assert!(content_width <= 200.0);
let expected_border_width = content_width + 8.0 + 12.0 + 2.0 + 2.0; assert_eq!(box_model.border_box.width, expected_border_width);
}
_ => panic!("Expected single box model"),
}
match &root.children[1].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_single_child() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(150.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_height_single_child() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(75.0),
..Default::default()
},
..Default::default()
});
let inner = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![child],
);
let mut root = LayoutNode::with_children(Style::default(), vec![inner]);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 75.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_multiple_children() {
let child1 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(120.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(80.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child1, child2],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_height_multiple_children() {
let child1 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(60.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(90.0),
..Default::default()
},
..Default::default()
});
let inner = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![child1, child2],
);
let mut root = LayoutNode::with_children(Style::default(), vec![inner]);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 150.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_padding() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(15.0),
padding_top: Length::Px(5.0),
padding_bottom: Length::Px(8.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0 - 10.0 - 15.0);
assert_eq!(box_model.content_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 800.0);
assert_eq!(box_model.padding_box.height, 100.0 + 5.0 + 8.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_height_with_padding() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(60.0),
..Default::default()
},
..Default::default()
});
let inner = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(15.0),
padding_top: Length::Px(5.0),
padding_bottom: Length::Px(8.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
let mut root = LayoutNode::with_children(Style::default(), vec![inner]);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 60.0);
assert_eq!(box_model.padding_box.width, 225.0); assert_eq!(box_model.padding_box.height, 73.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_border() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
border_left: Length::Px(3.0),
border_right: Length::Px(4.0),
border_top: Length::Px(2.0),
border_bottom: Length::Px(2.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0 - 3.0 - 4.0);
assert_eq!(box_model.content_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 800.0 - 3.0 - 4.0);
assert_eq!(box_model.padding_box.height, 100.0);
assert_eq!(box_model.border_box.width, 800.0);
assert_eq!(box_model.border_box.height, 100.0 + 2.0 + 2.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_height_with_border() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(70.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
spacing: Spacing {
border_left: Length::Px(2.0),
border_right: Length::Px(2.0),
border_top: Length::Px(3.0),
border_bottom: Length::Px(5.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 600.0 - 3.0 - 5.0);
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 600.0 - 3.0 - 5.0);
assert_eq!(box_model.border_box.width, 204.0);
assert_eq!(box_model.border_box.height, 600.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_sizing_with_padding_and_border() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(80.0),
height: Length::Px(60.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Auto,
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(10.0),
padding_top: Length::Px(8.0),
padding_bottom: Length::Px(8.0),
border_left: Length::Px(2.0),
border_right: Length::Px(2.0),
border_top: Length::Px(1.0),
border_bottom: Length::Px(1.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0 - 2.0 - 2.0 - 10.0 - 10.0);
assert_eq!(box_model.content_box.height, 600.0 - 1.0 - 1.0 - 8.0 - 8.0);
assert_eq!(box_model.padding_box.width, 800.0 - 2.0 - 2.0);
assert_eq!(box_model.padding_box.height, 600.0 - 1.0 - 1.0);
assert_eq!(box_model.border_box.width, 800.0);
assert_eq!(box_model.border_box.height, 600.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn padding_border_size_verification() {
let mut root = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(150.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(12.0),
padding_right: Length::Px(18.0),
padding_top: Length::Px(10.0),
padding_bottom: Length::Px(15.0),
border_left: Length::Px(2.0),
border_right: Length::Px(3.0),
border_top: Length::Px(1.0),
border_bottom: Length::Px(4.0),
..Default::default()
},
..Default::default()
});
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 200.0);
assert_eq!(box_model.content_box.height, 150.0);
let padding_width = box_model.padding_box.width - box_model.content_box.width;
let padding_height = box_model.padding_box.height - box_model.content_box.height;
assert_eq!(padding_width, 30.0); assert_eq!(padding_height, 25.0);
let border_width = box_model.border_box.width - box_model.padding_box.width;
let border_height = box_model.border_box.height - box_model.padding_box.height;
assert_eq!(border_width, 5.0); assert_eq!(border_height, 5.0);
assert_eq!(box_model.padding_box.width, 230.0); assert_eq!(box_model.padding_box.height, 175.0); assert_eq!(box_model.border_box.width, 235.0); assert_eq!(box_model.border_box.height, 180.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_child_margins() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
margin_left: Length::Px(10.0),
margin_right: Length::Px(15.0),
margin_top: Length::Px(5.0),
margin_bottom: Length::Px(5.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_child_padding() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(15.0),
padding_top: Length::Px(5.0),
padding_bottom: Length::Px(5.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_child_border() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
border_left: Length::Px(2.0),
border_right: Length::Px(3.0),
border_top: Length::Px(1.0),
border_bottom: Length::Px(1.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_multiple_children_different_sizes() {
let child1 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
margin_left: Length::Px(5.0),
margin_right: Length::Px(5.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(80.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
margin_left: Length::Px(10.0),
margin_right: Length::Px(10.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child1, child2],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_parent_padding() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(15.0),
padding_top: Length::Px(5.0),
padding_bottom: Length::Px(5.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0 - 10.0 - 15.0);
assert_eq!(box_model.content_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 800.0);
assert_eq!(box_model.padding_box.height, 110.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_parent_border() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
spacing: Spacing {
border_left: Length::Px(2.0),
border_right: Length::Px(3.0),
border_top: Length::Px(1.0),
border_bottom: Length::Px(1.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0 - 2.0 - 3.0);
assert_eq!(box_model.content_box.height, 100.0);
assert_eq!(box_model.padding_box.width, 800.0 - 2.0 - 3.0);
assert_eq!(box_model.padding_box.height, 100.0);
assert_eq!(box_model.border_box.width, 800.0);
assert_eq!(box_model.border_box.height, 100.0 + 1.0 + 1.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_nested_blocks() {
let grandchild = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(80.0),
height: Length::Px(30.0),
..Default::default()
},
..Default::default()
});
let child = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(50.0),
..Default::default()
},
..Default::default()
},
vec![grandchild],
);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 80.0);
assert_eq!(box_model.content_box.height, 30.0);
}
_ => panic!("Expected single box model for grandchild"),
}
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 50.0);
}
_ => panic!("Expected single box model for child"),
}
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model for root"),
}
}
#[test]
fn auto_width_empty_container() {
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn auto_width_with_border_box_sizing() {
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(50.0),
..Default::default()
},
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(10.0),
border_left: Length::Px(2.0),
border_right: Length::Px(2.0),
..Default::default()
},
box_sizing: BoxSizing::BorderBox,
..Default::default()
});
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Auto,
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
},
vec![child],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 800.0);
assert_eq!(box_model.content_box.height, 100.0);
}
_ => panic!("Expected single box model"),
}
}