]> git.sur5r.net Git - i3/i3/blob - testcases/t/117-workspace.t
Fix 'rename workspace to tosomething'
[i3/i3] / testcases / t / 117-workspace.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 whether we can switch to a non-existant workspace
18 # (necessary for further tests)
19 #
20 use List::Util qw(first);
21 use i3test;
22
23 # to ensure that workspace 1 stays open
24 cmd 'open';
25
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');
32 }
33
34 my $otmp = fresh_workspace;
35 diag("Other temporary workspace name: $otmp\n");
36
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');
41
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');
46
47
48 #####################################################################
49 # check if the workspace next / prev commands work
50 #####################################################################
51
52 cmd 'workspace next';
53
54 ok(!workspace_exists('next'), 'workspace "next" does not exist');
55
56 cmd "workspace $tmp";
57 cmd 'open';
58
59 ok(workspace_exists($tmp), 'workspace created');
60
61 cmd "workspace $otmp";
62 cmd 'open';
63
64 ok(workspace_exists($tmp), 'workspace tmp still exists');
65 ok(workspace_exists($otmp), 'workspace otmp created');
66
67 is(focused_ws(), $otmp, 'focused workspace is otmp');
68
69 cmd 'workspace prev';
70 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
71
72 cmd 'workspace next';
73 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
74
75
76 #####################################################################
77 # check that wrapping works
78 #####################################################################
79
80 cmd 'workspace next';
81 is(focused_ws(), '1', 'focused workspace is 1 after workspace next');
82
83 cmd 'workspace next';
84 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace next');
85
86 cmd 'workspace next';
87 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
88
89
90 cmd 'workspace prev';
91 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
92
93 cmd 'workspace prev';
94 is(focused_ws(), '1', 'focused workspace is tmp after workspace prev');
95
96 cmd 'workspace prev';
97 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace prev');
98
99
100 #####################################################################
101 # check if we can change to "next" / "prev"
102 #####################################################################
103
104 cmd 'workspace "next"';
105
106 ok(workspace_exists('next'), 'workspace "next" exists');
107 is(focused_ws(), 'next', 'now on workspace next');
108
109 cmd 'workspace "prev"';
110
111 ok(workspace_exists('prev'), 'workspace "prev" exists');
112 is(focused_ws(), 'prev', 'now on workspace prev');
113
114 #####################################################################
115 # check that the numbers are assigned/recognized correctly
116 #####################################################################
117
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');
122
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');
127
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');
132
133 ################################################################################
134 # Check that we can go to workspace "4: foo" with the command
135 # "workspace number 4".
136 ################################################################################
137
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');
142 cmd 'open';
143
144 cmd 'workspace 3';
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');
149
150 ################################################################################
151 # Check that we "workspace number 5" will create workspace 5 if it does not yet
152 # exist.
153 ################################################################################
154
155 ok(!workspace_exists('5'), 'workspace 5 does not exist');
156 cmd 'workspace number 5';
157 ok(workspace_exists('5'), 'workspace 5 was created');
158
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 ################################################################################
163
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');
169 cmd 'open';
170
171 cmd 'workspace 6';
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');
177
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 ################################################################################
182
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');
186
187 ################################################################################
188 # Verify that renaming workspaces works.
189 ################################################################################
190
191 sub workspace_numbers_sorted {
192     my ($name) = @_;
193     my $i3 = i3(get_socket_path());
194     my $tree = $i3->get_tree->recv;
195
196     my @outputs = @{$tree->{nodes}};
197     my @workspaces;
198     for my $output (@outputs) {
199         my $content = first { $_->{type} eq 'con' } @{$output->{nodes}};
200         @workspaces = (@workspaces, @{$content->{nodes}});
201     }
202
203     my @numbers = grep { $_ != -1 } map { $_->{num} } @workspaces;
204     is_deeply(
205         [ sort { $a <=> $b } @numbers ],
206         \@numbers,
207         'workspace numbers sorted');
208 }
209
210 # 1: numbered workspace
211 cmd 'workspace 10';
212 cmd 'open';
213 cmd 'workspace 13';
214 cmd 'open';
215
216 workspace_numbers_sorted();
217
218 cmd 'workspace 9';
219 is(focused_ws(), '9', 'now on workspace 9');
220
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');
225 $ws = get_ws('12');
226 is($ws->{num}, 12, 'number correctly changed');
227
228 workspace_numbers_sorted();
229
230 # 2: numbered + named workspace
231 cmd 'workspace 9: foo';
232 is(focused_ws(), '9: foo', 'now on workspace 9: foo');
233
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
242 cmd 'open';
243
244 # 3: named workspace
245 cmd 'workspace bleh';
246 is(focused_ws(), 'bleh', 'now on workspace bleh');
247
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');
252 $ws = get_ws('qux');
253 is($ws->{num}, -1, 'number correctly changed');
254 workspace_numbers_sorted();
255
256 # 4: rename current workspace
257 cmd 'workspace 4711';
258 is(focused_ws(), '4711', 'now on workspace 4711');
259
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');
264 $ws = get_ws('42');
265 is($ws->{num}, 42, 'number correctly changed');
266 workspace_numbers_sorted();
267
268 # 5: special cases
269 cmd 'workspace bla';
270 is(focused_ws(), 'bla', 'now on workspace to');
271
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');
288
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');
292
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');
296
297 # 8: change case
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');
302
303 done_testing;