]> git.sur5r.net Git - i3/i3/blob - testcases/t/129-focus-after-close.t
Merge branch 'fix-comment'
[i3/i3] / testcases / t / 129-focus-after-close.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # Check if the focus is correctly restored after closing windows.
5 #
6 use i3test;
7 use X11::XCB qw(:all);
8 use List::Util qw(first);
9
10 my $x = X11::XCB::Connection->new;
11
12 my $i3 = i3(get_socket_path());
13
14 my $tmp = fresh_workspace;
15
16 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
17
18 my $first = open_empty_con($i3);
19 my $second = open_empty_con($i3);
20
21 cmd 'split v';
22
23 my ($nodes, $focus) = get_ws_content($tmp);
24
25 ok(!$nodes->[1]->{focused}, 'split container not focused');
26 cmd 'focus parent';
27 ($nodes, $focus) = get_ws_content($tmp);
28 ok($nodes->[1]->{focused}, 'split container focused after focus parent');
29
30 my $third = open_empty_con($i3);
31
32 isnt(get_focused($tmp), $second, 'different container focused');
33
34 # We have the following layout now (con is focused):
35 # .----------------.
36 # | split  |       |
37 # | .----. |  con  |
38 # | | cn | |       |
39 # | `----' |       |
40 # `----------------'
41
42 ##############################################################
43 # see if the focus goes down to $first (not to its split parent)
44 # when closing $second
45 ##############################################################
46
47 cmd 'kill';
48 # TODO: this testcase sometimes has different outcomes when the
49 # sleep is missing. why?
50 sleep 0.25;
51 ($nodes, $focus) = get_ws_content($tmp);
52 is($nodes->[1]->{nodes}->[0]->{id}, $second, 'second container found');
53 ok($nodes->[1]->{nodes}->[0]->{focused}, 'second container focused');
54
55 ##############################################################
56 # another case, using a slightly different layout (regression)
57 ##############################################################
58
59 $tmp = fresh_workspace;
60
61 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
62
63 cmd 'split v';
64 $first = open_empty_con($i3);
65 my $bottom = open_empty_con($i3);
66
67 cmd 'focus up';
68 cmd 'split h';
69 my $middle = open_empty_con($i3);
70 my $right = open_empty_con($i3);
71 cmd 'focus down';
72
73 # We have the following layout now (second is focused):
74 # .----------------------------.
75 # | .------------------------. |
76 # | | first | middle | right | |
77 # | `------------------------' |
78 # |----------------------------|
79 # |                            |
80 # |          second            |
81 # |                            |
82 # `----------------------------'
83
84 # Check if the focus is restored to $right when we close $second
85 cmd 'kill';
86
87 is(get_focused($tmp), $right, 'top right container focused (in focus stack)');
88
89 ($nodes, $focus) = get_ws_content($tmp);
90 my $tr = first { $_->{id} eq $right } @{$nodes->[0]->{nodes}};
91 is($tr->{focused}, 1, 'top right container really has focus');
92
93 ##############################################################
94 # check if focus is correct after closing an unfocused window
95 ##############################################################
96
97 $tmp = fresh_workspace;
98
99 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
100
101 $first = open_empty_con($i3);
102 $middle = open_empty_con($i3);
103 # XXX: the $right empty con will be filled with the x11 window we are creating afterwards
104 $right = open_empty_con($i3);
105 my $win = open_window($x, { background_color => '#00ff00' });
106
107 cmd qq|[con_id="$middle"] focus|;
108 $win->destroy;
109
110 sleep 0.25;
111
112 is(get_focused($tmp), $middle, 'middle container focused');
113
114 ##############################################################
115 # and now for something completely different:
116 # check if the pointer position is relevant when restoring focus
117 # (it should not be relevant, of course)
118 ##############################################################
119
120 # TODO: add test code as soon as I can reproduce it
121
122 done_testing;