]> git.sur5r.net Git - i3/i3/blob - testcases/t/122-split.t
added "toggle" option to "split" command
[i3/i3] / testcases / t / 122-split.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 splitting
18 #
19 use i3test;
20 use List::Util qw(first);
21
22 my $tmp;
23 my $ws;
24
25 ################################################################################
26 # Open two containers, split, open another container. Then verify
27 # the layout is like we expect it to be
28 ################################################################################
29
30 sub verify_split_layout {
31     my (%args) = @_;
32
33     $tmp = fresh_workspace;
34
35     $ws = get_ws($tmp);
36     is($ws->{layout}, 'splith', 'orientation horizontal by default');
37     cmd 'split v';
38     $ws = get_ws($tmp);
39     is($ws->{layout}, 'splitv', 'split v changes workspace orientation');
40
41     cmd 'open';
42     cmd 'open';
43     my $content = get_ws_content($tmp);
44
45     is(@{$content}, 2, 'two containers on workspace level');
46     my $first = $content->[0];
47     my $second = $content->[1];
48
49     is(@{$first->{nodes}}, 0, 'first container has no children');
50     is(@{$second->{nodes}}, 0, 'second container has no children (yet)');
51     my $old_id = $second->{id};
52
53     cmd $args{split_command};
54     cmd 'open';
55
56     $content = get_ws_content($tmp);
57
58     is(@{$content}, 2, 'two containers on workspace level');
59     $first = $content->[0];
60     $second = $content->[1];
61
62     is(@{$first->{nodes}}, 0, 'first container has no children');
63     isnt($second->{id}, $old_id, 'second container was replaced');
64     is($second->{layout}, 'splith', 'orientation is horizontal');
65     is(@{$second->{nodes}}, 2, 'second container has 2 children');
66     is($second->{nodes}->[0]->{id}, $old_id, 'found old second container');
67 }
68
69 verify_split_layout(split_command => 'split h');
70 verify_split_layout(split_command => 'split horizontal');
71
72 # TODO: extend this test-case (test next/prev)
73 # - wrapping (no horizontal switch possible, goes level-up)
74 # - going level-up "manually"
75
76 ######################################################################
77 # Test splitting multiple times without actually creating windows
78 ######################################################################
79
80 $tmp = fresh_workspace;
81
82 $ws = get_ws($tmp);
83 is($ws->{layout}, 'splith', 'orientation horizontal by default');
84 cmd 'split v';
85 $ws = get_ws($tmp);
86 is($ws->{layout}, 'splitv', 'split v changes workspace orientation');
87
88 cmd 'open';
89 my @content = @{get_ws_content($tmp)};
90
91 # recursively sums up all nodes and their children
92 sub sum_nodes {
93     my ($nodes) = @_;
94
95     return 0 if !@{$nodes};
96
97     my @children = (map { @{$_->{nodes}} } @{$nodes},
98                     map { @{$_->{'floating_nodes'}} } @{$nodes});
99
100     return @{$nodes} + sum_nodes(\@children);
101 }
102
103 my $old_count = sum_nodes(\@content);
104 cmd 'split v';
105
106 @content = @{get_ws_content($tmp)};
107 $old_count = sum_nodes(\@content);
108
109 cmd 'split v';
110
111 @content = @{get_ws_content($tmp)};
112 my $count = sum_nodes(\@content);
113 is($count, $old_count, 'not more windows after splitting again');
114
115 ######################################################################
116 # In the special case of being inside a stacked or tabbed container, we don’t
117 # want this to happen.
118 ######################################################################
119
120 $tmp = fresh_workspace;
121
122 cmd 'open';
123 @content = @{get_ws_content($tmp)};
124 is(scalar @content, 1, 'Precisely one container on this ws');
125 cmd 'layout stacked';
126 @content = @{get_ws_content($tmp)};
127 is(scalar @content, 1, 'Still one container on this ws');
128 is(scalar @{$content[0]->{nodes}}, 1, 'Stacked con has one child node');
129
130 cmd 'split h';
131 cmd 'open';
132 @content = @{get_ws_content($tmp)};
133 is(scalar @content, 1, 'Still one container on this ws');
134 is(scalar @{$content[0]->{nodes}}, 1, 'Stacked con still has one child node');
135
136 ################################################################################
137 # When focusing the workspace, changing the layout should have an effect on the
138 # workspace, not on the parent (CT_CONTENT) container.
139 ################################################################################
140
141 sub get_output_content {
142     my $tree = i3(get_socket_path())->get_tree->recv;
143
144     my @outputs = grep { $_->{name} !~ /^__/ } @{$tree->{nodes}};
145     is(scalar @outputs, 1, 'exactly one output (testcase not multi-monitor capable)');
146     my $output = $outputs[0];
147     # get the first (and only) CT_CON
148     return first { $_->{type} eq 'con' } @{$output->{nodes}};
149 }
150
151 $tmp = fresh_workspace;
152
153 cmd 'open';
154 cmd 'split v';
155 cmd 'open';
156 cmd 'focus parent';
157 is(get_output_content()->{layout}, 'splith', 'content container layout ok');
158 cmd 'layout stacked';
159 is(get_output_content()->{layout}, 'splith', 'content container layout still ok');
160
161 ######################################################################
162 # Splitting a workspace that has more than one child
163 ######################################################################
164
165 $tmp = fresh_workspace;
166
167 cmd 'open';
168 cmd 'open';
169 cmd 'focus parent';
170 cmd 'split v';
171 cmd 'open';
172
173 my $content = get_ws_content($tmp);
174 my $fst = $content->[0];
175 my $snd = $content->[1];
176
177 is(@{$content}, 2, 'two containers on workspace');
178 is(@{$fst->{nodes}}, 2, 'first child has two children');
179 is(@{$snd->{nodes}}, 0, 'second child has no children');
180
181 ######################################################################
182 # Test split toggle
183 ######################################################################
184
185 $tmp = fresh_workspace;
186 cmd 'split h';
187 cmd 'split toggle';
188 $ws = get_ws($tmp);
189 is($ws->{layout}, 'splitv', 'toggled workspace split');
190
191 $tmp = fresh_workspace;
192 cmd 'split h';
193 cmd 'split toggle';
194 cmd 'split toggle';
195 $ws = get_ws($tmp);
196 is($ws->{layout}, 'splith', 'toggled workspace back and forth');
197
198 cmd 'open';
199 cmd 'open';
200 cmd 'split toggle';
201
202 $content = get_ws_content($tmp);
203 my $second = $content->[1];
204 is($second->{layout}, 'splitv', 'toggled container orientation to vertical');
205
206 cmd 'split toggle';
207 $content = get_ws_content($tmp);
208 $second = $content->[1];
209 is($second->{layout}, 'splith', 'toggled container orientation back to horizontal');
210
211 done_testing;