]> git.sur5r.net Git - i3/i3/blob - testcases/t/117-workspace.t
tests: Check that 'workspace number <number>' opens a new workspace
[i3/i3] / testcases / t / 117-workspace.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Tests whether we can switch to a non-existant workspace
5 # (necessary for further tests)
6 #
7 use List::Util qw(first);
8 use i3test;
9
10 # to ensure that workspace 1 stays open
11 cmd 'open';
12
13 my $tmp = fresh_workspace;
14 ok(workspace_exists($tmp), 'workspace created');
15 # if the workspace could not be created, we cannot run any other test
16 # (every test starts by creating its workspace)
17 if (!workspace_exists($tmp)) {
18     BAIL_OUT('Cannot create workspace, further tests make no sense');
19 }
20
21 my $otmp = fresh_workspace;
22 diag("Other temporary workspace name: $otmp\n");
23
24 # As the old workspace was empty, it should get
25 # cleaned up as we switch away from it
26 cmd "workspace $otmp";
27 ok(!workspace_exists($tmp), 'old workspace cleaned up');
28
29 # Switch to the same workspace again to make sure it doesn’t get cleaned up
30 cmd "workspace $otmp";
31 cmd "workspace $otmp";
32 ok(workspace_exists($otmp), 'other workspace still exists');
33
34
35 #####################################################################
36 # check if the workspace next / prev commands work
37 #####################################################################
38
39 cmd 'workspace next';
40
41 ok(!workspace_exists('next'), 'workspace "next" does not exist');
42
43 cmd "workspace $tmp";
44 cmd 'open';
45
46 ok(workspace_exists($tmp), 'workspace created');
47
48 cmd "workspace $otmp";
49 cmd 'open';
50
51 ok(workspace_exists($tmp), 'workspace tmp still exists');
52 ok(workspace_exists($otmp), 'workspace otmp created');
53
54 is(focused_ws(), $otmp, 'focused workspace is otmp');
55
56 cmd 'workspace prev';
57 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
58
59 cmd 'workspace next';
60 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
61
62
63 #####################################################################
64 # check that wrapping works
65 #####################################################################
66
67 cmd 'workspace next';
68 is(focused_ws(), '1', 'focused workspace is 1 after workspace next');
69
70 cmd 'workspace next';
71 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace next');
72
73 cmd 'workspace next';
74 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
75
76
77 cmd 'workspace prev';
78 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
79
80 cmd 'workspace prev';
81 is(focused_ws(), '1', 'focused workspace is tmp after workspace prev');
82
83 cmd 'workspace prev';
84 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace prev');
85
86
87 #####################################################################
88 # check if we can change to "next" / "prev"
89 #####################################################################
90
91 cmd 'workspace "next"';
92
93 ok(workspace_exists('next'), 'workspace "next" exists');
94 is(focused_ws(), 'next', 'now on workspace next');
95
96 cmd 'workspace "prev"';
97
98 ok(workspace_exists('prev'), 'workspace "prev" exists');
99 is(focused_ws(), 'prev', 'now on workspace prev');
100
101 #####################################################################
102 # check that the numbers are assigned/recognized correctly
103 #####################################################################
104
105 cmd "workspace 3: $tmp";
106 my $ws = get_ws("3: $tmp");
107 ok(defined($ws), "workspace 3: $tmp was created");
108 is($ws->{num}, 3, 'workspace number is 3');
109
110 cmd "workspace 0: $tmp";
111 $ws = get_ws("0: $tmp");
112 ok(defined($ws), "workspace 0: $tmp was created");
113 is($ws->{num}, 0, 'workspace number is 0');
114
115 cmd "workspace aa: $tmp";
116 $ws = get_ws("aa: $tmp");
117 ok(defined($ws), "workspace aa: $tmp was created");
118 is($ws->{num}, -1, 'workspace number is -1');
119
120 ################################################################################
121 # Check that we can go to workspace "4: foo" with the command
122 # "workspace number 4".
123 ################################################################################
124
125 ok(!workspace_exists('4'), 'workspace 4 does not exist');
126 ok(!workspace_exists('4: foo'), 'workspace 4: foo does not exist yet');
127 cmd 'workspace 4: foo';
128 ok(workspace_exists('4: foo'), 'workspace 4: foo was created');
129 cmd 'open';
130
131 cmd 'workspace 3';
132 ok(workspace_exists('4: foo'), 'workspace 4: foo still open');
133 cmd 'workspace number 4';
134 is(focused_ws(), '4: foo', 'now on workspace 4: foo');
135 ok(!workspace_exists('4'), 'workspace 4 still does not exist');
136
137 ################################################################################
138 # Check that we "workspace number 5" will create workspace 5 if it does not yet
139 # exist.
140 ################################################################################
141
142 ok(!workspace_exists('5'), 'workspace 5 does not exist');
143 cmd 'workspace number 5';
144 ok(workspace_exists('5'), 'workspace 5 was created');
145
146 ################################################################################
147 # Verify that renaming workspaces works.
148 ################################################################################
149
150 sub workspace_numbers_sorted {
151     my ($name) = @_;
152     my $i3 = i3(get_socket_path());
153     my $tree = $i3->get_tree->recv;
154
155     my @outputs = @{$tree->{nodes}};
156     my @workspaces;
157     for my $output (@outputs) {
158         # get the first CT_CON of each output
159         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
160         @workspaces = (@workspaces, @{$content->{nodes}});
161     }
162
163     my @numbers = grep { $_ != -1 } map { $_->{num} } @workspaces;
164     is_deeply(
165         [ sort { $a <=> $b } @numbers ],
166         \@numbers,
167         'workspace numbers sorted');
168 }
169
170 # 1: numbered workspace
171 cmd 'workspace 10';
172 cmd 'open';
173 cmd 'workspace 13';
174 cmd 'open';
175
176 workspace_numbers_sorted();
177
178 cmd 'workspace 9';
179 is(focused_ws(), '9', 'now on workspace 9');
180
181 ok(!workspace_exists('12'), 'workspace 12 does not exist yet');
182 cmd 'rename workspace 9 to 12';
183 ok(!workspace_exists('9'), 'workspace 9 does not exist anymore');
184 is(focused_ws(), '12', 'now on workspace 12');
185 $ws = get_ws('12');
186 is($ws->{num}, 12, 'number correctly changed');
187
188 workspace_numbers_sorted();
189
190 # 2: numbered + named workspace
191 cmd 'workspace 9: foo';
192 is(focused_ws(), '9: foo', 'now on workspace 9: foo');
193
194 ok(!workspace_exists('11: bar'), 'workspace 11: bar does not exist yet');
195 cmd 'rename workspace "9: foo" to "11: bar"';
196 ok(!workspace_exists('9: foo'), 'workspace 9 does not exist anymore');
197 is(focused_ws(), '11: bar', 'now on workspace 10');
198 $ws = get_ws('11: bar');
199 is($ws->{num}, 11, 'number correctly changed');
200 workspace_numbers_sorted();
201 # keep that one open, we need it later
202 cmd 'open';
203
204 # 3: named workspace
205 cmd 'workspace bleh';
206 is(focused_ws(), 'bleh', 'now on workspace bleh');
207
208 ok(!workspace_exists('qux'), 'workspace qux does not exist yet');
209 cmd 'rename workspace bleh to qux';
210 ok(!workspace_exists('bleh'), 'workspace 9 does not exist anymore');
211 is(focused_ws(), 'qux', 'now on workspace qux');
212 $ws = get_ws('qux');
213 is($ws->{num}, -1, 'number correctly changed');
214 workspace_numbers_sorted();
215
216 # 5: already existing workspace
217 my $result = cmd 'rename workspace qux to 11: bar';
218 ok(!$result->[0]->{success}, 'renaming workspace to an already existing one failed');
219
220 # 6: non-existing old workspace (verify command result)
221 $result = cmd 'rename workspace notexistant to bleh';
222 ok(!$result->[0]->{success}, 'renaming workspace which does not exist failed');
223
224
225 done_testing;