]> git.sur5r.net Git - i3/i3/blob - testcases/t/213-move-branch-position.t
Kill windows between tests
[i3/i3] / testcases / t / 213-move-branch-position.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 # Test that movement of a con into a branch will place the moving con at the
18 # correct position within the branch.
19 #
20 # If the direction of movement is the same as the orientation of the branch
21 # container, append or prepend the container to the branch in the obvious way.
22 # If the movement is to the right or downward, insert the moving container in
23 # the first position (i.e., the leftmost or top position resp.) If the movement
24 # is to the left or upward, insert the moving container in the last position
25 # (i.e., the rightmost or bottom position resp.)
26 #
27 # If the direction of movement is different from the orientation of the branch
28 # container, insert the container into the branch after the focused-inactive
29 # container.
30 #
31 # For testing purposes, we will demonstrate the behavior for tabbed containers
32 # to represent the case of split-horizontal branches and stacked containers to
33 # represent the case of split-vertical branches.
34 #
35 # Ticket: #1060
36 # Bug still in: 4.6-109-g18cfc36
37
38 use i3test;
39
40 # Opens tabs on the presently focused branch and adds several additional
41 # windows. Shifts focus to somewhere in the middle of the tabs so the most
42 # general case can be assumed.
43 sub open_tabs {
44     cmd 'layout tabbed';
45     open_window;
46     open_window;
47     open_window;
48     open_window;
49     cmd 'focus left; focus left'
50 }
51
52 # Likewise for a stack
53 sub open_stack {
54     cmd 'layout stacking';
55     open_window;
56     open_window;
57     open_window;
58     open_window;
59     cmd 'focus up; focus up'
60 }
61
62 # Gets the position of the given leaf within the given branch. The first
63 # position is one (1). Returns negative one (-1) if the leaf cannot be found
64 # within the branch.
65 sub get_leaf_position {
66     my ($branch, $leaf) = @_;
67     my $position = -1;
68     for my $i (0 .. @{$branch->{nodes}}) {
69         if ($branch->{nodes}[$i]->{id} == $leaf) {
70             $position = $i + 1;
71             last;
72         };
73     }
74     return $position;
75 }
76
77 # convenience function to focus a con by id to avoid having to type an ugly
78 # command each time
79 sub focus_con {
80     my $con_id = shift @_;
81     cmd "[con_id=\"$con_id\"] focus";
82 }
83
84 # Places a leaf into a branch and focuses the leaf. The newly created branch
85 # will have orientation specified by the second parameter.
86 sub branchify {
87     my ($con_id, $orientation) = @_;
88     focus_con($con_id);
89     $orientation eq 'horizontal' ? cmd 'splith' : cmd 'splitv';
90     open_window;
91     focus_con($con_id);
92 }
93
94 ##############################################################################
95 # When moving a con right into tabs, the moving con should be placed as the
96 # first tab in the branch
97 ##############################################################################
98 my $ws = fresh_workspace;
99
100 # create the target leaf
101 open_window;
102 my $target_leaf = get_focused($ws);
103
104 # create the tabbed branch container
105 open_window;
106 cmd 'splith';
107 open_tabs;
108
109 # move the target leaf into the tabbed branch
110 focus_con($target_leaf);
111 cmd 'move right';
112
113 # the target leaf should be the first in the branch
114 my $branch = shift @{get_ws_content($ws)};
115 is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con right into tabs placed it as the first tab in the branch');
116
117 # repeat the test when the target is in a branch
118 cmd 'move up; move left';
119 branchify($target_leaf, 'vertical');
120 cmd 'move right';
121
122 $branch = pop @{get_ws_content($ws)};
123 is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con right into tabs from a branch placed it as the first tab in the branch');
124
125 ##############################################################################
126 # When moving a con right into a stack, the moving con should be placed
127 # below the focused-inactive leaf
128 ##############################################################################
129 $ws = fresh_workspace;
130
131 # create the target leaf
132 open_window;
133 $target_leaf = get_focused($ws);
134
135 # create the stacked branch container and find the focused leaf
136 open_window;
137 cmd 'splith';
138 open_stack;
139 my $secondary_leaf = get_focused($ws);
140
141 # move the target leaf into the stacked branch
142 focus_con($target_leaf);
143 cmd 'move right';
144
145 # the secondary focus leaf should be below the target
146 $branch = shift @{get_ws_content($ws)};
147 my $target_leaf_position = get_leaf_position($branch, $target_leaf);
148 my $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
149
150 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con right into a stack placed it below the focused-inactive leaf');
151
152 # repeat the test when the target is in a branch
153 cmd 'move up; move left';
154 branchify($target_leaf, 'vertical');
155 cmd 'move right';
156
157 $branch = pop @{get_ws_content($ws)};
158 $target_leaf_position = get_leaf_position($branch, $target_leaf);
159 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
160
161 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con right into a stack from a branch placed it below the focused-inactive leaf');
162
163 ##############################################################################
164 # When moving a con down into a stack, the moving con should be placed at the
165 # top of the stack
166 ##############################################################################
167 $ws = fresh_workspace;
168 cmd 'layout splitv';
169
170 # create the target leaf
171 open_window;
172 $target_leaf = get_focused($ws);
173
174 # create the stacked branch container
175 open_window;
176 cmd 'splitv';
177 open_stack;
178
179 # move the target leaf into the stacked branch
180 focus_con($target_leaf);
181 cmd 'move down';
182
183 # the target leaf should be on the top of the stack
184 $branch = shift @{get_ws_content($ws)};
185 is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con down into a stack placed it on the top of the stack');
186
187 # repeat the test when the target is in a branch
188 cmd 'move right; move up';
189 branchify($target_leaf, 'horizontal');
190 cmd 'move down';
191
192 $branch = pop @{get_ws_content($ws)};
193 is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con down into a stack from a branch placed it on the top of the stack');
194
195 ##############################################################################
196 # When moving a con down into tabs, the moving con should be placed after the
197 # focused-inactive tab
198 ##############################################################################
199 $ws = fresh_workspace;
200 cmd 'layout splitv';
201
202 # create the target leaf
203 open_window;
204 $target_leaf = get_focused($ws);
205
206 # create the tabbed branch container and find the focused tab
207 open_window;
208 cmd 'splitv';
209 open_tabs;
210 $secondary_leaf = get_focused($ws);
211
212 # move the target leaf into the tabbed branch
213 focus_con($target_leaf);
214 cmd 'move down';
215
216 # the secondary focus tab should be to the right
217 $branch = shift @{get_ws_content($ws)};
218 $target_leaf_position = get_leaf_position($branch, $target_leaf);
219 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
220
221 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con down into tabs placed it after the focused-inactive tab');
222
223 # repeat the test when the target is in a branch
224 cmd 'move right; move up';
225 branchify($target_leaf, 'horizontal');
226 cmd 'move down';
227
228 $branch = pop @{get_ws_content($ws)};
229 $target_leaf_position = get_leaf_position($branch, $target_leaf);
230 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
231
232 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con down into tabs from a branch placed it after the focused-inactive tab');
233
234 ##############################################################################
235 # When moving a con left into tabs, the moving con should be placed as the last
236 # tab in the branch
237 ##############################################################################
238 $ws = fresh_workspace;
239
240 # create the tabbed branch container
241 open_window;
242 cmd 'splith';
243 open_tabs;
244
245 # create the target leaf
246 cmd 'focus parent';
247 open_window;
248 $target_leaf = get_focused($ws);
249
250 # move the target leaf into the tabbed branch
251 cmd 'move left';
252
253 # the target leaf should be last in the branch
254 $branch = shift @{get_ws_content($ws)};
255
256 is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con left into tabs placed it as the last tab in the branch');
257
258 # repeat the test when the target leaf is in a branch
259 cmd 'move up; move right';
260 branchify($target_leaf, 'vertical');
261 cmd 'move left';
262
263 $branch = shift @{get_ws_content($ws)};
264 is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con left into tabs from a branch placed it as the last tab in the branch');
265
266 ##############################################################################
267 # When moving a con left into a stack, the moving con should be placed below
268 # the focused-inactive leaf
269 ##############################################################################
270 $ws = fresh_workspace;
271
272 # create the stacked branch container and find the focused leaf
273 open_window;
274 open_stack;
275 $secondary_leaf = get_focused($ws);
276
277 # create the target leaf to the right
278 cmd 'focus parent';
279 open_window;
280 $target_leaf = get_focused($ws);
281
282 # move the target leaf into the stacked branch
283 cmd 'move left';
284
285 # the secondary focus leaf should be below
286 $branch = shift @{get_ws_content($ws)};
287 $target_leaf_position = get_leaf_position($branch, $target_leaf);
288 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
289
290 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con left into a stack placed it below the focused-inactive leaf');
291
292 # repeat the test when the target leaf is in a branch
293 cmd 'move up; move right';
294 branchify($target_leaf, 'vertical');
295 cmd 'move left';
296
297 $branch = shift @{get_ws_content($ws)};
298 $target_leaf_position = get_leaf_position($branch, $target_leaf);
299 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
300
301 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con left into a stack from a branch placed it below the focused-inactive leaf');
302
303 ##############################################################################
304 # When moving a con up into a stack, the moving con should be placed last in
305 # the stack
306 ##############################################################################
307 $ws = fresh_workspace;
308 cmd 'layout splitv';
309
310 # create the stacked branch container
311 open_window;
312 cmd 'splitv';
313 open_stack;
314
315 # create the target leaf
316 cmd 'focus parent';
317 open_window;
318 $target_leaf = get_focused($ws);
319
320 # move the target leaf into the stacked branch
321 cmd 'move up';
322
323 # the target leaf should be on the bottom of the stack
324 $branch = shift @{get_ws_content($ws)};
325
326 is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con up into stack placed it on the bottom of the stack');
327
328 # repeat the test when the target leaf is in a branch
329 cmd 'move right; move down';
330 branchify($target_leaf, 'horizontal');
331 cmd 'move up';
332
333 $branch = shift @{get_ws_content($ws)};
334
335 is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con up into stack from a branch placed it on the bottom of the stack');
336
337 ##############################################################################
338 # When moving a con up into tabs, the moving con should be placed after the
339 # focused-inactive tab
340 ##############################################################################
341 $ws = fresh_workspace;
342 cmd 'layout splitv';
343
344 # create the tabbed branch container and find the focused leaf
345 open_window;
346 cmd 'splitv';
347 open_tabs;
348 $secondary_leaf = get_focused($ws);
349
350 # create the target leaf below
351 cmd 'focus parent';
352 open_window;
353 $target_leaf = get_focused($ws);
354
355 # move the target leaf into the tabbed branch
356 cmd 'move up';
357
358 # the secondary focus tab should be to the right
359 $branch = shift @{get_ws_content($ws)};
360 $target_leaf_position = get_leaf_position($branch, $target_leaf);
361 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
362
363 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con up into tabs placed it after the focused-inactive tab');
364
365 # repeat the test when the target leaf is in a branch
366 cmd 'move right; move down';
367 branchify($target_leaf, 'horizontal');
368 cmd 'move up';
369
370 $branch = shift @{get_ws_content($ws)};
371 $target_leaf_position = get_leaf_position($branch, $target_leaf);
372 $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);
373
374 is($target_leaf_position, $secondary_leaf_position + 1, 'moving con up into tabs from a branch placed it after the focused-inactive tab');
375
376 done_testing;