2 # vim:ts=4:sw=4:expandtab
4 # Please read the following documents before working on tests:
5 # • http://build.i3wm.org/docs/testsuite.html
8 # • http://build.i3wm.org/docs/lib-i3test.html
9 # (alternatively: perldoc ./testcases/lib/i3test.pm)
11 # • http://build.i3wm.org/docs/ipc.html
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 # (unless you are already familiar with Perl)
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
24 sub send_net_active_window {
25 my ($id, $source) = @_;
27 $source = ($source eq 'pager' ? 2 : 0);
29 my $msg = pack "CCSLLLLLLL",
30 X11::XCB::CLIENT_MESSAGE, # response_type
33 $id, # destination window
34 $x->atom(name => '_NET_ACTIVE_WINDOW')->id,
41 $x->send_event(0, $x->get_root_window(), X11::XCB::EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
45 sub get_net_active_window {
46 my $cookie = $x->get_property(
48 $x->get_root_window(),
49 $x->atom(name => '_NET_ACTIVE_WINDOW')->id,
50 $x->atom(name => 'WINDOW')->id,
54 my $reply = $x->get_property_reply($cookie->{sequence});
55 my $len = $reply->{length};
57 return -1 if $len == 0;
58 return unpack("L", $reply->{value});
62 my $ws1 = fresh_workspace;
63 my $win1 = open_window;
64 my $win2 = open_window;
66 ################################################################################
67 # Ensure that the _NET_ACTIVE_WINDOW ClientMessage works when windows are visible
68 ################################################################################
70 is($x->input_focus, $win2->id, 'window 2 has focus');
72 send_net_active_window($win1->id);
74 is($x->input_focus, $win1->id, 'window 1 has focus');
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 ################################################################################
82 my $ws2 = fresh_workspace;
83 my $win3 = open_window;
85 is($x->input_focus, $win3->id, 'window 3 has focus');
87 send_net_active_window($win1->id, 'pager');
89 is($x->input_focus, $win1->id, 'focus switched to window 1 when message source was a pager');
91 cmd '[id="' . $win3->id . '"] focus';
93 send_net_active_window($win1->id);
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');
98 ok(get_ws($ws1)->{urgent}, 'urgent hint set on ws 1');
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 ################################################################################
106 my $dock = open_window(window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'));
108 send_net_active_window($dock->id);
111 is($x->input_focus, $win3->id, 'dock did not get input focus');
113 send_net_active_window($x->get_root_window());
116 is($x->input_focus, $win3->id, 'root window did not get input focus');
118 ################################################################################
119 # Move a window to the scratchpad, send a _NET_ACTIVE_WINDOW for it and verify
120 # that focus is still unchanged.
121 ################################################################################
123 my $scratch = open_window;
125 is($x->input_focus, $scratch->id, 'to-scratchpad window has focus');
127 cmd 'move scratchpad';
129 is($x->input_focus, $win3->id, 'focus reverted to window 3');
131 send_net_active_window($scratch->id);
133 is($x->input_focus, $win3->id, 'window 3 still focused');
135 ################################################################################
136 # A scratchpad window should be shown if _NET_ACTIVE_WINDOW from a pager
138 ################################################################################
140 my $scratch = open_window;
142 is($x->input_focus, $scratch->id, 'to-scratchpad window has focus');
144 cmd 'move scratchpad';
146 is($x->input_focus, $win3->id, 'focus reverted to window 3');
148 send_net_active_window($scratch->id, 'pager');
150 is($x->input_focus, $scratch->id, 'scratchpad window is shown');
152 ################################################################################
153 # Verify that the _NET_ACTIVE_WINDOW property is updated on the root window
155 ################################################################################
161 is(get_net_active_window(), 0, 'workspace content focus is indicated by the root property as "None" window');
163 my $win4 = open_window;
165 cmd '[id="' . $win4->id . '"] focus';
169 is(get_net_active_window(), $win4->id, 'window 4 is indicated as focused by the root property');
180 is(get_net_active_window(), 0, 'branch focus is indicated by the root property as "None" window');