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_autostart => 0;
23 font font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
25 for_window[class="mark_A"] mark A
26 for_window[class="mark_B"] mark B
30 my ($ws, $ws1, $ws2, $ws3);
31 my ($nodes, $expected_focus, $A, $B, $F);
35 $pid = launch_with_config($config);
37 ###############################################################################
38 # Invalid con_id should not crash i3
40 ###############################################################################
42 $ws = fresh_workspace;
45 cmd "swap container with con_id 1";
50 ###############################################################################
51 # Swap 2 windows in different workspaces using con_id
52 ###############################################################################
54 $ws = fresh_workspace;
56 $A = get_focused($ws);
58 $ws = fresh_workspace;
61 cmd "swap container with con_id $A";
62 is(get_focused($ws), $A, 'A is now focused');
66 ###############################################################################
67 # Swap two containers next to each other.
68 # Focus should stay on B because both windows are on the focused workspace.
69 # The focused container is B.
71 # +---+---+ Layout: H1[ A B ]
72 # | A | B | Focus Stacks:
74 ###############################################################################
75 $ws = fresh_workspace;
77 $A = open_window(wm_class => 'mark_A');
78 $B = open_window(wm_class => 'mark_B');
79 $expected_focus = get_focused($ws);
81 cmd '[con_mark=B] swap container with mark A';
83 $nodes = get_ws_content($ws);
84 is($nodes->[0]->{window}, $B->{id}, 'B is on the left');
85 is($nodes->[1]->{window}, $A->{id}, 'A is on the right');
86 is(get_focused($ws), $expected_focus, 'B is still focused');
90 ###############################################################################
91 # Swap two containers with different parents.
92 # In this test, the focus head of the left v-split container is A.
93 # The focused container is B.
95 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
96 # | A | X | Focus Stacks:
97 # +---+---+ H1: V2, V1
100 ###############################################################################
101 $ws = fresh_workspace;
103 $A = open_window(wm_class => 'mark_A');
104 $B = open_window(wm_class => 'mark_B');
107 cmd 'move up, focus left';
110 cmd 'focus up, focus right, focus down';
111 $expected_focus = get_focused($ws);
113 cmd '[con_mark=B] swap container with mark A';
115 $nodes = get_ws_content($ws);
116 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
117 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
118 is(get_focused($ws), $expected_focus, 'B is still focused');
122 ###############################################################################
123 # Swap two containers with different parents.
124 # In this test, the focus head of the left v-split container is _not_ A.
125 # The focused container is B.
127 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
128 # | A | X | Focus Stacks:
129 # +---+---+ H1: V2, V1
132 ###############################################################################
133 $ws = fresh_workspace;
135 $A = open_window(wm_class => 'mark_A');
136 $B = open_window(wm_class => 'mark_B');
139 cmd 'move up, focus left';
142 cmd 'focus right, focus down';
143 $expected_focus = get_focused($ws);
145 cmd '[con_mark=B] swap container with mark A';
147 $nodes = get_ws_content($ws);
148 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
149 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
150 is(get_focused($ws), $expected_focus, 'B is still focused');
154 ###############################################################################
155 # Swap two containers with one being on a different workspace.
156 # The focused container is B.
158 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
162 # +---+---+ Layout: H1[ A X ]
163 # | A | X | Focus Stacks:
166 # +---+---+ Layout: H2[ Y, B ]
167 # | Y | B | Focus Stacks:
169 ###############################################################################
170 $ws1 = fresh_workspace;
171 $A = open_window(wm_class => 'mark_A');
172 $expected_focus = get_focused($ws1);
176 $ws2 = fresh_workspace;
178 $B = open_window(wm_class => 'mark_B');
180 cmd '[con_mark=B] swap container with mark A';
182 $nodes = get_ws_content($ws1);
183 is($nodes->[0]->{window}, $B->{id}, 'B is on ws1:left');
185 $nodes = get_ws_content($ws2);
186 is($nodes->[1]->{window}, $A->{id}, 'A is on ws1:right');
187 is(get_focused($ws2), $expected_focus, 'A is focused');
191 ###############################################################################
192 # Swap two non-focused containers within the same workspace.
194 # +---+---+ Layout: H1[ V1[ A X ] V2[ F B ] ]
195 # | A | F | Focus Stacks:
196 # +---+---+ H1: V2, V1
199 ###############################################################################
200 $ws = fresh_workspace;
202 $A = open_window(wm_class => 'mark_A');
203 $B = open_window(wm_class => 'mark_B');
206 cmd 'move up, focus left';
209 cmd 'focus up, focus right';
210 $expected_focus = get_focused($ws);
212 cmd '[con_mark=B] swap container with mark A';
214 $nodes = get_ws_content($ws);
215 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
216 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
217 is(get_focused($ws), $expected_focus, 'F is still focused');
221 ###############################################################################
222 # Swap two non-focused containers which are both on different workspaces.
224 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
239 ###############################################################################
240 $ws1 = fresh_workspace;
241 $A = open_window(wm_class => 'mark_A');
243 $ws2 = fresh_workspace;
244 $B = open_window(wm_class => 'mark_B');
246 $ws3 = fresh_workspace;
248 $expected_focus = get_focused($ws3);
250 cmd '[con_mark=B] swap container with mark A';
252 $nodes = get_ws_content($ws1);
253 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
255 $nodes = get_ws_content($ws2);
256 is($nodes->[0]->{window}, $A->{id}, 'A is on the second workspace');
258 is(get_focused($ws3), $expected_focus, 'F is still focused');
262 ###############################################################################
263 # Swap two non-focused containers with one being on a different workspace.
265 # Layout: O1[ W1[ A ] W2[ H2 ] ]
273 # +---+---+ Layout: H2[ B, F ]
274 # | B | F | Focus Stacks:
276 ###############################################################################
278 $ws1 = fresh_workspace;
279 $A = open_window(wm_class => 'mark_A');
281 $ws2 = fresh_workspace;
282 $B = open_window(wm_class => 'mark_B');
284 $expected_focus = get_focused($ws2);
286 cmd '[con_mark=B] swap container with mark A';
288 $nodes = get_ws_content($ws1);
289 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
291 $nodes = get_ws_content($ws2);
292 is($nodes->[0]->{window}, $A->{id}, 'A is on the left of the second workspace');
293 is(get_focused($ws2), $expected_focus, 'F is still focused');
297 ###############################################################################
298 # 1. A container cannot be swapped with its parent.
299 # 2. A container cannot be swapped with one of its children.
302 # +---+---+ Layout: H1[ X V1[ Y B ] ]
303 # | | Y | (with A := V1)
307 ###############################################################################
308 $ws = fresh_workspace;
312 $B = open_window(wm_class => 'mark_B');
313 cmd 'focus parent, mark A, focus child';
315 $result = cmd '[con_mark=B] swap container with mark A';
316 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
318 $result = cmd '[con_mark=A] swap container with mark B';
319 is($result->[0]->{success}, 0, 'A cannot be swappd with one of its children');
323 ###############################################################################
324 # Swapping two containers preserves the geometry of the container they are
325 # being swapped with.
336 ###############################################################################
337 $ws = fresh_workspace;
338 $A = open_window(wm_class => 'mark_A');
339 $B = open_window(wm_class => 'mark_B');
340 cmd 'resize grow width 0 or 25 ppt';
343 $nodes = get_ws_content($ws);
344 cmp_float($nodes->[0]->{percent}, 0.25, 'A has 25% width');
345 cmp_float($nodes->[1]->{percent}, 0.75, 'B has 75% width');
347 cmd '[con_mark=B] swap container with mark A';
349 $nodes = get_ws_content($ws);
350 cmp_float($nodes->[0]->{percent}, 0.25, 'B has 25% width');
351 cmp_float($nodes->[1]->{percent}, 0.75, 'A has 75% width');
355 ###############################################################################
356 # Swapping containers not sharing the same parent preserves the geometry of
357 # the container they are swapped with.
376 ###############################################################################
377 $ws = fresh_workspace;
379 $A = open_window(wm_class => 'mark_A');
380 $B = open_window(wm_class => 'mark_B');
383 cmd 'focus up, resize grow height 0 or 25 ppt';
384 cmd 'focus left, split v';
386 cmd 'resize grow height 0 or 25 ppt';
389 $nodes = get_ws_content($ws);
390 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'A has 25% height');
391 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'B has 75% height');
393 cmd '[con_mark=B] swap container with mark A';
395 $nodes = get_ws_content($ws);
396 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'B has 25% height');
397 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'A has 75% height');
401 ###############################################################################
402 # Swapping containers moves the urgency hint correctly.
403 ###############################################################################
405 $ws1 = fresh_workspace;
406 $A = open_window(wm_class => 'mark_A');
407 $ws2 = fresh_workspace;
408 $B = open_window(wm_class => 'mark_B');
411 $B->add_hint('urgency');
414 cmd '[con_mark=B] swap container with mark A';
416 @urgent = grep { $_->{urgent} } @{get_ws_content($ws1)};
417 is(@urgent, 1, 'B is marked urgent');
418 is(get_ws($ws1)->{urgent}, 1, 'the first workspace is marked urgent');
420 @urgent = grep { $_->{urgent} } @{get_ws_content($ws2)};
421 is(@urgent, 0, 'A is not marked urgent');
422 is(get_ws($ws2)->{urgent}, 0, 'the second workspace is not marked urgent');
426 ###############################################################################
428 exit_gracefully($pid);