]> git.sur5r.net Git - i3/i3/blob - testcases/t/166-assign.t
Allow assign to output
[i3/i3] / testcases / t / 166-assign.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 # Tests if assignments work
18 #
19 use i3test i3_autostart => 0;
20
21 sub open_special {
22     my %args = @_;
23     $args{name} //= 'special window';
24     $args{wm_class} //= 'special';
25
26     # We use dont_map because i3 will not map the window on the current
27     # workspace. Thus, open_window would time out in wait_for_map (2 seconds).
28     my $window = open_window(
29         %args,
30         dont_map => 1,
31     );
32     $window->map;
33     return $window;
34 }
35
36 sub test_workspace_assignment {
37     my $target_ws = "@_";
38
39     # initialize the target workspace, then go to a fresh one
40     ok(!($target_ws ~~ @{get_workspace_names()}), "$target_ws does not exist yet");
41     cmd "workspace $target_ws";
42     cmp_ok(@{get_ws_content($target_ws)}, '==', 0, "no containers on $target_ws yet");
43     cmd 'open';
44     cmp_ok(@{get_ws_content($target_ws)}, '==', 1, "one container on $target_ws");
45     my $tmp = fresh_workspace;
46
47     ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
48     ok($target_ws ~~ @{get_workspace_names()}, "$target_ws does not exist yet");
49
50     # We use sync_with_i3 instead of wait_for_map here because i3 will not actually
51     # map the window -- it will be assigned to a different workspace and will only
52     # be mapped once you switch to that workspace
53     my $window = open_special;
54     sync_with_i3;
55
56     ok(@{get_ws_content($tmp)} == 0, 'still no containers');
57     ok(@{get_ws_content($target_ws)} == 2, "two containers on $target_ws");
58
59     return $window
60 }
61
62 #####################################################################
63 # start a window and see that it does not get assigned with an empty config
64 #####################################################################
65
66 my $config = <<EOT;
67 # i3 config file (v4)
68 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
69 EOT
70
71 my $pid = launch_with_config($config);
72
73 my $tmp = fresh_workspace;
74
75 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
76
77 my $window = open_special;
78 wait_for_map($window);
79
80 ok(@{get_ws_content($tmp)} == 1, 'special window got managed to current (random) workspace');
81
82 exit_gracefully($pid);
83
84 $window->destroy;
85
86 #####################################################################
87 # start a window and see that it gets assigned to a formerly unused
88 # workspace
89 #####################################################################
90
91 $config = <<EOT;
92 # i3 config file (v4)
93 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
94 assign [class="special"] → targetws
95 EOT
96
97 $pid = launch_with_config($config);
98
99 $tmp = fresh_workspace;
100
101 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
102 my $workspaces = get_workspace_names;
103 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
104
105 $window = open_special;
106 sync_with_i3;
107
108 ok(@{get_ws_content($tmp)} == 0, 'still no containers');
109 ok("targetws" ~~ @{get_workspace_names()}, 'targetws exists');
110
111 $window->destroy;
112
113 exit_gracefully($pid);
114
115 #####################################################################
116 # start a window and see that it gets assigned to a formerly unused
117 # numbered workspace
118 #####################################################################
119
120 my $config_numbered = <<EOT;
121 # i3 config file (v4)
122 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
123 assign [class="special"] → workspace number 2
124 EOT
125
126 $pid = launch_with_config($config_numbered);
127
128 $tmp = fresh_workspace;
129
130 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
131 $workspaces = get_workspace_names;
132 ok(!("2" ~~ @{$workspaces}), 'workspace number 2 does not exist yet');
133
134 $window = open_special;
135 sync_with_i3;
136
137 ok(@{get_ws_content($tmp)} == 0, 'still no containers');
138 ok("2" ~~ @{get_workspace_names()}, 'workspace number 2 exists');
139
140 $window->destroy;
141
142 exit_gracefully($pid);
143
144 #####################################################################
145 # start a window and see that it gets assigned to a numbered
146 # workspace which has content already, next to the existing node.
147 #####################################################################
148
149 $pid = launch_with_config($config_numbered);
150
151 $window = test_workspace_assignment("2");
152 $window->destroy;
153
154 exit_gracefully($pid);
155
156 #####################################################################
157 # start a window and see that it gets assigned to a numbered workspace with
158 # a name which has content already, next to the existing node.
159 #####################################################################
160
161 $pid = launch_with_config($config_numbered);
162
163 cmd 'workspace 2';  # Make sure that we are not testing for "2" again.
164 $window = test_workspace_assignment("2: targetws");
165 $window->destroy;
166
167 exit_gracefully($pid);
168
169 #####################################################################
170 # start a window and see that it gets assigned to a workspace which
171 # has content already, next to the existing node.
172 #####################################################################
173
174 $pid = launch_with_config($config);
175
176 test_workspace_assignment("targetws");
177
178 exit_gracefully($pid);
179
180 #####################################################################
181 # start a window and see that it gets assigned to a workspace which has content
182 # already, next to the existing node.
183 #####################################################################
184
185 $config = <<EOT;
186 # i3 config file (v4)
187 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
188 for_window [class="special"] floating enable
189 EOT
190
191 $pid = launch_with_config($config);
192
193 $tmp = fresh_workspace;
194
195 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
196 $workspaces = get_workspace_names;
197 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
198
199 $window = open_special;
200 sync_with_i3;
201
202 my $content = get_ws($tmp);
203 ok(@{$content->{nodes}} == 0, 'no tiling cons');
204 ok(@{$content->{floating_nodes}} == 1, 'one floating con');
205
206 kill_all_windows;
207 exit_gracefully($pid);
208
209 #####################################################################
210 # test assignments to named outputs
211 #####################################################################
212 $config = <<EOT;
213 # i3 config file (v4)
214 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
215
216 fake-outputs 1024x768+0+0,1024x768+1024+0,1024x768+1024+768,1024x768+0+768
217
218 workspace ws-0 output fake-0
219 workspace ws-1 output fake-1
220 workspace ws-2 output fake-2
221 workspace ws-3 output fake-3
222
223 assign [class="special-0"] → output fake-0
224 assign [class="special-1"] → output fake-1
225 assign [class="special-2"] → output fake-2
226 assign [class="special-3"] → output fake-3
227 assign [class="special-4"] → output invalid
228
229 EOT
230
231 $pid = launch_with_config($config);
232
233 sub open_in_output {
234     my ($num, $expected_count) = @_;
235     my $ws = "ws-$num";
236     my $class = "special-$num";
237     my $output = "fake-$num";
238
239     is_num_children($ws, $expected_count - 1,
240                     "before: " . ($expected_count - 1) . " containers on output $output");
241     $window = open_special(wm_class => $class);
242     sync_with_i3;
243     is_num_children($ws, $expected_count,
244                     "after: $expected_count containers on output $output");
245 }
246
247 cmd "workspace ws-0";
248 open_in_output(0, 1);
249 my $focused = $x->input_focus;
250
251 open_in_output(1, 1);
252 is($x->input_focus, $focused, 'focus remains on output fake-0');
253
254 open_in_output(2, 1);
255 is($x->input_focus, $focused, 'focus remains on output fake-0');
256
257 for my $i (1 .. 5){
258     open_in_output(3, $i);
259     is($x->input_focus, $focused, 'focus remains on output fake-0');
260 }
261
262 # Check invalid output
263 $tmp = fresh_workspace;
264 open_special(wm_class => "special-4");
265 sync_with_i3;
266 is_num_children($tmp, 1, 'window assigned to invalid output opened in current workspace');
267 open_special(wm_class => "special-3");
268 sync_with_i3;
269 is_num_children($tmp, 1, 'but window assigned to valid output did not');
270
271 kill_all_windows;
272 exit_gracefully($pid);
273
274 #####################################################################
275 # Test assignments to outputs with relative names
276 #####################################################################
277 $config = <<EOT;
278 # i3 config file (v4)
279 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
280
281 fake-outputs 1024x768+0+0,1024x768+1024+0,1024x768+1024+768,1024x768+0+768
282
283 workspace left-top output fake-0
284 workspace right-top output fake-1
285 workspace right-bottom output fake-2
286 workspace left-bottom output fake-3
287
288 assign [class="current"] → output current
289 assign [class="left"] → output left
290 assign [class="right"] → output right
291 assign [class="up"] → output up
292 assign [class="down"] → output down
293 EOT
294
295 $pid = launch_with_config($config);
296
297 cmd 'workspace left-top';
298
299 is_num_children('left-top', 0, 'no childreon on left-top');
300 for my $i (1 .. 5){
301     open_special(wm_class => 'current');
302 }
303 sync_with_i3;
304 is_num_children('left-top', 5, 'windows opened in current workspace');
305
306 is_num_children('right-top', 0, 'no children on right-top');
307 open_special(wm_class => 'right');
308 sync_with_i3;
309 is_num_children('right-top', 1, 'one child on right-top');
310
311 is_num_children('left-bottom', 0, 'no children on left-bottom');
312 open_special(wm_class => 'down');
313 sync_with_i3;
314 is_num_children('left-bottom', 1, 'one child on left-bottom');
315
316 cmd 'workspace right-bottom';
317
318 open_special(wm_class => 'up');
319 sync_with_i3;
320 is_num_children('right-top', 2, 'two children on right-top');
321
322 open_special(wm_class => 'left');
323 sync_with_i3;
324 is_num_children('left-bottom', 2, 'two children on left-bottom');
325
326 kill_all_windows;
327 exit_gracefully($pid);
328
329 #####################################################################
330 # regression test: dock clients with floating assignments should not crash
331 # (instead, nothing should happen - dock clients can’t float)
332 # ticket #501
333 #####################################################################
334
335 # Walks /proc to figure out whether a child process of $i3pid with the name
336 # 'i3-nagbar' exists.
337 sub i3nagbar_running {
338     my ($i3pid) = @_;
339
340     my @procfiles = grep { m,^/proc/[0-9]+$, } </proc/*>;
341     for my $path (@procfiles) {
342         open(my $fh, '<', "$path/stat") or next;
343         my $line = <$fh>;
344         close($fh);
345         my ($comm, $ppid) = ($line =~ /^[0-9]+ \(([^)]+)\) . ([0-9]+)/);
346         return 1 if $ppid == $i3pid && $comm eq 'i3-nagbar';
347     }
348     return 0;
349 }
350
351 $config = <<EOT;
352 # i3 config file (v4)
353 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
354 for_window [title="special"] floating enable
355 EOT
356
357 $pid = launch_with_config($config);
358
359 $tmp = fresh_workspace;
360
361 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
362 my @docked = get_dock_clients;
363 is(@docked, 0, 'no dock client yet');
364
365 $window = open_special(
366     window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'),
367 );
368 sync_with_i3;
369
370 $content = get_ws($tmp);
371 ok(@{$content->{nodes}} == 0, 'no tiling cons');
372 ok(@{$content->{floating_nodes}} == 0, 'one floating con');
373 @docked = get_dock_clients;
374 is(@docked, 1, 'one dock client now');
375
376 $window->destroy;
377
378 does_i3_live;
379
380 exit_gracefully($pid);
381
382 done_testing;