]> git.sur5r.net Git - i3/i3/blob - testcases/t/195-net-active-window.t
Handle _NET_ACTIVE_WINDOW for scratchpad windows. (#2458)
[i3/i3] / testcases / t / 195-net-active-window.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 # Verifies that the _NET_ACTIVE_WINDOW message only changes focus when the
18 # window is on a visible workspace and that focus changes properly update this
19 # property on the root window.
20 # ticket #774, bug still present in commit 1e49f1b08a3035c1f238fcd6615e332216ab582e
21 # ticket #1136, bug still present in commit fd07f989fdf441ef335245dd3436a70ff60e8896
22 use i3test;
23
24 sub send_net_active_window {
25     my ($id, $source) = @_;
26
27     $source = ($source eq 'pager' ? 2 : 0);
28
29     my $msg = pack "CCSLLLLLLL",
30         X11::XCB::CLIENT_MESSAGE, # response_type
31         32, # format
32         0, # sequence
33         $id, # destination window
34         $x->atom(name => '_NET_ACTIVE_WINDOW')->id,
35         $source,
36         0,
37         0,
38         0,
39         0;
40
41     $x->send_event(0, $x->get_root_window(), X11::XCB::EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
42     sync_with_i3;
43 }
44
45 sub get_net_active_window {
46     my $cookie = $x->get_property(
47         0,
48         $x->get_root_window(),
49         $x->atom(name => '_NET_ACTIVE_WINDOW')->id,
50         $x->atom(name => 'WINDOW')->id,
51         0,
52         4096,
53     );
54     my $reply = $x->get_property_reply($cookie->{sequence});
55     my $len = $reply->{length};
56
57     return -1 if $len == 0;
58     return unpack("L", $reply->{value});
59
60 }
61
62 my $ws1 = fresh_workspace;
63 my $win1 = open_window;
64 my $win2 = open_window;
65
66 ################################################################################
67 # Ensure that the _NET_ACTIVE_WINDOW ClientMessage works when windows are visible
68 ################################################################################
69
70 is($x->input_focus, $win2->id, 'window 2 has focus');
71
72 send_net_active_window($win1->id);
73
74 is($x->input_focus, $win1->id, 'window 1 has focus');
75
76 ################################################################################
77 # Switch to a different workspace and ensure sending the _NET_ACTIVE_WINDOW
78 # ClientMessage switches to that workspaces only if source indicates it is a
79 # pager and otherwise sets the urgent hint.
80 ################################################################################
81
82 my $ws2 = fresh_workspace;
83 my $win3 = open_window;
84
85 is($x->input_focus, $win3->id, 'window 3 has focus');
86
87 send_net_active_window($win1->id, 'pager');
88
89 is($x->input_focus, $win1->id, 'focus switched to window 1 when message source was a pager');
90
91 cmd '[id="' . $win3->id . '"] focus';
92
93 send_net_active_window($win1->id);
94
95 is($x->input_focus, $win3->id,
96     'focus did not switch to window 1 on a hidden workspace when message source was an application');
97
98 ok(get_ws($ws1)->{urgent}, 'urgent hint set on ws 1');
99
100
101 ################################################################################
102 # Make sure the ClientMessage only works with managed windows, and specifying a
103 # window that is not managed does not crash i3 (#774)
104 ################################################################################
105
106 my $dock = open_window(window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'));
107
108 send_net_active_window($dock->id);
109
110 does_i3_live;
111 is($x->input_focus, $win3->id, 'dock did not get input focus');
112
113 send_net_active_window($x->get_root_window());
114
115 does_i3_live;
116 is($x->input_focus, $win3->id, 'root window did not get input focus');
117
118 ################################################################################
119 # Move a window to the scratchpad, send a _NET_ACTIVE_WINDOW for it and verify
120 # that focus is still unchanged.
121 ################################################################################
122
123 my $scratch = open_window;
124
125 is($x->input_focus, $scratch->id, 'to-scratchpad window has focus');
126
127 cmd 'move scratchpad';
128
129 is($x->input_focus, $win3->id, 'focus reverted to window 3');
130
131 send_net_active_window($scratch->id);
132
133 is($x->input_focus, $win3->id, 'window 3 still focused');
134
135 ################################################################################
136 # A scratchpad window should be shown if _NET_ACTIVE_WINDOW from a pager
137 # is received.
138 ################################################################################
139
140 my $scratch = open_window;
141
142 is($x->input_focus, $scratch->id, 'to-scratchpad window has focus');
143
144 cmd 'move scratchpad';
145
146 is($x->input_focus, $win3->id, 'focus reverted to window 3');
147
148 send_net_active_window($scratch->id, 'pager');
149
150 is($x->input_focus, $scratch->id, 'scratchpad window is shown');
151
152 ################################################################################
153 # Verify that the _NET_ACTIVE_WINDOW property is updated on the root window
154 # correctly.
155 ################################################################################
156
157 fresh_workspace;
158
159 sync_with_i3;
160
161 is(get_net_active_window(), 0, 'workspace content focus is indicated by the root property as "None" window');
162
163 my $win4 = open_window;
164
165 cmd '[id="' . $win4->id . '"] focus';
166
167 sync_with_i3;
168
169 is(get_net_active_window(), $win4->id, 'window 4 is indicated as focused by the root property');
170
171 # make a branch
172 open_window;
173 open_window;
174 cmd 'split h';
175 open_window;
176 cmd 'focus parent';
177
178 sync_with_i3;
179
180 is(get_net_active_window(), 0, 'branch focus is indicated by the root property as "None" window');
181
182 done_testing;