#![allow(clippy::field_reassign_with_default)]
use strata_layout::box_model::collapse_margins;
use strata_layout::prelude::*;
use strata_style::types::{ComputedStyle, Dimension, Display, Overflow};
#[test]
fn test_css_margin_collapsing_basic_math() {
assert_eq!(collapse_margins(20.0, 30.0), 30.0);
assert_eq!(collapse_margins(50.0, 10.0), 50.0);
assert_eq!(collapse_margins(-15.0, -25.0), -25.0);
assert_eq!(collapse_margins(-40.0, -10.0), -40.0);
assert_eq!(collapse_margins(30.0, -10.0), 20.0);
assert_eq!(collapse_margins(10.0, -30.0), -20.0);
assert_eq!(collapse_margins(0.0, -15.0), -15.0);
assert_eq!(collapse_margins(25.0, 0.0), 25.0);
}
#[test]
fn test_empty_block_margin_collapsing_through() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.width = Dimension::Px(300.0);
root_style.height = Dimension::Auto;
let mut s1 = ComputedStyle::default();
s1.display = Display::Block;
s1.width = Dimension::Px(200.0);
s1.height = Dimension::Px(50.0);
s1.margin.bottom = 20.0;
let mut s2 = ComputedStyle::default();
s2.display = Display::Block;
s2.width = Dimension::Px(200.0);
s2.height = Dimension::Px(0.0);
s2.margin.top = 30.0;
s2.margin.bottom = 40.0;
let mut s3 = ComputedStyle::default();
s3.display = Display::Block;
s3.width = Dimension::Px(200.0);
s3.height = Dimension::Px(50.0);
s3.margin.top = 10.0;
let node1 = LayoutNode::new(s1);
let node2 = LayoutNode::new(s2);
let node3 = LayoutNode::new(s3);
let mut root = LayoutNode::with_children(root_style, vec![node1, node2, node3]);
root.compute_layout(300.0, 600.0);
assert_eq!(root.children[0].geometry.border_box.y, 0.0);
assert_eq!(root.children[0].geometry.border_box.height, 50.0);
assert_eq!(
root.children[2].geometry.border_box.y, 90.0,
"Child 3 must be positioned at 90.0 after single collapsed margin through empty block"
);
assert_eq!(root.children[2].geometry.border_box.height, 50.0);
}
#[test]
fn test_multiple_consecutive_empty_blocks_collapsing_through() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.width = Dimension::Px(300.0);
root_style.height = Dimension::Auto;
let mut s1 = ComputedStyle::default();
s1.display = Display::Block;
s1.height = Dimension::Px(40.0);
s1.margin.bottom = 15.0;
let mut s2 = ComputedStyle::default();
s2.display = Display::Block;
s2.height = Dimension::Px(0.0);
s2.margin.top = 25.0;
s2.margin.bottom = 10.0;
let mut s3 = ComputedStyle::default();
s3.display = Display::Block;
s3.height = Dimension::Px(0.0);
s3.margin.top = 35.0;
s3.margin.bottom = 5.0;
let mut s4 = ComputedStyle::default();
s4.display = Display::Block;
s4.height = Dimension::Px(40.0);
s4.margin.top = 20.0;
let mut root = LayoutNode::with_children(
root_style,
vec![
LayoutNode::new(s1),
LayoutNode::new(s2),
LayoutNode::new(s3),
LayoutNode::new(s4),
],
);
root.compute_layout(300.0, 600.0);
assert_eq!(root.children[0].geometry.border_box.y, 0.0);
assert_eq!(root.children[0].geometry.border_box.height, 40.0);
assert_eq!(
root.children[3].geometry.border_box.y, 75.0,
"Sibling 4 must be positioned at 75.0 across multiple empty blocks"
);
}
#[test]
fn test_parent_first_child_top_margin_collapsing() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Block;
container_style.width = Dimension::Px(300.0);
container_style.height = Dimension::Auto;
container_style.margin.top = 10.0;
let mut child_style = ComputedStyle::default();
child_style.display = Display::Block;
child_style.width = Dimension::Px(200.0);
child_style.height = Dimension::Px(50.0);
child_style.margin.top = 30.0;
let mut container =
LayoutNode::with_children(container_style, vec![LayoutNode::new(child_style)]);
container.compute_layout(300.0, 600.0);
assert_eq!(
container.children[0].geometry.border_box.y, 0.0,
"First child must sit at y = 0 relative to parent when top margins collapse"
);
assert_eq!(
container.style.margin.top, 30.0,
"Parent effective top margin must be collapsed to max(10, 30) = 30"
);
}
#[test]
fn test_parent_last_child_bottom_margin_collapsing() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Block;
container_style.width = Dimension::Px(300.0);
container_style.height = Dimension::Auto;
container_style.margin.bottom = 15.0;
let mut child_style = ComputedStyle::default();
child_style.display = Display::Block;
child_style.width = Dimension::Px(200.0);
child_style.height = Dimension::Px(60.0);
child_style.margin.bottom = 35.0;
let mut container =
LayoutNode::with_children(container_style, vec![LayoutNode::new(child_style)]);
container.compute_layout(300.0, 600.0);
assert_eq!(
container.geometry.border_box.height, 60.0,
"Parent auto height must not include the bottom margin that collapsed with parent"
);
assert_eq!(
container.style.margin.bottom, 35.0,
"Parent effective bottom margin must be collapsed to max(15, 35) = 35"
);
}
#[test]
fn test_bfc_boundary_prevents_parent_child_collapsing() {
let mut bfc_style = ComputedStyle::default();
bfc_style.display = Display::Block;
bfc_style.width = Dimension::Px(300.0);
bfc_style.height = Dimension::Auto;
bfc_style.overflow_x = Overflow::Hidden;
bfc_style.overflow_y = Overflow::Hidden; bfc_style.margin.top = 10.0;
let mut child_style = ComputedStyle::default();
child_style.display = Display::Block;
child_style.width = Dimension::Px(200.0);
child_style.height = Dimension::Px(50.0);
child_style.margin.top = 30.0;
let mut bfc_container =
LayoutNode::with_children(bfc_style, vec![LayoutNode::new(child_style)]);
bfc_container.compute_layout(300.0, 600.0);
assert_eq!(
bfc_container.children[0].geometry.border_box.y, 30.0,
"BFC root container must prevent child margin from collapsing with parent"
);
assert_eq!(
bfc_container.style.margin.top, 10.0,
"BFC container top margin remains 10"
);
}
#[test]
fn test_nested_bfc_child_prevents_margin_collapsing() {
let mut parent_style = ComputedStyle::default();
parent_style.display = Display::Block;
parent_style.width = Dimension::Px(400.0);
parent_style.height = Dimension::Auto;
parent_style.margin.top = 15.0;
parent_style.margin.bottom = 20.0;
let mut bfc_child_style = ComputedStyle::default();
bfc_child_style.display = Display::Block;
bfc_child_style.width = Dimension::Px(200.0);
bfc_child_style.height = Dimension::Px(50.0);
bfc_child_style.overflow_x = Overflow::Hidden;
bfc_child_style.overflow_y = Overflow::Hidden;
bfc_child_style.margin.top = 30.0;
bfc_child_style.margin.bottom = 25.0;
let mut container =
LayoutNode::with_children(parent_style, vec![LayoutNode::new(bfc_child_style)]);
container.compute_layout(400.0, 600.0);
assert_eq!(
container.style.margin.top, 15.0,
"Parent top margin must remain uncollapsed (15.0) when child establishes BFC"
);
assert_eq!(
container.children[0].geometry.border_box.y, 30.0,
"BFC child must sit at y = 30.0 inside parent due to its uncollapsed top margin"
);
assert_eq!(
container.style.margin.bottom, 20.0,
"Parent bottom margin must remain uncollapsed (20.0) when child establishes BFC"
);
assert_eq!(
container.geometry.border_box.height, 105.0,
"Parent auto height must enclose child plus uncollapsed top and bottom margins (105.0)"
);
}