nandi/jolt-nativepublic Fork 0
dce285fb5a5ec1f331b8afa7b2bdc4ed5e1bbd46
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 · 643 lines · 21.4 KBRust Blame HistoryRaw
Paint the same tree into a terminal, for the machines with no window a7f6202 nandi 17d 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 16d 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 16d 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
428#[test]
429fn an_emoji_is_two_columns_wide_and_what_follows_it_knows_that() {
430 let mut ui = Ui::new(12, 3);
431 let root = ui.tree.root();
432 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
433 node(&mut ui, row, "label", &[("label", "👍")]);
434 let after = node(&mut ui, row, "button", &[("label", "ok")]);
435 // A label that has to wrap wraps by column, not by character.
436 node(&mut ui, root, "label", &[("label", "👍👍👍👍👍👍👍")]);
437 ui.frame();
438 assert_eq!(ui.screen.line(0), "👍[ ok ]");
439 assert_eq!(ui.screen.line(1), "👍👍👍👍👍👍");
440
441 // The button starts at column 2, because the glyph before it took two.
442 assert!(ui.click(2, 0));
443 assert_eq!(
444 events(&mut ui),
445 vec![(after, "click".into(), String::new(), 0.0)]
446 );
447}
448
Paint the same tree into a terminal, for the machines with no window a7f6202 nandi 17d ago449#[test]
450fn an_overlay_floats_in_the_middle_over_whatever_was_under_it() {
451 let mut ui = Ui::new(14, 5);
452 let root = ui.tree.root();
453 node(&mut ui, root, "label", &[("label", "beneath")]);
454 let overlay = node(&mut ui, root, "overlay", &[("label", "Sure?")]);
455 node(&mut ui, overlay, "label", &[("label", "yes")]);
456 ui.frame();
457 assert_eq!(ui.screen.line(1), " ┌ Sure? ┐");
458 assert_eq!(ui.screen.line(2), " │yes │");
459 ui.key("esc");
460 assert_eq!(
461 events(&mut ui),
462 vec![(overlay, "close".into(), String::new(), 0.0)]
463 );
464}
465
466#[test]
467fn a_progress_bar_fills_the_share_of_its_width_it_was_given() {
468 let mut ui = Ui::new(10, 2);
469 let root = ui.tree.root();
470 let bar = node(&mut ui, root, "progress", &[]);
471 ui.tree.set(bar, "value", Value::Num(0.5));
472 ui.frame();
473 assert_eq!(ui.screen.line(0), "█████░░░░░");
474}
475
476#[test]
477fn colours_and_attributes_are_inherited_by_a_subtree() {
478 let mut ui = ui();
479 let root = ui.tree.root();
480 let column = node(&mut ui, root, "vbox", &[("color", "red")]);
481 ui.tree.set(column, "bold", Value::Bool(true));
482 node(&mut ui, column, "label", &[("label", "hi")]);
483 ui.frame();
484 let cell = ui.screen.cell(0, 0).unwrap();
485 assert_eq!(cell.style.fg, crate::screen::Color::Indexed(1));
486 assert!(cell.style.has(attr::BOLD));
487}
488
489#[test]
490fn an_unknown_tag_still_shows_its_children() {
491 let mut ui = ui();
492 let root = ui.tree.root();
493 let odd = node(&mut ui, root, "sparkline", &[]);
494 node(&mut ui, odd, "label", &[("label", "inside")]);
495 ui.frame();
496 assert_eq!(ui.screen.line(0), "inside");
497}
498
499#[test]
500fn focus_survives_a_repaint_and_lands_somewhere_when_its_widget_is_unmounted() {
501 let mut ui = ui();
502 let root = ui.tree.root();
503 let first = node(&mut ui, root, "button", &[("label", "one")]);
504 let second = node(&mut ui, root, "button", &[("label", "two")]);
505 ui.frame();
506 ui.key("tab");
507 assert_eq!(ui.focus(), second);
508 ui.frame();
509 assert_eq!(ui.focus(), second, "a repaint does not move the focus");
510 ui.tree.remove(root, second);
511 ui.frame();
512 assert_eq!(ui.focus(), first);
513}
Share a column's rows out at the width it has, not at its own height 5acc801 nandi 17d ago514
515#[test]
516fn an_unknown_leaf_paints_its_own_text() {
517 // A tag this backend does not know paints as a box so its children still
518 // show — but a leaf has none, and a `:status` badge or a `:link` in the
519 // middle of a message would otherwise be a hole in the sentence.
520 let mut ui = ui();
521 let root = ui.tree.root();
522 node(&mut ui, root, "status", &[("label", "joined")]);
523 node(&mut ui, root, "label", &[("label", "after")]);
524 ui.frame();
525 assert_eq!(ui.screen.line(0), "joined");
526 assert_eq!(ui.screen.line(1), "after");
527}
528
529#[test]
530fn a_nested_column_paints_every_child_and_not_only_the_first() {
531 // Its rows were shared out by measuring each child at the *rows* it had
532 // rather than the columns, so a label wrapped to a paragraph, the overrun
533 // came off the end, and everything after the first child was handed
534 // nothing. Two levels down is where it showed: the top box is as wide as
535 // the screen and as tall, so the two numbers were close enough to hide it.
536 let mut ui = ui();
537 let root = ui.tree.root();
538 let col = node(&mut ui, root, "vbox", &[("orientation", "vertical")]);
539 node(&mut ui, col, "label", &[("label", "the first line")]);
540 node(&mut ui, col, "button", &[("label", "Open")]);
541 ui.frame();
542 assert_eq!(ui.screen.line(0), "the first line");
543 assert_eq!(ui.screen.line(1), "[ Open ]");
544}
Let a backlog keep its place, and leave room for what is under it 68910bd nandi 17d ago545
546#[test]
547fn an_unknown_leaf_that_names_a_picture_paints_nothing() {
548 // An `:avatar`'s label is the nick behind the face — words for something
549 // that cannot be drawn here, and in frq already on the row beside it.
550 let mut ui = ui();
551 let root = ui.tree.root();
552 node(
553 &mut ui,
554 root,
555 "avatar",
556 &[("label", "nandi.uk"), ("src", "/tmp/a.png")],
557 );
558 node(&mut ui, root, "label", &[("label", "nandi.uk")]);
559 ui.frame();
560 assert_eq!(ui.screen.line(0), "nandi.uk");
561 assert_eq!(ui.screen.line(1), "");
562}
563
564#[test]
565fn a_sticky_viewport_opens_at_the_bottom_and_stays_there() {
566 // A backlog taller than its viewport, in a scroll that follows its own
567 // bottom: the newest line is what a chat client opens on, and a line
568 // arriving must not drag the screen out from under a reader who scrolled
569 // up to read history.
570 let mut ui = Ui::new(20, 3);
571 let root = ui.tree.root();
572 let scroll = node(
573 &mut ui,
574 root,
575 "scroll",
576 &[("scroll-key", "backlog"), ("stick-to-bottom", "true")],
577 );
578 for n in 1..=6 {
579 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
580 }
581 ui.frame();
582 assert_eq!(ui.screen.line(2), "line 6");
583
584 // Scrolled up, and it stays where it was put across a re-render — the
585 // position lives under the key, not in a prop the next render clears.
586 ui.wheel(0, 0, -3);
587 ui.frame();
588 assert_eq!(ui.screen.line(0), "line 1");
589 ui.tree.clear_props(scroll);
590 ui.tree.set(scroll, "scroll-key", Value::Str("backlog".into()));
591 ui.tree
592 .set(scroll, "stick-to-bottom", Value::Bool(true));
593 ui.frame();
594 assert_eq!(ui.screen.line(0), "line 1");
595
596 // Back down to the bottom, and it follows again.
597 ui.wheel(0, 0, 9);
598 ui.frame();
599 node(&mut ui, scroll, "label", &[("label", "line 7")]);
600 ui.frame();
601 assert_eq!(ui.screen.line(2), "line 7");
602}
603
604#[test]
605fn a_column_that_asks_for_a_width_gets_it_and_no_more() {
606 // Two panes in a row, the first with a width of its own. Its content is
607 // one long line, so measured naturally it is wider than the screen and the
608 // pane beside it is left nothing — which is the split view painting a
609 // sidebar and a ten-cell column of wrapped fragments.
610 let mut ui = Ui::new(40, 2);
611 let root = ui.tree.root();
612 let row = node(&mut ui, root, "hbox", &[("orientation", "horizontal")]);
613 let side = node(&mut ui, row, "vbox", &[]);
614 // A number, as the ABI sends one: a width read as a string is no width.
615 ui.tree.set(side, "width-request", Value::Num(10.0));
616 node(
617 &mut ui,
618 side,
619 "label",
620 &[("label", "a preview far longer than ten cells")],
621 );
622 let main = node(&mut ui, row, "vbox", &[]);
623 node(&mut ui, main, "label", &[("label", "the conversation")]);
624 ui.frame();
625 assert_eq!(ui.screen.line(0), "a preview the conversation");
626}
627
628#[test]
629fn a_backlog_taller_than_the_screen_leaves_the_compose_bar_its_row() {
630 // The shape of frq's chat screen: a viewport holding more than fits, and
631 // under it the things you act with. A column that cannot shrink hands the
632 // scroll every row it asks for and paints the entry off the bottom edge.
633 let mut ui = Ui::new(20, 4);
634 let root = ui.tree.root();
635 let scroll = node(&mut ui, root, "scroll", &[("scroll-key", "backlog")]);
636 for n in 1..=10 {
637 node(&mut ui, scroll, "label", &[("label", &format!("line {n}"))]);
638 }
639 node(&mut ui, root, "separator", &[]);
640 node(&mut ui, root, "entry", &[("placeholder", "Message")]);
641 ui.frame();
642 assert_eq!(ui.screen.line(3), "Message");
643}