]> git.sur5r.net Git - i3/i3/blob - testcases/t/265-swap.t
Merge pull request #2496 from Airblader/feature-917
[i3/i3] / testcases / t / 265-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_autostart => 0;
20
21 my $config = <<EOT;
22 # i3 config file (v4)
23 font font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
24
25 for_window[class="mark_A"] mark A
26 for_window[class="mark_B"] mark B
27 EOT
28
29 my ($pid);
30 my ($ws, $ws1, $ws2, $ws3);
31 my ($nodes, $expected_focus, $A, $B, $F);
32 my ($result);
33 my @urgent;
34
35 ###############################################################################
36 # Swap two containers next to each other.
37 # Focus should stay on B because both windows are on the focused workspace.
38 # The focused container is B.
39 #
40 # +---+---+    Layout: H1[ A B ]
41 # | A | B |    Focus Stacks:
42 # +---+---+        H1: B, A
43 ###############################################################################
44 $pid = launch_with_config($config);
45 $ws = fresh_workspace;
46
47 $A = open_window(wm_class => 'mark_A');
48 $B = open_window(wm_class => 'mark_B');
49 $expected_focus = get_focused($ws);
50
51 cmd '[con_mark=B] swap container with mark A';
52
53 $nodes = get_ws_content($ws);
54 is($nodes->[0]->{window}, $B->{id}, 'B is on the left');
55 is($nodes->[1]->{window}, $A->{id}, 'A is on the right');
56 is(get_focused($ws), $expected_focus, 'B is still focused');
57
58 exit_gracefully($pid);
59
60 ###############################################################################
61 # Swap two containers with different parents.
62 # In this test, the focus head of the left v-split container is A.
63 # The focused container is B.
64 #
65 # +---+---+    Layout: H1[ V1[ A Y ] V2[ X B ] ]
66 # | A | X |    Focus Stacks:
67 # +---+---+        H1: V2, V1
68 # | Y | B |        V1: A, Y
69 # +---+---+        V2: B, X
70 ###############################################################################
71 $pid = launch_with_config($config);
72 $ws = fresh_workspace;
73
74 $A = open_window(wm_class => 'mark_A');
75 $B = open_window(wm_class => 'mark_B');
76 cmd 'split v';
77 open_window;
78 cmd 'move up, focus left';
79 cmd 'split v';
80 open_window;
81 cmd 'focus up, focus right, focus down';
82 $expected_focus = get_focused($ws);
83
84 cmd '[con_mark=B] swap container with mark A';
85
86 $nodes = get_ws_content($ws);
87 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
88 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
89 is(get_focused($ws), $expected_focus, 'B is still focused');
90
91 exit_gracefully($pid);
92
93 ###############################################################################
94 # Swap two containers with different parents.
95 # In this test, the focus head of the left v-split container is _not_ A.
96 # The focused container is B.
97 #
98 # +---+---+    Layout: H1[ V1[ A Y ] V2[ X B ] ]
99 # | A | X |    Focus Stacks:
100 # +---+---+        H1: V2, V1
101 # | Y | B |        V1: Y, A
102 # +---+---+        V2: B, X
103 ###############################################################################
104 $pid = launch_with_config($config);
105 $ws = fresh_workspace;
106
107 $A = open_window(wm_class => 'mark_A');
108 $B = open_window(wm_class => 'mark_B');
109 cmd 'split v';
110 open_window;
111 cmd 'move up, focus left';
112 cmd 'split v';
113 open_window;
114 cmd 'focus right, focus down';
115 $expected_focus = get_focused($ws);
116
117 cmd '[con_mark=B] swap container with mark A';
118
119 $nodes = get_ws_content($ws);
120 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
121 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
122 is(get_focused($ws), $expected_focus, 'B is still focused');
123
124 exit_gracefully($pid);
125
126 ###############################################################################
127 # Swap two containers with one being on a different workspace.
128 # The focused container is B.
129 #
130 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
131 # Focus Stacks:
132 #     O1: W2, W1
133 #
134 # +---+---+    Layout: H1[ A X ]
135 # | A | X |    Focus Stacks:
136 # +---+---+        H1: A, X
137 #
138 # +---+---+    Layout: H2[ Y, B ]
139 # | Y | B |    Focus Stacks:
140 # +---+---+        H2: B, Y
141 ###############################################################################
142 $pid = launch_with_config($config);
143
144 $ws1 = fresh_workspace;
145 $A = open_window(wm_class => 'mark_A');
146 $expected_focus = get_focused($ws1);
147 open_window;
148 cmd 'focus left';
149
150 $ws2 = fresh_workspace;
151 open_window;
152 $B = open_window(wm_class => 'mark_B');
153
154 cmd '[con_mark=B] swap container with mark A';
155
156 $nodes = get_ws_content($ws1);
157 is($nodes->[0]->{window}, $B->{id}, 'B is on ws1:left');
158
159 $nodes = get_ws_content($ws2);
160 is($nodes->[1]->{window}, $A->{id}, 'A is on ws1:right');
161 is(get_focused($ws2), $expected_focus, 'A is focused');
162
163 exit_gracefully($pid);
164
165 ###############################################################################
166 # Swap two non-focused containers within the same workspace.
167 #
168 # +---+---+    Layout: H1[ V1[ A X ] V2[ F B ] ]
169 # | A | F |    Focus Stacks:
170 # +---+---+        H1: V2, V1
171 # | X | B |        V1: A, X
172 # +---+---+        V2: F, B
173 ###############################################################################
174 $pid = launch_with_config($config);
175 $ws = fresh_workspace;
176
177 $A = open_window(wm_class => 'mark_A');
178 $B = open_window(wm_class => 'mark_B');
179 cmd 'split v';
180 open_window;
181 cmd 'move up, focus left';
182 cmd 'split v';
183 open_window;
184 cmd 'focus up, focus right';
185 $expected_focus = get_focused($ws);
186
187 cmd '[con_mark=B] swap container with mark A';
188
189 $nodes = get_ws_content($ws);
190 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
191 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
192 is(get_focused($ws), $expected_focus, 'F is still focused');
193
194 exit_gracefully($pid);
195
196 ###############################################################################
197 # Swap two non-focused containers which are both on different workspaces.
198 #
199 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
200 # Focus Stacks:
201 #     O1: W3, W2, W1
202 #
203 # +---+
204 # | A |
205 # +---+
206 #
207 # +---+
208 # | B |
209 # +---+
210 #
211 # +---+
212 # | F |
213 # +---+
214 ###############################################################################
215 $pid = launch_with_config($config);
216
217 $ws1 = fresh_workspace;
218 $A = open_window(wm_class => 'mark_A');
219
220 $ws2 = fresh_workspace;
221 $B = open_window(wm_class => 'mark_B');
222
223 $ws3 = fresh_workspace;
224 open_window;
225 $expected_focus = get_focused($ws3);
226
227 cmd '[con_mark=B] swap container with mark A';
228
229 $nodes = get_ws_content($ws1);
230 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
231
232 $nodes = get_ws_content($ws2);
233 is($nodes->[0]->{window}, $A->{id}, 'A is on the second workspace');
234
235 is(get_focused($ws3), $expected_focus, 'F is still focused');
236
237 exit_gracefully($pid);
238
239 ###############################################################################
240 # Swap two non-focused containers with one being on a different workspace.
241 #
242 # Layout: O1[ W1[ A ] W2[ H2 ] ]
243 # Focus Stacks:
244 #     O1: W2, W1
245 #
246 # +---+
247 # | A |
248 # +---+
249 #
250 # +---+---+    Layout: H2[ B, F ]
251 # | B | F |    Focus Stacks:
252 # +---+---+        H2: F, B
253 ###############################################################################
254 $pid = launch_with_config($config);
255
256 $ws1 = fresh_workspace;
257 $A = open_window(wm_class => 'mark_A');
258
259 $ws2 = fresh_workspace;
260 $B = open_window(wm_class => 'mark_B');
261 open_window;
262 $expected_focus = get_focused($ws2);
263
264 cmd '[con_mark=B] swap container with mark A';
265
266 $nodes = get_ws_content($ws1);
267 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
268
269 $nodes = get_ws_content($ws2);
270 is($nodes->[0]->{window}, $A->{id}, 'A is on the left of the second workspace');
271 is(get_focused($ws2), $expected_focus, 'F is still focused');
272
273 exit_gracefully($pid);
274
275 ###############################################################################
276 # 1. A container cannot be swapped with its parent.
277 # 2. A container cannot be swapped with one of its children.
278 #
279 #      ↓A↓
280 # +---+---+    Layout: H1[ X V1[ Y B ] ]
281 # |   | Y |        (with A := V1)
282 # | X +---+
283 # |   | B |
284 # +---+---+
285 ###############################################################################
286 $pid = launch_with_config($config);
287
288 $ws = fresh_workspace;
289 open_window;
290 open_window;
291 cmd 'split v';
292 $B = open_window(wm_class => 'mark_B');
293 cmd 'focus parent, mark A, focus child';
294
295 $result = cmd '[con_mark=B] swap container with mark A';
296 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
297
298 $result = cmd '[con_mark=A] swap container with mark B';
299 is($result->[0]->{success}, 0, 'A cannot be swappd with one of its children');
300
301 exit_gracefully($pid);
302
303 ###############################################################################
304 # Swapping two containers preserves the geometry of the container they are
305 # being swapped with.
306 #
307 # Before:
308 # +---+-------+
309 # | A |   B   |
310 # +---+-------+
311 #
312 # After:
313 # +---+-------+
314 # | B |   A   |
315 # +---+-------+
316 ###############################################################################
317 $pid = launch_with_config($config);
318
319 $ws = fresh_workspace;
320 $A = open_window(wm_class => 'mark_A');
321 $B = open_window(wm_class => 'mark_B');
322 cmd 'resize grow width 0 or 25 ppt';
323
324 # sanity checks
325 $nodes = get_ws_content($ws);
326 cmp_float($nodes->[0]->{percent}, 0.25, 'A has 25% width');
327 cmp_float($nodes->[1]->{percent}, 0.75, 'B has 75% width');
328
329 cmd '[con_mark=B] swap container with mark A';
330
331 $nodes = get_ws_content($ws);
332 cmp_float($nodes->[0]->{percent}, 0.25, 'B has 25% width');
333 cmp_float($nodes->[1]->{percent}, 0.75, 'A has 75% width');
334
335 exit_gracefully($pid);
336
337 ###############################################################################
338 # Swapping containers not sharing the same parent preserves the geometry of
339 # the container they are swapped with.
340 #
341 # Before:
342 # +---+-----+
343 # | A |     |
344 # +---+  B  |
345 # |   |     |
346 # | Y +-----+
347 # |   |  X  |
348 # +---+-----+
349 #
350 # After:
351 # +---+-----+
352 # | B |     |
353 # +---+  A  |
354 # |   |     |
355 # | Y +-----+
356 # |   |  X  |
357 # +---+-----+
358 ###############################################################################
359 $pid = launch_with_config($config);
360 $ws = fresh_workspace;
361
362 $A = open_window(wm_class => 'mark_A');
363 $B = open_window(wm_class => 'mark_B');
364 cmd 'split v';
365 open_window;
366 cmd 'focus up, resize grow height 0 or 25 ppt';
367 cmd 'focus left, split v';
368 open_window;
369 cmd 'resize grow height 0 or 25 ppt';
370
371 # sanity checks
372 $nodes = get_ws_content($ws);
373 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'A has 25% height');
374 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'B has 75% height');
375
376 cmd '[con_mark=B] swap container with mark A';
377
378 $nodes = get_ws_content($ws);
379 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'B has 25% height');
380 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'A has 75% height');
381
382 exit_gracefully($pid);
383
384 ###############################################################################
385 # Swapping containers moves the urgency hint correctly.
386 ###############################################################################
387 $pid = launch_with_config($config);
388
389 $ws1 = fresh_workspace;
390 $A = open_window(wm_class => 'mark_A');
391 $ws2 = fresh_workspace;
392 $B = open_window(wm_class => 'mark_B');
393 open_window;
394
395 $B->add_hint('urgency');
396 sync_with_i3;
397
398 cmd '[con_mark=B] swap container with mark A';
399
400 @urgent = grep { $_->{urgent} } @{get_ws_content($ws1)};
401 is(@urgent, 1, 'B is marked urgent');
402 is(get_ws($ws1)->{urgent}, 1, 'the first workspace is marked urgent');
403
404 @urgent = grep { $_->{urgent} } @{get_ws_content($ws2)};
405 is(@urgent, 0, 'A is not marked urgent');
406 is(get_ws($ws2)->{urgent}, 0, 'the second workspace is not marked urgent');
407
408 exit_gracefully($pid);
409
410 ###############################################################################
411
412 done_testing;