]> git.sur5r.net Git - i3/i3/blob - testcases/t/166-assign.t
Allow assign to workspace by number
[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
25     # We use dont_map because i3 will not map the window on the current
26     # workspace. Thus, open_window would time out in wait_for_map (2 seconds).
27     my $window = open_window(
28         %args,
29         wm_class => 'special',
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(dont_map => 1);
54     $window->map;
55     sync_with_i3;
56
57     ok(@{get_ws_content($tmp)} == 0, 'still no containers');
58     ok(@{get_ws_content($target_ws)} == 2, "two containers on $target_ws");
59
60     return $window
61 }
62
63 #####################################################################
64 # start a window and see that it does not get assigned with an empty config
65 #####################################################################
66
67 my $config = <<EOT;
68 # i3 config file (v4)
69 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
70 EOT
71
72 my $pid = launch_with_config($config);
73
74 my $tmp = fresh_workspace;
75
76 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
77
78 my $window = open_special;
79 wait_for_map($window);
80
81 ok(@{get_ws_content($tmp)} == 1, 'special window got managed to current (random) workspace');
82
83 exit_gracefully($pid);
84
85 $window->destroy;
86
87 #####################################################################
88 # start a window and see that it gets assigned to a formerly unused
89 # workspace
90 #####################################################################
91
92 $config = <<EOT;
93 # i3 config file (v4)
94 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
95 assign [class="special"] → targetws
96 EOT
97
98 $pid = launch_with_config($config);
99
100 $tmp = fresh_workspace;
101
102 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
103 my $workspaces = get_workspace_names;
104 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
105
106 $window = open_special;
107 sync_with_i3;
108
109 ok(@{get_ws_content($tmp)} == 0, 'still no containers');
110 ok("targetws" ~~ @{get_workspace_names()}, 'targetws exists');
111
112 $window->destroy;
113
114 exit_gracefully($pid);
115
116 #####################################################################
117 # start a window and see that it gets assigned to a formerly unused
118 # numbered workspace
119 #####################################################################
120
121 my $config_numbered = <<EOT;
122 # i3 config file (v4)
123 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
124 assign [class="special"] → workspace number 2
125 EOT
126
127 $pid = launch_with_config($config_numbered);
128
129 $tmp = fresh_workspace;
130
131 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
132 $workspaces = get_workspace_names;
133 ok(!("2" ~~ @{$workspaces}), 'workspace number 2 does not exist yet');
134
135 $window = open_special;
136 sync_with_i3;
137
138 ok(@{get_ws_content($tmp)} == 0, 'still no containers');
139 ok("2" ~~ @{get_workspace_names()}, 'workspace number 2 exists');
140
141 $window->destroy;
142
143 exit_gracefully($pid);
144
145 #####################################################################
146 # start a window and see that it gets assigned to a numbered
147 # workspace which has content already, next to the existing node.
148 #####################################################################
149
150 $pid = launch_with_config($config_numbered);
151
152 $window = test_workspace_assignment("2");
153 $window->destroy;
154
155 exit_gracefully($pid);
156
157 #####################################################################
158 # start a window and see that it gets assigned to a numbered workspace with
159 # a name which has content already, next to the existing node.
160 #####################################################################
161
162 $pid = launch_with_config($config_numbered);
163
164 cmd 'workspace 2';  # Make sure that we are not testing for "2" again.
165 $window = test_workspace_assignment("2: targetws");
166 $window->destroy;
167
168 exit_gracefully($pid);
169
170 #####################################################################
171 # start a window and see that it gets assigned to a workspace which
172 # has content already, next to the existing node.
173 #####################################################################
174
175 $pid = launch_with_config($config);
176
177 test_workspace_assignment("targetws");
178
179 exit_gracefully($pid);
180
181 #####################################################################
182 # start a window and see that it gets assigned to a workspace which has content
183 # already, next to the existing node.
184 #####################################################################
185
186 $config = <<EOT;
187 # i3 config file (v4)
188 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
189 for_window [class="special"] floating enable
190 EOT
191
192 $pid = launch_with_config($config);
193
194 $tmp = fresh_workspace;
195
196 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
197 $workspaces = get_workspace_names;
198 ok(!("targetws" ~~ @{$workspaces}), 'targetws does not exist yet');
199
200 $window = open_special;
201 sync_with_i3;
202
203 my $content = get_ws($tmp);
204 ok(@{$content->{nodes}} == 0, 'no tiling cons');
205 ok(@{$content->{floating_nodes}} == 1, 'one floating con');
206
207 $window->destroy;
208
209 exit_gracefully($pid);
210
211 #####################################################################
212 # regression test: dock clients with floating assignments should not crash
213 # (instead, nothing should happen - dock clients can’t float)
214 # ticket #501
215 #####################################################################
216
217 # Walks /proc to figure out whether a child process of $i3pid with the name
218 # 'i3-nagbar' exists.
219 sub i3nagbar_running {
220     my ($i3pid) = @_;
221
222     my @procfiles = grep { m,^/proc/[0-9]+$, } </proc/*>;
223     for my $path (@procfiles) {
224         open(my $fh, '<', "$path/stat") or next;
225         my $line = <$fh>;
226         close($fh);
227         my ($comm, $ppid) = ($line =~ /^[0-9]+ \(([^)]+)\) . ([0-9]+)/);
228         return 1 if $ppid == $i3pid && $comm eq 'i3-nagbar';
229     }
230     return 0;
231 }
232
233 $config = <<EOT;
234 # i3 config file (v4)
235 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
236 for_window [title="special"] floating enable
237 EOT
238
239 $pid = launch_with_config($config);
240
241 $tmp = fresh_workspace;
242
243 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
244 my @docked = get_dock_clients;
245 is(@docked, 0, 'one dock client yet');
246
247 $window = open_special(
248     window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'),
249 );
250 sync_with_i3;
251
252 $content = get_ws($tmp);
253 ok(@{$content->{nodes}} == 0, 'no tiling cons');
254 ok(@{$content->{floating_nodes}} == 0, 'one floating con');
255 @docked = get_dock_clients;
256 is(@docked, 1, 'one dock client now');
257
258 $window->destroy;
259
260 does_i3_live;
261
262 exit_gracefully($pid);
263
264 done_testing;