forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

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