2 # vim:ts=4:sw=4:expandtab
4 # Tests whether we can switch to a non-existant workspace
5 # (necessary for further tests)
7 use List::Util qw(first);
10 # to ensure that workspace 1 stays open
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');
21 my $otmp = fresh_workspace;
22 diag("Other temporary workspace name: $otmp\n");
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');
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');
35 #####################################################################
36 # check if the workspace next / prev commands work
37 #####################################################################
41 ok(!workspace_exists('next'), 'workspace "next" does not exist');
46 ok(workspace_exists($tmp), 'workspace created');
48 cmd "workspace $otmp";
51 ok(workspace_exists($tmp), 'workspace tmp still exists');
52 ok(workspace_exists($otmp), 'workspace otmp created');
54 is(focused_ws(), $otmp, 'focused workspace is otmp');
57 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
60 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
63 #####################################################################
64 # check that wrapping works
65 #####################################################################
68 is(focused_ws(), '1', 'focused workspace is 1 after workspace next');
71 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace next');
74 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
78 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
81 is(focused_ws(), '1', 'focused workspace is tmp after workspace prev');
84 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace prev');
87 #####################################################################
88 # check if we can change to "next" / "prev"
89 #####################################################################
91 cmd 'workspace "next"';
93 ok(workspace_exists('next'), 'workspace "next" exists');
94 is(focused_ws(), 'next', 'now on workspace next');
96 cmd 'workspace "prev"';
98 ok(workspace_exists('prev'), 'workspace "prev" exists');
99 is(focused_ws(), 'prev', 'now on workspace prev');
101 #####################################################################
102 # check that the numbers are assigned/recognized correctly
103 #####################################################################
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');
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');
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');
120 ################################################################################
121 # Check that we can go to workspace "4: foo" with the command
122 # "workspace number 4".
123 ################################################################################
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');
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');
137 ################################################################################
138 # Check that we "workspace number 5" will create workspace 5 if it does not yet
140 ################################################################################
142 ok(!workspace_exists('5'), 'workspace 5 does not exist');
143 cmd 'workspace number 5';
144 ok(workspace_exists('5'), 'workspace 5 was created');
146 ################################################################################
147 # Verify that renaming workspaces works.
148 ################################################################################
150 sub workspace_numbers_sorted {
152 my $i3 = i3(get_socket_path());
153 my $tree = $i3->get_tree->recv;
155 my @outputs = @{$tree->{nodes}};
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}});
163 my @numbers = grep { $_ != -1 } map { $_->{num} } @workspaces;
165 [ sort { $a <=> $b } @numbers ],
167 'workspace numbers sorted');
170 # 1: numbered workspace
176 workspace_numbers_sorted();
179 is(focused_ws(), '9', 'now on workspace 9');
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');
186 is($ws->{num}, 12, 'number correctly changed');
188 workspace_numbers_sorted();
190 # 2: numbered + named workspace
191 cmd 'workspace 9: foo';
192 is(focused_ws(), '9: foo', 'now on workspace 9: foo');
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
205 cmd 'workspace bleh';
206 is(focused_ws(), 'bleh', 'now on workspace bleh');
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');
213 is($ws->{num}, -1, 'number correctly changed');
214 workspace_numbers_sorted();
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');
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');