use morphorm::*;
use morphorm_ecs::*;
#[test]
fn absolute_pixels_width_pixels_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Pixels(100.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 100.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 100.0 }));
}
#[test]
fn absolute_pixels_width_percentage_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Percentage(25.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 150.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 150.0 }));
}
#[test]
fn absolute_pixels_width_stretch_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Stretch(1.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 600.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 600.0 }));
}
#[test]
fn absolute_pixels_width_auto_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Auto);
world.set_position_type(node, PositionType::Absolute);
let child = world.add(Some(node));
world.set_width(child, Units::Pixels(50.0));
world.set_height(child, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 100.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
}
#[test]
fn absolute_percentage_width_pixels_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Percentage(50.0));
world.set_height(node, Units::Pixels(100.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 300.0, height: 100.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 300.0, height: 100.0 }));
}
#[test]
fn absolute_stretch_width_pixels_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Stretch(1.0));
world.set_height(node, Units::Pixels(100.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 600.0, height: 100.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 600.0, height: 100.0 }));
}
#[test]
fn absolute_auto_width_pixels_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Auto);
world.set_height(node, Units::Pixels(100.0));
world.set_position_type(node, PositionType::Absolute);
let child = world.add(Some(node));
world.set_width(child, Units::Pixels(50.0));
world.set_height(child, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 100.0 }));
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 100.0 }));
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
}
#[test]
fn absolute_auto_width_auto_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Auto);
world.set_height(node, Units::Auto);
world.set_position_type(node, PositionType::Absolute);
let child = world.add(Some(node));
world.set_width(child, Units::Pixels(50.0));
world.set_height(child, Units::Pixels(50.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
}
#[test]
fn absolute_stretch_width_stretch_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Stretch(1.0));
world.set_height(node, Units::Stretch(1.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 600.0, height: 600.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 600.0, height: 600.0 }));
}
#[test]
fn absolute_percentage_width_percentage_height() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Percentage(50.0));
world.set_height(node, Units::Percentage(25.0));
world.set_position_type(node, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 300.0, height: 150.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 300.0, height: 150.0 }));
}
#[test]
fn auto_parent_pixels_child_stretch_absolute_child() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Auto);
world.set_height(node, Units::Auto);
let child1 = world.add(Some(node));
world.set_width(child1, Units::Pixels(50.0));
world.set_height(child1, Units::Pixels(50.0));
let child2 = world.add(Some(node));
world.set_width(child2, Units::Stretch(1.0));
world.set_height(child2, Units::Stretch(1.0));
world.set_position_type(child2, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child2), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child2), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
}
#[test]
fn auto_parent_pixels_child_percentage_absolute_child() {
let mut world = World::default();
let root = world.add(None);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(600.0));
let node = world.add(Some(root));
world.set_width(node, Units::Auto);
world.set_height(node, Units::Auto);
let child1 = world.add(Some(node));
world.set_width(child1, Units::Pixels(50.0));
world.set_height(child1, Units::Pixels(50.0));
let child2 = world.add(Some(node));
world.set_width(child2, Units::Percentage(50.0));
world.set_height(child2, Units::Percentage(25.0));
world.set_position_type(child2, PositionType::Absolute);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child2), Some(&Rect { posx: 0.0, posy: 0.0, width: 25.0, height: 13.0 }));
world.set_layout_type(root, LayoutType::Row);
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child1), Some(&Rect { posx: 0.0, posy: 0.0, width: 50.0, height: 50.0 }));
assert_eq!(world.cache.bounds(child2), Some(&Rect { posx: 0.0, posy: 0.0, width: 25.0, height: 13.0 }));
}
#[test]
fn rtl_absolute_in_column_only_flips_horizontal_offsets() {
let mut world = World::default();
let root = world.add(None);
world.set_layout_type(root, LayoutType::Column);
world.set_direction(root, Direction::RightToLeft);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
let node = world.add(Some(root));
world.set_position_type(node, PositionType::Absolute);
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Pixels(80.0));
world.set_left(node, Units::Pixels(20.0));
world.set_top(node, Units::Pixels(10.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 480.0, posy: 10.0, width: 100.0, height: 80.0 }));
}
#[test]
fn rtl_absolute_in_column_bottom_offset_is_not_flipped() {
let mut world = World::default();
let root = world.add(None);
world.set_layout_type(root, LayoutType::Column);
world.set_direction(root, Direction::RightToLeft);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
let node = world.add(Some(root));
world.set_position_type(node, PositionType::Absolute);
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Pixels(80.0));
world.set_left(node, Units::Pixels(20.0));
world.set_bottom(node, Units::Pixels(10.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 480.0, posy: 310.0, width: 100.0, height: 80.0 }));
}
#[test]
fn rtl_absolute_stretch_height_in_column_uses_top_and_bottom_without_flip() {
let mut world = World::default();
let root = world.add(None);
world.set_layout_type(root, LayoutType::Column);
world.set_direction(root, Direction::RightToLeft);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
let node = world.add(Some(root));
world.set_position_type(node, PositionType::Absolute);
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Stretch(1.0));
world.set_left(node, Units::Pixels(20.0));
world.set_top(node, Units::Pixels(10.0));
world.set_bottom(node, Units::Pixels(30.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 480.0, posy: 10.0, width: 100.0, height: 360.0 }));
}
#[test]
fn rtl_absolute_in_row_only_flips_horizontal_offsets() {
let mut world = World::default();
let root = world.add(None);
world.set_layout_type(root, LayoutType::Row);
world.set_direction(root, Direction::RightToLeft);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
let node = world.add(Some(root));
world.set_position_type(node, PositionType::Absolute);
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Pixels(80.0));
world.set_left(node, Units::Pixels(20.0));
world.set_top(node, Units::Pixels(10.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 480.0, posy: 10.0, width: 100.0, height: 80.0 }));
}
#[test]
fn rtl_absolute_bottom_offset_is_not_flipped() {
let mut world = World::default();
let root = world.add(None);
world.set_layout_type(root, LayoutType::Row);
world.set_direction(root, Direction::RightToLeft);
world.set_width(root, Units::Pixels(600.0));
world.set_height(root, Units::Pixels(400.0));
let node = world.add(Some(root));
world.set_position_type(node, PositionType::Absolute);
world.set_width(node, Units::Pixels(100.0));
world.set_height(node, Units::Pixels(80.0));
world.set_left(node, Units::Pixels(20.0));
world.set_bottom(node, Units::Pixels(10.0));
root.layout(&mut world.cache, &world.tree, &world.store, &mut ());
assert_eq!(world.cache.bounds(node), Some(&Rect { posx: 480.0, posy: 310.0, width: 100.0, height: 80.0 }));
}