nandi/jolt-nativepublic Fork 0
5ae197f7be55bd41678978f11e41a4282ffcf4b7
Commits
Clone
git clone https://git.rickub.com/nandi/jolt-native.git
git clone ssh://git@rickub.com/nandi/jolt-native.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

tests.rs · 862 lines · 29.9 KBRust Blame HistoryRaw
Paint the same tree into a terminal, for the machines with no window a7f6202 nandi 18d ago1//! The backend end to end, with no terminal.
2//!
3//! Every one of these mounts real nodes through the same calls the ABI makes,
4//! paints a real frame, and asserts on the lines that came out — which is the
5//! whole reason painting goes through a grid. Nothing here needs a TTY, a
6//! display or raw mode, so it all runs in CI.
7
8use crate::screen::attr;
9use crate::tree::Value;
10use crate::ui::Ui;
11
12fn ui() -> Ui {
13 Ui::new(24, 8)
14}
15
16/// Mount `tag` under `parent` with the string props given.
17fn node(ui: &mut Ui, parent: u32, tag: &str, props: &[(&str, &str)]) -> u32 {
18 let id = ui.tree.new_node(tag);
19 for (key, value) in props {
20 ui.tree.set(id, key, Value::Str((*value).to_owned()));
21 }
22 ui.tree.append(parent, id);
23 id
24}
25
26fn events(ui: &mut Ui) -> Vec<(u32, String, String, f64)> {
27 let mut out = Vec::new();
28 while ui.tree.poll() {
29 let event = ui.tree.current().unwrap();
30 out.push((
31 event.node,
32 event.name.to_owned(),
33 event.text.clone(),
34 event.num,
35 ));
36 }
37 out
38}
39
40#[test]
41fn a_column_paints_its_children_down_the_page() {
42 let mut ui = ui();
43 let root = ui.tree.root();
44 node(&mut ui, root, "label", &[("label", "first")]);
45 node(&mut ui, root, "label", &[("label", "second")]);
46 ui.frame();
47 assert_eq!(ui.screen.line(0), "first");
48 assert_eq!(ui.screen.line(1), "second");
49}
50
51#[test]
52fn a_row_paints_its_children_across_with_its_spacing_between_them() {
53 let mut ui = ui();
54 let root = ui.tree.root();
55 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
56 ui.tree.set(row, "spacing", Value::Num(2.0));
57 node(&mut ui, row, "label", &[("label", "aa")]);
58 node(&mut ui, row, "label", &[("label", "bb")]);
59 ui.frame();
60 assert_eq!(ui.screen.line(0), "aa bb");
61}
62
63#[test]
64fn a_label_wraps_to_the_width_it_was_given() {
65 let mut ui = Ui::new(10, 4);
66 let root = ui.tree.root();
67 node(&mut ui, root, "label", &[("label", "one two three four")]);
68 ui.frame();
69 assert_eq!(ui.screen.line(0), "one two");
70 assert_eq!(ui.screen.line(1), "three four");
71}
72
73#[test]
74fn a_frame_draws_a_border_with_its_label_in_the_top_edge() {
75 let mut ui = Ui::new(12, 4);
76 let root = ui.tree.root();
77 let frame = node(&mut ui, root, "frame", &[("label", "Task")]);
78 node(&mut ui, frame, "label", &[("label", "hi")]);
79 ui.frame();
80 assert_eq!(ui.screen.line(0), "┌ Task ────┐");
81 assert_eq!(ui.screen.line(1), "│hi │");
82 assert_eq!(ui.screen.line(2), "└──────────┘");
83}
84
85#[test]
86fn the_first_focusable_widget_takes_focus_and_tab_walks_the_ring() {
87 let mut ui = ui();
88 let root = ui.tree.root();
89 let first = node(&mut ui, root, "button", &[("label", "one")]);
90 let second = node(&mut ui, root, "button", &[("label", "two")]);
91 ui.frame();
92 assert_eq!(ui.focus(), first);
93 ui.key("tab");
94 assert_eq!(ui.focus(), second);
95 ui.key("tab");
96 assert_eq!(ui.focus(), first, "the ring wraps");
97 ui.key("shift+tab");
98 assert_eq!(ui.focus(), second);
99}
100
101#[test]
102fn autofocus_beats_paint_order() {
103 let mut ui = ui();
104 let root = ui.tree.root();
105 node(&mut ui, root, "button", &[("label", "one")]);
106 let wanted = node(&mut ui, root, "entry", &[]);
107 ui.tree.set(wanted, "autofocus", Value::Bool(true));
108 ui.frame();
109 assert_eq!(ui.focus(), wanted);
110}
111
112#[test]
113fn a_focused_button_is_drawn_in_reverse_and_activates_on_enter() {
114 let mut ui = ui();
115 let root = ui.tree.root();
116 let button = node(&mut ui, root, "button", &[("label", "go")]);
117 ui.frame();
118 assert_eq!(ui.screen.line(0), "[ go ]");
119 assert!(ui.screen.cell(0, 0).unwrap().style.has(attr::REVERSE));
120 ui.key("enter");
121 assert_eq!(
122 events(&mut ui),
123 vec![(button, "click".into(), String::new(), 0.0)]
124 );
125}
126
127#[test]
128fn a_checkbutton_writes_its_new_state_back_as_well_as_reporting_it() {
129 let mut ui = ui();
130 let root = ui.tree.root();
131 let check = node(&mut ui, root, "checkbutton", &[("label", "live")]);
132 ui.frame();
133 assert_eq!(ui.screen.line(0), "[ ] live");
134 ui.key("space");
135 assert_eq!(
136 events(&mut ui),
137 vec![(check, "toggled".into(), String::new(), 1.0)]
138 );
139 // A caller that ignores the event still has a working control.
140 assert_eq!(ui.tree.props(check).bool("active", false), true);
141 ui.frame();
142 assert_eq!(ui.screen.line(0), "[x] live");
143}
144
145#[test]
146fn typing_in_an_entry_edits_its_text_and_reports_every_change() {
147 let mut ui = ui();
148 let root = ui.tree.root();
149 let entry = node(&mut ui, root, "entry", &[("placeholder", "name")]);
150 ui.frame();
151 assert_eq!(
152 ui.screen.line(0),
153 "name",
154 "the placeholder shows until it is typed in"
155 );
156 for key in ["h", "i", "space", "there"] {
157 for name in [key] {
158 if name.chars().count() > 1 && name != "space" {
159 for ch in name.chars() {
160 ui.key(&ch.to_string());
161 }
162 } else {
163 ui.key(name);
164 }
165 }
166 }
167 assert_eq!(ui.tree.props(entry).str("text"), "hi there");
168 let changes = events(&mut ui);
169 assert_eq!(changes.len(), 8);
170 assert_eq!(changes.last().unwrap().2, "hi there");
171 ui.frame();
172 assert_eq!(ui.screen.line(0), "hi there");
173}
174
175#[test]
176fn readline_keys_edit_where_the_caret_is() {
177 let mut ui = ui();
178 let root = ui.tree.root();
179 let entry = node(&mut ui, root, "entry", &[("text", "one two three")]);
180 ui.frame();
181 ui.key("ctrl+w");
182 assert_eq!(ui.tree.props(entry).str("text"), "one two ");
183 ui.key("ctrl+a");
184 ui.key("delete");
185 assert_eq!(ui.tree.props(entry).str("text"), "ne two ");
186 ui.key("ctrl+e");
187 ui.key("backspace");
188 assert_eq!(ui.tree.props(entry).str("text"), "ne two");
189 ui.key("ctrl+u");
190 assert_eq!(ui.tree.props(entry).str("text"), "");
191}
192
193#[test]
194fn enter_in_an_entry_activates_rather_than_typing() {
195 let mut ui = ui();
196 let root = ui.tree.root();
197 let entry = node(&mut ui, root, "entry", &[("text", "search")]);
198 ui.frame();
199 ui.key("enter");
200 assert_eq!(
201 events(&mut ui),
202 vec![(entry, "activate".into(), "search".into(), 0.0)]
203 );
204 assert_eq!(ui.tree.props(entry).str("text"), "search");
205}
206
207#[test]
208fn a_listbox_moves_its_cursor_with_the_arrows_and_with_j_and_k() {
209 let mut ui = ui();
210 let root = ui.tree.root();
211 let list = node(&mut ui, root, "listbox", &[]);
212 for name in ["alpha", "beta", "gamma"] {
213 node(&mut ui, list, "label", &[("label", name)]);
214 }
215 ui.frame();
216 assert_eq!(ui.screen.line(0), "› alpha");
217 ui.key("j");
218 assert_eq!(
219 events(&mut ui),
220 vec![(list, "select".into(), "beta".into(), 1.0)]
221 );
222 ui.key("G");
223 assert_eq!(ui.tree.props(list).num("selected", -1.0), 2.0);
224 ui.key("enter");
225 assert_eq!(
226 events(&mut ui),
227 vec![
228 (list, "select".into(), "gamma".into(), 2.0),
229 (list, "activate".into(), "gamma".into(), 2.0)
230 ]
231 );
232 ui.frame();
233 assert_eq!(ui.screen.line(2), "› gamma");
234}
235
236#[test]
237fn a_key_nothing_wanted_reaches_the_caller_as_an_event() {
238 let mut ui = ui();
239 let root = ui.tree.root();
240 let button = node(&mut ui, root, "button", &[("label", "go")]);
241 ui.frame();
242 assert!(!ui.key("f5"));
243 assert_eq!(
244 events(&mut ui),
245 vec![(button, "key".into(), "f5".into(), 0.0)]
246 );
247}
248
249#[test]
250fn ctrl_c_asks_the_loop_to_stop() {
251 let mut ui = ui();
252 assert!(!ui.should_close());
253 ui.key("ctrl+c");
254 assert!(ui.should_close());
255}
256
257#[test]
258fn an_insensitive_subtree_is_dimmed_and_out_of_the_focus_ring() {
259 let mut ui = ui();
260 let root = ui.tree.root();
261 let column = node(&mut ui, root, "vbox", &[]);
262 ui.tree.set(column, "sensitive", Value::Bool(false));
263 node(&mut ui, column, "button", &[("label", "go")]);
264 let live = node(&mut ui, root, "button", &[("label", "live")]);
265 ui.frame();
266 assert!(ui.screen.cell(0, 0).unwrap().style.has(attr::DIM));
267 assert_eq!(ui.focus(), live);
268}
269
270#[test]
271fn a_click_focuses_and_activates_what_is_under_it() {
272 let mut ui = ui();
273 let root = ui.tree.root();
274 node(&mut ui, root, "button", &[("label", "one")]);
275 let second = node(&mut ui, root, "button", &[("label", "two")]);
276 ui.frame();
277 assert!(ui.click(2, 1));
278 assert_eq!(ui.focus(), second);
279 assert_eq!(
280 events(&mut ui),
281 vec![(second, "click".into(), String::new(), 0.0)]
282 );
283 assert!(!ui.click(20, 7), "a click on nothing is not an event");
284}
285
286#[test]
287fn a_scroll_shows_a_window_of_its_content_and_will_not_go_past_the_end() {
288 let mut ui = Ui::new(10, 3);
289 let root = ui.tree.root();
290 let scroll = node(&mut ui, root, "scroll", &[]);
291 for i in 0..6 {
292 node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
293 }
294 ui.frame();
295 assert_eq!(ui.screen.lines(), vec!["row 0", "row 1", "row 2"]);
296 ui.wheel(1, 1, 2);
297 ui.frame();
298 assert_eq!(ui.screen.lines(), vec!["row 2", "row 3", "row 4"]);
299 ui.wheel(1, 1, 40);
300 ui.frame();
301 assert_eq!(ui.screen.lines(), vec!["row 3", "row 4", "row 5"]);
302 // The clamped viewport is written back, so the next scroll up starts from
303 // where the reader actually is.
304 assert_eq!(ui.tree.props(scroll).num("offset", -1.0), 3.0);
305}
306
Scroll the list under the pointer, and from where it actually is a785201 nandi 17d ago307#[test]
308fn the_wheel_scrolls_the_list_under_the_pointer_not_the_first_one_painted() {
309 // frq's wide layout: the chats list down the left, the conversation beside
310 // it, and both of them scrolls. A wheel over the conversation used to move
311 // the sidebar — every scroll answered to a pointer anywhere on screen, so
312 // the first one the walk reached took the lot.
313 let mut ui = Ui::new(20, 3);
314 let root = ui.tree.root();
315 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
316 let sidebar = node(&mut ui, row, "scroll", &[]);
317 ui.tree.set(sidebar, "width-request", Value::Num(10.0));
318 for i in 0..6 {
319 node(&mut ui, sidebar, "label", &[("label", &format!("chat {i}"))]);
320 }
321 let backlog = node(&mut ui, row, "scroll", &[]);
322 ui.tree.set(backlog, "width-request", Value::Num(10.0));
323 for i in 0..6 {
324 node(&mut ui, backlog, "label", &[("label", &format!("line {i}"))]);
325 }
326 ui.frame();
327 assert_eq!(ui.screen.line(0), "chat 0 line 0");
328
329 ui.wheel(15, 1, 2);
330 ui.frame();
331 assert_eq!(
332 ui.screen.line(0),
333 "chat 0 line 2",
334 "the wheel was over the backlog, and only the backlog moved"
335 );
336
337 ui.wheel(2, 1, 1);
338 ui.frame();
339 assert_eq!(
340 ui.screen.line(0),
341 "chat 1 line 2",
342 "and over the sidebar it is the sidebar that moves"
343 );
344}
345
346#[test]
347fn page_keys_move_the_backlog_while_the_entry_keeps_the_focus() {
348 let mut ui = Ui::new(10, 4);
349 let root = ui.tree.root();
350 let scroll = node(&mut ui, root, "scroll", &[]);
351 for i in 0..9 {
352 node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
353 }
354 // What a reader is typing into, under the list they are reading.
355 let entry = node(&mut ui, root, "entry", &[("text", "hi")]);
356 ui.frame();
357 assert_eq!(ui.screen.line(0), "row 0");
358
359 assert!(ui.key("page-down"), "the page was taken, not passed on");
360 ui.frame();
361 // A page is the viewport less the line that says where you were.
362 assert_eq!(ui.screen.line(0), "row 2");
363 assert_eq!(ui.focus(), entry, "and the entry still has the keyboard");
364
365 ui.key("page-up");
366 ui.frame();
367 assert_eq!(ui.screen.line(0), "row 0");
368 assert!(events(&mut ui).iter().all(|(_, name, _, _)| name != "key"));
369}
370
371#[test]
372fn a_wheel_between_a_re_render_and_a_frame_moves_from_where_the_list_was() {
373 // A render clears a node's props and writes them again; a wheel or a page
374 // key that landed in that gap used to read an offset of zero and answer
375 // with the top of the buffer.
376 let mut ui = Ui::new(10, 3);
377 let root = ui.tree.root();
378 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
379 for i in 0..9 {
380 node(&mut ui, scroll, "label", &[("label", &format!("row {i}"))]);
381 }
382 ui.frame();
383 ui.wheel(1, 1, 4);
384 ui.frame();
385 assert_eq!(ui.screen.line(0), "row 4");
386
387 // The caller re-renders the list: props off, props on, no frame between.
388 ui.tree.clear_props(scroll);
389 ui.tree
390 .set(scroll, "scroll-key", Value::Str("backlog".into()));
391 ui.wheel(1, 1, 1);
392 ui.frame();
393 assert_eq!(ui.screen.line(0), "row 5");
394}
395
Paint a reaction, and give an emoji the two columns it takes dce285f nandi 17d ago396#[test]
397fn a_reaction_paints_its_glyph_and_answers_a_click_on_it() {
398 let mut ui = Ui::new(20, 2);
399 let root = ui.tree.root();
400 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
401 ui.tree.set(row, "spacing", Value::Num(1.0));
402 let pill = node(&mut ui, row, "reaction", &[("emoji", "👍")]);
403 ui.tree.set(pill, "count", Value::Num(2.0));
404 ui.tree.set(pill, "mine", Value::Bool(true));
405 let chip = node(&mut ui, row, "reaction", &[("emoji", "🙂")]);
406 ui.frame();
407 // The tally where there is one; the bare glyph where there is not — that
408 // is the chip you press to start one.
409 assert_eq!(ui.screen.line(0), "👍 2 🙂");
410
411 // Two cells for the glyph, so the chip after it starts where it looks
412 // like it starts.
413 assert!(ui.click(5, 0), "the chip took the click");
414 assert_eq!(
415 events(&mut ui),
416 vec![(chip, "click".into(), String::new(), 0.0)]
417 );
418
419 // And by keyboard, for a terminal with no pointer at all.
420 ui.key("shift+tab");
421 ui.key("enter");
422 assert_eq!(
423 events(&mut ui),
424 vec![(pill, "click".into(), String::new(), 0.0)]
425 );
426}
427
Keep a glyph whole, selector and all, and copy it across whole 3dd441e nandi 16d ago428#[test]
429fn a_glyph_keeps_the_selector_that_says_to_draw_it_as_a_picture() {
430 let mut ui = Ui::new(10, 2);
431 let root = ui.tree.root();
432 // The reply chip: an arrow, and a selector asking for the emoji rather
433 // than the small mono arrow a terminal draws without it.
434 node(&mut ui, root, "reaction", &[("emoji", "↩️")]);
435 ui.frame();
436 assert_eq!(ui.screen.line(0), "\u{fe0f}");
437 // And it is two columns, like the emoji beside it on the row.
438 assert_eq!(ui.screen.cell(1, 0).map(|c| c.trail), Some(true));
439}
440
Paint a reaction, and give an emoji the two columns it takes dce285f nandi 17d ago441#[test]
442fn an_emoji_is_two_columns_wide_and_what_follows_it_knows_that() {
443 let mut ui = Ui::new(12, 3);
444 let root = ui.tree.root();
445 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
446 node(&mut ui, row, "label", &[("label", "👍")]);
447 let after = node(&mut ui, row, "button", &[("label", "ok")]);
448 // A label that has to wrap wraps by column, not by character.
449 node(&mut ui, root, "label", &[("label", "👍👍👍👍👍👍👍")]);
450 ui.frame();
451 assert_eq!(ui.screen.line(0), "👍[ ok ]");
452 assert_eq!(ui.screen.line(1), "👍👍👍👍👍👍");
453
454 // The button starts at column 2, because the glyph before it took two.
455 assert!(ui.click(2, 0));
456 assert_eq!(
457 events(&mut ui),
458 vec![(after, "click".into(), String::new(), 0.0)]
459 );
460}
461
Draw a picture in a terminal, over the Kitty graphics protocol c2d912f nandi 16d ago462/// A PNG that is only a header: this backend reads the size out of one and
463/// hands the file to the terminal, so a header is all a test of the layout
464/// needs.
465fn png_file(name: &str, w: u32, h: u32) -> String {
466 let mut bytes = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
467 bytes.extend_from_slice(&13u32.to_be_bytes());
468 bytes.extend_from_slice(b"IHDR");
469 bytes.extend_from_slice(&w.to_be_bytes());
470 bytes.extend_from_slice(&h.to_be_bytes());
471 let path = std::env::temp_dir().join(name);
472 std::fs::write(&path, &bytes).expect("wrote the picture");
473 path.to_string_lossy().into_owned()
474}
475
476#[test]
477fn a_picture_takes_the_cells_its_shape_asks_for_and_says_where_it_is() {
478 crate::graphics::force(Some(true));
479 crate::graphics::set_cell((8, 16));
480 let path = png_file("jolt-tui-wide.png", 320, 160);
481
482 let mut ui = Ui::new(60, 20);
483 let root = ui.tree.root();
484 node(&mut ui, root, "label", &[("label", "a picture:")]);
485 let image = node(&mut ui, root, "image", &[("src", &path)]);
486 ui.tree.set(image, "max-height", Value::Num(6.0));
487 ui.frame();
488
489 // 320x160 pixels over 8x16 cells is 40 by 10, held to the six rows the
490 // caller allowed — and the width comes down with it, not separately.
491 let placed = ui.images().first().cloned().expect("a placement");
492 assert_eq!((placed.area.w, placed.area.h), (24, 6));
493 assert_eq!((placed.area.x, placed.area.y), (0, 1));
494 assert_eq!(placed.path, path);
495 // The cells themselves stay blank: the terminal draws over them.
496 assert_eq!(ui.screen.line(1), "");
497 crate::graphics::force(None);
498}
499
500#[test]
501fn a_picture_scrolling_past_the_edge_is_cropped_rather_than_lost() {
502 crate::graphics::force(Some(true));
503 crate::graphics::set_cell((8, 16));
504 let path = png_file("jolt-tui-tall.png", 160, 320);
505
506 let mut ui = Ui::new(40, 4);
507 let root = ui.tree.root();
508 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
509 let image = node(&mut ui, scroll, "image", &[("src", &path)]);
510 ui.tree.set(image, "max-height", Value::Num(8.0));
511 node(&mut ui, scroll, "label", &[("label", "under it")]);
512 ui.frame();
513 let placed = ui.images().first().cloned().expect("a placement");
514 assert_eq!((placed.area.y, placed.area.h), (0, 4), "as much as fits");
515 assert_eq!((placed.crop_top, placed.crop_bottom), (0, 4));
516
517 ui.wheel(1, 1, 2);
518 ui.frame();
519 let placed = ui.images().first().cloned().expect("still placed");
520 assert_eq!((placed.area.y, placed.area.h), (0, 4));
521 assert_eq!(
522 (placed.crop_top, placed.crop_bottom),
523 (2, 2),
524 "two rows of the picture have gone off the top"
525 );
526 crate::graphics::force(None);
527}
528
529#[test]
530fn a_terminal_that_draws_no_pictures_says_so_where_one_would_be() {
531 crate::graphics::force(Some(false));
532 let path = png_file("jolt-tui-note.png", 320, 160);
533 let mut ui = Ui::new(40, 4);
534 let root = ui.tree.root();
535 node(&mut ui, root, "image", &[("src", &path)]);
536 ui.frame();
537 assert_eq!(ui.screen.line(0), "[ picture ]");
538 assert!(ui.images().is_empty());
539 crate::graphics::force(None);
540}
541
Paint the same tree into a terminal, for the machines with no window a7f6202 nandi 18d ago542#[test]
543fn an_overlay_floats_in_the_middle_over_whatever_was_under_it() {
544 let mut ui = Ui::new(14, 5);
545 let root = ui.tree.root();
546 node(&mut ui, root, "label", &[("label", "beneath")]);
547 let overlay = node(&mut ui, root, "overlay", &[("label", "Sure?")]);
548 node(&mut ui, overlay, "label", &[("label", "yes")]);
549 ui.frame();
550 assert_eq!(ui.screen.line(1), " ┌ Sure? ┐");
551 assert_eq!(ui.screen.line(2), " │yes │");
552 ui.key("esc");
553 assert_eq!(
554 events(&mut ui),
555 vec![(overlay, "close".into(), String::new(), 0.0)]
556 );
557}
558
559#[test]
560fn a_progress_bar_fills_the_share_of_its_width_it_was_given() {
561 let mut ui = Ui::new(10, 2);
562 let root = ui.tree.root();
563 let bar = node(&mut ui, root, "progress", &[]);
564 ui.tree.set(bar, "value", Value::Num(0.5));
565 ui.frame();
566 assert_eq!(ui.screen.line(0), "█████░░░░░");
567}
568
569#[test]
570fn colours_and_attributes_are_inherited_by_a_subtree() {
571 let mut ui = ui();
572 let root = ui.tree.root();
573 let column = node(&mut ui, root, "vbox", &[("color", "red")]);
574 ui.tree.set(column, "bold", Value::Bool(true));
575 node(&mut ui, column, "label", &[("label", "hi")]);
576 ui.frame();
577 let cell = ui.screen.cell(0, 0).unwrap();
578 assert_eq!(cell.style.fg, crate::screen::Color::Indexed(1));
579 assert!(cell.style.has(attr::BOLD));
580}
581
582#[test]
583fn an_unknown_tag_still_shows_its_children() {
584 let mut ui = ui();
585 let root = ui.tree.root();
586 let odd = node(&mut ui, root, "sparkline", &[]);
587 node(&mut ui, odd, "label", &[("label", "inside")]);
588 ui.frame();
589 assert_eq!(ui.screen.line(0), "inside");
590}
591
592#[test]
593fn focus_survives_a_repaint_and_lands_somewhere_when_its_widget_is_unmounted() {
594 let mut ui = ui();
595 let root = ui.tree.root();
596 let first = node(&mut ui, root, "button", &[("label", "one")]);
597 let second = node(&mut ui, root, "button", &[("label", "two")]);
598 ui.frame();
599 ui.key("tab");
600 assert_eq!(ui.focus(), second);
601 ui.frame();
602 assert_eq!(ui.focus(), second, "a repaint does not move the focus");
603 ui.tree.remove(root, second);
604 ui.frame();
605 assert_eq!(ui.focus(), first);
606}
Share a column's rows out at the width it has, not at its own height 5acc801 nandi 18d ago607
608#[test]
609fn an_unknown_leaf_paints_its_own_text() {
610 // A tag this backend does not know paints as a box so its children still
611 // show — but a leaf has none, and a `:status` badge or a `:link` in the
612 // middle of a message would otherwise be a hole in the sentence.
613 let mut ui = ui();
614 let root = ui.tree.root();
615 node(&mut ui, root, "status", &[("label", "joined")]);
616 node(&mut ui, root, "label", &[("label", "after")]);
617 ui.frame();
618 assert_eq!(ui.screen.line(0), "joined");
619 assert_eq!(ui.screen.line(1), "after");
620}
621
622#[test]
623fn a_nested_column_paints_every_child_and_not_only_the_first() {
624 // Its rows were shared out by measuring each child at the *rows* it had
625 // rather than the columns, so a label wrapped to a paragraph, the overrun
626 // came off the end, and everything after the first child was handed
627 // nothing. Two levels down is where it showed: the top box is as wide as
628 // the screen and as tall, so the two numbers were close enough to hide it.
629 let mut ui = ui();
630 let root = ui.tree.root();
631 let col = node(&mut ui, root, "vbox", &[("orientation", "vertical")]);
632 node(&mut ui, col, "label", &[("label", "the first line")]);
633 node(&mut ui, col, "button", &[("label", "Open")]);
634 ui.frame();
635 assert_eq!(ui.screen.line(0), "the first line");
636 assert_eq!(ui.screen.line(1), "[ Open ]");
637}
Let a backlog keep its place, and leave room for what is under it 68910bd nandi 18d ago638
639#[test]
640fn an_unknown_leaf_that_names_a_picture_paints_nothing() {
641 // An `:avatar`'s label is the nick behind the face — words for something
642 // that cannot be drawn here, and in frq already on the row beside it.
643 let mut ui = ui();
644 let root = ui.tree.root();
645 node(
646 &mut ui,
647 root,
648 "avatar",
649 &[("label", "nandi.uk"), ("src", "/tmp/a.png")],
650 );
651 node(&mut ui, root, "label", &[("label", "nandi.uk")]);
652 ui.frame();
653 assert_eq!(ui.screen.line(0), "nandi.uk");
654 assert_eq!(ui.screen.line(1), "");
655}
656
657#[test]
658fn a_sticky_viewport_opens_at_the_bottom_and_stays_there() {
659 // A backlog taller than its viewport, in a scroll that follows its own
660 // bottom: the newest line is what a chat client opens on, and a line
661 // arriving must not drag the screen out from under a reader who scrolled
662 // up to read history.
663 let mut ui = Ui::new(20, 3);
664 let root = ui.tree.root();
665 let scroll = node(
666 &mut ui,
667 root,
668 "scroll",
669 &[("scroll-key", "backlog"), ("stick-to-bottom", "true")],
670 );
671 for n in 1..=6 {
672 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
673 }
674 ui.frame();
675 assert_eq!(ui.screen.line(2), "line 6");
676
677 // Scrolled up, and it stays where it was put across a re-render — the
678 // position lives under the key, not in a prop the next render clears.
679 ui.wheel(0, 0, -3);
680 ui.frame();
681 assert_eq!(ui.screen.line(0), "line 1");
682 ui.tree.clear_props(scroll);
683 ui.tree.set(scroll, "scroll-key", Value::Str("backlog".into()));
684 ui.tree
685 .set(scroll, "stick-to-bottom", Value::Bool(true));
686 ui.frame();
687 assert_eq!(ui.screen.line(0), "line 1");
688
689 // Back down to the bottom, and it follows again.
690 ui.wheel(0, 0, 9);
691 ui.frame();
692 node(&mut ui, scroll, "label", &[("label", "line 7")]);
693 ui.frame();
694 assert_eq!(ui.screen.line(2), "line 7");
695}
696
697#[test]
698fn a_column_that_asks_for_a_width_gets_it_and_no_more() {
699 // Two panes in a row, the first with a width of its own. Its content is
700 // one long line, so measured naturally it is wider than the screen and the
701 // pane beside it is left nothing — which is the split view painting a
702 // sidebar and a ten-cell column of wrapped fragments.
703 let mut ui = Ui::new(40, 2);
704 let root = ui.tree.root();
705 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
706 let side = node(&mut ui, row, "vbox", &[]);
707 // A number, as the ABI sends one: a width read as a string is no width.
708 ui.tree.set(side, "width-request", Value::Num(10.0));
709 node(
710 &mut ui,
711 side,
712 "label",
713 &[("label", "a preview far longer than ten cells")],
714 );
715 let main = node(&mut ui, row, "vbox", &[]);
716 node(&mut ui, main, "label", &[("label", "the conversation")]);
717 ui.frame();
718 assert_eq!(ui.screen.line(0), "a preview the conversation");
719}
720
721#[test]
722fn a_backlog_taller_than_the_screen_leaves_the_compose_bar_its_row() {
723 // The shape of frq's chat screen: a viewport holding more than fits, and
724 // under it the things you act with. A column that cannot shrink hands the
725 // scroll every row it asks for and paints the entry off the bottom edge.
726 let mut ui = Ui::new(20, 4);
727 let root = ui.tree.root();
728 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
729 for n in 1..=10 {
730 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
731 }
732 node(&mut ui, root, "separator", &[]);
733 node(&mut ui, root, "entry", &[("placeholder", "Message")]);
734 ui.frame();
735 assert_eq!(ui.screen.line(3), "Message");
736}
Measure a node once, and paint only what is on screen c41903b nandi 16d ago737
738#[test]
739fn a_message_straddling_the_top_of_a_viewport_shows_the_part_that_is_in_it() {
740 // Culling paints whole children and lets the copy cut them, so the row a
741 // reader is half way through is the row they see — not the next one down.
742 let mut ui = Ui::new(20, 3);
743 let root = ui.tree.root();
744 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
745 for n in 1..=6 {
746 let block = node(&mut ui, scroll, "vbox", &[]);
747 node(&mut ui, block, "label", &[("label", &format!("head {n}"))]);
748 node(&mut ui, block, "label", &[("label", &format!("body {n}"))]);
749 }
750 ui.tree.set(scroll, "offset", Value::Num(3.0));
751 ui.frame();
752 // Two rows a message, so an offset of three lands mid-way through the
753 // second one: its body, then the third whole.
754 assert_eq!(ui.screen.line(0), "body 2");
755 assert_eq!(ui.screen.line(1), "head 3");
756 assert_eq!(ui.screen.line(2), "body 3");
757}
758
759#[test]
760fn a_button_below_the_fold_keeps_its_place_in_the_focus_ring() {
761 // Nothing off screen is painted, and the ring is built while painting —
762 // so the ring has to be told about the parts that were skipped. Tabbing
763 // onto something below the fold is how a reader gets to it.
764 let mut ui = Ui::new(20, 2);
765 let root = ui.tree.root();
766 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
767 let first = node(&mut ui, scroll, "button", &[("label", "first")]);
768 for n in 1..=20 {
769 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
770 }
771 let below = node(&mut ui, scroll, "button", &[("label", "last")]);
772 ui.frame();
773 // Twenty lines down and well out of a two-row viewport, but still next in
774 // the ring after the button at the top.
775 assert_eq!(ui.focus(), first);
776 ui.key("tab");
777 assert_eq!(ui.focus(), below);
778}
779
780#[test]
781fn a_scrolled_backlog_paints_what_an_unscrolled_one_would_have_shown() {
782 // The check that culling changed nothing: paint a viewport onto the middle
783 // of a long list, and every row is the row that list has there.
784 let mut ui = Ui::new(20, 5);
785 let root = ui.tree.root();
786 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
787 for n in 0..40 {
788 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
789 }
790 ui.frame();
791 let mut offset = 0;
792 for step in [0, 1, 16, 18] {
793 // Through the wheel rather than the prop: the viewport's position is
794 // the library's own state, and it writes it back over anything set
795 // here on the frame after.
796 ui.wheel(0, 0, step);
797 offset += step as usize;
798 ui.frame();
799 for row in 0..5u16 {
800 assert_eq!(
801 ui.screen.line(row),
802 format!("line {}", offset + row as usize),
803 "row {row} at offset {offset}"
804 );
805 }
806 }
807}
808
809/// A backlog the shape frq mounts: a scrolling column of messages, each a few
810/// boxes deep, with a heading row and wrapping text under it.
811#[cfg(test)]
812fn backlog(cols: u16, rows: u16, messages: usize) -> Ui {
813 let mut ui = Ui::new(cols, rows);
814 let root = ui.tree.root();
815 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
816 for n in 0..messages {
817 let row = node(&mut ui, scroll, "hbox", &[("orientation", "horizontal")]);
818 node(&mut ui, row, "spacer", &[]);
819 let body = node(&mut ui, row, "vbox", &[]);
820 let head = node(&mut ui, body, "hbox", &[("orientation", "horizontal")]);
821 node(&mut ui, head, "label", &[("label", "nandi")]);
822 node(&mut ui, head, "dim-label", &[("label", "12:01")]);
823 node(
824 &mut ui,
825 body,
826 "label",
827 &[(
828 "label",
829 &format!("message number {n} with enough words in it to wrap across a line or two"),
830 )],
831 );
832 }
833 node(&mut ui, root, "separator", &[]);
834 node(&mut ui, root, "entry", &[("placeholder", "Message")]);
835 ui
836}
837
838/// What a frame costs on a backlog, which is what scrolling one costs. Not a
839/// test — it asserts nothing — so it is `--ignored` and run by hand:
840///
841/// cargo test --release -p jolt-tui -- --ignored --nocapture backlog_cost
842#[test]
843#[ignore]
844fn backlog_cost() {
845 for messages in [25, 50, 100, 200, 400] {
846 let mut ui = backlog(100, 36, messages);
847 ui.frame();
848 let mut times = Vec::new();
849 for i in 0..21 {
850 ui.wheel(10, 10, if i % 2 == 0 { 3 } else { -3 });
851 let at = std::time::Instant::now();
852 ui.frame();
853 times.push(at.elapsed().as_secs_f64() * 1000.0);
854 }
855 times.sort_by(|a, b| a.partial_cmp(b).unwrap());
856 println!(
857 "{messages:5} messages median {:8.2}ms max {:8.2}ms",
858 times[times.len() / 2],
859 times[times.len() - 1]
860 );
861 }
862}