]> git.sur5r.net Git - i3/i3/blob - testcases/t/217-NET_CURRENT_DESKTOP.t
Merge pull request #3451 from orestisf1993/tray
[i3/i3] / testcases / t / 217-NET_CURRENT_DESKTOP.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 # Verifies the _NET_CURRENT_DESKTOP property correctly tracks the currently
18 # active workspace. Specifically checks that the property is 0 on startup with
19 # an empty config, tracks changes when switching workspaces and when
20 # workspaces are created and deleted.
21 #
22 # The property not being set on startup was last present in commit
23 # 6578976b6159437c16187cf8d1ea38aa9fec9fc8.
24
25 use i3test i3_autostart => 0;
26 use X11::XCB qw(PROP_MODE_REPLACE);
27
28 my $config = <<EOT;
29 # i3 config file (v4)
30 font font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
31 EOT
32
33 my $root = $x->get_root_window;
34 # Manually intern _NET_CURRENT_DESKTOP as $x->atom will not create atoms if
35 # they are not yet interned.
36 my $atom_cookie = $x->intern_atom(0, length("_NET_CURRENT_DESKTOP"), "_NET_CURRENT_DESKTOP");
37 my $_NET_CURRENT_DESKTOP = $x->intern_atom_reply($atom_cookie->{sequence})->{atom};
38 my $CARDINAL = $x->atom(name => 'CARDINAL')->id;
39
40 $x->delete_property($root, $_NET_CURRENT_DESKTOP);
41
42 $x->flush();
43
44 # Returns the _NET_CURRENT_DESKTOP property from the root window. This is
45 # the 0 based index of the current desktop.
46 sub current_desktop_index {
47     sync_with_i3;
48
49     my $cookie = $x->get_property(0, $root, $_NET_CURRENT_DESKTOP,
50                                   $CARDINAL, 0, 1);
51     my $reply = $x->get_property_reply($cookie->{sequence});
52
53     return undef if $reply->{value_len} != 1;
54     return undef if $reply->{format} != 32;
55     return undef if $reply->{type} != $CARDINAL;
56
57     return unpack 'L', $reply->{value};
58 }
59
60 my $pid = launch_with_config($config);
61
62 is(current_desktop_index, 0, "Starting on desktop 0");
63 cmd 'workspace 1';
64 is(current_desktop_index, 0, "Change from empty to empty");
65 open_window;
66 cmd 'workspace 0';
67 is(current_desktop_index, 0, "Open on 1 and view 0");
68 open_window;
69 cmd 'workspace 1';
70 is(current_desktop_index, 1, "Open on 0 and view 1");
71 cmd 'workspace 2';
72 is(current_desktop_index, 2, "Open and view empty");
73
74 #########################################################
75 # Test the _NET_CURRENT_DESKTOP client request
76 # This request is sent by pagers and bars to switch the current desktop (which
77 # is like an ersatz workspace) to the given index
78 #########################################################
79
80 sub send_current_desktop_request {
81     my ($idx) = @_;
82
83     my $msg = pack "CCSLLLLLL",
84         X11::XCB::CLIENT_MESSAGE, # response_type
85         32, # format
86         0, # sequence
87         0,
88         $_NET_CURRENT_DESKTOP,
89         $idx, # data32[0] (the desktop index)
90         0, # data32[1] (can be a timestamp)
91         0, # data32[2]
92         0, # data32[3]
93         0; # data32[4]
94
95     $x->send_event(0, $x->get_root_window(), X11::XCB::EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
96 }
97
98 send_current_desktop_request(1);
99 is(current_desktop_index, 1, 'current desktop request switched to desktop 1');
100 # note that _NET_CURRENT_DESKTOP is an index and that in this case, workspace 1
101 # is at index 1 as a convenience for the test
102 is(focused_ws, '1', 'current desktop request switched to workspace 1');
103
104 send_current_desktop_request(0);
105 is(current_desktop_index, 0, 'current desktop request switched to desktop 0');
106 is(focused_ws, '0', 'current desktop request switched to workspace 0');
107
108 exit_gracefully($pid);
109
110 done_testing;