Files
makeCAD/crates/ui-egui/src/browser_card_tests.rs
T

242 lines
11 KiB
Rust

use super::*;
use crate::scenario::{Harness, handle_at, rect_of};
use crate::shell::{LayoutMode, SHELL_GAP};
#[test]
fn browser_card_shrinks_and_scrolls_without_covering_history() {
let mut h = Harness::new();
h.call("ui.resize", json!({"width":1400,"height":900}));
h.settle();
let short = rect_of("browser:card").unwrap();
let history = rect_of("history:card").unwrap();
assert_eq!(short.left(), SHELL_GAP);
assert_eq!(short.width(), 250.0);
assert_eq!(short.top(), rect_of("ribbon:card").unwrap().bottom() + SHELL_GAP);
assert!(short.bottom() < history.top() - SHELL_GAP);
assert!(drawn_rows().iter().all(|(_, rect, _, _)| rect.height() == 26.0));
for i in 0..35 {
h.app.run("sketch.create", json!({"plane":"XY", "name":format!("Sketch{i}")})).unwrap();
h.app.run("sketch.finish", json!({})).unwrap();
}
h.settle();
let tall = rect_of("browser:card").unwrap();
assert!(tall.height() > short.height());
assert!((tall.bottom() - (history.top() - SHELL_GAP)).abs() < 1.0, "{tall:?} {history:?}");
assert!(h.app.viewport.rect.unwrap().contains_rect(tall));
assert!(handle_at("row:Sketch34").is_none(), "last row starts below the scroll clip");
let camera = h.app.cam;
h.call("ui.scroll", json!({"x":tall.center().x,"y":tall.center().y,"delta":-1800}));
h.settle();
assert!(handle_at("row:Sketch34").is_some(), "scrolling must reveal the last row");
assert_eq!(h.app.cam, camera);
assert_eq!(rect_of("browser:card").unwrap(), tall);
h.call("ui.set", json!({"layout":"docked"}));
h.settle();
assert!(rect_of("browser:card").is_none());
assert_eq!(h.app.viewport.rect.unwrap().left(), 250.0);
assert!(drawn_rows().iter().all(|(_, rect, _, _)| rect.height() == 26.0));
h.call("ui.set", json!({"layout":"floating"}));
h.app.tree.collapsed.insert("c0".into());
h.settle();
assert!(rect_of("browser:card").unwrap().height() < short.height());
}
#[test]
fn selected_row_paints_tab_soft_fill_and_accent_text_in_both_layouts() {
let previous = crate::theme::is_dark();
for dark in [false, true] {
crate::theme::set_dark(dark);
for mode in [LayoutMode::Floating, LayoutMode::Docked] {
for tab in ["MODEL", "SKETCH", "SURFACE", "MESH", "FORM", "SHEET METAL", "RENDER"] {
let ctx = crate::fonts::test_context();
let mut app = SolveApp::new(Default::default(), Default::default());
app.ui.layout = mode;
app.ui.tab = tab.into();
crate::theme::TabAccent::publish(&app, &ctx);
let accent = crate::theme::TabAccent::for_tab(&Tokens::get(), tab);
let mut row_rect = Rect::NOTHING;
let out = crate::test_support::frame(&ctx, egui::RawInput::default(), |ui| {
let row = Row { selected: true, label: "Selected body", dim: true, color: Some(Tokens::get().error), ..Default::default() };
row_rect = draw_row(ui, egui::Id::new("selected-row"), &row).rect.unwrap();
});
assert_eq!(row_rect.height(), 26.0);
assert!(out.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.rect == row_rect && r.fill == accent.soft)));
assert!(
out.shapes
.iter()
.any(|s| matches!(&s.shape, egui::Shape::Text(t) if t.galley.text() == "Selected body" && t.fallback_color == accent.ink))
);
}
}
}
crate::theme::set_dark(previous);
}
#[test]
fn card_and_column_keep_body_eyes_menus_and_group_drops() {
for mode in [LayoutMode::Floating, LayoutMode::Docked] {
let mut h = Harness::new();
h.app.ui.layout = mode;
for (name, x) in [("One", 0), ("Two", 20)] {
h.app.run("solid.box", json!({"length":10,"width":10,"height":10,"corner":[x,0,0],"operation":"new_body","body_name":name})).unwrap();
}
let group =
h.app.run("browser.group", json!({"component":0,"folder":"bodies","items":["One"],"name":"Group"})).unwrap()["group"].as_u64().unwrap();
h.settle();
h.step(&json!({"click":{"handle":"eye:Two"}})).unwrap();
assert!(h.app.session.visibility.hidden_bodies.iter().any(|name| name == "Two"));
h.step(&json!({"click":{"handle":"eye:Two"}})).unwrap();
assert!(!h.app.session.visibility.hidden_bodies.iter().any(|name| name == "Two"));
h.step(&json!({"click":{"handle":"row:Two"},"button":"right"})).unwrap();
assert!(matches!(&h.app.menu.target, crate::context_menu::Target::Body { name } if name == "Two"));
h.step(&json!({"key":"Escape"})).unwrap();
h.step(&json!({"drag":[{"handle":"row:Two"},{"handle":"row:Group (1)"}]})).unwrap();
h.settle();
let group = h.app.session.doc.browser_groups.iter().find(|g| g.id == group).unwrap();
assert!(group.items.iter().any(|name| name == "Two"), "body drop into the folder failed in {mode:?}");
}
}
#[test]
fn browser_header_folds_the_floating_card_to_its_header_and_back() {
let mut h = Harness::new();
h.call("ui.resize", json!({"width":1400,"height":900}));
h.settle();
let open = rect_of("browser:card").unwrap();
assert!(!h.app.tree.panel_collapsed);
let at = handle_at("browser:header").unwrap();
h.call("ui.click", json!({"x":at.x,"y":at.y}));
h.settle();
let folded = rect_of("browser:card").unwrap();
assert!(h.app.tree.panel_collapsed);
assert!(folded.height() < 60.0 && folded.height() < open.height(), "{folded:?} {open:?}");
assert_eq!(folded.top(), open.top());
assert!(drawn_rows().is_empty() || handle_at("row:Plate").is_none());
h.call("ui.click", json!({"x":at.x,"y":at.y}));
h.settle();
assert!(!h.app.tree.panel_collapsed);
assert!((rect_of("browser:card").unwrap().height() - open.height()).abs() < 1.0);
}
/// Tiny or odd window sizes never panic: the cards leave out what cannot fit.
#[test]
fn tiny_floating_windows_never_panic() {
for (w, h) in [(100, 60), (240, 90), (320, 140), (700, 150), (700, 300), (1400, 120), (180, 800)] {
for scale in [1.0, 2.0] {
let mut harness = Harness::new();
harness.call("ui.resize", json!({"width": w, "height": h}));
harness.call("ui.prefs", json!({"ui_scale": scale}));
harness.app.run("sketch.create", json!({"plane": "XY"})).unwrap();
harness.settle();
for mode in [LayoutMode::Floating, LayoutMode::Docked] {
harness.app.ui.layout = mode;
harness.settle();
}
}
}
}
/// Both shell layouts share one tree and one saved width. Docked can switch its card
/// back into a column; Floating keeps its card when the overlay preference is cleared.
#[test]
fn one_browser_keeps_its_resized_width_across_layouts_and_overlay_preferences() {
let mut h = Harness::new();
h.call("ui.resize", json!({"width":1400,"height":900}));
for mode in [LayoutMode::Floating, LayoutMode::Docked] {
for overlay in [false, true] {
h.app.ui.layout = mode;
h.app.preferences.browser_float = overlay;
h.app.preferences.browser_width = 310.0;
h.settle();
let card = mode == LayoutMode::Floating || overlay;
assert_eq!(rect_of("browser:card").is_some(), card);
assert_eq!(crate::scenario::handles("browser:header").len(), 1, "only one browser header");
assert_eq!(drawn_rows().iter().filter(|(name, ..)| name == "Untitled").count(), 1, "only one tree");
if card {
assert!((rect_of("browser:card").unwrap().width() - 310.0).abs() < 1.0);
} else {
assert_eq!(h.app.viewport.rect.unwrap().left(), 310.0);
}
let rail = handle_at("left-browser-rail").unwrap();
h.call("ui.drag", json!({"x0":rail.x,"y0":rail.y,"x1":rail.x+60.0,"y1":rail.y,"steps":6}));
h.settle();
let width = h.app.preferences.browser_width;
assert!(width > 340.0 && width <= MAX_BROWSER_WIDTH, "{mode:?}, overlay={overlay}: {width}");
h.app.ui.layout = LayoutMode::Docked;
h.app.preferences.browser_float = false;
h.settle();
assert!((h.app.viewport.rect.unwrap().left() - width).abs() < 1.0, "the column uses the card's saved width");
}
}
}
#[test]
fn browser_title_uses_a_single_semibold_run() {
let ctx = crate::fonts::test_context();
let mut app = SolveApp::new(Default::default(), Default::default());
let output = crate::test_support::frame(&ctx, egui::RawInput::default(), |ui| {
ui.set_width(250.0);
browser_header(&mut app, ui, true);
});
let title = crate::i18n::tr("ui.browser_title");
let titles: Vec<_> = output
.shapes
.iter()
.filter_map(|s| match &s.shape {
egui::Shape::Text(text) if text.galley.text() == title => Some(text),
_ => None,
})
.collect();
assert_eq!(titles.len(), 1);
assert_eq!(titles[0].galley.job.sections[0].format.font_id, crate::fonts::semibold(Tokens::get().font_heading));
}
#[test]
fn resizing_browser_card_and_column_persists_width_including_floating_preference() {
for (mode, float, left_history) in [
(LayoutMode::Floating, false, false),
(LayoutMode::Docked, false, false),
(LayoutMode::Docked, true, false),
(LayoutMode::Floating, true, true),
(LayoutMode::Docked, true, true),
] {
let mut h = Harness::new();
h.app.ui.layout = mode;
h.app.preferences.browser_float = float;
h.app.preferences.timeline_left = left_history;
h.call("ui.resize", json!({"width":1400,"height":900}));
h.settle();
let before = h.app.viewport.rect.unwrap();
let at = handle_at("left-browser-rail").unwrap();
h.call("ui.drag", json!({"x0":at.x,"y0":at.y,"x1":at.x+80.0,"y1":at.y,"steps":8}));
h.settle();
let width = h.app.preferences.browser_width;
assert!(width > crate::prefs::DEFAULT_BROWSER_WIDTH + 40.0, "{mode:?}, float={float}, width={width}");
if mode == LayoutMode::Floating || float {
assert!((rect_of("browser:card").unwrap().width() - width).abs() < 1.0);
assert_eq!(h.app.viewport.rect.unwrap(), before, "the card leaves the projection intact");
} else {
assert!((h.app.viewport.rect.unwrap().left() - width).abs() < 1.0);
}
let mut restored = SolveApp::new(Default::default(), Default::default());
restored.load_prefs(&h.app.prefs());
assert_eq!(restored.preferences.browser_width, width);
assert_eq!(restored.preferences.browser_float, float);
}
}
#[test]
fn browser_width_rejects_hostile_preferences_and_tiny_windows_still_fit() {
for width in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
assert_eq!(browser_width(width), crate::prefs::DEFAULT_BROWSER_WIDTH);
}
assert_eq!(browser_width(-100.0), MIN_BROWSER_WIDTH);
assert_eq!(browser_width(f32::MAX), MAX_BROWSER_WIDTH);
let available = Rect::from_min_size(Pos2::ZERO, vec2(100.0, 300.0));
for top_gap in [0.0, crate::shell::SHELL_GAP] {
let bounds = floating_bounds(available, None, MAX_BROWSER_WIDTH, top_gap);
assert!(available.contains_rect(bounds));
}
}