]> git.sur5r.net Git - i3/i3/blob - testcases/t/135-floating-focus.t
Add testcases for toggling floating windows from different workspaces
[i3/i3] / testcases / t / 135-floating-focus.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Please read the following documents before working on tests:
5 # • https://build.i3wm.org/docs/testsuite.html
6 #   (or docs/testsuite)
7 #
8 # • https://build.i3wm.org/docs/lib-i3test.html
9 #   (alternatively: perldoc ./testcases/lib/i3test.pm)
10 #
11 # • https://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 use i3test;
18
19 my $tmp = fresh_workspace;
20
21 #############################################################################
22 # 1: see if focus stays the same when toggling tiling/floating mode
23 #############################################################################
24
25 my $first = open_window;
26 my $second = open_window;
27
28 is($x->input_focus, $second->id, 'second window focused');
29
30 cmd 'floating enable';
31 cmd 'floating disable';
32
33 is($x->input_focus, $second->id, 'second window still focused after mode toggle');
34
35 #############################################################################
36 # 2: see if focus stays on the current floating window if killing another
37 # floating window
38 #############################################################################
39
40 $tmp = fresh_workspace;
41
42 $first = open_window;    # window 2
43 $second = open_window;   # window 3
44 my $third = open_window; # window 4
45
46 is($x->input_focus, $third->id, 'last container focused');
47
48 cmd 'floating enable';
49
50 cmd '[id="' . $second->id . '"] focus';
51
52 is($x->input_focus, $second->id, 'second con focused');
53
54 cmd 'floating enable';
55
56 # now kill the third one (it's floating). focus should stay unchanged
57 cmd '[id="' . $third->id . '"] kill';
58
59 wait_for_unmap($third);
60
61 is($x->input_focus, $second->id, 'second con still focused after killing third');
62
63
64 #############################################################################
65 # 3: see if the focus gets reverted correctly when closing floating clients
66 # (first to the next floating client, then to the last focused tiling client)
67 #############################################################################
68
69 $tmp = fresh_workspace;
70
71 $first = open_window({ background_color => '#ff0000' });    # window 5
72 $second = open_window({ background_color => '#00ff00' });   # window 6
73 $third = open_window({ background_color => '#0000ff' }); # window 7
74
75 is($x->input_focus, $third->id, 'last container focused');
76
77 cmd 'floating enable';
78
79 cmd '[id="' . $second->id . '"] focus';
80
81 is($x->input_focus, $second->id, 'second con focused');
82
83 cmd 'floating enable';
84
85 # now kill the second one. focus should fall back to the third one, which is
86 # also floating
87 cmd 'kill';
88 wait_for_unmap($second);
89
90 is($x->input_focus, $third->id, 'third con focused');
91
92 cmd 'kill';
93 wait_for_unmap($third);
94
95 is($x->input_focus, $first->id, 'first con focused after killing all floating cons');
96
97 #############################################################################
98 # 4: same test as 3, but with another split con
99 #############################################################################
100
101 $tmp = fresh_workspace;
102
103 $first = open_window({ background_color => '#ff0000' });    # window 5
104 cmd 'split v';
105 cmd 'layout stacked';
106 $second = open_window({ background_color => '#00ff00' });   # window 6
107 $third = open_window({ background_color => '#0000ff' }); # window 7
108
109 is($x->input_focus, $third->id, 'last container focused');
110
111 cmd 'floating enable';
112
113 cmd '[id="' . $second->id . '"] focus';
114
115 is($x->input_focus, $second->id, 'second con focused');
116
117 cmd 'floating enable';
118
119 sync_with_i3;
120
121 # now kill the second one. focus should fall back to the third one, which is
122 # also floating
123 cmd 'kill';
124 wait_for_unmap($second);
125
126 is($x->input_focus, $third->id, 'third con focused');
127
128 cmd 'kill';
129 wait_for_unmap($third);
130
131 is($x->input_focus, $first->id, 'first con focused after killing all floating cons');
132
133 #############################################################################
134 # 5: see if the 'focus tiling' and 'focus floating' commands work
135 #############################################################################
136
137 $tmp = fresh_workspace;
138
139 $first = open_window({ background_color => '#ff0000' });    # window 8
140 $second = open_window({ background_color => '#00ff00' });   # window 9
141
142 is($x->input_focus, $second->id, 'second container focused');
143
144 cmd 'floating enable';
145
146 is($x->input_focus, $second->id, 'second container focused');
147
148 cmd 'focus tiling';
149
150 is($x->input_focus, $first->id, 'first (tiling) container focused');
151
152 cmd 'focus floating';
153
154 is($x->input_focus, $second->id, 'second (floating) container focused');
155
156 cmd 'focus floating';
157
158 is($x->input_focus, $second->id, 'second (floating) container still focused');
159
160 cmd 'focus mode_toggle';
161
162 is($x->input_focus, $first->id, 'first (tiling) container focused');
163
164 cmd 'focus mode_toggle';
165
166 is($x->input_focus, $second->id, 'second (floating) container focused');
167
168 #############################################################################
169 # 6: see if switching floating focus using the focus left/right command works
170 #############################################################################
171
172 $tmp = fresh_workspace;
173
174 $first = open_floating_window({ background_color => '#ff0000' });# window 10
175 $second = open_floating_window({ background_color => '#00ff00' }); # window 11
176 $third = open_floating_window({ background_color => '#0000ff' }); # window 12
177
178 is($x->input_focus, $third->id, 'third container focused');
179
180 cmd 'focus left';
181
182 is($x->input_focus, $second->id, 'second container focused');
183
184 cmd 'focus left';
185
186 is($x->input_focus, $first->id, 'first container focused');
187
188 cmd 'focus left';
189
190 is($x->input_focus, $third->id, 'focus wrapped to third container');
191
192 cmd 'focus right';
193
194 is($x->input_focus, $first->id, 'focus wrapped to first container');
195
196 cmd 'focus right';
197
198 is($x->input_focus, $second->id, 'focus on second container');
199
200 #############################################################################
201 # 7: verify that focusing the parent of a window inside a floating con goes
202 # up to the grandparent (workspace) and that focusing child from the ws
203 # goes back down to the child of the floating con
204 #############################################################################
205
206 $tmp = fresh_workspace;
207
208 my $tiled = open_window;
209 my $floating = open_floating_window;
210 is($x->input_focus, $floating->id, 'floating window focused');
211
212 cmd 'focus parent';
213
214 is(get_ws($tmp)->{focused}, 1, 'workspace is focused');
215 cmd 'focus child';
216
217 is($x->input_focus, $floating->id, 'floating window focused');
218
219 #############################################################################
220 # 8: verify that focusing a floating window raises it to the top.
221 # This test can't verify that the floating container is visually on top, just
222 # that it is placed on the tail of the floating_head.
223 # See issue: 2572
224 #############################################################################
225
226 $tmp = fresh_workspace;
227
228 $first = open_floating_window;
229 $second = open_floating_window;
230
231 is($x->input_focus, $second->id, 'second floating window focused');
232 my $ws = get_ws($tmp);
233 is($ws->{floating_nodes}->[1]->{nodes}->[0]->{window}, $second->id, 'second on top');
234 is($ws->{floating_nodes}->[0]->{nodes}->[0]->{window}, $first->id, 'first behind');
235
236 cmd '[id=' . $first->id . '] focus';
237
238 is($x->input_focus, $first->id, 'first floating window focused');
239 $ws = get_ws($tmp);
240 is($ws->{floating_nodes}->[1]->{nodes}->[0]->{window}, $first->id, 'first on top');
241 is($ws->{floating_nodes}->[0]->{nodes}->[0]->{window}, $second->id, 'second behind');
242
243 #############################################################################
244 # 9: verify that disabling / enabling floating for a window from a different
245 # workspace maintains the correct focus order.
246 #############################################################################
247
248 sub open_window_helper {
249     my $floating = shift if @_;
250     if ($floating){
251         return open_floating_window;
252     }
253     else {
254         return open_window;
255     }
256 }
257
258 for my $floating (0, 1){
259     $tmp = fresh_workspace;
260     $first = open_window;
261     $second = open_window_helper($floating);
262     is($x->input_focus, $second->id, "second window focused");
263
264     fresh_workspace;
265     cmd "[id=" . $second->id . "] floating toggle";
266     cmd "workspace $tmp";
267     sync_with_i3;
268
269     my $workspace = get_ws($tmp);
270     is($workspace->{floating_nodes}->[0]->{nodes}->[0]->{window}, $second->id, 'second window on first workspace, floating') unless $floating;
271     is($workspace->{nodes}->[1]->{window}, $second->id, 'second window on first workspace, right') unless !$floating;
272     is($x->input_focus, $second->id, 'second window still focused');
273 }
274
275 #############################################################################
276 # 10: verify that toggling floating for an unfocused window on another
277 # workspace doesn't make it focused.
278 #############################################################################
279
280 for my $floating (0, 1){
281     $tmp = fresh_workspace;
282     $first = open_window_helper($floating);
283     $second = open_window;
284     is($x->input_focus, $second->id, 'second (tiling) window focused');
285
286     fresh_workspace;
287     cmd "[id=" . $first->id . "] floating toggle";
288     cmd "workspace $tmp";
289     sync_with_i3;
290
291     my $workspace = get_ws($tmp);
292     is($workspace->{floating_nodes}->[0]->{nodes}->[0]->{window}, $first->id, 'first window on first workspace, floating') unless $floating;
293     is($workspace->{nodes}->[1]->{window}, $first->id, 'first window on first workspace, right') unless !$floating;
294     is($x->input_focus, $second->id, 'second window still focused');
295 }
296
297 #############################################################################
298 # 11: verify that toggling floating for a focused window on another workspace
299 # which has another, unfocused floating window maintains the focus of the
300 # first window.
301 #############################################################################
302 for my $floating (0, 1){
303     $tmp = fresh_workspace;
304     $first = open_window;
305     $second = open_floating_window;
306     is($x->input_focus, $second->id, 'second (floating) window focused');
307     $third = open_window_helper($floating);
308     is($x->input_focus, $third->id, "third (floating = $floating) window focused");
309
310     fresh_workspace;
311     cmd "[id=" . $third->id . "] floating toggle";
312     cmd "workspace $tmp";
313     sync_with_i3;
314
315     my $workspace = get_ws($tmp);
316     is($workspace->{floating_nodes}->[0]->{nodes}->[0]->{window}, $third->id, 'third window on first workspace, floating') unless $floating;
317     is($workspace->{nodes}->[1]->{window}, $third->id, 'third window on first workspace, right') unless !$floating;
318     is($x->input_focus, $third->id, 'third window still focused');
319 }
320
321 #############################################################################
322 # 12: verify that toggling floating for an unfocused window on another
323 # workspace which has another, focused floating window doesn't change focus.
324 #############################################################################
325
326 for my $floating (0, 1){
327     $tmp = fresh_workspace;
328     $first = open_window;
329     $second = open_window_helper($floating);
330     is($x->input_focus, $second->id, "second (floating = $floating) window focused");
331     $third = open_floating_window;
332     is($x->input_focus, $third->id, 'third (floating) window focused');
333
334     fresh_workspace;
335     cmd "[id=" . $second->id . "] floating toggle";
336     cmd "workspace $tmp";
337     sync_with_i3;
338
339     my $workspace = get_ws($tmp);
340     is($workspace->{floating_nodes}->[0]->{nodes}->[0]->{window}, $second->id, 'second window on first workspace, floating') unless $floating;
341     is($workspace->{nodes}->[1]->{window}, $second->id, 'second window on first workspace, right') unless !$floating;
342     is($x->input_focus, $third->id, 'third window still focused');
343 }
344
345 #############################################################################
346 # 13: For layout [H1 [A V1[ B F ] ] ] verify that toggling F's floating
347 # mode maintains its focus.
348 #############################################################################
349
350 for my $floating (0, 1){
351     $tmp = fresh_workspace;
352     $first = open_window;
353     $second = open_window;
354     cmd "split v";
355     sync_with_i3;
356     is($x->input_focus, $second->id, "second (floating = $floating) window focused");
357     $third = open_window_helper($floating);
358     is($x->input_focus, $third->id, 'third (floating) window focused');
359
360     fresh_workspace;
361     cmd "[id=" . $third->id . "] floating toggle";
362     cmd "workspace $tmp";
363     sync_with_i3;
364
365     my $workspace = get_ws($tmp);
366     is($workspace->{floating_nodes}->[0]->{nodes}->[0]->{window}, $third->id, 'third window on first workspace, floating') unless $floating;
367     is($workspace->{nodes}->[1]->{nodes}->[1]->{window}, $third->id, 'third window on first workspace') unless !$floating;
368     is($x->input_focus, $third->id, 'third window still focused');
369 }
370
371 #############################################################################
372 # 14: For layout [H1 [A V1[ H2 [B H2 [ C V2 [ F D ] ] ] ] ] ] verify that
373 # toggling F's floating mode maintains its focus.
374 #############################################################################
375
376 sub kill_and_confirm_focus {
377     my $focus = shift;
378     my $msg = shift;
379     cmd "kill";
380     sync_with_i3;
381     is($x->input_focus, $focus, $msg);
382 }
383
384 $tmp = fresh_workspace;
385 my $A = open_window;
386 my $B = open_window;
387 cmd "split v";
388 my $C = open_window;
389 cmd "split h";
390 my $F = open_window;
391 cmd "split v";
392 my $D = open_window;
393 is($x->input_focus, $D->id, "D is focused");
394
395 sync_with_i3;
396 my $workspace = get_ws($tmp);
397 is($workspace->{nodes}->[1]->{nodes}->[1]->{nodes}->[1]->{nodes}->[0]->{window}, $F->id, 'F opened in its expected position');
398
399 fresh_workspace;
400 cmd "[id=" . $F->id . "] floating enable";
401 cmd "workspace $tmp";
402 sync_with_i3;
403
404 $workspace = get_ws($tmp);
405 is($workspace->{floating_nodes}->[0]->{nodes}->[0]->{window}, $F->id, 'F on first workspace, floating');
406 is($workspace->{nodes}->[1]->{nodes}->[1]->{nodes}->[1]->{nodes}->[0]->{window}, $D->id, 'D where F used to be');
407 is($x->input_focus, $D->id, 'D still focused');
408
409 fresh_workspace;
410 cmd "[id=" . $F->id . "] floating disable";
411 cmd "workspace $tmp";
412 sync_with_i3;
413
414 $workspace = get_ws($tmp);
415 is($workspace->{nodes}->[1]->{nodes}->[1]->{nodes}->[1]->{nodes}->[1]->{window}, $F->id, 'F where D used to be');
416 is($x->input_focus, $D->id, 'D still focused');
417
418 kill_and_confirm_focus($F->id, 'F focused after D is killed');
419 kill_and_confirm_focus($C->id, 'C focused after F is killed');
420 kill_and_confirm_focus($B->id, 'B focused after C is killed');
421 kill_and_confirm_focus($A->id, 'A focused after B is killed');
422
423 done_testing;