forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB99bf2dfIvan Molodetskikh1 year ago2106 commits

window_opening.rs653 lines, 18.9 KiBHistoryRawDownload
1use std::fmt::{self, Write as _};
2
3use insta::assert_snapshot;
4use niri_config::Config;
5use rayon::iter::{IntoParallelIterator, ParallelIterator};
6
7use super::*;
8use crate::layout::LayoutElement;
9use crate::utils::with_toplevel_role;
10
11#[test]
12fn simple_no_workspaces() {
13 let mut f = Fixture::new();
14
15 let id = f.add_client();
16 let window = f.client(id).create_window();
17 let surface = window.surface.clone();
18 window.commit();
19 f.roundtrip(id);
20
21 let window = f.client(id).window(&surface);
22 assert_snapshot!(
23 window.format_recent_configures(),
24 @"size: 0 × 0, bounds: 0 × 0, states: []"
25 );
26
27 window.attach_new_buffer();
28 window.set_size(100, 100);
29 window.ack_last_and_commit();
30 f.double_roundtrip(id);
31
32 let window = f.client(id).window(&surface);
33 assert_snapshot!(
34 window.format_recent_configures(),
35 @"size: 100 × 688, bounds: 1248 × 688, states: []"
36 );
37}
38
39#[test]
40fn simple() {
41 let mut f = Fixture::new();
42 f.add_output(1, (1920, 1080));
43
44 let id = f.add_client();
45 let window = f.client(id).create_window();
46 let surface = window.surface.clone();
47 window.commit();
48 f.roundtrip(id);
49
50 let window = f.client(id).window(&surface);
51 assert_snapshot!(
52 window.format_recent_configures(),
53 @"size: 936 × 1048, bounds: 1888 × 1048, states: []"
54 );
55
56 window.attach_new_buffer();
57 window.ack_last_and_commit();
58 f.double_roundtrip(id);
59
60 let window = f.client(id).window(&surface);
61 assert_snapshot!(
62 window.format_recent_configures(),
63 @"size: 936 × 1048, bounds: 1888 × 1048, states: [Activated]"
64 );
65}
66
67#[test]
68fn dont_ack_initial_configure() {
69 let mut f = Fixture::new();
70 f.add_output(1, (1920, 1080));
71
72 let id = f.add_client();
73 let window = f.client(id).create_window();
74 let surface = window.surface.clone();
75 window.commit();
76 f.roundtrip(id);
77
78 let window = f.client(id).window(&surface);
79 window.attach_new_buffer();
80 // Don't ack the configure.
81 window.commit();
82 f.double_roundtrip(id);
83
84 // FIXME: Technically this is a protocol violation but uh. Smithay currently doesn't check it,
85 // and I'm not sure if it can be done generically in Smithay (because a compositor may not use
86 // its rendering helpers). I might add a check in niri itself sometime; I'm just not sure if
87 // there might be clients that this could break.
88 let window = f.client(id).window(&surface);
89 assert_snapshot!(
90 window.format_recent_configures(),
91 @r"
92 size: 936 × 1048, bounds: 1888 × 1048, states: []
93 size: 936 × 1048, bounds: 1888 × 1048, states: [Activated]
94 "
95 );
96}
97
98#[derive(Clone, Copy)]
99enum WantFullscreen {
100 No,
101 UnsetBeforeInitial,
102 BeforeInitial(Option<&'static str>),
103 UnsetAfterInitial,
104 AfterInitial(Option<&'static str>),
105}
106
107impl fmt::Display for WantFullscreen {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 match self {
110 WantFullscreen::No => write!(f, "U")?,
111 WantFullscreen::UnsetBeforeInitial => write!(f, "BU")?,
112 WantFullscreen::UnsetAfterInitial => write!(f, "AU")?,
113 WantFullscreen::BeforeInitial(m) => write!(f, "B{}", m.unwrap_or("N"))?,
114 WantFullscreen::AfterInitial(m) => write!(f, "A{}", m.unwrap_or("N"))?,
115 }
116 Ok(())
117 }
118}
119
120#[derive(Clone, Copy)]
121enum SetParent {
122 BeforeInitial(&'static str),
123 AfterInitial(&'static str),
124}
125
126impl fmt::Display for SetParent {
127 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 match self {
129 SetParent::BeforeInitial(m) => write!(f, "B{m}")?,
130 SetParent::AfterInitial(m) => write!(f, "A{m}")?,
131 }
132 Ok(())
133 }
134}
135
136#[derive(Clone, Copy)]
137enum DefaultSize {
138 WindowChooses,
139 Proportion(&'static str),
140 Fixed(&'static str),
141}
142
143impl fmt::Display for DefaultSize {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 match self {
146 DefaultSize::WindowChooses => write!(f, "U"),
147 DefaultSize::Proportion(prop) => write!(f, "P{prop}"),
148 DefaultSize::Fixed(fixed) => write!(f, "F{fixed}"),
149 }
150 }
151}
152
153#[test]
154fn target_output_and_workspaces() {
155 // Here we test a massive powerset of settings that can affect where a window opens:
156 //
157 // * open-on-workspace
158 // * open-on-output
159 // * has parent (windows will open next to their parent)
160 // * want fullscreen (windows can request the target fullscreen output)
161 // * open-fullscreen (can deny the fullscreen request)
162
163 let open_on_workspace = [None, Some("1"), Some("2")];
164 let open_on_output = [None, Some("1"), Some("2")];
165 let open_fullscreen = [None, Some("false"), Some("true")];
166 let want_fullscreen = [
167 WantFullscreen::No,
168 WantFullscreen::UnsetBeforeInitial, // GTK 4
169 WantFullscreen::BeforeInitial(None),
170 WantFullscreen::BeforeInitial(Some("1")),
171 WantFullscreen::BeforeInitial(Some("2")),
172 WantFullscreen::UnsetAfterInitial,
173 // mpv, osu!
174 WantFullscreen::AfterInitial(None),
175 WantFullscreen::AfterInitial(Some("1")),
176 WantFullscreen::AfterInitial(Some("2")),
177 ];
178 let set_parent = [
179 None,
180 Some(SetParent::BeforeInitial("1")),
181 Some(SetParent::BeforeInitial("2")),
182 Some(SetParent::AfterInitial("1")),
183 Some(SetParent::AfterInitial("2")),
184 ];
185
186 let mut powerset = Vec::new();
187 for ws in open_on_workspace {
188 for out in open_on_output {
189 for fs in open_fullscreen {
190 for wfs in want_fullscreen {
191 for sp in set_parent {
192 powerset.push((ws, out, fs, wfs, sp));
193 }
194 }
195 }
196 }
197 }
198
199 powerset.into_par_iter().for_each(|(ws, out, fs, wfs, sp)| {
200 check_target_output_and_workspace(ws, out, fs, wfs, sp);
201 });
202}
203
204fn check_target_output_and_workspace(
205 open_on_workspace: Option<&str>,
206 open_on_output: Option<&str>,
207 open_fullscreen: Option<&str>,
208 want_fullscreen: WantFullscreen,
209 set_parent: Option<SetParent>,
210) {
211 let mut snapshot_desc = Vec::new();
212 let mut snapshot_suffix = Vec::new();
213
214 let mut config = String::from(
215 r##"
216workspace "ws-1" {
217 open-on-output "headless-1"
218}
219
220workspace "ws-2" {
221 open-on-output "headless-2"
222}
223
224window-rule {
225 exclude title="parent"
226
227"##,
228 );
229
230 if let Some(x) = open_on_workspace {
231 writeln!(config, " open-on-workspace \"ws-{x}\"").unwrap();
232 snapshot_suffix.push(format!("ws{x}"));
233 }
234
235 if let Some(x) = open_on_output {
236 writeln!(config, " open-on-output \"headless-{x}\"").unwrap();
237 snapshot_suffix.push(format!("out{x}"));
238 }
239
240 if let Some(x) = open_fullscreen {
241 writeln!(config, " open-fullscreen {x}").unwrap();
242
243 let x = if x == "true" { "T" } else { "F" };
244 snapshot_suffix.push(format!("fs{x}"));
245 }
246 config.push('}');
247
248 match &want_fullscreen {
249 WantFullscreen::No => (),
250 x => {
251 snapshot_desc.push(format!("want fullscreen: {x}"));
252 snapshot_suffix.push(format!("wfs{x}"));
253 }
254 }
255
256 if let Some(set_parent) = set_parent {
257 let mon = match set_parent {
258 SetParent::BeforeInitial(mon) => mon,
259 SetParent::AfterInitial(mon) => mon,
260 };
261 write!(
262 config,
263 "
264
265window-rule {{
266 match title=\"parent\"
267 open-on-output \"headless-{mon}\"
268}}"
269 )
270 .unwrap();
271
272 snapshot_desc.push(format!("set parent: {set_parent}"));
273 snapshot_suffix.push(format!("sp{set_parent}"));
274 }
275
276 snapshot_desc.push(format!("config:{config}"));
277
278 let config = Config::parse("config.kdl", &config).unwrap();
279
280 let mut f = Fixture::with_config(config);
281 f.add_output(1, (1280, 720));
282 f.add_output(2, (1920, 1080));
283
284 let id = f.add_client();
285
286 // To get output names.
287 f.roundtrip(id);
288
289 let mut parent = None;
290 if set_parent.is_some() {
291 let window = f.client(id).create_window();
292 let surface = window.surface.clone();
293 parent = Some(window.xdg_toplevel.clone());
294 window.set_title("parent");
295 window.commit();
296 f.roundtrip(id);
297
298 let window = f.client(id).window(&surface);
299 window.attach_new_buffer();
300 window.ack_last_and_commit();
301 f.roundtrip(id);
302 }
303
304 let client = f.client(id);
305 let window = client.create_window();
306 let surface = window.surface.clone();
307
308 if let Some(SetParent::BeforeInitial(_)) = set_parent {
309 client.window(&surface).set_parent(parent.as_ref());
310 }
311
312 if let WantFullscreen::UnsetBeforeInitial = want_fullscreen {
313 client.window(&surface).unset_fullscreen();
314 } else if let WantFullscreen::BeforeInitial(mon) = want_fullscreen {
315 let output = mon.map(|mon| client.output(&format!("headless-{mon}")));
316 client.window(&surface).set_fullscreen(output.as_ref());
317 }
318
319 client.window(&surface).commit();
320 f.roundtrip(id);
321
322 let client = f.client(id);
323 let initial = client.window(&surface).format_recent_configures();
324
325 if let Some(SetParent::AfterInitial(_)) = set_parent {
326 client.window(&surface).set_parent(parent.as_ref());
327 }
328
329 if let WantFullscreen::UnsetAfterInitial = want_fullscreen {
330 client.window(&surface).unset_fullscreen();
331 } else if let WantFullscreen::AfterInitial(mon) = want_fullscreen {
332 let output = mon.map(|mon| client.output(&format!("headless-{mon}")));
333 client.window(&surface).set_fullscreen(output.as_ref());
334 }
335
336 let window = client.window(&surface);
337 window.attach_new_buffer();
338 let serial = window.configures_received.last().unwrap().0;
339 window.ack_last_and_commit();
340 f.double_roundtrip(id);
341
342 // Commit to the post-initial configures.
343 let window = f.client(id).window(&surface);
344 let new_serial = window.configures_received.last().unwrap().0;
345 if new_serial != serial {
346 window.ack_last_and_commit();
347 f.double_roundtrip(id);
348 }
349
350 let niri = f.niri();
351 let (mon, ws_idx, ws, mapped) = niri
352 .layout
353 .workspaces()
354 .find_map(|(mon, ws_idx, ws)| {
355 ws.windows().find_map(|win| {
356 if with_toplevel_role(win.toplevel(), |role| {
357 role.title.as_deref() != Some("parent")
358 }) {
359 Some((mon, ws_idx, ws, win))
360 } else {
361 None
362 }
363 })
364 })
365 .unwrap();
366 let is_fullscreen = mapped.is_fullscreen();
367 let win = mapped.window.clone();
368 let mon = mon.unwrap().output_name().clone();
369 let ws = ws.name().cloned().unwrap_or(String::from("unnamed"));
370
371 let window = f.client(id).window(&surface);
372 let post_map = window.format_recent_configures();
373
374 // If the window ended up fullscreen, unfullscreen it and output the configure.
375 let mut post_unfullscreen = String::new();
376 if is_fullscreen {
377 f.niri().layout.set_fullscreen(&win, false);
378 f.double_roundtrip(id);
379
380 let window = f.client(id).window(&surface);
381 post_unfullscreen = format!(
382 "\n\nunfullscreen configure:\n{}",
383 window.format_recent_configures()
384 );
385 }
386
387 let snapshot = format!(
388 "\
389final monitor: {mon}
390final workspace: {ws_idx} ({ws})
391
392initial configure:
393{initial}
394
395post-map configures:
396{post_map}{post_unfullscreen}",
397 );
398
399 let mut settings = insta::Settings::clone_current();
400 settings.set_snapshot_suffix(snapshot_suffix.join("-"));
401 settings.set_description(snapshot_desc.join("\n"));
402 let _guard = settings.bind_to_scope();
403 assert_snapshot!(snapshot);
404}
405
406#[test]
407fn target_size() {
408 if std::env::var_os("RUN_SLOW_TESTS").is_none() {
409 eprintln!("ignoring slow test");
410 return;
411 }
412
413 // Here we test a massive powerset of settings that can affect the window size:
414 //
415 // * want fullscreen
416 // * open-fullscreen
417 // * open-maximized
418 // * open-floating
419 // * default-column-width
420 // * border
421 // * default-column-display normal, tabbed
422
423 let open_fullscreen = [None, Some("false"), Some("true")];
424 let want_fullscreen = [
425 WantFullscreen::No,
426 WantFullscreen::UnsetBeforeInitial, // GTK 4
427 WantFullscreen::BeforeInitial(None),
428 WantFullscreen::UnsetAfterInitial,
429 // mpv, osu!
430 WantFullscreen::AfterInitial(None),
431 ];
432 let open_maximized = [None, Some("true")];
433 let open_floating = [None, Some("true")];
434 let default_column_width = [
435 None,
436 Some(DefaultSize::WindowChooses),
437 Some(DefaultSize::Proportion("0.25")),
438 Some(DefaultSize::Fixed("1000")),
439 ];
440 let default_window_height = [
441 None,
442 Some(DefaultSize::WindowChooses),
443 Some(DefaultSize::Proportion("0.5")),
444 Some(DefaultSize::Fixed("500")),
445 ];
446 let border = [false, true];
447 let tabbed = [false, true];
448
449 let mut powerset = Vec::new();
450 for fs in open_fullscreen {
451 for wfs in want_fullscreen {
452 for om in open_maximized {
453 for of in open_floating {
454 for dw in default_column_width {
455 for dh in default_window_height {
456 for b in border {
457 for t in tabbed {
458 powerset.push((fs, wfs, om, of, dw, dh, b, t));
459 }
460 }
461 }
462 }
463 }
464 }
465 }
466 }
467
468 powerset
469 .into_par_iter()
470 .for_each(|(fs, wfs, om, of, dw, dh, b, t)| {
471 check_target_size(fs, wfs, om, of, dw, dh, b, t);
472 });
473}
474
475#[allow(clippy::too_many_arguments)]
476fn check_target_size(
477 open_fullscreen: Option<&str>,
478 want_fullscreen: WantFullscreen,
479 open_maximized: Option<&str>,
480 open_floating: Option<&str>,
481 default_width: Option<DefaultSize>,
482 default_height: Option<DefaultSize>,
483 border: bool,
484 tabbed: bool,
485) {
486 let mut snapshot_desc = Vec::new();
487 let mut snapshot_suffix = Vec::new();
488
489 let mut config = String::from(
490 r##"
491window-rule {
492"##,
493 );
494
495 if let Some(x) = open_fullscreen {
496 writeln!(config, " open-fullscreen {x}").unwrap();
497
498 let x = if x == "true" { "T" } else { "F" };
499 snapshot_suffix.push(format!("fs{x}"));
500 }
501
502 if let Some(x) = open_maximized {
503 writeln!(config, " open-maximized {x}").unwrap();
504
505 let x = if x == "true" { "T" } else { "F" };
506 snapshot_suffix.push(format!("om{x}"));
507 }
508
509 if let Some(x) = open_floating {
510 writeln!(config, " open-floating {x}").unwrap();
511
512 let x = if x == "true" { "T" } else { "F" };
513 snapshot_suffix.push(format!("of{x}"));
514 }
515
516 if let Some(x) = default_width {
517 let value = match x {
518 DefaultSize::WindowChooses => String::new(),
519 DefaultSize::Proportion(prop) => format!("proportion {prop};"),
520 DefaultSize::Fixed(fixed) => format!("fixed {fixed};"),
521 };
522 writeln!(config, " default-column-width {{ {value} }}").unwrap();
523
524 snapshot_suffix.push(format!("dw{x}"));
525 }
526
527 if let Some(x) = default_height {
528 let value = match x {
529 DefaultSize::WindowChooses => String::new(),
530 DefaultSize::Proportion(prop) => format!("proportion {prop};"),
531 DefaultSize::Fixed(fixed) => format!("fixed {fixed};"),
532 };
533 writeln!(config, " default-window-height {{ {value} }}").unwrap();
534
535 snapshot_suffix.push(format!("dh{x}"));
536 }
537
538 if border {
539 writeln!(config, " border {{ on; }}").unwrap();
540 snapshot_suffix.push(String::from("b"));
541 }
542
543 if tabbed {
544 writeln!(config, " default-column-display \"tabbed\"").unwrap();
545 }
546
547 config.push('}');
548
549 match &want_fullscreen {
550 WantFullscreen::No => (),
551 x => {
552 snapshot_desc.push(format!("want fullscreen: {x}"));
553 snapshot_suffix.push(format!("wfs{x}"));
554 }
555 }
556
557 if tabbed {
558 config.push_str(
559 "\n
560layout {
561 tab-indicator {
562 place-within-column
563 }
564}",
565 );
566 snapshot_suffix.push(String::from("t"));
567 }
568
569 snapshot_desc.push(format!("config:{config}"));
570
571 let config = Config::parse("config.kdl", &config).unwrap();
572
573 let mut f = Fixture::with_config(config);
574 f.add_output(1, (1280, 720));
575 f.add_output(2, (1920, 1080));
576
577 let id = f.add_client();
578
579 // To get output names.
580 f.roundtrip(id);
581
582 let client = f.client(id);
583 let window = client.create_window();
584 let surface = window.surface.clone();
585
586 if let WantFullscreen::UnsetBeforeInitial = want_fullscreen {
587 client.window(&surface).unset_fullscreen();
588 } else if let WantFullscreen::BeforeInitial(mon) = want_fullscreen {
589 let output = mon.map(|mon| client.output(&format!("headless-{mon}")));
590 client.window(&surface).set_fullscreen(output.as_ref());
591 }
592
593 client.window(&surface).commit();
594 f.roundtrip(id);
595
596 let client = f.client(id);
597 let initial = client.window(&surface).format_recent_configures();
598
599 if let WantFullscreen::UnsetAfterInitial = want_fullscreen {
600 client.window(&surface).unset_fullscreen();
601 } else if let WantFullscreen::AfterInitial(mon) = want_fullscreen {
602 let output = mon.map(|mon| client.output(&format!("headless-{mon}")));
603 client.window(&surface).set_fullscreen(output.as_ref());
604 }
605
606 let window = client.window(&surface);
607 window.attach_new_buffer();
608 let serial = window.configures_received.last().unwrap().0;
609 window.ack_last_and_commit();
610 f.double_roundtrip(id);
611
612 // Commit to the post-initial configures.
613 let window = f.client(id).window(&surface);
614 let new_serial = window.configures_received.last().unwrap().0;
615 if new_serial != serial {
616 window.ack_last_and_commit();
617 f.double_roundtrip(id);
618 }
619
620 let window = f.client(id).window(&surface);
621 let post_map = window.format_recent_configures();
622
623 // If the window ended up fullscreen, unfullscreen it and output the configure.
624 let mut post_unfullscreen = String::new();
625 let mapped = f.niri().layout.windows().next().unwrap().1;
626 let is_fullscreen = mapped.is_fullscreen();
627 let win = mapped.window.clone();
628 if is_fullscreen {
629 f.niri().layout.set_fullscreen(&win, false);
630 f.double_roundtrip(id);
631
632 let window = f.client(id).window(&surface);
633 post_unfullscreen = format!(
634 "\n\nunfullscreen configure:\n{}",
635 window.format_recent_configures()
636 );
637 }
638
639 let snapshot = format!(
640 "\
641initial configure:
642{initial}
643
644post-map configures:
645{post_map}{post_unfullscreen}",
646 );
647
648 let mut settings = insta::Settings::clone_current();
649 settings.set_snapshot_suffix(snapshot_suffix.join("-"));
650 settings.set_description(snapshot_desc.join("\n"));
651 let _guard = settings.bind_to_scope();
652 assert_snapshot!(snapshot);
653}