2 # vim:ts=4:sw=4:expandtab
4 # Check if the focus is correctly restored after closing windows.
7 use List::Util qw(first);
8 use Time::HiRes qw(sleep);
10 my $i3 = i3("/tmp/nestedcons");
12 my $tmp = get_unused_workspace();
13 $i3->command("workspace $tmp")->recv;
15 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
17 my $first = open_empty_con($i3);
19 $i3->command('split v')->recv;
21 my ($nodes, $focus) = get_ws_content($tmp);
23 is($nodes->[0]->{focused}, 0, 'split container not focused');
24 $i3->command('level up')->recv;
25 ($nodes, $focus) = get_ws_content($tmp);
26 is($nodes->[0]->{focused}, 1, 'split container focused after level up');
28 my $second = open_empty_con($i3);
30 isnt($first, $second, 'different container focused');
32 # We have the following layout now (con is focused):
40 ##############################################################
41 # see if the focus goes down to $first (not to its split parent)
42 # when closing $second
43 ##############################################################
45 $i3->command('kill')->recv;
46 # TODO: this testcase sometimes has different outcomes when the
47 # sleep is missing. why?
49 ($nodes, $focus) = get_ws_content($tmp);
50 is($nodes->[0]->{nodes}->[0]->{id}, $first, 'first container found');
51 is($nodes->[0]->{nodes}->[0]->{focused}, 1, 'first container focused');
53 ##############################################################
54 # another case, using a slightly different layout (regression)
55 ##############################################################
57 $tmp = get_unused_workspace();
58 $i3->command("workspace $tmp")->recv;
60 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
62 $i3->command('split v')->recv;
63 $first = open_empty_con($i3);
64 my $bottom = open_empty_con($i3);
66 $i3->command('prev v')->recv;
67 $i3->command('split h')->recv;
68 my $middle = open_empty_con($i3);
69 my $right = open_empty_con($i3);
70 $i3->command('next v')->recv;
72 # We have the following layout now (second is focused):
73 # .----------------------------.
74 # | .------------------------. |
75 # | | first | middle | right | |
76 # | `------------------------' |
77 # |----------------------------|
81 # `----------------------------'
83 # Check if the focus is restored to $right when we close $second
84 $i3->command('kill')->recv;
86 is(get_focused($tmp), $right, 'top right container focused (in focus stack)');
88 ($nodes, $focus) = get_ws_content($tmp);
89 my $tr = first { $_->{id} eq $right } @{$nodes->[0]->{nodes}};
90 is($tr->{focused}, 1, 'top right container really has focus');
92 ##############################################################
93 # and now for something completely different:
94 # check if the pointer position is relevant when restoring focus
95 # (it should not be relevant, of course)
96 ##############################################################
98 # TODO: add test code as soon as I can reproduce it
100 diag( "Testing i3, Perl $], $^X" );