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} == 2 } @{$__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 (once!) on the next 'scratchpad show' and the
97 # flag will be changed to SCRATCHPAD_CHANGED.
98 ################################################################################
100 my ($nodes, $focus) = get_ws_content($tmp);
101 is(scalar @$nodes, 1, 'precisely one window on current ws');
102 is($nodes->[0]->{scratchpad_state}, 'none', 'scratchpad_state none');
104 cmd 'move scratchpad';
106 $__i3_scratch = get_ws('__i3_scratch');
107 my @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
108 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
109 ($nodes, $focus) = get_ws_content($tmp);
110 is(scalar @$nodes, 0, 'no window on current ws anymore');
112 is($scratch_nodes[0]->{scratchpad_state}, 'fresh', 'scratchpad_state fresh');
114 $tree = $i3->get_tree->recv;
115 my $__i3 = first { $_->{name} eq '__i3' } @{$tree->{nodes}};
116 isnt($tree->{focus}->[0], $__i3->{id}, '__i3 output not focused');
118 $get_outputs = $i3->get_outputs->recv;
119 $get_ws = $i3->get_workspaces->recv;
120 @output_names = map { $_->{name} } @$get_outputs;
121 @ws_names = map { $_->{name} } @$get_ws;
123 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
124 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
126 ################################################################################
127 # 4: Verify that 'scratchpad show' makes the window visible.
128 ################################################################################
130 # Open another window so that we can check if focus is on the scratchpad window
132 my $second_window = open_window;
133 my $old_focus = get_focused($tmp);
135 cmd 'scratchpad show';
137 isnt(get_focused($tmp), $old_focus, 'focus changed');
139 $__i3_scratch = get_ws('__i3_scratch');
140 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
141 is(scalar @scratch_nodes, 0, '__i3_scratch is now empty');
143 my $ws = get_ws($tmp);
144 my $output = $tree->{nodes}->[1];
145 my $scratchrect = $ws->{floating_nodes}->[0]->{rect};
146 my $outputrect = $output->{rect};
148 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
149 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
150 is($scratchrect->{x},
151 ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
152 'scratch window centered horizontally');
153 is($scratchrect->{y},
154 ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
155 'scratch window centered vertically');
157 ################################################################################
158 # 5: Another 'scratchpad show' should make that window go to the scratchpad
160 ################################################################################
162 cmd 'scratchpad show';
164 $__i3_scratch = get_ws('__i3_scratch');
165 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
166 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
168 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'scratchpad_state changed');
170 ################################################################################
171 # 6: Verify that repeated 'scratchpad show' cycle through the stack, that is,
172 # toggling a visible window should insert it at the bottom of the stack of the
173 # __i3_scratch workspace.
174 ################################################################################
176 my $third_window = open_window(name => 'scratch-match');
177 cmd 'move scratchpad';
179 $__i3_scratch = get_ws('__i3_scratch');
180 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
181 is(scalar @scratch_nodes, 2, '__i3_scratch contains both windows');
183 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'changed window first');
184 is($scratch_nodes[1]->{scratchpad_state}, 'fresh', 'fresh window is second');
186 my $changed_id = $scratch_nodes[0]->{nodes}->[0]->{id};
187 my $fresh_id = $scratch_nodes[1]->{nodes}->[0]->{id};
188 is($scratch_nodes[0]->{id}, $__i3_scratch->{focus}->[0], 'changed window first');
189 is($scratch_nodes[1]->{id}, $__i3_scratch->{focus}->[1], 'fresh window second');
191 # Repeatedly use 'scratchpad show' and check that the windows are different.
192 cmd 'scratchpad show';
194 is(get_focused($tmp), $changed_id, 'focus changed');
197 $scratchrect = $ws->{floating_nodes}->[0]->{rect};
198 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
199 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
200 is($scratchrect->{x},
201 ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
202 'scratch window centered horizontally');
203 is($scratchrect->{y},
204 ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
205 'scratch window centered vertically');
207 cmd 'scratchpad show';
209 isnt(get_focused($tmp), $changed_id, 'focus changed');
211 cmd 'scratchpad show';
213 is(get_focused($tmp), $fresh_id, 'focus changed');
215 cmd 'scratchpad show';
217 isnt(get_focused($tmp), $fresh_id, 'focus changed');
219 ################################################################################
220 # 7: Verify that using scratchpad show with criteria works as expected:
221 # When matching a scratchpad window which is visible, it should hide it.
222 # When matching a scratchpad window which is on __i3_scratch, it should show it.
223 # When matching a non-scratchpad window, it should be a no-op.
224 ################################################################################
226 # Verify that using 'scratchpad show' without any matching windows is a no-op.
227 $old_focus = get_focused($tmp);
229 cmd '[title="nomatch"] scratchpad show';
231 is(get_focused($tmp), $old_focus, 'non-matching criteria have no effect');
233 # Verify that we can use criteria to show a scratchpad window.
234 cmd '[title="scratch-match"] scratchpad show';
236 my $scratch_focus = get_focused($tmp);
237 isnt($scratch_focus, $old_focus, 'matching criteria works');
239 cmd '[title="scratch-match"] scratchpad show';
241 isnt(get_focused($tmp), $scratch_focus, 'matching criteria works');
242 is(get_focused($tmp), $old_focus, 'focus restored');
244 # Verify that we cannot use criteria to show a non-scratchpad window.
245 my $tmp2 = fresh_workspace;
246 my $non_scratch_window = open_window(name => 'non-scratch');
247 cmd "workspace $tmp";
248 is(get_focused($tmp), $old_focus, 'focus still ok');
249 cmd '[title="non-match"] scratchpad show';
250 is(get_focused($tmp), $old_focus, 'focus unchanged');
252 ################################################################################
253 # 8: Show it, move it around, hide it. Verify that the position is retained
254 # when showing it again.
255 ################################################################################
257 cmd '[title="scratch-match"] scratchpad show';
259 isnt(get_focused($tmp), $old_focus, 'scratchpad window shown');
261 my $oldrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
265 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
266 isnt($scratchrect->{x}, $oldrect->{x}, 'x position changed');
267 $oldrect = $scratchrect;
269 # hide it, then show it again
270 cmd '[title="scratch-match"] scratchpad show';
271 cmd '[title="scratch-match"] scratchpad show';
273 # verify the position is still the same
274 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
276 is_deeply($scratchrect, $oldrect, 'position/size the same');
278 # hide it again for the next test
279 cmd '[title="scratch-match"] scratchpad show';
281 is(get_focused($tmp), $old_focus, 'scratchpad window hidden');
283 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'precisely one window on current ws');
285 ################################################################################
286 # 9: restart i3 and verify that the scratchpad show still works
287 ################################################################################
289 $__i3_scratch = get_ws('__i3_scratch');
290 my $old_nodes = scalar @{$__i3_scratch->{nodes}};
291 my $old_floating_nodes = scalar @{$__i3_scratch->{floating_nodes}};
297 $__i3_scratch = get_ws('__i3_scratch');
298 is(scalar @{$__i3_scratch->{nodes}}, $old_nodes, "number of nodes matches ($old_nodes)");
299 is(scalar @{$__i3_scratch->{floating_nodes}}, $old_floating_nodes, "number of floating nodes matches ($old_floating_nodes)");
301 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'still precisely one window on current ws');
302 is(scalar @{get_ws($tmp)->{floating_nodes}}, 0, 'still no floating windows on current ws');
304 # verify that we can display the scratchpad window
305 cmd '[title="scratch-match"] scratchpad show';
308 is(scalar @{$ws->{nodes}}, 1, 'still precisely one window on current ws');
309 is(scalar @{$ws->{floating_nodes}}, 1, 'precisely one floating windows on current ws');
310 is($ws->{floating_nodes}->[0]->{scratchpad_state}, 'changed', 'scratchpad_state is "changed"');
312 ################################################################################
313 # 10: on an empty workspace, ensure the 'move scratchpad' command does nothing
314 ################################################################################
316 $tmp = fresh_workspace;
318 cmd 'move scratchpad';
322 ################################################################################
323 # 11: focus a workspace and move all of its children to the scratchpad area
324 ################################################################################
326 sub verify_scratchpad_move_multiple_win {
327 my $floating = shift;
329 my $first = open_window;
330 my $second = open_window;
333 cmd 'floating toggle';
338 cmd 'move scratchpad';
343 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
344 is(scalar @{$ws->{floating_nodes}}, 0, 'no floating windows on ws');
346 # show the first 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 $old_focus = get_focused($tmp);
355 cmd 'scratchpad show';
357 # show the second window.
358 cmd 'scratchpad show';
361 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
362 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
364 isnt(get_focused($tmp), $old_focus, 'focus changed');
367 $tmp = fresh_workspace;
368 verify_scratchpad_move_multiple_win(0);
369 $tmp = fresh_workspace;
370 verify_scratchpad_move_multiple_win(1);
372 # TODO: make i3bar display *something* when a window on the scratchpad has the urgency hint