]> git.sur5r.net Git - i3/i3/blob - testcases/t/158-wm_take_focus.t
Fix t/158-wm_take_focus, it was not properly verifying events (Thanks TonyC)
[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 # • 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 if the WM_TAKE_FOCUS protocol is correctly handled by i3
18 #
19 use i3test;
20
21 subtest 'Window without WM_TAKE_FOCUS', sub {
22     fresh_workspace;
23
24     my $window = open_window;
25     # sync_with_i3 will send a ClientMessage to i3 and i3 will send the same
26     # payload back to $window->id.
27     my $myrnd = sync_with_i3(
28         window_id => $window->id,
29         dont_wait_for_event => 1,
30     );
31
32     # We check whether the first received message has the correct payload — if
33     # not, the received message was a WM_TAKE_FOCUS message.
34     my $first_event_is_clientmessage;
35     wait_for_event 2, sub {
36         my ($event) = @_;
37         # TODO: const
38         return 0 unless $event->{response_type} == 161;
39
40         my ($win, $rnd) = unpack "LL", $event->{data};
41         if (!defined($first_event_is_clientmessage)) {
42             $first_event_is_clientmessage = ($rnd == $myrnd);
43         }
44         return ($rnd == $myrnd);
45     };
46
47     ok($first_event_is_clientmessage, 'did not receive ClientMessage');
48
49     done_testing;
50 };
51
52 subtest 'Window with WM_TAKE_FOCUS', sub {
53     fresh_workspace;
54
55     my $take_focus = $x->atom(name => 'WM_TAKE_FOCUS');
56
57     my $window = open_window({
58         dont_map => 1,
59         protocols => [ $take_focus ],
60     });
61
62     $window->map;
63
64     ok(wait_for_event(1, sub {
65         return 0 unless $_[0]->{response_type} == 161;
66         my ($data, $time) = unpack("L2", $_[0]->{data});
67         return ($data == $take_focus->id);
68     }), 'got ClientMessage with WM_TAKE_FOCUS atom');
69
70     done_testing;
71 };
72
73
74 done_testing;