]> git.sur5r.net Git - i3/i3/blob - testcases/t/291-swap.t
ipc: document how to detect i3’s byte order in memory-safe languages (#2961)
[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 # • http://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • http://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • http://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
230 cmd '[con_mark=B] swap container with mark A';
231
232 $nodes = get_ws_content($ws1);
233 is($nodes->[0]->{window}, $B->{id}, 'B is on ws1:left');
234 is(fullscreen_windows($ws1), 1, 'F still fullscreen in ws1');
235 is(get_focused($ws1), $expected_focus, 'F is still focused');
236
237 $nodes = get_ws_content($ws2);
238 is($nodes->[0]->{window}, $A->{id}, 'A is on ws1');
239
240 ###############################################################################
241 # Try a more exotic layout with fullscreen containers.
242 # A and F are fullscreened as a stack of two vertical containers before the
243 # swap is performed.
244 # A is swapped with fullscreened window B which is in another workspace.
245 #
246 # +---+---+    Layout: H1[ X V1[ A F ] ]
247 # |   | A |    Focus Stacks:
248 # | X +---+        H1: V1, X
249 # |   | F |        V1: F, A
250 # +---+---+
251 ###############################################################################
252 $ws1 = fresh_workspace;
253
254 open_window;
255 $A = open_window(wm_class => 'mark_A');
256 cmd "split v";
257 open_window;
258 cmd "focus parent";
259 cmd "fullscreen enable";
260 $F = fullscreen_windows($ws1);
261 $expected_focus = get_focused($ws1);
262
263 $ws2 = fresh_workspace;
264 $B = open_window(wm_class => 'mark_B');
265 $B->fullscreen(1);
266
267 cmd '[con_mark=B] swap container with mark A';
268
269 sync_with_i3;
270 does_i3_live;
271
272 $nodes = get_ws_content($ws1);
273 is($nodes->[1]->{nodes}->[0]->{window}, $B->{id}, 'B is on top right in ws1');
274 is(get_focused($ws1), $expected_focus, 'The container of the stacked windows remains focused in ws1');
275 is(fullscreen_windows($ws1), $F, 'Same amount of fullscreen windows in ws1');
276
277 $nodes = get_ws_content($ws2);
278 is($nodes->[0]->{window}, $A->{id}, 'A is on ws2');
279 is(fullscreen_windows($ws2), 1, 'A is in fullscreen mode');
280
281 ###############################################################################
282 # Swap two non-focused containers within the same workspace.
283 #
284 # +---+---+    Layout: H1[ V1[ A X ] V2[ F B ] ]
285 # | A | F |    Focus Stacks:
286 # +---+---+        H1: V2, V1
287 # | X | B |        V1: A, X
288 # +---+---+        V2: F, B
289 ###############################################################################
290 $ws = fresh_workspace;
291
292 $A = open_window(wm_class => 'mark_A');
293 $B = open_window(wm_class => 'mark_B');
294 cmd 'split v';
295 open_window;
296 cmd 'move up, focus left';
297 cmd 'split v';
298 open_window;
299 cmd 'focus up, focus right';
300 $expected_focus = get_focused($ws);
301
302 cmd '[con_mark=B] swap container with mark A';
303
304 $nodes = get_ws_content($ws);
305 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
306 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
307 is(get_focused($ws), $expected_focus, 'F is still focused');
308
309 kill_all_windows;
310
311 ###############################################################################
312 # Swap two non-focused containers which are both on different workspaces.
313 #
314 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
315 # Focus Stacks:
316 #     O1: W3, W2, W1
317 #
318 # +---+
319 # | A |
320 # +---+
321 #
322 # +---+
323 # | B |
324 # +---+
325 #
326 # +---+
327 # | F |
328 # +---+
329 ###############################################################################
330 for my $fullscreen (@fullscreen_permutations){
331     $ws1 = fresh_workspace;
332     $A = open_window(wm_class => 'mark_A');
333
334     $ws2 = fresh_workspace;
335     $B = open_window(wm_class => 'mark_B');
336
337     $ws3 = fresh_workspace;
338     open_window;
339     $expected_focus = get_focused($ws3);
340
341     my $A_fullscreen = "A" ~~ @$fullscreen || 0;
342     my $B_fullscreen = "B" ~~ @$fullscreen || 0;
343     $A->fullscreen($A_fullscreen);
344     $B->fullscreen($B_fullscreen);
345     sync_with_i3;
346
347     cmd '[con_mark=B] swap container with mark A';
348
349     $nodes = get_ws_content($ws1);
350     $node = $nodes->[0];
351     is($node->{window}, $B->{id}, 'B is on the first workspace');
352     is(fullscreen_windows($ws1), $A_fullscreen, 'amount of fullscreen windows in ws1');
353     is($node->{fullscreen_mode}, $A_fullscreen, 'B got A\'s fullscreen mode');
354
355     $nodes = get_ws_content($ws2);
356     $node = $nodes->[0];
357     is($node->{window}, $A->{id}, 'A is on the second workspace');
358     is(fullscreen_windows($ws2), $B_fullscreen, 'amount of fullscreen windows in ws2');
359     is($node->{fullscreen_mode}, $B_fullscreen, 'A got B\'s fullscreen mode');
360
361     is(get_focused($ws3), $expected_focus, 'F is still focused');
362
363     kill_all_windows;
364 }
365
366 ###############################################################################
367 # Swap two non-focused containers with one being on a different workspace.
368 #
369 # Layout: O1[ W1[ A ] W2[ H2 ] ]
370 # Focus Stacks:
371 #     O1: W2, W1
372 #
373 # +---+
374 # | A |
375 # +---+
376 #
377 # +---+---+    Layout: H2[ B, F ]
378 # | B | F |    Focus Stacks:
379 # +---+---+        H2: F, B
380 ###############################################################################
381
382 $ws1 = fresh_workspace;
383 $A = open_window(wm_class => 'mark_A');
384
385 $ws2 = fresh_workspace;
386 $B = open_window(wm_class => 'mark_B');
387 open_window;
388 $expected_focus = get_focused($ws2);
389
390 cmd '[con_mark=B] swap container with mark A';
391
392 $nodes = get_ws_content($ws1);
393 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
394
395 $nodes = get_ws_content($ws2);
396 is($nodes->[0]->{window}, $A->{id}, 'A is on the left of the second workspace');
397 is(get_focused($ws2), $expected_focus, 'F is still focused');
398
399 kill_all_windows;
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;