2 # vim:ts=4:sw=4:expandtab
4 # Please read the following documents before working on tests:
5 # • https://build.i3wm.org/docs/testsuite.html
8 # • https://build.i3wm.org/docs/lib-i3test.html
9 # (alternatively: perldoc ./testcases/lib/i3test.pm)
11 # • https://build.i3wm.org/docs/ipc.html
14 # • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
15 # (unless you are already familiar with Perl)
17 # Check if the focus is correctly restored after closing windows.
20 use List::Util qw(first);
22 my $i3 = i3(get_socket_path());
24 my $tmp = fresh_workspace;
26 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
28 my $first = open_empty_con($i3);
29 my $second = open_empty_con($i3);
33 my ($nodes, $focus) = get_ws_content($tmp);
35 ok(!$nodes->[1]->{focused}, 'split container not focused');
37 ($nodes, $focus) = get_ws_content($tmp);
38 ok($nodes->[1]->{focused}, 'split container focused after focus parent');
40 my $third = open_empty_con($i3);
42 isnt(get_focused($tmp), $second, 'different container focused');
44 # We have the following layout now (con is focused):
52 ##############################################################
53 # see if the focus goes down to $first (not to its split parent)
54 # when closing $second
55 ##############################################################
60 ($nodes, $focus) = get_ws_content($tmp);
61 is($nodes->[1]->{nodes}->[0]->{id}, $second, 'second container found');
62 ok($nodes->[1]->{nodes}->[0]->{focused}, 'second container focused');
64 ##############################################################
65 # another case, using a slightly different layout (regression)
66 ##############################################################
68 $tmp = fresh_workspace;
70 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
73 $first = open_empty_con($i3);
74 my $bottom = open_empty_con($i3);
78 my $middle = open_empty_con($i3);
79 my $right = open_empty_con($i3);
82 # We have the following layout now (second is focused):
83 # .----------------------------.
84 # | .------------------------. |
85 # | | first | middle | right | |
86 # | `------------------------' |
87 # |----------------------------|
91 # `----------------------------'
93 # Check if the focus is restored to $right when we close $second
96 is(get_focused($tmp), $right, 'top right container focused (in focus stack)');
98 ($nodes, $focus) = get_ws_content($tmp);
99 my $tr = first { $_->{id} eq $right } @{$nodes->[0]->{nodes}};
100 is($tr->{focused}, 1, 'top right container really has focus');
102 ##############################################################
103 # check if focus is correct after closing an unfocused window
104 ##############################################################
106 $tmp = fresh_workspace;
108 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
110 $first = open_empty_con($i3);
111 $middle = open_empty_con($i3);
112 # XXX: the $right empty con will be filled with the x11 window we are creating afterwards
113 $right = open_empty_con($i3);
114 my $win = open_window({ background_color => '#00ff00' });
116 cmd qq|[con_id="$middle"] focus|;
120 is(get_focused($tmp), $middle, 'middle container focused');
122 ##############################################################
123 # check if the workspace container can be closed
124 ##############################################################
126 $tmp = fresh_workspace;
128 my $window = open_window();
130 # one window opened on the current workspace
131 ($nodes, $focus) = get_ws_content($tmp);
132 is(scalar @$nodes, 1, 'workspace contains one node');
134 # focus the workspace
138 # try to kill the workspace
142 # the workspace should now be empty
143 ($nodes, $focus) = get_ws_content($tmp);
144 is(scalar @$nodes, 0, 'workspace is empty');
146 ################################################################################
147 # check if killing a workspace also closes floating windows.
148 ################################################################################
150 $tmp = fresh_workspace;
152 $window = open_window;
153 my $floating_window = open_floating_window;
155 # one window opened on the current workspace
156 ($nodes, $focus) = get_ws_content($tmp);
157 is(scalar @$focus, 2, 'workspace contains two nodes');
159 # focus the workspace
163 # try to kill the workspace
167 # the workspace should now be empty
168 ($nodes, $focus) = get_ws_content($tmp);
169 is(scalar @$focus, 0, 'workspace is empty');
171 ##############################################################
172 # and now for something completely different:
173 # check if the pointer position is relevant when restoring focus
174 # (it should not be relevant, of course)
175 ##############################################################
177 # TODO: add test code as soon as I can reproduce it