]> git.sur5r.net Git - i3/i3/blob - testcases/t/117-workspace.t
bugfix: don't use con_is_internal
[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 # Verify that renaming workspaces works.
161 ################################################################################
162
163 sub workspace_numbers_sorted {
164     my ($name) = @_;
165     my $i3 = i3(get_socket_path());
166     my $tree = $i3->get_tree->recv;
167
168     my @outputs = @{$tree->{nodes}};
169     my @workspaces;
170     for my $output (@outputs) {
171         # get the first CT_CON of each output
172         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
173         @workspaces = (@workspaces, @{$content->{nodes}});
174     }
175
176     my @numbers = grep { $_ != -1 } map { $_->{num} } @workspaces;
177     is_deeply(
178         [ sort { $a <=> $b } @numbers ],
179         \@numbers,
180         'workspace numbers sorted');
181 }
182
183 # 1: numbered workspace
184 cmd 'workspace 10';
185 cmd 'open';
186 cmd 'workspace 13';
187 cmd 'open';
188
189 workspace_numbers_sorted();
190
191 cmd 'workspace 9';
192 is(focused_ws(), '9', 'now on workspace 9');
193
194 ok(!workspace_exists('12'), 'workspace 12 does not exist yet');
195 cmd 'rename workspace 9 to 12';
196 ok(!workspace_exists('9'), 'workspace 9 does not exist anymore');
197 is(focused_ws(), '12', 'now on workspace 12');
198 $ws = get_ws('12');
199 is($ws->{num}, 12, 'number correctly changed');
200
201 workspace_numbers_sorted();
202
203 # 2: numbered + named workspace
204 cmd 'workspace 9: foo';
205 is(focused_ws(), '9: foo', 'now on workspace 9: foo');
206
207 ok(!workspace_exists('11: bar'), 'workspace 11: bar does not exist yet');
208 cmd 'rename workspace "9: foo" to "11: bar"';
209 ok(!workspace_exists('9: foo'), 'workspace 9 does not exist anymore');
210 is(focused_ws(), '11: bar', 'now on workspace 10');
211 $ws = get_ws('11: bar');
212 is($ws->{num}, 11, 'number correctly changed');
213 workspace_numbers_sorted();
214 # keep that one open, we need it later
215 cmd 'open';
216
217 # 3: named workspace
218 cmd 'workspace bleh';
219 is(focused_ws(), 'bleh', 'now on workspace bleh');
220
221 ok(!workspace_exists('qux'), 'workspace qux does not exist yet');
222 cmd 'rename workspace bleh to qux';
223 ok(!workspace_exists('bleh'), 'workspace 9 does not exist anymore');
224 is(focused_ws(), 'qux', 'now on workspace qux');
225 $ws = get_ws('qux');
226 is($ws->{num}, -1, 'number correctly changed');
227 workspace_numbers_sorted();
228
229 # 5: already existing workspace
230 my $result = cmd 'rename workspace qux to 11: bar';
231 ok(!$result->[0]->{success}, 'renaming workspace to an already existing one failed');
232
233 # 6: non-existing old workspace (verify command result)
234 $result = cmd 'rename workspace notexistant to bleh';
235 ok(!$result->[0]->{success}, 'renaming workspace which does not exist failed');
236
237
238 done_testing;