|
| 1 | +//! Words that are longer in another language keep to their place: a button does not lie over |
| 2 | +//! its neighbour, a choice's name stays inside the choice, a row of the filter bar ends inside |
| 3 | +//! the bar, and a value begins after its label. |
| 4 | +//! |
| 5 | +//! Before, in German: Reset ("Zurücksetzen") lay over HDR in the head of the Edit panel, "Mit |
| 6 | +//! Hilfslinien" ran out of its third of the Upright choices, and the filter bar's first row ran |
| 7 | +//! off the grid at "Bearbeitet oder nicht". (The card of Info already gives a long label its |
| 8 | +//! room, "Velocidade do obturador" among them: its scenario keeps it so.) |
| 9 | +
|
| 10 | +use std::time::Duration; |
| 11 | + |
| 12 | +use egui::Rect; |
| 13 | +use serde_json::{Value, json}; |
| 14 | + |
| 15 | +use crate::headless::Headless; |
| 16 | +use crate::{LightcraftApp, Services}; |
| 17 | + |
| 18 | +const T: Duration = Duration::from_secs(20); |
| 19 | + |
| 20 | +fn demo(language: &str) -> Headless { |
| 21 | + let services = Services { png: None, write_shared: Some(std::sync::Arc::new(|_: &str, _: &[u8]| Ok(()))), ..Default::default() }; |
| 22 | + let mut h = Headless::new(LightcraftApp::new(lightcraft_engine::Session::with_demo(), services), [1600.0, 1000.0], 1.0); |
| 23 | + // let the first renders finish (a render still running at exit can crash in GPU teardown) |
| 24 | + h.settle(T); |
| 25 | + run(&mut h, language, json!({})); |
| 26 | + run(&mut h, "library.select", json!({"ids": [1]})); |
| 27 | + h |
| 28 | +} |
| 29 | + |
| 30 | +fn run(h: &mut Headless, command: &str, params: Value) { |
| 31 | + let r = h.request("engine.execute", json!({"command": command, "params": params}), T); |
| 32 | + assert_eq!(r["ok"], true, "{command}: {r}"); |
| 33 | + rest(h); |
| 34 | +} |
| 35 | + |
| 36 | +fn set(h: &mut Headless, what: Value) { |
| 37 | + let r = h.request("ui.set", what, T); |
| 38 | + assert_eq!(r["ok"], true, "ui.set: {r}"); |
| 39 | + rest(h); |
| 40 | +} |
| 41 | + |
| 42 | +fn rest(h: &mut Headless) { |
| 43 | + h.settle(T); |
| 44 | + h.request("ui.move", json!({"x": 2.0, "y": 2.0}), T); |
| 45 | + // (a layout that follows what it measured the frame before has come to rest) |
| 46 | + for _ in 0..5 { |
| 47 | + h.step(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn rect(h: &Headless, id: &str) -> Rect { |
| 52 | + h.app.widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r).unwrap_or_else(|| panic!("no widget {id}")) |
| 53 | +} |
| 54 | + |
| 55 | +fn widgets(h: &Headless, prefix: &str) -> Vec<(String, Rect)> { |
| 56 | + h.app.widgets.iter().filter(|(w, _)| w.starts_with(prefix)).cloned().collect() |
| 57 | +} |
| 58 | + |
| 59 | +/// Every piece of text drawn, with where it is. |
| 60 | +fn texts(h: &Headless) -> Vec<(String, Rect)> { |
| 61 | + h.view |
| 62 | + .painted_shapes() |
| 63 | + .iter() |
| 64 | + .filter_map(|shape| match shape { |
| 65 | + egui::Shape::Text(text) => Some((text.galley.job.text.clone(), text.visual_bounding_rect())), |
| 66 | + _ => None, |
| 67 | + }) |
| 68 | + .collect() |
| 69 | +} |
| 70 | + |
| 71 | +#[test] |
| 72 | +fn the_buttons_at_the_head_of_edit_do_not_lie_over_one_another() { |
| 73 | + for language in ["app.language.english", "app.language.german", "app.language.russian", "app.language.french"] { |
| 74 | + // Given a photo being edited in a language |
| 75 | + let mut h = demo(language); |
| 76 | + run(&mut h, "view.detail", json!({})); |
| 77 | + set(&mut h, json!({"leftPanel": false, "right": "edit", "openSections": ["light"]})); |
| 78 | + // When the head of the Edit panel is drawn |
| 79 | + let buttons: Vec<(&str, Rect)> = ["auto", "bw", "hdr", "reset"].into_iter().map(|id| (id, rect(&h, &format!("button:{id}")))).collect(); |
| 80 | + // Then no button touches another |
| 81 | + for (i, (a, ra)) in buttons.iter().enumerate() { |
| 82 | + for (b, rb) in buttons.iter().skip(i + 1) { |
| 83 | + assert!(!ra.expand(1.0).intersects(*rb), "{language}: {a} {ra:?} lies over {b} {rb:?}"); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Reset used to be drawn in a right-to-left layout of the head's single row, and lay over HDR in |
| 90 | +/// German. Now the head wraps: Reset goes on the row of the others where it has room (English), |
| 91 | +/// on a row of its own where it has not (German), and at the right of the head either way. |
| 92 | +#[test] |
| 93 | +fn reset_is_on_the_row_of_the_others_when_it_has_room_and_below_them_when_it_has_not() { |
| 94 | + for (language, own_row) in [("app.language.english", false), ("app.language.german", true)] { |
| 95 | + // Given a photo being edited in a language |
| 96 | + let mut h = demo(language); |
| 97 | + run(&mut h, "view.detail", json!({})); |
| 98 | + set(&mut h, json!({"leftPanel": false, "right": "edit", "openSections": ["light"]})); |
| 99 | + // When the head of the Edit panel is drawn |
| 100 | + let hdr = rect(&h, "button:hdr"); |
| 101 | + let reset = rect(&h, "button:reset"); |
| 102 | + // Then Reset is below the others only where it has no room beside them |
| 103 | + if own_row { |
| 104 | + assert!(reset.top() >= hdr.bottom(), "{language}: reset {reset:?} is not below hdr {hdr:?}"); |
| 105 | + } else { |
| 106 | + assert!((reset.center().y - hdr.center().y).abs() < 4.0, "{language}: reset {reset:?} is not on the row of hdr {hdr:?}"); |
| 107 | + assert!(reset.left() >= hdr.right(), "{language}: reset {reset:?} lies over hdr {hdr:?}"); |
| 108 | + } |
| 109 | + // And at the right of the head either way |
| 110 | + assert!(reset.right() > hdr.right(), "{language}: reset {reset:?} is not at the right of hdr {hdr:?}"); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +fn the_name_of_a_choice_stays_inside_the_choice() { |
| 116 | + for language in ["app.language.english", "app.language.german", "app.language.russian", "app.language.french", "app.language.spanish"] { |
| 117 | + // Given the Crop panel in a language, with its rows of choices |
| 118 | + let mut h = demo(language); |
| 119 | + run(&mut h, "view.detail", json!({})); |
| 120 | + set(&mut h, json!({"leftPanel": false, "right": "crop"})); |
| 121 | + let drawn = texts(&h); |
| 122 | + let mut choices = widgets(&h, "button:cropOverlay-"); |
| 123 | + choices.extend(widgets(&h, "button:upright-")); |
| 124 | + assert!(choices.len() >= 10, "{language}: the overlay and upright choices are on screen ({})", choices.len()); |
| 125 | + // Then each choice's name lies inside it, with a little room at each side |
| 126 | + for (id, choice) in choices { |
| 127 | + let name = drawn.iter().find(|(_, at)| choice.contains(at.center())).unwrap_or_else(|| panic!("{language}: {id} has no name")); |
| 128 | + assert!( |
| 129 | + choice.shrink2(egui::vec2(2.0, 0.0)).contains_rect(name.1), |
| 130 | + "{language}: {:?} at {:?} runs out of {id} {choice:?}", |
| 131 | + name.0, |
| 132 | + name.1 |
| 133 | + ); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +#[test] |
| 139 | +fn a_row_of_the_filter_bar_ends_inside_the_bar() { |
| 140 | + for language in ["app.language.english", "app.language.german", "app.language.russian", "app.language.french"] { |
| 141 | + // Given the grid with both side panels and the filter bar, in a language |
| 142 | + let mut h = demo(language); |
| 143 | + run(&mut h, "view.photoGrid", json!({})); |
| 144 | + set(&mut h, json!({"leftPanel": true, "right": "info", "thumbSize": 200, "filterBar": true})); |
| 145 | + let bar = rect(&h, "filterbar"); |
| 146 | + // When its controls are drawn |
| 147 | + let mut controls = widgets(&h, "dropdown:filter-"); |
| 148 | + controls.extend(["button:filterSave", "button:filterClear", "button:filterPresets"].into_iter().map(|id| (id.to_string(), rect(&h, id)))); |
| 149 | + assert!(controls.len() >= 8, "{language}: {}", controls.len()); |
| 150 | + // Then every one of them lies in the bar |
| 151 | + for (id, control) in controls { |
| 152 | + assert!(bar.contains_rect(control), "{language}: {id} {control:?} runs out of the bar {bar:?}"); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +#[test] |
| 158 | +fn a_value_on_the_card_of_info_begins_after_its_label() { |
| 159 | + for language in ["app.language.english", "app.language.german", "app.language.portuguese", "app.language.spanish", "app.language.french"] { |
| 160 | + // Given the Info panel of a photo in a language |
| 161 | + let mut h = demo(language); |
| 162 | + run(&mut h, "view.photoGrid", json!({})); |
| 163 | + set(&mut h, json!({"leftPanel": true, "right": "info", "thumbSize": 200})); |
| 164 | + let drawn = texts(&h); |
| 165 | + let meta = h.app.session.catalog.photo(lightcraft_catalog::PhotoId(1)).map(|p| p.meta.clone()).expect("photo 1"); |
| 166 | + let mut begins = Vec::new(); |
| 167 | + for (label, value) in [ |
| 168 | + ("Focal length", meta.focal_mm.and_then(crate::readout::focal)), |
| 169 | + ("Shutter speed", crate::readout::shutter(&meta.shutter)), |
| 170 | + ("Aperture", meta.aperture.and_then(crate::readout::aperture)), |
| 171 | + ] { |
| 172 | + let (label, value) = (crate::i18n::tr(label).to_string(), value.expect("the demo photo has it")); |
| 173 | + let at = |said: &str| { |
| 174 | + drawn.iter().find(|(text, _)| text == said).map(|(_, at)| *at).unwrap_or_else(|| panic!("{language}: {said:?} is not drawn")) |
| 175 | + }; |
| 176 | + let (label_at, value_at) = (at(&label), at(&value)); |
| 177 | + // Then the value begins at least a gap after its label ends |
| 178 | + assert!(value_at.left() >= label_at.right() + 8.0, "{language}: {label:?} {label_at:?} and {value:?} {value_at:?}"); |
| 179 | + begins.push(value_at.left()); |
| 180 | + } |
| 181 | + // And the values stand under one another |
| 182 | + assert!(begins.iter().all(|x| (x - begins[0]).abs() < 1.0), "{language}: {begins:?}"); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +#[test] |
| 187 | +fn the_filter_bar_comes_to_rest_at_every_width() { |
| 188 | + for language in ["app.language.english", "app.language.german", "app.language.french"] { |
| 189 | + // Given the grid and the filter bar in a language, and what the bar's three parts measure |
| 190 | + let mut h = demo(language); |
| 191 | + run(&mut h, "view.photoGrid", json!({})); |
| 192 | + set(&mut h, json!({"leftPanel": true, "right": "info", "thumbSize": 200, "filterBar": true})); |
| 193 | + let parts: [f32; 3] = h.view.ctx.data(|d| d.get_temp(egui::Id::unique("filterbar:widths"))).expect("the bar has measured its parts"); |
| 194 | + assert!(parts.iter().all(|w| *w > 40.0), "{language}: {parts:?}"); |
| 195 | + let gap = 4.0; |
| 196 | + // When the window is as wide as leaves the bar a point or two more or less than each of |
| 197 | + // the widths at which what a row holds changes |
| 198 | + for edge in [parts[0] + parts[1] + parts[2] + 2.0 * gap, parts[0] + parts[1] + gap, parts[1] + parts[2] + gap] { |
| 199 | + for tenth in -40..=40 { |
| 200 | + let room = edge + tenth as f32 / 10.0; |
| 201 | + h.size.x += room - (rect(&h, "filterbar").width() - 32.0); |
| 202 | + let mut heights = Vec::new(); |
| 203 | + for _ in 0..12 { |
| 204 | + h.step(); |
| 205 | + heights.push(rect(&h, "filterbar").height()); |
| 206 | + } |
| 207 | + assert!(((rect(&h, "filterbar").width() - 32.0) - room).abs() < 0.05, "{language}: the bar has the room asked for"); |
| 208 | + // Then within a few frames the bar is one height and stays it: it used to change |
| 209 | + // every frame in a band four points wide, and the grid under it jumped |
| 210 | + let rest: Vec<f32> = heights.iter().skip(6).copied().collect(); |
| 211 | + assert!(rest.iter().all(|height| *height == rest[0]), "{language}: at {room} pt of room the bar is {heights:?} high frame by frame"); |
| 212 | + // And nothing in it runs out of it |
| 213 | + let bar = rect(&h, "filterbar"); |
| 214 | + for (id, control) in widgets(&h, "dropdown:filter-") { |
| 215 | + assert!(control.right() <= bar.right() + 0.5, "{language}: at {room} pt {id} {control:?} runs out of {bar:?}"); |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments