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);
44 sub get_net_active_window {
45 my $cookie = $x->get_property(
47 $x->get_root_window(),
48 $x->atom(name => '_NET_ACTIVE_WINDOW')->id,
49 $x->atom(name => 'WINDOW')->id,
53 my $reply = $x->get_property_reply($cookie->{sequence});
54 my $len = $reply->{length};
56 return -1 if $len == 0;
57 return unpack("L", $reply->{value});
61 my $ws1 = fresh_workspace;
62 my $win1 = open_window;
63 my $win2 = open_window;
65 ################################################################################
66 # Ensure that the _NET_ACTIVE_WINDOW ClientMessage works when windows are visible
67 ################################################################################
69 is($x->input_focus, $win2->id, 'window 2 has focus');
71 send_net_active_window($win1->id);
73 is($x->input_focus, $win1->id, 'window 1 has focus');
75 ################################################################################
76 # Switch to a different workspace and ensure sending the _NET_ACTIVE_WINDOW
77 # ClientMessage switches to that workspaces only if source indicates it is a
78 # pager and otherwise sets the urgent hint.
79 ################################################################################
81 my $ws2 = fresh_workspace;
82 my $win3 = open_window;
84 is($x->input_focus, $win3->id, 'window 3 has focus');
86 send_net_active_window($win1->id, 'pager');
88 is($x->input_focus, $win1->id, 'focus switched to window 1 when message source was a pager');
90 cmd '[id="' . $win3->id . '"] focus';
92 send_net_active_window($win1->id);
94 is($x->input_focus, $win3->id,
95 'focus did not switch to window 1 on a hidden workspace when message source was an application');
97 ok(get_ws($ws1)->{urgent}, 'urgent hint set on ws 1');
100 ################################################################################
101 # Make sure the ClientMessage only works with managed windows, and specifying a
102 # window that is not managed does not crash i3 (#774)
103 ################################################################################
105 my $dock = open_window(window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'));
107 send_net_active_window($dock->id);
110 is($x->input_focus, $win3->id, 'dock did not get input focus');
112 send_net_active_window($x->get_root_window());
115 is($x->input_focus, $win3->id, 'root window did not get input focus');
117 ################################################################################
118 # Move a window to the scratchpad, send a _NET_ACTIVE_WINDOW for it and verify
119 # that focus is still unchanged.
120 ################################################################################
122 my $scratch = open_window;
124 is($x->input_focus, $scratch->id, 'to-scratchpad window has focus');
126 cmd 'move scratchpad';
128 is($x->input_focus, $win3->id, 'focus reverted to window 3');
130 send_net_active_window($scratch->id);
132 is($x->input_focus, $win3->id, 'window 3 still focused');
134 ################################################################################
135 # Verify that the _NET_ACTIVE_WINDOW property is updated on the root window
137 ################################################################################
143 is(get_net_active_window(), 0, 'workspace content focus is indicated by the root property as "None" window');
145 my $win4 = open_window;
147 cmd '[id="' . $win4->id . '"] focus';
151 is(get_net_active_window(), $win4->id, 'window 4 is indicated as focused by the root property');
162 is(get_net_active_window(), 0, 'branch focus is indicated by the root property as "None" window');