]> git.sur5r.net Git - i3/i3/blob - testcases/t/158-wm_take_focus.t
c4b42964fc3875580b2dc40e6ee691785e13ce0a
[i3/i3] / testcases / t / 158-wm_take_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 # Tests if the WM_TAKE_FOCUS protocol is correctly handled by i3
18 #
19 # For more information on the protocol and input handling, see:
20 # https://tronche.com/gui/x/icccm/sec-4.html#s-4.1.7
21 #
22 use i3test;
23
24 sub recv_take_focus {
25     my ($window) = @_;
26
27     # sync_with_i3 will send a ClientMessage to i3 and i3 will send the same
28     # payload back to $window->id.
29     my $myrnd = sync_with_i3(
30         window_id => $window->id,
31         dont_wait_for_event => 1,
32     );
33
34     # We check whether the first received message has the correct payload — if
35     # not, the received message was a WM_TAKE_FOCUS message.
36     my $first_event_is_clientmessage;
37     wait_for_event 2, sub {
38         my ($event) = @_;
39         # TODO: const
40         return 0 unless $event->{response_type} == 161;
41
42         my ($win, $rnd) = unpack "LL", $event->{data};
43         if (!defined($first_event_is_clientmessage)) {
44             $first_event_is_clientmessage = ($rnd == $myrnd);
45         }
46         return ($rnd == $myrnd);
47     };
48
49     return !$first_event_is_clientmessage;
50 }
51
52 subtest 'Window without WM_TAKE_FOCUS', sub {
53     my $ws = fresh_workspace;
54
55     my $window = open_window;
56
57     ok(!recv_take_focus($window), 'did not receive ClientMessage');
58     ok(is_net_wm_state_focused($window), '_NET_WM_STATE_FOCUSED set');
59
60     my ($nodes) = get_ws_content($ws);
61     my $con = shift @$nodes;
62     ok($con->{focused}, 'con is focused');
63
64     done_testing;
65 };
66
67 # https://tronche.com/gui/x/icccm/sec-4.html#s-4.1.7
68 # > Clients using the Globally Active model can only use a SetInputFocus request
69 # > to acquire the input focus when they do not already have it on receipt of one
70 # > of the following events:
71 # > * ButtonPress
72 # > * ButtonRelease
73 # > * Passive-grabbed KeyPress
74 # > * Passive-grabbed KeyRelease
75 #
76 # Since managing a window happens on a MapNotify (which is absent from this
77 # list), the window cannot accept input focus, so we should not try to focus
78 # the window at all.
79 subtest 'Window with WM_TAKE_FOCUS and without InputHint', sub {
80     my $ws = fresh_workspace;
81
82     my $take_focus = $x->atom(name => 'WM_TAKE_FOCUS');
83
84     my $window = open_window({
85         dont_map => 1,
86         protocols => [ $take_focus ],
87     });
88
89     # add an (empty) WM_HINTS property without the InputHint
90     $window->delete_hint('input');
91
92     $window->map;
93
94     ok(!recv_take_focus($window), 'did not receive ClientMessage');
95     ok(is_net_wm_state_focused($window), '_NET_WM_STATE_FOCUSED set');
96
97     my ($nodes) = get_ws_content($ws);
98     my $con = shift @$nodes;
99     ok($con->{focused}, 'con is focused');
100
101     done_testing;
102 };
103
104 # If the InputHint is unspecified, i3 should use the simpler method of focusing
105 # the window directly rather than using the WM_TAKE_FOCUS protocol.
106 # XXX: The code paths for an unspecified and set InputHint are
107 # nearly identical presently, so this is currently used also as a proxy test
108 # for the latter case.
109 subtest 'Window with WM_TAKE_FOCUS and unspecified InputHint', sub {
110     my $ws = fresh_workspace;
111
112     my $take_focus = $x->atom(name => 'WM_TAKE_FOCUS');
113
114     my $window = open_window({ protocols => [ $take_focus ] });
115
116     ok(!recv_take_focus($window), 'did not receive ClientMessage');
117     ok(is_net_wm_state_focused($window), '_NET_WM_STATE_FOCUSED set');
118
119     my ($nodes) = get_ws_content($ws);
120     my $con = shift @$nodes;
121     ok($con->{focused}, 'con is focused');
122
123     done_testing;
124 };
125
126 done_testing;