]> git.sur5r.net Git - i3/i3/blob - testcases/t/117-workspace.t
Implement 'rename workspace <old_name> to <new_name>'
[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 # Verify that renaming workspaces works.
139 ################################################################################
140
141 sub workspace_numbers_sorted {
142     my ($name) = @_;
143     my $i3 = i3(get_socket_path());
144     my $tree = $i3->get_tree->recv;
145
146     my @outputs = @{$tree->{nodes}};
147     my @workspaces;
148     for my $output (@outputs) {
149         # get the first CT_CON of each output
150         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
151         @workspaces = (@workspaces, @{$content->{nodes}});
152     }
153
154     my @numbers = grep { $_ != -1 } map { $_->{num} } @workspaces;
155     is_deeply(
156         [ sort { $a <=> $b } @numbers ],
157         \@numbers,
158         'workspace numbers sorted');
159 }
160
161 # 1: numbered workspace
162 cmd 'workspace 10';
163 cmd 'open';
164 cmd 'workspace 13';
165 cmd 'open';
166
167 workspace_numbers_sorted();
168
169 cmd 'workspace 9';
170 is(focused_ws(), '9', 'now on workspace 9');
171
172 ok(!workspace_exists('12'), 'workspace 12 does not exist yet');
173 cmd 'rename workspace 9 to 12';
174 ok(!workspace_exists('9'), 'workspace 9 does not exist anymore');
175 is(focused_ws(), '12', 'now on workspace 12');
176 $ws = get_ws('12');
177 is($ws->{num}, 12, 'number correctly changed');
178
179 workspace_numbers_sorted();
180
181 # 2: numbered + named workspace
182 cmd 'workspace 9: foo';
183 is(focused_ws(), '9: foo', 'now on workspace 9: foo');
184
185 ok(!workspace_exists('11: bar'), 'workspace 11: bar does not exist yet');
186 cmd 'rename workspace "9: foo" to "11: bar"';
187 ok(!workspace_exists('9: foo'), 'workspace 9 does not exist anymore');
188 is(focused_ws(), '11: bar', 'now on workspace 10');
189 $ws = get_ws('11: bar');
190 is($ws->{num}, 11, 'number correctly changed');
191 workspace_numbers_sorted();
192 # keep that one open, we need it later
193 cmd 'open';
194
195 # 3: named workspace
196 cmd 'workspace bleh';
197 is(focused_ws(), 'bleh', 'now on workspace bleh');
198
199 ok(!workspace_exists('qux'), 'workspace qux does not exist yet');
200 cmd 'rename workspace bleh to qux';
201 ok(!workspace_exists('bleh'), 'workspace 9 does not exist anymore');
202 is(focused_ws(), 'qux', 'now on workspace qux');
203 $ws = get_ws('qux');
204 is($ws->{num}, -1, 'number correctly changed');
205 workspace_numbers_sorted();
206
207 # 5: already existing workspace
208 my $result = cmd 'rename workspace qux to 11: bar';
209 ok(!$result->[0]->{success}, 'renaming workspace to an already existing one failed');
210
211 # 6: non-existing old workspace (verify command result)
212 $result = cmd 'rename workspace notexistant to bleh';
213 ok(!$result->[0]->{success}, 'renaming workspace which does not exist failed');
214
215
216 done_testing;