]> git.sur5r.net Git - i3/i3/blob - testcases/t/185-scratchpad.t
bugfix: don't use con_is_internal
[i3/i3] / testcases / t / 185-scratchpad.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 # Tests for the scratchpad functionality.
18 #
19 use i3test;
20 use List::Util qw(first);
21
22 my $i3 = i3(get_socket_path());
23 my $tmp = fresh_workspace;
24
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 ################################################################################
31
32 my $tree = $i3->get_tree->recv;
33 is($tree->{name}, 'root', 'root node is the first thing we get');
34
35 my @__i3 = grep { $_->{name} eq '__i3' } @{$tree->{nodes}};
36 is(scalar @__i3, 1, 'output __i3 found');
37
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');
42
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;
47
48 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
49 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
50
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 ################################################################################
56
57 # Try focusing the workspace.
58 my $__i3_scratch = get_ws('__i3_scratch');
59 is($__i3_scratch->{focused}, 0, '__i3_scratch ws not focused');
60
61 cmd 'workspace __i3_scratch';
62
63 $__i3_scratch = get_ws('__i3_scratch');
64 is($__i3_scratch->{focused}, 0, '__i3_scratch ws still not focused');
65
66
67 # Try moving a window to it.
68 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
69
70 my $window = open_window;
71 cmd 'move workspace __i3_scratch';
72
73 $__i3_scratch = get_ws('__i3_scratch');
74 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
75
76
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';
81
82 $__i3_scratch = get_ws('__i3_scratch');
83 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
84
85
86 # Try moving the window with the 'output <name>' command.
87 cmd 'move output __i3';
88
89 $__i3_scratch = get_ws('__i3_scratch');
90 is(scalar @{$__i3_scratch->{floating_nodes}}, 0, '__i3_scratch ws empty');
91
92
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 ################################################################################
99
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');
103
104 cmd 'move scratchpad';
105
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');
111
112 is($scratch_nodes[0]->{scratchpad_state}, 'fresh', 'scratchpad_state fresh');
113
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');
117
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;
122
123 ok(!('__i3' ~~ @output_names), '__i3 not in GET_OUTPUTS');
124 ok(!('__i3_scratch' ~~ @ws_names), '__i3_scratch ws not in GET_WORKSPACES');
125
126 ################################################################################
127 # 4: Verify that 'scratchpad show' makes the window visible.
128 ################################################################################
129
130 # Open another window so that we can check if focus is on the scratchpad window
131 # after showing it.
132 my $second_window = open_window;
133 my $old_focus = get_focused($tmp);
134
135 cmd 'scratchpad show';
136
137 isnt(get_focused($tmp), $old_focus, 'focus changed');
138
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');
142
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};
147
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');
156
157 ################################################################################
158 # 5: Another 'scratchpad show' should make that window go to the scratchpad
159 # again.
160 ################################################################################
161
162 cmd 'scratchpad show';
163
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');
167
168 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'scratchpad_state changed');
169
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 ################################################################################
175
176 my $third_window = open_window(name => 'scratch-match');
177 cmd 'move scratchpad';
178
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');
182
183 is($scratch_nodes[0]->{scratchpad_state}, 'changed', 'changed window first');
184 is($scratch_nodes[1]->{scratchpad_state}, 'fresh', 'fresh window is second');
185
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');
190
191 # Repeatedly use 'scratchpad show' and check that the windows are different.
192 cmd 'scratchpad show';
193
194 is(get_focused($tmp), $changed_id, 'focus changed');
195
196 $ws = get_ws($tmp);
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');
206
207 cmd 'scratchpad show';
208
209 isnt(get_focused($tmp), $changed_id, 'focus changed');
210
211 cmd 'scratchpad show';
212
213 is(get_focused($tmp), $fresh_id, 'focus changed');
214
215 cmd 'scratchpad show';
216
217 isnt(get_focused($tmp), $fresh_id, 'focus changed');
218
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 ################################################################################
225
226 # Verify that using 'scratchpad show' without any matching windows is a no-op.
227 $old_focus = get_focused($tmp);
228
229 cmd '[title="nomatch"] scratchpad show';
230
231 is(get_focused($tmp), $old_focus, 'non-matching criteria have no effect');
232
233 # Verify that we can use criteria to show a scratchpad window.
234 cmd '[title="scratch-match"] scratchpad show';
235
236 my $scratch_focus = get_focused($tmp);
237 isnt($scratch_focus, $old_focus, 'matching criteria works');
238
239 cmd '[title="scratch-match"] scratchpad show';
240
241 isnt(get_focused($tmp), $scratch_focus, 'matching criteria works');
242 is(get_focused($tmp), $old_focus, 'focus restored');
243
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');
251
252 ################################################################################
253 # 8: Show it, move it around, hide it. Verify that the position is retained
254 # when showing it again.
255 ################################################################################
256
257 cmd '[title="scratch-match"] scratchpad show';
258
259 isnt(get_focused($tmp), $old_focus, 'scratchpad window shown');
260
261 my $oldrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
262
263 cmd 'move left';
264
265 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
266 isnt($scratchrect->{x}, $oldrect->{x}, 'x position changed');
267 $oldrect = $scratchrect;
268
269 # hide it, then show it again
270 cmd '[title="scratch-match"] scratchpad show';
271 cmd '[title="scratch-match"] scratchpad show';
272
273 # verify the position is still the same
274 $scratchrect = get_ws($tmp)->{floating_nodes}->[0]->{rect};
275
276 is_deeply($scratchrect, $oldrect, 'position/size the same');
277
278 # hide it again for the next test
279 cmd '[title="scratch-match"] scratchpad show';
280
281 is(get_focused($tmp), $old_focus, 'scratchpad window hidden');
282
283 is(scalar @{get_ws($tmp)->{nodes}}, 1, 'precisely one window on current ws');
284
285 ################################################################################
286 # 9: restart i3 and verify that the scratchpad show still works
287 ################################################################################
288
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}};
292
293 cmd 'restart';
294
295 does_i3_live;
296
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)");
300
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');
303
304 # verify that we can display the scratchpad window
305 cmd '[title="scratch-match"] scratchpad show';
306
307 $ws = get_ws($tmp);
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"');
311
312 ################################################################################
313 # 10: on an empty workspace, ensure the 'move scratchpad' command does nothing
314 ################################################################################
315
316 $tmp = fresh_workspace;
317
318 cmd 'move scratchpad';
319
320 does_i3_live;
321
322 ################################################################################
323 # 11: focus a workspace and move all of its children to the scratchpad area
324 ################################################################################
325
326 $tmp = fresh_workspace;
327
328 my $first = open_window;
329 my $second = open_window;
330
331 cmd 'focus parent';
332 cmd 'move scratchpad';
333
334 does_i3_live;
335
336 $ws = get_ws($tmp);
337 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
338 is(scalar @{$ws->{floating_nodes}}, 0, 'no floating windows on ws');
339
340 # show the first window.
341 cmd 'scratchpad show';
342
343 $ws = get_ws($tmp);
344 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
345 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
346
347 $old_focus = get_focused($tmp);
348
349 cmd 'scratchpad show';
350
351 # show the second window.
352 cmd 'scratchpad show';
353
354 $ws = get_ws($tmp);
355 is(scalar @{$ws->{nodes}}, 0, 'no windows on ws');
356 is(scalar @{$ws->{floating_nodes}}, 1, 'one floating windows on ws');
357
358 isnt(get_focused($tmp), $old_focus, 'focus changed');
359
360 # TODO: make i3bar display *something* when a window on the scratchpad has the urgency hint
361
362 done_testing;