use ui_layout::*;
#[test]
fn inline_basic_flow() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 30.0,
height: 20.0,
});
let fragment2 = ItemFragment::Fragment(Fragment {
width: 40.0,
height: 25.0,
});
let fragment3 = ItemFragment::Fragment(Fragment {
width: 35.0,
height: 15.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
..Default::default()
});
inline_node.set_fragments(vec![fragment1, fragment2, fragment3]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
assert_eq!(root.children[0].placements.len(), 3);
for placement in &root.children[0].placements {
assert_eq!(placement.line_index, 0);
}
assert_eq!(root.children[0].placements[0].offset.0, 0.0); assert_eq!(root.children[0].placements[1].offset.0, 30.0); assert_eq!(root.children[0].placements[2].offset.0, 70.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.width, 105.0); assert_eq!(box_model.content_box.height, 25.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn inline_line_wrapping() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 80.0,
height: 20.0,
});
let fragment2 = ItemFragment::Fragment(Fragment {
width: 70.0,
height: 25.0,
});
let fragment3 = ItemFragment::Fragment(Fragment {
width: 60.0,
height: 15.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
..Default::default()
});
inline_node.set_fragments(vec![fragment1, fragment2, fragment3]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(120.0), height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
assert_eq!(root.children[0].placements.len(), 3);
assert_eq!(root.children[0].placements[0].line_index, 0);
assert_eq!(root.children[0].placements[0].offset.0, 0.0);
assert_eq!(root.children[0].placements[1].line_index, 1);
assert_eq!(root.children[0].placements[1].offset.0, 0.0);
assert_eq!(root.children[0].placements[2].line_index, 2);
assert_eq!(root.children[0].placements[2].offset.0, 0.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.height, 60.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn inline_with_line_breaks() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 30.0,
height: 20.0,
});
let line_break = ItemFragment::LineBreak;
let fragment2 = ItemFragment::Fragment(Fragment {
width: 40.0,
height: 25.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
..Default::default()
});
inline_node.set_fragments(vec![fragment1, line_break, fragment2]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
assert_eq!(root.children[0].placements.len(), 3);
assert_eq!(root.children[0].placements[0].line_index, 0);
assert_eq!(root.children[0].placements[0].offset.0, 0.0);
assert_eq!(root.children[0].placements[1].line_index, 1);
assert_eq!(root.children[0].placements[2].line_index, 1);
assert_eq!(root.children[0].placements[2].offset.0, 0.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.height, 45.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn inline_with_margins() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 50.0,
height: 20.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
spacing: Spacing {
margin_left: Length::Px(10.0),
margin_right: Length::Px(15.0),
margin_top: Length::Px(5.0), margin_bottom: Length::Px(8.0), ..Default::default()
},
..Default::default()
});
inline_node.set_fragments(vec![fragment1]);
let inner = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
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, 10.0);
assert_eq!(box_model.border_box.y, 0.0);
assert_eq!(box_model.content_box.width, 50.0);
assert_eq!(box_model.content_box.height, 20.0);
}
_ => panic!("Expected single box model"),
}
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.content_box.height, 20.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn inline_with_padding() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 40.0,
height: 18.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
spacing: Spacing {
padding_left: Length::Px(12.0),
padding_right: Length::Px(8.0),
padding_top: Length::Px(6.0),
padding_bottom: Length::Px(4.0),
..Default::default()
},
..Default::default()
});
inline_node.set_fragments(vec![fragment1]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
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, 40.0);
assert_eq!(box_model.content_box.height, 18.0);
assert_eq!(box_model.padding_box.width, 60.0); assert_eq!(box_model.padding_box.height, 28.0);
assert_eq!(box_model.border_box.width, 60.0);
assert_eq!(box_model.border_box.height, 28.0);
}
_ => panic!("Expected single box model"),
}
assert_eq!(root.children[0].placements.len(), 1);
assert_eq!(root.children[0].placements[0].offset.0, 0.0); }
#[test]
fn inline_with_borders() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 35.0,
height: 22.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
spacing: Spacing {
padding_left: Length::Px(8.0),
padding_right: Length::Px(6.0),
padding_top: Length::Px(4.0),
padding_bottom: Length::Px(3.0),
border_left: Length::Px(3.0),
border_right: Length::Px(2.0),
border_top: Length::Px(1.0),
border_bottom: Length::Px(2.0),
..Default::default()
},
..Default::default()
});
inline_node.set_fragments(vec![fragment1]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
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, 35.0);
assert_eq!(box_model.content_box.height, 22.0);
assert_eq!(box_model.padding_box.width, 49.0); assert_eq!(box_model.padding_box.height, 29.0);
assert_eq!(box_model.border_box.width, 54.0); assert_eq!(box_model.border_box.height, 32.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn inline_percentage_spacing() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 30.0,
height: 20.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
spacing: Spacing {
padding_left: Length::Percent(5.0), padding_right: Length::Percent(2.5), padding_top: Length::Percent(7.5), padding_bottom: Length::Percent(1.0),
margin_left: Length::Percent(2.0), margin_top: Length::Percent(0.0), ..Default::default()
},
..Default::default()
});
inline_node.set_fragments(vec![fragment1]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
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, 30.0);
assert_eq!(box_model.content_box.height, 20.0);
assert_eq!(box_model.padding_box.width, 45.0); assert_eq!(box_model.padding_box.height, 37.0);
assert_eq!(box_model.border_box.x, 4.0); assert_eq!(box_model.border_box.y, 0.0); }
_ => panic!("Expected single box model"),
}
}
#[test]
fn mixed_inline_and_block_children() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 40.0,
height: 15.0,
});
let fragment2 = ItemFragment::Fragment(Fragment {
width: 35.0,
height: 20.0,
});
let mut inline_node1 = LayoutNode::new(Style {
display: Display::Inline,
..Default::default()
});
inline_node1.set_fragments(vec![fragment1]);
let block_node = LayoutNode::new(Style {
display: Display::Block,
size: SizeStyle {
width: Length::Px(100.0),
height: Length::Px(25.0),
..Default::default()
},
..Default::default()
});
let mut inline_node2 = LayoutNode::new(Style {
display: Display::Inline,
..Default::default()
});
inline_node2.set_fragments(vec![fragment2]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node1, block_node, inline_node2],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.x, 0.0);
assert_eq!(box_model.border_box.y, 0.0);
}
_ => panic!("Expected single box model"),
}
match &root.children[1].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.x, 0.0);
assert_eq!(box_model.border_box.y, 15.0);
}
_ => panic!("Expected single box model"),
}
match &root.children[2].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.x, 0.0);
assert_eq!(box_model.border_box.y, 40.0);
}
_ => panic!("Expected single box model"),
}
}
#[test]
fn inline_empty_fragments() {
let inline_node = LayoutNode::new(Style {
display: Display::Inline,
spacing: Spacing {
padding_left: Length::Px(10.0),
padding_right: Length::Px(10.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::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
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, 0.0); assert_eq!(box_model.content_box.height, 0.0); assert_eq!(box_model.padding_box.width, 20.0); assert_eq!(box_model.padding_box.height, 10.0); }
_ => panic!("Expected single box model"),
}
assert_eq!(root.children[0].placements.len(), 0);
}
#[test]
fn inline_viewport_relative_spacing() {
let fragment1 = ItemFragment::Fragment(Fragment {
width: 25.0,
height: 18.0,
});
let mut inline_node = LayoutNode::new(Style {
display: Display::Inline,
spacing: Spacing {
margin_left: Length::Vw(2.0), margin_top: Length::Vh(1.5), padding_left: Length::Vw(1.25), padding_top: Length::Vw(0.625), border_left: Length::Px(2.0),
border_top: Length::Px(1.0),
..Default::default()
},
..Default::default()
});
inline_node.set_fragments(vec![fragment1]);
let mut root = LayoutNode::with_children(
Style {
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Auto,
..Default::default()
},
..Default::default()
},
vec![inline_node],
);
LayoutEngine::layout(&mut root, 800.0, 600.0);
match &root.children[0].layout_boxes {
LayoutBoxes::Single(box_model) => {
assert_eq!(box_model.border_box.x, 16.0); assert_eq!(box_model.border_box.y, 0.0);
assert_eq!(box_model.content_box.width, 25.0);
assert_eq!(box_model.content_box.height, 18.0);
assert_eq!(box_model.padding_box.width, 35.0); assert_eq!(box_model.padding_box.height, 23.0);
assert_eq!(box_model.border_box.width, 37.0); assert_eq!(box_model.border_box.height, 24.0); }
_ => panic!("Expected single box model"),
}
}