]> git.sur5r.net Git - i3/i3/blob - testcases/t/291-swap.t
da2d564d0df069d4d054f85732a7e58ed8c9d604
[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 ###############################################################################
34 # Invalid con_id should not crash i3
35 # See issue #2895.
36 ###############################################################################
37
38 $ws = fresh_workspace;
39
40 open_window;
41 cmd "swap container with con_id 1";
42
43 does_i3_live;
44 kill_all_windows;
45
46 ###############################################################################
47 # Swap 2 windows in different workspaces using con_id
48 ###############################################################################
49
50 $ws = fresh_workspace;
51 open_window;
52 $A = get_focused($ws);
53
54 $ws = fresh_workspace;
55 open_window;
56
57 cmd "swap container with con_id $A";
58 is(get_focused($ws), $A, 'A is now focused');
59
60 kill_all_windows;
61
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.
66 #
67 # +---+---+    Layout: H1[ A B ]
68 # | A | B |    Focus Stacks:
69 # +---+---+        H1: B, A
70 ###############################################################################
71 $ws = fresh_workspace;
72
73 $A = open_window(wm_class => 'mark_A');
74 $B = open_window(wm_class => 'mark_B');
75 $expected_focus = get_focused($ws);
76
77 cmd '[con_mark=B] swap container with mark A';
78
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');
83
84 kill_all_windows;
85
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.
90 #
91 # +---+---+    Layout: H1[ V1[ A Y ] V2[ X B ] ]
92 # | A | X |    Focus Stacks:
93 # +---+---+        H1: V2, V1
94 # | Y | B |        V1: A, Y
95 # +---+---+        V2: B, X
96 ###############################################################################
97 $ws = fresh_workspace;
98
99 $A = open_window(wm_class => 'mark_A');
100 $B = open_window(wm_class => 'mark_B');
101 cmd 'split v';
102 open_window;
103 cmd 'move up, focus left';
104 cmd 'split v';
105 open_window;
106 cmd 'focus up, focus right, focus down';
107 $expected_focus = get_focused($ws);
108
109 cmd '[con_mark=B] swap container with mark A';
110
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');
115
116 kill_all_windows;
117
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.
122 #
123 # +---+---+    Layout: H1[ V1[ A Y ] V2[ X B ] ]
124 # | A | X |    Focus Stacks:
125 # +---+---+        H1: V2, V1
126 # | Y | B |        V1: Y, A
127 # +---+---+        V2: B, X
128 ###############################################################################
129 $ws = fresh_workspace;
130
131 $A = open_window(wm_class => 'mark_A');
132 $B = open_window(wm_class => 'mark_B');
133 cmd 'split v';
134 open_window;
135 cmd 'move up, focus left';
136 cmd 'split v';
137 open_window;
138 cmd 'focus right, focus down';
139 $expected_focus = get_focused($ws);
140
141 cmd '[con_mark=B] swap container with mark A';
142
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');
147
148 kill_all_windows;
149
150 ###############################################################################
151 # Swap two containers with one being on a different workspace.
152 # The focused container is B.
153 #
154 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
155 # Focus Stacks:
156 #     O1: W2, W1
157 #
158 # +---+---+    Layout: H1[ A X ]
159 # | A | X |    Focus Stacks:
160 # +---+---+        H1: A, X
161 #
162 # +---+---+    Layout: H2[ Y, B ]
163 # | Y | B |    Focus Stacks:
164 # +---+---+        H2: B, Y
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);
170     open_window;
171     cmd 'focus left';
172
173     $ws2 = fresh_workspace;
174     open_window;
175     $B = open_window(wm_class => 'mark_B');
176
177     my $A_fullscreen = "A" ~~ @$fullscreen || 0;
178     my $B_fullscreen = "B" ~~ @$fullscreen || 0;
179     $A->fullscreen($A_fullscreen);
180     $B->fullscreen($B_fullscreen);
181     sync_with_i3;
182
183     cmd '[con_mark=B] swap container with mark A';
184
185     $nodes = get_ws_content($ws1);
186     $node = $nodes->[0];
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');
190
191     $nodes = get_ws_content($ws2);
192     $node = $nodes->[1];
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');
197
198     kill_all_windows;
199 }
200
201 ###############################################################################
202 # Swap a non-fullscreen window with a fullscreen one in different workspaces.
203 # Layout: O1[ W1[ H1 ] W2[ B ] ]
204 #
205 # +---+---+    Layout: H1[ A F ]
206 # | A | F |    Focus Stacks:
207 # +---+---+        H1: F, A
208 #
209 # +---+---+
210 # |   B   |
211 # +---+---+
212 ###############################################################################
213 $ws1 = fresh_workspace;
214
215 $A = open_window(wm_class => 'mark_A');
216 $F = open_window();
217 $F->fullscreen(1);
218 $expected_focus = get_focused($ws1);
219
220 $ws2 = fresh_workspace;
221 $B = open_window(wm_class => 'mark_B');
222 $B->fullscreen(1);
223 sync_with_i3;
224
225 cmd '[con_mark=B] swap container with mark A';
226
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');
231
232 $nodes = get_ws_content($ws2);
233 is($nodes->[0]->{window}, $A->{id}, 'A is on ws1');
234
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
238 # swap is performed.
239 # A is swapped with fullscreened window B which is in another workspace.
240 #
241 # +---+---+    Layout: H1[ X V1[ A F ] ]
242 # |   | A |    Focus Stacks:
243 # | X +---+        H1: V1, X
244 # |   | F |        V1: F, A
245 # +---+---+
246 ###############################################################################
247 $ws1 = fresh_workspace;
248
249 open_window;
250 $A = open_window(wm_class => 'mark_A');
251 cmd "split v";
252 open_window;
253 cmd "focus parent";
254 cmd "fullscreen enable";
255 $expected_focus = get_focused($ws1);
256
257 $ws2 = fresh_workspace;
258 $B = open_window(wm_class => 'mark_B');
259 $B->fullscreen(1);
260 sync_with_i3;
261
262 cmd '[con_mark=B] swap container with mark A';
263
264 sync_with_i3;
265 does_i3_live;
266
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');
271
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');
275
276 ###############################################################################
277 # Swap two non-focused containers within the same workspace.
278 #
279 # +---+---+    Layout: H1[ V1[ A X ] V2[ F B ] ]
280 # | A | F |    Focus Stacks:
281 # +---+---+        H1: V2, V1
282 # | X | B |        V1: A, X
283 # +---+---+        V2: F, B
284 ###############################################################################
285 $ws = fresh_workspace;
286
287 $A = open_window(wm_class => 'mark_A');
288 $B = open_window(wm_class => 'mark_B');
289 cmd 'split v';
290 open_window;
291 cmd 'move up, focus left';
292 cmd 'split v';
293 open_window;
294 cmd 'focus up, focus right';
295 $expected_focus = get_focused($ws);
296
297 cmd '[con_mark=B] swap container with mark A';
298
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');
303
304 kill_all_windows;
305
306 ###############################################################################
307 # Swap two non-focused containers which are both on different workspaces.
308 #
309 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
310 # Focus Stacks:
311 #     O1: W3, W2, W1
312 #
313 # +---+
314 # | A |
315 # +---+
316 #
317 # +---+
318 # | B |
319 # +---+
320 #
321 # +---+
322 # | F |
323 # +---+
324 ###############################################################################
325 for my $fullscreen (@fullscreen_permutations){
326     $ws1 = fresh_workspace;
327     $A = open_window(wm_class => 'mark_A');
328
329     $ws2 = fresh_workspace;
330     $B = open_window(wm_class => 'mark_B');
331
332     $ws3 = fresh_workspace;
333     open_window;
334     $expected_focus = get_focused($ws3);
335
336     my $A_fullscreen = "A" ~~ @$fullscreen || 0;
337     my $B_fullscreen = "B" ~~ @$fullscreen || 0;
338     $A->fullscreen($A_fullscreen);
339     $B->fullscreen($B_fullscreen);
340     sync_with_i3;
341
342     cmd '[con_mark=B] swap container with mark A';
343
344     $nodes = get_ws_content($ws1);
345     $node = $nodes->[0];
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');
349
350     $nodes = get_ws_content($ws2);
351     $node = $nodes->[0];
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');
355
356     is(get_focused($ws3), $expected_focus, 'F is still focused');
357
358     kill_all_windows;
359 }
360
361 ###############################################################################
362 # Swap two non-focused containers with one being on a different workspace.
363 #
364 # Layout: O1[ W1[ A ] W2[ H2 ] ]
365 # Focus Stacks:
366 #     O1: W2, W1
367 #
368 # +---+
369 # | A |
370 # +---+
371 #
372 # +---+---+    Layout: H2[ B, F ]
373 # | B | F |    Focus Stacks:
374 # +---+---+        H2: F, B
375 #
376 # See issue: #3259
377 ###############################################################################
378
379 for my $fullscreen (0..1){
380     $ws1 = fresh_workspace;
381     $A = open_window(wm_class => 'mark_A');
382
383     $ws2 = fresh_workspace;
384     $B = open_window(wm_class => 'mark_B');
385     open_window;
386     cmd 'fullscreen enable' if $fullscreen;
387     $expected_focus = get_focused($ws2);
388
389     cmd '[con_mark=B] swap container with mark A';
390
391     $nodes = get_ws_content($ws1);
392     is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
393
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');
397
398     kill_all_windows;
399 }
400
401 ###############################################################################
402 # 1. A container cannot be swapped with its parent.
403 # 2. A container cannot be swapped with one of its children.
404 #
405 #      ↓A↓
406 # +---+---+    Layout: H1[ X V1[ Y B ] ]
407 # |   | Y |        (with A := V1)
408 # | X +---+
409 # |   | B |
410 # +---+---+
411 ###############################################################################
412 $ws = fresh_workspace;
413 open_window;
414 open_window;
415 cmd 'split v';
416 $B = open_window(wm_class => 'mark_B');
417 cmd 'focus parent, mark A, focus child';
418
419 $result = cmd '[con_mark=B] swap container with mark A';
420 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
421
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');
424
425 kill_all_windows;
426
427 ###############################################################################
428 # Swapping two containers preserves the geometry of the container they are
429 # being swapped with.
430 #
431 # Before:
432 # +---+-------+
433 # | A |   B   |
434 # +---+-------+
435 #
436 # After:
437 # +---+-------+
438 # | B |   A   |
439 # +---+-------+
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';
445
446 # sanity checks
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');
450
451 cmd '[con_mark=B] swap container with mark A';
452
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');
456
457 kill_all_windows;
458
459 ###############################################################################
460 # Swapping containers not sharing the same parent preserves the geometry of
461 # the container they are swapped with.
462 #
463 # Before:
464 # +---+-----+
465 # | A |     |
466 # +---+  B  |
467 # |   |     |
468 # | Y +-----+
469 # |   |  X  |
470 # +---+-----+
471 #
472 # After:
473 # +---+-----+
474 # | B |     |
475 # +---+  A  |
476 # |   |     |
477 # | Y +-----+
478 # |   |  X  |
479 # +---+-----+
480 ###############################################################################
481 $ws = fresh_workspace;
482
483 $A = open_window(wm_class => 'mark_A');
484 $B = open_window(wm_class => 'mark_B');
485 cmd 'split v';
486 open_window;
487 cmd 'focus up, resize grow height 0 or 25 ppt';
488 cmd 'focus left, split v';
489 open_window;
490 cmd 'resize grow height 0 or 25 ppt';
491
492 # sanity checks
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');
496
497 cmd '[con_mark=B] swap container with mark A';
498
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');
502
503 kill_all_windows;
504
505 ###############################################################################
506 # Swapping containers moves the urgency hint correctly.
507 ###############################################################################
508
509 $ws1 = fresh_workspace;
510 $A = open_window(wm_class => 'mark_A');
511 $ws2 = fresh_workspace;
512 $B = open_window(wm_class => 'mark_B');
513 open_window;
514
515 $B->add_hint('urgency');
516 sync_with_i3;
517
518 cmd '[con_mark=B] swap container with mark A';
519
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');
523
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');
527
528 kill_all_windows;
529
530 ###############################################################################
531
532 done_testing;