]> git.sur5r.net Git - i3/i3/blob - testcases/t/113-urgent.t
1e2644ad96ed334437042825293b50df54660753
[i3/i3] / testcases / t / 113-urgent.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 i3_autostart => 0;
18 use List::Util qw(first);
19
20 my $_NET_WM_STATE_REMOVE = 0;
21 my $_NET_WM_STATE_ADD = 1;
22 my $_NET_WM_STATE_TOGGLE = 2;
23
24 sub set_urgency {
25     my ($win, $urgent_flag, $type) = @_;
26     if ($type == 1) {
27         # Because X11::XCB does not keep track of clearing the urgency hint
28         # when receiving focus, we just delete it in all cases and then re-set
29         # it if appropriate.
30         $win->delete_hint('urgency');
31         $win->add_hint('urgency') if ($urgent_flag);
32     } elsif ($type == 2) {
33         my $msg = pack "CCSLLLLLL",
34             X11::XCB::CLIENT_MESSAGE, # response_type
35             32, # format
36             0, # sequence
37             $win->id, # window
38             $x->atom(name => '_NET_WM_STATE')->id, # message type
39             ($urgent_flag ? $_NET_WM_STATE_ADD : $_NET_WM_STATE_REMOVE), # data32[0]
40             $x->atom(name => '_NET_WM_STATE_DEMANDS_ATTENTION')->id, # data32[1]
41             0, # data32[2]
42             0, # data32[3]
43             0; # data32[4]
44
45         $x->send_event(0, $x->get_root_window(), X11::XCB::EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
46     }
47 }
48
49 my $config = <<EOT;
50 # i3 config file (v4)
51 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
52
53 force_display_urgency_hint 0ms
54 EOT
55
56 my ($type, $tmp, $w1, $w2);
57 for ($type = 1; $type <= 2; $type++) {
58     my $pid = launch_with_config($config);
59     $tmp = fresh_workspace;
60
61 #####################################################################
62 # Create two windows and put them in stacking mode
63 #####################################################################
64
65     cmd 'split v';
66
67     my $top = open_window;
68     my $bottom = open_window;
69
70     my @urgent = grep { $_->{urgent} } @{get_ws_content($tmp)};
71     is(@urgent, 0, 'no window got the urgent flag');
72
73 # cmd 'layout stacking';
74
75 #####################################################################
76 # Add the urgency hint, switch to a different workspace and back again
77 #####################################################################
78     set_urgency($top, 1, $type);
79     sync_with_i3;
80
81     my @content = @{get_ws_content($tmp)};
82     @urgent = grep { $_->{urgent} } @content;
83     my $top_info = first { $_->{window} == $top->id } @content;
84     my $bottom_info = first { $_->{window} == $bottom->id } @content;
85
86     ok($top_info->{urgent}, 'top window is marked urgent');
87     ok(!$bottom_info->{urgent}, 'bottom window is not marked urgent');
88     is(@urgent, 1, 'exactly one window got the urgent flag');
89
90     cmd '[id="' . $top->id . '"] focus';
91
92     @urgent = grep { $_->{urgent} } @{get_ws_content($tmp)};
93     is(@urgent, 0, 'no window got the urgent flag after focusing');
94
95     set_urgency($top, 1, $type);
96     sync_with_i3;
97
98     @urgent = grep { $_->{urgent} } @{get_ws_content($tmp)};
99     is(@urgent, 0, 'no window got the urgent flag after re-setting urgency hint');
100
101 #####################################################################
102 # Check if the workspace urgency hint gets set/cleared correctly
103 #####################################################################
104
105     my $ws = get_ws($tmp);
106     ok(!$ws->{urgent}, 'urgent flag not set on workspace');
107
108     my $otmp = fresh_workspace;
109
110     set_urgency($top, 1, $type);
111     sync_with_i3;
112
113     $ws = get_ws($tmp);
114     ok($ws->{urgent}, 'urgent flag set on workspace');
115
116     cmd "workspace $tmp";
117
118     $ws = get_ws($tmp);
119     ok(!$ws->{urgent}, 'urgent flag not set on workspace after switching');
120
121 ################################################################################
122 # Use the 'urgent' criteria to switch to windows which have the urgency hint set.
123 ################################################################################
124
125 # Go to a new workspace, open a different window, verify focus is on it.
126     $otmp = fresh_workspace;
127     my $different_window = open_window;
128     is($x->input_focus, $different_window->id, 'new window focused');
129
130 # Add the urgency hint on the other window.
131     set_urgency($top, 1, $type);
132     sync_with_i3;
133
134 # Now try to switch to that window and see if focus changes.
135     cmd '[urgent=latest] focus';
136     isnt($x->input_focus, $different_window->id, 'window no longer focused');
137     is($x->input_focus, $top->id, 'urgent window focused');
138
139 ################################################################################
140 # Same thing, but with multiple windows and using the 'urgency=latest' criteria
141 # (verify that it works in the correct order).
142 ################################################################################
143
144     cmd "workspace $otmp";
145     is($x->input_focus, $different_window->id, 'new window focused again');
146
147     set_urgency($top, 1, $type);
148     sync_with_i3;
149
150     set_urgency($bottom, 1, $type);
151     sync_with_i3;
152
153     cmd '[urgent=latest] focus';
154     is($x->input_focus, $bottom->id, 'latest urgent window focused');
155     set_urgency($bottom, 0, $type);
156     sync_with_i3;
157
158     cmd '[urgent=latest] focus';
159     is($x->input_focus, $top->id, 'second urgent window focused');
160     set_urgency($top, 0, $type);
161     sync_with_i3;
162
163 ################################################################################
164 # Same thing, but with multiple windows and using the 'urgency=oldest' criteria
165 # (verify that it works in the correct order).
166 ################################################################################
167
168     cmd "workspace $otmp";
169     is($x->input_focus, $different_window->id, 'new window focused again');
170
171     set_urgency($top, 1, $type);
172     sync_with_i3;
173
174     set_urgency($bottom, 1, $type);
175     sync_with_i3;
176
177     cmd '[urgent=oldest] focus';
178     is($x->input_focus, $top->id, 'oldest urgent window focused');
179     set_urgency($top, 0, $type);
180     sync_with_i3;
181
182     cmd '[urgent=oldest] focus';
183     is($x->input_focus, $bottom->id, 'oldest urgent window focused');
184     set_urgency($bottom, 0, $type);
185     sync_with_i3;
186
187 ################################################################################
188 # Check if urgent flag gets propagated to parent containers
189 ################################################################################
190
191     cmd 'split v';
192
193
194
195     sub count_urgent {
196         my ($con) = @_;
197
198         my @children = (@{$con->{nodes}}, @{$con->{floating_nodes}});
199         my $urgent = grep { $_->{urgent} } @children;
200         $urgent += count_urgent($_) for @children;
201         return $urgent;
202     }
203
204     $tmp = fresh_workspace;
205
206     my $win1 = open_window;
207     my $win2 = open_window;
208     cmd 'layout stacked';
209     cmd 'split vertical';
210     my $win3 = open_window;
211     my $win4 = open_window;
212     cmd 'split horizontal' ;
213     my $win5 = open_window;
214     my $win6 = open_window;
215
216     sync_with_i3;
217
218
219     my $urgent = count_urgent(get_ws($tmp));
220     is($urgent, 0, 'no window got the urgent flag');
221
222     cmd '[id="' . $win2->id . '"] focus';
223     sync_with_i3;
224     set_urgency($win5, 1, $type);
225     set_urgency($win6, 1, $type);
226     sync_with_i3;
227
228 # we should have 5 urgent cons. win5, win6 and their 3 split parents.
229
230     $urgent = count_urgent(get_ws($tmp));
231     is($urgent, 5, '2 windows and 3 split containers got the urgent flag');
232
233     cmd '[id="' . $win5->id . '"] focus';
234     sync_with_i3;
235
236 # now win5 and still the split parents should be urgent.
237     $urgent = count_urgent(get_ws($tmp));
238     is($urgent, 4, '1 window and 3 split containers got the urgent flag');
239
240     cmd '[id="' . $win6->id . '"] focus';
241     sync_with_i3;
242
243 # now now window should be urgent.
244     $urgent = count_urgent(get_ws($tmp));
245     is($urgent, 0, 'All urgent flags got cleared');
246
247 ################################################################################
248 # Regression test: Check that urgent floating containers work properly (ticket
249 # #821)
250 ################################################################################
251
252     $tmp = fresh_workspace;
253     my $floating_win = open_floating_window;
254
255 # switch away
256     fresh_workspace;
257
258     set_urgency($floating_win, 1, $type);
259     sync_with_i3;
260
261     cmd "workspace $tmp";
262
263     does_i3_live;
264
265 ###############################################################################
266 # Check if the urgency hint is still set when the urgent window is killed
267 ###############################################################################
268
269     my $ws1 = fresh_workspace;
270     my $ws2 = fresh_workspace;
271     cmd "workspace $ws1";
272     $w1 = open_window;
273     $w2 = open_window;
274     cmd "workspace $ws2";
275     sync_with_i3;
276     set_urgency($w1, 1, $type);
277     sync_with_i3;
278     cmd '[id="' . $w1->id . '"] kill';
279     sync_with_i3;
280     my $w = get_ws($ws1);
281     is($w->{urgent}, 0, 'Urgent flag no longer set after killing the window ' .
282        'from another workspace');
283
284 ##############################################################################
285 # Check if urgent flag can be unset if we move the window out of the container
286 ##############################################################################
287     $tmp = fresh_workspace;
288     cmd 'layout tabbed';
289     $w1 = open_window;
290     $w2 = open_window;
291     sync_with_i3;
292     cmd '[id="' . $w2->id . '"] focus';
293     sync_with_i3;
294     cmd 'split v';
295     cmd 'layout stacked';
296     my $w3 = open_window;
297     sync_with_i3;
298     cmd '[id="' . $w2->id . '"] focus';
299     sync_with_i3;
300     set_urgency($w3, 1, $type);
301     sync_with_i3;
302     cmd 'focus parent';
303     sync_with_i3;
304     cmd 'move right';
305     cmd '[id="' . $w3->id . '"] focus';
306     sync_with_i3;
307     $ws = get_ws($tmp);
308     ok(!$ws->{urgent}, 'urgent flag not set on workspace');
309
310 ##############################################################################
311 # Regression test for #1187: Urgency hint moves to new workspace when moving
312 # a container to another workspace.
313 ##############################################################################
314
315     my $tmp_source = fresh_workspace;
316     my $tmp_target = fresh_workspace;
317     cmd 'workspace ' . $tmp_source;
318     sync_with_i3;
319     $w1 = open_window;
320     $w2 = open_window;
321     sync_with_i3;
322     cmd '[id="' . $w1->id . '"] focus';
323     sync_with_i3;
324     cmd 'mark urgent_con';
325     cmd '[id="' . $w2->id . '"] focus';
326     set_urgency($w1, 1, $type);
327     sync_with_i3;
328     cmd '[con_mark="urgent_con"] move container to workspace ' . $tmp_target;
329     sync_with_i3;
330     my $source_ws = get_ws($tmp_source);
331     my $target_ws = get_ws($tmp_target);
332     ok(!$source_ws->{urgent}, 'Source workspace is no longer marked urgent');
333     is($target_ws->{urgent}, 1, 'Target workspace is now marked urgent');
334
335 ##############################################################################
336
337     exit_gracefully($pid);
338 }
339
340 done_testing;