]> git.sur5r.net Git - i3/i3/blob - testcases/t/17-workspace.t
Implement 'workspace next/prev' (+test)
[i3/i3] / testcases / t / 17-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 sub workspace_exists {
11     my ($name) = @_;
12     ($name ~~ @{get_workspace_names()})
13 }
14
15 my $tmp = fresh_workspace;
16 ok(workspace_exists($tmp), 'workspace created');
17 # if the workspace could not be created, we cannot run any other test
18 # (every test starts by creating its workspace)
19 if (!workspace_exists($tmp)) {
20     BAIL_OUT('Cannot create workspace, further tests make no sense');
21 }
22
23 my $otmp = fresh_workspace;
24 diag("Other temporary workspace name: $otmp\n");
25
26 # As the old workspace was empty, it should get
27 # cleaned up as we switch away from it
28 cmd "workspace $otmp";
29 ok(!workspace_exists($tmp), 'old workspace cleaned up');
30
31 # Switch to the same workspace again to make sure it doesn’t get cleaned up
32 cmd "workspace $otmp";
33 cmd "workspace $otmp";
34 ok(workspace_exists($otmp), 'other workspace still exists');
35
36
37 #####################################################################
38 # check if the workspace next / prev commands work
39 #####################################################################
40
41 cmd 'workspace next';
42
43 ok(!workspace_exists('next'), 'workspace "next" does not exist');
44
45 cmd "workspace $tmp";
46 cmd 'open';
47
48 ok(workspace_exists($tmp), 'workspace created');
49
50 cmd "workspace $otmp";
51 cmd 'open';
52
53 ok(workspace_exists($tmp), 'workspace tmp still exists');
54 ok(workspace_exists($otmp), 'workspace otmp created');
55
56 sub focused_ws_con {
57     my $i3 = i3("/tmp/nestedcons");
58     my $tree = $i3->get_tree->recv;
59     my @outputs = @{$tree->{nodes}};
60     my @cons;
61     for my $output (@outputs) {
62         # get the first CT_CON of each output
63         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
64         my @focused = @{$content->{focus}};
65         return first { $_->{id} == $focused[0] } @{$content->{nodes}};
66     }
67 }
68
69 sub focused_ws {
70     my $con = focused_ws_con;
71     return $con->{name};
72 }
73
74 is(focused_ws(), $otmp, 'focused workspace is otmp');
75
76 cmd 'workspace prev';
77 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
78
79 cmd 'workspace next';
80 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
81
82
83 #####################################################################
84 # check that wrapping works
85 #####################################################################
86
87 cmd 'workspace next';
88 is(focused_ws(), '1', 'focused workspace is 1 after workspace next');
89
90 cmd 'workspace next';
91 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace next');
92
93 cmd 'workspace next';
94 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace next');
95
96
97 cmd 'workspace prev';
98 is(focused_ws(), $tmp, 'focused workspace is tmp after workspace prev');
99
100 cmd 'workspace prev';
101 is(focused_ws(), '1', 'focused workspace is tmp after workspace prev');
102
103 cmd 'workspace prev';
104 is(focused_ws(), $otmp, 'focused workspace is otmp after workspace prev');
105
106
107 #####################################################################
108 # check if we can change to "next" / "prev"
109 #####################################################################
110
111 cmd 'workspace "next"';
112
113 ok(workspace_exists('next'), 'workspace "next" exists');
114 is(focused_ws(), 'next', 'now on workspace next');
115
116 cmd 'workspace "prev"';
117
118 ok(workspace_exists('prev'), 'workspace "prev" exists');
119 is(focused_ws(), 'prev', 'now on workspace prev');
120
121 done_testing;