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 for the scratchpad functionality.
20 use List::Util qw(first);
22 my $i3 = i3(get_socket_path());
23 my $tmp = fresh_workspace;
25 ################################################################################
26 # 1: Verify that the __i3 output contains the __i3_scratch workspace and that
27 # it’s empty initially. Also, __i3 should not show up in GET_OUTPUTS so that
28 # tools like i3bar will not handle it. Similarly, __i3_scratch should not show
29 # up in GET_WORKSPACES. After all, you should not be able to switch to it.
30 ################################################################################
32 my $tree = $i3->get_tree->recv;
33 is($tree->{name}, 'root', 'root node is the first thing we get');
35 my @__i3 = grep { $_->{name} eq '__i3' } @{$tree->{nodes}};
36 is(scalar @__i3, 1, 'output __i3 found');
38 my $content = first { $_->{type} eq 'con' } @{$__i3[0]->{nodes}};
39 my @workspaces = @{$content->{nodes}};
40 my @workspace_names = map { $_->{name} } @workspaces;
41 ok('__i3_scratch' ~~ @workspace_names, '__i3_scratch workspace found');
43 my $get_outputs = $i3->get_outputs->recv;
44 my $get_ws = $i3->get_workspaces->recv;
45 my @output_names = map { $_->{name} } @$get_outputs;
46 my @ws_names = map { $_->{name} } @$get_ws;
48 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
49 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
51 ################################################################################
52 # 2: Verify that you cannot switch to the __i3_scratch workspace and moving
53 # windows to __i3_scratch does not work (users should be aware of the different
54 # behavior and acknowledge that by using the scratchpad commands).
55 ################################################################################
57 # Try focusing the workspace.
58 my $__i3_scratch = get_ws('__i3_scratch');
59 is($__i3_scratch->{focused}, 0, '__i3_scratch ws not focused');
61 cmd 'workspace __i3_scratch';
63 $__i3_scratch = get_ws('__i3_scratch');
64 is($__i3_scratch->{focused}, 0, '__i3_scratch ws still not focused');
67 # Try moving a window to it.
68 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
70 my $window = open_window;
71 cmd 'move workspace __i3_scratch';
73 $__i3_scratch = get_ws('__i3_scratch');
74 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
77 # Try moving the window with the 'output <direction>' command.
78 # We hardcode output left since the pseudo-output will be initialized before
79 # every other output, so it will always be the first one.
80 cmd 'move output left';
82 $__i3_scratch = get_ws('__i3_scratch');
83 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
86 # Try moving the window with the 'output <name>' command.
87 cmd 'move output __i3';
89 $__i3_scratch = get_ws('__i3_scratch');
90 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
93 ################################################################################
94 # 3: Verify that 'scratchpad toggle' sends a window to the __i3_scratch
95 # workspace and sets the scratchpad flag to SCRATCHPAD_FRESH. The window’s size
96 # and position will be changed on the next 'scratchpad show'.
97 ################################################################################
99 my ($nodes, $focus) = get_ws_content($tmp);
100 is(scalar @$nodes, 1, 'precisely one window on current ws');
101 is($nodes->[0]->{scratchpad_state}, 'none', 'scratchpad_state none');
103 cmd 'move scratchpad';
105 $__i3_scratch = get_ws('__i3_scratch');
106 my @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
107 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
108 ($nodes, $focus) = get_ws_content($tmp);
109 is(scalar @$nodes, 0, 'no window on current ws anymore');
111 is($scratch_nodes[0]->{scratchpad_state}, 'fresh', 'scratchpad_state fresh');
113 $tree = $i3->get_tree->recv;
114 my $__i3 = first { $_->{name} eq '__i3' } @{$tree->{nodes}};
115 isnt($tree->{focus}->[0], $__i3->{id}, '__i3 output not focused');
117 $get_outputs = $i3->get_outputs->recv;
118 $get_ws = $i3->get_workspaces->recv;
119 @output_names = map { $_->{name} } @$get_outputs;
120 @ws_names = map { $_->{name} } @$get_ws;
122 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
123 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
125 ################################################################################
126 # 4: Verify that 'scratchpad show' makes the window visible.
127 ################################################################################
129 # Open another window so that we can check if focus is on the scratchpad window
131 my $second_window = open_window;
132 my $old_focus = get_focused($tmp);
134 cmd 'scratchpad show';
136 isnt(get_focused($tmp), $old_focus, 'focus changed');
138 $__i3_scratch = get_ws('__i3_scratch');
139 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
140 is(scalar @scratch_nodes, 0, '__i3_scratch is now empty');
142 my $ws = get_ws($tmp);
143 my $output = $tree->{nodes}->[1];
144 my $scratchrect = $ws->{floating_nodes}->[0]->{rect};
145 my $outputrect = $output->{rect};
147 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
148 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
149 is($scratchrect->{x},
150 ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
151 'scratch window centered horizontally');
152 is($scratchrect->{y},
153 ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
154 'scratch window centered vertically');
156 ################################################################################
157 # 5: Another 'scratchpad show' should make that window go to the scratchpad
159 ################################################################################
161 cmd 'scratchpad show';
163 $__i3_scratch = get_ws('__i3_scratch');
164 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
165 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
167 ################################################################################
168 # 6: Resizing the window should disable auto centering on scratchpad show
169 ################################################################################
171 cmd 'scratchpad show';
174 is($ws->{floating_nodes}->[0]->{scratchpad_state}, 'fresh',
175 'scratchpad_state fresh');
177 cmd 'resize grow width 10 px';
178 cmd 'scratchpad show';
179 cmd 'scratchpad show';
182 $scratchrect = $ws->{floating_nodes}->[0]->{rect};
183 $outputrect = $output->{rect};
185 is($ws->{floating_nodes}->[0]->{scratchpad_state}, 'changed',
186 'scratchpad_state changed');
187 is($scratchrect->{width}, $outputrect->{width} * 0.5 + 10, 'scratch width is 50% + 10px');
189 cmd 'resize shrink width 10 px';
190 cmd 'scratchpad show';
192 ################################################################################
193 # 7: Verify that repeated 'scratchpad show' cycle through the stack, that is,
194 # toggling a visible window should insert it at the bottom of the stack of the
195 # __i3_scratch workspace.
196 ################################################################################
198 my $third_window = open_window(name => 'scratch-match');
199 cmd 'move scratchpad';
201 $__i3_scratch = get_ws('__i3_scratch');
202 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
203 is(scalar @scratch_nodes, 2, '__i3_scratch contains both windows');
205 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'changed window first');
206 is($scratch_nodes[1]->{scratchpad_state}, 'fresh', 'fresh window is second');
208 my $changed_id = $scratch_nodes[0]->{nodes}->[0]->{id};
209 my $fresh_id = $scratch_nodes[1]->{nodes}->[0]->{id};
210 is($scratch_nodes[0]->{id}, $__i3_scratch->{focus}->[0], 'changed window first');
211 is($scratch_nodes[1]->{id}, $__i3_scratch->{focus}->[1], 'fresh window second');
213 # Repeatedly use 'scratchpad show' and check that the windows are different.
214 cmd 'scratchpad show';
216 is(get_focused($tmp), $changed_id, 'focus changed');
219 $scratchrect = $ws->{floating_nodes}->[0]->{rect};
220 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
221 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
222 is($scratchrect->{x},
223 ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
224 'scratch window centered horizontally');
225 is($scratchrect->{y},
226 ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
227 'scratch window centered vertically');
229 cmd 'scratchpad show';
231 isnt(get_focused($tmp), $changed_id, 'focus changed');
233 cmd 'scratchpad show';
235 is(get_focused($tmp), $fresh_id, 'focus changed');
237 cmd 'scratchpad show';
239 isnt(get_focused($tmp), $fresh_id, 'focus changed');
241 ################################################################################
242 # 8: Show it, move it around, hide it. Verify that the position is retained
243 # when showing it again.
244 ################################################################################
246 cmd '[title="scratch-match"] scratchpad show';
248 isnt(get_focused($tmp), $old_focus, 'scratchpad window shown');
250 my $oldrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
254 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
255 isnt($scratchrect->{x}, $oldrect->{x}, 'x position changed');
256 $oldrect = $scratchrect;
258 # hide it, then show it again
259 cmd '[title="scratch-match"] scratchpad show';
260 cmd '[title="scratch-match"] scratchpad show';
262 # verify the position is still the same
263 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
265 is_deeply($scratchrect, $oldrect, 'position/size the same');
267 # hide it again for the next test
268 cmd '[title="scratch-match"] scratchpad show';
270 is(get_focused($tmp), $old_focus, 'scratchpad window hidden');
272 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'precisely one window on current ws');
274 ################################################################################
275 # 9: restart i3 and verify that the scratchpad show still works
276 ################################################################################
278 $__i3_scratch = get_ws('__i3_scratch');
279 my $old_nodes = scalar @{$__i3_scratch->{nodes}};
280 my $old_floating_nodes = scalar @{$__i3_scratch->{floating_nodes}};
286 $__i3_scratch = get_ws('__i3_scratch');
287 is(scalar @{$__i3_scratch->{nodes}}, $old_nodes, "number of nodes matches ($old_nodes)");
288 is(scalar @{$__i3_scratch->{floating_nodes}}, $old_floating_nodes, "number of floating nodes matches ($old_floating_nodes)");
290 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'still precisely one window on current ws');
291 is(scalar @{get_ws($tmp)->{floating_nodes}}, 0, 'still no floating windows on current ws');
293 # verify that we can display the scratchpad window
294 cmd '[title="scratch-match"] scratchpad show';
297 is(scalar @{$ws->{nodes}}, 1, 'still precisely one window on current ws');
298 is(scalar @{$ws->{floating_nodes}}, 1, 'precisely one floating windows on current ws');
299 is($ws->{floating_nodes}->[0]->{scratchpad_state}, 'changed', 'scratchpad_state is "changed"');
301 ################################################################################
302 # 10: on an empty workspace, ensure the 'move scratchpad' command does nothing
303 ################################################################################
305 $tmp = fresh_workspace;
307 cmd 'move scratchpad';
311 ################################################################################
312 # 11: focus a workspace and move all of its children to the scratchpad area
313 ################################################################################
315 sub verify_scratchpad_move_multiple_win {
316 my $floating = shift;
318 my $first = open_window;
319 my $second = open_window;
322 cmd 'floating toggle';
327 cmd 'move scratchpad';
332 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
333 is(scalar @{$ws->{floating_nodes}}, 0, 'no floating windows on ws');
335 # show the first window.
336 cmd 'scratchpad show';
339 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
340 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
342 $old_focus = get_focused($tmp);
344 cmd 'scratchpad show';
346 # show the second window.
347 cmd 'scratchpad show';
350 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
351 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
353 isnt(get_focused($tmp), $old_focus, 'focus changed');
356 $tmp = fresh_workspace;
357 verify_scratchpad_move_multiple_win(0);
358 $tmp = fresh_workspace;
359 verify_scratchpad_move_multiple_win(1);
361 ################################################################################
362 # 12: open a scratchpad window on a workspace, switch to another workspace and
363 # call 'scratchpad show' again
364 ################################################################################
366 sub verify_scratchpad_move_with_visible_scratch_con {
367 my ($first, $second, $cross_output) = @_;
369 cmd "workspace $first";
371 my $window1 = open_window;
372 cmd 'move scratchpad';
374 my $window2 = open_window;
375 cmd 'move scratchpad';
377 # this should bring up window 1
378 cmd 'scratchpad show';
380 my $ws = get_ws($first);
381 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating node on ws1');
382 is($x->input_focus, $window1->id, "showed the correct scratchpad window1");
384 # this should bring up window 1
385 cmd "workspace $second";
386 cmd 'scratchpad show';
387 is($x->input_focus, $window1->id, "showed the correct scratchpad window1");
389 my $ws2 = get_ws($second);
390 is(scalar @{$ws2->{floating_nodes}}, 1, 'one floating node on ws2');
391 unless ($cross_output) {
392 ok(!workspace_exists($first), 'ws1 was empty and therefore closed');
394 $ws = get_ws($first);
395 is(scalar @{$ws->{floating_nodes}}, 0, 'ws1 has no floating nodes');
398 # hide window 1 again
399 cmd 'move scratchpad';
401 # this should bring up window 2
402 cmd "workspace $first";
403 cmd 'scratchpad show';
404 is($x->input_focus, $window2->id, "showed the correct scratchpad window");
407 # let's clear the scratchpad first
408 sub clear_scratchpad {
409 while (scalar @{get_ws('__i3_scratch')->{floating_nodes}}) {
410 cmd 'scratchpad show';
416 is (scalar @{get_ws('__i3_scratch')->{floating_nodes}}, 0, "scratchpad is empty");
418 my ($first, $second);
419 $first = fresh_workspace;
420 $second = fresh_workspace;
422 verify_scratchpad_move_with_visible_scratch_con($first, $second, 0);
426 ################################################################################
427 # 13: Test whether scratchpad show moves focus to the scratchpad window
428 # when another window on the same workspace has focus
429 ################################################################################
432 my $ws = fresh_workspace;
435 my $scratch = get_focused($ws);
436 cmd 'move scratchpad';
437 cmd 'scratchpad show';
440 my $not_scratch = get_focused($ws);
441 is(get_focused($ws), $not_scratch, 'not scratch window has focus');
443 cmd 'scratchpad show';
445 is(get_focused($ws), $scratch, 'scratchpad is focused');
447 # TODO: make i3bar display *something* when a window on the scratchpad has the urgency hint
449 ################################################################################
450 # 14: Verify that 'move scratchpad' sends floating containers to scratchpad but
451 # does not resize/resposition the container on the next 'scratchpad show', i.e.,
452 # i3 sets the scratchpad flag to SCRATCHPAD_CHANGED
453 ################################################################################
456 $tmp = fresh_workspace;
459 ($nodes, $focus) = get_ws_content($tmp);
460 is(scalar @$nodes, 1, 'precisely one window on current ws');
461 is($nodes->[0]->{scratchpad_state}, 'none', 'scratchpad_state none');
463 cmd 'floating toggle';
464 cmd 'move scratchpad';
466 $__i3_scratch = get_ws('__i3_scratch');
467 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
468 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
469 ($nodes, $focus) = get_ws_content($tmp);
470 is(scalar @$nodes, 0, 'no window on current ws anymore');
472 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'scratchpad_state changed');