thing-core test: the form is read off the final height; empty Button; shrink to compact; render placeholder
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
eb57a4281a
commit
e5f497714f
2 changed files with 106 additions and 0 deletions
|
|
@ -1390,6 +1390,78 @@ fn a_compact_buttons_label_stands_on_its_first_row_inside_the_brackets() {
|
|||
}
|
||||
}
|
||||
|
||||
/// `root` laid in a Cells frame, text measured in cells.
|
||||
fn lay_in_cells(root: Node, w: f32, h: f32) -> LaidTree {
|
||||
let tree = bind(&mut MemState::new(), &surface_of(root)).expect("resolves");
|
||||
layout(&tree, &cells(w, h), &mut FixedMeasure::cells()).unwrap()
|
||||
}
|
||||
|
||||
/// A column of `rows` rows holding each of `children` in a box of its own that may shrink to nothing.
|
||||
fn column_of(rows: f64, children: Vec<(f64, Node)>) -> Node {
|
||||
let boxes = children
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(i, (grow, node))| {
|
||||
let shrinkable = BoxStyle { grow, min_size: size2(Dimension::Auto, px(0.0)), ..plain() };
|
||||
bx(&format!("holder{i}"), shrinkable, vec![node])
|
||||
})
|
||||
.collect();
|
||||
bx("root", BoxStyle { direction: Direction::Column, size: size2(px(160.0), px(rows * ROW)), ..plain() }, boxes)
|
||||
}
|
||||
|
||||
fn a_button(id: &str, label: &str) -> Node {
|
||||
Node::Button(ButtonNode { common: common(id), content: vec![text(&format!("{id}-label"), label)], on_activate: command("dismiss"), enabled: lit_bool(true) })
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_form_is_read_off_the_final_height_whatever_a_sibling_does() {
|
||||
// Soul's sticky probe: X grows and Y does not, each a min-height-0 box around a control. Whatever
|
||||
// heights the one solve leaves, every control's form is Compact iff it is under 3 rows and its
|
||||
// content box is its rect less that form's inset.
|
||||
for kind in BORDERED {
|
||||
for rows in [4.0_f64, 5.0, 6.0, 9.0] {
|
||||
let root = column_of(rows, vec![(1.0, bordered(kind)), (0.0, a_button("other", "Go"))]);
|
||||
let laid = lay_in_cells(root, 40.0, 20.0);
|
||||
for id in [kind, "other"] {
|
||||
let (x, y, w, h) = rect(&laid, id);
|
||||
let expected = if h < 3.0 {
|
||||
(ControlForm::Compact, (x + 1.0, y, w - 2.0, h))
|
||||
} else {
|
||||
(ControlForm::Bordered, (x + 1.0, y + 1.0, w - 2.0, h - 2.0))
|
||||
};
|
||||
assert_eq!(control(&laid, id), expected, "{kind} beside a button in {rows} rows: {id} is {h} rows");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_content_sized_button_is_three_bordered_rows() {
|
||||
let button = Node::Button(ButtonNode { common: common("btn"), content: vec![], on_activate: command("dismiss"), enabled: lit_bool(true) });
|
||||
let laid = lay_in_cells(bx("root", plain(), vec![button]), 40.0, 10.0);
|
||||
let (x, y, w, h) = rect(&laid, "btn");
|
||||
assert_eq!(h, 3.0, "a border row above and below one content row");
|
||||
assert_eq!(control(&laid, "btn"), (ControlForm::Bordered, (x + 1.0, y + 1.0, w - 2.0, 1.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_content_sized_control_shrinks_to_one_compact_row_in_a_short_column() {
|
||||
// Directly in the column: no wrapper that may shrink, only the shell's edge rows giving way.
|
||||
for kind in BORDERED {
|
||||
for rows in [1.0_f64, 2.0] {
|
||||
let root = bx("root", BoxStyle { direction: Direction::Column, size: size2(px(160.0), px(rows * ROW)), ..plain() }, vec![bordered(kind)]);
|
||||
let laid = lay_in_cells(root, 40.0, 10.0);
|
||||
let (x, y, w, h) = rect(&laid, kind);
|
||||
assert_eq!(h, rows as f32, "{kind} in {rows} rows");
|
||||
assert_eq!(control(&laid, kind), (ControlForm::Compact, (x + 1.0, y, w - 2.0, h)), "{kind} in {rows} rows");
|
||||
}
|
||||
}
|
||||
let root = bx("root", BoxStyle { direction: Direction::Column, size: size2(px(160.0), px(2.0 * ROW)), ..plain() }, vec![a_button("btn", "Go")]);
|
||||
let laid = lay_in_cells(root, 40.0, 10.0);
|
||||
let (button, label) = (rect(&laid, "btn"), rect(&laid, "btn-label"));
|
||||
assert_eq!((label.0, label.1, label.3), (button.0 + 1.0, button.1, 1.0), "a 1-row label on the first row of the 2-row Button");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_pixels_control_is_always_bordered_with_a_two_px_inset_and_a_pixels_button_has_none() {
|
||||
// The compact form is a Cells form: a short Pixels control keeps its chrome.
|
||||
|
|
|
|||
|
|
@ -591,6 +591,40 @@ fn leaves_draw_what_this_cut_owns() {
|
|||
assert_eq!(rendered.placed.len(), laid.nodes.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_control_layout_states_no_form_for_paints_the_placeholder() {
|
||||
// Render invents no form: a Button and a TextInput whose laid node carries no control are the
|
||||
// placeholder and nothing else, not chrome as a bordered control.
|
||||
let button = Node::Button(ButtonNode { common: common("btn"), content: vec![], on_activate: command("dismiss"), enabled: lit_bool(true) });
|
||||
let field = Node::TextInput(TextInputNode {
|
||||
common: common("field"),
|
||||
multiline: false,
|
||||
input_kind: InputKind::Text,
|
||||
placeholder: lit_text("type"),
|
||||
initial: lit_text("hi"),
|
||||
submit: command("dismiss"),
|
||||
});
|
||||
let root = flat("root", BoxStyle { direction: Direction::Column, size: size2(px(160.0), px(160.0)), ..plain() }, vec![button, field]);
|
||||
let tree = bind(&mut MemState::new(), &surface_of(root)).expect("resolves");
|
||||
let frame = frame(20.0, 10.0, 1.0, Metrics::Cells);
|
||||
let mut laid = layout(&tree, &frame, &mut *measure(&frame)).unwrap();
|
||||
for node in laid.nodes.iter_mut().filter(|n| matches!(n.id.as_str(), "btn" | "field")) {
|
||||
assert!(node.control.take().is_some(), "{} is a laid control", node.id.as_str());
|
||||
}
|
||||
let scroll = ScrollOffsets::default();
|
||||
let theme = Theme::std();
|
||||
let rendered = render(&tree, &laid, &input(&frame, &scroll, &theme), &mut *measure(&frame));
|
||||
let (btn, field) = (placed(&rendered, "btn").rect, placed(&rendered, "field").rect);
|
||||
let placeholder = |r: thing_core::layout::Rect| {
|
||||
[
|
||||
RenderCommand::FillRect { rect: r, radius: 0.0, color: color("thing.placeholder") },
|
||||
RenderCommand::StrokeRect { rect: r, radius: 0.0, width: 1.0, color: color("thing.error") },
|
||||
]
|
||||
};
|
||||
let expected: Vec<_> = placeholder(btn).into_iter().chain(placeholder(field)).collect();
|
||||
assert_eq!(rendered.commands, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_oversize_asset_extent_through_bind_is_an_invalid_measure_and_renders() {
|
||||
let provider = ProviderId::new("provider.render");
|
||||
|
|
|
|||
Loading…
Reference in a new issue