2 # vim:ts=4:sw=4:expandtab
4 # Please read the following documents before working on tests:
5 # • https://build.i3wm.org/docs/testsuite.html
8 # • https://build.i3wm.org/docs/lib-i3test.html
9 # (alternatively: perldoc ./testcases/lib/i3test.pm)
11 # • https://build.i3wm.org/docs/ipc.html
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 # (unless you are already familiar with Perl)
17 # Tests the swap command.
19 use i3test i3_config => <<EOT;
21 font font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
23 for_window[class="mark_A"] mark A
24 for_window[class="mark_B"] mark B
27 my ($ws, $ws1, $ws2, $ws3);
28 my ($node, $nodes, $expected_focus, $A, $B, $F);
30 my @fullscreen_permutations = ([], ["A"], ["B"], ["A", "B"]);
33 ###############################################################################
34 # Invalid con_id should not crash i3
36 ###############################################################################
38 $ws = fresh_workspace;
41 cmd "swap container with con_id 1";
46 ###############################################################################
47 # Swap 2 windows in different workspaces using con_id
48 ###############################################################################
50 $ws = fresh_workspace;
52 $A = get_focused($ws);
54 $ws = fresh_workspace;
57 cmd "swap container with con_id $A";
58 is(get_focused($ws), $A, 'A is now focused');
62 ###############################################################################
63 # Swap two containers next to each other.
64 # Focus should stay on B because both windows are on the focused workspace.
65 # The focused container is B.
67 # +---+---+ Layout: H1[ A B ]
68 # | A | B | Focus Stacks:
70 ###############################################################################
71 $ws = fresh_workspace;
73 $A = open_window(wm_class => 'mark_A');
74 $B = open_window(wm_class => 'mark_B');
75 $expected_focus = get_focused($ws);
77 cmd '[con_mark=B] swap container with mark A';
79 $nodes = get_ws_content($ws);
80 is($nodes->[0]->{window}, $B->{id}, 'B is on the left');
81 is($nodes->[1]->{window}, $A->{id}, 'A is on the right');
82 is(get_focused($ws), $expected_focus, 'B is still focused');
86 ###############################################################################
87 # Swap two containers with different parents.
88 # In this test, the focus head of the left v-split container is A.
89 # The focused container is B.
91 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
92 # | A | X | Focus Stacks:
93 # +---+---+ H1: V2, V1
96 ###############################################################################
97 $ws = fresh_workspace;
99 $A = open_window(wm_class => 'mark_A');
100 $B = open_window(wm_class => 'mark_B');
103 cmd 'move up, focus left';
106 cmd 'focus up, focus right, focus down';
107 $expected_focus = get_focused($ws);
109 cmd '[con_mark=B] swap container with mark A';
111 $nodes = get_ws_content($ws);
112 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
113 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
114 is(get_focused($ws), $expected_focus, 'B is still focused');
118 ###############################################################################
119 # Swap two containers with different parents.
120 # In this test, the focus head of the left v-split container is _not_ A.
121 # The focused container is B.
123 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
124 # | A | X | Focus Stacks:
125 # +---+---+ H1: V2, V1
128 ###############################################################################
129 $ws = fresh_workspace;
131 $A = open_window(wm_class => 'mark_A');
132 $B = open_window(wm_class => 'mark_B');
135 cmd 'move up, focus left';
138 cmd 'focus right, focus down';
139 $expected_focus = get_focused($ws);
141 cmd '[con_mark=B] swap container with mark A';
143 $nodes = get_ws_content($ws);
144 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
145 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
146 is(get_focused($ws), $expected_focus, 'B is still focused');
150 ###############################################################################
151 # Swap two containers with one being on a different workspace.
152 # The focused container is B.
154 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
158 # +---+---+ Layout: H1[ A X ]
159 # | A | X | Focus Stacks:
162 # +---+---+ Layout: H2[ Y, B ]
163 # | Y | B | Focus Stacks:
165 ###############################################################################
166 for my $fullscreen (@fullscreen_permutations){
167 $ws1 = fresh_workspace;
168 $A = open_window(wm_class => 'mark_A');
169 $expected_focus = get_focused($ws1);
173 $ws2 = fresh_workspace;
175 $B = open_window(wm_class => 'mark_B');
177 my $A_fullscreen = "A" ~~ @$fullscreen || 0;
178 my $B_fullscreen = "B" ~~ @$fullscreen || 0;
179 $A->fullscreen($A_fullscreen);
180 $B->fullscreen($B_fullscreen);
183 cmd '[con_mark=B] swap container with mark A';
185 $nodes = get_ws_content($ws1);
187 is($node->{window}, $B->{id}, 'B is on ws1:left');
188 is_num_fullscreen($ws1, $A_fullscreen, 'amount of fullscreen windows in ws1');
189 is($node->{fullscreen_mode}, $A_fullscreen, 'B got A\'s fullscreen mode');
191 $nodes = get_ws_content($ws2);
193 is($node->{window}, $A->{id}, 'A is on ws2:right');
194 is(get_focused($ws2), $expected_focus, 'A is focused');
195 is_num_fullscreen($ws2, $B_fullscreen, 'amount of fullscreen windows in ws2');
196 is($node->{fullscreen_mode}, $B_fullscreen, 'A got B\'s fullscreen mode');
201 ###############################################################################
202 # Swap a non-fullscreen window with a fullscreen one in different workspaces.
203 # Layout: O1[ W1[ H1 ] W2[ B ] ]
205 # +---+---+ Layout: H1[ A F ]
206 # | A | F | Focus Stacks:
212 ###############################################################################
213 $ws1 = fresh_workspace;
215 $A = open_window(wm_class => 'mark_A');
218 $expected_focus = get_focused($ws1);
220 $ws2 = fresh_workspace;
221 $B = open_window(wm_class => 'mark_B');
225 cmd '[con_mark=B] swap container with mark A';
227 $nodes = get_ws_content($ws1);
228 is($nodes->[0]->{window}, $B->{id}, 'B is on ws1:left');
229 is_num_fullscreen($ws1, 1, 'F still fullscreen in ws1');
230 is(get_focused($ws1), $expected_focus, 'F is still focused');
232 $nodes = get_ws_content($ws2);
233 is($nodes->[0]->{window}, $A->{id}, 'A is on ws1');
235 ###############################################################################
236 # Try a more exotic layout with fullscreen containers.
237 # A and F are fullscreened as a stack of two vertical containers before the
239 # A is swapped with fullscreened window B which is in another workspace.
241 # +---+---+ Layout: H1[ X V1[ A F ] ]
242 # | | A | Focus Stacks:
243 # | X +---+ H1: V1, X
246 ###############################################################################
247 $ws1 = fresh_workspace;
250 $A = open_window(wm_class => 'mark_A');
254 cmd "fullscreen enable";
255 $expected_focus = get_focused($ws1);
257 $ws2 = fresh_workspace;
258 $B = open_window(wm_class => 'mark_B');
262 cmd '[con_mark=B] swap container with mark A';
267 $nodes = get_ws_content($ws1);
268 is($nodes->[1]->{nodes}->[0]->{window}, $B->{id}, 'B is on top right in ws1');
269 is(get_focused($ws1), $expected_focus, 'The container of the stacked windows remains focused in ws1');
270 is_num_fullscreen($ws1, 1, 'Same amount of fullscreen windows in ws1');
272 $nodes = get_ws_content($ws2);
273 is($nodes->[0]->{window}, $A->{id}, 'A is on ws2');
274 is_num_fullscreen($ws2, 1, 'A is in fullscreen mode');
276 ###############################################################################
277 # Swap two non-focused containers within the same workspace.
279 # +---+---+ Layout: H1[ V1[ A X ] V2[ F B ] ]
280 # | A | F | Focus Stacks:
281 # +---+---+ H1: V2, V1
284 ###############################################################################
285 $ws = fresh_workspace;
287 $A = open_window(wm_class => 'mark_A');
288 $B = open_window(wm_class => 'mark_B');
291 cmd 'move up, focus left';
294 cmd 'focus up, focus right';
295 $expected_focus = get_focused($ws);
297 cmd '[con_mark=B] swap container with mark A';
299 $nodes = get_ws_content($ws);
300 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
301 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
302 is(get_focused($ws), $expected_focus, 'F is still focused');
306 ###############################################################################
307 # Swap two non-focused containers which are both on different workspaces.
309 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
324 ###############################################################################
325 for my $fullscreen (@fullscreen_permutations){
326 $ws1 = fresh_workspace;
327 $A = open_window(wm_class => 'mark_A');
329 $ws2 = fresh_workspace;
330 $B = open_window(wm_class => 'mark_B');
332 $ws3 = fresh_workspace;
334 $expected_focus = get_focused($ws3);
336 my $A_fullscreen = "A" ~~ @$fullscreen || 0;
337 my $B_fullscreen = "B" ~~ @$fullscreen || 0;
338 $A->fullscreen($A_fullscreen);
339 $B->fullscreen($B_fullscreen);
342 cmd '[con_mark=B] swap container with mark A';
344 $nodes = get_ws_content($ws1);
346 is($node->{window}, $B->{id}, 'B is on the first workspace');
347 is_num_fullscreen($ws1, $A_fullscreen, 'amount of fullscreen windows in ws1');
348 is($node->{fullscreen_mode}, $A_fullscreen, 'B got A\'s fullscreen mode');
350 $nodes = get_ws_content($ws2);
352 is($node->{window}, $A->{id}, 'A is on the second workspace');
353 is_num_fullscreen($ws2, $B_fullscreen, 'amount of fullscreen windows in ws2');
354 is($node->{fullscreen_mode}, $B_fullscreen, 'A got B\'s fullscreen mode');
356 is(get_focused($ws3), $expected_focus, 'F is still focused');
361 ###############################################################################
362 # Swap two non-focused containers with one being on a different workspace.
364 # Layout: O1[ W1[ A ] W2[ H2 ] ]
372 # +---+---+ Layout: H2[ B, F ]
373 # | B | F | Focus Stacks:
377 ###############################################################################
379 for my $fullscreen (0..1){
380 $ws1 = fresh_workspace;
381 $A = open_window(wm_class => 'mark_A');
383 $ws2 = fresh_workspace;
384 $B = open_window(wm_class => 'mark_B');
386 cmd 'fullscreen enable' if $fullscreen;
387 $expected_focus = get_focused($ws2);
389 cmd '[con_mark=B] swap container with mark A';
391 $nodes = get_ws_content($ws1);
392 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
394 $nodes = get_ws_content($ws2);
395 is($nodes->[0]->{window}, $A->{id}, 'A is on the left of the second workspace');
396 is(get_focused($ws2), $expected_focus, 'F is still focused');
401 ###############################################################################
402 # 1. A container cannot be swapped with its parent.
403 # 2. A container cannot be swapped with one of its children.
406 # +---+---+ Layout: H1[ X V1[ Y B ] ]
407 # | | Y | (with A := V1)
411 ###############################################################################
412 $ws = fresh_workspace;
416 $B = open_window(wm_class => 'mark_B');
417 cmd 'focus parent, mark A, focus child';
419 $result = cmd '[con_mark=B] swap container with mark A';
420 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
422 $result = cmd '[con_mark=A] swap container with mark B';
423 is($result->[0]->{success}, 0, 'A cannot be swappd with one of its children');
427 ###############################################################################
428 # Swapping two containers preserves the geometry of the container they are
429 # being swapped with.
440 ###############################################################################
441 $ws = fresh_workspace;
442 $A = open_window(wm_class => 'mark_A');
443 $B = open_window(wm_class => 'mark_B');
444 cmd 'resize grow width 0 or 25 ppt';
447 $nodes = get_ws_content($ws);
448 cmp_float($nodes->[0]->{percent}, 0.25, 'A has 25% width');
449 cmp_float($nodes->[1]->{percent}, 0.75, 'B has 75% width');
451 cmd '[con_mark=B] swap container with mark A';
453 $nodes = get_ws_content($ws);
454 cmp_float($nodes->[0]->{percent}, 0.25, 'B has 25% width');
455 cmp_float($nodes->[1]->{percent}, 0.75, 'A has 75% width');
459 ###############################################################################
460 # Swapping containers not sharing the same parent preserves the geometry of
461 # the container they are swapped with.
480 ###############################################################################
481 $ws = fresh_workspace;
483 $A = open_window(wm_class => 'mark_A');
484 $B = open_window(wm_class => 'mark_B');
487 cmd 'focus up, resize grow height 0 or 25 ppt';
488 cmd 'focus left, split v';
490 cmd 'resize grow height 0 or 25 ppt';
493 $nodes = get_ws_content($ws);
494 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'A has 25% height');
495 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'B has 75% height');
497 cmd '[con_mark=B] swap container with mark A';
499 $nodes = get_ws_content($ws);
500 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'B has 25% height');
501 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'A has 75% height');
505 ###############################################################################
506 # Swapping containers moves the urgency hint correctly.
507 ###############################################################################
509 $ws1 = fresh_workspace;
510 $A = open_window(wm_class => 'mark_A');
511 $ws2 = fresh_workspace;
512 $B = open_window(wm_class => 'mark_B');
515 $B->add_hint('urgency');
518 cmd '[con_mark=B] swap container with mark A';
520 @urgent = grep { $_->{urgent} } @{get_ws_content($ws1)};
521 is(@urgent, 1, 'B is marked urgent');
522 is(get_ws($ws1)->{urgent}, 1, 'the first workspace is marked urgent');
524 @urgent = grep { $_->{urgent} } @{get_ws_content($ws2)};
525 is(@urgent, 0, 'A is not marked urgent');
526 is(get_ws($ws2)->{urgent}, 0, 'the second workspace is not marked urgent');
530 ###############################################################################