2 # vim:ts=4:sw=4:expandtab
4 # Tests for the scratchpad functionality.
7 use List::Util qw(first);
9 my $i3 = i3(get_socket_path());
10 my $tmp = fresh_workspace;
12 ################################################################################
13 # 1: Verify that the __i3 output contains the __i3_scratch workspace and that
14 # it’s empty initially. Also, __i3 should not show up in GET_OUTPUTS so that
15 # tools like i3bar will not handle it. Similarly, __i3_scratch should not show
16 # up in GET_WORKSPACES. After all, you should not be able to switch to it.
17 ################################################################################
19 my $tree = $i3->get_tree->recv;
20 is($tree->{name}, 'root', 'root node is the first thing we get');
22 my @__i3 = grep { $_->{name} eq '__i3' } @{$tree->{nodes}};
23 is(scalar @__i3, 1, 'output __i3 found');
25 my $content = first { $_->{type} == 2 } @{$__i3[0]->{nodes}};
26 my @workspaces = @{$content->{nodes}};
27 my @workspace_names = map { $_->{name} } @workspaces;
28 ok('__i3_scratch' ~~ @workspace_names, '__i3_scratch workspace found');
30 my $get_outputs = $i3->get_outputs->recv;
31 my $get_ws = $i3->get_workspaces->recv;
32 my @output_names = map { $_->{name} } @$get_outputs;
33 my @ws_names = map { $_->{name} } @$get_ws;
35 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
36 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
38 ################################################################################
39 # 2: Verify that you cannot switch to the __i3_scratch workspace and moving
40 # windows to __i3_scratch does not work (users should be aware of the different
41 # behavior and acknowledge that by using the scratchpad commands).
42 ################################################################################
44 # Try focusing the workspace.
45 my $__i3_scratch = get_ws('__i3_scratch');
46 is($__i3_scratch->{focused}, 0, '__i3_scratch ws not focused');
48 cmd 'workspace __i3_scratch';
50 $__i3_scratch = get_ws('__i3_scratch');
51 is($__i3_scratch->{focused}, 0, '__i3_scratch ws still not focused');
54 # Try moving a window to it.
55 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
57 my $window = open_window;
58 cmd 'move workspace __i3_scratch';
60 $__i3_scratch = get_ws('__i3_scratch');
61 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
64 # Try moving the window with the 'output <direction>' command.
65 # We hardcode output left since the pseudo-output will be initialized before
66 # every other output, so it will always be the first one.
67 cmd 'move output left';
69 $__i3_scratch = get_ws('__i3_scratch');
70 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
73 # Try moving the window with the 'output <name>' command.
74 cmd 'move output __i3';
76 $__i3_scratch = get_ws('__i3_scratch');
77 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
80 ################################################################################
81 # 3: Verify that 'scratchpad toggle' sends a window to the __i3_scratch
82 # workspace and sets the scratchpad flag to SCRATCHPAD_FRESH. The window’s size
83 # and position will be changed (once!) on the next 'scratchpad show' and the
84 # flag will be changed to SCRATCHPAD_CHANGED.
85 ################################################################################
87 my ($nodes, $focus) = get_ws_content($tmp);
88 is(scalar @$nodes, 1, 'precisely one window on current ws');
89 is($nodes->[0]->{scratchpad_state}, 'none', 'scratchpad_state none');
91 cmd 'move scratchpad';
93 $__i3_scratch = get_ws('__i3_scratch');
94 my @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
95 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
96 ($nodes, $focus) = get_ws_content($tmp);
97 is(scalar @$nodes, 0, 'no window on current ws anymore');
99 is($scratch_nodes[0]->{scratchpad_state}, 'fresh', 'scratchpad_state fresh');
101 $tree = $i3->get_tree->recv;
102 my $__i3 = first { $_->{name} eq '__i3' } @{$tree->{nodes}};
103 isnt($tree->{focus}->[0], $__i3->{id}, '__i3 output not focused');
105 $get_outputs = $i3->get_outputs->recv;
106 $get_ws = $i3->get_workspaces->recv;
107 @output_names = map { $_->{name} } @$get_outputs;
108 @ws_names = map { $_->{name} } @$get_ws;
110 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
111 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
113 ################################################################################
114 # 4: Verify that 'scratchpad show' makes the window visible.
115 ################################################################################
117 # Open another window so that we can check if focus is on the scratchpad window
119 my $second_window = open_window;
120 my $old_focus = get_focused($tmp);
122 cmd 'scratchpad show';
124 isnt(get_focused($tmp), $old_focus, 'focus changed');
126 $__i3_scratch = get_ws('__i3_scratch');
127 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
128 is(scalar @scratch_nodes, 0, '__i3_scratch is now empty');
130 my $ws = get_ws($tmp);
131 my $output = $tree->{nodes}->[1];
132 my $scratchrect = $ws->{floating_nodes}->[0]->{rect};
133 my $outputrect = $output->{rect};
135 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
136 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
137 is($scratchrect->{x},
138 ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
139 'scratch window centered horizontally');
140 is($scratchrect->{y},
141 ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
142 'scratch window centered vertically');
144 ################################################################################
145 # 5: Another 'scratchpad show' should make that window go to the scratchpad
147 ################################################################################
149 cmd 'scratchpad show';
151 $__i3_scratch = get_ws('__i3_scratch');
152 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
153 is(scalar @scratch_nodes, 1, '__i3_scratch contains our window');
155 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'scratchpad_state changed');
157 ################################################################################
158 # 6: Verify that repeated 'scratchpad show' cycle through the stack, that is,
159 # toggling a visible window should insert it at the bottom of the stack of the
160 # __i3_scratch workspace.
161 ################################################################################
163 my $third_window = open_window(name => 'scratch-match');
164 cmd 'move scratchpad';
166 $__i3_scratch = get_ws('__i3_scratch');
167 @scratch_nodes = @{$__i3_scratch->{floating_nodes}};
168 is(scalar @scratch_nodes, 2, '__i3_scratch contains both windows');
170 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'changed window first');
171 is($scratch_nodes[1]->{scratchpad_state}, 'fresh', 'fresh window is second');
173 my $changed_id = $scratch_nodes[0]->{nodes}->[0]->{id};
174 my $fresh_id = $scratch_nodes[1]->{nodes}->[0]->{id};
175 is($scratch_nodes[0]->{id}, $__i3_scratch->{focus}->[0], 'changed window first');
176 is($scratch_nodes[1]->{id}, $__i3_scratch->{focus}->[1], 'fresh window second');
178 # Repeatedly use 'scratchpad show' and check that the windows are different.
179 cmd 'scratchpad show';
181 is(get_focused($tmp), $changed_id, 'focus changed');
184 $scratchrect = $ws->{floating_nodes}->[0]->{rect};
185 is($scratchrect->{width}, $outputrect->{width} * 0.5, 'scratch width is 50%');
186 is($scratchrect->{height}, $outputrect->{height} * 0.75, 'scratch height is 75%');
187 is($scratchrect->{x},
188 ($outputrect->{width} / 2) - ($scratchrect->{width} / 2),
189 'scratch window centered horizontally');
190 is($scratchrect->{y},
191 ($outputrect->{height} / 2 ) - ($scratchrect->{height} / 2),
192 'scratch window centered vertically');
194 cmd 'scratchpad show';
196 isnt(get_focused($tmp), $changed_id, 'focus changed');
198 cmd 'scratchpad show';
200 is(get_focused($tmp), $fresh_id, 'focus changed');
202 cmd 'scratchpad show';
204 isnt(get_focused($tmp), $fresh_id, 'focus changed');
206 ################################################################################
207 # 7: Verify that using scratchpad show with criteria works as expected:
208 # When matching a scratchpad window which is visible, it should hide it.
209 # When matching a scratchpad window which is on __i3_scratch, it should show it.
210 # When matching a non-scratchpad window, it should be a no-op.
211 ################################################################################
213 # Verify that using 'scratchpad show' without any matching windows is a no-op.
214 $old_focus = get_focused($tmp);
216 cmd '[title="nomatch"] scratchpad show';
218 is(get_focused($tmp), $old_focus, 'non-matching criteria have no effect');
220 # Verify that we can use criteria to show a scratchpad window.
221 cmd '[title="scratch-match"] scratchpad show';
223 my $scratch_focus = get_focused($tmp);
224 isnt($scratch_focus, $old_focus, 'matching criteria works');
226 cmd '[title="scratch-match"] scratchpad show';
228 isnt(get_focused($tmp), $scratch_focus, 'matching criteria works');
229 is(get_focused($tmp), $old_focus, 'focus restored');
231 # Verify that we cannot use criteria to show a non-scratchpad window.
232 my $tmp2 = fresh_workspace;
233 my $non_scratch_window = open_window(name => 'non-scratch');
234 cmd "workspace $tmp";
235 is(get_focused($tmp), $old_focus, 'focus still ok');
236 cmd '[title="non-match"] scratchpad show';
237 is(get_focused($tmp), $old_focus, 'focus unchanged');
239 ################################################################################
240 # 8: Show it, move it around, hide it. Verify that the position is retained
241 # when showing it again.
242 ################################################################################
244 cmd '[title="scratch-match"] scratchpad show';
246 isnt(get_focused($tmp), $old_focus, 'scratchpad window shown');
248 my $oldrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
252 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
253 isnt($scratchrect->{x}, $oldrect->{x}, 'x position changed');
254 $oldrect = $scratchrect;
256 # hide it, then show it again
257 cmd '[title="scratch-match"] scratchpad show';
258 cmd '[title="scratch-match"] scratchpad show';
260 # verify the position is still the same
261 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
263 is_deeply($scratchrect, $oldrect, 'position/size the same');
265 # hide it again for the next test
266 cmd '[title="scratch-match"] scratchpad show';
268 is(get_focused($tmp), $old_focus, 'scratchpad window hidden');
270 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'precisely one window on current ws');
272 ################################################################################
273 # 9: restart i3 and verify that the scratchpad show still works
274 ################################################################################
276 $__i3_scratch = get_ws('__i3_scratch');
277 my $old_nodes = scalar @{$__i3_scratch->{nodes}};
278 my $old_floating_nodes = scalar @{$__i3_scratch->{floating_nodes}};
285 $__i3_scratch = get_ws('__i3_scratch');
286 is(scalar @{$__i3_scratch->{nodes}}, $old_nodes, "number of nodes matches ($old_nodes)");
287 is(scalar @{$__i3_scratch->{floating_nodes}}, $old_floating_nodes, "number of floating nodes matches ($old_floating_nodes)");
289 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'still precisely one window on current ws');
290 is(scalar @{get_ws($tmp)->{floating_nodes}}, 0, 'still no floating windows on current ws');
292 # verify that we can display the scratchpad window
293 cmd '[title="scratch-match"] scratchpad show';
296 is(scalar @{$ws->{nodes}}, 1, 'still precisely one window on current ws');
297 is(scalar @{$ws->{floating_nodes}}, 1, 'precisely one floating windows on current ws');
298 is($ws->{floating_nodes}->[0]->{scratchpad_state}, 'changed', 'scratchpad_state is "changed"');
300 ################################################################################
301 # 10: on an empty workspace, ensure the 'move scratchpad' command does nothing
302 ################################################################################
304 $tmp = fresh_workspace;
306 cmd 'move scratchpad';
310 ################################################################################
311 # 11: focus a workspace and move all of its children to the scratchpad area
312 ################################################################################
314 $tmp = fresh_workspace;
316 my $first = open_window;
317 my $second = open_window;
320 cmd 'move scratchpad';
325 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
326 is(scalar @{$ws->{floating_nodes}}, 0, 'no floating windows on ws');
328 # show the first window.
329 cmd 'scratchpad show';
332 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
333 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
335 $old_focus = get_focused($tmp);
337 cmd 'scratchpad show';
339 # show the second window.
340 cmd 'scratchpad show';
343 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
344 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
346 isnt(get_focused($tmp), $old_focus, 'focus changed');
348 # TODO: make i3bar display *something* when a window on the scratchpad has the urgency hint