aboutsummaryrefslogtreecommitdiff
path: root/testcases/t/122-split.t
blob: 66f26ce6897c53a6c6a053b0eb6563b8425b27e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!perl
# vim:ts=4:sw=4:expandtab
#
# Please read the following documents before working on tests:
# • https://build.i3wm.org/docs/testsuite.html
#   (or docs/testsuite)
#
# • https://build.i3wm.org/docs/lib-i3test.html
#   (alternatively: perldoc ./testcases/lib/i3test.pm)
#
# • https://build.i3wm.org/docs/ipc.html
#   (or docs/ipc)
#
# • https://i3wm.org/downloads/modern_perl_a4.pdf
#   (unless you are already familiar with Perl)
#
# Tests splitting
#
use i3test;
use List::Util qw(first);

my $tmp;
my $ws;

################################################################################
# Open two containers, split, open another container. Then verify
# the layout is like we expect it to be
################################################################################

sub verify_split_layout {
    my (%args) = @_;

    $tmp = fresh_workspace;

    $ws = get_ws($tmp);
    is($ws->{layout}, 'splith', 'orientation horizontal by default');
    cmd 'split v';
    $ws = get_ws($tmp);
    is($ws->{layout}, 'splitv', 'split v changes workspace orientation');

    cmd 'open';
    cmd 'open';
    my $content = get_ws_content($tmp);

    is(@{$content}, 2, 'two containers on workspace level');
    my $first = $content->[0];
    my $second = $content->[1];

    is(@{$first->{nodes}}, 0, 'first container has no children');
    is(@{$second->{nodes}}, 0, 'second container has no children (yet)');
    my $old_id = $second->{id};

    cmd $args{split_command};
    cmd 'open';

    $content = get_ws_content($tmp);

    is(@{$content}, 2, 'two containers on workspace level');
    $first = $content->[0];
    $second = $content->[1];

    is(@{$first->{nodes}}, 0, 'first container has no children');
    isnt($second->{id}, $old_id, 'second container was replaced');
    is($second->{layout}, 'splith', 'orientation is horizontal');
    is(@{$second->{nodes}}, 2, 'second container has 2 children');
    is($second->{nodes}->[0]->{id}, $old_id, 'found old second container');
}

verify_split_layout(split_command => 'split h');
verify_split_layout(split_command => 'split horizontal');

# TODO: extend this test-case (test next/prev)
# - wrapping (no horizontal switch possible, goes level-up)
# - going level-up "manually"

######################################################################
# Test splitting multiple times without actually creating windows
######################################################################

$tmp = fresh_workspace;

$ws = get_ws($tmp);
is($ws->{layout}, 'splith', 'orientation horizontal by default');
cmd 'split v';
$ws = get_ws($tmp);
is($ws->{layout}, 'splitv', 'split v changes workspace orientation');

cmd 'open';
my @content = @{get_ws_content($tmp)};

# recursively sums up all nodes and their children
sub sum_nodes {
    my ($nodes) = @_;

    return 0 if !@{$nodes};

    my @children = (map { @{$_->{nodes}} } @{$nodes},
                    map { @{$_->{'floating_nodes'}} } @{$nodes});

    return @{$nodes} + sum_nodes(\@children);
}

my $old_count = sum_nodes(\@content);
cmd 'split v';

@content = @{get_ws_content($tmp)};
$old_count = sum_nodes(\@content);

cmd 'split v';

@content = @{get_ws_content($tmp)};
my $count = sum_nodes(\@content);
is($count, $old_count, 'not more windows after splitting again');

######################################################################
# In the special case of being inside a stacked or tabbed container, we don’t
# want this to happen.
######################################################################

$tmp = fresh_workspace;

cmd 'open';
@content = @{get_ws_content($tmp)};
is(scalar @content, 1, 'Precisely one container on this ws');
cmd 'layout stacked';
@content = @{get_ws_content($tmp)};
is(scalar @content, 1, 'Still one container on this ws');
is(scalar @{$content[0]->{nodes}}, 1, 'Stacked con has one child node');

cmd 'split h';
cmd 'open';
@content = @{get_ws_content($tmp)};
is(scalar @content, 1, 'Still one container on this ws');
is(scalar @{$content[0]->{nodes}}, 1, 'Stacked con still has one child node');

################################################################################
# When focusing the workspace, changing the layout should have an effect on the
# workspace, not on the parent (CT_CONTENT) container.
################################################################################

sub get_output_content {
    my $tree = i3(get_socket_path())->get_tree->recv;

    my @outputs = grep { $_->{name} !~ /^__/ } @{$tree->{nodes}};
    is(scalar @outputs, 1, 'exactly one output (testcase not multi-monitor capable)');
    my $output = $outputs[0];
    # get the first (and only) CT_CON
    return first { $_->{type} eq 'con' } @{$output->{nodes}};
}

$tmp = fresh_workspace;

cmd 'open';
cmd 'split v';
cmd 'open';
cmd 'focus parent';
is(get_output_content()->{layout}, 'splith', 'content container layout ok');
cmd 'layout stacked';
is(get_output_content()->{layout}, 'splith', 'content container layout still ok');

######################################################################
# Splitting a workspace that has more than one child
######################################################################

$tmp = fresh_workspace;

cmd 'open';
cmd 'open';
cmd 'focus parent';
cmd 'split v';
cmd 'open';

my $content = get_ws_content($tmp);
my $fst = $content->[0];
my $snd = $content->[1];

is(@{$content}, 2, 'two containers on workspace');
is(@{$fst->{nodes}}, 2, 'first child has two children');
is(@{$snd->{nodes}}, 0, 'second child has no children');

######################################################################
# Test split toggle
######################################################################

$tmp = fresh_workspace;
cmd 'split h';
cmd 'split toggle';
$ws = get_ws($tmp);
is($ws->{layout}, 'splitv', 'toggled workspace split');

$tmp = fresh_workspace;
cmd 'split h';
cmd 'split toggle';
cmd 'split toggle';
$ws = get_ws($tmp);
is($ws->{layout}, 'splith', 'toggled workspace back and forth');

cmd 'open';
cmd 'open';
cmd 'split toggle';

$content = get_ws_content($tmp);
my $second = $content->[1];
is($second->{layout}, 'splitv', 'toggled container orientation to vertical');

cmd 'split toggle';
$content = get_ws_content($tmp);
$second = $content->[1];
is($second->{layout}, 'splith', 'toggled container orientation back to horizontal');

done_testing;