]> git.sur5r.net Git - i3/i3/blob - testcases/t/122-split.t
7f8f392d4ab74254830686bec9d943d7639006a5
[i3/i3] / testcases / t / 122-split.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Tests splitting
5 #
6 use i3test;
7 use List::Util qw(first);
8
9 my $tmp;
10 my $ws;
11
12 ################################################################################
13 # Open two containers, split, open another container. Then verify
14 # the layout is like we expect it to be
15 ################################################################################
16
17 sub verify_split_layout {
18     my (%args) = @_;
19
20     $tmp = fresh_workspace;
21
22     $ws = get_ws($tmp);
23     is($ws->{layout}, 'splith', 'orientation horizontal by default');
24     cmd 'split v';
25     $ws = get_ws($tmp);
26     is($ws->{layout}, 'splitv', 'split v changes workspace orientation');
27
28     cmd 'open';
29     cmd 'open';
30     my $content = get_ws_content($tmp);
31
32     is(@{$content}, 2, 'two containers on workspace level');
33     my $first = $content->[0];
34     my $second = $content->[1];
35
36     is(@{$first->{nodes}}, 0, 'first container has no children');
37     is(@{$second->{nodes}}, 0, 'second container has no children (yet)');
38     my $old_name = $second->{name};
39
40     cmd $args{split_command};
41     cmd 'open';
42
43     $content = get_ws_content($tmp);
44
45     is(@{$content}, 2, 'two containers on workspace level');
46     $first = $content->[0];
47     $second = $content->[1];
48
49     is(@{$first->{nodes}}, 0, 'first container has no children');
50     isnt($second->{name}, $old_name, 'second container was replaced');
51     is($second->{layout}, 'splith', 'orientation is horizontal');
52     is(@{$second->{nodes}}, 2, 'second container has 2 children');
53     is($second->{nodes}->[0]->{name}, $old_name, 'found old second container');
54 }
55
56 verify_split_layout(split_command => 'split h');
57 verify_split_layout(split_command => 'split horizontal');
58
59 # TODO: extend this test-case (test next/prev)
60 # - wrapping (no horizontal switch possible, goes level-up)
61 # - going level-up "manually"
62
63 ######################################################################
64 # Test splitting multiple times without actually creating windows
65 ######################################################################
66
67 $tmp = fresh_workspace;
68
69 $ws = get_ws($tmp);
70 is($ws->{layout}, 'splith', 'orientation horizontal by default');
71 cmd 'split v';
72 $ws = get_ws($tmp);
73 is($ws->{layout}, 'splitv', 'split v changes workspace orientation');
74
75 cmd 'open';
76 my @content = @{get_ws_content($tmp)};
77
78 # recursively sums up all nodes and their children
79 sub sum_nodes {
80     my ($nodes) = @_;
81
82     return 0 if !@{$nodes};
83
84     my @children = (map { @{$_->{nodes}} } @{$nodes},
85                     map { @{$_->{'floating_nodes'}} } @{$nodes});
86
87     return @{$nodes} + sum_nodes(\@children);
88 }
89
90 my $old_count = sum_nodes(\@content);
91 cmd 'split v';
92
93 @content = @{get_ws_content($tmp)};
94 $old_count = sum_nodes(\@content);
95
96 cmd 'split v';
97
98 @content = @{get_ws_content($tmp)};
99 my $count = sum_nodes(\@content);
100 is($count, $old_count, 'not more windows after splitting again');
101
102 ######################################################################
103 # In the special case of being inside a stacked or tabbed container, we don’t
104 # want this to happen.
105 ######################################################################
106
107 $tmp = fresh_workspace;
108
109 cmd 'open';
110 @content = @{get_ws_content($tmp)};
111 is(scalar @content, 1, 'Precisely one container on this ws');
112 cmd 'layout stacked';
113 @content = @{get_ws_content($tmp)};
114 is(scalar @content, 1, 'Still one container on this ws');
115 is(scalar @{$content[0]->{nodes}}, 1, 'Stacked con has one child node');
116
117 cmd 'split h';
118 cmd 'open';
119 @content = @{get_ws_content($tmp)};
120 is(scalar @content, 1, 'Still one container on this ws');
121 is(scalar @{$content[0]->{nodes}}, 1, 'Stacked con still has one child node');
122
123 ################################################################################
124 # When focusing the workspace, changing the layout should have an effect on the
125 # workspace, not on the parent (CT_CONTENT) container.
126 ################################################################################
127
128 sub get_output_content {
129     my $tree = i3(get_socket_path())->get_tree->recv;
130
131     my @outputs = grep { $_->{name} !~ /^__/ } @{$tree->{nodes}};
132     is(scalar @outputs, 1, 'exactly one output (testcase not multi-monitor capable)');
133     my $output = $outputs[0];
134     # get the first (and only) CT_CON
135     return first { $_->{type} == 2 } @{$output->{nodes}};
136 }
137
138 $tmp = fresh_workspace;
139
140 cmd 'open';
141 cmd 'split v';
142 cmd 'open';
143 cmd 'focus parent';
144 is(get_output_content()->{layout}, 'splith', 'content container layout ok');
145 cmd 'layout stacked';
146 is(get_output_content()->{layout}, 'splith', 'content container layout still ok');
147
148 done_testing;