use ui_layout::*;
#[test]
fn test_flex_basis_auto() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(300.0),
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child1 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(50.0),
height: Length::Px(50.0),
..Default::default()
},
item_style: ItemStyle {
flex_basis: Length::Auto,
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(80.0),
height: Length::Px(50.0),
..Default::default()
},
item_style: ItemStyle {
flex_basis: Length::Auto,
..Default::default()
},
..Default::default()
});
container.children = vec![child1, child2];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref container_box) = container.layout_boxes {
assert_eq!(container_box.border_box.width, 300.0);
assert_eq!(container_box.border_box.height, 100.0);
}
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert_eq!(child_box.border_box.width, 50.0);
assert_eq!(child_box.border_box.height, 50.0);
}
if let LayoutBoxes::Single(ref child_box) = container.children[1].layout_boxes {
assert_eq!(child_box.border_box.width, 80.0);
assert_eq!(child_box.border_box.height, 50.0);
}
}
#[test]
#[ignore = "The implementation of ignoring Width and treating it as basis when flexing is not implemented yet."]
fn flex_basis_overrides_width_when_no_grow_or_shrink() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(300.0),
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(50.0),
..Default::default()
},
item_style: ItemStyle {
flex_basis: Length::Px(120.0),
flex_grow: 0.0,
flex_shrink: 0.0,
..Default::default()
},
..Default::default()
});
container.children = vec![child];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert_eq!(child_box.border_box.width, 120.0);
}
}
#[test]
fn flex_basis_is_starting_point_for_grow() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(300.0),
..Default::default()
},
..Default::default()
});
let child = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(100.0),
flex_grow: 1.0,
..Default::default()
},
..Default::default()
});
container.children = vec![child];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert_eq!(child_box.border_box.width, 300.0);
}
}
#[test]
fn flex_basis_is_starting_point_for_shrink() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(200.0),
flex_shrink: 1.0,
..Default::default()
},
..Default::default()
});
container.children = vec![child];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert_eq!(child_box.border_box.width, 100.0);
}
}
#[test]
fn test_flex_basis_with_grow() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(400.0),
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child1 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(100.0),
flex_grow: 1.0,
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(100.0),
flex_grow: 2.0,
..Default::default()
},
..Default::default()
});
container.children = vec![child1, child2];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert!((child_box.border_box.width - 166.7).abs() < 0.1);
}
if let LayoutBoxes::Single(ref child_box) = container.children[1].layout_boxes {
assert!((child_box.border_box.width - 233.3).abs() < 0.1);
}
}
#[test]
fn test_flex_basis_with_shrink() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child1 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(150.0),
flex_shrink: 1.0,
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(100.0),
flex_shrink: 2.0,
..Default::default()
},
..Default::default()
});
container.children = vec![child1, child2];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert!((child_box.border_box.width - 128.6).abs() < 0.1);
}
if let LayoutBoxes::Single(ref child_box) = container.children[1].layout_boxes {
assert!((child_box.border_box.width - 71.4).abs() < 0.1);
}
}
#[test]
fn test_flex_basis_percentage() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(400.0),
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child1 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Percent(25.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Percent(50.0),
flex_grow: 1.0,
..Default::default()
},
..Default::default()
});
container.children = vec![child1, child2];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert_eq!(child_box.border_box.width, 100.0);
}
if let LayoutBoxes::Single(ref child_box) = container.children[1].layout_boxes {
assert_eq!(child_box.border_box.width, 300.0);
}
}
#[test]
#[ignore = "The implementation of ignoring Width and treating it as basis when flexing is not implemented yet."]
fn test_mixed_flex_properties_complete() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Row,
},
size: SizeStyle {
width: Length::Px(500.0),
height: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child1 = LayoutNode::new(Style {
size: SizeStyle {
width: Length::Px(60.0),
..Default::default()
},
item_style: ItemStyle {
flex_basis: Length::Auto,
flex_grow: 1.0,
flex_shrink: 1.0,
align_self: None,
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(120.0),
flex_grow: 0.0,
flex_shrink: 0.0,
align_self: None,
},
..Default::default()
});
let child3 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Percent(20.0), flex_grow: 2.0,
flex_shrink: 1.0,
align_self: None,
},
..Default::default()
});
container.children = vec![child1, child2, child3];
LayoutEngine::layout(&mut container, 500.0, 100.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
let expected = 60.0 + 220.0 * (1.0 / 3.0);
assert!(
(child_box.border_box.width - expected).abs() < 0.1,
"Child1 width incorrect: expected {}, got {}",
expected,
child_box.border_box.width
);
} else {
panic!("Child1 layout box missing");
}
if let LayoutBoxes::Single(ref child_box) = container.children[1].layout_boxes {
assert_eq!(
child_box.border_box.width, 120.0,
"Child2 should remain fixed at 120px"
);
} else {
panic!("Child2 layout box missing");
}
if let LayoutBoxes::Single(ref child_box) = container.children[2].layout_boxes {
let expected = 100.0 + 220.0 * (2.0 / 3.0);
assert!(
(child_box.border_box.width - expected).abs() < 0.1,
"Child3 width incorrect: expected {}, got {}",
expected,
child_box.border_box.width
);
} else {
panic!("Child3 layout box missing");
}
let total: f32 = container
.children
.iter()
.map(|c| {
if let LayoutBoxes::Single(ref b) = c.layout_boxes {
b.border_box.width
} else {
0.0
}
})
.sum();
assert!(
(total - 500.0).abs() < 0.1,
"Total width {} does not match container width 500px",
total
);
}
#[test]
fn test_flex_basis_column_direction() {
let mut container = LayoutNode::new(Style {
display: Display::Flex {
flex_direction: FlexDirection::Column,
},
size: SizeStyle {
width: Length::Px(200.0),
height: Length::Px(300.0),
..Default::default()
},
..Default::default()
});
let child1 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(100.0),
..Default::default()
},
..Default::default()
});
let child2 = LayoutNode::new(Style {
item_style: ItemStyle {
flex_basis: Length::Px(150.0),
..Default::default()
},
..Default::default()
});
container.children = vec![child1, child2];
LayoutEngine::layout(&mut container, 800.0, 600.0);
if let LayoutBoxes::Single(ref child_box) = container.children[0].layout_boxes {
assert_eq!(child_box.border_box.height, 100.0);
assert_eq!(child_box.border_box.width, 200.0); }
if let LayoutBoxes::Single(ref child_box) = container.children[1].layout_boxes {
assert_eq!(child_box.border_box.height, 150.0);
assert_eq!(child_box.border_box.width, 200.0); }
}