]> git.sur5r.net Git - i3/i3/blob - testcases/t/132-move-workspace.t
60705f96d4de586998724d1270f77d6c3721e4ca
[i3/i3] / testcases / t / 132-move-workspace.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 # Checks if the 'move [window/container] to workspace' command works correctly
18 #
19 use i3test;
20
21 my $i3 = i3(get_socket_path());
22
23 # We move the pointer out of our way to avoid a bug where the focus will
24 # be set to the window under the cursor
25 sync_with_i3;
26 $x->root->warp_pointer(0, 0);
27 sync_with_i3;
28
29 sub move_workspace_test {
30     my ($movecmd) = @_;
31
32     my $tmp = get_unused_workspace();
33     my $tmp2 = get_unused_workspace();
34     cmd "workspace $tmp";
35
36     is_num_children($tmp, 0, 'no containers yet');
37
38     my $first = open_empty_con($i3);
39     my $second = open_empty_con($i3);
40     is_num_children($tmp, 2, 'two containers on first ws');
41
42     cmd "workspace $tmp2";
43     is_num_children($tmp2, 0, 'no containers on second ws yet');
44
45     cmd "workspace $tmp";
46
47     cmd "$movecmd $tmp2";
48     is_num_children($tmp, 1, 'one container on first ws anymore');
49     is_num_children($tmp2, 1, 'one container on second ws');
50     my ($nodes, $focus) = get_ws_content($tmp2);
51
52     is($focus->[0], $second, 'same container on different ws');
53
54     ($nodes, $focus) = get_ws_content($tmp);
55     ok($nodes->[0]->{focused}, 'first container focused on first ws');
56 }
57
58 move_workspace_test('move workspace');  # supported for legacy reasons
59 move_workspace_test('move to workspace');
60 # Those are just synonyms and more verbose ways of saying the same thing:
61 move_workspace_test('move window to workspace');
62 move_workspace_test('move container to workspace');
63
64 ################################################################################
65 # Check that 'move to workspace number <number>' works to move a window to
66 # named workspaces which start with <number>.
67 ################################################################################
68
69 cmd 'workspace 13: meh';
70 cmd 'open';
71 is_num_children('13: meh', 1, 'one container on 13: meh');
72
73 ok(!workspace_exists('13'), 'workspace 13 does not exist yet');
74
75 cmd 'workspace 12';
76 cmd 'open';
77
78 cmd 'move to workspace number 13';
79 is_num_children('13: meh', 2, 'one container on 13: meh');
80 is_num_children('12', 0, 'no container on 12 anymore');
81
82 ok(!workspace_exists('13'), 'workspace 13 does still not exist');
83
84 ################################################################################
85 # Check that 'move to workspace number <number><name>' works to move a window to
86 # named workspaces which start with <number>.
87 ################################################################################
88
89 cmd 'workspace 15: meh';
90 cmd 'open';
91 is_num_children('15: meh', 1, 'one container on 15: meh');
92
93 ok(!workspace_exists('15'), 'workspace 15 does not exist yet');
94 ok(!workspace_exists('15: duh'), 'workspace 15: duh does not exist yet');
95
96 cmd 'workspace 14';
97 cmd 'open';
98
99 cmd 'move to workspace number 15: duh';
100 is_num_children('15: meh', 2, 'two containers on 15: meh');
101 is_num_children('14', 0, 'no container on 14 anymore');
102
103 ok(!workspace_exists('15'), 'workspace 15 does still not exist');
104 ok(!workspace_exists('15: duh'), 'workspace 15 does still not exist');
105
106 ###################################################################
107 # check if 'move workspace next' and 'move workspace prev' work
108 ###################################################################
109
110 # Open two containers on the first workspace, one container on the second
111 # workspace. Because the workspaces are named, they will be sorted by order of
112 # creation.
113 my $tmp = get_unused_workspace();
114 my $tmp2 = get_unused_workspace();
115 cmd "workspace $tmp";
116 is_num_children($tmp, 0, 'no containers yet');
117 my $first = open_empty_con($i3);
118 my $second = open_empty_con($i3);
119 is_num_children($tmp, 2, 'two containers');
120
121 cmd "workspace $tmp2";
122 is_num_children($tmp2, 0, 'no containers yet');
123 my $third = open_empty_con($i3);
124 is_num_children($tmp2, 1, 'one container on second ws');
125
126 # go back to the first workspace, move one of the containers to the next one
127 cmd "workspace $tmp";
128 cmd 'move workspace next';
129 is_num_children($tmp, 1, 'one container on first ws');
130 is_num_children($tmp2, 2, 'two containers on second ws');
131
132 # go to the second workspace and move two containers to the first one
133 cmd "workspace $tmp2";
134 cmd 'move workspace prev';
135 cmd 'move workspace prev';
136 is_num_children($tmp, 3, 'three containers on first ws');
137 is_num_children($tmp2, 0, 'no containers on second ws');
138
139 ###################################################################
140 # check if 'move workspace current' works
141 ###################################################################
142
143 $tmp = get_unused_workspace();
144 $tmp2 = get_unused_workspace();
145
146 cmd "workspace $tmp";
147 $first = open_window(name => 'win-name');
148 is_num_children($tmp, 1, 'one container on first ws');
149
150 cmd "workspace $tmp2";
151 is_num_children($tmp2, 0, 'no containers yet');
152
153 cmd qq|[title="win-name"] move workspace $tmp2|;
154 is_num_children($tmp2, 1, 'one container on second ws');
155
156 cmd qq|[title="win-name"] move workspace $tmp|;
157 is_num_children($tmp2, 0, 'no containers on second ws');
158
159 ###################################################################
160 # check if floating cons are moved to new workspaces properly
161 # (that is, if they are floating on the target ws, too)
162 ###################################################################
163
164 $tmp = get_unused_workspace();
165 $tmp2 = get_unused_workspace();
166 cmd "workspace $tmp";
167
168 cmd "open";
169 cmd "floating toggle";
170
171 my $ws = get_ws($tmp);
172 is(@{$ws->{nodes}}, 0, 'no nodes on workspace');
173 is(@{$ws->{floating_nodes}}, 1, 'one floating node on workspace');
174
175 cmd "move workspace $tmp2";
176
177 $ws = get_ws($tmp2);
178 is(@{$ws->{nodes}}, 0, 'no nodes on workspace');
179 is(@{$ws->{floating_nodes}}, 1, 'one floating node on workspace');
180
181 ################################################################################
182 # Check that 'move workspace number' works correctly.
183 ################################################################################
184
185 $tmp = get_unused_workspace();
186 cmd 'open';
187
188 cmd 'workspace 16';
189 cmd 'open';
190 is_num_children('16', 1, 'one node on ws 16');
191
192 cmd "workspace $tmp";
193 cmd 'open';
194 cmd 'move workspace number 16';
195 is_num_children('16', 2, 'two nodes on ws 16');
196
197 ok(!workspace_exists('17'), 'workspace 17 does not exist yet');
198 cmd 'open';
199 cmd 'move workspace number 17';
200 ok(workspace_exists('17'), 'workspace 17 created by moving');
201 is(@{get_ws('17')->{nodes}}, 1, 'one node on ws 16');
202
203 ################################################################################
204 # The following four tests verify the various 'move workspace' commands when
205 # the selection is itself a workspace.
206 ################################################################################
207
208 # borrowed from 122-split.t
209 # recursively sums up all nodes and their children
210 sub sum_nodes {
211     my ($nodes) = @_;
212
213     return 0 if !@{$nodes};
214
215     my @children = (map { @{$_->{nodes}} } @{$nodes},
216                     map { @{$_->{'floating_nodes'}} } @{$nodes});
217
218     return @{$nodes} + sum_nodes(\@children);
219 }
220
221 ############################################################
222 # move workspace 'next|prev'
223 ############################################################
224 $tmp = get_unused_workspace();
225 $tmp2 = get_unused_workspace();
226
227 cmd "workspace $tmp";
228 cmd 'open';
229 is_num_children($tmp, 1, 'one container on first ws');
230
231 cmd "workspace $tmp2";
232 cmd 'open';
233 is_num_children($tmp2, 1, 'one container on second ws');
234 cmd 'open';
235 is_num_children($tmp2, 2, 'two containers on second ws');
236
237 cmd 'focus parent';
238 cmd 'move workspace prev';
239
240 is_num_children($tmp, 2, 'two child containers on first ws');
241 is(sum_nodes(get_ws_content($tmp)), 4, 'four total containers on first ws');
242 is_num_children($tmp2, 0, 'no containers on second ws');
243
244 ############################################################
245 # move workspace current
246 # This is a special case that should be a no-op.
247 ############################################################
248 $tmp = fresh_workspace();
249
250 cmd 'open';
251 is_num_children($tmp, 1, 'one container on first ws');
252 my $tmpcount = sum_nodes(get_ws_content($tmp));
253
254 cmd 'focus parent';
255 cmd "move workspace $tmp";
256
257 is(sum_nodes(get_ws_content($tmp)), $tmpcount, 'number of containers in first ws unchanged');
258
259 ############################################################
260 # move workspace '<name>'
261 ############################################################
262 $tmp2 = get_unused_workspace();
263 $tmp = fresh_workspace();
264
265 cmd 'open';
266 is_num_children($tmp, 1, 'one container on first ws');
267
268 cmd "workspace $tmp2";
269 cmd 'open';
270 is_num_children($tmp2, 1, 'one container on second ws');
271 cmd 'open';
272 is_num_children($tmp2, 2, 'two containers on second ws');
273
274 cmd 'focus parent';
275 cmd "move workspace $tmp";
276
277 is_num_children($tmp, 2, 'two child containers on first ws');
278 is(sum_nodes(get_ws_content($tmp)), 4, 'four total containers on first ws');
279 is_num_children($tmp2, 0, 'no containers on second ws');
280
281 ############################################################
282 # move workspace number '<number>'
283 ############################################################
284 cmd 'workspace 18';
285 cmd 'open';
286 is_num_children('18', 1, 'one container on ws 18');
287
288 cmd 'workspace 19';
289 cmd 'open';
290 is_num_children('19', 1, 'one container on ws 19');
291 cmd 'open';
292 is_num_children('19', 2, 'two containers on ws 19');
293
294 cmd 'focus parent';
295 cmd 'move workspace number 18';
296
297 is_num_children('18', 2, 'two child containers on ws 18');
298 is(sum_nodes(get_ws_content('18')), 4, 'four total containers on ws 18');
299 is_num_children('19', 0, 'no containers on ws 19');
300
301 ###################################################################
302 # move workspace '<name>' with a floating child
303 ###################################################################
304 $tmp2 = get_unused_workspace();
305 $tmp = fresh_workspace();
306 cmd 'open';
307 cmd 'floating toggle';
308 cmd 'open';
309 cmd 'floating toggle';
310 cmd 'open';
311
312 $ws = get_ws($tmp);
313 is_num_children($tmp, 1, 'one container on first workspace');
314 is(@{$ws->{floating_nodes}}, 2, 'two floating nodes on first workspace');
315
316 cmd 'focus parent';
317 cmd "move workspace $tmp2";
318
319 $ws = get_ws($tmp2);
320 is_num_children($tmp2, 1, 'one container on second workspace');
321 is(@{$ws->{floating_nodes}}, 2, 'two floating nodes on second workspace');
322
323 ###################################################################
324 # same as the above, but with only floating children
325 ###################################################################
326 $tmp2 = get_unused_workspace();
327 $tmp = fresh_workspace();
328 cmd 'open';
329 cmd 'floating toggle';
330
331 $ws = get_ws($tmp);
332 is_num_children($tmp, 0, 'no regular nodes on first workspace');
333 is(@{$ws->{floating_nodes}}, 1, 'one floating node on first workspace');
334
335 cmd 'focus parent';
336 cmd "move workspace $tmp2";
337
338 $ws = get_ws($tmp2);
339 is_num_children($tmp2, 0, 'no regular nodes on second workspace');
340 is(@{$ws->{floating_nodes}}, 1, 'one floating node on second workspace');
341
342 ###################################################################
343 # Check that moving an empty workspace using criteria doesn't
344 # create unfocused empty workspace.
345 ###################################################################
346 $tmp2 = get_unused_workspace();
347 $tmp = fresh_workspace();
348 cmd 'mark a';
349 cmd "[con_mark=a] move to workspace $tmp2";
350
351 is (get_ws($tmp2), undef, 'No empty workspace created');
352
353 done_testing;