aboutsummaryrefslogtreecommitdiff
path: root/testcases/t/274-move-branch-position.t
blob: e4d97819f3e6d988c60f65585882682b3a9e6155 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!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)
#
# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
#   (unless you are already familiar with Perl)
#
# Test that movement of a con into a branch will place the moving con at the
# correct position within the branch.
#
# If the direction of movement is the same as the orientation of the branch
# container, append or prepend the container to the branch in the obvious way.
# If the movement is to the right or downward, insert the moving container in
# the first position (i.e., the leftmost or top position resp.) If the movement
# is to the left or upward, insert the moving container in the last position
# (i.e., the rightmost or bottom position resp.)
#
# If the direction of movement is different from the orientation of the branch
# container, insert the container into the branch after the focused-inactive
# container.
#
# For testing purposes, we will demonstrate the behavior for tabbed containers
# to represent the case of split-horizontal branches and stacked containers to
# represent the case of split-vertical branches.
#
# Ticket: #1060
# Bug still in: 4.6-109-g18cfc36

use i3test;

# Opens tabs on the presently focused branch and adds several additional
# windows. Shifts focus to somewhere in the middle of the tabs so the most
# general case can be assumed.
sub open_tabs {
    cmd 'layout tabbed';
    open_window;
    open_window;
    open_window;
    open_window;
    cmd 'focus left; focus left'
}

# Likewise for a stack
sub open_stack {
    cmd 'layout stacking';
    open_window;
    open_window;
    open_window;
    open_window;
    cmd 'focus up; focus up'
}

# Gets the position of the given leaf within the given branch. The first
# position is one (1). Returns negative one (-1) if the leaf cannot be found
# within the branch.
sub get_leaf_position {
    my ($branch, $leaf) = @_;
    my $position = -1;
    for my $i (0 .. @{$branch->{nodes}}) {
        if ($branch->{nodes}[$i]->{id} == $leaf) {
            $position = $i + 1;
            last;
        };
    }
    return $position;
}

# convenience function to focus a con by id to avoid having to type an ugly
# command each time
sub focus_con {
    my $con_id = shift @_;
    cmd "[con_id=\"$con_id\"] focus";
}

# Places a leaf into a branch and focuses the leaf. The newly created branch
# will have orientation specified by the second parameter.
sub branchify {
    my ($con_id, $orientation) = @_;
    focus_con($con_id);
    $orientation eq 'horizontal' ? cmd 'splith' : cmd 'splitv';
    open_window;
    focus_con($con_id);
}

##############################################################################
# When moving a con right into tabs, the moving con should be placed as the
# first tab in the branch
##############################################################################
my $ws = fresh_workspace;

# create the target leaf
open_window;
my $target_leaf = get_focused($ws);

# create the tabbed branch container
open_window;
cmd 'splith';
open_tabs;

# move the target leaf into the tabbed branch
focus_con($target_leaf);
cmd 'move right';

# the target leaf should be the first in the branch
my $branch = shift @{get_ws_content($ws)};
is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con right into tabs placed it as the first tab in the branch');

# repeat the test when the target is in a branch
cmd 'move up; move left';
branchify($target_leaf, 'vertical');
cmd 'move right';

$branch = pop @{get_ws_content($ws)};
is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con right into tabs from a branch placed it as the first tab in the branch');

##############################################################################
# When moving a con right into a stack, the moving con should be placed
# below the focused-inactive leaf
##############################################################################
$ws = fresh_workspace;

# create the target leaf
open_window;
$target_leaf = get_focused($ws);

# create the stacked branch container and find the focused leaf
open_window;
cmd 'splith';
open_stack;
my $secondary_leaf = get_focused($ws);

# move the target leaf into the stacked branch
focus_con($target_leaf);
cmd 'move right';

# the secondary focus leaf should be below the target
$branch = shift @{get_ws_content($ws)};
my $target_leaf_position = get_leaf_position($branch, $target_leaf);
my $secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con right into a stack placed it below the focused-inactive leaf');

# repeat the test when the target is in a branch
cmd 'move up; move left';
branchify($target_leaf, 'vertical');
cmd 'move right';

$branch = pop @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con right into a stack from a branch placed it below the focused-inactive leaf');

##############################################################################
# When moving a con down into a stack, the moving con should be placed at the
# top of the stack
##############################################################################
$ws = fresh_workspace;
cmd 'layout splitv';

# create the target leaf
open_window;
$target_leaf = get_focused($ws);

# create the stacked branch container
open_window;
cmd 'splitv';
open_stack;

# move the target leaf into the stacked branch
focus_con($target_leaf);
cmd 'move down';

# the target leaf should be on the top of the stack
$branch = shift @{get_ws_content($ws)};
is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con down into a stack placed it on the top of the stack');

