forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB53500dcTahinli1 month ago2998 commits

tests.rs4051 lines, 120.4 KiBHistoryRawDownload
1use std::cell::{Cell, OnceCell, RefCell};
2
3use niri_config::utils::Flag;
4use niri_config::workspace::WorkspaceName;
5use niri_config::{
6 CenterFocusedColumn, FloatOrInt, OutputName, Struts, TabIndicatorLength, TabIndicatorPosition,
7 WorkspaceReference,
8};
9use proptest::prelude::*;
10use proptest_derive::Arbitrary;
11use smithay::output::{Mode, PhysicalProperties, Subpixel};
12use smithay::utils::Rectangle;
13
14use super::*;
15
16mod animations;
17mod fullscreen;
18
19impl<W: LayoutElement> Default for Layout<W> {
20 fn default() -> Self {
21 Self::with_options(Clock::with_time(Duration::ZERO), Default::default())
22 }
23}
24
25#[derive(Debug)]
26struct TestWindowInner {
27 id: usize,
28 parent_id: Cell<Option<usize>>,
29 bbox: Cell<Rectangle<i32, Logical>>,
30 initial_bbox: Rectangle<i32, Logical>,
31 requested_size: Cell<Option<Size<i32, Logical>>>,
32 // Emulates the window ignoring the compositor-provided size.
33 forced_size: Cell<Option<Size<i32, Logical>>>,
34 min_size: Size<i32, Logical>,
35 max_size: Size<i32, Logical>,
36 pending_sizing_mode: Cell<SizingMode>,
37 pending_activated: Cell<bool>,
38 sizing_mode: Cell<SizingMode>,
39 is_windowed_fullscreen: Cell<bool>,
40 is_pending_windowed_fullscreen: Cell<bool>,
41 animate_next_configure: Cell<bool>,
42 animation_snapshot: RefCell<Option<LayoutElementRenderSnapshot>>,
43 rules: ResolvedWindowRules,
44}
45
46#[derive(Debug, Clone)]
47struct TestWindow(Rc<TestWindowInner>);
48
49#[derive(Debug, Clone, Arbitrary)]
50struct TestWindowParams {
51 #[proptest(strategy = "1..=5usize")]
52 id: usize,
53 #[proptest(strategy = "arbitrary_parent_id()")]
54 parent_id: Option<usize>,
55 is_floating: bool,
56 #[proptest(strategy = "arbitrary_bbox()")]
57 bbox: Rectangle<i32, Logical>,
58 #[proptest(strategy = "arbitrary_min_max_size()")]
59 min_max_size: (Size<i32, Logical>, Size<i32, Logical>),
60 #[proptest(strategy = "prop::option::of(arbitrary_rules())")]
61 rules: Option<ResolvedWindowRules>,
62}
63
64impl TestWindowParams {
65 pub fn new(id: usize) -> Self {
66 Self {
67 id,
68 parent_id: None,
69 is_floating: false,
70 bbox: Rectangle::from_size(Size::from((100, 200))),
71 min_max_size: Default::default(),
72 rules: None,
73 }
74 }
75}
76
77impl TestWindow {
78 fn new(params: TestWindowParams) -> Self {
79 Self(Rc::new(TestWindowInner {
80 id: params.id,
81 parent_id: Cell::new(params.parent_id),
82 bbox: Cell::new(params.bbox),
83 initial_bbox: params.bbox,
84 requested_size: Cell::new(None),
85 forced_size: Cell::new(None),
86 min_size: params.min_max_size.0,
87 max_size: params.min_max_size.1,
88 pending_sizing_mode: Cell::new(SizingMode::Normal),
89 pending_activated: Cell::new(false),
90 sizing_mode: Cell::new(SizingMode::Normal),
91 is_windowed_fullscreen: Cell::new(false),
92 is_pending_windowed_fullscreen: Cell::new(false),
93 animate_next_configure: Cell::new(false),
94 animation_snapshot: RefCell::new(None),
95 rules: params.rules.unwrap_or_default(),
96 }))
97 }
98
99 fn communicate(&self) -> bool {
100 let mut changed = false;
101
102 let size = self.0.forced_size.get().or(self.0.requested_size.get());
103 if let Some(size) = size {
104 assert!(size.w >= 0);
105 assert!(size.h >= 0);
106
107 let mut new_bbox = self.0.initial_bbox;
108 if size.w != 0 {
109 new_bbox.size.w = size.w;
110 }
111 if size.h != 0 {
112 new_bbox.size.h = size.h;
113 }
114
115 if self.0.bbox.get() != new_bbox {
116 if self.0.animate_next_configure.get() {
117 self.0.animation_snapshot.replace(Some(RenderSnapshot {
118 contents: Vec::new(),
119 contents_with_blocked_out_bg: None,
120 blocked_out_contents: Vec::new(),
121 block_out_from: None,
122 size: self.0.bbox.get().size.to_f64(),
123 texture: OnceCell::new(),
124 texture_with_blocked_out_bg: Default::default(),
125 blocked_out_texture: OnceCell::new(),
126 }));
127 }
128
129 self.0.bbox.set(new_bbox);
130 changed = true;
131 }
132 }
133
134 self.0.animate_next_configure.set(false);
135
136 if self.0.sizing_mode.get() != self.0.pending_sizing_mode.get() {
137 self.0.sizing_mode.set(self.0.pending_sizing_mode.get());
138 changed = true;
139 }
140
141 if self.0.is_windowed_fullscreen.get() != self.0.is_pending_windowed_fullscreen.get() {
142 self.0
143 .is_windowed_fullscreen
144 .set(self.0.is_pending_windowed_fullscreen.get());
145 changed = true;
146 }
147
148 changed
149 }
150}
151
152impl LayoutElement for TestWindow {
153 type Id = usize;
154
155 fn id(&self) -> &Self::Id {
156 &self.0.id
157 }
158
159 fn size(&self) -> Size<i32, Logical> {
160 self.0.bbox.get().size
161 }
162
163 fn buf_loc(&self) -> Point<i32, Logical> {
164 (0, 0).into()
165 }
166
167 fn is_in_input_region(&self, _point: Point<f64, Logical>) -> bool {
168 false
169 }
170
171 fn request_size(
172 &mut self,
173 size: Size<i32, Logical>,
174 mode: SizingMode,
175 _animate: bool,
176 _transaction: Option<Transaction>,
177 ) {
178 if self.0.requested_size.get() != Some(size) {
179 self.0.requested_size.set(Some(size));
180 self.0.animate_next_configure.set(true);
181 }
182
183 self.0.pending_sizing_mode.set(mode);
184
185 if mode.is_fullscreen() {
186 self.0.is_pending_windowed_fullscreen.set(false);
187 }
188 }
189
190 fn min_size(&self) -> Size<i32, Logical> {
191 self.0.min_size
192 }
193
194 fn max_size(&self) -> Size<i32, Logical> {
195 self.0.max_size
196 }
197
198 fn is_wl_surface(&self, _wl_surface: &WlSurface) -> bool {
199 false
200 }
201
202 fn set_preferred_scale_transform(&self, _scale: output::Scale, _transform: Transform) {}
203
204 fn has_ssd(&self) -> bool {
205 false
206 }
207
208 fn output_enter(&self, _output: &Output) {}
209
210 fn output_leave(&self, _output: &Output) {}
211
212 fn set_offscreen_data(&self, _data: Option<OffscreenData>) {}
213
214 fn set_activated(&mut self, active: bool) {
215 self.0.pending_activated.set(active);
216 }
217
218 fn set_bounds(&self, _bounds: Size<i32, Logical>) {}
219
220 fn is_ignoring_opacity_window_rule(&self) -> bool {
221 false
222 }
223
224 fn configure_intent(&self) -> ConfigureIntent {
225 ConfigureIntent::CanSend
226 }
227
228 fn send_pending_configure(&mut self) {}
229
230 fn set_active_in_column(&mut self, _active: bool) {}
231
232 fn set_floating(&mut self, _floating: bool) {}
233
234 fn sizing_mode(&self) -> SizingMode {
235 self.0.sizing_mode.get()
236 }
237
238 fn pending_sizing_mode(&self) -> SizingMode {
239 self.0.pending_sizing_mode.get()
240 }
241
242 fn requested_size(&self) -> Option<Size<i32, Logical>> {
243 self.0.requested_size.get()
244 }
245
246 fn is_windowed_fullscreen(&self) -> bool {
247 self.0.is_windowed_fullscreen.get()
248 }
249
250 fn is_pending_windowed_fullscreen(&self) -> bool {
251 self.0.is_pending_windowed_fullscreen.get()
252 }
253
254 fn request_windowed_fullscreen(&mut self, value: bool) {
255 self.0.is_pending_windowed_fullscreen.set(value);
256 }
257
258 fn is_child_of(&self, parent: &Self) -> bool {
259 self.0.parent_id.get() == Some(parent.0.id)
260 }
261
262 fn refresh(&self) {}
263
264 fn rules(&self) -> &ResolvedWindowRules {
265 &self.0.rules
266 }
267
268 fn take_animation_snapshot(&mut self) -> Option<LayoutElementRenderSnapshot> {
269 self.0.animation_snapshot.take()
270 }
271
272 fn set_interactive_resize(&mut self, _data: Option<InteractiveResizeData>) {}
273
274 fn cancel_interactive_resize(&mut self) {}
275
276 fn on_commit(&mut self, _serial: Serial) {}
277
278 fn interactive_resize_data(&self) -> Option<InteractiveResizeData> {
279 None
280 }
281
282 fn is_urgent(&self) -> bool {
283 false
284 }
285}
286
287fn arbitrary_size() -> impl Strategy<Value = Size<i32, Logical>> {
288 any::<(u16, u16)>().prop_map(|(w, h)| Size::from((w.max(1).into(), h.max(1).into())))
289}
290
291fn arbitrary_bbox() -> impl Strategy<Value = Rectangle<i32, Logical>> {
292 any::<(i16, i16, u16, u16)>().prop_map(|(x, y, w, h)| {
293 let loc: Point<i32, _> = Point::from((x.into(), y.into()));
294 let size: Size<i32, _> = Size::from((w.max(1).into(), h.max(1).into()));
295 Rectangle::new(loc, size)
296 })
297}
298
299fn arbitrary_size_change() -> impl Strategy<Value = SizeChange> {
300 prop_oneof![
301 (0..).prop_map(SizeChange::SetFixed),
302 (0f64..).prop_map(SizeChange::SetProportion),
303 any::<i32>().prop_map(SizeChange::AdjustFixed),
304 any::<f64>().prop_map(SizeChange::AdjustProportion),
305 // Interactive resize can have negative values here.
306 Just(SizeChange::SetFixed(-100)),
307 ]
308}
309
310fn arbitrary_position_change() -> impl Strategy<Value = PositionChange> {
311 prop_oneof![
312 (-1000f64..1000f64).prop_map(PositionChange::SetFixed),
313 any::<f64>().prop_map(PositionChange::SetProportion),
314 (-1000f64..1000f64).prop_map(PositionChange::AdjustFixed),
315 any::<f64>().prop_map(PositionChange::AdjustProportion),
316 any::<f64>().prop_map(PositionChange::SetFixed),
317 any::<f64>().prop_map(PositionChange::AdjustFixed),
318 ]
319}
320
321fn arbitrary_min_max() -> impl Strategy<Value = (i32, i32)> {
322 prop_oneof![
323 Just((0, 0)),
324 (1..65536).prop_map(|n| (n, n)),
325 (1..65536).prop_map(|min| (min, 0)),
326 (1..).prop_map(|max| (0, max)),
327 (1..65536, 1..).prop_map(|(min, max): (i32, i32)| (min, max.max(min))),
328 ]
329}
330
331fn arbitrary_min_max_size() -> impl Strategy<Value = (Size<i32, Logical>, Size<i32, Logical>)> {
332 prop_oneof![
333 5 => (arbitrary_min_max(), arbitrary_min_max()).prop_map(
334 |((min_w, max_w), (min_h, max_h))| {
335 let min_size = Size::from((min_w, min_h));
336 let max_size = Size::from((max_w, max_h));
337 (min_size, max_size)
338 },
339 ),
340 1 => arbitrary_min_max().prop_map(|(w, h)| {
341 let size = Size::from((w, h));
342 (size, size)
343 }),
344 ]
345}
346
347prop_compose! {
348 fn arbitrary_rules()(
349 focus_ring in arbitrary_focus_ring(),
350 border in arbitrary_border(),
351 ) -> ResolvedWindowRules {
352 ResolvedWindowRules {
353 focus_ring,
354 border,
355 ..ResolvedWindowRules::default()
356 }
357 }
358}
359
360fn arbitrary_view_offset_gesture_delta() -> impl Strategy<Value = f64> {
361 prop_oneof![(-10f64..10f64), (-50000f64..50000f64),]
362}
363
364fn arbitrary_resize_edge() -> impl Strategy<Value = ResizeEdge> {
365 prop_oneof![
366 Just(ResizeEdge::RIGHT),
367 Just(ResizeEdge::BOTTOM),
368 Just(ResizeEdge::LEFT),
369 Just(ResizeEdge::TOP),
370 Just(ResizeEdge::BOTTOM_RIGHT),
371 Just(ResizeEdge::BOTTOM_LEFT),
372 Just(ResizeEdge::TOP_RIGHT),
373 Just(ResizeEdge::TOP_LEFT),
374 Just(ResizeEdge::empty()),
375 ]
376}
377
378fn arbitrary_scale() -> impl Strategy<Value = f64> {
379 prop_oneof![Just(1.), Just(1.5), Just(2.),]
380}
381
382fn arbitrary_msec_delta() -> impl Strategy<Value = i32> {
383 prop_oneof![
384 1 => Just(-1000),
385 2 => Just(-10),
386 1 => Just(0),
387 2 => Just(10),
388 6 => Just(1000),
389 ]
390}
391
392fn arbitrary_parent_id() -> impl Strategy<Value = Option<usize>> {
393 prop_oneof![
394 5 => Just(None),
395 1 => prop::option::of(1..=5usize),
396 ]
397}
398
399fn arbitrary_scroll_direction() -> impl Strategy<Value = ScrollDirection> {
400 prop_oneof![Just(ScrollDirection::Left), Just(ScrollDirection::Right)]
401}
402
403fn arbitrary_column_display() -> impl Strategy<Value = ColumnDisplay> {
404 prop_oneof![Just(ColumnDisplay::Normal), Just(ColumnDisplay::Tabbed)]
405}
406
407#[derive(Debug, Clone, Arbitrary)]
408enum Op {
409 AddOutput(#[proptest(strategy = "1..=5usize")] usize),
410 AddScaledOutput {
411 #[proptest(strategy = "1..=5usize")]
412 id: usize,
413 #[proptest(strategy = "arbitrary_scale()")]
414 scale: f64,
415 #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
416 layout_config: Option<Box<niri_config::LayoutPart>>,
417 },
418 RemoveOutput(#[proptest(strategy = "1..=5usize")] usize),
419 FocusOutput(#[proptest(strategy = "1..=5usize")] usize),
420 UpdateOutputLayoutConfig {
421 #[proptest(strategy = "1..=5usize")]
422 id: usize,
423 #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
424 layout_config: Option<Box<niri_config::LayoutPart>>,
425 },
426 AddNamedWorkspace {
427 #[proptest(strategy = "1..=5usize")]
428 ws_name: usize,
429 #[proptest(strategy = "proptest::collection::vec(1..=5usize, 0..=3)")]
430 output_names: Vec<usize>,
431 #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
432 layout_config: Option<Box<niri_config::LayoutPart>>,
433 },
434 UnnameWorkspace {
435 #[proptest(strategy = "1..=5usize")]
436 ws_name: usize,
437 },
438 UpdateWorkspaceLayoutConfig {
439 #[proptest(strategy = "1..=5usize")]
440 ws_name: usize,
441 #[proptest(strategy = "prop::option::of(arbitrary_layout_part().prop_map(Box::new))")]
442 layout_config: Option<Box<niri_config::LayoutPart>>,
443 },
444 AddWindow {
445 params: TestWindowParams,
446 },
447 AddWindowNextTo {
448 params: TestWindowParams,
449 #[proptest(strategy = "1..=5usize")]
450 next_to_id: usize,
451 },
452 AddWindowToNamedWorkspace {
453 params: TestWindowParams,
454 #[proptest(strategy = "1..=5usize")]
455 ws_name: usize,
456 },
457 CloseWindow(#[proptest(strategy = "1..=5usize")] usize),
458 FullscreenWindow(#[proptest(strategy = "1..=5usize")] usize),
459 SetFullscreenWindow {
460 #[proptest(strategy = "1..=5usize")]
461 window: usize,
462 is_fullscreen: bool,
463 },
464 ToggleWindowedFullscreen(#[proptest(strategy = "1..=5usize")] usize),
465 FocusColumnLeft,
466 FocusColumnRight,
467 FocusColumnFirst,
468 FocusColumnLast,
469 FocusColumnRightOrFirst,
470 FocusColumnLeftOrLast,
471 FocusColumn(#[proptest(strategy = "1..=5usize")] usize),
472 FocusWindowOrMonitorUp(#[proptest(strategy = "1..=2u8")] u8),
473 FocusWindowOrMonitorDown(#[proptest(strategy = "1..=2u8")] u8),
474 FocusColumnOrMonitorLeft(#[proptest(strategy = "1..=2u8")] u8),
475 FocusColumnOrMonitorRight(#[proptest(strategy = "1..=2u8")] u8),
476 FocusWindowDown,
477 FocusWindowUp,
478 FocusWindowDownOrColumnLeft,
479 FocusWindowDownOrColumnRight,
480 FocusWindowUpOrColumnLeft,
481 FocusWindowUpOrColumnRight,
482 FocusWindowOrWorkspaceDown,
483 FocusWindowOrWorkspaceUp,
484 FocusWindow(#[proptest(strategy = "1..=5usize")] usize),
485 FocusWindowInColumn(#[proptest(strategy = "1..=5u8")] u8),
486 FocusWindowTop,
487 FocusWindowBottom,
488 FocusWindowDownOrTop,
489 FocusWindowUpOrBottom,
490 MoveColumnLeft,
491 MoveColumnRight,
492 MoveColumnToFirst,
493 MoveColumnToLast,
494 MoveColumnLeftOrToMonitorLeft(#[proptest(strategy = "1..=2u8")] u8),
495 MoveColumnRightOrToMonitorRight(#[proptest(strategy = "1..=2u8")] u8),
496 MoveColumnToIndex(#[proptest(strategy = "1..=5usize")] usize),
497 MoveWindowDown,
498 MoveWindowUp,
499 MoveWindowDownOrToWorkspaceDown,
500 MoveWindowUpOrToWorkspaceUp,
501 ConsumeOrExpelWindowLeft {
502 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
503 id: Option<usize>,
504 },
505 ConsumeOrExpelWindowRight {
506 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
507 id: Option<usize>,
508 },
509 ConsumeWindowIntoColumn,
510 ExpelWindowFromColumn,
511 SwapWindowInDirection(#[proptest(strategy = "arbitrary_scroll_direction()")] ScrollDirection),
512 ToggleColumnTabbedDisplay,
513 SetColumnDisplay(#[proptest(strategy = "arbitrary_column_display()")] ColumnDisplay),
514 CenterColumn,
515 CenterWindow {
516 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
517 id: Option<usize>,
518 },
519 CenterVisibleColumns,
520 FocusWorkspaceDown,
521 FocusWorkspaceUp,
522 FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize),
523 FocusWorkspaceAutoBackAndForth(#[proptest(strategy = "0..=4usize")] usize),
524 FocusWorkspacePrevious,
525 MoveWindowToWorkspaceDown(bool),
526 MoveWindowToWorkspaceUp(bool),
527 MoveWindowToWorkspace {
528 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
529 window_id: Option<usize>,
530 #[proptest(strategy = "0..=4usize")]
531 workspace_idx: usize,
532 },
533 MoveColumnToWorkspaceDown(bool),
534 MoveColumnToWorkspaceUp(bool),
535 MoveColumnToWorkspace(#[proptest(strategy = "0..=4usize")] usize, bool),
536 MoveWorkspaceDown,
537 MoveWorkspaceUp,
538 MoveWorkspaceToIndex {
539 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
540 ws_name: Option<usize>,
541 #[proptest(strategy = "0..=4usize")]
542 target_idx: usize,
543 },
544 MoveWorkspaceToMonitor {
545 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
546 ws_name: Option<usize>,
547 #[proptest(strategy = "0..=5usize")]
548 output_id: usize,
549 },
550 SetWorkspaceName {
551 #[proptest(strategy = "1..=5usize")]
552 new_ws_name: usize,
553 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
554 ws_name: Option<usize>,
555 },
556 UnsetWorkspaceName {
557 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
558 ws_name: Option<usize>,
559 },
560 MoveWindowToOutput {
561 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
562 window_id: Option<usize>,
563 #[proptest(strategy = "1..=5usize")]
564 output_id: usize,
565 #[proptest(strategy = "proptest::option::of(0..=4usize)")]
566 target_ws_idx: Option<usize>,
567 },
568 MoveColumnToOutput {
569 #[proptest(strategy = "1..=5usize")]
570 output_id: usize,
571 #[proptest(strategy = "proptest::option::of(0..=4usize)")]
572 target_ws_idx: Option<usize>,
573 activate: bool,
574 },
575 SwitchPresetColumnWidth,
576 SwitchPresetColumnWidthBack,
577 SwitchPresetWindowWidth {
578 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
579 id: Option<usize>,
580 },
581 SwitchPresetWindowWidthBack {
582 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
583 id: Option<usize>,
584 },
585 SwitchPresetWindowHeight {
586 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
587 id: Option<usize>,
588 },
589 SwitchPresetWindowHeightBack {
590 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
591 id: Option<usize>,
592 },
593 MaximizeColumn,
594 MaximizeWindowToEdges {
595 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
596 id: Option<usize>,
597 },
598 SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
599 SetWindowWidth {
600 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
601 id: Option<usize>,
602 #[proptest(strategy = "arbitrary_size_change()")]
603 change: SizeChange,
604 },
605 SetWindowHeight {
606 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
607 id: Option<usize>,
608 #[proptest(strategy = "arbitrary_size_change()")]
609 change: SizeChange,
610 },
611 ResetWindowHeight {
612 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
613 id: Option<usize>,
614 },
615 ExpandColumnToAvailableWidth,
616 ToggleWindowFloating {
617 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
618 id: Option<usize>,
619 },
620 SetWindowFloating {
621 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
622 id: Option<usize>,
623 floating: bool,
624 },
625 FocusFloating,
626 FocusTiling,
627 SwitchFocusFloatingTiling,
628 MoveFloatingWindow {
629 #[proptest(strategy = "proptest::option::of(1..=5usize)")]
630 id: Option<usize>,
631 #[proptest(strategy = "arbitrary_position_change()")]
632 x: PositionChange,
633 #[proptest(strategy = "arbitrary_position_change()")]
634 y: PositionChange,
635 animate: bool,
636 },
637 SetParent {
638 #[proptest(strategy = "1..=5usize")]
639 id: usize,
640 #[proptest(strategy = "prop::option::of(1..=5usize)")]
641 new_parent_id: Option<usize>,
642 },
643 SetForcedSize {
644 #[proptest(strategy = "1..=5usize")]
645 id: usize,
646 #[proptest(strategy = "proptest::option::of(arbitrary_size())")]
647 size: Option<Size<i32, Logical>>,
648 },
649 Communicate(#[proptest(strategy = "1..=5usize")] usize),
650 Refresh {
651 is_active: bool,
652 },
653 AdvanceAnimations {
654 #[proptest(strategy = "arbitrary_msec_delta()")]
655 msec_delta: i32,
656 },
657 CompleteAnimations,
658 MoveWorkspaceToOutput(#[proptest(strategy = "1..=5usize")] usize),
659 ViewOffsetGestureBegin {
660 #[proptest(strategy = "1..=5usize")]
661 output_idx: usize,
662 #[proptest(strategy = "proptest::option::of(0..=4usize)")]
663 workspace_idx: Option<usize>,
664 is_touchpad: bool,
665 },
666 ViewOffsetGestureUpdate {
667 #[proptest(strategy = "arbitrary_view_offset_gesture_delta()")]
668 delta: f64,
669 timestamp: Duration,
670 is_touchpad: bool,
671 },
672 ViewOffsetGestureEnd {
673 is_touchpad: Option<bool>,
674 },
675 WorkspaceSwitchGestureBegin {
676 #[proptest(strategy = "1..=5usize")]
677 output_idx: usize,
678 is_touchpad: bool,
679 },
680 WorkspaceSwitchGestureUpdate {
681 #[proptest(strategy = "-400f64..400f64")]
682 delta: f64,
683 timestamp: Duration,
684 is_touchpad: bool,
685 },
686 WorkspaceSwitchGestureEnd {
687 is_touchpad: Option<bool>,
688 },
689 OverviewGestureBegin,
690 OverviewGestureUpdate {
691 #[proptest(strategy = "-400f64..400f64")]
692 delta: f64,
693 timestamp: Duration,
694 },
695 OverviewGestureEnd,
696 InteractiveMoveBegin {
697 #[proptest(strategy = "1..=5usize")]
698 window: usize,
699 #[proptest(strategy = "1..=5usize")]
700 output_idx: usize,
701 #[proptest(strategy = "-20000f64..20000f64")]
702 px: f64,
703 #[proptest(strategy = "-20000f64..20000f64")]
704 py: f64,
705 },
706 InteractiveMoveUpdate {
707 #[proptest(strategy = "1..=5usize")]
708 window: usize,
709 #[proptest(strategy = "-20000f64..20000f64")]
710 dx: f64,
711 #[proptest(strategy = "-20000f64..20000f64")]
712 dy: f64,
713 #[proptest(strategy = "1..=5usize")]
714 output_idx: usize,
715 #[proptest(strategy = "-20000f64..20000f64")]
716 px: f64,
717 #[proptest(strategy = "-20000f64..20000f64")]
718 py: f64,
719 },
720 InteractiveMoveEnd {
721 #[proptest(strategy = "1..=5usize")]
722 window: usize,
723 },
724 DndUpdate {
725 #[proptest(strategy = "1..=5usize")]
726 output_idx: usize,
727 #[proptest(strategy = "-20000f64..20000f64")]
728 px: f64,
729 #[proptest(strategy = "-20000f64..20000f64")]
730 py: f64,
731 },
732 DndEnd,
733 InteractiveResizeBegin {
734 #[proptest(strategy = "1..=5usize")]
735 window: usize,
736 #[proptest(strategy = "arbitrary_resize_edge()")]
737 edges: ResizeEdge,
738 },
739 InteractiveResizeUpdate {
740 #[proptest(strategy = "1..=5usize")]
741 window: usize,
742 #[proptest(strategy = "-20000f64..20000f64")]
743 dx: f64,
744 #[proptest(strategy = "-20000f64..20000f64")]
745 dy: f64,
746 },
747 InteractiveResizeEnd {
748 #[proptest(strategy = "1..=5usize")]
749 window: usize,
750 },
751 ToggleOverview,
752 UpdateConfig {
753 #[proptest(strategy = "arbitrary_layout_part().prop_map(Box::new)")]
754 layout_config: Box<niri_config::LayoutPart>,
755 },
756}
757
758impl Op {
759 fn apply(self, layout: &mut Layout<TestWindow>) {
760 match self {
761 Op::AddOutput(id) => {
762 let name = format!("output{id}");
763 if layout.outputs().any(|o| o.name() == name) {
764 return;
765 }
766
767 let output = Output::new(
768 name.clone(),
769 PhysicalProperties {
770 size: Size::from((1280, 720)),
771 subpixel: Subpixel::Unknown,
772 make: String::new(),
773 model: String::new(),
774 serial_number: String::new(),
775 },
776 );
777 output.change_current_state(
778 Some(Mode {
779 size: Size::from((1280, 720)),
780 refresh: 60000,
781 }),
782 None,
783 None,
784 None,
785 );
786 output.user_data().insert_if_missing(|| OutputName {
787 connector: name,
788 make: None,
789 model: None,
790 serial: None,
791 });
792 layout.add_output(output.clone(), None);
793 }
794 Op::AddScaledOutput {
795 id,
796 scale,
797 layout_config,
798 } => {
799 let name = format!("output{id}");
800 if layout.outputs().any(|o| o.name() == name) {
801 return;
802 }
803
804 let output = Output::new(
805 name.clone(),
806 PhysicalProperties {
807 size: Size::from((1280, 720)),
808 subpixel: Subpixel::Unknown,
809 make: String::new(),
810 model: String::new(),
811 serial_number: String::new(),
812 },
813 );
814 output.change_current_state(
815 Some(Mode {
816 size: Size::from((1280, 720)),
817 refresh: 60000,
818 }),
819 None,
820 Some(smithay::output::Scale::Fractional(scale)),
821 None,
822 );
823 output.user_data().insert_if_missing(|| OutputName {
824 connector: name,
825 make: None,
826 model: None,
827 serial: None,
828 });
829 layout.add_output(output.clone(), layout_config.map(|x| *x));
830 }
831 Op::RemoveOutput(id) => {
832 let name = format!("output{id}");
833 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
834 return;
835 };
836
837 layout.remove_output(&output);
838 }
839 Op::FocusOutput(id) => {
840 let name = format!("output{id}");
841 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
842 return;
843 };
844
845 layout.focus_output(&output);
846 }
847 Op::UpdateOutputLayoutConfig { id, layout_config } => {
848 let name = format!("output{id}");
849 let Some(mon) = layout.monitors_mut().find(|m| m.output_name() == &name) else {
850 return;
851 };
852
853 mon.update_layout_config(layout_config.map(|x| *x));
854 }
855 Op::AddNamedWorkspace {
856 ws_name,
857 output_names,
858 layout_config,
859 } => {
860 layout.ensure_named_workspace(&WorkspaceConfig {
861 name: WorkspaceName(format!("ws{ws_name}")),
862 open_on_output: output_names
863 .into_iter()
864 .map(|name| format!("output{name}"))
865 .collect(),
866 hidden: false,
867 layout: layout_config.map(|x| niri_config::WorkspaceLayoutPart(*x)),
868 });
869 }
870 Op::UnnameWorkspace { ws_name } => {
871 layout.unname_workspace(&format!("ws{ws_name}"));
872 }
873 Op::UpdateWorkspaceLayoutConfig {
874 ws_name,
875 layout_config,
876 } => {
877 let ws_name = format!("ws{ws_name}");
878 let Some(ws) = layout
879 .workspaces_mut()
880 .find(|ws| ws.name() == Some(&ws_name))
881 else {
882 return;
883 };
884
885 ws.update_layout_config(layout_config.map(|x| *x));
886 }
887 Op::SetWorkspaceName {
888 new_ws_name,
889 ws_name,
890 } => {
891 let ws_ref =
892 ws_name.map(|ws_name| WorkspaceReference::Name(format!("ws{ws_name}")));
893 layout.set_workspace_name(format!("ws{new_ws_name}"), ws_ref);
894 }
895 Op::UnsetWorkspaceName { ws_name } => {
896 let ws_ref =
897 ws_name.map(|ws_name| WorkspaceReference::Name(format!("ws{ws_name}")));
898 layout.unset_workspace_name(ws_ref);
899 }
900 Op::AddWindow { mut params } => {
901 if layout.has_window(&params.id) {
902 return;
903 }
904 if let Some(parent_id) = params.parent_id {
905 if parent_id_causes_loop(layout, params.id, parent_id) {
906 params.parent_id = None;
907 }
908 }
909
910 let is_floating = params.is_floating;
911 let win = TestWindow::new(params);
912 layout.add_window(
913 win,
914 AddWindowTarget::Auto,
915 None,
916 None,
917 false,
918 is_floating,
919 ActivateWindow::default(),
920 );
921 }
922 Op::AddWindowNextTo {
923 mut params,
924 next_to_id,
925 } => {
926 let mut found_next_to = false;
927
928 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
929 let win_id = move_.tile.window().0.id;
930 if win_id == params.id {
931 return;
932 }
933 if win_id == next_to_id {
934 found_next_to = true;
935 }
936 }
937
938 match &mut layout.monitor_set {
939 MonitorSet::Normal { monitors, .. } => {
940 for mon in monitors {
941 for ws in &mut mon.workspaces {
942 for win in ws.windows() {
943 if win.0.id == params.id {
944 return;
945 }
946
947 if win.0.id == next_to_id {
948 found_next_to = true;
949 }
950 }
951 }
952 }
953 }
954 MonitorSet::NoOutputs { workspaces, .. } => {
955 for ws in workspaces {
956 for win in ws.windows() {
957 if win.0.id == params.id {
958 return;
959 }
960
961 if win.0.id == next_to_id {
962 found_next_to = true;
963 }
964 }
965 }
966 }
967 }
968
969 if !found_next_to {
970 return;
971 }
972
973 if let Some(parent_id) = params.parent_id {
974 if parent_id_causes_loop(layout, params.id, parent_id) {
975 params.parent_id = None;
976 }
977 }
978
979 let is_floating = params.is_floating;
980 let win = TestWindow::new(params);
981 layout.add_window(
982 win,
983 AddWindowTarget::NextTo(&next_to_id),
984 None,
985 None,
986 false,
987 is_floating,
988 ActivateWindow::default(),
989 );
990 }
991 Op::AddWindowToNamedWorkspace {
992 mut params,
993 ws_name,
994 } => {
995 let ws_name = format!("ws{ws_name}");
996 let mut ws_id = None;
997
998 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
999 if move_.tile.window().0.id == params.id {
1000 return;
1001 }
1002 }
1003
1004 match &mut layout.monitor_set {
1005 MonitorSet::Normal { monitors, .. } => {
1006 for mon in monitors {
1007 for ws in &mut mon.workspaces {
1008 for win in ws.windows() {
1009 if win.0.id == params.id {
1010 return;
1011 }
1012 }
1013
1014 if ws
1015 .name
1016 .as_ref()
1017 .is_some_and(|name| name.eq_ignore_ascii_case(&ws_name))
1018 {
1019 ws_id = Some(ws.id());
1020 }
1021 }
1022 }
1023 }
1024 MonitorSet::NoOutputs { workspaces, .. } => {
1025 for ws in workspaces {
1026 for win in ws.windows() {
1027 if win.0.id == params.id {
1028 return;
1029 }
1030 }
1031
1032 if ws
1033 .name
1034 .as_ref()
1035 .is_some_and(|name| name.eq_ignore_ascii_case(&ws_name))
1036 {
1037 ws_id = Some(ws.id());
1038 }
1039 }
1040 }
1041 }
1042
1043 let Some(ws_id) = ws_id else {
1044 return;
1045 };
1046
1047 if let Some(parent_id) = params.parent_id {
1048 if parent_id_causes_loop(layout, params.id, parent_id) {
1049 params.parent_id = None;
1050 }
1051 }
1052
1053 let is_floating = params.is_floating;
1054 let win = TestWindow::new(params);
1055 layout.add_window(
1056 win,
1057 AddWindowTarget::Workspace(ws_id),
1058 None,
1059 None,
1060 false,
1061 is_floating,
1062 ActivateWindow::default(),
1063 );
1064 }
1065 Op::CloseWindow(id) => {
1066 layout.remove_window(&id, Transaction::new());
1067 }
1068 Op::FullscreenWindow(id) => {
1069 if !layout.has_window(&id) {
1070 return;
1071 }
1072 layout.toggle_fullscreen(&id);
1073 }
1074 Op::SetFullscreenWindow {
1075 window,
1076 is_fullscreen,
1077 } => {
1078 if !layout.has_window(&window) {
1079 return;
1080 }
1081 layout.set_fullscreen(&window, is_fullscreen);
1082 }
1083 Op::ToggleWindowedFullscreen(id) => {
1084 if !layout.has_window(&id) {
1085 return;
1086 }
1087 layout.toggle_windowed_fullscreen(&id);
1088 }
1089 Op::FocusColumnLeft => layout.focus_left(),
1090 Op::FocusColumnRight => layout.focus_right(),
1091 Op::FocusColumnFirst => layout.focus_column_first(),
1092 Op::FocusColumnLast => layout.focus_column_last(),
1093 Op::FocusColumnRightOrFirst => layout.focus_column_right_or_first(),
1094 Op::FocusColumnLeftOrLast => layout.focus_column_left_or_last(),
1095 Op::FocusColumn(index) => layout.focus_column(index),
1096 Op::FocusWindowOrMonitorUp(id) => {
1097 let name = format!("output{id}");
1098 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1099 return;
1100 };
1101
1102 layout.focus_window_up_or_output(&output);
1103 }
1104 Op::FocusWindowOrMonitorDown(id) => {
1105 let name = format!("output{id}");
1106 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1107 return;
1108 };
1109
1110 layout.focus_window_down_or_output(&output);
1111 }
1112 Op::FocusColumnOrMonitorLeft(id) => {
1113 let name = format!("output{id}");
1114 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1115 return;
1116 };
1117
1118 layout.focus_column_left_or_output(&output);
1119 }
1120 Op::FocusColumnOrMonitorRight(id) => {
1121 let name = format!("output{id}");
1122 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1123 return;
1124 };
1125
1126 layout.focus_column_right_or_output(&output);
1127 }
1128 Op::FocusWindowDown => layout.focus_down(),
1129 Op::FocusWindowUp => layout.focus_up(),
1130 Op::FocusWindowDownOrColumnLeft => layout.focus_down_or_left(),
1131 Op::FocusWindowDownOrColumnRight => layout.focus_down_or_right(),
1132 Op::FocusWindowUpOrColumnLeft => layout.focus_up_or_left(),
1133 Op::FocusWindowUpOrColumnRight => layout.focus_up_or_right(),
1134 Op::FocusWindowOrWorkspaceDown => layout.focus_window_or_workspace_down(),
1135 Op::FocusWindowOrWorkspaceUp => layout.focus_window_or_workspace_up(),
1136 Op::FocusWindow(id) => layout.activate_window(&id),
1137 Op::FocusWindowInColumn(index) => layout.focus_window_in_column(index),
1138 Op::FocusWindowTop => layout.focus_window_top(),
1139 Op::FocusWindowBottom => layout.focus_window_bottom(),
1140 Op::FocusWindowDownOrTop => layout.focus_window_down_or_top(),
1141 Op::FocusWindowUpOrBottom => layout.focus_window_up_or_bottom(),
1142 Op::MoveColumnLeft => layout.move_left(),
1143 Op::MoveColumnRight => layout.move_right(),
1144 Op::MoveColumnToFirst => layout.move_column_to_first(),
1145 Op::MoveColumnToLast => layout.move_column_to_last(),
1146 Op::MoveColumnLeftOrToMonitorLeft(id) => {
1147 let name = format!("output{id}");
1148 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1149 return;
1150 };
1151
1152 layout.move_column_left_or_to_output(&output);
1153 }
1154 Op::MoveColumnRightOrToMonitorRight(id) => {
1155 let name = format!("output{id}");
1156 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1157 return;
1158 };
1159
1160 layout.move_column_right_or_to_output(&output);
1161 }
1162 Op::MoveColumnToIndex(index) => layout.move_column_to_index(index),
1163 Op::MoveWindowDown => layout.move_down(),
1164 Op::MoveWindowUp => layout.move_up(),
1165 Op::MoveWindowDownOrToWorkspaceDown => layout.move_down_or_to_workspace_down(),
1166 Op::MoveWindowUpOrToWorkspaceUp => layout.move_up_or_to_workspace_up(),
1167 Op::ConsumeOrExpelWindowLeft { id } => {
1168 let id = id.filter(|id| layout.has_window(id));
1169 layout.consume_or_expel_window_left(id.as_ref());
1170 }
1171 Op::ConsumeOrExpelWindowRight { id } => {
1172 let id = id.filter(|id| layout.has_window(id));
1173 layout.consume_or_expel_window_right(id.as_ref());
1174 }
1175 Op::ConsumeWindowIntoColumn => layout.consume_into_column(),
1176 Op::ExpelWindowFromColumn => layout.expel_from_column(),
1177 Op::SwapWindowInDirection(direction) => layout.swap_window_in_direction(direction),
1178 Op::ToggleColumnTabbedDisplay => layout.toggle_column_tabbed_display(),
1179 Op::SetColumnDisplay(display) => layout.set_column_display(display),
1180 Op::CenterColumn => layout.center_column(),
1181 Op::CenterWindow { id } => {
1182 let id = id.filter(|id| layout.has_window(id));
1183 layout.center_window(id.as_ref());
1184 }
1185 Op::CenterVisibleColumns => layout.center_visible_columns(),
1186 Op::FocusWorkspaceDown => layout.switch_workspace_down(),
1187 Op::FocusWorkspaceUp => layout.switch_workspace_up(),
1188 Op::FocusWorkspace(idx) => layout.switch_workspace(idx),
1189 Op::FocusWorkspaceAutoBackAndForth(idx) => {
1190 layout.switch_workspace_auto_back_and_forth(idx)
1191 }
1192 Op::FocusWorkspacePrevious => layout.switch_workspace_previous(),
1193 Op::MoveWindowToWorkspaceDown(focus) => layout.move_to_workspace_down(focus),
1194 Op::MoveWindowToWorkspaceUp(focus) => layout.move_to_workspace_up(focus),
1195 Op::MoveWindowToWorkspace {
1196 window_id,
1197 workspace_idx,
1198 } => {
1199 let window_id = window_id.filter(|id| layout.has_window(id));
1200 layout.move_to_workspace(window_id.as_ref(), workspace_idx, ActivateWindow::Smart);
1201 }
1202 Op::MoveColumnToWorkspaceDown(focus) => layout.move_column_to_workspace_down(focus),
1203 Op::MoveColumnToWorkspaceUp(focus) => layout.move_column_to_workspace_up(focus),
1204 Op::MoveColumnToWorkspace(idx, focus) => layout.move_column_to_workspace(idx, focus),
1205 Op::MoveWindowToOutput {
1206 window_id,
1207 output_id: id,
1208 target_ws_idx,
1209 } => {
1210 let name = format!("output{id}");
1211 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1212 return;
1213 };
1214 let mon = layout.monitor_for_output(&output).unwrap();
1215
1216 let window_id = window_id.filter(|id| layout.has_window(id));
1217 let target_ws_idx = target_ws_idx.filter(|idx| mon.workspaces.len() > *idx);
1218 layout.move_to_output(
1219 window_id.as_ref(),
1220 &output,
1221 target_ws_idx,
1222 ActivateWindow::Smart,
1223 );
1224 }
1225 Op::MoveColumnToOutput {
1226 output_id: id,
1227 target_ws_idx,
1228 activate,
1229 } => {
1230 let name = format!("output{id}");
1231 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1232 return;
1233 };
1234
1235 layout.move_column_to_output(&output, target_ws_idx, activate);
1236 }
1237 Op::MoveWorkspaceDown => layout.move_workspace_down(),
1238 Op::MoveWorkspaceUp => layout.move_workspace_up(),
1239 Op::MoveWorkspaceToIndex {
1240 ws_name: Some(ws_name),
1241 target_idx,
1242 } => {
1243 let MonitorSet::Normal { monitors, .. } = &mut layout.monitor_set else {
1244 return;
1245 };
1246
1247 let Some((old_idx, old_output)) = monitors.iter().find_map(|monitor| {
1248 monitor
1249 .workspaces
1250 .iter()
1251 .enumerate()
1252 .find_map(|(i, ws)| {
1253 if ws.name == Some(format!("ws{ws_name}")) {
1254 Some(i)
1255 } else {
1256 None
1257 }
1258 })
1259 .map(|i| (i, monitor.output.clone()))
1260 }) else {
1261 return;
1262 };
1263
1264 layout.move_workspace_to_idx(Some((Some(old_output), old_idx)), target_idx)
1265 }
1266 Op::MoveWorkspaceToIndex {
1267 ws_name: None,
1268 target_idx,
1269 } => layout.move_workspace_to_idx(None, target_idx),
1270 Op::MoveWorkspaceToMonitor {
1271 ws_name: None,
1272 output_id: id,
1273 } => {
1274 let name = format!("output{id}");
1275 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1276 return;
1277 };
1278 layout.move_workspace_to_output(&output);
1279 }
1280 Op::MoveWorkspaceToMonitor {
1281 ws_name: Some(ws_name),
1282 output_id: id,
1283 } => {
1284 let name = format!("output{id}");
1285 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1286 return;
1287 };
1288 let MonitorSet::Normal { monitors, .. } = &mut layout.monitor_set else {
1289 return;
1290 };
1291
1292 let Some((old_idx, old_output)) = monitors.iter().find_map(|monitor| {
1293 monitor
1294 .workspaces
1295 .iter()
1296 .enumerate()
1297 .find_map(|(i, ws)| {
1298 if ws.name == Some(format!("ws{ws_name}")) {
1299 Some(i)
1300 } else {
1301 None
1302 }
1303 })
1304 .map(|i| (i, monitor.output.clone()))
1305 }) else {
1306 return;
1307 };
1308
1309 layout.move_workspace_to_output_by_id(old_idx, Some(old_output), &output);
1310 }
1311 Op::SwitchPresetColumnWidth => layout.toggle_width(true),
1312 Op::SwitchPresetColumnWidthBack => layout.toggle_width(false),
1313 Op::SwitchPresetWindowWidth { id } => {
1314 let id = id.filter(|id| layout.has_window(id));
1315 layout.toggle_window_width(id.as_ref(), true);
1316 }
1317 Op::SwitchPresetWindowWidthBack { id } => {
1318 let id = id.filter(|id| layout.has_window(id));
1319 layout.toggle_window_width(id.as_ref(), false);
1320 }
1321 Op::SwitchPresetWindowHeight { id } => {
1322 let id = id.filter(|id| layout.has_window(id));
1323 layout.toggle_window_height(id.as_ref(), true);
1324 }
1325 Op::SwitchPresetWindowHeightBack { id } => {
1326 let id = id.filter(|id| layout.has_window(id));
1327 layout.toggle_window_height(id.as_ref(), false);
1328 }
1329 Op::MaximizeColumn => layout.toggle_full_width(),
1330 Op::MaximizeWindowToEdges { id } => {
1331 let id = id.or_else(|| layout.focus().map(|win| *win.id()));
1332 let Some(id) = id else {
1333 return;
1334 };
1335 if !layout.has_window(&id) {
1336 return;
1337 }
1338 layout.toggle_maximized(&id);
1339 }
1340 Op::SetColumnWidth(change) => layout.set_column_width(change),
1341 Op::SetWindowWidth { id, change } => {
1342 let id = id.filter(|id| layout.has_window(id));
1343 layout.set_window_width(id.as_ref(), change);
1344 }
1345 Op::SetWindowHeight { id, change } => {
1346 let id = id.filter(|id| layout.has_window(id));
1347 layout.set_window_height(id.as_ref(), change);
1348 }
1349 Op::ResetWindowHeight { id } => {
1350 let id = id.filter(|id| layout.has_window(id));
1351 layout.reset_window_height(id.as_ref());
1352 }
1353 Op::ExpandColumnToAvailableWidth => layout.expand_column_to_available_width(),
1354 Op::ToggleWindowFloating { id } => {
1355 let id = id.filter(|id| layout.has_window(id));
1356 layout.toggle_window_floating(id.as_ref());
1357 }
1358 Op::SetWindowFloating { id, floating } => {
1359 let id = id.filter(|id| layout.has_window(id));
1360 layout.set_window_floating(id.as_ref(), floating);
1361 }
1362 Op::FocusFloating => {
1363 layout.focus_floating();
1364 }
1365 Op::FocusTiling => {
1366 layout.focus_tiling();
1367 }
1368 Op::SwitchFocusFloatingTiling => {
1369 layout.switch_focus_floating_tiling();
1370 }
1371 Op::MoveFloatingWindow { id, x, y, animate } => {
1372 let id = id.filter(|id| layout.has_window(id));
1373 layout.move_floating_window(id.as_ref(), x, y, animate);
1374 }
1375 Op::SetParent {
1376 id,
1377 mut new_parent_id,
1378 } => {
1379 if !layout.has_window(&id) {
1380 return;
1381 }
1382
1383 if let Some(parent_id) = new_parent_id {
1384 if parent_id_causes_loop(layout, id, parent_id) {
1385 new_parent_id = None;
1386 }
1387 }
1388
1389 let mut update = false;
1390
1391 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
1392 if move_.tile.window().0.id == id {
1393 move_.tile.window().0.parent_id.set(new_parent_id);
1394 update = true;
1395 }
1396 }
1397
1398 match &mut layout.monitor_set {
1399 MonitorSet::Normal { monitors, .. } => {
1400 'outer: for mon in monitors {
1401 for ws in &mut mon.workspaces {
1402 for win in ws.windows() {
1403 if win.0.id == id {
1404 win.0.parent_id.set(new_parent_id);
1405 update = true;
1406 break 'outer;
1407 }
1408 }
1409 }
1410 }
1411 }
1412 MonitorSet::NoOutputs { workspaces, .. } => {
1413 'outer: for ws in workspaces {
1414 for win in ws.windows() {
1415 if win.0.id == id {
1416 win.0.parent_id.set(new_parent_id);
1417 update = true;
1418 break 'outer;
1419 }
1420 }
1421 }
1422 }
1423 }
1424
1425 if update {
1426 if let Some(new_parent_id) = new_parent_id {
1427 layout.descendants_added(&new_parent_id);
1428 }
1429 }
1430 }
1431 Op::SetForcedSize { id, size } => {
1432 for (_mon, win) in layout.windows() {
1433 if win.0.id == id {
1434 win.0.forced_size.set(size);
1435 return;
1436 }
1437 }
1438 }
1439 Op::Communicate(id) => {
1440 let mut update = false;
1441
1442 if let Some(InteractiveMoveState::Moving(move_)) = &layout.interactive_move {
1443 if move_.tile.window().0.id == id {
1444 if move_.tile.window().communicate() {
1445 update = true;
1446 }
1447
1448 if update {
1449 // FIXME: serial.
1450 layout.update_window(&id, None);
1451 }
1452 return;
1453 }
1454 }
1455
1456 match &mut layout.monitor_set {
1457 MonitorSet::Normal { monitors, .. } => {
1458 'outer: for mon in monitors {
1459 for ws in &mut mon.workspaces {
1460 for win in ws.windows() {
1461 if win.0.id == id {
1462 if win.communicate() {
1463 update = true;
1464 }
1465 break 'outer;
1466 }
1467 }
1468 }
1469 }
1470 }
1471 MonitorSet::NoOutputs { workspaces, .. } => {
1472 'outer: for ws in workspaces {
1473 for win in ws.windows() {
1474 if win.0.id == id {
1475 if win.communicate() {
1476 update = true;
1477 }
1478 break 'outer;
1479 }
1480 }
1481 }
1482 }
1483 }
1484
1485 if update {
1486 // FIXME: serial.
1487 layout.update_window(&id, None);
1488 }
1489 }
1490 Op::Refresh { is_active } => {
1491 layout.refresh(is_active);
1492 }
1493 Op::AdvanceAnimations { msec_delta } => {
1494 let mut now = layout.clock.now_unadjusted();
1495 if msec_delta >= 0 {
1496 now = now.saturating_add(Duration::from_millis(msec_delta as u64));
1497 } else {
1498 now = now.saturating_sub(Duration::from_millis(-msec_delta as u64));
1499 }
1500 layout.clock.set_unadjusted(now);
1501 layout.advance_animations();
1502 }
1503 Op::CompleteAnimations => {
1504 layout.clock.set_complete_instantly(true);
1505 layout.advance_animations();
1506 layout.clock.set_complete_instantly(false);
1507 }
1508 Op::MoveWorkspaceToOutput(id) => {
1509 let name = format!("output{id}");
1510 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1511 return;
1512 };
1513
1514 layout.move_workspace_to_output(&output);
1515 }
1516 Op::ViewOffsetGestureBegin {
1517 output_idx: id,
1518 workspace_idx,
1519 is_touchpad: normalize,
1520 } => {
1521 let name = format!("output{id}");
1522 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1523 return;
1524 };
1525
1526 layout.view_offset_gesture_begin(&output, workspace_idx, normalize);
1527 }
1528 Op::ViewOffsetGestureUpdate {
1529 delta,
1530 timestamp,
1531 is_touchpad,
1532 } => {
1533 layout.view_offset_gesture_update(delta, timestamp, is_touchpad);
1534 }
1535 Op::ViewOffsetGestureEnd { is_touchpad } => {
1536 layout.view_offset_gesture_end(is_touchpad);
1537 }
1538 Op::WorkspaceSwitchGestureBegin {
1539 output_idx: id,
1540 is_touchpad,
1541 } => {
1542 let name = format!("output{id}");
1543 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1544 return;
1545 };
1546
1547 layout.workspace_switch_gesture_begin(&output, is_touchpad);
1548 }
1549 Op::WorkspaceSwitchGestureUpdate {
1550 delta,
1551 timestamp,
1552 is_touchpad,
1553 } => {
1554 layout.workspace_switch_gesture_update(delta, timestamp, is_touchpad);
1555 }
1556 Op::WorkspaceSwitchGestureEnd { is_touchpad } => {
1557 layout.workspace_switch_gesture_end(is_touchpad);
1558 }
1559 Op::OverviewGestureBegin => {
1560 layout.overview_gesture_begin();
1561 }
1562 Op::OverviewGestureUpdate { delta, timestamp } => {
1563 layout.overview_gesture_update(delta, timestamp);
1564 }
1565 Op::OverviewGestureEnd => {
1566 layout.overview_gesture_end();
1567 }
1568 Op::InteractiveMoveBegin {
1569 window,
1570 output_idx,
1571 px,
1572 py,
1573 } => {
1574 let name = format!("output{output_idx}");
1575 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1576 return;
1577 };
1578 layout.interactive_move_begin(window, &output, Point::from((px, py)));
1579 }
1580 Op::InteractiveMoveUpdate {
1581 window,
1582 dx,
1583 dy,
1584 output_idx,
1585 px,
1586 py,
1587 } => {
1588 let name = format!("output{output_idx}");
1589 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1590 return;
1591 };
1592 layout.interactive_move_update(
1593 &window,
1594 Point::from((dx, dy)),
1595 output,
1596 Point::from((px, py)),
1597 );
1598 }
1599 Op::InteractiveMoveEnd { window } => {
1600 layout.interactive_move_end(&window);
1601 }
1602 Op::DndUpdate { output_idx, px, py } => {
1603 let name = format!("output{output_idx}");
1604 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1605 return;
1606 };
1607 layout.dnd_update(output, Point::from((px, py)));
1608 }
1609 Op::DndEnd => {
1610 layout.dnd_end();
1611 }
1612 Op::InteractiveResizeBegin { window, edges } => {
1613 layout.interactive_resize_begin(window, edges);
1614 }
1615 Op::InteractiveResizeUpdate { window, dx, dy } => {
1616 layout.interactive_resize_update(&window, Point::from((dx, dy)));
1617 }
1618 Op::InteractiveResizeEnd { window } => {
1619 layout.interactive_resize_end(&window);
1620 }
1621 Op::ToggleOverview => {
1622 layout.toggle_overview();
1623 }
1624 Op::UpdateConfig { layout_config } => {
1625 let options = Options {
1626 layout: niri_config::Layout::from_part(&layout_config),
1627 ..Default::default()
1628 };
1629
1630 layout.update_options(options);
1631 }
1632 }
1633 }
1634}
1635
1636#[track_caller]
1637fn check_ops_on_layout(layout: &mut Layout<TestWindow>, ops: impl IntoIterator<Item = Op>) {
1638 for op in ops {
1639 op.apply(layout);
1640 layout.verify_invariants();
1641 }
1642}
1643
1644#[track_caller]
1645fn check_ops(ops: impl IntoIterator<Item = Op>) -> Layout<TestWindow> {
1646 let mut layout = Layout::default();
1647 check_ops_on_layout(&mut layout, ops);
1648 layout
1649}
1650
1651fn workspace_output_name(layout: &Layout<TestWindow>, ws_name: usize) -> &str {
1652 layout
1653 .workspaces()
1654 .find(|(_, _, workspace)| workspace.name() == Some(&format!("ws{ws_name}")))
1655 .and_then(|(monitor, _, _)| monitor)
1656 .unwrap()
1657 .output_name()
1658}
1659
1660#[test]
1661fn workspace_output_fallback_promotes_preferred_in_reverse_connection_order() {
1662 // output3 is not a candidate, so it stays the primary monitor throughout; every assertion
1663 // below can only hold because of the candidate list.
1664 let mut layout = Layout::default();
1665 check_ops_on_layout(
1666 &mut layout,
1667 [
1668 Op::AddOutput(3),
1669 Op::AddNamedWorkspace {
1670 ws_name: 1,
1671 output_names: vec![1, 2],
1672 layout_config: None,
1673 },
1674 ],
1675 );
1676 assert_eq!(workspace_output_name(&layout, 1), "output3");
1677
1678 // The second candidate connects first; the workspace must leave the primary monitor.
1679 check_ops_on_layout(&mut layout, [Op::AddOutput(2)]);
1680 assert_eq!(workspace_output_name(&layout, 1), "output2");
1681
1682 // The first candidate connects last; it still wins.
1683 check_ops_on_layout(&mut layout, [Op::AddOutput(1)]);
1684 assert_eq!(workspace_output_name(&layout, 1), "output1");
1685}
1686
1687#[test]
1688fn workspace_output_preferred_removal_uses_fallback() {
1689 let mut layout = Layout::default();
1690 check_ops_on_layout(
1691 &mut layout,
1692 [
1693 Op::AddOutput(3),
1694 Op::AddNamedWorkspace {
1695 ws_name: 1,
1696 output_names: vec![1, 2],
1697 layout_config: None,
1698 },
1699 Op::AddOutput(1),
1700 Op::AddOutput(2),
1701 ],
1702 );
1703 assert_eq!(workspace_output_name(&layout, 1), "output1");
1704
1705 // The preferred candidate disconnects: the next candidate wins over the primary monitor.
1706 check_ops_on_layout(&mut layout, [Op::RemoveOutput(1)]);
1707 assert_eq!(workspace_output_name(&layout, 1), "output2");
1708
1709 // No candidate left: fall back to the primary monitor.
1710 check_ops_on_layout(&mut layout, [Op::RemoveOutput(2)]);
1711 assert_eq!(workspace_output_name(&layout, 1), "output3");
1712}
1713
1714#[test]
1715fn workspace_output_without_match_stays_on_default_output() {
1716 let mut layout = Layout::default();
1717 check_ops_on_layout(
1718 &mut layout,
1719 [
1720 Op::AddOutput(3),
1721 Op::AddNamedWorkspace {
1722 ws_name: 1,
1723 output_names: vec![1, 2],
1724 layout_config: None,
1725 },
1726 ],
1727 );
1728 assert_eq!(workspace_output_name(&layout, 1), "output3");
1729
1730 // An output that is not in the candidate list must not take the workspace.
1731 check_ops_on_layout(&mut layout, [Op::AddOutput(4)]);
1732 assert_eq!(workspace_output_name(&layout, 1), "output3");
1733}
1734
1735#[test]
1736fn explicit_workspace_move_overrides_output_candidates() {
1737 let mut layout = Layout::default();
1738 check_ops_on_layout(
1739 &mut layout,
1740 [
1741 Op::AddOutput(3),
1742 Op::AddNamedWorkspace {
1743 ws_name: 1,
1744 output_names: vec![1, 2],
1745 layout_config: None,
1746 },
1747 Op::AddOutput(2),
1748 ],
1749 );
1750 assert_eq!(workspace_output_name(&layout, 1), "output2");
1751
1752 // After an explicit move, the candidate list no longer applies, even when the preferred
1753 // candidate connects afterwards.
1754 check_ops_on_layout(
1755 &mut layout,
1756 [
1757 Op::MoveWorkspaceToMonitor {
1758 ws_name: Some(1),
1759 output_id: 3,
1760 },
1761 Op::AddOutput(1),
1762 ],
1763 );
1764 assert_eq!(workspace_output_name(&layout, 1), "output3");
1765}
1766
1767#[track_caller]
1768fn check_ops_with_options(
1769 options: Options,
1770 ops: impl IntoIterator<Item = Op>,
1771) -> Layout<TestWindow> {
1772 let mut layout = Layout::with_options(Clock::with_time(Duration::ZERO), options);
1773 check_ops_on_layout(&mut layout, ops);
1774 layout
1775}
1776
1777#[test]
1778fn operations_dont_panic() {
1779 if std::env::var_os("RUN_SLOW_TESTS").is_none() {
1780 eprintln!("ignoring slow test");
1781 return;
1782 }
1783
1784 let every_op = [
1785 Op::AddOutput(0),
1786 Op::AddOutput(1),
1787 Op::AddOutput(2),
1788 Op::RemoveOutput(0),
1789 Op::RemoveOutput(1),
1790 Op::RemoveOutput(2),
1791 Op::FocusOutput(0),
1792 Op::FocusOutput(1),
1793 Op::FocusOutput(2),
1794 Op::AddNamedWorkspace {
1795 ws_name: 1,
1796 output_names: vec![1],
1797 layout_config: None,
1798 },
1799 Op::UnnameWorkspace { ws_name: 1 },
1800 Op::AddWindow {
1801 params: TestWindowParams::new(0),
1802 },
1803 Op::AddWindow {
1804 params: TestWindowParams::new(1),
1805 },
1806 Op::AddWindowNextTo {
1807 params: TestWindowParams::new(2),
1808 next_to_id: 1,
1809 },
1810 Op::AddWindowToNamedWorkspace {
1811 params: TestWindowParams::new(3),
1812 ws_name: 1,
1813 },
1814 Op::CloseWindow(0),
1815 Op::CloseWindow(1),
1816 Op::CloseWindow(2),
1817 Op::FullscreenWindow(1),
1818 Op::FullscreenWindow(2),
1819 Op::FullscreenWindow(3),
1820 Op::MaximizeWindowToEdges { id: Some(1) },
1821 Op::MaximizeWindowToEdges { id: Some(2) },
1822 Op::MaximizeWindowToEdges { id: Some(3) },
1823 Op::FocusColumnLeft,
1824 Op::FocusColumnRight,
1825 Op::FocusColumnRightOrFirst,
1826 Op::FocusColumnLeftOrLast,
1827 Op::FocusWindowOrMonitorUp(0),
1828 Op::FocusWindowOrMonitorDown(1),
1829 Op::FocusColumnOrMonitorLeft(0),
1830 Op::FocusColumnOrMonitorRight(1),
1831 Op::FocusWindowUp,
1832 Op::FocusWindowUpOrColumnLeft,
1833 Op::FocusWindowUpOrColumnRight,
1834 Op::FocusWindowOrWorkspaceUp,
1835 Op::FocusWindowDown,
1836 Op::FocusWindowDownOrColumnLeft,
1837 Op::FocusWindowDownOrColumnRight,
1838 Op::FocusWindowOrWorkspaceDown,
1839 Op::MoveColumnLeft,
1840 Op::MoveColumnRight,
1841 Op::MoveColumnLeftOrToMonitorLeft(0),
1842 Op::MoveColumnRightOrToMonitorRight(1),
1843 Op::ConsumeWindowIntoColumn,
1844 Op::ExpelWindowFromColumn,
1845 Op::CenterColumn,
1846 Op::FocusWorkspaceDown,
1847 Op::FocusWorkspaceUp,
1848 Op::FocusWorkspace(1),
1849 Op::FocusWorkspace(2),
1850 Op::MoveWindowToWorkspaceDown(true),
1851 Op::MoveWindowToWorkspaceUp(true),
1852 Op::MoveWindowToWorkspace {
1853 window_id: None,
1854 workspace_idx: 1,
1855 },
1856 Op::MoveWindowToWorkspace {
1857 window_id: None,
1858 workspace_idx: 2,
1859 },
1860 Op::MoveColumnToWorkspaceDown(true),
1861 Op::MoveColumnToWorkspaceUp(true),
1862 Op::MoveColumnToWorkspace(1, true),
1863 Op::MoveColumnToWorkspace(2, true),
1864 Op::MoveWindowDown,
1865 Op::MoveWindowDownOrToWorkspaceDown,
1866 Op::MoveWindowUp,
1867 Op::MoveWindowUpOrToWorkspaceUp,
1868 Op::ConsumeOrExpelWindowLeft { id: None },
1869 Op::ConsumeOrExpelWindowRight { id: None },
1870 Op::MoveWorkspaceToOutput(1),
1871 Op::ToggleColumnTabbedDisplay,
1872 ];
1873
1874 for third in &every_op {
1875 for second in &every_op {
1876 for first in &every_op {
1877 // eprintln!("{first:?}, {second:?}, {third:?}");
1878
1879 let mut layout = Layout::default();
1880 first.clone().apply(&mut layout);
1881 layout.verify_invariants();
1882 second.clone().apply(&mut layout);
1883 layout.verify_invariants();
1884 third.clone().apply(&mut layout);
1885 layout.verify_invariants();
1886 }
1887 }
1888 }
1889}
1890
1891#[test]
1892fn operations_from_starting_state_dont_panic() {
1893 if std::env::var_os("RUN_SLOW_TESTS").is_none() {
1894 eprintln!("ignoring slow test");
1895 return;
1896 }
1897
1898 // Running every op from an empty state doesn't get us to all the interesting states. So,
1899 // also run it from a manually-created starting state with more things going on to exercise
1900 // more code paths.
1901 let setup_ops = [
1902 Op::AddOutput(1),
1903 Op::AddWindow {
1904 params: TestWindowParams::new(1),
1905 },
1906 Op::MoveWindowToWorkspaceDown(true),
1907 Op::AddWindow {
1908 params: TestWindowParams::new(2),
1909 },
1910 Op::AddWindow {
1911 params: TestWindowParams::new(3),
1912 },
1913 Op::FocusColumnLeft,
1914 Op::ConsumeWindowIntoColumn,
1915 Op::AddWindow {
1916 params: TestWindowParams::new(4),
1917 },
1918 Op::AddOutput(2),
1919 Op::AddWindow {
1920 params: TestWindowParams::new(5),
1921 },
1922 Op::MoveWindowToOutput {
1923 window_id: None,
1924 output_id: 2,
1925 target_ws_idx: None,
1926 },
1927 Op::FocusOutput(1),
1928 Op::Communicate(1),
1929 Op::Communicate(2),
1930 Op::Communicate(3),
1931 Op::Communicate(4),
1932 Op::Communicate(5),
1933 ];
1934
1935 let every_op = [
1936 Op::AddOutput(0),
1937 Op::AddOutput(1),
1938 Op::AddOutput(2),
1939 Op::RemoveOutput(0),
1940 Op::RemoveOutput(1),
1941 Op::RemoveOutput(2),
1942 Op::FocusOutput(0),
1943 Op::FocusOutput(1),
1944 Op::FocusOutput(2),
1945 Op::AddNamedWorkspace {
1946 ws_name: 1,
1947 output_names: vec![1],
1948 layout_config: None,
1949 },
1950 Op::UnnameWorkspace { ws_name: 1 },
1951 Op::AddWindow {
1952 params: TestWindowParams::new(0),
1953 },
1954 Op::AddWindow {
1955 params: TestWindowParams::new(1),
1956 },
1957 Op::AddWindow {
1958 params: TestWindowParams::new(2),
1959 },
1960 Op::AddWindowNextTo {
1961 params: TestWindowParams::new(6),
1962 next_to_id: 0,
1963 },
1964 Op::AddWindowNextTo {
1965 params: TestWindowParams::new(7),
1966 next_to_id: 1,
1967 },
1968 Op::AddWindowToNamedWorkspace {
1969 params: TestWindowParams::new(5),
1970 ws_name: 1,
1971 },
1972 Op::CloseWindow(0),
1973 Op::CloseWindow(1),
1974 Op::CloseWindow(2),
1975 Op::FullscreenWindow(1),
1976 Op::FullscreenWindow(2),
1977 Op::FullscreenWindow(3),
1978 Op::MaximizeWindowToEdges { id: Some(1) },
1979 Op::MaximizeWindowToEdges { id: Some(2) },
1980 Op::MaximizeWindowToEdges { id: Some(3) },
1981 Op::SetFullscreenWindow {
1982 window: 1,
1983 is_fullscreen: false,
1984 },
1985 Op::SetFullscreenWindow {
1986 window: 1,
1987 is_fullscreen: true,
1988 },
1989 Op::SetFullscreenWindow {
1990 window: 2,
1991 is_fullscreen: false,
1992 },
1993 Op::SetFullscreenWindow {
1994 window: 2,
1995 is_fullscreen: true,
1996 },
1997 Op::FocusColumnLeft,
1998 Op::FocusColumnRight,
1999 Op::FocusColumnRightOrFirst,
2000 Op::FocusColumnLeftOrLast,
2001 Op::FocusWindowOrMonitorUp(0),
2002 Op::FocusWindowOrMonitorDown(1),
2003 Op::FocusColumnOrMonitorLeft(0),
2004 Op::FocusColumnOrMonitorRight(1),
2005 Op::FocusWindowUp,
2006 Op::FocusWindowUpOrColumnLeft,
2007 Op::FocusWindowUpOrColumnRight,
2008 Op::FocusWindowOrWorkspaceUp,
2009 Op::FocusWindowDown,
2010 Op::FocusWindowDownOrColumnLeft,
2011 Op::FocusWindowDownOrColumnRight,
2012 Op::FocusWindowOrWorkspaceDown,
2013 Op::MoveColumnLeft,
2014 Op::MoveColumnRight,
2015 Op::MoveColumnLeftOrToMonitorLeft(0),
2016 Op::MoveColumnRightOrToMonitorRight(1),
2017 Op::ConsumeWindowIntoColumn,
2018 Op::ExpelWindowFromColumn,
2019 Op::CenterColumn,
2020 Op::FocusWorkspaceDown,
2021 Op::FocusWorkspaceUp,
2022 Op::FocusWorkspace(1),
2023 Op::FocusWorkspace(2),
2024 Op::FocusWorkspace(3),
2025 Op::MoveWindowToWorkspaceDown(true),
2026 Op::MoveWindowToWorkspaceUp(true),
2027 Op::MoveWindowToWorkspace {
2028 window_id: None,
2029 workspace_idx: 1,
2030 },
2031 Op::MoveWindowToWorkspace {
2032 window_id: None,
2033 workspace_idx: 2,
2034 },
2035 Op::MoveWindowToWorkspace {
2036 window_id: None,
2037 workspace_idx: 3,
2038 },
2039 Op::MoveColumnToWorkspaceDown(true),
2040 Op::MoveColumnToWorkspaceUp(true),
2041 Op::MoveColumnToWorkspace(1, true),
2042 Op::MoveColumnToWorkspace(2, true),
2043 Op::MoveColumnToWorkspace(3, true),
2044 Op::MoveWindowDown,
2045 Op::MoveWindowDownOrToWorkspaceDown,
2046 Op::MoveWindowUp,
2047 Op::MoveWindowUpOrToWorkspaceUp,
2048 Op::ConsumeOrExpelWindowLeft { id: None },
2049 Op::ConsumeOrExpelWindowRight { id: None },
2050 Op::ToggleColumnTabbedDisplay,
2051 ];
2052
2053 for third in &every_op {
2054 for second in &every_op {
2055 for first in &every_op {
2056 // eprintln!("{first:?}, {second:?}, {third:?}");
2057
2058 let mut layout = Layout::default();
2059 for op in &setup_ops {
2060 op.clone().apply(&mut layout);
2061 }
2062
2063 let mut layout = Layout::default();
2064 first.clone().apply(&mut layout);
2065 layout.verify_invariants();
2066 second.clone().apply(&mut layout);
2067 layout.verify_invariants();
2068 third.clone().apply(&mut layout);
2069 layout.verify_invariants();
2070 }
2071 }
2072 }
2073}
2074
2075#[test]
2076fn primary_active_workspace_idx_not_updated_on_output_add() {
2077 let ops = [
2078 Op::AddOutput(1),
2079 Op::AddOutput(2),
2080 Op::FocusOutput(1),
2081 Op::AddWindow {
2082 params: TestWindowParams::new(0),
2083 },
2084 Op::FocusOutput(2),
2085 Op::AddWindow {
2086 params: TestWindowParams::new(1),
2087 },
2088 Op::RemoveOutput(2),
2089 Op::FocusWorkspace(3),
2090 Op::AddOutput(2),
2091 ];
2092
2093 check_ops(ops);
2094}
2095
2096#[test]
2097fn window_closed_on_previous_workspace() {
2098 let ops = [
2099 Op::AddOutput(1),
2100 Op::AddWindow {
2101 params: TestWindowParams::new(0),
2102 },
2103 Op::FocusWorkspaceDown,
2104 Op::CloseWindow(0),
2105 ];
2106
2107 check_ops(ops);
2108}
2109
2110#[test]
2111fn removing_output_must_keep_empty_focus_on_primary() {
2112 let ops = [
2113 Op::AddOutput(1),
2114 Op::AddWindow {
2115 params: TestWindowParams::new(0),
2116 },
2117 Op::AddOutput(2),
2118 Op::RemoveOutput(1),
2119 ];
2120
2121 let layout = check_ops(ops);
2122
2123 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2124 unreachable!()
2125 };
2126
2127 // The workspace from the removed output was inserted at position 0, so the active workspace
2128 // must change to 1 to keep the focus on the empty workspace.
2129 assert_eq!(monitors[0].active_workspace_idx, 1);
2130}
2131
2132#[test]
2133fn move_to_workspace_by_idx_does_not_leave_empty_workspaces() {
2134 let ops = [
2135 Op::AddOutput(1),
2136 Op::AddWindow {
2137 params: TestWindowParams::new(0),
2138 },
2139 Op::AddOutput(2),
2140 Op::FocusOutput(2),
2141 Op::AddWindow {
2142 params: TestWindowParams::new(1),
2143 },
2144 Op::RemoveOutput(1),
2145 Op::MoveWindowToWorkspace {
2146 window_id: Some(0),
2147 workspace_idx: 2,
2148 },
2149 ];
2150
2151 let layout = check_ops(ops);
2152
2153 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2154 unreachable!()
2155 };
2156
2157 assert!(monitors[0].workspaces[1].has_windows());
2158}
2159
2160#[test]
2161fn empty_workspaces_dont_move_back_to_original_output() {
2162 let ops = [
2163 Op::AddOutput(1),
2164 Op::AddWindow {
2165 params: TestWindowParams::new(1),
2166 },
2167 Op::FocusWorkspaceDown,
2168 Op::AddWindow {
2169 params: TestWindowParams::new(2),
2170 },
2171 Op::AddOutput(2),
2172 Op::RemoveOutput(1),
2173 Op::FocusWorkspace(1),
2174 Op::CloseWindow(1),
2175 Op::AddOutput(1),
2176 ];
2177
2178 check_ops(ops);
2179}
2180
2181#[test]
2182fn named_workspaces_dont_update_original_output_on_adding_window() {
2183 let ops = [
2184 Op::AddOutput(1),
2185 Op::SetWorkspaceName {
2186 new_ws_name: 1,
2187 ws_name: None,
2188 },
2189 Op::AddOutput(2),
2190 Op::RemoveOutput(1),
2191 Op::FocusWorkspaceUp,
2192 // Adding a window updates the original output for unnamed workspaces.
2193 Op::AddWindow {
2194 params: TestWindowParams::new(1),
2195 },
2196 // Connecting the previous output should move the named workspace back since its
2197 // original output wasn't updated.
2198 Op::AddOutput(1),
2199 ];
2200
2201 let layout = check_ops(ops);
2202 let (mon, _, ws) = layout
2203 .workspaces()
2204 .find(|(_, _, ws)| ws.name().is_some())
2205 .unwrap();
2206 assert!(ws.name().is_some()); // Sanity check.
2207 let mon = mon.unwrap();
2208 assert_eq!(mon.output_name(), "output1");
2209}
2210
2211#[test]
2212fn workspaces_update_original_output_on_moving_to_same_output() {
2213 let ops = [
2214 Op::AddOutput(1),
2215 Op::SetWorkspaceName {
2216 new_ws_name: 1,
2217 ws_name: None,
2218 },
2219 Op::AddOutput(2),
2220 Op::RemoveOutput(1),
2221 Op::FocusWorkspaceUp,
2222 Op::MoveWorkspaceToOutput(2),
2223 Op::AddOutput(1),
2224 ];
2225
2226 let layout = check_ops(ops);
2227 let (mon, _, ws) = layout
2228 .workspaces()
2229 .find(|(_, _, ws)| ws.name().is_some())
2230 .unwrap();
2231 assert!(ws.name().is_some()); // Sanity check.
2232 let mon = mon.unwrap();
2233 assert_eq!(mon.output_name(), "output2");
2234}
2235
2236#[test]
2237fn workspaces_update_original_output_on_moving_to_same_monitor() {
2238 let ops = [
2239 Op::AddOutput(1),
2240 Op::SetWorkspaceName {
2241 new_ws_name: 1,
2242 ws_name: None,
2243 },
2244 Op::AddOutput(2),
2245 Op::RemoveOutput(1),
2246 Op::FocusWorkspaceUp,
2247 Op::MoveWorkspaceToMonitor {
2248 ws_name: Some(1),
2249 output_id: 2,
2250 },
2251 Op::AddOutput(1),
2252 ];
2253
2254 let layout = check_ops(ops);
2255 let (mon, _, ws) = layout
2256 .workspaces()
2257 .find(|(_, _, ws)| ws.name().is_some())
2258 .unwrap();
2259 assert!(ws.name().is_some()); // Sanity check.
2260 let mon = mon.unwrap();
2261 assert_eq!(mon.output_name(), "output2");
2262}
2263
2264#[test]
2265fn large_negative_height_change() {
2266 let ops = [
2267 Op::AddOutput(1),
2268 Op::AddWindow {
2269 params: TestWindowParams::new(1),
2270 },
2271 Op::SetWindowHeight {
2272 id: None,
2273 change: SizeChange::AdjustProportion(-1e129),
2274 },
2275 ];
2276
2277 let mut options = Options::default();
2278 options.layout.border.off = false;
2279 options.layout.border.width = 1.;
2280
2281 check_ops_with_options(options, ops);
2282}
2283
2284#[test]
2285fn large_max_size() {
2286 let ops = [
2287 Op::AddOutput(1),
2288 Op::AddWindow {
2289 params: TestWindowParams {
2290 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2291 ..TestWindowParams::new(1)
2292 },
2293 },
2294 ];
2295
2296 let mut options = Options::default();
2297 options.layout.border.off = false;
2298 options.layout.border.width = 1.;
2299
2300 check_ops_with_options(options, ops);
2301}
2302
2303#[test]
2304fn workspace_cleanup_during_switch() {
2305 let ops = [
2306 Op::AddOutput(1),
2307 Op::AddWindow {
2308 params: TestWindowParams::new(1),
2309 },
2310 Op::FocusWorkspaceDown,
2311 Op::CloseWindow(1),
2312 ];
2313
2314 check_ops(ops);
2315}
2316
2317#[test]
2318fn workspace_transfer_during_switch() {
2319 let ops = [
2320 Op::AddOutput(1),
2321 Op::AddWindow {
2322 params: TestWindowParams::new(1),
2323 },
2324 Op::AddOutput(2),
2325 Op::FocusOutput(2),
2326 Op::AddWindow {
2327 params: TestWindowParams::new(2),
2328 },
2329 Op::RemoveOutput(1),
2330 Op::FocusWorkspaceDown,
2331 Op::FocusWorkspaceDown,
2332 Op::AddOutput(1),
2333 ];
2334
2335 check_ops(ops);
2336}
2337
2338#[test]
2339fn workspace_transfer_during_switch_from_last() {
2340 let ops = [
2341 Op::AddOutput(1),
2342 Op::AddWindow {
2343 params: TestWindowParams::new(1),
2344 },
2345 Op::AddOutput(2),
2346 Op::RemoveOutput(1),
2347 Op::FocusWorkspaceUp,
2348 Op::AddOutput(1),
2349 ];
2350
2351 check_ops(ops);
2352}
2353
2354#[test]
2355fn workspace_transfer_during_switch_gets_cleaned_up() {
2356 let ops = [
2357 Op::AddOutput(1),
2358 Op::AddWindow {
2359 params: TestWindowParams::new(1),
2360 },
2361 Op::RemoveOutput(1),
2362 Op::AddOutput(2),
2363 Op::MoveColumnToWorkspaceDown(true),
2364 Op::MoveColumnToWorkspaceDown(true),
2365 Op::AddOutput(1),
2366 ];
2367
2368 check_ops(ops);
2369}
2370
2371#[test]
2372fn move_workspace_to_output() {
2373 let ops = [
2374 Op::AddOutput(1),
2375 Op::AddOutput(2),
2376 Op::FocusOutput(1),
2377 Op::AddWindow {
2378 params: TestWindowParams::new(0),
2379 },
2380 Op::MoveWorkspaceToOutput(2),
2381 ];
2382
2383 let layout = check_ops(ops);
2384
2385 let MonitorSet::Normal {
2386 monitors,
2387 active_monitor_idx,
2388 ..
2389 } = layout.monitor_set
2390 else {
2391 unreachable!()
2392 };
2393
2394 assert_eq!(active_monitor_idx, 1);
2395 assert_eq!(monitors[0].workspaces.len(), 1);
2396 assert!(!monitors[0].workspaces[0].has_windows());
2397 assert_eq!(monitors[1].active_workspace_idx, 0);
2398 assert_eq!(monitors[1].workspaces.len(), 2);
2399 assert!(monitors[1].workspaces[0].has_windows());
2400}
2401
2402#[test]
2403fn open_right_of_on_different_workspace() {
2404 let ops = [
2405 Op::AddOutput(1),
2406 Op::AddWindow {
2407 params: TestWindowParams::new(1),
2408 },
2409 Op::FocusWorkspaceDown,
2410 Op::AddWindow {
2411 params: TestWindowParams::new(2),
2412 },
2413 Op::AddWindowNextTo {
2414 params: TestWindowParams::new(3),
2415 next_to_id: 1,
2416 },
2417 ];
2418
2419 let layout = check_ops(ops);
2420
2421 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2422 unreachable!()
2423 };
2424
2425 let mon = monitors.into_iter().next().unwrap();
2426 assert_eq!(
2427 mon.active_workspace_idx, 1,
2428 "the second workspace must remain active"
2429 );
2430 assert_eq!(
2431 mon.workspaces[0].scrolling().active_column_idx(),
2432 1,
2433 "the new window must become active"
2434 );
2435}
2436
2437#[test]
2438// empty_workspace_above_first = true
2439fn open_right_of_on_different_workspace_ewaf() {
2440 let ops = [
2441 Op::AddOutput(1),
2442 Op::AddWindow {
2443 params: TestWindowParams::new(1),
2444 },
2445 Op::FocusWorkspaceDown,
2446 Op::AddWindow {
2447 params: TestWindowParams::new(2),
2448 },
2449 Op::AddWindowNextTo {
2450 params: TestWindowParams::new(3),
2451 next_to_id: 1,
2452 },
2453 ];
2454
2455 let options = Options {
2456 layout: niri_config::Layout {
2457 empty_workspace_above_first: true,
2458 ..Default::default()
2459 },
2460 ..Default::default()
2461 };
2462 let layout = check_ops_with_options(options, ops);
2463
2464 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2465 unreachable!()
2466 };
2467
2468 let mon = monitors.into_iter().next().unwrap();
2469 assert_eq!(
2470 mon.active_workspace_idx, 2,
2471 "the second workspace must remain active"
2472 );
2473 assert_eq!(
2474 mon.workspaces[1].scrolling().active_column_idx(),
2475 1,
2476 "the new window must become active"
2477 );
2478}
2479
2480#[test]
2481fn removing_all_outputs_preserves_empty_named_workspaces() {
2482 let ops = [
2483 Op::AddOutput(1),
2484 Op::AddNamedWorkspace {
2485 ws_name: 1,
2486 output_names: vec![],
2487 layout_config: None,
2488 },
2489 Op::AddNamedWorkspace {
2490 ws_name: 2,
2491 output_names: vec![],
2492 layout_config: None,
2493 },
2494 Op::RemoveOutput(1),
2495 ];
2496
2497 let layout = check_ops(ops);
2498
2499 let MonitorSet::NoOutputs { workspaces } = layout.monitor_set else {
2500 unreachable!()
2501 };
2502
2503 assert_eq!(workspaces.len(), 2);
2504}
2505
2506#[test]
2507fn config_change_updates_cached_sizes() {
2508 let mut config = Config::default();
2509 let border = &mut config.layout.border;
2510 border.off = false;
2511 border.width = 2.;
2512
2513 let mut layout = Layout::new(Clock::default(), &config);
2514
2515 Op::AddWindow {
2516 params: TestWindowParams {
2517 bbox: Rectangle::from_size(Size::from((1280, 200))),
2518 ..TestWindowParams::new(1)
2519 },
2520 }
2521 .apply(&mut layout);
2522
2523 config.layout.border.width = 4.;
2524 layout.update_config(&config);
2525
2526 layout.verify_invariants();
2527}
2528
2529#[test]
2530fn preset_height_change_removes_preset() {
2531 let mut config = Config::default();
2532 config.layout.preset_window_heights = vec![PresetSize::Fixed(1), PresetSize::Fixed(2)];
2533
2534 let mut layout = Layout::new(Clock::default(), &config);
2535
2536 let ops = [
2537 Op::AddOutput(1),
2538 Op::AddWindow {
2539 params: TestWindowParams::new(1),
2540 },
2541 Op::AddWindow {
2542 params: TestWindowParams::new(2),
2543 },
2544 Op::ConsumeOrExpelWindowLeft { id: None },
2545 Op::SwitchPresetWindowHeight { id: None },
2546 Op::SwitchPresetWindowHeight { id: None },
2547 ];
2548 for op in ops {
2549 op.apply(&mut layout);
2550 }
2551
2552 // Leave only one.
2553 config.layout.preset_window_heights = vec![PresetSize::Fixed(1)];
2554
2555 layout.update_config(&config);
2556
2557 layout.verify_invariants();
2558}
2559
2560#[test]
2561fn set_window_height_recomputes_to_auto() {
2562 let ops = [
2563 Op::AddOutput(1),
2564 Op::AddWindow {
2565 params: TestWindowParams::new(0),
2566 },
2567 Op::AddWindow {
2568 params: TestWindowParams::new(1),
2569 },
2570 Op::ConsumeOrExpelWindowLeft { id: None },
2571 Op::AddWindow {
2572 params: TestWindowParams::new(2),
2573 },
2574 Op::ConsumeOrExpelWindowLeft { id: None },
2575 Op::SetWindowHeight {
2576 id: None,
2577 change: SizeChange::SetFixed(100),
2578 },
2579 Op::FocusWindowUp,
2580 Op::SetWindowHeight {
2581 id: None,
2582 change: SizeChange::SetFixed(200),
2583 },
2584 ];
2585
2586 check_ops(ops);
2587}
2588
2589#[test]
2590fn one_window_in_column_becomes_weight_1() {
2591 let ops = [
2592 Op::AddOutput(1),
2593 Op::AddWindow {
2594 params: TestWindowParams::new(0),
2595 },
2596 Op::AddWindow {
2597 params: TestWindowParams::new(1),
2598 },
2599 Op::ConsumeOrExpelWindowLeft { id: None },
2600 Op::AddWindow {
2601 params: TestWindowParams::new(2),
2602 },
2603 Op::ConsumeOrExpelWindowLeft { id: None },
2604 Op::SetWindowHeight {
2605 id: None,
2606 change: SizeChange::SetFixed(100),
2607 },
2608 Op::Communicate(2),
2609 Op::FocusWindowUp,
2610 Op::SetWindowHeight {
2611 id: None,
2612 change: SizeChange::SetFixed(200),
2613 },
2614 Op::Communicate(1),
2615 Op::CloseWindow(0),
2616 Op::CloseWindow(1),
2617 ];
2618
2619 check_ops(ops);
2620}
2621
2622#[test]
2623fn fixed_height_takes_max_non_auto_into_account() {
2624 let ops = [
2625 Op::AddOutput(1),
2626 Op::AddWindow {
2627 params: TestWindowParams::new(0),
2628 },
2629 Op::SetWindowHeight {
2630 id: Some(0),
2631 change: SizeChange::SetFixed(704),
2632 },
2633 Op::AddWindow {
2634 params: TestWindowParams::new(1),
2635 },
2636 Op::ConsumeOrExpelWindowLeft { id: None },
2637 ];
2638
2639 let options = Options {
2640 layout: niri_config::Layout {
2641 border: niri_config::Border {
2642 off: false,
2643 width: 4.,
2644 ..Default::default()
2645 },
2646 gaps: 0.,
2647 ..Default::default()
2648 },
2649 ..Default::default()
2650 };
2651 check_ops_with_options(options, ops);
2652}
2653
2654#[test]
2655fn start_interactive_move_then_remove_window() {
2656 let ops = [
2657 Op::AddOutput(1),
2658 Op::AddWindow {
2659 params: TestWindowParams::new(0),
2660 },
2661 Op::InteractiveMoveBegin {
2662 window: 0,
2663 output_idx: 1,
2664 px: 0.,
2665 py: 0.,
2666 },
2667 Op::CloseWindow(0),
2668 ];
2669
2670 check_ops(ops);
2671}
2672
2673#[test]
2674fn interactive_move_onto_empty_output() {
2675 let ops = [
2676 Op::AddOutput(1),
2677 Op::AddWindow {
2678 params: TestWindowParams::new(0),
2679 },
2680 Op::InteractiveMoveBegin {
2681 window: 0,
2682 output_idx: 1,
2683 px: 0.,
2684 py: 0.,
2685 },
2686 Op::AddOutput(2),
2687 Op::InteractiveMoveUpdate {
2688 window: 0,
2689 dx: 1000.,
2690 dy: 0.,
2691 output_idx: 2,
2692 px: 0.,
2693 py: 0.,
2694 },
2695 Op::InteractiveMoveEnd { window: 0 },
2696 ];
2697
2698 check_ops(ops);
2699}
2700
2701#[test]
2702fn interactive_move_onto_empty_output_ewaf() {
2703 let ops = [
2704 Op::AddOutput(1),
2705 Op::AddWindow {
2706 params: TestWindowParams::new(0),
2707 },
2708 Op::InteractiveMoveBegin {
2709 window: 0,
2710 output_idx: 1,
2711 px: 0.,
2712 py: 0.,
2713 },
2714 Op::AddOutput(2),
2715 Op::InteractiveMoveUpdate {
2716 window: 0,
2717 dx: 1000.,
2718 dy: 0.,
2719 output_idx: 2,
2720 px: 0.,
2721 py: 0.,
2722 },
2723 Op::InteractiveMoveEnd { window: 0 },
2724 ];
2725
2726 let options = Options {
2727 layout: niri_config::Layout {
2728 empty_workspace_above_first: true,
2729 ..Default::default()
2730 },
2731 ..Default::default()
2732 };
2733 check_ops_with_options(options, ops);
2734}
2735
2736#[test]
2737fn interactive_move_onto_last_workspace() {
2738 let ops = [
2739 Op::AddOutput(1),
2740 Op::AddWindow {
2741 params: TestWindowParams::new(0),
2742 },
2743 Op::InteractiveMoveBegin {
2744 window: 0,
2745 output_idx: 1,
2746 px: 0.,
2747 py: 0.,
2748 },
2749 Op::InteractiveMoveUpdate {
2750 window: 0,
2751 dx: 1000.,
2752 dy: 0.,
2753 output_idx: 1,
2754 px: 0.,
2755 py: 0.,
2756 },
2757 Op::FocusWorkspaceDown,
2758 Op::AdvanceAnimations { msec_delta: 1000 },
2759 Op::InteractiveMoveEnd { window: 0 },
2760 ];
2761
2762 check_ops(ops);
2763}
2764
2765#[test]
2766fn interactive_move_onto_first_empty_workspace() {
2767 let ops = [
2768 Op::AddOutput(1),
2769 Op::AddWindow {
2770 params: TestWindowParams::new(1),
2771 },
2772 Op::InteractiveMoveBegin {
2773 window: 1,
2774 output_idx: 1,
2775 px: 0.,
2776 py: 0.,
2777 },
2778 Op::InteractiveMoveUpdate {
2779 window: 1,
2780 dx: 1000.,
2781 dy: 0.,
2782 output_idx: 1,
2783 px: 0.,
2784 py: 0.,
2785 },
2786 Op::FocusWorkspaceUp,
2787 Op::AdvanceAnimations { msec_delta: 1000 },
2788 Op::InteractiveMoveEnd { window: 1 },
2789 ];
2790 let options = Options {
2791 layout: niri_config::Layout {
2792 empty_workspace_above_first: true,
2793 ..Default::default()
2794 },
2795 ..Default::default()
2796 };
2797 check_ops_with_options(options, ops);
2798}
2799
2800#[test]
2801fn output_active_workspace_is_preserved() {
2802 let ops = [
2803 Op::AddOutput(1),
2804 Op::AddWindow {
2805 params: TestWindowParams::new(1),
2806 },
2807 Op::FocusWorkspaceDown,
2808 Op::AddWindow {
2809 params: TestWindowParams::new(2),
2810 },
2811 Op::RemoveOutput(1),
2812 Op::AddOutput(1),
2813 ];
2814
2815 let layout = check_ops(ops);
2816
2817 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2818 unreachable!()
2819 };
2820
2821 assert_eq!(monitors[0].active_workspace_idx, 1);
2822}
2823
2824#[test]
2825fn output_active_workspace_is_preserved_with_other_outputs() {
2826 let ops = [
2827 Op::AddOutput(1),
2828 Op::AddOutput(2),
2829 Op::AddWindow {
2830 params: TestWindowParams::new(1),
2831 },
2832 Op::FocusWorkspaceDown,
2833 Op::AddWindow {
2834 params: TestWindowParams::new(2),
2835 },
2836 Op::RemoveOutput(1),
2837 Op::AddOutput(1),
2838 ];
2839
2840 let layout = check_ops(ops);
2841
2842 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2843 unreachable!()
2844 };
2845
2846 assert_eq!(monitors[1].active_workspace_idx, 1);
2847}
2848
2849#[test]
2850fn named_workspace_to_output() {
2851 let ops = [
2852 Op::AddNamedWorkspace {
2853 ws_name: 1,
2854 output_names: vec![],
2855 layout_config: None,
2856 },
2857 Op::AddOutput(1),
2858 Op::MoveWorkspaceToOutput(1),
2859 Op::FocusWorkspaceUp,
2860 ];
2861 check_ops(ops);
2862}
2863
2864#[test]
2865// empty_workspace_above_first = true
2866fn named_workspace_to_output_ewaf() {
2867 let ops = [
2868 Op::AddNamedWorkspace {
2869 ws_name: 1,
2870 output_names: vec![2],
2871 layout_config: None,
2872 },
2873 Op::AddOutput(1),
2874 Op::AddOutput(2),
2875 ];
2876 let options = Options {
2877 layout: niri_config::Layout {
2878 empty_workspace_above_first: true,
2879 ..Default::default()
2880 },
2881 ..Default::default()
2882 };
2883 check_ops_with_options(options, ops);
2884}
2885
2886#[test]
2887fn move_window_to_empty_workspace_above_first() {
2888 let ops = [
2889 Op::AddOutput(1),
2890 Op::AddWindow {
2891 params: TestWindowParams::new(1),
2892 },
2893 Op::MoveWorkspaceUp,
2894 Op::MoveWorkspaceDown,
2895 Op::FocusWorkspaceUp,
2896 Op::MoveWorkspaceDown,
2897 ];
2898 let options = Options {
2899 layout: niri_config::Layout {
2900 empty_workspace_above_first: true,
2901 ..Default::default()
2902 },
2903 ..Default::default()
2904 };
2905 check_ops_with_options(options, ops);
2906}
2907
2908#[test]
2909fn move_window_to_different_output() {
2910 let ops = [
2911 Op::AddWindow {
2912 params: TestWindowParams::new(1),
2913 },
2914 Op::AddOutput(1),
2915 Op::AddOutput(2),
2916 Op::MoveWorkspaceToOutput(2),
2917 ];
2918 let options = Options {
2919 layout: niri_config::Layout {
2920 empty_workspace_above_first: true,
2921 ..Default::default()
2922 },
2923 ..Default::default()
2924 };
2925 check_ops_with_options(options, ops);
2926}
2927
2928#[test]
2929fn close_window_empty_ws_above_first() {
2930 let ops = [
2931 Op::AddWindow {
2932 params: TestWindowParams::new(1),
2933 },
2934 Op::AddOutput(1),
2935 Op::CloseWindow(1),
2936 ];
2937 let options = Options {
2938 layout: niri_config::Layout {
2939 empty_workspace_above_first: true,
2940 ..Default::default()
2941 },
2942 ..Default::default()
2943 };
2944 check_ops_with_options(options, ops);
2945}
2946
2947#[test]
2948fn add_and_remove_output() {
2949 let ops = [
2950 Op::AddOutput(2),
2951 Op::AddOutput(1),
2952 Op::AddWindow {
2953 params: TestWindowParams::new(1),
2954 },
2955 Op::RemoveOutput(2),
2956 ];
2957 let options = Options {
2958 layout: niri_config::Layout {
2959 empty_workspace_above_first: true,
2960 ..Default::default()
2961 },
2962 ..Default::default()
2963 };
2964 check_ops_with_options(options, ops);
2965}
2966
2967#[test]
2968fn switch_ewaf_on() {
2969 let ops = [
2970 Op::AddOutput(1),
2971 Op::AddWindow {
2972 params: TestWindowParams::new(1),
2973 },
2974 ];
2975
2976 let mut layout = check_ops(ops);
2977 layout.update_options(Options {
2978 layout: niri_config::Layout {
2979 empty_workspace_above_first: true,
2980 ..Default::default()
2981 },
2982 ..Default::default()
2983 });
2984 layout.verify_invariants();
2985}
2986
2987#[test]
2988fn switch_ewaf_off() {
2989 let ops = [
2990 Op::AddOutput(1),
2991 Op::AddWindow {
2992 params: TestWindowParams::new(1),
2993 },
2994 ];
2995
2996 let options = Options {
2997 layout: niri_config::Layout {
2998 empty_workspace_above_first: true,
2999 ..Default::default()
3000 },
3001 ..Default::default()
3002 };
3003 let mut layout = check_ops_with_options(options, ops);
3004 layout.update_options(Options::default());
3005 layout.verify_invariants();
3006}
3007
3008#[test]
3009fn interactive_move_drop_on_other_output_during_animation() {
3010 let ops = [
3011 Op::AddOutput(3),
3012 Op::AddWindow {
3013 params: TestWindowParams::new(3),
3014 },
3015 Op::InteractiveMoveBegin {
3016 window: 3,
3017 output_idx: 3,
3018 px: 0.0,
3019 py: 0.0,
3020 },
3021 Op::FocusWorkspaceDown,
3022 Op::AddOutput(4),
3023 Op::InteractiveMoveUpdate {
3024 window: 3,
3025 dx: 0.0,
3026 dy: 8300.68619826683,
3027 output_idx: 4,
3028 px: 0.0,
3029 py: 0.0,
3030 },
3031 Op::RemoveOutput(4),
3032 Op::InteractiveMoveEnd { window: 3 },
3033 ];
3034 check_ops(ops);
3035}
3036
3037#[test]
3038fn add_window_next_to_only_interactively_moved_without_outputs() {
3039 let ops = [
3040 Op::AddWindow {
3041 params: TestWindowParams::new(2),
3042 },
3043 Op::AddOutput(1),
3044 Op::InteractiveMoveBegin {
3045 window: 2,
3046 output_idx: 1,
3047 px: 0.0,
3048 py: 0.0,
3049 },
3050 Op::InteractiveMoveUpdate {
3051 window: 2,
3052 dx: 0.0,
3053 dy: 3586.692842955048,
3054 output_idx: 1,
3055 px: 0.0,
3056 py: 0.0,
3057 },
3058 Op::RemoveOutput(1),
3059 // We have no outputs, and the only existing window is interactively moved, meaning there
3060 // are no workspaces either.
3061 Op::AddWindowNextTo {
3062 params: TestWindowParams::new(3),
3063 next_to_id: 2,
3064 },
3065 ];
3066
3067 check_ops(ops);
3068}
3069
3070#[test]
3071fn interactive_move_toggle_floating_ends_dnd_gesture() {
3072 let ops = [
3073 Op::AddOutput(1),
3074 Op::AddWindow {
3075 params: TestWindowParams::new(2),
3076 },
3077 Op::InteractiveMoveBegin {
3078 window: 2,
3079 output_idx: 1,
3080 px: 0.0,
3081 py: 0.0,
3082 },
3083 Op::InteractiveMoveUpdate {
3084 window: 2,
3085 dx: 0.0,
3086 dy: 3586.692842955048,
3087 output_idx: 1,
3088 px: 0.0,
3089 py: 0.0,
3090 },
3091 Op::Refresh { is_active: false },
3092 Op::ToggleWindowFloating { id: None },
3093 Op::InteractiveMoveEnd { window: 2 },
3094 ];
3095
3096 check_ops(ops);
3097}
3098
3099#[test]
3100fn interactive_move_from_workspace_with_layout_config() {
3101 let ops = [
3102 Op::AddNamedWorkspace {
3103 ws_name: 1,
3104 output_names: vec![2],
3105 layout_config: Some(Box::new(niri_config::LayoutPart {
3106 border: Some(niri_config::BorderRule {
3107 on: true,
3108 ..Default::default()
3109 }),
3110 ..Default::default()
3111 })),
3112 },
3113 Op::AddOutput(1),
3114 Op::AddWindow {
3115 params: TestWindowParams::new(2),
3116 },
3117 Op::InteractiveMoveBegin {
3118 window: 2,
3119 output_idx: 1,
3120 px: 0.0,
3121 py: 0.0,
3122 },
3123 Op::InteractiveMoveUpdate {
3124 window: 2,
3125 dx: 0.0,
3126 dy: 3586.692842955048,
3127 output_idx: 1,
3128 px: 0.0,
3129 py: 0.0,
3130 },
3131 // Now remove and add the output. It will have the same workspace.
3132 Op::RemoveOutput(1),
3133 Op::AddOutput(1),
3134 Op::InteractiveMoveUpdate {
3135 window: 2,
3136 dx: 0.0,
3137 dy: 0.0,
3138 output_idx: 1,
3139 px: 0.0,
3140 py: 0.0,
3141 },
3142 // Now move onto a different workspace.
3143 Op::FocusWorkspaceDown,
3144 Op::CompleteAnimations,
3145 Op::InteractiveMoveUpdate {
3146 window: 2,
3147 dx: 0.0,
3148 dy: 0.0,
3149 output_idx: 1,
3150 px: 0.0,
3151 py: 0.0,
3152 },
3153 ];
3154
3155 check_ops(ops);
3156}
3157
3158#[test]
3159fn set_width_fixed_negative() {
3160 let ops = [
3161 Op::AddOutput(3),
3162 Op::AddWindow {
3163 params: TestWindowParams::new(3),
3164 },
3165 Op::ToggleWindowFloating { id: Some(3) },
3166 Op::SetColumnWidth(SizeChange::SetFixed(-100)),
3167 ];
3168 check_ops(ops);
3169}
3170
3171#[test]
3172fn set_height_fixed_negative() {
3173 let ops = [
3174 Op::AddOutput(3),
3175 Op::AddWindow {
3176 params: TestWindowParams::new(3),
3177 },
3178 Op::ToggleWindowFloating { id: Some(3) },
3179 Op::SetWindowHeight {
3180 id: None,
3181 change: SizeChange::SetFixed(-100),
3182 },
3183 ];
3184 check_ops(ops);
3185}
3186
3187#[test]
3188fn interactive_resize_to_negative() {
3189 let ops = [
3190 Op::AddOutput(3),
3191 Op::AddWindow {
3192 params: TestWindowParams::new(3),
3193 },
3194 Op::ToggleWindowFloating { id: Some(3) },
3195 Op::InteractiveResizeBegin {
3196 window: 3,
3197 edges: ResizeEdge::BOTTOM_RIGHT,
3198 },
3199 Op::InteractiveResizeUpdate {
3200 window: 3,
3201 dx: -10000.,
3202 dy: -10000.,
3203 },
3204 ];
3205 check_ops(ops);
3206}
3207
3208#[test]
3209fn windows_on_other_workspaces_remain_activated() {
3210 let ops = [
3211 Op::AddOutput(3),
3212 Op::AddWindow {
3213 params: TestWindowParams::new(3),
3214 },
3215 Op::FocusWorkspaceDown,
3216 Op::Refresh { is_active: true },
3217 ];
3218
3219 let layout = check_ops(ops);
3220 let (_, win) = layout.windows().next().unwrap();
3221 assert!(win.0.pending_activated.get());
3222}
3223
3224#[test]
3225fn stacking_add_parent_brings_up_child() {
3226 let ops = [
3227 Op::AddOutput(0),
3228 Op::AddWindow {
3229 params: TestWindowParams {
3230 is_floating: true,
3231 parent_id: Some(1),
3232 ..TestWindowParams::new(0)
3233 },
3234 },
3235 Op::AddWindow {
3236 params: TestWindowParams {
3237 is_floating: true,
3238 ..TestWindowParams::new(1)
3239 },
3240 },
3241 ];
3242
3243 check_ops(ops);
3244}
3245
3246#[test]
3247fn stacking_add_parent_brings_up_descendants() {
3248 let ops = [
3249 Op::AddOutput(0),
3250 Op::AddWindow {
3251 params: TestWindowParams {
3252 is_floating: true,
3253 parent_id: Some(2),
3254 ..TestWindowParams::new(0)
3255 },
3256 },
3257 Op::AddWindow {
3258 params: TestWindowParams {
3259 is_floating: true,
3260 parent_id: Some(0),
3261 ..TestWindowParams::new(1)
3262 },
3263 },
3264 Op::AddWindow {
3265 params: TestWindowParams {
3266 is_floating: true,
3267 ..TestWindowParams::new(2)
3268 },
3269 },
3270 ];
3271
3272 check_ops(ops);
3273}
3274
3275#[test]
3276fn stacking_activate_brings_up_descendants() {
3277 let ops = [
3278 Op::AddOutput(0),
3279 Op::AddWindow {
3280 params: TestWindowParams {
3281 is_floating: true,
3282 ..TestWindowParams::new(0)
3283 },
3284 },
3285 Op::AddWindow {
3286 params: TestWindowParams {
3287 is_floating: true,
3288 parent_id: Some(0),
3289 ..TestWindowParams::new(1)
3290 },
3291 },
3292 Op::AddWindow {
3293 params: TestWindowParams {
3294 is_floating: true,
3295 parent_id: Some(1),
3296 ..TestWindowParams::new(2)
3297 },
3298 },
3299 Op::AddWindow {
3300 params: TestWindowParams {
3301 is_floating: true,
3302 ..TestWindowParams::new(3)
3303 },
3304 },
3305 Op::FocusWindow(0),
3306 ];
3307
3308 check_ops(ops);
3309}
3310
3311#[test]
3312fn stacking_set_parent_brings_up_child() {
3313 let ops = [
3314 Op::AddOutput(0),
3315 Op::AddWindow {
3316 params: TestWindowParams {
3317 is_floating: true,
3318 ..TestWindowParams::new(0)
3319 },
3320 },
3321 Op::AddWindow {
3322 params: TestWindowParams {
3323 is_floating: true,
3324 ..TestWindowParams::new(1)
3325 },
3326 },
3327 Op::SetParent {
3328 id: 0,
3329 new_parent_id: Some(1),
3330 },
3331 ];
3332
3333 check_ops(ops);
3334}
3335
3336#[test]
3337fn move_window_to_workspace_with_different_active_output() {
3338 let ops = [
3339 Op::AddOutput(0),
3340 Op::AddOutput(1),
3341 Op::AddWindow {
3342 params: TestWindowParams::new(0),
3343 },
3344 Op::FocusOutput(1),
3345 Op::MoveWindowToWorkspace {
3346 window_id: Some(0),
3347 workspace_idx: 2,
3348 },
3349 ];
3350
3351 check_ops(ops);
3352}
3353
3354#[test]
3355fn set_first_workspace_name() {
3356 let ops = [
3357 Op::AddOutput(0),
3358 Op::SetWorkspaceName {
3359 new_ws_name: 0,
3360 ws_name: None,
3361 },
3362 ];
3363
3364 check_ops(ops);
3365}
3366
3367#[test]
3368fn set_first_workspace_name_ewaf() {
3369 let ops = [
3370 Op::AddOutput(0),
3371 Op::SetWorkspaceName {
3372 new_ws_name: 0,
3373 ws_name: None,
3374 },
3375 ];
3376
3377 let options = Options {
3378 layout: niri_config::Layout {
3379 empty_workspace_above_first: true,
3380 ..Default::default()
3381 },
3382 ..Default::default()
3383 };
3384 check_ops_with_options(options, ops);
3385}
3386
3387#[test]
3388fn set_last_workspace_name() {
3389 let ops = [
3390 Op::AddOutput(0),
3391 Op::AddWindow {
3392 params: TestWindowParams::new(0),
3393 },
3394 Op::FocusWorkspaceDown,
3395 Op::SetWorkspaceName {
3396 new_ws_name: 0,
3397 ws_name: None,
3398 },
3399 ];
3400
3401 check_ops(ops);
3402}
3403
3404#[test]
3405fn move_workspace_to_same_monitor_doesnt_reorder() {
3406 let ops = [
3407 Op::AddOutput(0),
3408 Op::SetWorkspaceName {
3409 new_ws_name: 0,
3410 ws_name: None,
3411 },
3412 Op::AddWindow {
3413 params: TestWindowParams::new(0),
3414 },
3415 Op::FocusWorkspaceDown,
3416 Op::AddWindow {
3417 params: TestWindowParams::new(1),
3418 },
3419 Op::AddWindow {
3420 params: TestWindowParams::new(2),
3421 },
3422 Op::MoveWorkspaceToMonitor {
3423 ws_name: Some(0),
3424 output_id: 0,
3425 },
3426 ];
3427
3428 let layout = check_ops(ops);
3429 let counts: Vec<_> = layout
3430 .workspaces()
3431 .map(|(_, _, ws)| ws.windows().count())
3432 .collect();
3433 assert_eq!(counts, &[1, 2, 0]);
3434}
3435
3436#[test]
3437fn removing_window_above_preserves_focused_window() {
3438 let ops = [
3439 Op::AddOutput(0),
3440 Op::AddWindow {
3441 params: TestWindowParams::new(0),
3442 },
3443 Op::AddWindow {
3444 params: TestWindowParams::new(1),
3445 },
3446 Op::AddWindow {
3447 params: TestWindowParams::new(2),
3448 },
3449 Op::FocusColumnFirst,
3450 Op::ConsumeWindowIntoColumn,
3451 Op::ConsumeWindowIntoColumn,
3452 Op::FocusWindowDown,
3453 Op::CloseWindow(0),
3454 ];
3455
3456 let layout = check_ops(ops);
3457 let win = layout.focus().unwrap();
3458 assert_eq!(win.0.id, 1);
3459}
3460
3461#[test]
3462fn preset_column_width_fixed_correct_with_border() {
3463 let ops = [
3464 Op::AddOutput(0),
3465 Op::AddWindow {
3466 params: TestWindowParams::new(0),
3467 },
3468 Op::SwitchPresetColumnWidth,
3469 ];
3470
3471 let options = Options {
3472 layout: niri_config::Layout {
3473 preset_column_widths: vec![PresetSize::Fixed(500)],
3474 ..Default::default()
3475 },
3476 ..Default::default()
3477 };
3478 let mut layout = check_ops_with_options(options, ops);
3479
3480 let win = layout.windows().next().unwrap().1;
3481 assert_eq!(win.requested_size().unwrap().w, 500);
3482
3483 // Add border.
3484 let options = Options {
3485 layout: niri_config::Layout {
3486 preset_column_widths: vec![PresetSize::Fixed(500)],
3487 border: niri_config::Border {
3488 off: false,
3489 width: 5.,
3490 ..Default::default()
3491 },
3492 ..Default::default()
3493 },
3494 ..Default::default()
3495 };
3496 layout.update_options(options);
3497
3498 // With border, the window gets less size.
3499 let win = layout.windows().next().unwrap().1;
3500 assert_eq!(win.requested_size().unwrap().w, 490);
3501
3502 // However, preset fixed width will still work correctly.
3503 layout.toggle_width(true);
3504 let win = layout.windows().next().unwrap().1;
3505 assert_eq!(win.requested_size().unwrap().w, 500);
3506}
3507
3508#[test]
3509fn preset_column_width_reset_after_set_width() {
3510 let ops = [
3511 Op::AddOutput(0),
3512 Op::AddWindow {
3513 params: TestWindowParams::new(0),
3514 },
3515 Op::SwitchPresetColumnWidth,
3516 Op::SetWindowWidth {
3517 id: None,
3518 change: SizeChange::AdjustFixed(-10),
3519 },
3520 Op::SwitchPresetColumnWidth,
3521 ];
3522
3523 let options = Options {
3524 layout: niri_config::Layout {
3525 preset_column_widths: vec![PresetSize::Fixed(500), PresetSize::Fixed(1000)],
3526 ..Default::default()
3527 },
3528 ..Default::default()
3529 };
3530 let layout = check_ops_with_options(options, ops);
3531 let win = layout.windows().next().unwrap().1;
3532 assert_eq!(win.requested_size().unwrap().w, 500);
3533}
3534
3535#[test]
3536fn move_column_to_workspace_unfocused_with_multiple_monitors() {
3537 let ops = [
3538 Op::AddOutput(1),
3539 Op::SetWorkspaceName {
3540 new_ws_name: 101,
3541 ws_name: None,
3542 },
3543 Op::AddWindow {
3544 params: TestWindowParams::new(1),
3545 },
3546 Op::FocusWorkspaceDown,
3547 Op::SetWorkspaceName {
3548 new_ws_name: 102,
3549 ws_name: None,
3550 },
3551 Op::AddWindow {
3552 params: TestWindowParams::new(2),
3553 },
3554 Op::AddOutput(2),
3555 Op::FocusOutput(2),
3556 Op::SetWorkspaceName {
3557 new_ws_name: 201,
3558 ws_name: None,
3559 },
3560 Op::AddWindow {
3561 params: TestWindowParams::new(3),
3562 },
3563 Op::AddWindow {
3564 params: TestWindowParams::new(4),
3565 },
3566 Op::MoveColumnToOutput {
3567 output_id: 1,
3568 target_ws_idx: Some(0),
3569 activate: false,
3570 },
3571 Op::FocusOutput(1),
3572 ];
3573
3574 let layout = check_ops(ops);
3575
3576 assert_eq!(layout.active_workspace().unwrap().name().unwrap(), "ws102");
3577
3578 for (mon, win) in layout.windows() {
3579 let mon = mon.unwrap();
3580 let ws = mon
3581 .workspaces
3582 .iter()
3583 .find(|w| w.has_window(win.id()))
3584 .unwrap();
3585
3586 assert_eq!(
3587 ws.name().unwrap(),
3588 match win.id() {
3589 1 | 4 => "ws101",
3590 2 => "ws102",
3591 3 => "ws201",
3592 _ => unreachable!(),
3593 }
3594 );
3595 }
3596}
3597
3598#[test]
3599fn move_column_to_workspace_down_focus_false_on_floating_window() {
3600 let ops = [
3601 Op::AddOutput(1),
3602 Op::AddWindow {
3603 params: TestWindowParams::new(1),
3604 },
3605 Op::AddWindow {
3606 params: TestWindowParams::new(2),
3607 },
3608 Op::ToggleWindowFloating { id: None },
3609 Op::MoveColumnToWorkspaceDown(false),
3610 ];
3611
3612 let layout = check_ops(ops);
3613
3614 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3615 unreachable!()
3616 };
3617
3618 assert_eq!(monitors[0].active_workspace_idx, 0);
3619}
3620
3621#[test]
3622fn move_column_to_workspace_focus_false_on_floating_window() {
3623 let ops = [
3624 Op::AddOutput(1),
3625 Op::AddWindow {
3626 params: TestWindowParams::new(1),
3627 },
3628 Op::AddWindow {
3629 params: TestWindowParams::new(2),
3630 },
3631 Op::ToggleWindowFloating { id: None },
3632 Op::MoveColumnToWorkspace(1, false),
3633 ];
3634
3635 let layout = check_ops(ops);
3636
3637 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3638 unreachable!()
3639 };
3640
3641 assert_eq!(monitors[0].active_workspace_idx, 0);
3642}
3643
3644#[test]
3645fn restore_to_floating_persists_across_fullscreen_maximize() {
3646 let ops = [
3647 Op::AddOutput(1),
3648 Op::AddWindow {
3649 params: TestWindowParams::new(1),
3650 },
3651 Op::ToggleWindowFloating { id: None },
3652 // Maximize then fullscreen.
3653 Op::MaximizeWindowToEdges { id: None },
3654 Op::FullscreenWindow(1),
3655 // Unfullscreen.
3656 Op::FullscreenWindow(1),
3657 ];
3658
3659 let mut layout = check_ops(ops);
3660
3661 // Unfullscreening should return the window to the maximized state.
3662 let scrolling = layout.active_workspace().unwrap().scrolling();
3663 assert!(scrolling.tiles().next().is_some());
3664
3665 let ops = [
3666 // Unmaximize.
3667 Op::MaximizeWindowToEdges { id: None },
3668 ];
3669 check_ops_on_layout(&mut layout, ops);
3670
3671 // Unmaximize should return the window back to floating.
3672 let scrolling = layout.active_workspace().unwrap().scrolling();
3673 assert!(scrolling.tiles().next().is_none());
3674}
3675
3676#[test]
3677fn unmaximize_during_fullscreen_does_not_float() {
3678 let ops = [
3679 Op::AddOutput(1),
3680 Op::AddWindow {
3681 params: TestWindowParams::new(1),
3682 },
3683 Op::ToggleWindowFloating { id: None },
3684 // Maximize then fullscreen.
3685 Op::MaximizeWindowToEdges { id: None },
3686 Op::FullscreenWindow(1),
3687 // Unmaximize.
3688 Op::MaximizeWindowToEdges { id: None },
3689 ];
3690
3691 let mut layout = check_ops(ops);
3692
3693 // Unmaximize shouldn't have changed the window state since it's fullscreen.
3694 let scrolling = layout.active_workspace().unwrap().scrolling();
3695 assert!(scrolling.tiles().next().is_some());
3696
3697 let ops = [
3698 // Unfullscreen.
3699 Op::FullscreenWindow(1),
3700 ];
3701 check_ops_on_layout(&mut layout, ops);
3702
3703 // Unfullscreen should return the window back to floating.
3704 let scrolling = layout.active_workspace().unwrap().scrolling();
3705 assert!(scrolling.tiles().next().is_none());
3706}
3707
3708#[test]
3709fn move_column_to_workspace_maximize_and_fullscreen() {
3710 let ops = [
3711 Op::AddOutput(1),
3712 Op::AddWindow {
3713 params: TestWindowParams::new(1),
3714 },
3715 Op::MaximizeWindowToEdges { id: None },
3716 Op::FullscreenWindow(1),
3717 Op::MoveColumnToWorkspaceDown(true),
3718 Op::FullscreenWindow(1),
3719 ];
3720
3721 let layout = check_ops(ops);
3722 let (_, win) = layout.windows().next().unwrap();
3723
3724 // Unfullscreening should return to maximized because the window was maximized before.
3725 assert_eq!(win.pending_sizing_mode(), SizingMode::Maximized);
3726}
3727
3728#[test]
3729fn move_window_to_workspace_maximize_and_fullscreen() {
3730 let ops = [
3731 Op::AddOutput(1),
3732 Op::AddWindow {
3733 params: TestWindowParams::new(1),
3734 },
3735 Op::MaximizeWindowToEdges { id: None },
3736 Op::FullscreenWindow(1),
3737 Op::MoveWindowToWorkspaceDown(true),
3738 Op::FullscreenWindow(1),
3739 ];
3740
3741 let layout = check_ops(ops);
3742 let (_, win) = layout.windows().next().unwrap();
3743
3744 // Unfullscreening should return to maximized because the window was maximized before.
3745 //
3746 // FIXME: it currently doesn't because windows themselves can only be either fullscreen or
3747 // maximized. So when a window is fullscreen, whether it is also maximized or not is stored in
3748 // the column. MoveWindowToWorkspace removes the window from the column and this information is
3749 // forgotten.
3750 assert_eq!(win.pending_sizing_mode(), SizingMode::Normal);
3751}
3752
3753#[test]
3754fn tabs_with_different_border() {
3755 let ops = [
3756 Op::AddOutput(1),
3757 Op::AddWindow {
3758 params: TestWindowParams {
3759 rules: Some(ResolvedWindowRules {
3760 border: niri_config::BorderRule {
3761 on: true,
3762 ..Default::default()
3763 },
3764 ..ResolvedWindowRules::default()
3765 }),
3766 ..TestWindowParams::new(2)
3767 },
3768 },
3769 Op::SwitchPresetWindowHeight { id: None },
3770 Op::ToggleColumnTabbedDisplay,
3771 Op::AddWindow {
3772 params: TestWindowParams::new(3),
3773 },
3774 Op::ConsumeOrExpelWindowLeft { id: None },
3775 ];
3776
3777 let options = Options {
3778 layout: niri_config::Layout {
3779 struts: Struts {
3780 left: FloatOrInt(0.),
3781 right: FloatOrInt(0.),
3782 top: FloatOrInt(20000.),
3783 bottom: FloatOrInt(0.),
3784 },
3785 ..Default::default()
3786 },
3787 ..Default::default()
3788 };
3789 check_ops_with_options(options, ops);
3790}
3791
3792#[test]
3793fn expel_pending_left_from_fullscreen_tabbed_column() {
3794 let ops = [
3795 Op::AddOutput(1),
3796 Op::AddWindow {
3797 params: TestWindowParams::new(1),
3798 },
3799 Op::FullscreenWindow(1),
3800 Op::Communicate(1),
3801 // 1 is now fullscreen, view_offset_to_restore is set.
3802 Op::ToggleColumnTabbedDisplay,
3803 Op::AddWindow {
3804 params: TestWindowParams::new(2),
3805 },
3806 Op::ConsumeOrExpelWindowLeft { id: Some(2) },
3807 // 2 is consumed into a fullscreen column, fullscreen is requested but not applied.
3808 //
3809 // Now, get it back out while keeping it focused.
3810 //
3811 // Importantly, we expel it *left*, which results in adding a new column with the exact
3812 // same active_column_idx.
3813 Op::FocusWindow(2),
3814 Op::ConsumeOrExpelWindowLeft { id: None },
3815 ];
3816
3817 check_ops(ops);
3818}
3819
3820#[test]
3821fn workspace_render_geo_at_fractional_scale() {
3822 let ops = [
3823 Op::AddScaledOutput {
3824 id: 1,
3825 scale: 1.1,
3826 layout_config: None,
3827 },
3828 Op::AddWindow {
3829 params: TestWindowParams::new(1),
3830 },
3831 Op::FocusWorkspaceDown,
3832 Op::CompleteAnimations,
3833 ];
3834
3835 let layout = check_ops(ops);
3836
3837 let MonitorSet::Normal { monitors, .. } = &layout.monitor_set else {
3838 unreachable!()
3839 };
3840
3841 let mon = &monitors[0];
3842 let mut iter = mon.workspaces_with_render_geo();
3843 let (_ws, geo) = iter.next().unwrap();
3844 assert!(
3845 iter.next().is_none(),
3846 "animations are completed, only one workspace should be visible"
3847 );
3848 assert_eq!(
3849 geo.loc.y, 0.,
3850 "active workspace must be at y = 0 exactly, \
3851 otherwise a pointer against the screen edge at y = 0 won't hit it"
3852 );
3853}
3854
3855fn parent_id_causes_loop(layout: &Layout<TestWindow>, id: usize, mut parent_id: usize) -> bool {
3856 if parent_id == id {
3857 return true;
3858 }
3859
3860 'outer: loop {
3861 for (_, win) in layout.windows() {
3862 if win.0.id == parent_id {
3863 match win.0.parent_id.get() {
3864 Some(new_parent_id) => {
3865 if new_parent_id == id {
3866 // Found a loop.
3867 return true;
3868 }
3869
3870 parent_id = new_parent_id;
3871 continue 'outer;
3872 }
3873 // Reached window with no parent.
3874 None => return false,
3875 }
3876 }
3877 }
3878
3879 // Parent is not in the layout.
3880 return false;
3881 }
3882}
3883
3884fn arbitrary_spacing() -> impl Strategy<Value = f64> {
3885 // Give equal weight to:
3886 // - 0: the element is disabled
3887 // - 4: some reasonable value
3888 // - random value, likely unreasonably big
3889 prop_oneof![Just(0.), Just(4.), ((1.)..=65535.)]
3890}
3891
3892fn arbitrary_spacing_neg() -> impl Strategy<Value = f64> {
3893 // Give equal weight to:
3894 // - 0: the element is disabled
3895 // - 4: some reasonable value
3896 // - -4: some reasonable negative value
3897 // - random value, likely unreasonably big
3898 prop_oneof![Just(0.), Just(4.), Just(-4.), ((1.)..=65535.)]
3899}
3900
3901fn arbitrary_struts() -> impl Strategy<Value = Struts> {
3902 (
3903 arbitrary_spacing_neg(),
3904 arbitrary_spacing_neg(),
3905 arbitrary_spacing_neg(),
3906 arbitrary_spacing_neg(),
3907 )
3908 .prop_map(|(left, right, top, bottom)| Struts {
3909 left: FloatOrInt(left),
3910 right: FloatOrInt(right),
3911 top: FloatOrInt(top),
3912 bottom: FloatOrInt(bottom),
3913 })
3914}
3915
3916fn arbitrary_center_focused_column() -> impl Strategy<Value = CenterFocusedColumn> {
3917 prop_oneof![
3918 Just(CenterFocusedColumn::Never),
3919 Just(CenterFocusedColumn::OnOverflow),
3920 Just(CenterFocusedColumn::Always),
3921 ]
3922}
3923
3924fn arbitrary_tab_indicator_position() -> impl Strategy<Value = TabIndicatorPosition> {
3925 prop_oneof![
3926 Just(TabIndicatorPosition::Left),
3927 Just(TabIndicatorPosition::Right),
3928 Just(TabIndicatorPosition::Top),
3929 Just(TabIndicatorPosition::Bottom),
3930 ]
3931}
3932
3933prop_compose! {
3934 fn arbitrary_focus_ring()(
3935 off in any::<bool>(),
3936 width in prop::option::of(arbitrary_spacing().prop_map(FloatOrInt)),
3937 ) -> niri_config::BorderRule {
3938 niri_config::BorderRule {
3939 off,
3940 on: !off,
3941 width,
3942 ..Default::default()
3943 }
3944 }
3945}
3946
3947prop_compose! {
3948 fn arbitrary_border()(
3949 off in any::<bool>(),
3950 width in prop::option::of(arbitrary_spacing().prop_map(FloatOrInt)),
3951 ) -> niri_config::BorderRule {
3952 niri_config::BorderRule {
3953 off,
3954 on: !off,
3955 width,
3956 ..Default::default()
3957 }
3958 }
3959}
3960
3961prop_compose! {
3962 fn arbitrary_shadow()(
3963 off in any::<bool>(),
3964 softness in prop::option::of(arbitrary_spacing().prop_map(FloatOrInt)),
3965 ) -> niri_config::ShadowRule {
3966 niri_config::ShadowRule {
3967 off,
3968 on: !off,
3969 softness,
3970 ..Default::default()
3971 }
3972 }
3973}
3974
3975prop_compose! {
3976 fn arbitrary_tab_indicator()(
3977 off in any::<bool>(),
3978 hide_when_single_tab in prop::option::of(any::<bool>().prop_map(Flag)),
3979 place_within_column in prop::option::of(any::<bool>().prop_map(Flag)),
3980 width in prop::option::of(arbitrary_spacing().prop_map(FloatOrInt)),
3981 gap in prop::option::of(arbitrary_spacing_neg().prop_map(FloatOrInt)),
3982 length in prop::option::of((0f64..2f64)
3983 .prop_map(|x| TabIndicatorLength { total_proportion: Some(x) })),
3984 position in prop::option::of(arbitrary_tab_indicator_position()),
3985 ) -> niri_config::TabIndicatorPart {
3986 niri_config::TabIndicatorPart {
3987 off,
3988 on: !off,
3989 hide_when_single_tab,
3990 place_within_column,
3991 width,
3992 gap,
3993 length,
3994 position,
3995 ..Default::default()
3996 }
3997 }
3998}
3999
4000prop_compose! {
4001 fn arbitrary_layout_part()(
4002 gaps in prop::option::of(arbitrary_spacing().prop_map(FloatOrInt)),
4003 struts in prop::option::of(arbitrary_struts()),
4004 focus_ring in prop::option::of(arbitrary_focus_ring()),
4005 border in prop::option::of(arbitrary_border()),
4006 shadow in prop::option::of(arbitrary_shadow()),
4007 tab_indicator in prop::option::of(arbitrary_tab_indicator()),
4008 center_focused_column in prop::option::of(arbitrary_center_focused_column()),
4009 always_center_single_column in prop::option::of(any::<bool>().prop_map(Flag)),
4010 empty_workspace_above_first in prop::option::of(any::<bool>().prop_map(Flag)),
4011 ) -> niri_config::LayoutPart {
4012 niri_config::LayoutPart {
4013 gaps,
4014 struts,
4015 center_focused_column,
4016 always_center_single_column,
4017 empty_workspace_above_first,
4018 focus_ring,
4019 border,
4020 shadow,
4021 tab_indicator,
4022 ..Default::default()
4023 }
4024 }
4025}
4026
4027proptest! {
4028 #![proptest_config(ProptestConfig {
4029 cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() {
4030 eprintln!("ignoring slow test");
4031 0
4032 } else {
4033 ProptestConfig::default().cases
4034 },
4035 ..ProptestConfig::default()
4036 })]
4037
4038 #[test]
4039 fn random_operations_dont_panic(
4040 ops: Vec<Op>,
4041 layout_config in arbitrary_layout_part(),
4042 ) {
4043 // eprintln!("{ops:?}");
4044 let options = Options {
4045 layout: niri_config::Layout::from_part(&layout_config),
4046 ..Default::default()
4047 };
4048
4049 check_ops_with_options(options, ops);
4050 }
4051}