]> git.sur5r.net Git - i3/i3/blob - testcases/t/166-assign.t
t/166-assign: check for i3-nagbar in the process hierarchy instead of sleep(1)
[i3/i3] / testcases / t / 166-assign.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Tests if assignments work
5 #
6 use i3test i3_autostart => 0;
7 use X11::XCB qw(PROP_MODE_REPLACE);
8
9 # TODO: move to X11::XCB
10 sub set_wm_class {
11     my ($id, $class, $instance) = @_;
12
13     # Add a _NET_WM_STRUT_PARTIAL hint
14     my $atomname = $x->atom(name => 'WM_CLASS');
15     my $atomtype = $x->atom(name => 'STRING');
16
17     $x->change_property(
18         PROP_MODE_REPLACE,
19         $id,
20         $atomname->id,
21         $atomtype->id,
22         8,
23         length($class) + length($instance) + 2,
24         "$instance\x00$class\x00"
25     );
26 }
27
28 sub open_special {
29     my %args = @_;
30     my $wm_class = delete($args{wm_class}) || 'special';
31     $args{name} //= 'special window';
32
33     # We use dont_map because i3 will not map the window on the current
34     # workspace. Thus, open_window would time out in wait_for_map (2 seconds).
35     my $window = open_window(
36         %args,
37         before_map => sub { set_wm_class($_->id, $wm_class, $wm_class) },
38         dont_map => 1,
39     );
40     $window->map;
41     return $window;
42 }
43
44 #####################################################################
45 # start a window and see that it does not get assigned with an empty config
46 #####################################################################
47
48 my $config = <<EOT;
49 # i3 config file (v4)
50 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
51 EOT
52
53 my $pid = launch_with_config($config);
54
55 my $tmp = fresh_workspace;
56
57 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
58
59 my $window = open_special;
60
61 ok(@{get_ws_content($tmp)} == 1, 'special window got managed to current (random) workspace');
62
63 exit_gracefully($pid);
64
65 $window->destroy;
66
67 #####################################################################
68 # start a window and see that it gets assigned to a formerly unused
69 # workspace
70 #####################################################################
71
72 $config = <<EOT;
73 # i3 config file (v4)
74 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
75 assign "special" → targetws
76 EOT
77
78 $pid = launch_with_config($config);
79
80 $tmp = fresh_workspace;
81
82 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
83 my $workspaces = get_workspace_names;
84 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
85
86 $window = open_special;
87
88 ok(@{get_ws_content($tmp)} == 0, 'still no containers');
89 ok("targetws" ~~ @{get_workspace_names()}, 'targetws exists');
90
91 $window->destroy;
92
93 exit_gracefully($pid);
94
95 sleep 0.25;
96
97 #####################################################################
98 # start a window and see that it gets assigned to a workspace which has content
99 # already, next to the existing node.
100 #####################################################################
101
102 $pid = launch_with_config($config);
103
104 # initialize the target workspace, then go to a fresh one
105 ok(!("targetws" ~~ @{get_workspace_names()}), 'targetws does not exist yet');
106 cmd 'workspace targetws';
107 cmp_ok(@{get_ws_content('targetws')}, '==', 0, 'no containers on targetws yet');
108 cmd 'open';
109 cmp_ok(@{get_ws_content('targetws')}, '==', 1, 'one container on targetws');
110 $tmp = fresh_workspace;
111
112 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
113 ok("targetws" ~~ @{get_workspace_names()}, 'targetws does not exist yet');
114
115
116 # We use sync_with_i3 instead of wait_for_map here because i3 will not actually
117 # map the window -- it will be assigned to a different workspace and will only
118 # be mapped once you switch to that workspace
119 $window = open_special(dont_map => 1);
120 $window->map;
121 sync_with_i3;
122
123 ok(@{get_ws_content($tmp)} == 0, 'still no containers');
124 ok(@{get_ws_content('targetws')} == 2, 'two containers on targetws');
125
126 exit_gracefully($pid);
127
128 #####################################################################
129 # start a window and see that it gets assigned to a workspace which has content
130 # already, next to the existing node.
131 #####################################################################
132
133 $config = <<EOT;
134 # i3 config file (v4)
135 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
136 assign "special" → ~
137 EOT
138
139 $pid = launch_with_config($config);
140
141 $tmp = fresh_workspace;
142
143 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
144 $workspaces = get_workspace_names;
145 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
146
147 $window = open_special;
148
149 my $content = get_ws($tmp);
150 ok(@{$content->{nodes}} == 0, 'no tiling cons');
151 ok(@{$content->{floating_nodes}} == 1, 'one floating con');
152
153 $window->destroy;
154
155 exit_gracefully($pid);
156
157 sleep 0.25;
158
159 #####################################################################
160 # make sure that assignments are case-insensitive in the old syntax.
161 #####################################################################
162
163 $config = <<EOT;
164 # i3 config file (v4)
165 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
166 assign "special" → ~
167 EOT
168
169 $pid = launch_with_config($config);
170
171 $tmp = fresh_workspace;
172
173 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
174 $workspaces = get_workspace_names;
175 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
176
177 $window = open_special(wm_class => 'SPEcial');
178
179 $content = get_ws($tmp);
180 ok(@{$content->{nodes}} == 0, 'no tiling cons');
181 ok(@{$content->{floating_nodes}} == 1, 'one floating con');
182
183 $window->destroy;
184
185 exit_gracefully($pid);
186
187 sleep 0.25;
188
189 #####################################################################
190 # regression test: dock clients with floating assignments should not crash
191 # (instead, nothing should happen - dock clients can’t float)
192 # ticket #501
193 #####################################################################
194
195 # Walks /proc to figure out whether a child process of $i3pid with the name
196 # 'i3-nagbar' exists.
197 sub i3nagbar_running {
198     my ($i3pid) = @_;
199
200     my @procfiles = grep { m,^/proc/[0-9]+$, } </proc/*>;
201     for my $path (@procfiles) {
202         open(my $fh, '<', "$path/stat") or next;
203         my $line = <$fh>;
204         close($fh);
205         my ($comm, $ppid) = ($line =~ /^[0-9]+ \(([^)]+)\) . ([0-9]+)/);
206         return 1 if $ppid == $i3pid && $comm eq 'i3-nagbar';
207     }
208     return 0;
209 }
210
211 $config = <<EOT;
212 # i3 config file (v4)
213 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
214 assign "special" → ~
215 EOT
216
217 $pid = launch_with_config($config);
218
219 # Ensure that i3-nagbar is running. It should be started pretty quickly, so we
220 # busy-loop with a short delay.
221 while (!i3nagbar_running($pid)) {
222     sleep 0.05;
223 }
224
225 $tmp = fresh_workspace;
226
227 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
228 my @docked = get_dock_clients;
229 # We expect i3-nagbar as the first dock client due to using the old assign
230 # syntax
231 is(@docked, 1, 'one dock client yet');
232
233 $window = open_special(
234     window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'),
235 );
236
237 $content = get_ws($tmp);
238 ok(@{$content->{nodes}} == 0, 'no tiling cons');
239 ok(@{$content->{floating_nodes}} == 0, 'one floating con');
240 @docked = get_dock_clients;
241 is(@docked, 2, 'two dock clients now');
242
243 $window->destroy;
244
245 does_i3_live;
246
247 exit_gracefully($pid);
248
249 done_testing;