2 # vim:ts=4:sw=4:expandtab
4 # Please read the following documents before working on tests:
5 # • http://build.i3wm.org/docs/testsuite.html
8 # • http://build.i3wm.org/docs/lib-i3test.html
9 # (alternatively: perldoc ./testcases/lib/i3test.pm)
11 # • http://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 sub fullscreen_windows {
36 scalar grep { $_->{fullscreen_mode} != 0 } @{get_ws_content($ws)}
39 ###############################################################################
40 # Invalid con_id should not crash i3
42 ###############################################################################
44 $ws = fresh_workspace;
47 cmd "swap container with con_id 1";
52 ###############################################################################
53 # Swap 2 windows in different workspaces using con_id
54 ###############################################################################
56 $ws = fresh_workspace;
58 $A = get_focused($ws);
60 $ws = fresh_workspace;
63 cmd "swap container with con_id $A";
64 is(get_focused($ws), $A, 'A is now focused');
68 ###############################################################################
69 # Swap two containers next to each other.
70 # Focus should stay on B because both windows are on the focused workspace.
71 # The focused container is B.
73 # +---+---+ Layout: H1[ A B ]
74 # | A | B | Focus Stacks:
76 ###############################################################################
77 $ws = fresh_workspace;
79 $A = open_window(wm_class => 'mark_A');
80 $B = open_window(wm_class => 'mark_B');
81 $expected_focus = get_focused($ws);
83 cmd '[con_mark=B] swap container with mark A';
85 $nodes = get_ws_content($ws);
86 is($nodes->[0]->{window}, $B->{id}, 'B is on the left');
87 is($nodes->[1]->{window}, $A->{id}, 'A is on the right');
88 is(get_focused($ws), $expected_focus, 'B is still focused');
92 ###############################################################################
93 # Swap two containers with different parents.
94 # In this test, the focus head of the left v-split container is A.
95 # The focused container is B.
97 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
98 # | A | X | Focus Stacks:
99 # +---+---+ H1: V2, V1
102 ###############################################################################
103 $ws = fresh_workspace;
105 $A = open_window(wm_class => 'mark_A');
106 $B = open_window(wm_class => 'mark_B');
109 cmd 'move up, focus left';
112 cmd 'focus up, focus right, focus down';
113 $expected_focus = get_focused($ws);
115 cmd '[con_mark=B] swap container with mark A';
117 $nodes = get_ws_content($ws);
118 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
119 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
120 is(get_focused($ws), $expected_focus, 'B is still focused');
124 ###############################################################################
125 # Swap two containers with different parents.
126 # In this test, the focus head of the left v-split container is _not_ A.
127 # The focused container is B.
129 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
130 # | A | X | Focus Stacks:
131 # +---+---+ H1: V2, V1
134 ###############################################################################
135 $ws = fresh_workspace;
137 $A = open_window(wm_class => 'mark_A');
138 $B = open_window(wm_class => 'mark_B');
141 cmd 'move up, focus left';
144 cmd 'focus right, focus down';
145 $expected_focus = get_focused($ws);
147 cmd '[con_mark=B] swap container with mark A';
149 $nodes = get_ws_content($ws);
150 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
151 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
152 is(get_focused($ws), $expected_focus, 'B is still focused');
156 ###############################################################################
157 # Swap two containers with one being on a different workspace.
158 # The focused container is B.
160 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
164 # +---+---+ Layout: H1[ A X ]
165 # | A | X | Focus Stacks:
168 # +---+---+ Layout: H2[ Y, B ]
169 # | Y | B | Focus Stacks:
171 ###############################################################################
172 for my $fullscreen (@fullscreen_permutations){
173 $ws1 = fresh_workspace;
174 $A = open_window(wm_class => 'mark_A');
175 $expected_focus = get_focused($ws1);
179 $ws2 = fresh_workspace;
181 $B = open_window(wm_class => 'mark_B');
183 my $A_fullscreen = "A" ~~ @$fullscreen || 0;
184 my $B_fullscreen = "B" ~~ @$fullscreen || 0;
185 $A->fullscreen($A_fullscreen);
186 $B->fullscreen($B_fullscreen);
189 cmd '[con_mark=B] swap container with mark A';
191 $nodes = get_ws_content($ws1);
193 is($node->{window}, $B->{id}, 'B is on ws1:left');
194 is(fullscreen_windows($ws1), $A_fullscreen, 'amount of fullscreen windows in ws1');
195 is($node->{fullscreen_mode}, $A_fullscreen, 'B got A\'s fullscreen mode');
197 $nodes = get_ws_content($ws2);
199 is($node->{window}, $A->{id}, 'A is on ws2:right');
200 is(get_focused($ws2), $expected_focus, 'A is focused');
201 is(fullscreen_windows($ws2), $B_fullscreen, 'amount of fullscreen windows in ws2');
202 is($node->{fullscreen_mode}, $B_fullscreen, 'A got B\'s fullscreen mode');
207 ###############################################################################
208 # Swap a non-fullscreen window with a fullscreen one in different workspaces.
209 # Layout: O1[ W1[ H1 ] W2[ B ] ]
211 # +---+---+ Layout: H1[ A F ]
212 # | A | F | Focus Stacks:
218 ###############################################################################
219 $ws1 = fresh_workspace;
221 $A = open_window(wm_class => 'mark_A');
224 $expected_focus = get_focused($ws1);
226 $ws2 = fresh_workspace;
227 $B = open_window(wm_class => 'mark_B');
231 cmd '[con_mark=B] swap container with mark A';
233 $nodes = get_ws_content($ws1);
234 is($nodes->[0]->{window}, $B->{id}, 'B is on ws1:left');
235 is(fullscreen_windows($ws1), 1, 'F still fullscreen in ws1');
236 is(get_focused($ws1), $expected_focus, 'F is still focused');
238 $nodes = get_ws_content($ws2);
239 is($nodes->[0]->{window}, $A->{id}, 'A is on ws1');
241 ###############################################################################
242 # Try a more exotic layout with fullscreen containers.
243 # A and F are fullscreened as a stack of two vertical containers before the
245 # A is swapped with fullscreened window B which is in another workspace.
247 # +---+---+ Layout: H1[ X V1[ A F ] ]
248 # | | A | Focus Stacks:
249 # | X +---+ H1: V1, X
252 ###############################################################################
253 $ws1 = fresh_workspace;
256 $A = open_window(wm_class => 'mark_A');
260 cmd "fullscreen enable";
261 $F = fullscreen_windows($ws1);
262 $expected_focus = get_focused($ws1);
264 $ws2 = fresh_workspace;
265 $B = open_window(wm_class => 'mark_B');
269 cmd '[con_mark=B] swap container with mark A';
274 $nodes = get_ws_content($ws1);
275 is($nodes->[1]->{nodes}->[0]->{window}, $B->{id}, 'B is on top right in ws1');
276 is(get_focused($ws1), $expected_focus, 'The container of the stacked windows remains focused in ws1');
277 is(fullscreen_windows($ws1), $F, 'Same amount of fullscreen windows in ws1');
279 $nodes = get_ws_content($ws2);
280 is($nodes->[0]->{window}, $A->{id}, 'A is on ws2');
281 is(fullscreen_windows($ws2), 1, 'A is in fullscreen mode');
283 ###############################################################################
284 # Swap two non-focused containers within the same workspace.
286 # +---+---+ Layout: H1[ V1[ A X ] V2[ F B ] ]
287 # | A | F | Focus Stacks:
288 # +---+---+ H1: V2, V1
291 ###############################################################################
292 $ws = fresh_workspace;
294 $A = open_window(wm_class => 'mark_A');
295 $B = open_window(wm_class => 'mark_B');
298 cmd 'move up, focus left';
301 cmd 'focus up, focus right';
302 $expected_focus = get_focused($ws);
304 cmd '[con_mark=B] swap container with mark A';
306 $nodes = get_ws_content($ws);
307 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
308 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
309 is(get_focused($ws), $expected_focus, 'F is still focused');
313 ###############################################################################
314 # Swap two non-focused containers which are both on different workspaces.
316 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
331 ###############################################################################
332 for my $fullscreen (@fullscreen_permutations){
333 $ws1 = fresh_workspace;
334 $A = open_window(wm_class => 'mark_A');
336 $ws2 = fresh_workspace;
337 $B = open_window(wm_class => 'mark_B');
339 $ws3 = fresh_workspace;
341 $expected_focus = get_focused($ws3);
343 my $A_fullscreen = "A" ~~ @$fullscreen || 0;
344 my $B_fullscreen = "B" ~~ @$fullscreen || 0;
345 $A->fullscreen($A_fullscreen);
346 $B->fullscreen($B_fullscreen);
349 cmd '[con_mark=B] swap container with mark A';
351 $nodes = get_ws_content($ws1);
353 is($node->{window}, $B->{id}, 'B is on the first workspace');
354 is(fullscreen_windows($ws1), $A_fullscreen, 'amount of fullscreen windows in ws1');
355 is($node->{fullscreen_mode}, $A_fullscreen, 'B got A\'s fullscreen mode');
357 $nodes = get_ws_content($ws2);
359 is($node->{window}, $A->{id}, 'A is on the second workspace');
360 is(fullscreen_windows($ws2), $B_fullscreen, 'amount of fullscreen windows in ws2');
361 is($node->{fullscreen_mode}, $B_fullscreen, 'A got B\'s fullscreen mode');
363 is(get_focused($ws3), $expected_focus, 'F is still focused');
368 ###############################################################################
369 # Swap two non-focused containers with one being on a different workspace.
371 # Layout: O1[ W1[ A ] W2[ H2 ] ]
379 # +---+---+ Layout: H2[ B, F ]
380 # | B | F | Focus Stacks:
382 ###############################################################################
384 $ws1 = fresh_workspace;
385 $A = open_window(wm_class => 'mark_A');
387 $ws2 = fresh_workspace;
388 $B = open_window(wm_class => 'mark_B');
390 $expected_focus = get_focused($ws2);
392 cmd '[con_mark=B] swap container with mark A';
394 $nodes = get_ws_content($ws1);
395 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
397 $nodes = get_ws_content($ws2);
398 is($nodes->[0]->{window}, $A->{id}, 'A is on the left of the second workspace');
399 is(get_focused($ws2), $expected_focus, 'F is still focused');
403 ###############################################################################
404 # 1. A container cannot be swapped with its parent.
405 # 2. A container cannot be swapped with one of its children.
408 # +---+---+ Layout: H1[ X V1[ Y B ] ]
409 # | | Y | (with A := V1)
413 ###############################################################################
414 $ws = fresh_workspace;
418 $B = open_window(wm_class => 'mark_B');
419 cmd 'focus parent, mark A, focus child';
421 $result = cmd '[con_mark=B] swap container with mark A';
422 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
424 $result = cmd '[con_mark=A] swap container with mark B';
425 is($result->[0]->{success}, 0, 'A cannot be swappd with one of its children');
429 ###############################################################################
430 # Swapping two containers preserves the geometry of the container they are
431 # being swapped with.
442 ###############################################################################
443 $ws = fresh_workspace;
444 $A = open_window(wm_class => 'mark_A');
445 $B = open_window(wm_class => 'mark_B');
446 cmd 'resize grow width 0 or 25 ppt';
449 $nodes = get_ws_content($ws);
450 cmp_float($nodes->[0]->{percent}, 0.25, 'A has 25% width');
451 cmp_float($nodes->[1]->{percent}, 0.75, 'B has 75% width');
453 cmd '[con_mark=B] swap container with mark A';
455 $nodes = get_ws_content($ws);
456 cmp_float($nodes->[0]->{percent}, 0.25, 'B has 25% width');
457 cmp_float($nodes->[1]->{percent}, 0.75, 'A has 75% width');
461 ###############################################################################
462 # Swapping containers not sharing the same parent preserves the geometry of
463 # the container they are swapped with.
482 ###############################################################################
483 $ws = fresh_workspace;
485 $A = open_window(wm_class => 'mark_A');
486 $B = open_window(wm_class => 'mark_B');
489 cmd 'focus up, resize grow height 0 or 25 ppt';
490 cmd 'focus left, split v';
492 cmd 'resize grow height 0 or 25 ppt';
495 $nodes = get_ws_content($ws);
496 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'A has 25% height');
497 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'B has 75% height');
499 cmd '[con_mark=B] swap container with mark A';
501 $nodes = get_ws_content($ws);
502 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'B has 25% height');
503 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'A has 75% height');
507 ###############################################################################
508 # Swapping containers moves the urgency hint correctly.
509 ###############################################################################
511 $ws1 = fresh_workspace;
512 $A = open_window(wm_class => 'mark_A');
513 $ws2 = fresh_workspace;
514 $B = open_window(wm_class => 'mark_B');
517 $B->add_hint('urgency');
520 cmd '[con_mark=B] swap container with mark A';
522 @urgent = grep { $_->{urgent} } @{get_ws_content($ws1)};
523 is(@urgent, 1, 'B is marked urgent');
524 is(get_ws($ws1)->{urgent}, 1, 'the first workspace is marked urgent');
526 @urgent = grep { $_->{urgent} } @{get_ws_content($ws2)};
527 is(@urgent, 0, 'A is not marked urgent');
528 is(get_ws($ws2)->{urgent}, 0, 'the second workspace is not marked urgent');
532 ###############################################################################