]> git.sur5r.net Git - i3/i3/blob - testcases/t/166-assign.t
166-assign.t: improve open_special call
[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 $window->destroy;
207
208 exit_gracefully($pid);
209
210 #####################################################################
211 # regression test: dock clients with floating assignments should not crash
212 # (instead, nothing should happen - dock clients can’t float)
213 # ticket #501
214 #####################################################################
215
216 # Walks /proc to figure out whether a child process of $i3pid with the name
217 # 'i3-nagbar' exists.
218 sub i3nagbar_running {
219     my ($i3pid) = @_;
220
221     my @procfiles = grep { m,^/proc/[0-9]+$, } </proc/*>;
222     for my $path (@procfiles) {
223         open(my $fh, '<', "$path/stat") or next;
224         my $line = <$fh>;
225         close($fh);
226         my ($comm, $ppid) = ($line =~ /^[0-9]+ \(([^)]+)\) . ([0-9]+)/);
227         return 1 if $ppid == $i3pid && $comm eq 'i3-nagbar';
228     }
229     return 0;
230 }
231
232 $config = <<EOT;
233 # i3 config file (v4)
234 font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
235 for_window [title="special"] floating enable
236 EOT
237
238 $pid = launch_with_config($config);
239
240 $tmp = fresh_workspace;
241
242 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
243 my @docked = get_dock_clients;
244 is(@docked, 0, 'no dock client yet');
245
246 $window = open_special(
247     window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_DOCK'),
248 );
249 sync_with_i3;
250
251 $content = get_ws($tmp);
252 ok(@{$content->{nodes}} == 0, 'no tiling cons');
253 ok(@{$content->{floating_nodes}} == 0, 'one floating con');
254 @docked = get_dock_clients;
255 is(@docked, 1, 'one dock client now');
256
257 $window->destroy;
258
259 does_i3_live;
260
261 exit_gracefully($pid);
262
263 done_testing;