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_config => <<EOT;
21 font font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
23 for_window[class="mark_A"] mark A
24 for_window[class="mark_B"] mark B
27 my ($ws, $ws1, $ws2, $ws3);
28 my ($nodes, $expected_focus, $A, $B, $F);
32 ###############################################################################
33 # Invalid con_id should not crash i3
35 ###############################################################################
37 $ws = fresh_workspace;
40 cmd "swap container with con_id 1";
45 ###############################################################################
46 # Swap 2 windows in different workspaces using con_id
47 ###############################################################################
49 $ws = fresh_workspace;
51 $A = get_focused($ws);
53 $ws = fresh_workspace;
56 cmd "swap container with con_id $A";
57 is(get_focused($ws), $A, 'A is now focused');
61 ###############################################################################
62 # Swap two containers next to each other.
63 # Focus should stay on B because both windows are on the focused workspace.
64 # The focused container is B.
66 # +---+---+ Layout: H1[ A B ]
67 # | A | B | Focus Stacks:
69 ###############################################################################
70 $ws = fresh_workspace;
72 $A = open_window(wm_class => 'mark_A');
73 $B = open_window(wm_class => 'mark_B');
74 $expected_focus = get_focused($ws);
76 cmd '[con_mark=B] swap container with mark A';
78 $nodes = get_ws_content($ws);
79 is($nodes->[0]->{window}, $B->{id}, 'B is on the left');
80 is($nodes->[1]->{window}, $A->{id}, 'A is on the right');
81 is(get_focused($ws), $expected_focus, 'B is still focused');
85 ###############################################################################
86 # Swap two containers with different parents.
87 # In this test, the focus head of the left v-split container is A.
88 # The focused container is B.
90 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
91 # | A | X | Focus Stacks:
92 # +---+---+ H1: V2, V1
95 ###############################################################################
96 $ws = fresh_workspace;
98 $A = open_window(wm_class => 'mark_A');
99 $B = open_window(wm_class => 'mark_B');
102 cmd 'move up, focus left';
105 cmd 'focus up, focus right, focus down';
106 $expected_focus = get_focused($ws);
108 cmd '[con_mark=B] swap container with mark A';
110 $nodes = get_ws_content($ws);
111 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
112 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
113 is(get_focused($ws), $expected_focus, 'B is still focused');
117 ###############################################################################
118 # Swap two containers with different parents.
119 # In this test, the focus head of the left v-split container is _not_ A.
120 # The focused container is B.
122 # +---+---+ Layout: H1[ V1[ A Y ] V2[ X B ] ]
123 # | A | X | Focus Stacks:
124 # +---+---+ H1: V2, V1
127 ###############################################################################
128 $ws = fresh_workspace;
130 $A = open_window(wm_class => 'mark_A');
131 $B = open_window(wm_class => 'mark_B');
134 cmd 'move up, focus left';
137 cmd 'focus right, focus down';
138 $expected_focus = get_focused($ws);
140 cmd '[con_mark=B] swap container with mark A';
142 $nodes = get_ws_content($ws);
143 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
144 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
145 is(get_focused($ws), $expected_focus, 'B is still focused');
149 ###############################################################################
150 # Swap two containers with one being on a different workspace.
151 # The focused container is B.
153 # Layout: O1[ W1[ H1 ] W2[ H2 ] ]
157 # +---+---+ Layout: H1[ A X ]
158 # | A | X | Focus Stacks:
161 # +---+---+ Layout: H2[ Y, B ]
162 # | Y | B | Focus Stacks:
164 ###############################################################################
165 $ws1 = fresh_workspace;
166 $A = open_window(wm_class => 'mark_A');
167 $expected_focus = get_focused($ws1);
171 $ws2 = fresh_workspace;
173 $B = open_window(wm_class => 'mark_B');
175 cmd '[con_mark=B] swap container with mark A';
177 $nodes = get_ws_content($ws1);
178 is($nodes->[0]->{window}, $B->{id}, 'B is on ws1:left');
180 $nodes = get_ws_content($ws2);
181 is($nodes->[1]->{window}, $A->{id}, 'A is on ws1:right');
182 is(get_focused($ws2), $expected_focus, 'A is focused');
186 ###############################################################################
187 # Swap two non-focused containers within the same workspace.
189 # +---+---+ Layout: H1[ V1[ A X ] V2[ F B ] ]
190 # | A | F | Focus Stacks:
191 # +---+---+ H1: V2, V1
194 ###############################################################################
195 $ws = fresh_workspace;
197 $A = open_window(wm_class => 'mark_A');
198 $B = open_window(wm_class => 'mark_B');
201 cmd 'move up, focus left';
204 cmd 'focus up, focus right';
205 $expected_focus = get_focused($ws);
207 cmd '[con_mark=B] swap container with mark A';
209 $nodes = get_ws_content($ws);
210 is($nodes->[0]->{nodes}->[0]->{window}, $B->{id}, 'B is on the top left');
211 is($nodes->[1]->{nodes}->[1]->{window}, $A->{id}, 'A is on the bottom right');
212 is(get_focused($ws), $expected_focus, 'F is still focused');
216 ###############################################################################
217 # Swap two non-focused containers which are both on different workspaces.
219 # Layout: O1[ W1[ A ] W2[ B ] W3[ F ] ]
234 ###############################################################################
235 $ws1 = fresh_workspace;
236 $A = open_window(wm_class => 'mark_A');
238 $ws2 = fresh_workspace;
239 $B = open_window(wm_class => 'mark_B');
241 $ws3 = fresh_workspace;
243 $expected_focus = get_focused($ws3);
245 cmd '[con_mark=B] swap container with mark A';
247 $nodes = get_ws_content($ws1);
248 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
250 $nodes = get_ws_content($ws2);
251 is($nodes->[0]->{window}, $A->{id}, 'A is on the second workspace');
253 is(get_focused($ws3), $expected_focus, 'F is still focused');
257 ###############################################################################
258 # Swap two non-focused containers with one being on a different workspace.
260 # Layout: O1[ W1[ A ] W2[ H2 ] ]
268 # +---+---+ Layout: H2[ B, F ]
269 # | B | F | Focus Stacks:
271 ###############################################################################
273 $ws1 = fresh_workspace;
274 $A = open_window(wm_class => 'mark_A');
276 $ws2 = fresh_workspace;
277 $B = open_window(wm_class => 'mark_B');
279 $expected_focus = get_focused($ws2);
281 cmd '[con_mark=B] swap container with mark A';
283 $nodes = get_ws_content($ws1);
284 is($nodes->[0]->{window}, $B->{id}, 'B is on the first workspace');
286 $nodes = get_ws_content($ws2);
287 is($nodes->[0]->{window}, $A->{id}, 'A is on the left of the second workspace');
288 is(get_focused($ws2), $expected_focus, 'F is still focused');
292 ###############################################################################
293 # 1. A container cannot be swapped with its parent.
294 # 2. A container cannot be swapped with one of its children.
297 # +---+---+ Layout: H1[ X V1[ Y B ] ]
298 # | | Y | (with A := V1)
302 ###############################################################################
303 $ws = fresh_workspace;
307 $B = open_window(wm_class => 'mark_B');
308 cmd 'focus parent, mark A, focus child';
310 $result = cmd '[con_mark=B] swap container with mark A';
311 is($result->[0]->{success}, 0, 'B cannot be swappd with its parent');
313 $result = cmd '[con_mark=A] swap container with mark B';
314 is($result->[0]->{success}, 0, 'A cannot be swappd with one of its children');
318 ###############################################################################
319 # Swapping two containers preserves the geometry of the container they are
320 # being swapped with.
331 ###############################################################################
332 $ws = fresh_workspace;
333 $A = open_window(wm_class => 'mark_A');
334 $B = open_window(wm_class => 'mark_B');
335 cmd 'resize grow width 0 or 25 ppt';
338 $nodes = get_ws_content($ws);
339 cmp_float($nodes->[0]->{percent}, 0.25, 'A has 25% width');
340 cmp_float($nodes->[1]->{percent}, 0.75, 'B has 75% width');
342 cmd '[con_mark=B] swap container with mark A';
344 $nodes = get_ws_content($ws);
345 cmp_float($nodes->[0]->{percent}, 0.25, 'B has 25% width');
346 cmp_float($nodes->[1]->{percent}, 0.75, 'A has 75% width');
350 ###############################################################################
351 # Swapping containers not sharing the same parent preserves the geometry of
352 # the container they are swapped with.
371 ###############################################################################
372 $ws = fresh_workspace;
374 $A = open_window(wm_class => 'mark_A');
375 $B = open_window(wm_class => 'mark_B');
378 cmd 'focus up, resize grow height 0 or 25 ppt';
379 cmd 'focus left, split v';
381 cmd 'resize grow height 0 or 25 ppt';
384 $nodes = get_ws_content($ws);
385 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'A has 25% height');
386 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'B has 75% height');
388 cmd '[con_mark=B] swap container with mark A';
390 $nodes = get_ws_content($ws);
391 cmp_float($nodes->[0]->{nodes}->[0]->{percent}, 0.25, 'B has 25% height');
392 cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.75, 'A has 75% height');
396 ###############################################################################
397 # Swapping containers moves the urgency hint correctly.
398 ###############################################################################
400 $ws1 = fresh_workspace;
401 $A = open_window(wm_class => 'mark_A');
402 $ws2 = fresh_workspace;
403 $B = open_window(wm_class => 'mark_B');
406 $B->add_hint('urgency');
409 cmd '[con_mark=B] swap container with mark A';
411 @urgent = grep { $_->{urgent} } @{get_ws_content($ws1)};
412 is(@urgent, 1, 'B is marked urgent');
413 is(get_ws($ws1)->{urgent}, 1, 'the first workspace is marked urgent');
415 @urgent = grep { $_->{urgent} } @{get_ws_content($ws2)};
416 is(@urgent, 0, 'A is not marked urgent');
417 is(get_ws($ws2)->{urgent}, 0, 'the second workspace is not marked urgent');
421 ###############################################################################