]> git.sur5r.net Git - i3/i3/blob - testcases/t/129-focus-after-close.t
Merge branch 'master' into next
[i3/i3] / testcases / t / 129-focus-after-close.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 # Check if the focus is correctly restored after closing windows.
18 #
19 use i3test;
20 use List::Util qw(first);
21
22 my $i3 = i3(get_socket_path());
23
24 my $tmp = fresh_workspace;
25
26 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
27
28 my $first = open_empty_con($i3);
29 my $second = open_empty_con($i3);
30
31 cmd 'split v';
32
33 my ($nodes, $focus) = get_ws_content($tmp);
34
35 ok(!$nodes->[1]->{focused}, 'split container not focused');
36 cmd 'focus parent';
37 ($nodes, $focus) = get_ws_content($tmp);
38 ok($nodes->[1]->{focused}, 'split container focused after focus parent');
39
40 my $third = open_empty_con($i3);
41
42 isnt(get_focused($tmp), $second, 'different container focused');
43
44 # We have the following layout now (con is focused):
45 # .----------------.
46 # | split  |       |
47 # | .----. |  con  |
48 # | | cn | |       |
49 # | `----' |       |
50 # `----------------'
51
52 ##############################################################
53 # see if the focus goes down to $first (not to its split parent)
54 # when closing $second
55 ##############################################################
56
57 cmd 'kill';
58 sync_with_i3;
59
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');
63
64 ##############################################################
65 # another case, using a slightly different layout (regression)
66 ##############################################################
67
68 $tmp = fresh_workspace;
69
70 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
71
72 cmd 'split v';
73 $first = open_empty_con($i3);
74 my $bottom = open_empty_con($i3);
75
76 cmd 'focus up';
77 cmd 'split h';
78 my $middle = open_empty_con($i3);
79 my $right = open_empty_con($i3);
80 cmd 'focus down';
81
82 # We have the following layout now (second is focused):
83 # .----------------------------.
84 # | .------------------------. |
85 # | | first | middle | right | |
86 # | `------------------------' |
87 # |----------------------------|
88 # |                            |
89 # |          second            |
90 # |                            |
91 # `----------------------------'
92
93 # Check if the focus is restored to $right when we close $second
94 cmd 'kill';
95
96 is(get_focused($tmp), $right, 'top right container focused (in focus stack)');
97
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');
101
102 ##############################################################
103 # check if focus is correct after closing an unfocused window
104 ##############################################################
105
106 $tmp = fresh_workspace;
107
108 ok(@{get_ws_content($tmp)} == 0, 'no containers yet');
109
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' });
115
116 cmd qq|[con_id="$middle"] focus|;
117 $win->destroy;
118 sync_with_i3;
119
120 is(get_focused($tmp), $middle, 'middle container focused');
121
122 ##############################################################
123 # check if the workspace container can be closed
124 ##############################################################
125
126 $tmp = fresh_workspace;
127
128 my $window = open_window();
129
130 # one window opened on the current workspace
131 ($nodes, $focus) = get_ws_content($tmp);
132 is(scalar @$nodes, 1, 'workspace contains one node');
133
134 # focus the workspace
135 cmd "focus parent";
136 cmd "focus parent";
137
138 # try to kill the workspace
139 cmd "kill";
140 sync_with_i3;
141
142 # the workspace should now be empty
143 ($nodes, $focus) = get_ws_content($tmp);
144 is(scalar @$nodes, 0, 'workspace is empty');
145
146 ################################################################################
147 # check if killing a workspace also closes floating windows.
148 ################################################################################
149
150 $tmp = fresh_workspace;
151
152 $window = open_window;
153 my $floating_window = open_floating_window;
154
155 # one window opened on the current workspace
156 ($nodes, $focus) = get_ws_content($tmp);
157 is(scalar @$focus, 2, 'workspace contains two nodes');
158
159 # focus the workspace
160 cmd "focus parent";
161 cmd "focus parent";
162
163 # try to kill the workspace
164 cmd "kill";
165 sync_with_i3;
166
167 # the workspace should now be empty
168 ($nodes, $focus) = get_ws_content($tmp);
169 is(scalar @$focus, 0, 'workspace is empty');
170
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 ##############################################################
176
177 # TODO: add test code as soon as I can reproduce it
178
179 done_testing;