# repeat the test when the target is in a branch
cmd 'move right; move up';
branchify($target_leaf, 'horizontal');
cmd 'move down';

$branch = pop @{get_ws_content($ws)};
is($branch->{nodes}[0]->{id}, $target_leaf, 'moving con down into a stack from a branch placed it on the top of the stack');

##############################################################################
# When moving a con down into tabs, the moving con should be placed after the
# focused-inactive tab
##############################################################################
$ws = fresh_workspace;
cmd 'layout splitv';

# create the target leaf
open_window;
$target_leaf = get_focused($ws);

# create the tabbed branch container and find the focused tab
open_window;
cmd 'splitv';
open_tabs;
$secondary_leaf = get_focused($ws);

# move the target leaf into the tabbed branch
focus_con($target_leaf);
cmd 'move down';

# the secondary focus tab should be to the right
$branch = shift @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con down into tabs placed it after the focused-inactive tab');

# repeat the test when the target is in a branch
cmd 'move right; move up';
branchify($target_leaf, 'horizontal');
cmd 'move down';

$branch = pop @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con down into tabs from a branch placed it after the focused-inactive tab');

##############################################################################
# When moving a con left into tabs, the moving con should be placed as the last
# tab in the branch
##############################################################################
$ws = fresh_workspace;

# create the tabbed branch container
open_window;
cmd 'splith';
open_tabs;

# create the target leaf
cmd 'focus parent';
open_window;
$target_leaf = get_focused($ws);

# move the target leaf into the tabbed branch
cmd 'move left';

# the target leaf should be last in the branch
$branch = shift @{get_ws_content($ws)};

is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con left into tabs placed it as the last tab in the branch');

# repeat the test when the target leaf is in a branch
cmd 'move up; move right';
branchify($target_leaf, 'vertical');
cmd 'move left';

$branch = shift @{get_ws_content($ws)};
is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con left into tabs from a branch placed it as the last tab in the branch');

##############################################################################
# When moving a con left into a stack, the moving con should be placed below
# the focused-inactive leaf
##############################################################################
$ws = fresh_workspace;

# create the stacked branch container and find the focused leaf
open_window;
open_stack;
$secondary_leaf = get_focused($ws);

# create the target leaf to the right
cmd 'focus parent';
open_window;
$target_leaf = get_focused($ws);

# move the target leaf into the stacked branch
cmd 'move left';

# the secondary focus leaf should be below
$branch = shift @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con left into a stack placed it below the focused-inactive leaf');

# repeat the test when the target leaf is in a branch
cmd 'move up; move right';
branchify($target_leaf, 'vertical');
cmd 'move left';

$branch = shift @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con left into a stack from a branch placed it below the focused-inactive leaf');

##############################################################################
# When moving a con up into a stack, the moving con should be placed last in
# the stack
##############################################################################
$ws = fresh_workspace;
cmd 'layout splitv';

# create the stacked branch container
open_window;
cmd 'splitv';
open_stack;

# create the target leaf
cmd 'focus parent';
open_window;
$target_leaf = get_focused($ws);

# move the target leaf into the stacked branch
cmd 'move up';

# the target leaf should be on the bottom of the stack
$branch = shift @{get_ws_content($ws)};

is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con up into stack placed it on the bottom of the stack');

# repeat the test when the target leaf is in a branch
cmd 'move right; move down';
branchify($target_leaf, 'horizontal');
cmd 'move up';

$branch = shift @{get_ws_content($ws)};

is($branch->{nodes}->[-1]->{id}, $target_leaf, 'moving con up into stack from a branch placed it on the bottom of the stack');

##############################################################################
# When moving a con up into tabs, the moving con should be placed after the
# focused-inactive tab
##############################################################################
$ws = fresh_workspace;
cmd 'layout splitv';

# create the tabbed branch container and find the focused leaf
open_window;
cmd 'splitv';
open_tabs;
$secondary_leaf = get_focused($ws);

# create the target leaf below
cmd 'focus parent';
open_window;
$target_leaf = get_focused($ws);

# move the target leaf into the tabbed branch
cmd 'move up';

# the secondary focus tab should be to the right
$branch = shift @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con up into tabs placed it after the focused-inactive tab');

# repeat the test when the target leaf is in a branch
cmd 'move right; move down';
branchify($target_leaf, 'horizontal');
cmd 'move up';

$branch = shift @{get_ws_content($ws)};
$target_leaf_position = get_leaf_position($branch, $target_leaf);
$secondary_leaf_position = get_leaf_position($branch, $secondary_leaf);

is($target_leaf_position, $secondary_leaf_position + 1, 'moving con up into tabs from a branch placed it after the focused-inactive tab');

done_testing;