]> git.sur5r.net Git - i3/i3/blob - testcases/t/291-swap.t
d4a43bfb1ca7b6637001140283443c476cde822d
[i3/i3] / testcases / t / 291-swap.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Please read the following documents before working on tests:
5 # • https://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • https://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • https://build.i3wm.org/docs/ipc.html
12 #   (or docs/ipc)
13 #
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 #   (unless you are already familiar with Perl)
16 #
17 # Tests the swap command.
18 # Ticket: #917
19 use i3test i3_config => <<EOT;
20 # i3 config file (v4)
21 font font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
22
23 for_window[class="mark_A"] mark A
24 for_window[class="mark_B"] mark B
25 EOT
26
27 my ($ws, $ws1, $ws2, $ws3);
28 my ($node, $nodes, $expected_focus, $A, $B, $F);
29 my ($result);
30 my @fullscreen_permutations = ([], ["A"], ["B"], ["A", "B"]);
31 my @urgent;
32
33 sub fullscreen_windows {
34     my $ws = shift if @_;
35
36     scalar grep { $_->{fullscreen_mode} != 0 } @{get_ws_content($ws)}
37 }
38
39 ###############################################################################
40 # Invalid con_id should not crash i3
41 # See issue #2895.
42 ###############################################################################
43
44 $ws = fresh_workspace;
45
46 open_window;
47 cmd "swap container with con_id 1";
48
49 does_i3_live;
50 kill_all_windows;
51
52 ###############################################################################
53 # Swap 2 windows in different workspaces using con_id
54 ###############################################################################
55
56 $ws = fresh_workspace;
57 open_window;
58 $A = get_focused($ws);
59
60 $ws = fresh_workspace;
61 open_window;
62
63 cmd "swap container with con_id $A";
64 is(get_focused($ws), $A, 'A is now focused');
65
66 kill_all_windows;
67
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.
72 #
73 # +---+---+    Layout: H1[ A B ]
74 # | A | B |    Focus Stacks:
75 # +---+---+        H1: B, A
76 ###############################################################################
77 $ws = fresh_workspace;
78
79 $A = open_window(wm_class => 'mark_A');
80 $B = open_window(wm_class => 'mark_B');
81 $expected_focus = get_focused($ws);
82
83 cmd '[con_mark=B] swap container with mark A';
84
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');
89
90 kill_all_windows;
91
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.
96 #
97 # +---+---+    Layout: H1[ V1[ A Y ] V2[ X B ] ]
98 # | A | X |    Focus Stacks:
99 # +---+---+        H1: V2, V1
100 # | Y | B |        V1: A, Y
101 # +---+---+        V2: B, X
102 ###############################################################################
103 $ws = fresh_workspace;
104
105 $A = open_window(wm_class => 'mark_A');
106 $B = open_window(wm_class => 'mark_B');
107 cmd 'split v';
108 open_window;
109 cmd 'move up, focus left';
110 cmd 'split v';
111 open_window;
112 cmd 'focus up, focus right, focus down';
113 $expected_focus = get_focused($ws);
114
115 cmd '[con_mark=B] swap container with mark A';
116
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');
121
122 kill_all_windows;
123
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.
128 #
129 # +---+---+    Layout: H1[ V1[ A Y ] V2[ X B ] ]
130 # | A | X |    Focus Stacks:
131 # +---+---+        H1: V2, V1
132 # | Y | B |        V1: Y, A
133 # +---+---+        V2: B, X
134 ###############################################################################
135 $ws = fresh_workspace;
136
137 $A = open_window(wm_class => 'mark_A');
138 $B = open_window(wm_class => 'mark_B');
139 cmd 'split v';
140 open_window;
141 cmd 'move up, focus left';
142 cmd 'split v';
143 open_window;
144 cmd 'focus right, focus down';
145 $expected_focus = get_focused($ws);
146
147 cmd '[con_mark=B] swap container with mark A';
148
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');
153
154 kill_all_windows;
155
156 ###############################################################################
157 # Swap two containers with one being on a different workspace.
158 # The focused container is B.
159 #
160 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
161 # Focus Stacks:
162 #     O1: W2, W1
163 #
164 # +---+---+    Layout: H1[ A X ]
165 # | A | X |    Focus Stacks:
166 # +---+---+        H1: A, X
167 #
168 # +---+---+    Layout: H2[ Y, B ]
169 # | Y | B |    Focus Stacks:
170 # +---+---+        H2: B, Y
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);
176     open_window;
177     cmd 'focus left';
178
179     $ws2 = fresh_workspace;
180     open_window;
181     $B = open_window(wm_class => 'mark_B');
182
183     my $A_fullscreen = "A" ~~ @$fullscreen || 0;
184     my $B_fullscreen = "B" ~~ @$fullscreen || 0;
185     $A->fullscreen($A_fullscreen);
186     $B->fullscreen($B_fullscreen);
187     sync_with_i3;
188
189     cmd '[con_mark=B] swap container with mark A';
190
191     $nodes = get_ws_content($ws1);
192     $node = $nodes->[0];
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');
196
197     $nodes = get_ws_content($ws2);
198     $node = $nodes->[1];
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');
203
204     kill_all_windows;
205 }
206
207 ###############################################################################
208 # Swap a non-fullscreen window with a fullscreen one in different workspaces.
209 # Layout: O1[ W1[ H1 ] W2[ B ] ]
210 #
211 # +---+---+    Layout: H1[ A F ]
212 # | A | F |    Focus Stacks:
213 # +---+---+        H1: F, A
214 #
215 # +---+---+
216 # |   B   |
217 # +---+---+
218 ###############################################################################
219 $ws1 = fresh_workspace;
220
221 $A = open_window(wm_class => 'mark_A');
222 $F = open_window();
223 $F->fullscreen(1);
224 $expected_focus = get_focused($ws1);
225
226 $ws2 = fresh_workspace;
227 $B = open_window(wm_class => 'mark_B');
228 $B->fullscreen(1);
229 sync_with_i3;
230
231 cmd '[con_mark=B] swap container with mark A';
232
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');
237
238 $nodes = get_ws_content($ws2);
239 is($nodes->[0]->{window}, $A->{id}, 'A is on ws1');
240
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
244 # swap is performed.
245 # A is swapped with fullscreened window B which is in another workspace.
246 #
247 # +---+---+    Layout: H1[ X V1[ A F ] ]
248 # |   | A |    Focus Stacks:
249 # | X +---+        H1: V1, X
250 # |   | F |        V1: F, A
251 # +---+---+
252 ###############################################################################
253 $ws1 = fresh_workspace;
254
255 open_window;
256 $A = open_window(wm_class => 'mark_A');
257 cmd "split v";
258 open_window;
259 cmd "focus parent";
260 cmd "fullscreen enable";
261 $F = fullscreen_windows($ws1);
262 $expected_focus = get_focused($ws1);
263
264 $ws2 = fresh_workspace;
265 $B = open_window(wm_class => 'mark_B');
266 $B->fullscreen(1);
267 sync_with_i3;
268
269 cmd '[con_mark=B] swap container with mark A';
270
271 sync_with_i3;
272 does_i3_live;
273
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');
278
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');
282
283 ###############################################################################
284 # Swap two non-focused containers within the same workspace.
285 #
286 # +---+---+    Layout: H1[ V1[ A X ] V2[ F B ] ]
287 # | A | F |    Focus Stacks:
288 # +---+---+        H1: V2, V1
289 # | X | B |        V1: A, X
290 # +---+---+        V2: F, B
291 ###############################################################################
292 $ws = fresh_workspace;
293
294 $A = open_window(wm_class => 'mark_A');
295 $B = open_window(wm_class => 'mark_B');
296 cmd 'split v';
297 open_window;
298 cmd 'move up, focus left';
299 cmd 'split v';
300 open_window;
301 cmd 'focus up, focus right';
302 $expected_focus = get_focused($ws);
303
304 cmd '[con_mark=B] swap container with mark A';
305
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');
310
311 kill_all_windows;
312
313 ###############################################################################
314 # Swap two non-focused containers which are both on different workspaces.
315 #
316 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
317 # Focus Stacks:
318 #     O1: W3, W2, W1
319 #
320 # +---+
321 # | A |
322 # +---+
323 #
324 # +---+
325 # | B |
326 # +---+
327 #
328 # +---+
329 # | F |
330 # +---+
331 ###############################################################################
332 for my $fullscreen (@fullscreen_permutations){
333     $ws1 = fresh_workspace;
334     $A = open_window(wm_class => 'mark_A');
335
336     $ws2 = fresh_workspace;
337     $B = open_window(wm_class => 'mark_B');
338
339     $ws3 = fresh_workspace;
340     open_window;
341     $expected_focus = get_focused($ws3);
342
343     my $A_fullscreen = "A" ~~ @$fullscreen || 0;
344     my $B_fullscreen = "B" ~~ @$fullscreen || 0;
345     $A->fullscreen($A_fullscreen);
346     $B->fullscreen($B_fullscreen);
347     sync_with_i3;
348
349     cmd '[con_mark=B] swap container with mark A';
350
351     $nodes = get_ws_content($ws1);
352     $node = $nodes->[0];
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');
356
357     $nodes = get_ws_content($ws2);
358     $node = $nodes->[0];
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');
362
363     is(get_focused($ws3), $expected_focus, 'F is still focused');
364
365     kill_all_windows;
366 }
367
368 ###############################################################################
369 # Swap two non-focused containers with one being on a different workspace.
370 #
371 # Layout: O1[ W1[ A ] W2[ H2 ] ]
372 # Focus Stacks:
373 #     O1: W2, W1
374 #
375 # +---+
376 # | A |
377 # +---+
378 #
379 # +---+---+    Layout: H2[ B, F ]
380 # | B | F |    Focus Stacks:
381 # +---+---+        H2: F, B
382 ###############################################################################
383
384 $ws1 = fresh_workspace;
385 $A = open_window(wm_class => 'mark_A');
386
387 $ws2 = fresh_workspace;
388 $B = open_window(wm_class => 'mark_B');
389 open_window;
390 $expected_focus = get_focused($ws2);
391
392 cmd '[con_mark=B] swap container with mark A';
393
394 $nodes = get_ws_content($ws1);
395 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
396
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');
400
401 kill_all_windows;
402
403 ###############################################################################
404 # 1. A container cannot be swapped with its parent.
405 # 2. A container cannot be swapped with one of its children.
406 #
407 #      ↓A↓
408 # +---+---+    Layout: H1[ X V1[ Y B ] ]
409 # |   | Y |        (with A := V1)
410 # | X +---+
411 # |   | B |
412 # +---+---+
413 ###############################################################################
414 $ws = fresh_workspace;
415 open_window;
416 open_window;
417 cmd 'split v';
418 $B = open_window(wm_class => 'mark_B');
419 cmd 'focus parent, mark A, focus child';
420
421 $result = cmd '[con_mark=B] swap container with mark A';
422 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
423
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');
426
427 kill_all_windows;
428
429 ###############################################################################
430 # Swapping two containers preserves the geometry of the container they are
431 # being swapped with.
432 #
433 # Before:
434 # +---+-------+
435 # | A |   B   |
436 # +---+-------+
437 #
438 # After:
439 # +---+-------+
440 # | B |   A   |
441 # +---+-------+
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';
447
448 # sanity checks
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');
452
453 cmd '[con_mark=B] swap container with mark A';
454
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');
458
459 kill_all_windows;
460
461 ###############################################################################
462 # Swapping containers not sharing the same parent preserves the geometry of
463 # the container they are swapped with.
464 #
465 # Before:
466 # +---+-----+
467 # | A |     |
468 # +---+  B  |
469 # |   |     |
470 # | Y +-----+
471 # |   |  X  |
472 # +---+-----+
473 #
474 # After:
475 # +---+-----+
476 # | B |     |
477 # +---+  A  |
478 # |   |     |
479 # | Y +-----+
480 # |   |  X  |
481 # +---+-----+
482 ###############################################################################
483 $ws = fresh_workspace;
484
485 $A = open_window(wm_class => 'mark_A');
486 $B = open_window(wm_class => 'mark_B');
487 cmd 'split v';
488 open_window;
489 cmd 'focus up, resize grow height 0 or 25 ppt';
490 cmd 'focus left, split v';
491 open_window;
492 cmd 'resize grow height 0 or 25 ppt';
493
494 # sanity checks
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');
498
499 cmd '[con_mark=B] swap container with mark A';
500
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');
504
505 kill_all_windows;
506
507 ###############################################################################
508 # Swapping containers moves the urgency hint correctly.
509 ###############################################################################
510
511 $ws1 = fresh_workspace;
512 $A = open_window(wm_class => 'mark_A');
513 $ws2 = fresh_workspace;
514 $B = open_window(wm_class => 'mark_B');
515 open_window;
516
517 $B->add_hint('urgency');
518 sync_with_i3;
519
520 cmd '[con_mark=B] swap container with mark A';
521
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');
525
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');
529
530 kill_all_windows;
531
532 ###############################################################################
533
534 done_testing;