forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBea3bbeaIvan Molodetskikh1 year ago2422 commits

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