niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::fmt::{self, Write as _}; |
| 2 | |
| 3 | use insta::assert_snapshot; |
| 4 | use niri_config::Config; |
| 5 | use rayon::iter::{IntoParallelIterator, ParallelIterator}; |
| 6 | |
| 7 | use super::*; |
| 8 | use crate::layout::LayoutElement as _; |
| 9 | use crate::utils::spawning::store_and_increase_nofile_rlimit; |
| 10 | use crate::utils::with_toplevel_role; |
| 11 | |
| 12 | #[test] |
| 13 | fn simple_no_workspaces() { |
| 14 | let mut f = Fixture::new(); |
| 15 | |
| 16 | let id = f.add_client(); |
| 17 | let window = f.client(id).create_window(); |
| 18 | let surface = window.surface.clone(); |
| 19 | window.commit(); |
| 20 | f.roundtrip(id); |
| 21 | |
| 22 | let window = f.client(id).window(&surface); |
| 23 | assert_snapshot!( |
| 24 | window.format_recent_configures(), |
| 25 | @"size: 0 × 0, bounds: 0 × 0, states: []" |
| 26 | ); |
| 27 | |
| 28 | window.attach_new_buffer(); |
| 29 | window.set_size(100, 100); |
| 30 | window.ack_last_and_commit(); |
| 31 | f.double_roundtrip(id); |
| 32 | |
| 33 | let window = f.client(id).window(&surface); |
| 34 | assert_snapshot!( |
| 35 | window.format_recent_configures(), |
| 36 | @"size: 100 × 688, bounds: 1248 × 688, states: []" |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn simple() { |
| 42 | let mut f = Fixture::new(); |
| 43 | f.add_output(1, (1920, 1080)); |
| 44 | |
| 45 | let id = f.add_client(); |
| 46 | let window = f.client(id).create_window(); |
| 47 | let surface = window.surface.clone(); |
| 48 | window.commit(); |
| 49 | f.roundtrip(id); |
| 50 | |
| 51 | let window = f.client(id).window(&surface); |
| 52 | assert_snapshot!( |
| 53 | window.format_recent_configures(), |
| 54 | @"size: 936 × 1048, bounds: 1888 × 1048, states: []" |
| 55 | ); |
| 56 | |
| 57 | window.attach_new_buffer(); |
| 58 | window.ack_last_and_commit(); |
| 59 | f.double_roundtrip(id); |
| 60 | |
| 61 | let window = f.client(id).window(&surface); |
| 62 | assert_snapshot!( |
| 63 | window.format_recent_configures(), |
| 64 | @"size: 936 × 1048, bounds: 1888 × 1048, states: [Activated]" |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | #[should_panic(expected = "Protocol error 3 on object xdg_surface")] |
| 70 | fn dont_ack_initial_configure() { |
| 71 | let mut f = Fixture::new(); |
| 72 | f.add_output(1, (1920, 1080)); |
| 73 | |
| 74 | let id = f.add_client(); |
| 75 | let window = f.client(id).create_window(); |
| 76 | let surface = window.surface.clone(); |
| 77 | window.commit(); |
| 78 | f.roundtrip(id); |
| 79 | |
| 80 | let window = f.client(id).window(&surface); |
| 81 | window.attach_new_buffer(); |
| 82 | // Don't ack the configure. |
| 83 | window.commit(); |
| 84 | f.double_roundtrip(id); |
| 85 | } |
| 86 | |
| 87 | #[derive(Clone, Copy)] |
| 88 | enum WantFullscreen { |
| 89 | No, |
| 90 | UnsetBeforeInitial, |
| 91 | BeforeInitial(Option<&'static str>), |
| 92 | UnsetAfterInitial, |
| 93 | AfterInitial(Option<&'static str>), |
| 94 | } |
| 95 | |
| 96 | impl fmt::Display for WantFullscreen { |
| 97 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 98 | match self { |
| 99 | WantFullscreen::No => write!(f, "U")?, |
| 100 | WantFullscreen::UnsetBeforeInitial => write!(f, "BU")?, |
| 101 | WantFullscreen::UnsetAfterInitial => write!(f, "AU")?, |
| 102 | WantFullscreen::BeforeInitial(m) => write!(f, "B{}", m.unwrap_or("N"))?, |
| 103 | WantFullscreen::AfterInitial(m) => write!(f, "A{}", m.unwrap_or("N"))?, |
| 104 | } |
| 105 | Ok(()) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | #[derive(Clone, Copy)] |
| 110 | enum WantMaximized { |
| 111 | No, |
| 112 | UnsetBeforeInitial, |
| 113 | BeforeInitial, |
| 114 | UnsetAfterInitial, |
| 115 | AfterInitial, |
| 116 | } |
| 117 | |
| 118 | impl fmt::Display for WantMaximized { |
| 119 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 120 | match self { |
| 121 | WantMaximized::No => write!(f, "U")?, |
| 122 | WantMaximized::UnsetBeforeInitial => write!(f, "BU")?, |
| 123 | WantMaximized::UnsetAfterInitial => write!(f, "AU")?, |
| 124 | WantMaximized::BeforeInitial => write!(f, "B")?, |
| 125 | WantMaximized::AfterInitial => write!(f, "A")?, |
| 126 | } |
| 127 | Ok(()) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | #[derive(Clone, Copy)] |
| 132 | enum SetParent { |
| 133 | BeforeInitial(&'static str), |
| 134 | AfterInitial(&'static str), |
| 135 | } |
| 136 | |
| 137 | impl fmt::Display for SetParent { |
| 138 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 139 | match self { |
| 140 | SetParent::BeforeInitial(m) => write!(f, "B{m}")?, |
| 141 | SetParent::AfterInitial(m) => write!(f, "A{m}")?, |
| 142 | } |
| 143 | Ok(()) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[derive(Clone, Copy)] |
| 148 | enum DefaultSize { |
| 149 | WindowChooses, |
| 150 | Proportion(&'static str), |
| 151 | Fixed(&'static str), |
| 152 | } |
| 153 | |
| 154 | impl fmt::Display for DefaultSize { |
| 155 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 156 | match self { |
| 157 | DefaultSize::WindowChooses => write!(f, "U"), |
| 158 | DefaultSize::Proportion(prop) => write!(f, "P{prop}"), |
| 159 | DefaultSize::Fixed(fixed) => write!(f, "F{fixed}"), |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn target_output_and_workspaces() { |
| 166 | store_and_increase_nofile_rlimit(); |
| 167 | |
| 168 | // Here we test a massive powerset of settings that can affect where a window opens: |
| 169 | // |
| 170 | // * open-on-workspace |
| 171 | // * open-on-output |
| 172 | // * has parent (windows will open next to their parent) |
| 173 | // * want fullscreen (windows can request the target fullscreen output) |
| 174 | // * open-fullscreen (can deny the fullscreen request) |
| 175 | |
| 176 | let open_on_workspace = [None, Some("1"), Some("2")]; |
| 177 | let open_on_output = [None, Some("1"), Some("2")]; |
| 178 | let open_fullscreen = [None, Some("false"), Some("true")]; |
| 179 | let want_fullscreen = [ |
| 180 | WantFullscreen::No, |
| 181 | WantFullscreen::UnsetBeforeInitial, // GTK 4 |
| 182 | WantFullscreen::BeforeInitial(None), |
| 183 | WantFullscreen::BeforeInitial(Some("1")), |
| 184 | WantFullscreen::BeforeInitial(Some("2")), |
| 185 | WantFullscreen::UnsetAfterInitial, |
| 186 | // mpv, osu! |
| 187 | WantFullscreen::AfterInitial(None), |
| 188 | WantFullscreen::AfterInitial(Some("1")), |
| 189 | WantFullscreen::AfterInitial(Some("2")), |
| 190 | ]; |
| 191 | let set_parent = [ |
| 192 | None, |
| 193 | Some(SetParent::BeforeInitial("1")), |
| 194 | Some(SetParent::BeforeInitial("2")), |
| 195 | Some(SetParent::AfterInitial("1")), |
| 196 | Some(SetParent::AfterInitial("2")), |
| 197 | ]; |
| 198 | |
| 199 | let mut powerset = Vec::new(); |
| 200 | for ws in open_on_workspace { |
| 201 | for out in open_on_output { |
| 202 | for fs in open_fullscreen { |
| 203 | for wfs in want_fullscreen { |
| 204 | for sp in set_parent { |
| 205 | powerset.push((ws, out, fs, wfs, sp)); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | powerset.into_par_iter().for_each(|(ws, out, fs, wfs, sp)| { |
| 213 | check_target_output_and_workspace(ws, out, fs, wfs, sp); |
| 214 | }); |
| 215 | } |
| 216 | |
| 217 | fn check_target_output_and_workspace( |
| 218 | open_on_workspace: Option<&str>, |
| 219 | open_on_output: Option<&str>, |
| 220 | open_fullscreen: Option<&str>, |
| 221 | want_fullscreen: WantFullscreen, |
| 222 | set_parent: Option<SetParent>, |
| 223 | ) { |
| 224 | let mut snapshot_desc = Vec::new(); |
| 225 | let mut snapshot_suffix = Vec::new(); |
| 226 | |
| 227 | let mut config = String::from( |
| 228 | r##" |
| 229 | output "headless-2" { |
| 230 | layout { |
| 231 | border { |
| 232 | on |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | workspace "ws-1" { |
| 238 | open-on-output "headless-1" |
| 239 | } |
| 240 | |
| 241 | workspace "ws-2" { |
| 242 | open-on-output "headless-2" |
| 243 | |
| 244 | layout { |
| 245 | border { |
| 246 | width 10 |
| 247 | } |
| 248 | |
| 249 | default-column-width { |
| 250 | fixed 500 |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | window-rule { |
| 256 | exclude title="parent" |
| 257 | |
| 258 | "##, |
| 259 | ); |
| 260 | |
| 261 | if let Some(x) = open_on_workspace { |
| 262 | writeln!(config, " open-on-workspace \"ws-{x}\"").unwrap(); |
| 263 | snapshot_suffix.push(format!("ws{x}")); |
| 264 | } |
| 265 | |
| 266 | if let Some(x) = open_on_output { |
| 267 | writeln!(config, " open-on-output \"headless-{x}\"").unwrap(); |
| 268 | snapshot_suffix.push(format!("out{x}")); |
| 269 | } |
| 270 | |
| 271 | if let Some(x) = open_fullscreen { |
| 272 | writeln!(config, " open-fullscreen {x}").unwrap(); |
| 273 | |
| 274 | let x = if x == "true" { "T" } else { "F" }; |
| 275 | snapshot_suffix.push(format!("fs{x}")); |
| 276 | } |
| 277 | config.push('}'); |
| 278 | |
| 279 | match &want_fullscreen { |
| 280 | WantFullscreen::No => (), |
| 281 | x => { |
| 282 | snapshot_desc.push(format!("want fullscreen: {x}")); |
| 283 | snapshot_suffix.push(format!("wfs{x}")); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | if let Some(set_parent) = set_parent { |
| 288 | let mon = match set_parent { |
| 289 | SetParent::BeforeInitial(mon) => mon, |
| 290 | SetParent::AfterInitial(mon) => mon, |
| 291 | }; |
| 292 | write!( |
| 293 | config, |
| 294 | " |
| 295 | |
| 296 | window-rule {{ |
| 297 | match title=\"parent\" |
| 298 | open-on-output \"headless-{mon}\" |
| 299 | }}" |
| 300 | ) |
| 301 | .unwrap(); |
| 302 | |
| 303 | snapshot_desc.push(format!("set parent: {set_parent}")); |
| 304 | snapshot_suffix.push(format!("sp{set_parent}")); |
| 305 | } |
| 306 | |
| 307 | snapshot_desc.push(format!("config:{config}")); |
| 308 | |
| 309 | let config = Config::parse_mem(&config).unwrap(); |
| 310 | |
| 311 | let mut f = Fixture::with_config(config); |
| 312 | f.add_output(1, (1280, 720)); |
| 313 | f.add_output(2, (1920, 1080)); |
| 314 | |
| 315 | let id = f.add_client(); |
| 316 | |
| 317 | // To get output names. |
| 318 | f.roundtrip(id); |
| 319 | |
| 320 | let mut parent = None; |
| 321 | if set_parent.is_some() { |
| 322 | let window = f.client(id).create_window(); |
| 323 | let surface = window.surface.clone(); |
| 324 | parent = Some(window.xdg_toplevel.clone()); |
| 325 | window.set_title("parent"); |
| 326 | window.commit(); |
| 327 | f.roundtrip(id); |
| 328 | |
| 329 | let window = f.client(id).window(&surface); |
| 330 | window.attach_new_buffer(); |
| 331 | window.ack_last_and_commit(); |
| 332 | f.roundtrip(id); |
| 333 | } |
| 334 | |
| 335 | let client = f.client(id); |
| 336 | let window = client.create_window(); |
| 337 | let surface = window.surface.clone(); |
| 338 | |
| 339 | if let Some(SetParent::BeforeInitial(_)) = set_parent { |
| 340 | client.window(&surface).set_parent(parent.as_ref()); |
| 341 | } |
| 342 | |
| 343 | if let WantFullscreen::UnsetBeforeInitial = want_fullscreen { |
| 344 | client.window(&surface).unset_fullscreen(); |
| 345 | } else if let WantFullscreen::BeforeInitial(mon) = want_fullscreen { |
| 346 | let output = mon.map(|mon| client.output(&format!("headless-{mon}"))); |
| 347 | client.window(&surface).set_fullscreen(output.as_ref()); |
| 348 | } |
| 349 | |
| 350 | client.window(&surface).commit(); |
| 351 | f.roundtrip(id); |
| 352 | |
| 353 | let client = f.client(id); |
| 354 | let initial = client.window(&surface).format_recent_configures(); |
| 355 | |
| 356 | if let Some(SetParent::AfterInitial(_)) = set_parent { |
| 357 | client.window(&surface).set_parent(parent.as_ref()); |
| 358 | } |
| 359 | |
| 360 | if let WantFullscreen::UnsetAfterInitial = want_fullscreen { |
| 361 | client.window(&surface).unset_fullscreen(); |
| 362 | } else if let WantFullscreen::AfterInitial(mon) = want_fullscreen { |
| 363 | let output = mon.map(|mon| client.output(&format!("headless-{mon}"))); |
| 364 | client.window(&surface).set_fullscreen(output.as_ref()); |
| 365 | } |
| 366 | |
| 367 | let window = client.window(&surface); |
| 368 | window.attach_new_buffer(); |
| 369 | let serial = window.configures_received.last().unwrap().0; |
| 370 | window.ack_last_and_commit(); |
| 371 | f.double_roundtrip(id); |
| 372 | |
| 373 | // Commit to the post-initial configures. |
| 374 | let window = f.client(id).window(&surface); |
| 375 | let new_serial = window.configures_received.last().unwrap().0; |
| 376 | if new_serial != serial { |
| 377 | window.ack_last_and_commit(); |
| 378 | f.double_roundtrip(id); |
| 379 | } |
| 380 | |
| 381 | let niri = f.niri(); |
| 382 | let (mon, ws_idx, ws, mapped) = niri |
| 383 | .layout |
| 384 | .workspaces() |
| 385 | .find_map(|(mon, ws_idx, ws)| { |
| 386 | ws.windows().find_map(|win| { |
| 387 | if with_toplevel_role(win.toplevel(), |role| { |
| 388 | role.title.as_deref() != Some("parent") |
| 389 | }) { |
| 390 | Some((mon, ws_idx, ws, win)) |
| 391 | } else { |
| 392 | None |
| 393 | } |
| 394 | }) |
| 395 | }) |
| 396 | .unwrap(); |
| 397 | let is_fullscreen = mapped.sizing_mode().is_fullscreen(); |
| 398 | let win = mapped.window.clone(); |
| 399 | let mon = mon.unwrap().output_name().clone(); |
| 400 | let ws = ws.name().cloned().unwrap_or(String::from("unnamed")); |
| 401 | |
| 402 | let window = f.client(id).window(&surface); |
| 403 | let post_map = window.format_recent_configures(); |
| 404 | |
| 405 | // If the window ended up fullscreen, unfullscreen it and output the configure. |
| 406 | let mut post_unfullscreen = String::new(); |
| 407 | if is_fullscreen { |
| 408 | f.niri().layout.set_fullscreen(&win, false); |
| 409 | f.double_roundtrip(id); |
| 410 | |
| 411 | let window = f.client(id).window(&surface); |
| 412 | post_unfullscreen = format!( |
| 413 | "\n\nunfullscreen configure:\n{}", |
| 414 | window.format_recent_configures() |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | let snapshot = format!( |
| 419 | "\ |
| 420 | final monitor: {mon} |
| 421 | final workspace: {ws_idx} ({ws}) |
| 422 | |
| 423 | initial configure: |
| 424 | {initial} |
| 425 | |
| 426 | post-map configures: |
| 427 | {post_map}{post_unfullscreen}", |
| 428 | ); |
| 429 | |
| 430 | let mut settings = insta::Settings::clone_current(); |
| 431 | settings.set_snapshot_suffix(snapshot_suffix.join("-")); |
| 432 | settings.set_description(snapshot_desc.join("\n")); |
| 433 | let _guard = settings.bind_to_scope(); |
| 434 | assert_snapshot!(snapshot); |
| 435 | } |
| 436 | |
| 437 | #[test] |
| 438 | fn target_size() { |
| 439 | if std::env::var_os("RUN_SLOW_TESTS").is_none() { |
| 440 | eprintln!("ignoring slow test"); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | store_and_increase_nofile_rlimit(); |
| 445 | |
| 446 | // Here we test a massive powerset of settings that can affect the window size: |
| 447 | // |
| 448 | // * want fullscreen |
| 449 | // * open-fullscreen |
| 450 | // * open-maximized |
| 451 | // * open-floating |
| 452 | // * default-column-width |
| 453 | // * border |
| 454 | // * default-column-display normal, tabbed |
| 455 | |
| 456 | let open_fullscreen = [None, Some("false"), Some("true")]; |
| 457 | let want_fullscreen = [ |
| 458 | WantFullscreen::No, |
| 459 | WantFullscreen::UnsetBeforeInitial, // GTK 4 |
| 460 | WantFullscreen::BeforeInitial(None), |
| 461 | WantFullscreen::UnsetAfterInitial, |
| 462 | // mpv, osu! |
| 463 | WantFullscreen::AfterInitial(None), |
| 464 | ]; |
| 465 | let open_maximized = [None, Some("true")]; |
| 466 | let open_floating = [None, Some("true")]; |
| 467 | let default_column_width = [ |
| 468 | None, |
| 469 | Some(DefaultSize::WindowChooses), |
| 470 | Some(DefaultSize::Proportion("0.25")), |
| 471 | Some(DefaultSize::Fixed("1000")), |
| 472 | ]; |
| 473 | let default_window_height = [ |
| 474 | None, |
| 475 | Some(DefaultSize::WindowChooses), |
| 476 | Some(DefaultSize::Proportion("0.5")), |
| 477 | Some(DefaultSize::Fixed("500")), |
| 478 | ]; |
| 479 | let border = [false, true]; |
| 480 | let tabbed = [false, true]; |
| 481 | |
| 482 | let mut powerset = Vec::new(); |
| 483 | for fs in open_fullscreen { |
| 484 | for wfs in want_fullscreen { |
| 485 | for om in open_maximized { |
| 486 | for of in open_floating { |
| 487 | for dw in default_column_width { |
| 488 | for dh in default_window_height { |
| 489 | for b in border { |
| 490 | for t in tabbed { |
| 491 | powerset.push((fs, wfs, om, of, dw, dh, b, t)); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | powerset |
| 502 | .into_par_iter() |
| 503 | .for_each(|(fs, wfs, om, of, dw, dh, b, t)| { |
| 504 | check_target_size(fs, wfs, om, of, dw, dh, b, t); |
| 505 | }); |
| 506 | } |
| 507 | |
| 508 | #[allow(clippy::too_many_arguments)] |
| 509 | fn check_target_size( |
| 510 | open_fullscreen: Option<&str>, |
| 511 | want_fullscreen: WantFullscreen, |
| 512 | open_maximized: Option<&str>, |
| 513 | open_floating: Option<&str>, |
| 514 | default_width: Option<DefaultSize>, |
| 515 | default_height: Option<DefaultSize>, |
| 516 | border: bool, |
| 517 | tabbed: bool, |
| 518 | ) { |
| 519 | let mut snapshot_desc = Vec::new(); |
| 520 | let mut snapshot_suffix = Vec::new(); |
| 521 | |
| 522 | let mut config = String::from( |
| 523 | r##" |
| 524 | window-rule { |
| 525 | "##, |
| 526 | ); |
| 527 | |
| 528 | if let Some(x) = open_fullscreen { |
| 529 | writeln!(config, " open-fullscreen {x}").unwrap(); |
| 530 | |
| 531 | let x = if x == "true" { "T" } else { "F" }; |
| 532 | snapshot_suffix.push(format!("fs{x}")); |
| 533 | } |
| 534 | |
| 535 | if let Some(x) = open_maximized { |
| 536 | writeln!(config, " open-maximized {x}").unwrap(); |
| 537 | |
| 538 | let x = if x == "true" { "T" } else { "F" }; |
| 539 | snapshot_suffix.push(format!("om{x}")); |
| 540 | } |
| 541 | |
| 542 | if let Some(x) = open_floating { |
| 543 | writeln!(config, " open-floating {x}").unwrap(); |
| 544 | |
| 545 | let x = if x == "true" { "T" } else { "F" }; |
| 546 | snapshot_suffix.push(format!("of{x}")); |
| 547 | } |
| 548 | |
| 549 | if let Some(x) = default_width { |
| 550 | let value = match x { |
| 551 | DefaultSize::WindowChooses => String::new(), |
| 552 | DefaultSize::Proportion(prop) => format!("proportion {prop};"), |
| 553 | DefaultSize::Fixed(fixed) => format!("fixed {fixed};"), |
| 554 | }; |
| 555 | writeln!(config, " default-column-width {{ {value} }}").unwrap(); |
| 556 | |
| 557 | snapshot_suffix.push(format!("dw{x}")); |
| 558 | } |
| 559 | |
| 560 | if let Some(x) = default_height { |
| 561 | let value = match x { |
| 562 | DefaultSize::WindowChooses => String::new(), |
| 563 | DefaultSize::Proportion(prop) => format!("proportion {prop};"), |
| 564 | DefaultSize::Fixed(fixed) => format!("fixed {fixed};"), |
| 565 | }; |
| 566 | writeln!(config, " default-window-height {{ {value} }}").unwrap(); |
| 567 | |
| 568 | snapshot_suffix.push(format!("dh{x}")); |
| 569 | } |
| 570 | |
| 571 | if border { |
| 572 | writeln!(config, " border {{ on; }}").unwrap(); |
| 573 | snapshot_suffix.push(String::from("b")); |
| 574 | } |
| 575 | |
| 576 | if tabbed { |
| 577 | writeln!(config, " default-column-display \"tabbed\"").unwrap(); |
| 578 | } |
| 579 | |
| 580 | config.push('}'); |
| 581 | |
| 582 | match &want_fullscreen { |
| 583 | WantFullscreen::No => (), |
| 584 | x => { |
| 585 | snapshot_desc.push(format!("want fullscreen: {x}")); |
| 586 | snapshot_suffix.push(format!("wfs{x}")); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | if tabbed { |
| 591 | config.push_str( |
| 592 | "\n |
| 593 | layout { |
| 594 | tab-indicator { |
| 595 | place-within-column |
| 596 | } |
| 597 | }", |
| 598 | ); |
| 599 | snapshot_suffix.push(String::from("t")); |
| 600 | } |
| 601 | |
| 602 | snapshot_desc.push(format!("config:{config}")); |
| 603 | |
| 604 | let config = Config::parse_mem(&config).unwrap(); |
| 605 | |
| 606 | let mut f = Fixture::with_config(config); |
| 607 | f.add_output(1, (1280, 720)); |
| 608 | f.add_output(2, (1920, 1080)); |
| 609 | |
| 610 | let id = f.add_client(); |
| 611 | |
| 612 | // To get output names. |
| 613 | f.roundtrip(id); |
| 614 | |
| 615 | let client = f.client(id); |
| 616 | let window = client.create_window(); |
| 617 | let surface = window.surface.clone(); |
| 618 | |
| 619 | if let WantFullscreen::UnsetBeforeInitial = want_fullscreen { |
| 620 | client.window(&surface).unset_fullscreen(); |
| 621 | } else if let WantFullscreen::BeforeInitial(mon) = want_fullscreen { |
| 622 | let output = mon.map(|mon| client.output(&format!("headless-{mon}"))); |
| 623 | client.window(&surface).set_fullscreen(output.as_ref()); |
| 624 | } |
| 625 | |
| 626 | client.window(&surface).commit(); |
| 627 | f.roundtrip(id); |
| 628 | |
| 629 | let client = f.client(id); |
| 630 | let initial = client.window(&surface).format_recent_configures(); |
| 631 | |
| 632 | if let WantFullscreen::UnsetAfterInitial = want_fullscreen { |
| 633 | client.window(&surface).unset_fullscreen(); |
| 634 | } else if let WantFullscreen::AfterInitial(mon) = want_fullscreen { |
| 635 | let output = mon.map(|mon| client.output(&format!("headless-{mon}"))); |
| 636 | client.window(&surface).set_fullscreen(output.as_ref()); |
| 637 | } |
| 638 | |
| 639 | let window = client.window(&surface); |
| 640 | window.attach_new_buffer(); |
| 641 | let serial = window.configures_received.last().unwrap().0; |
| 642 | window.ack_last_and_commit(); |
| 643 | f.double_roundtrip(id); |
| 644 | |
| 645 | // Commit to the post-initial configures. |
| 646 | let window = f.client(id).window(&surface); |
| 647 | let new_serial = window.configures_received.last().unwrap().0; |
| 648 | if new_serial != serial { |
| 649 | window.ack_last_and_commit(); |
| 650 | f.double_roundtrip(id); |
| 651 | } |
| 652 | |
| 653 | let window = f.client(id).window(&surface); |
| 654 | let post_map = window.format_recent_configures(); |
| 655 | |
| 656 | // If the window ended up fullscreen, unfullscreen it and output the configure. |
| 657 | let mut post_unfullscreen = String::new(); |
| 658 | let mapped = f.niri().layout.windows().next().unwrap().1; |
| 659 | let is_fullscreen = mapped.sizing_mode().is_fullscreen(); |
| 660 | let win = mapped.window.clone(); |
| 661 | if is_fullscreen { |
| 662 | f.niri().layout.set_fullscreen(&win, false); |
| 663 | f.double_roundtrip(id); |
| 664 | |
| 665 | let window = f.client(id).window(&surface); |
| 666 | post_unfullscreen = format!( |
| 667 | "\n\nunfullscreen configure:\n{}", |
| 668 | window.format_recent_configures() |
| 669 | ); |
| 670 | } |
| 671 | |
| 672 | let snapshot = format!( |
| 673 | "\ |
| 674 | initial configure: |
| 675 | {initial} |
| 676 | |
| 677 | post-map configures: |
| 678 | {post_map}{post_unfullscreen}", |
| 679 | ); |
| 680 | |
| 681 | let mut settings = insta::Settings::clone_current(); |
| 682 | settings.set_snapshot_suffix(snapshot_suffix.join("-")); |
| 683 | settings.set_description(snapshot_desc.join("\n")); |
| 684 | let _guard = settings.bind_to_scope(); |
| 685 | assert_snapshot!(snapshot); |
| 686 | } |
| 687 | |
| 688 | #[test] |
| 689 | fn fullscreen_maximize() { |
| 690 | store_and_increase_nofile_rlimit(); |
| 691 | |
| 692 | let open_fullscreen = [None, Some("false"), Some("true")]; |
| 693 | let want_fullscreen = [ |
| 694 | WantFullscreen::No, |
| 695 | WantFullscreen::UnsetBeforeInitial, // GTK 4 |
| 696 | WantFullscreen::BeforeInitial(None), |
| 697 | WantFullscreen::UnsetAfterInitial, |
| 698 | // mpv, osu! |
| 699 | WantFullscreen::AfterInitial(None), |
| 700 | ]; |
| 701 | let open_maximized = [None, Some("false"), Some("true")]; |
| 702 | let want_maximized = [ |
| 703 | WantMaximized::No, |
| 704 | WantMaximized::UnsetBeforeInitial, |
| 705 | WantMaximized::BeforeInitial, |
| 706 | WantMaximized::UnsetAfterInitial, |
| 707 | WantMaximized::AfterInitial, |
| 708 | ]; |
| 709 | |
| 710 | let mut powerset = Vec::new(); |
| 711 | for fs in open_fullscreen { |
| 712 | for wfs in want_fullscreen { |
| 713 | for tm in open_maximized { |
| 714 | for wm in want_maximized { |
| 715 | powerset.push((fs, wfs, tm, wm)); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | powerset.into_par_iter().for_each(|(fs, wfs, tm, wm)| { |
| 722 | check_fullscreen_maximize(fs, wfs, tm, wm); |
| 723 | }); |
| 724 | } |
| 725 | |
| 726 | fn check_fullscreen_maximize( |
| 727 | open_fullscreen: Option<&str>, |
| 728 | want_fullscreen: WantFullscreen, |
| 729 | open_maximized: Option<&str>, |
| 730 | want_maximized: WantMaximized, |
| 731 | ) { |
| 732 | let mut snapshot_desc = Vec::new(); |
| 733 | let mut snapshot_suffix = Vec::new(); |
| 734 | |
| 735 | let mut config = String::from( |
| 736 | r##" |
| 737 | window-rule { |
| 738 | "##, |
| 739 | ); |
| 740 | |
| 741 | if let Some(x) = open_fullscreen { |
| 742 | writeln!(config, " open-fullscreen {x}").unwrap(); |
| 743 | |
| 744 | let x = if x == "true" { "T" } else { "F" }; |
| 745 | snapshot_suffix.push(format!("fs{x}")); |
| 746 | } |
| 747 | |
| 748 | if let Some(x) = open_maximized { |
| 749 | writeln!(config, " open-maximized-to-edges {x}").unwrap(); |
| 750 | |
| 751 | let x = if x == "true" { "T" } else { "F" }; |
| 752 | snapshot_suffix.push(format!("tm{x}")); |
| 753 | } |
| 754 | |
| 755 | config.push('}'); |
| 756 | |
| 757 | match &want_fullscreen { |
| 758 | WantFullscreen::No => (), |
| 759 | x => { |
| 760 | snapshot_desc.push(format!("want fullscreen: {x}")); |
| 761 | snapshot_suffix.push(format!("wfs{x}")); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | match &want_maximized { |
| 766 | WantMaximized::No => (), |
| 767 | x => { |
| 768 | snapshot_desc.push(format!("want maximized: {x}")); |
| 769 | snapshot_suffix.push(format!("wm{x}")); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | snapshot_desc.push(format!("config:{config}")); |
| 774 | |
| 775 | let config = Config::parse_mem(&config).unwrap(); |
| 776 | |
| 777 | let mut f = Fixture::with_config(config); |
| 778 | f.add_output(1, (1280, 720)); |
| 779 | f.add_output(2, (1920, 1080)); |
| 780 | |
| 781 | let id = f.add_client(); |
| 782 | |
| 783 | // To get output names. |
| 784 | f.roundtrip(id); |
| 785 | |
| 786 | let client = f.client(id); |
| 787 | let window = client.create_window(); |
| 788 | let surface = window.surface.clone(); |
| 789 | |
| 790 | if let WantMaximized::UnsetBeforeInitial = want_maximized { |
| 791 | client.window(&surface).unset_maximized(); |
| 792 | } else if let WantMaximized::BeforeInitial = want_maximized { |
| 793 | client.window(&surface).set_maximized(); |
| 794 | } |
| 795 | |
| 796 | if let WantFullscreen::UnsetBeforeInitial = want_fullscreen { |
| 797 | client.window(&surface).unset_fullscreen(); |
| 798 | } else if let WantFullscreen::BeforeInitial(mon) = want_fullscreen { |
| 799 | let output = mon.map(|mon| client.output(&format!("headless-{mon}"))); |
| 800 | client.window(&surface).set_fullscreen(output.as_ref()); |
| 801 | } |
| 802 | |
| 803 | client.window(&surface).commit(); |
| 804 | f.roundtrip(id); |
| 805 | |
| 806 | let client = f.client(id); |
| 807 | let initial = client.window(&surface).format_recent_configures(); |
| 808 | |
| 809 | if let WantMaximized::UnsetAfterInitial = want_maximized { |
| 810 | client.window(&surface).unset_maximized(); |
| 811 | } else if let WantMaximized::AfterInitial = want_maximized { |
| 812 | client.window(&surface).set_maximized(); |
| 813 | } |
| 814 | |
| 815 | if let WantFullscreen::UnsetAfterInitial = want_fullscreen { |
| 816 | client.window(&surface).unset_fullscreen(); |
| 817 | } else if let WantFullscreen::AfterInitial(mon) = want_fullscreen { |
| 818 | let output = mon.map(|mon| client.output(&format!("headless-{mon}"))); |
| 819 | client.window(&surface).set_fullscreen(output.as_ref()); |
| 820 | } |
| 821 | |
| 822 | let window = client.window(&surface); |
| 823 | window.attach_new_buffer(); |
| 824 | let serial = window.configures_received.last().unwrap().0; |
| 825 | window.ack_last_and_commit(); |
| 826 | f.double_roundtrip(id); |
| 827 | |
| 828 | // Commit to the post-initial configures. |
| 829 | let window = f.client(id).window(&surface); |
| 830 | let new_serial = window.configures_received.last().unwrap().0; |
| 831 | if new_serial != serial { |
| 832 | window.ack_last_and_commit(); |
| 833 | f.double_roundtrip(id); |
| 834 | } |
| 835 | |
| 836 | let window = f.client(id).window(&surface); |
| 837 | let post_map = window.format_recent_configures(); |
| 838 | |
| 839 | // If the window ended up fullscreen, unfullscreen it and output the configure. |
| 840 | let mut post_unfullscreen = String::new(); |
| 841 | let mapped = f.niri().layout.windows().next().unwrap().1; |
| 842 | let is_fullscreen = mapped.sizing_mode().is_fullscreen(); |
| 843 | let win = mapped.window.clone(); |
| 844 | if is_fullscreen { |
| 845 | f.niri().layout.set_fullscreen(&win, false); |
| 846 | f.double_roundtrip(id); |
| 847 | |
| 848 | let window = f.client(id).window(&surface); |
| 849 | window.ack_last_and_commit(); |
| 850 | f.double_roundtrip(id); |
| 851 | |
| 852 | let window = f.client(id).window(&surface); |
| 853 | post_unfullscreen = format!( |
| 854 | "\n\nunfullscreen configure:\n{}", |
| 855 | window.format_recent_configures() |
| 856 | ); |
| 857 | } |
| 858 | |
| 859 | // If the window ended up maximized, unmaximize it and output the configure. |
| 860 | let mut post_unmaximize = String::new(); |
| 861 | let mapped = f.niri().layout.windows().next().unwrap().1; |
| 862 | let is_maximized = mapped.sizing_mode().is_maximized(); |
| 863 | let win = mapped.window.clone(); |
| 864 | if is_maximized { |
| 865 | f.niri().layout.set_maximized(&win, false); |
| 866 | f.double_roundtrip(id); |
| 867 | |
| 868 | let window = f.client(id).window(&surface); |
| 869 | window.ack_last_and_commit(); |
| 870 | f.double_roundtrip(id); |
| 871 | |
| 872 | let window = f.client(id).window(&surface); |
| 873 | post_unmaximize = format!( |
| 874 | "\n\nunmaximize configure:\n{}", |
| 875 | window.format_recent_configures() |
| 876 | ); |
| 877 | } |
| 878 | |
| 879 | let snapshot = format!( |
| 880 | "\ |
| 881 | initial configure: |
| 882 | {initial} |
| 883 | |
| 884 | post-map configures: |
| 885 | {post_map}{post_unfullscreen}{post_unmaximize}", |
| 886 | ); |
| 887 | |
| 888 | let mut settings = insta::Settings::clone_current(); |
| 889 | settings.set_snapshot_suffix(snapshot_suffix.join("-")); |
| 890 | settings.set_description(snapshot_desc.join("\n")); |
| 891 | let _guard = settings.bind_to_scope(); |
| 892 | assert_snapshot!(snapshot); |
| 893 | } |