forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBad3847fIvan Molodetskikh1 year ago2457 commits

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