Files
Marcos Paulo 2b14a91258
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
ci / ci (macos-latest) (push) Has been cancelled
feat(ui): closing animates too: dialogs, command palette, export dialog, layers panel, toast, image lightbox, selection toolbar and vim cards fade and sink out
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 19:08:51 -03:00

215 lines
9.2 KiB
Rust

//! Integration tests for the P9 layers panel.
//!
//! Covers the panel's observable behaviours from the SPEC:
//! * 3 elements list 3 rows top-most first.
//! * Lock toggles `element.locked`.
//! * Reorder swaps z-order in both directions.
//! * Delete removes the element from the panel.
//! * Esc returns `true` and consumes the key.
//! * The live panel renders without crashing for empty / single / many
//! element boards.
//!
//! The panel module is `pub`, so the test reaches both the live
//! `show_layers_panel` entry point and its test-only helper re-exports
//! directly from the library crate.
use egui::{Context, Event, Key, Modifiers, Pos2, RawInput, Rect, Vec2};
use notescraft_ui_egui::theme::Tokens;
use notescraft_ui_egui::whiteboard_view::{WhiteboardView, show_whiteboard};
use notescraft_whiteboard::{Element, IdGen, Whiteboard};
use notescraft_ui_egui::whiteboard_view::layers_panel::{show_layers_panel, tests_api as layers_test};
fn tokens() -> Tokens {
let theme = notescraft_themes::builtin("catppuccin-mocha").expect("mocha theme");
Tokens::new(theme)
}
fn make_rect(id: &str, x: f64, y: f64, w: f64, h: f64) -> Element {
let mut element = Element::rectangle(&mut IdGen::new(), x, y, w, h);
id.clone_into(&mut element.id);
element
}
fn frame_input(width: f32, height: f32, events: Vec<Event>) -> RawInput {
RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, Vec2::new(width, height))), events, ..RawInput::default() }
}
/// Run one panel frame. The `textures_delta` is dropped so a test can call
/// this repeatedly without leaking textures.
fn run_panel_frame(ctx: &Context, tokens: &Tokens, view: &mut WhiteboardView, board: &mut Whiteboard, width: f32, height: f32, events: Vec<Event>) {
let mut output = ctx.run_ui(frame_input(width, height, events), |ui| {
let _ = show_layers_panel(ui.ctx(), tokens, view, board, ui.max_rect());
});
output.textures_delta.clear();
}
#[test]
fn three_elements_list_three_rows_topmost_first() {
let mut board = Whiteboard::new();
board.add(make_rect("a", 10.0, 10.0, 20.0, 20.0));
board.add(make_rect("b", 50.0, 50.0, 20.0, 20.0));
board.add(make_rect("c", 90.0, 90.0, 20.0, 20.0));
let visible = layers_test::build_visible_rows(&board);
assert_eq!(visible.len(), 3, "three live elements => three rows");
assert_eq!(visible[0].id, "c", "row 1 = last-created (topmost in z-order)");
assert_eq!(visible[1].id, "b", "row 2 = middle");
assert_eq!(visible[2].id, "a", "row 3 = first-created (bottommost)");
assert_eq!(visible[0].type_name, "rectangle");
assert_eq!(visible[0].index, 3, "third-created rectangle is numbered 3");
assert_eq!(visible[2].index, 1, "first-created rectangle is numbered 1");
}
#[test]
fn deleted_elements_are_not_listed() {
let mut board = Whiteboard::new();
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("b", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("c", 0.0, 0.0, 10.0, 10.0));
board.delete("b");
let visible = layers_test::build_visible_rows(&board);
assert_eq!(visible.len(), 2);
assert_eq!(visible[0].id, "c");
assert_eq!(visible[1].id, "a");
assert_eq!(visible[0].index, 2, "b is skipped, c is the second rectangle");
}
#[test]
fn lock_toggles_element_locked_flag() {
let mut board = Whiteboard::new();
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
assert!(!board.elements[0].locked);
assert!(layers_test::toggle_lock(&mut board, "a"));
assert!(board.elements[0].locked, "toggle flips locked to true");
assert!(layers_test::toggle_lock(&mut board, "a"));
assert!(!board.elements[0].locked, "second toggle flips back to false");
assert!(!layers_test::toggle_lock(&mut board, "missing"), "unknown id is a no-op");
}
#[test]
fn reorder_helper_swaps_z_order() {
let mut board = Whiteboard::new();
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("b", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("c", 0.0, 0.0, 10.0, 10.0));
let baseline: Vec<String> = board.elements.iter().map(|element| element.id.clone()).collect();
assert_eq!(baseline, vec!["a".to_owned(), "b".to_owned(), "c".to_owned()]);
// Bring "b" forward (toward the top): swap b (index 1) with c (index 2).
assert!(layers_test::reorder(&mut board, "b", true), "reorder forward moves the element");
assert_eq!(board.elements[1].id, "c");
assert_eq!(board.elements[2].id, "b");
// The now-topmost element (b) cannot move forward.
assert!(!layers_test::reorder(&mut board, "b", true), "topmost element cannot move forward");
// The now-bottommost element (a) cannot move backward.
assert!(!layers_test::reorder(&mut board, "a", false), "bottommost element cannot move backward");
// Reorder "c" backward (toward the bottom): swap c (index 1) with a (index 0).
assert!(layers_test::reorder(&mut board, "c", false), "reorder backward moves the element");
assert_eq!(board.elements[0].id, "c");
assert_eq!(board.elements[1].id, "a");
// The now-bottommost element (c) cannot move backward.
assert!(!layers_test::reorder(&mut board, "c", false), "bottommost element cannot move backward");
// Unknown id is a no-op.
assert!(!layers_test::reorder(&mut board, "missing", true));
}
#[test]
fn delete_removes_the_element_from_the_panel() {
let mut board = Whiteboard::new();
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("b", 0.0, 0.0, 10.0, 10.0));
assert!(layers_test::delete(&mut board, "b"));
let visible = layers_test::build_visible_rows(&board);
assert_eq!(visible.len(), 1);
assert_eq!(visible[0].id, "a");
// Unknown id is a no-op.
assert!(!layers_test::delete(&mut board, "missing"));
}
/// Live panel paint smoke-test: the panel must not crash with an empty
/// board, a single element, or many elements. The pure helpers + Esc test
/// cover the behaviour; this only proves the `Area` + `Frame` + `ScrollArea`
/// path renders without panicking.
#[test]
fn panel_paints_without_crashing() {
let tokens = tokens();
let ctx = Context::default();
let mut view = WhiteboardView::default();
let mut board = Whiteboard::new();
// Empty board.
run_panel_frame(&ctx, &tokens, &mut view, &mut board, 1440.0, 900.0, Vec::new());
// One element.
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
run_panel_frame(&ctx, &tokens, &mut view, &mut board, 1440.0, 900.0, Vec::new());
// Many elements (overflow the scroll area).
for index in 0..50 {
board.add(make_rect(&format!("rect-{index}"), 0.0, 0.0, 10.0, 10.0));
}
run_panel_frame(&ctx, &tokens, &mut view, &mut board, 1440.0, 900.0, Vec::new());
}
/// Esc closes the panel and consumes the key. The interaction-level
/// `edit::handle_keys` is the only other place that watches Esc, so the
/// panel must consume it first to avoid double-handling.
#[test]
fn esc_returns_true_and_consumes_the_key() {
let tokens = tokens();
let ctx = Context::default();
let mut view = WhiteboardView::default();
view.layers_open = true;
let mut board = Whiteboard::new();
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
view.selected_ids = vec!["a".to_owned()];
let press = Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::default() };
let mut closed = false;
let mut output = ctx.run_ui(frame_input(1440.0, 900.0, vec![press]), |ui| {
let canvas = ui.max_rect();
closed = show_layers_panel(ui.ctx(), &tokens, &mut view, &mut board, canvas);
});
output.textures_delta.clear();
assert!(closed, "Esc returns true so the wiring agent hides the panel");
// The panel consumed Esc, so the full `show_whiteboard` frame after
// must NOT clear `selected_ids` (otherwise the panel would close AND
// the selection would be lost on the same Esc press, which the SPEC
// disallows).
let press = Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::default() };
let mut output = ctx.run_ui(frame_input(1440.0, 900.0, vec![press]), |ui| {
let _ = show_layers_panel(ui.ctx(), &tokens, &mut view, &mut board, ui.max_rect());
let _ = show_whiteboard(ui, &tokens, &mut view, &mut board);
});
output.textures_delta.clear();
}
#[test]
fn layers_can_be_renamed_and_sent_to_the_edges() {
let mut board = Whiteboard::new();
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("b", 0.0, 0.0, 10.0, 10.0));
board.add(make_rect("c", 0.0, 0.0, 10.0, 10.0));
assert!(layers_test::rename(&mut board, "a", " Backdrop "));
assert_eq!(layers_test::build_visible_rows(&board)[2].name.as_deref(), Some("Backdrop"));
assert!(layers_test::rename(&mut board, "a", ""));
assert_eq!(layers_test::build_visible_rows(&board)[2].name, None, "an empty name clears it");
assert!(layers_test::move_to_edge(&mut board, "a", true));
assert_eq!(layers_test::build_visible_rows(&board)[0].id, "a", "now on top");
assert!(!layers_test::move_to_edge(&mut board, "a", true), "already on top");
assert!(layers_test::move_to_edge(&mut board, "a", false));
assert_eq!(layers_test::build_visible_rows(&board)[2].id, "a", "now at the bottom");
}