2 # vim:ts=4:sw=4:expandtab
4 # Please read the following documents before working on tests:
5 # • https://build.i3wm.org/docs/testsuite.html
8 # • https://build.i3wm.org/docs/lib-i3test.html
9 # (alternatively: perldoc ./testcases/lib/i3test.pm)
11 # • https://build.i3wm.org/docs/ipc.html
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 # (unless you are already familiar with Perl)
17 # Tests whether we can switch to a non-existant workspace
18 # (necessary for further tests)
20 use List::Util qw(first);
23 # to ensure that workspace 1 stays open
26 my $tmp = fresh_workspace;
27 ok(workspace_exists($tmp), 'workspace created');
28 # if the workspace could not be created, we cannot run any other test
29 # (every test starts by creating its workspace)
30 if (!workspace_exists($tmp)) {
31 BAIL_OUT('Cannot create workspace, further tests make no sense');
34 my $otmp = fresh_workspace;
35 diag("Other temporary workspace name: $otmp\n");
37 # As the old workspace was empty, it should get
38 # cleaned up as we switch away from it
39 cmd "workspace $otmp";
40 ok(!workspace_exists($tmp), 'old workspace cleaned up');
42 # Switch to the same workspace again to make sure it doesn’t get cleaned up
43 cmd "workspace $otmp";
44 cmd "workspace $otmp";
45 ok(workspace_exists($otmp), 'other workspace still exists');
48 #####################################################################
49 # check if the workspace next / prev commands work
50 #####################################################################
54 ok(!workspace_exists('next'), 'workspace "next" does not exist');
59 ok(workspace_exists($tmp), 'workspace created');
61 cmd "workspace $otmp";
64 ok(workspace_exists($tmp), 'workspace tmp still exists');
65 ok(workspace_exists($otmp), 'workspace otmp created');
67 is(focused_ws(), $otmp, 'focused workspace is otmp');
70 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
73 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
76 #####################################################################
77 # check that wrapping works
78 #####################################################################
81 is(focused_ws(), '1', 'focused workspace is 1 after workspace next');
84 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace next');
87 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
91 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
94 is(focused_ws(), '1', 'focused workspace is tmp after workspace prev');
97 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace prev');
100 #####################################################################
101 # check if we can change to "next" / "prev"
102 #####################################################################
104 cmd 'workspace "next"';
106 ok(workspace_exists('next'), 'workspace "next" exists');
107 is(focused_ws(), 'next', 'now on workspace next');
109 cmd 'workspace "prev"';
111 ok(workspace_exists('prev'), 'workspace "prev" exists');
112 is(focused_ws(), 'prev', 'now on workspace prev');
114 #####################################################################
115 # check that the numbers are assigned/recognized correctly
116 #####################################################################
118 cmd "workspace 3: $tmp";
119 my $ws = get_ws("3: $tmp");
120 ok(defined($ws), "workspace 3: $tmp was created");
121 is($ws->{num}, 3, 'workspace number is 3');
123 cmd "workspace 0: $tmp";
124 $ws = get_ws("0: $tmp");
125 ok(defined($ws), "workspace 0: $tmp was created");
126 is($ws->{num}, 0, 'workspace number is 0');
128 cmd "workspace aa: $tmp";
129 $ws = get_ws("aa: $tmp");
130 ok(defined($ws), "workspace aa: $tmp was created");
131 is($ws->{num}, -1, 'workspace number is -1');
133 ################################################################################
134 # Check that we can go to workspace "4: foo" with the command
135 # "workspace number 4".
136 ################################################################################
138 ok(!workspace_exists('4'), 'workspace 4 does not exist');
139 ok(!workspace_exists('4: foo'), 'workspace 4: foo does not exist yet');
140 cmd 'workspace 4: foo';
141 ok(workspace_exists('4: foo'), 'workspace 4: foo was created');
145 ok(workspace_exists('4: foo'), 'workspace 4: foo still open');
146 cmd 'workspace number 4';
147 is(focused_ws(), '4: foo', 'now on workspace 4: foo');
148 ok(!workspace_exists('4'), 'workspace 4 still does not exist');
150 ################################################################################
151 # Check that we "workspace number 5" will create workspace 5 if it does not yet
153 ################################################################################
155 ok(!workspace_exists('5'), 'workspace 5 does not exist');
156 cmd 'workspace number 5';
157 ok(workspace_exists('5'), 'workspace 5 was created');
159 ################################################################################
160 # Check that we can go to workspace "7: foo" with the command
161 # "workspace number 7: bar", i.e. the additional workspace name is ignored.
162 ################################################################################
164 ok(!workspace_exists('7'), 'workspace 7 does not exist');
165 ok(!workspace_exists('7: bar'), 'workspace 7: bar does not exist');
166 ok(!workspace_exists('7: foo'), 'workspace 7: foo does not exist yet');
167 cmd 'workspace 7: foo';
168 ok(workspace_exists('7: foo'), 'workspace 7: foo was created');
172 ok(workspace_exists('7: foo'), 'workspace 7: foo still open');
173 cmd 'workspace number 7: bar';
174 is(focused_ws(), '7: foo', 'now on workspace 7: foo');
175 ok(!workspace_exists('7'), 'workspace 7 still does not exist');
176 ok(!workspace_exists('7: bar'), 'workspace 7: bar still does not exist');
178 ################################################################################
179 # Check that "workspace number 8: foo" will create workspace "8: foo" if it
180 # does not yet exist (just like "workspace 8: foo" would).
181 ################################################################################
183 ok(!workspace_exists('8: foo'), 'workspace 8: foo does not exist');
184 cmd 'workspace number 8: foo';
185 ok(workspace_exists('8: foo'), 'workspace 8: foo was created');
187 ################################################################################
188 # Verify that renaming workspaces works.
189 ################################################################################
191 sub workspace_numbers_sorted {
193 my $i3 = i3(get_socket_path());
194 my $tree = $i3->get_tree->recv;
196 my @outputs = @{$tree->{nodes}};
198 for my $output (@outputs) {
199 my $content = first { $_->{type} eq 'con' } @{$output->{nodes}};
200 @workspaces = (@workspaces, @{$content->{nodes}});
203 my @numbers = grep { $_ != -1 } map { $_->{num} } @workspaces;
205 [ sort { $a <=> $b } @numbers ],
207 'workspace numbers sorted');
210 # 1: numbered workspace
216 workspace_numbers_sorted();
219 is(focused_ws(), '9', 'now on workspace 9');
221 ok(!workspace_exists('12'), 'workspace 12 does not exist yet');
222 cmd 'rename workspace 9 to 12';
223 ok(!workspace_exists('9'), 'workspace 9 does not exist anymore');
224 is(focused_ws(), '12', 'now on workspace 12');
226 is($ws->{num}, 12, 'number correctly changed');
228 workspace_numbers_sorted();
230 # 2: numbered + named workspace
231 cmd 'workspace 9: foo';
232 is(focused_ws(), '9: foo', 'now on workspace 9: foo');
234 ok(!workspace_exists('11: bar'), 'workspace 11: bar does not exist yet');
235 cmd 'rename workspace "9: foo" to "11: bar"';
236 ok(!workspace_exists('9: foo'), 'workspace 9 does not exist anymore');
237 is(focused_ws(), '11: bar', 'now on workspace 10');
238 $ws = get_ws('11: bar');
239 is($ws->{num}, 11, 'number correctly changed');
240 workspace_numbers_sorted();
241 # keep that one open, we need it later
245 cmd 'workspace bleh';
246 is(focused_ws(), 'bleh', 'now on workspace bleh');
248 ok(!workspace_exists('qux'), 'workspace qux does not exist yet');
249 cmd 'rename workspace bleh to qux';
250 ok(!workspace_exists('bleh'), 'workspace 9 does not exist anymore');
251 is(focused_ws(), 'qux', 'now on workspace qux');
253 is($ws->{num}, -1, 'number correctly changed');
254 workspace_numbers_sorted();
256 # 4: rename current workspace
257 cmd 'workspace 4711';
258 is(focused_ws(), '4711', 'now on workspace 4711');
260 ok(!workspace_exists('42'), 'workspace 42 does not exist yet');
261 cmd 'rename workspace to 42';
262 ok(!workspace_exists('4711'), 'workspace 4711 does not exist anymore');
263 is(focused_ws(), '42', 'now on workspace 42');
265 is($ws->{num}, 42, 'number correctly changed');
266 workspace_numbers_sorted();
270 is(focused_ws(), 'bla', 'now on workspace to');
272 ok(!workspace_exists('to'), 'workspace to does not exist yet');
273 cmd 'rename workspace bla to to';
274 ok(!workspace_exists('bla'), 'workspace bla does not exist anymore');
275 is(focused_ws(), 'to', 'now on workspace to');
276 cmd 'rename workspace to to bla';
277 ok(!workspace_exists('to'), 'workspace to does not exist anymore');
278 is(focused_ws(), 'bla', 'now on workspace bla');
279 cmd 'rename workspace to to';
280 ok(!workspace_exists('bla'), 'workspace bla does not exist anymore');
281 is(focused_ws(), 'to', 'now on workspace to');
282 cmd 'rename workspace to bla';
283 ok(!workspace_exists('to'), 'workspace to does not exist anymore');
284 is(focused_ws(), 'bla', 'now on workspace bla');
285 cmd 'rename workspace to tosomething';
286 ok(!workspace_exists('bla'), 'workspace bla does not exist anymore');
287 is(focused_ws(), 'tosomething', 'now on workspace tosomething');
289 # 6: already existing workspace
290 my $result = cmd 'rename workspace qux to 11: bar';
291 ok(!$result->[0]->{success}, 'renaming workspace to an already existing one failed');
293 # 7: non-existing old workspace (verify command result)
294 $result = cmd 'rename workspace notexistant to bleh';
295 ok(!$result->[0]->{success}, 'renaming workspace which does not exist failed');
298 ok(!workspace_exists('11: BAR'), 'workspace 11: BAR does not exist yet');
299 $result = cmd 'rename workspace "11: bar" to "11: BAR"';
300 ok($result->[0]->{success}, 'renaming workspace from 11: bar to 11: BAR worked');
301 ok(workspace_exists('11: BAR'), 'workspace 11: BAR now exists');