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