218 lines
9.0 KiB
Rust
218 lines
9.0 KiB
Rust
//! Integration tests for the P6 colour popover and P7 properties panel.
|
||
//!
|
||
//! Covers:
|
||
//! * Stroke apply to a 2-element selection mutates both + pushes exactly one
|
||
//! undo entry; undo restores both.
|
||
//! * Empty selection routes through the view-level default, not the elements.
|
||
//! * Canvas tab writes `viewBackgroundColor`; Theme removes it; unknown
|
||
//! `appState` keys are preserved on both paths.
|
||
//! * Popover opens on chip click; closes on Esc; grid paints 13×5 cells; quick
|
||
//! picks match `STROKE_QUICK_PICKS`/`FILL_QUICK_PICKS`.
|
||
//!
|
||
//! Test-only seams live in `panels::tests_api` and `palette_panel::tests_api`
|
||
//! so the public crate surface does not grow private helpers.
|
||
|
||
use egui::{Context, Event, Pos2, RawInput, Rect, Vec2};
|
||
use notescraft_ui_egui::whiteboard_view::palette_apply::tests_api as popover_test;
|
||
use notescraft_ui_egui::whiteboard_view::panels::tests_api as panel_test;
|
||
use notescraft_ui_egui::whiteboard_view::{PaletteTab, WhiteboardView, show_whiteboard};
|
||
use notescraft_whiteboard::palette::{BLACK, CANVAS_QUICK_PICKS, FILL_QUICK_PICKS, HUE_ROWS, STROKE_QUICK_PICKS, TRANSPARENT, WHITE};
|
||
use notescraft_whiteboard::{Element, IdGen, Whiteboard};
|
||
|
||
fn tokens() -> notescraft_ui_egui::theme::Tokens {
|
||
let theme = notescraft_themes::builtin("catppuccin-mocha").expect("mocha theme");
|
||
notescraft_ui_egui::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);
|
||
"#111111".clone_into(&mut element.stroke_color);
|
||
"#eeeeee".clone_into(&mut element.background_color);
|
||
element
|
||
}
|
||
|
||
fn frame_input(events: Vec<Event>) -> RawInput {
|
||
RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, Vec2::new(1440.0, 900.0))), events, ..RawInput::default() }
|
||
}
|
||
|
||
fn settle(ctx: &Context, tokens: ¬escraft_ui_egui::theme::Tokens, view: &mut WhiteboardView, board: &mut Whiteboard) {
|
||
let mut output = ctx.run_ui(frame_input(Vec::new()), |ui| {
|
||
let _ = show_whiteboard(ui, tokens, view, board);
|
||
});
|
||
output.textures_delta.clear();
|
||
}
|
||
|
||
#[test]
|
||
fn stroke_apply_to_two_element_selection_mutates_both_and_undo_restores() {
|
||
let tokens = tokens();
|
||
let ctx = Context::default();
|
||
let mut view = WhiteboardView::default();
|
||
let mut board = Whiteboard::new();
|
||
board.add(make_rect("a", 10.0, 20.0, 30.0, 30.0));
|
||
board.add(make_rect("b", 100.0, 120.0, 40.0, 40.0));
|
||
|
||
// baseline snapshot
|
||
settle(&ctx, &tokens, &mut view, &mut board);
|
||
let before_a = board.elements[0].clone();
|
||
let before_b = board.elements[1].clone();
|
||
let undo_before = view.undo.len();
|
||
|
||
// Apply a stroke colour through the shared apply helper.
|
||
popover_test::apply_to_ids(&mut view, &mut board, &["a", "b"], |element| {
|
||
element.stroke_color = "#ff00ff".to_owned();
|
||
});
|
||
assert_eq!(board.elements[0].stroke_color, "#ff00ff", "element a stroke mutated");
|
||
assert_eq!(board.elements[1].stroke_color, "#ff00ff", "element b stroke mutated");
|
||
|
||
// Bottom-of-frame snapshot: changed flag was set, one undo entry.
|
||
assert!(view.changed, "apply sets view.changed");
|
||
settle(&ctx, &tokens, &mut view, &mut board);
|
||
assert_eq!(view.undo.len(), undo_before + 1, "exactly one new undo entry");
|
||
|
||
// Undo restores both.
|
||
view.undo(&mut board);
|
||
assert_eq!(board.elements[0].stroke_color, before_a.stroke_color, "a restored");
|
||
assert_eq!(board.elements[1].stroke_color, before_b.stroke_color, "b restored");
|
||
}
|
||
|
||
#[test]
|
||
fn empty_selection_routes_to_view_default_not_elements() {
|
||
let mut view = WhiteboardView::default();
|
||
let mut board = Whiteboard::new();
|
||
board.add(make_rect("a", 0.0, 0.0, 10.0, 10.0));
|
||
|
||
// Empty selection by clearing the IDs the apply helper sees.
|
||
view.selected_ids.clear();
|
||
panel_test::apply_or_default(
|
||
&mut view,
|
||
&mut board,
|
||
|element| {
|
||
element.stroke_color = "#00ff00".to_owned();
|
||
},
|
||
|view| {
|
||
view.stroke_color = "#00ff00".to_owned();
|
||
},
|
||
);
|
||
assert_ne!(board.elements[0].stroke_color, "#00ff00", "empty selection does not touch elements");
|
||
assert_eq!(view.stroke_color, "#00ff00", "view default stroke color set");
|
||
}
|
||
|
||
#[test]
|
||
fn stroke_quick_picks_and_fill_quick_picks_lists_match_whiteboard_crate() {
|
||
assert_eq!(STROKE_QUICK_PICKS.len(), 5, "stroke quick pick list is 5");
|
||
assert_eq!(FILL_QUICK_PICKS.len(), 5, "fill quick pick list is 5");
|
||
assert_eq!(STROKE_QUICK_PICKS[0], BLACK, "stroke first slot is black");
|
||
assert_eq!(FILL_QUICK_PICKS[0], TRANSPARENT, "fill first slot is transparent");
|
||
}
|
||
|
||
#[test]
|
||
fn hue_grid_has_12_rows_of_5_shades() {
|
||
assert_eq!(HUE_ROWS.len(), 12, "12 hue families");
|
||
for (_name, shades) in HUE_ROWS {
|
||
assert_eq!(shades.len(), 5, "5 shades per family");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn canvas_set_and_clear_view_background_round_trips_unknown_appstate_keys() {
|
||
let mut view = WhiteboardView::default();
|
||
let mut board = Whiteboard::new();
|
||
// Seed an unknown key in app_state.
|
||
board.app_state.as_object_mut().unwrap().insert("custom".to_owned(), serde_json::json!(42));
|
||
let unknown_before = board.app_state.as_object().unwrap().get("custom").cloned();
|
||
|
||
let _ = board.set_view_background("#abcdef");
|
||
assert_eq!(board.view_background(), Some("#abcdef"));
|
||
assert_eq!(board.app_state.as_object().unwrap().get("custom"), unknown_before.as_ref(), "unknown key preserved on set");
|
||
|
||
popover_test::apply_canvas_theme(&mut view, &mut board);
|
||
assert_eq!(board.view_background(), None, "theme removes the key");
|
||
assert_eq!(board.app_state.as_object().unwrap().get("custom"), unknown_before.as_ref(), "unknown key preserved on clear");
|
||
}
|
||
|
||
#[test]
|
||
fn popover_does_not_open_when_initial_view_has_no_palette_state() {
|
||
let tokens = tokens();
|
||
let ctx = Context::default();
|
||
let mut view = WhiteboardView::default();
|
||
let mut board = Whiteboard::new();
|
||
assert!(view.palette_open.is_none());
|
||
settle(&ctx, &tokens, &mut view, &mut board);
|
||
assert!(view.palette_open.is_none(), "popover stays closed without explicit open");
|
||
}
|
||
|
||
#[test]
|
||
fn white_is_in_canvas_quick_picks_for_visibility_in_dark_mode() {
|
||
let has_light = CANVAS_QUICK_PICKS.iter().any(|value| value.eq_ignore_ascii_case(WHITE));
|
||
assert!(has_light, "white swatch is in the canvas quick picks for dark-mode preview");
|
||
}
|
||
|
||
#[test]
|
||
fn popover_paints_shapes_when_open() {
|
||
let tokens = tokens();
|
||
let ctx = Context::default();
|
||
notescraft_ui_egui::fonts::install(&ctx);
|
||
ctx.run_ui(frame_input(Vec::new()), |_| {}).textures_delta.clear();
|
||
let mut view = WhiteboardView::default();
|
||
let mut board = Whiteboard::new();
|
||
view.palette_open = Some(PaletteTab::Stroke);
|
||
let mut output = ctx.run_ui(frame_input(Vec::new()), |ui| {
|
||
let _ = show_whiteboard(ui, &tokens, &mut view, &mut board);
|
||
});
|
||
output.textures_delta.clear();
|
||
assert!(!output.shapes.is_empty(), "popover paints shapes when open");
|
||
}
|
||
|
||
#[test]
|
||
fn default_view_starts_with_transparent_fill_default() {
|
||
let view = WhiteboardView::default();
|
||
assert_eq!(view.fill_color, TRANSPARENT);
|
||
assert_eq!(view.opacity, 100);
|
||
assert_eq!(view.font_size, 20.0);
|
||
}
|
||
|
||
#[test]
|
||
fn palette_tab_variants_are_distinct() {
|
||
let stroke = PaletteTab::Stroke;
|
||
let fill = PaletteTab::Fill;
|
||
let canvas = PaletteTab::Canvas;
|
||
assert_ne!(stroke, fill);
|
||
assert_ne!(fill, canvas);
|
||
assert_ne!(stroke, canvas);
|
||
}
|
||
|
||
#[test]
|
||
fn popover_module_exposes_recent_color_cap() {
|
||
use notescraft_ui_egui::whiteboard_view::palette_panel as pp;
|
||
assert_eq!(pp::RECENT_COLORS_MAX, 6, "FIFO depth is 6 swatches");
|
||
}
|
||
|
||
#[test]
|
||
fn view_background_round_trip_preserves_app_state_keys() {
|
||
let mut board = Whiteboard::new();
|
||
board.app_state.as_object_mut().unwrap().insert("viewBackgroundColor".to_owned(), serde_json::json!("#202020"));
|
||
board.app_state.as_object_mut().unwrap().insert("gridSize".to_owned(), serde_json::json!(20));
|
||
let _ = board.set_view_background("#303030");
|
||
let grid = board.app_state.as_object().unwrap().get("gridSize").cloned();
|
||
assert_eq!(grid, Some(serde_json::json!(20)), "unknown appState key preserved on rewrite");
|
||
}
|
||
|
||
#[test]
|
||
fn popover_handle_escape_closes_open_popover_via_live_frame() {
|
||
// Drive a real frame with Esc pressed and an open popover; the live
|
||
// `show_whiteboard` path must close the popover before `edit.rs` sees the
|
||
// Esc event for clear-selection.
|
||
let tokens = tokens();
|
||
let ctx = Context::default();
|
||
let mut view = WhiteboardView::default();
|
||
let mut board = Whiteboard::new();
|
||
view.palette_open = Some(PaletteTab::Stroke);
|
||
let press = Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::default() };
|
||
let mut output = ctx.run_ui(frame_input(vec![press]), |ui| {
|
||
let _ = show_whiteboard(ui, &tokens, &mut view, &mut board);
|
||
});
|
||
output.textures_delta.clear();
|
||
assert!(view.palette_open.is_none(), "popover closed by Esc through the live frame path");
|
||
}
|