5a24289898fd
compare.rs
//! Headless tests of Compare with the filmstrip.
use std::time::Duration;
use serde_json::json;
use crate::headless::Headless;
use crate::state::Zoom;
use crate::{LightcraftApp, Services};
const T: Duration = Duration::from_secs(20);
const SETTLE: Duration = Duration::from_secs(120);
fn demo() -> Headless {
let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() });
Headless::new(app, [1400.0, 900.0], 1.0)
}
/// In Compare, the photo the Edit panel and the rating keys act on is always one of the two on
/// screen: a filmstrip click must not leave the pair showing while a third photo becomes active.
#[test]
fn filmstrip_click_in_compare_keeps_the_active_photo_on_screen() {
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
let rating = |h: &Headless, id: u64| h.app.session.catalog.photo(lightcraft_catalog::PhotoId(id)).map(|p| p.rating);
for zoom in [Zoom::Fit, Zoom::Percent(100.0)] {
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
let r = h.request("engine.execute", json!({"command": "view.compare"}), T);
assert_eq!(r["ok"], true, "{r}");
h.app.ui.zoom = zoom;
h.settle(SETTLE);
let target = vis[4];
let before = rating(&h, target);
let r = h.request("ui.clickWidget", json!({"id": format!("film:{target}")}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
let (sel, cand) = h.app.ui.compare.expect("still comparing");
let active = h.app.session.selection.active.map(|p| p.0).expect("an active photo");
// a rating key must not change a photo that isn't shown
h.request("ui.key", json!({"key": "3"}), T);
if target != sel && target != cand {
assert_eq!(rating(&h, target), before, "zoom {zoom:?}: the hidden photo {target} was rated");
}
assert!(active == sel || active == cand, "zoom {zoom:?}: active photo {active} is not on screen (compare shows {sel} | {cand})");
}
}
/// A filmstrip click in Compare puts the photo into the active side: the candidate after
/// entering Compare, the select after clicking the select's pane; a photo already on the other
/// side swaps the two. `compare.set` does the same for agents, with an explicit `side`.
#[test]
fn filmstrip_click_replaces_the_active_side_in_compare() {
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
let r = h.request("engine.execute", json!({"command": "view.compare"}), T);
let (sel, cand) = (r["result"]["select"].as_u64().unwrap(), r["result"]["candidate"].as_u64().unwrap());
h.settle(SETTLE);
let click_film = |h: &mut Headless, id: u64| {
let r = h.request("ui.clickWidget", json!({"id": format!("film:{id}")}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
};
let active = |h: &Headless| h.app.session.selection.active.map(|p| p.0);
let others: Vec<u64> = vis.iter().copied().filter(|v| *v != sel && *v != cand).take(3).collect();
// the candidate is active after entering Compare: it is replaced
click_film(&mut h, others[0]);
assert_eq!(h.app.ui.compare, Some((sel, others[0])));
assert_eq!(active(&h), Some(others[0]));
// the select's photo, clicked in the filmstrip: the two swap, it stays active
click_film(&mut h, sel);
assert_eq!(h.app.ui.compare, Some((others[0], sel)));
assert_eq!(active(&h), Some(sel));
// a side chosen explicitly
let r = h.request("engine.execute", json!({"command": "compare.set", "params": {"id": others[1], "side": "select"}}), T);
assert_eq!(r["ok"], true, "{r}");
assert_eq!(h.app.ui.compare, Some((others[1], sel)));
assert_eq!(active(&h), Some(others[1]));
// the select is active now: the next filmstrip click replaces it
click_film(&mut h, others[2]);
assert_eq!(h.app.ui.compare, Some((others[2], sel)));
let r = h.request("engine.execute", json!({"command": "compare.set", "params": {"id": others[0], "side": "left"}}), T);
assert_eq!(r["ok"], false, "unknown side is an error: {r}");
}
/// A β/Ctrl-click or Shift-click on the filmstrip in Compare changes the selection as it does in
/// the grid (#187): the photo is toggled, or the range from the active photo is added. The two
/// photos shown stay selected and the active one stays active, so a rating key still acts on a
/// photo that is on screen (#744).
#[test]
fn modified_filmstrip_clicks_in_compare_change_the_selection_not_the_pair() {
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
let r = h.request("engine.execute", json!({"command": "view.compare"}), T);
let (sel, cand) = (r["result"]["select"].as_u64().unwrap(), r["result"]["candidate"].as_u64().unwrap());
h.settle(SETTLE);
let click_film = |h: &mut Headless, id: u64, cmd: bool, shift: bool| {
let r = h.request("ui.clickWidget", json!({"id": format!("film:{id}"), "cmd": cmd, "shift": shift}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
};
let selected = |h: &Headless| -> Vec<u64> {
let s = &h.app.session.selection;
vis.iter().copied().filter(|v| s.contains(lightcraft_catalog::PhotoId(*v))).collect()
};
let active = |h: &Headless| h.app.session.selection.active.map(|p| p.0);
let rating = |h: &Headless, id: u64| h.app.session.catalog.photo(lightcraft_catalog::PhotoId(id)).map(|p| p.rating);
let others: Vec<u64> = vis.iter().copied().filter(|v| *v != sel && *v != cand).collect();
assert!(others.len() >= 4, "the demo library has {} photos", vis.len());
let before = active(&h);
// β-click: a third photo joins the selection; the pair and the active photo stay
click_film(&mut h, others[0], true, false);
assert_eq!(selected(&h), {
let mut v = vec![sel, cand, others[0]];
v.sort_by_key(|id| vis.iter().position(|v| v == id));
v
});
assert_eq!(h.app.ui.compare, Some((sel, cand)), "the pair is untouched");
assert_eq!(active(&h), before, "the active photo is untouched");
// β-click again: it leaves
click_film(&mut h, others[0], true, false);
assert_eq!(selected(&h), {
let mut v = vec![sel, cand];
v.sort_by_key(|id| vis.iter().position(|v| v == id));
v
});
assert_eq!(h.app.ui.compare, Some((sel, cand)));
assert_eq!(active(&h), before);
// Shift-click: everything from the active photo to the clicked one is selected
let far = others[3];
click_film(&mut h, far, false, true);
let (a, b) = (vis.iter().position(|v| Some(*v) == before).unwrap(), vis.iter().position(|v| *v == far).unwrap());
let now = selected(&h);
for id in &vis[a.min(b)..=a.max(b)] {
assert!(now.contains(id), "{id} is in the range {before:?}..{far}: {now:?}");
}
assert!(now.contains(&sel) && now.contains(&cand), "the pair stays selected: {now:?}");
assert_eq!(h.app.ui.compare, Some((sel, cand)));
assert_eq!(active(&h), before, "Shift-click does not move the active photo in Compare");
// β-click on a photo that is on screen: it cannot leave the selection while it is compared
click_film(&mut h, sel, true, false);
assert!(selected(&h).contains(&sel), "the select stays selected");
assert_eq!(h.app.ui.compare, Some((sel, cand)));
assert_eq!(active(&h), before);
// β-click on the active photo: `toggle` would hand "active" to another photo; it stays put
click_film(&mut h, before.unwrap(), true, false);
assert!(selected(&h).contains(&before.unwrap()), "the active photo stays selected");
assert_eq!(h.app.ui.compare, Some((sel, cand)));
assert_eq!(active(&h), before);
// a rating key acts on the active photo, which is on screen; a merely selected photo is left alone
let was = rating(&h, far);
h.request("ui.key", json!({"key": "3"}), T);
h.settle(SETTLE);
assert_eq!(rating(&h, before.unwrap()), Some(3));
assert_eq!(rating(&h, far), was, "the hidden photo was rated");
// a plain click still replaces the active side
click_film(&mut h, others[1], false, false);
assert_ne!(h.app.ui.compare, Some((sel, cand)));
assert!(h.app.ui.compare.is_some_and(|(s, c)| s == others[1] || c == others[1]));
}
/// Compare is where sharpness is judged: while a pane's render of an edit is on its way, that pane
/// says "Renderingβ¦" once it has lasted a moment, and stops when the render lands.
#[test]
fn a_compare_pane_says_rendering_while_its_edit_renders() {
use std::{cell::Cell, rc::Rc};
let pill = |h: &Headless| h.app.widgets.iter().any(|(w, _)| w.starts_with("compare:rendering:"));
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
let r = h.request("engine.execute", json!({"command": "view.compare"}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
assert!(!pill(&h), "finished renders");
let open = Rc::new(Cell::new(false));
h.app.renderer.set_offload(Box::new(crate::headless::RenderGate::new(open.clone())));
h.request("engine.execute", json!({"command": "develop.set", "params": {"control": "light.exposure", "value": 0.5}}), T);
for _ in 0..12 {
h.step();
}
assert!(!pill(&h), "not at once");
for _ in 0..30 {
h.step();
}
assert!(pill(&h), "the edited pane's render is on its way");
open.set(true);
assert!(h.step_until(SETTLE, |h| !pill(h)), "gone once the render lands");
}
/// A pane whose render fails behind its thumbnail says so, as the loupe does, instead of showing
/// the thumbnail as if it were the photo.
#[test]
fn a_compare_pane_says_when_its_render_failed() {
use crate::render::Slot;
use std::{cell::Cell, rc::Rc};
let failed = |h: &Headless| h.app.widgets.iter().any(|(w, _)| w.starts_with("compare:renderFailed:"));
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
h.request("engine.execute", json!({"command": "view.compare"}), T);
h.settle(SETTLE);
assert!(!failed(&h));
let open = Rc::new(Cell::new(true));
h.app.renderer.set_offload(Box::new(crate::headless::RenderGate::failing(open, |s| matches!(s, Slot::Compare(_)))));
// another photo in the second pane: its render fails, its thumbnail stays up
let r = h.request("engine.execute", json!({"command": "compare.set", "params": {"id": vis[2], "side": "candidate"}}), T);
assert_eq!(r["ok"], true, "{r}");
assert!(h.step_until(SETTLE, failed), "the failure is said");
assert!(!h.app.widgets.iter().any(|(w, _)| w.starts_with("compare:rendering:")));
}
#[test]
fn reference_escape_leaves_overlays_then_the_view_without_changing_edits() {
use crate::state::{LightsOut, ViewMode};
let mut h = demo();
let reference = h.app.session.visible_cloned()[0];
h.app.run("library.select", json!({"ids": [reference.0]})).unwrap();
h.app.run("view.reference", json!({})).unwrap();
let active = h.app.session.active().unwrap();
h.app.run("develop.set", json!({"control": "light.exposure", "value": 0.7})).unwrap();
h.app.ui.lights_out = LightsOut::Dim;
h.request("ui.key", json!({"key": "Escape"}), T);
assert_eq!((h.app.ui.view, h.app.ui.lights_out), (ViewMode::Reference, LightsOut::On));
h.request("ui.key", json!({"key": "Escape"}), T);
assert_eq!(h.app.ui.view, ViewMode::Detail);
assert_eq!(h.app.ui.reference, Some(reference.0), "leaving the view keeps the pinned photo");
assert_eq!(h.app.session.active(), Some(active));
assert_eq!(h.app.session.develop_of(active).unwrap().light.exposure, 0.7);
h.request("ui.key", json!({"key": "Escape"}), T);
assert_eq!(h.app.ui.view, ViewMode::PhotoGrid);
}
#[test]
fn invalid_or_deleted_reference_is_refused_without_replacing_the_pin() {
let mut h = demo();
let ids = h.app.session.visible_cloned();
h.app.run("library.select", json!({"ids": [ids[0].0]})).unwrap();
h.app.run("photo.setReference", json!({})).unwrap();
h.app.run("photo.delete", json!({"ids": [ids[1].0]})).unwrap();
for id in [u64::MAX, ids[1].0] {
let r = h.app.run("photo.setReference", json!({"id": id}));
assert!(r.is_err(), "unavailable photo {id} must not be pinned");
assert_eq!(h.app.ui.reference, Some(ids[0].0));
}
let deleted = h.app.run("photo.setReference", json!({"id": ids[1].0})).unwrap_err();
assert!(deleted.contains("Recently Deleted"), "the caller learns to restore it: {deleted}");
// a malformed id is refused, not taken for "the active photo"
h.app.run("library.select", json!({"ids": [ids[2].0]})).unwrap();
for id in [json!(ids[2].0.to_string()), json!(-1), json!(1.5), json!([ids[2].0])] {
assert!(h.app.run("photo.setReference", json!({"id": id})).is_err(), "{id}");
assert_eq!(h.app.ui.reference, Some(ids[0].0));
}
h.app.run("photo.setReference", json!({"id": null})).unwrap();
assert_eq!(h.app.ui.reference, Some(ids[2].0), "a null id means the active photo");
h.app.run("photo.setReference", json!({"id": ids[0].0})).unwrap();
h.app.run("library.select", json!({"ids": []})).unwrap();
assert!(h.app.run("photo.setReference", json!({})).is_err());
assert_eq!(h.app.ui.reference, Some(ids[0].0));
}
#[test]
fn deleting_the_pinned_reference_stops_drawing_it_and_reentry_uses_a_live_photo() {
let mut h = demo();
let reference = h.app.session.visible_cloned()[0];
h.app.run("library.select", json!({"ids": [reference.0]})).unwrap();
h.app.run("view.reference", json!({})).unwrap();
h.settle(SETTLE);
let texture = h.app.renderer.textures.get(&crate::render::Slot::Compare(0)).unwrap().tex.id();
let drawn = |h: &Headless| h.view.painted_shapes().iter().any(|s| matches!(s, egui::epaint::Shape::Mesh(mesh) if mesh.texture_id == texture));
assert!(drawn(&h), "the live reference is shown before deletion");
let active = h.app.session.active().unwrap();
h.app.run("photo.delete", json!({"ids": [reference.0]})).unwrap();
h.settle(SETTLE);
assert!(!drawn(&h), "Recently Deleted photos must not remain on the reference canvas");
assert!(!h.app.renderer.textures.contains_key(&crate::render::Slot::Compare(0)), "the deleted reference's pane is released");
assert!(!h.app.renderer.textures.contains_key(&crate::render::Slot::Compare(1)), "and the active photo's pane beside it");
assert!(h.app.compare_windows[0].is_none());
let r = h.app.run("view.reference", json!({})).unwrap();
assert_eq!(r["reference"], active.0);
assert_eq!(h.app.ui.reference, Some(active.0));
h.app.run("photo.restore", json!({"ids": [reference.0]})).unwrap();
h.app.run("photo.setReference", json!({"id": reference.0})).unwrap();
h.settle(SETTLE);
assert!(h.app.renderer.textures.get(&crate::render::Slot::Compare(0)).is_some_and(|t| t.photo == reference));
}
/// The zoom window is checked against the reference frame the request was made for: a window is a
/// part of the frame, so its bounds are the window's pixels laid over that frame, not over whatever
/// rect the whole-frame texture happens to be fitted into. In the candidate pane that frame is the
/// one recorded for the pane: the tile is laid over it, so any other frame rect is the texture's.
#[test]
fn compare_zoom_window_tracks_the_zoomed_pane() {
use crate::render::Slot;
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
let r = h.request("engine.execute", json!({"command": "view.compare"}), T);
assert_eq!(r["ok"], true, "{r}");
h.app.ui.zoom = Zoom::Percent(100.0);
h.settle(super::ui_settle_timeout());
let candidate = vis[1];
let photo = h.app.session.catalog.photo(lightcraft_catalog::PhotoId(candidate)).cloned().expect("photo");
let frame = lightcraft_pipeline::geometry::Frame::with_lens(
photo.width.max(1) as usize,
photo.height.max(1) as usize,
&photo.develop,
true,
photo.embedded_lens.as_ref(),
);
let aspect = frame.aspect() as f32;
let native = crate::panels::detail::output_px(&frame);
let view = h.app.compare_windows[1].expect("a zoom window");
assert_eq!(view.photo, lightcraft_catalog::PhotoId(candidate));
let tex = h.app.renderer.textures.get(&Slot::CompareRegion(1)).expect("window texture");
assert_eq!(tex.photo, lightcraft_catalog::PhotoId(candidate));
let tex_id = tex.tex.id();
// Lay the whole-frame texture over the same reference frame with an aspect just inside the
// same-frame tolerance: the drawn rect moves, the request stays valid, and the window is
// drawn over the moved texture rect. Hold the replacement renders so the existing window is
// the one drawn, magnified, over the texture.
let texture_size = {
let whole = h.app.renderer.textures.get_mut(&Slot::Compare(1)).expect("whole-frame texture");
let target_aspect = aspect * 1.015;
whole.size[1] = (whole.size[0] as f32 / target_aspect).round().max(8.0) as usize;
whole.size
};
let texture_aspect = texture_size[0].max(1) as f32 / texture_size[1].max(1) as f32;
assert!(crate::region::same_aspect(texture_aspect, aspect), "a trace off the frame's aspect stays in tolerance: {texture_aspect} {}", aspect);
let open = std::rc::Rc::new(std::cell::Cell::new(false));
h.app.renderer.set_offload(Box::new(crate::headless::RenderGate::new(open.clone())));
h.app.session.begin_interaction("Exposure").unwrap();
h.step();
let pane_img = h.app.image_rect.expect("the candidate pane's frame");
let img_area = egui::Rect::from_min_max(pane_img.min, egui::pos2(pane_img.right(), pane_img.bottom()));
let img = pane_img;
let display = crate::panels::detail::fit_rect(img_area, texture_aspect, h.app.ui.zoom, native, 1.0, h.app.ui.pan);
let expected = egui::Rect::from_min_size(
img.min + egui::vec2(view.window.x as f32 / view.full.0 as f32 * img.width(), view.window.y as f32 / view.full.1 as f32 * img.height()),
egui::vec2(view.window.w as f32 / view.full.0 as f32 * img.width(), view.window.h as f32 / view.full.1 as f32 * img.height()),
);
let wrong = egui::Rect::from_min_size(
display.min
+ egui::vec2(view.window.x as f32 / view.full.0 as f32 * display.width(), view.window.y as f32 / view.full.1 as f32 * display.height()),
egui::vec2(view.window.w as f32 / view.full.0 as f32 * display.width(), view.window.h as f32 / view.full.1 as f32 * display.height()),
);
assert!(
(wrong.min - expected.min).abs().max_elem() > 3.0,
"the frame rect and the texture rect must differ for this check: {img:?} vs {display:?}"
);
let drawn: Vec<egui::Rect> = h
.view
.shapes
.iter()
.filter_map(|c| match &c.shape {
egui::epaint::Shape::Mesh(m) if m.texture_id == tex_id => Some(m.calc_bounds()),
_ => None,
})
.collect();
let bounds = drawn.iter().copied().find(|b| (b.min - wrong.min).abs().max_elem() < 1.0).or_else(|| drawn.first().copied());
let bounds = bounds.expect("the window tile is painted");
assert!(
(bounds.min - expected.min).abs().max_elem() < 1.0 && (bounds.max - expected.max).abs().max_elem() < 1.0,
"the tile {bounds:?} must sit in the zoomed pane's frame {expected:?}, not in the texture's rect {wrong:?}"
);
}
/// Clicking a colour label (or stars) in the Info panel while culling labels the active photo
/// only, as the label keys and the bottom bar's stars and flags do, and the label shows under that
/// photo in Compare (#2398: "Can't assign color to photo when culling").
#[test]
fn info_panel_label_in_compare_labels_the_active_photo_and_shows_it() {
use lightcraft_catalog::{ColorLabel, PhotoId};
let mut h = demo();
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
for id in &vis[..2] {
h.request("engine.execute", json!({"command": "photo.label", "params": {"label": "none", "ids": [id]}}), T);
h.request("engine.execute", json!({"command": "photo.rate", "params": {"rating": 0, "ids": [id]}}), T);
}
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1]], "active": vis[0]}}), T);
let r = h.request("engine.execute", json!({"command": "view.compare"}), T);
assert_eq!(r["ok"], true, "{r}");
let (sel, cand) = (r["result"]["select"].as_u64().unwrap(), r["result"]["candidate"].as_u64().unwrap());
h.app.ui.right = crate::state::RightPanel::Info;
h.settle(SETTLE);
let photo = |h: &Headless, id: u64| h.app.session.catalog.photo(PhotoId(id)).cloned().expect("photo");
let widgets = |h: &mut Headless| h.request("ui.widgets", json!({}), T)["result"].to_string();
assert!(!widgets(&mut h).contains(&format!("cull:{cand}:label")), "no label yet");
let r = h.request("ui.clickWidget", json!({"id": "label:red"}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
assert_eq!(photo(&h, cand).label, Some(ColorLabel::Red), "the active photo (the candidate) is labelled");
assert_eq!(photo(&h, sel).label, None, "the select is not labelled with it");
assert!(widgets(&mut h).contains(&format!("cull:{cand}:label")), "the label shows under the candidate");
let r = h.request("ui.clickWidget", json!({"id": "star:info:3"}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
assert_eq!(photo(&h, cand).rating, 3);
assert_eq!(photo(&h, sel).rating, 0, "the select is not rated with it");
}
/// Survey frames its active tile in the theme's active ring, which must not be the white that
/// vanishes on a light theme's canvas.
#[test]
fn survey_rings_the_active_tile_in_the_theme_colour() {
use egui::epaint::Shape;
let mut h = demo();
h.app.ui.settings.appearance_mode = crate::state::AppearanceMode::Light;
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": [vis[0], vis[1], vis[2]], "active": vis[1]}}), T);
let r = h.request("engine.execute", json!({"command": "view.survey"}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
let t = crate::theme::Tokens::get(&h.view.ctx);
assert_ne!(t.ring_active, egui::Color32::WHITE);
// the filmstrip below rings its active cell the same way: only the tile counts here
let films: Vec<egui::Rect> = h.app.widgets.iter().filter(|(w, _)| w.starts_with("film:")).map(|(_, r)| *r).collect();
let rings: Vec<(f32, egui::Color32)> = h
.view
.painted_shapes()
.iter()
.filter_map(|s| match s {
Shape::Rect(rs)
if rs.stroke_kind == egui::StrokeKind::Outside
&& rs.stroke.color == t.ring_active
&& !films.iter().any(|f| f.intersects(rs.rect)) =>
{
Some((rs.stroke.width, rs.stroke.color))
}
_ => None,
})
.collect();
assert_eq!(rings, vec![(3.0, t.ring_active)], "one active tile, ringed once");
let white = h
.view
.painted_shapes()
.iter()
.filter(|s| matches!(s, Shape::Rect(rs) if rs.stroke.width == 2.0 && rs.stroke.color == egui::Color32::WHITE))
.count();
assert_eq!(white, 0, "no white 2 pt frame");
}
/// Survey with three photos selected and the second active; returns the three ids.
fn survey_of_three(h: &mut Headless) -> [u64; 3] {
let vis: Vec<u64> = h.app.session.visible_cloned().iter().map(|p| p.0).collect();
let ids = [vis[0], vis[1], vis[2]];
h.request("engine.execute", json!({"command": "library.select", "params": {"ids": ids, "active": ids[1]}}), T);
let r = h.request("engine.execute", json!({"command": "view.survey"}), T);
assert_eq!(r["ok"], true, "{r}");
h.settle(SETTLE);
ids
}
/// Move the pointer to `pos` and press and release the primary button there, with `modifiers`.
fn click_with(h: &mut Headless, pos: egui::Pos2, modifiers: egui::Modifiers) {
h.app.synthetic.push(egui::Event::PointerMoved(pos));
h.settle(SETTLE);
h.app.synthetic.push(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, modifiers });
h.app.synthetic.push(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, modifiers });
h.settle(SETTLE);
}
/// Click the centre of photo `id`'s survey tile with `modifiers`.
fn click_tile(h: &mut Headless, id: u64, modifiers: egui::Modifiers) {
let name = format!("cull:{id}");
let pos = h.app.widgets.iter().find(|(w, _)| *w == name).map(|(_, r)| r.center()).expect("the tile is laid out");
click_with(h, pos, modifiers);
}
/// β/Ctrl-click on a survey tile takes that photo out of the survey, as in Lightroom Classic,
/// whether or not it is the active one; a plain click only makes it active, and the last photo stays.
#[test]
fn survey_command_click_takes_the_photo_out() {
let mut h = demo();
let [a, b, c] = survey_of_three(&mut h);
let sel = |h: &Headless| h.app.session.selection.ids.iter().map(|p| p.0).collect::<Vec<u64>>();
let active = |h: &Headless| h.app.session.selection.active.map(|p| p.0);
click_tile(&mut h, a, egui::Modifiers::NONE);
assert_eq!((sel(&h), active(&h)), (vec![a, b, c], Some(a)), "a plain click keeps the photo and makes it active");
click_tile(&mut h, c, egui::Modifiers::COMMAND);
assert_eq!((sel(&h), active(&h)), (vec![a, b], Some(a)), "the β/Ctrl-clicked photo (not the active one) left");
click_tile(&mut h, b, egui::Modifiers::COMMAND);
assert_eq!(sel(&h), vec![a]);
click_tile(&mut h, a, egui::Modifiers::COMMAND);
assert_eq!((sel(&h), active(&h)), (vec![a], Some(a)), "the last photo stays in the survey");
}
/// A photo taken out of the survey leaves the others to glide to their new cells, so it shows
/// which photo went where: a frame later a tile is between its old and new place, then it lands.
#[test]
fn survey_tiles_glide_to_their_new_cells() {
let mut h = demo();
let [a, _, c] = survey_of_three(&mut h);
let tile = |h: &Headless| h.app.widgets.iter().find(|(w, _)| *w == format!("cull:{c}")).map(|(_, r)| *r).expect("the tile is laid out");
let before = tile(&h);
h.app.run("library.select", json!({"ids": [a], "mode": "toggle"})).unwrap();
h.step();
h.step();
let moving = tile(&h);
assert!(h.app.survey_motion.moving(h.time()), "the tiles are on their way");
h.settle(SETTLE);
let after = tile(&h);
assert!(!h.app.survey_motion.moving(h.time()));
assert_ne!(before, after, "the photo has a new cell");
assert!(moving != before && moving != after, "between {before:?} and {after:?}: {moving:?}");
}
/// A photo that joins the survey makes room: the tiles already there glide to their new cells too.
#[test]
fn survey_tiles_glide_when_a_photo_joins() {
let mut h = demo();
let ids = survey_of_three(&mut h);
let joining = h.app.session.visible_cloned().iter().map(|p| p.0).find(|p| !ids.contains(p)).expect("a fourth photo");
let tile = |h: &Headless, id: u64| h.app.widgets.iter().find(|(w, _)| *w == format!("cull:{id}")).map(|(_, r)| *r).expect("the tile is laid out");
let before = ids.map(|id| tile(&h, id));
h.app.run("library.select", json!({"ids": [joining], "mode": "toggle"})).unwrap();
h.step();
h.step();
let moving = ids.map(|id| tile(&h, id));
assert!(h.app.survey_motion.moving(h.time()), "the tiles are on their way");
h.settle(SETTLE);
let after = ids.map(|id| tile(&h, id));
let moved: Vec<usize> = (0..3).filter(|&i| before[i] != after[i]).collect();
assert!(!moved.is_empty(), "a photo joining changes the grid: {before:?} β {after:?}");
for i in moved {
assert!(moving[i] != before[i] && moving[i] != after[i], "tile {i} between {:?} and {:?}: {:?}", before[i], after[i], moving[i]);
}
}
/// A photo that joins takes its cell at once, so it asks for its own render while the others are
/// still gliding (it doesn't wait on the thumbnail until they land).
#[test]
fn a_photo_that_joins_renders_while_the_others_glide() {
let mut h = demo();
let ids = survey_of_three(&mut h);
let joining = h.app.session.visible_cloned().into_iter().find(|p| !ids.contains(&p.0)).expect("a fourth photo");
h.app.run("library.select", json!({"ids": [joining.0], "mode": "toggle"})).unwrap();
h.step();
h.step();
let now = h.time();
assert!(h.app.survey_motion.moving(now), "the others are on their way");
assert!(!h.app.survey_motion.gliding(joining, now), "the photo that joined stays put");
let slot = h.app.survey_motion.slot(joining).expect("the photo that joined has a slot");
assert!(h.app.renderer.wanted(crate::render::Slot::Compare(slot)).is_some(), "its render is asked for");
}
/// Photo ids are local to a library: opening another one forgets the survey's tiles, so a photo
/// there with the same id doesn't glide from a cell of the library left.
#[test]
fn opening_another_library_forgets_the_survey_tiles() {
let dir = std::env::temp_dir().join(format!("lc-ui-survey-switch-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let mut h = demo();
let ids = survey_of_three(&mut h);
let tracked = |h: &Headless| ids.iter().filter(|id| h.app.survey_motion.tracks(lightcraft_catalog::PhotoId(**id))).count();
assert_eq!(tracked(&h), 3);
let r = h.app.run("app.openLibrary", json!({"path": dir.join("second").to_string_lossy()}));
assert!(r.is_ok(), "{r:?}");
assert_eq!(tracked(&h), 0, "nothing of the library left is remembered");
drop(h);
let _ = std::fs::remove_dir_all(&dir);
}
/// Every surveyed photo gets its own render, and keeps it when another photo leaves the survey
/// (each photo keeps its render slot, so the tiles that move don't fall back to thumbnails).
#[test]
fn survey_tiles_keep_their_renders_when_a_photo_leaves() {
let mut h = demo();
let [a, b, c] = survey_of_three(&mut h);
let rendered = |h: &Headless, ids: &[u64]| {
ids.iter().all(|id| h.app.renderer.textures.iter().any(|(s, t)| matches!(s, crate::render::Slot::Compare(_)) && t.photo.0 == *id))
};
assert!(h.step_until(SETTLE, |h| rendered(h, &[a, b, c])), "each tile shows its render");
let slot_of = |h: &Headless, id: u64| {
h.app.renderer.textures.iter().find(|(s, t)| matches!(s, crate::render::Slot::Compare(_)) && t.photo.0 == id).map(|(s, _)| *s)
};
let c_slot = slot_of(&h, c).expect("c is rendered");
let c_tex = h.app.renderer.textures.get(&c_slot).map(|t| t.tex.id()).expect("c's texture");
h.app.run("library.select", json!({"ids": [a], "mode": "toggle"})).unwrap();
h.step();
h.step();
assert!(h.app.survey_motion.moving(h.time()));
let drawn = h.view.painted_shapes().iter().any(|s| match s {
egui::epaint::Shape::Rect(r) => r.fill_texture_id() == c_tex,
egui::epaint::Shape::Mesh(m) => m.texture_id == c_tex,
_ => false,
});
assert!(drawn, "c's render is drawn while it moves, not its thumbnail");
assert!(h.step_until(SETTLE, |h| rendered(h, &[b, c])));
}
/// Hover photo `id`'s survey tile, as a person does, so its Γ appears; then click the Γ in its corner.
fn click_x(h: &mut Headless, id: u64) {
let name = format!("cull:{id}");
let tile = h.app.widgets.iter().find(|(w, _)| *w == name).map(|(_, r)| *r).expect("the tile is laid out");
h.app.synthetic.push(egui::Event::PointerMoved(tile.center()));
h.settle(SETTLE);
click_with(h, egui::pos2(tile.right() - 16.0, tile.top() + 16.0), egui::Modifiers::NONE);
}
/// The Γ in a survey tile's corner takes the photo out of the survey. It shows while the pointer is
/// over the tile, and it must stay while the pointer is on the Γ itself (#1639, #3614): the tile
/// loses its hover to the Γ, and an Γ tied to that hover vanished before the click landed.
#[test]
fn survey_x_takes_the_photo_out() {
let mut h = demo();
let [a, b, c] = survey_of_three(&mut h);
click_x(&mut h, c);
let sel: Vec<u64> = h.app.session.selection.ids.iter().map(|p| p.0).collect();
let active = h.app.session.selection.active.map(|p| p.0);
assert_eq!((sel, active), (vec![a, b], Some(b)), "the photo whose Γ was clicked left the survey; the active one stays active");
}
/// The Γ on the active survey photo takes it out too, and the last remaining photo becomes active,
/// as a β/Ctrl-click on it does.
#[test]
fn survey_x_on_the_active_photo_activates_the_last_one() {
let mut h = demo();
let [a, b, c] = survey_of_three(&mut h);
click_x(&mut h, b);
let sel: Vec<u64> = h.app.session.selection.ids.iter().map(|p| p.0).collect();
assert_eq!((sel, h.app.session.selection.active.map(|p| p.0)), (vec![a, c], Some(c)));
}
/// β/Ctrl-click on the active survey photo while others remain: it leaves, and the last
/// remaining photo becomes active, so the keys still act on a photo on screen.
#[test]
fn survey_command_click_on_the_active_photo_activates_the_last_one() {
let mut h = demo();
let [a, b, c] = survey_of_three(&mut h);
click_tile(&mut h, a, egui::Modifiers::NONE);
click_tile(&mut h, a, egui::Modifiers::COMMAND);
let sel: Vec<u64> = h.app.session.selection.ids.iter().map(|p| p.0).collect();
assert_eq!((sel, h.app.session.selection.active.map(|p| p.0)), (vec![b, c], Some(c)));
}
No file is open. Choose one in the tree, or press Ctrl+O (βO on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.