#![allow(clippy::field_reassign_with_default)]
use strata_layout::box_model::collapse_margins;
use strata_layout::prelude::*;
use strata_primitives::geom::EdgeInsets;
use strata_style::types::{
AlignItems, ComputedStyle, Dimension, Display, FlexDirection, FlexWrap, GridTrack,
JustifyContent, Position,
};
#[test]
fn test_css_margin_collapsing_rules() {
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_block_layout_sibling_margin_collapsing() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.width = Dimension::Px(400.0);
root_style.height = Dimension::Auto;
let mut child1_style = ComputedStyle::default();
child1_style.display = Display::Block;
child1_style.width = Dimension::Px(200.0);
child1_style.height = Dimension::Px(100.0);
child1_style.margin.bottom = 30.0;
let mut child2_style = ComputedStyle::default();
child2_style.display = Display::Block;
child2_style.width = Dimension::Px(200.0);
child2_style.height = Dimension::Px(80.0);
child2_style.margin.top = 20.0;
let child1 = LayoutNode::new(child1_style);
let child2 = LayoutNode::new(child2_style);
let mut root = LayoutNode::with_children(root_style, vec![child1, child2]);
root.compute_layout(400.0, 800.0);
assert_eq!(root.children[0].geometry.border_box.y, 0.0);
assert_eq!(root.children[0].geometry.border_box.height, 100.0);
assert_eq!(root.children[1].geometry.border_box.y, 130.0);
assert_eq!(root.children[1].geometry.border_box.height, 80.0);
}
#[test]
fn test_flexbox_grow_expansion() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Flex;
container_style.flex_direction = FlexDirection::Row;
container_style.width = Dimension::Px(300.0);
container_style.height = Dimension::Px(100.0);
let mut item1_style = ComputedStyle::default();
item1_style.width = Dimension::Px(0.0);
item1_style.flex_grow = 1.0;
let mut item2_style = ComputedStyle::default();
item2_style.width = Dimension::Px(0.0);
item2_style.flex_grow = 2.0;
let mut container = LayoutNode::with_children(
container_style,
vec![LayoutNode::new(item1_style), LayoutNode::new(item2_style)],
);
container.compute_layout(300.0, 100.0);
assert_eq!(container.children[0].geometry.border_box.width, 100.0);
assert_eq!(container.children[1].geometry.border_box.width, 200.0);
assert_eq!(container.children[0].geometry.border_box.x, 0.0);
assert_eq!(container.children[1].geometry.border_box.x, 100.0);
}
#[test]
fn test_flexbox_weighted_shrink_contraction() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Flex;
container_style.flex_direction = FlexDirection::Row;
container_style.width = Dimension::Px(500.0);
container_style.height = Dimension::Px(100.0);
let mut item1_style = ComputedStyle::default();
item1_style.width = Dimension::Px(400.0);
item1_style.flex_shrink = 1.0;
let mut item2_style = ComputedStyle::default();
item2_style.width = Dimension::Px(200.0);
item2_style.flex_shrink = 2.0;
let mut container = LayoutNode::with_children(
container_style,
vec![LayoutNode::new(item1_style), LayoutNode::new(item2_style)],
);
container.compute_layout(500.0, 100.0);
assert_eq!(container.children[0].geometry.border_box.width, 350.0);
assert_eq!(container.children[1].geometry.border_box.width, 150.0);
assert_eq!(
container.children[0].geometry.border_box.width
+ container.children[1].geometry.border_box.width,
500.0
);
}
#[test]
fn test_flexbox_multi_line_wrapping() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Flex;
container_style.flex_direction = FlexDirection::Row;
container_style.flex_wrap = FlexWrap::Wrap;
container_style.width = Dimension::Px(200.0);
container_style.gap = 10.0;
let mut item1 = ComputedStyle::default();
item1.width = Dimension::Px(90.0);
item1.height = Dimension::Px(40.0);
let mut item2 = ComputedStyle::default();
item2.width = Dimension::Px(90.0);
item2.height = Dimension::Px(40.0);
let mut item3 = ComputedStyle::default();
item3.width = Dimension::Px(90.0);
item3.height = Dimension::Px(40.0);
let mut container = LayoutNode::with_children(
container_style,
vec![
LayoutNode::new(item1),
LayoutNode::new(item2),
LayoutNode::new(item3),
],
);
container.compute_layout(200.0, 500.0);
assert_eq!(container.children[0].geometry.border_box.x, 0.0);
assert_eq!(container.children[0].geometry.border_box.y, 0.0);
assert_eq!(container.children[1].geometry.border_box.x, 100.0); assert_eq!(container.children[1].geometry.border_box.y, 0.0);
assert_eq!(container.children[2].geometry.border_box.x, 0.0);
assert_eq!(container.children[2].geometry.border_box.y, 50.0);
}
#[test]
fn test_flexbox_justification_and_alignment() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Flex;
container_style.flex_direction = FlexDirection::Row;
container_style.justify_content = JustifyContent::Center;
container_style.align_items = AlignItems::Center;
container_style.width = Dimension::Px(400.0);
container_style.height = Dimension::Px(200.0);
let mut item_style = ComputedStyle::default();
item_style.width = Dimension::Px(100.0);
item_style.height = Dimension::Px(50.0);
let mut container =
LayoutNode::with_children(container_style, vec![LayoutNode::new(item_style)]);
container.compute_layout(400.0, 200.0);
assert_eq!(container.children[0].geometry.border_box.x, 150.0);
assert_eq!(container.children[0].geometry.border_box.y, 75.0);
}
#[test]
fn test_css_grid_fractional_tracks_and_span() {
let mut grid_style = ComputedStyle::default();
grid_style.display = Display::Grid;
grid_style.width = Dimension::Px(400.0);
grid_style.height = Dimension::Px(300.0);
grid_style.gap = 10.0;
grid_style.grid_template_columns =
vec![GridTrack::Px(100.0), GridTrack::Fr(1.0), GridTrack::Fr(2.0)];
let mut item1 = ComputedStyle::default();
item1.height = Dimension::Px(50.0);
let mut item2 = ComputedStyle::default();
item2.height = Dimension::Px(50.0);
let mut item3 = ComputedStyle::default();
item3.height = Dimension::Px(50.0);
let mut container = LayoutNode::with_children(
grid_style,
vec![
LayoutNode::new(item1),
LayoutNode::new(item2),
LayoutNode::new(item3),
],
);
container.compute_layout(400.0, 300.0);
assert_eq!(container.children[0].geometry.border_box.x, 0.0);
assert_eq!(container.children[0].geometry.border_box.width, 100.0);
assert_eq!(container.children[1].geometry.border_box.x, 110.0);
let track1_width = container.children[1].geometry.border_box.width;
let track2_width = container.children[2].geometry.border_box.width;
assert!((track2_width - (track1_width * 2.0)).abs() < 0.2);
}
#[test]
fn test_css_grid_auto_placement_matrix() {
let mut grid_style = ComputedStyle::default();
grid_style.display = Display::Grid;
grid_style.width = Dimension::Px(200.0);
grid_style.grid_template_columns = vec![GridTrack::Px(100.0), GridTrack::Px(100.0)];
let mut item1 = ComputedStyle::default();
item1.height = Dimension::Px(40.0);
item1.grid_column_span = Some(1);
let mut item2 = ComputedStyle::default();
item2.height = Dimension::Px(40.0);
item2.grid_column_span = Some(2);
let mut item3 = ComputedStyle::default();
item3.height = Dimension::Px(40.0);
item3.grid_column_span = Some(1);
let mut container = LayoutNode::with_children(
grid_style,
vec![
LayoutNode::new(item1),
LayoutNode::new(item2),
LayoutNode::new(item3),
],
);
container.compute_layout(200.0, 400.0);
assert_eq!(container.children[0].geometry.border_box.x, 0.0);
assert_eq!(container.children[0].geometry.border_box.y, 0.0);
assert_eq!(container.children[1].geometry.border_box.x, 0.0);
assert_eq!(container.children[1].geometry.border_box.y, 40.0);
assert_eq!(container.children[1].geometry.border_box.width, 200.0);
assert_eq!(container.children[2].geometry.border_box.x, 0.0);
assert_eq!(container.children[2].geometry.border_box.y, 80.0);
}
#[test]
fn test_relative_and_absolute_positioning() {
let mut container_style = ComputedStyle::default();
container_style.display = Display::Block;
container_style.width = Dimension::Px(500.0);
container_style.height = Dimension::Px(400.0);
container_style.padding = EdgeInsets::all(20.0);
let mut rel_style = ComputedStyle::default();
rel_style.display = Display::Block;
rel_style.position = Position::Relative;
rel_style.width = Dimension::Px(100.0);
rel_style.height = Dimension::Px(50.0);
rel_style.insets.left = Dimension::Px(15.0);
rel_style.insets.top = Dimension::Px(10.0);
let mut abs_style = ComputedStyle::default();
abs_style.display = Display::Block;
abs_style.position = Position::Absolute;
abs_style.width = Dimension::Px(80.0);
abs_style.height = Dimension::Px(40.0);
abs_style.insets.top = Dimension::Px(5.0);
abs_style.insets.right = Dimension::Px(10.0);
let mut container = LayoutNode::with_children(
container_style,
vec![LayoutNode::new(rel_style), LayoutNode::new(abs_style)],
);
container.compute_layout(500.0, 400.0);
assert_eq!(container.children[0].geometry.border_box.x, 35.0);
assert_eq!(container.children[0].geometry.border_box.y, 30.0);
assert_eq!(container.children[1].geometry.border_box.x, 410.0);
assert_eq!(container.children[1].geometry.border_box.y, 5.0);
}
#[test]
fn test_fixed_positioning_relative_to_viewport() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.width = Dimension::Px(800.0);
root_style.height = Dimension::Px(600.0);
let mut fixed_style = ComputedStyle::default();
fixed_style.position = Position::Fixed;
fixed_style.width = Dimension::Px(120.0);
fixed_style.height = Dimension::Px(60.0);
fixed_style.insets.bottom = Dimension::Px(20.0);
fixed_style.insets.right = Dimension::Px(30.0);
let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(fixed_style)]);
root.compute_layout(800.0, 600.0);
assert_eq!(root.children[0].geometry.border_box.x, 650.0);
assert_eq!(root.children[0].geometry.border_box.y, 520.0);
}
#[test]
fn test_text_intrinsic_measurement_and_wrapping() {
let text = "Hello Strata Zero Dependency Frontend Engine";
let font_size = 16.0;
let line_height_mul = 1.2;
let (w1, h1) = measure_text_bounds(text, font_size, line_height_mul, None);
assert!(w1 > 100.0);
assert_eq!(h1, 16.0 * 1.2);
let (w2, h2) = measure_text_bounds(text, font_size, line_height_mul, Some(80.0));
assert!(w2 <= 80.0 || w2 < w1);
assert!(h2 > h1); }
#[test]
fn test_subpixel_layout_snapping() {
let rect = Rect::new(10.1234, 20.5678, 100.3333, 50.6666);
let snapped = rect.snap_to_pixel();
let unit_x = (snapped.x * 64.0).round();
let reconstructed_x = unit_x / 64.0;
assert_eq!(snapped.x, reconstructed_x);
let unit_y = (snapped.y * 64.0).round();
let reconstructed_y = unit_y / 64.0;
assert_eq!(snapped.y, reconstructed_y);
}
#[test]
fn test_internal_layout_regression_suite_execution_and_compliance() {
let results = InternalLayoutRegressionSuite::run_all();
assert_eq!(results.len(), 14);
for vector in &results {
assert!(
vector.passed,
"Specification conformance vector '{}' ({}) failed with deviation: {:.4}px",
vector.id, vector.title, vector.deviation_px
);
assert!(vector.deviation_px <= 0.016);
}
let compliance = InternalLayoutRegressionSuite::compliance_percentage();
assert_eq!(compliance, 100.0);
}
#[test]
fn test_subtree_layout_cache_invalidation_and_hit() {
let mut root_style = ComputedStyle::default();
root_style.display = Display::Flex;
root_style.width = Dimension::Px(500.0);
root_style.height = Dimension::Px(300.0);
let mut child_style = ComputedStyle::default();
child_style.width = Dimension::Px(200.0);
child_style.height = Dimension::Px(100.0);
let mut root = LayoutNode::with_children(root_style, vec![LayoutNode::new(child_style)]);
assert!(!root.layout_cache.is_valid);
assert!(!root.children[0].layout_cache.is_valid);
root.compute_layout(500.0, 300.0);
assert!(root.layout_cache.is_valid);
assert_eq!(root.layout_cache.available_w, 500.0);
assert_eq!(root.layout_cache.available_h, 300.0);
assert_eq!(root.layout_cache.result_w, 500.0);
assert_eq!(root.layout_cache.result_h, 300.0);
root.compute_layout(500.0, 300.0);
assert!(root.layout_cache.is_valid);
root.invalidate_cache();
assert!(!root.layout_cache.is_valid);
assert!(!root.children[0].layout_cache.is_valid);
}
#[test]
fn test_layout_boundary_containment_classification() {
use strata_style::types::{Containment, Dimension, Overflow, Position};
let mut strict_style = ComputedStyle::default();
strict_style.contain = Containment::STRICT;
let strict_node = LayoutNode::new(strict_style);
assert!(
strict_node.is_layout_boundary(),
"contain: strict must be a layout boundary"
);
let mut content_style = ComputedStyle::default();
content_style.contain = Containment::CONTENT;
let content_node = LayoutNode::new(content_style);
assert!(
content_node.is_layout_boundary(),
"contain: content must be a layout boundary"
);
let mut layout_size_style = ComputedStyle::default();
layout_size_style.contain.layout = true;
layout_size_style.contain.size = true;
let layout_size_node = LayoutNode::new(layout_size_style);
assert!(
layout_size_node.is_layout_boundary(),
"contain: layout + size must be a layout boundary"
);
let mut layout_definite_style = ComputedStyle::default();
layout_definite_style.contain.layout = true;
layout_definite_style.width = Dimension::Px(300.0);
layout_definite_style.height = Dimension::Px(200.0);
let layout_definite_node = LayoutNode::new(layout_definite_style);
assert!(
layout_definite_node.is_layout_boundary(),
"contain: layout with definite dimensions must be a layout boundary"
);
let mut layout_auto_style = ComputedStyle::default();
layout_auto_style.contain.layout = true;
layout_auto_style.width = Dimension::Px(300.0);
layout_auto_style.height = Dimension::Auto;
let layout_auto_node = LayoutNode::new(layout_auto_style);
assert!(
!layout_auto_node.is_layout_boundary(),
"contain: layout with auto height must not be a layout boundary"
);
let mut abs_style = ComputedStyle::default();
abs_style.position = Position::Absolute;
abs_style.overflow_x = Overflow::Hidden;
abs_style.overflow_y = Overflow::Hidden;
abs_style.width = Dimension::Px(150.0);
abs_style.height = Dimension::Px(100.0);
let abs_node = LayoutNode::new(abs_style);
assert!(
abs_node.is_layout_boundary(),
"clipped absolute box with definite dimensions must be a layout boundary"
);
}
#[test]
fn test_granular_subtree_layout_invalidation_and_preservation() {
use strata_style::types::{Containment, Dimension, Display};
let mut root_style = ComputedStyle::default();
root_style.display = Display::Block;
root_style.width = Dimension::Px(600.0);
root_style.height = Dimension::Px(400.0);
let mut a_style = ComputedStyle::default();
a_style.display = Display::Block;
a_style.contain = Containment::STRICT;
a_style.width = Dimension::Px(250.0);
a_style.height = Dimension::Px(200.0);
let mut a_child_style = ComputedStyle::default();
a_child_style.width = Dimension::Px(100.0);
a_child_style.height = Dimension::Px(50.0);
let sib_a = LayoutNode::with_children(
a_style,
vec![
LayoutNode::new(a_child_style.clone()),
LayoutNode::new(a_child_style),
],
);
let mut b_style = ComputedStyle::default();
b_style.display = Display::Block;
b_style.width = Dimension::Px(250.0);
b_style.height = Dimension::Px(200.0);
let mut b_child_style = ComputedStyle::default();
b_child_style.width = Dimension::Px(120.0);
b_child_style.height = Dimension::Px(60.0);
let sib_b = LayoutNode::with_children(
b_style,
vec![
LayoutNode::new(b_child_style.clone()),
LayoutNode::new(b_child_style),
],
);
let mut root = LayoutNode::with_children(root_style, vec![sib_a, sib_b]);
root.compute_layout(600.0, 400.0);
assert!(root.layout_cache.is_valid);
assert!(root.children[0].layout_cache.is_valid);
assert!(root.children[1].layout_cache.is_valid);
let invalidated = root.invalidate_layout_boundary(&[0, 0]);
assert!(invalidated, "Must locate boundary along path");
assert!(!root.children[0].layout_cache.is_valid);
assert!(
root.children[1].layout_cache.is_valid,
"Sibling B layout cache must be preserved across isolated boundary invalidation"
);
let localized = root.compute_subgrid_or_boundary_layout(&[0, 0]);
assert!(localized, "Must recompute strictly within layout boundary");
assert!(root.children[0].geometry.border_box.width > 0.0);
assert!(root.children[1].layout_cache.is_valid);
assert_eq!(root.children[1].geometry.border_box.width, 250.0);
}
#[test]
fn test_flex_wrapped_log_rows_clean_subtree_caches_preserved() {
use strata_style::types::{FlexDirection, Overflow};
fn make_row(idx: usize, extra: &str) -> LayoutNode {
let mut row_style = ComputedStyle::default();
row_style.display = Display::Flex;
row_style.flex_direction = FlexDirection::Row;
row_style.width = Dimension::Percent(100.0);
let mut badge_style = ComputedStyle::default();
badge_style.width = Dimension::Px(56.0);
badge_style.flex_shrink = 0.0;
let badge = LayoutNode::with_text(badge_style, format!("[{:03}]", idx));
let mut msg_style = ComputedStyle::default();
msg_style.flex_grow = 1.0;
msg_style.flex_shrink = 1.0;
msg_style.flex_basis = Dimension::Px(0.0);
msg_style.min_width = Dimension::Px(0.0);
let msg = LayoutNode::with_text(
msg_style,
format!(
"Log entry #{idx}{extra}: deterministic telemetry frame streamed across multiple wrapped lines inside a flex log viewer."
),
);
LayoutNode::with_children(row_style, vec![badge, msg])
}
let mut root_style = ComputedStyle::default();
root_style.display = Display::Flex;
root_style.flex_direction = FlexDirection::Column;
root_style.width = Dimension::Px(640.0);
root_style.height = Dimension::Px(480.0);
root_style.overflow_y = Overflow::Auto;
let rows: Vec<LayoutNode> = (0..150).map(|i| make_row(i, "")).collect();
let mut root = LayoutNode::with_children(root_style.clone(), rows);
root.compute_layout(640.0, 480.0);
assert!(root.children[1].children_layout_clean);
assert!(root.children[1].intrinsic_cache.get(Some(640.0)).is_some()
|| root.children[1].intrinsic_cache.get(None).is_some()
|| root.children[1].measure_cache[0].is_valid
|| root.children[1].layout_cache.is_valid);
root.children[0] = make_row(
0,
" — extended payload forcing additional wrapped lines to verify downstream translation accuracy across cached siblings",
);
root.invalidate_ancestors(&[0]);
assert!(root.children[1].children_layout_clean);
assert!(root.children[149].children_layout_clean);
root.compute_layout(640.0, 480.0);
let mut ref_rows: Vec<LayoutNode> = (0..150).map(|i| make_row(i, "")).collect();
ref_rows[0] = make_row(
0,
" — extended payload forcing additional wrapped lines to verify downstream translation accuracy across cached siblings",
);
let mut reference = LayoutNode::with_children(root_style, ref_rows);
reference.compute_layout(640.0, 480.0);
for idx in [0usize, 1, 50, 149] {
let a = root.children[idx].geometry.border_box;
let b = reference.children[idx].geometry.border_box;
assert!(
(a.x - b.x).abs() < 0.05
&& (a.y - b.y).abs() < 0.05
&& (a.width - b.width).abs() < 0.05
&& (a.height - b.height).abs() < 0.05,
"row {idx} incremental layout {:?} must match cold layout {:?}",
a,
b
);
let a_msg = root.children[idx].children[1].geometry.border_box;
let b_msg = reference.children[idx].children[1].geometry.border_box;
assert!(
(a_msg.y - b_msg.y).abs() < 0.05 && (a_msg.height - b_msg.height).abs() < 0.05,
"row {idx} message child {:?} must match cold layout {:?}",
a_msg,
b_msg
);
}
}