]> git.sur5r.net Git - i3/i3/blob - testcases/t/lib/i3test.pm
tests: extend t/35-floating-focus to use focus left/right on floating windows
[i3/i3] / testcases / t / lib / i3test.pm
1 package i3test;
2 # vim:ts=4:sw=4:expandtab
3
4 use File::Temp qw(tmpnam tempfile tempdir);
5 use Test::Builder;
6 use X11::XCB::Rect;
7 use X11::XCB::Window;
8 use X11::XCB qw(:all);
9 use AnyEvent::I3;
10 use List::Util qw(first);
11 use List::MoreUtils qw(lastval);
12 use Time::HiRes qw(sleep);
13 use Try::Tiny;
14 use Cwd qw(abs_path);
15 use Proc::Background;
16
17 use v5.10;
18
19 use Exporter ();
20 our @EXPORT = qw(get_workspace_names get_unused_workspace fresh_workspace get_ws_content get_ws get_focused open_empty_con open_standard_window get_dock_clients cmd does_i3_live exit_gracefully workspace_exists focused_ws get_socket_path launch_with_config);
21
22 my $tester = Test::Builder->new();
23 my $_cached_socket_path = undef;
24 my $tmp_socket_path = undef;
25
26 BEGIN {
27     my $window_count = 0;
28     sub counter_window {
29         return $window_count++;
30     }
31 }
32
33 sub import {
34     my $class = shift;
35     my $pkg = caller;
36     eval "package $pkg;
37 use Test::Most" . (@_ > 0 ? " qw(@_)" : "") . ";
38 use Data::Dumper;
39 use AnyEvent::I3;
40 use Time::HiRes qw(sleep);
41 use Test::Deep qw(eq_deeply cmp_deeply cmp_set cmp_bag cmp_methods useclass noclass set bag subbagof superbagof subsetof supersetof superhashof subhashof bool str arraylength Isa ignore methods regexprefonly regexpmatches num regexponly scalref reftype hashkeysonly blessed array re hash regexpref hash_each shallow array_each code arrayelementsonly arraylengthonly scalarrefonly listmethods any hashkeys isa);
42 use v5.10;
43 use strict;
44 use warnings;
45 ";
46     @_ = ($class);
47     goto \&Exporter::import;
48 }
49
50 sub open_standard_window {
51     my ($x, $color, $floating) = @_;
52
53     $color ||= '#c0c0c0';
54
55     # We cannot use a hashref here because create_child expands the arguments into an array
56     my @args = (
57         class => WINDOW_CLASS_INPUT_OUTPUT,
58         rect => X11::XCB::Rect->new(x => 0, y => 0, width => 30, height => 30 ),
59         background_color => $color,
60     );
61
62     if (defined($floating) && $floating) {
63         @args = (@args, window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY'));
64     }
65
66     my $window = $x->root->create_child(@args);
67
68     $window->name('Window ' . counter_window());
69     $window->map;
70
71     sleep(0.25);
72
73     return $window;
74 }
75
76 sub open_empty_con {
77     my ($i3) = @_;
78
79     my $reply = $i3->command('open')->recv;
80     return $reply->{id};
81 }
82
83 sub get_workspace_names {
84     my $i3 = i3(get_socket_path());
85     my $tree = $i3->get_tree->recv;
86     my @outputs = @{$tree->{nodes}};
87     my @cons;
88     for my $output (@outputs) {
89         # get the first CT_CON of each output
90         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
91         @cons = (@cons, @{$content->{nodes}});
92     }
93     [ map { $_->{name} } @cons ]
94 }
95
96 sub get_unused_workspace {
97     my @names = get_workspace_names();
98     my $tmp;
99     do { $tmp = tmpnam() } while ($tmp ~~ @names);
100     $tmp
101 }
102
103 sub fresh_workspace {
104     my $unused = get_unused_workspace;
105     cmd("workspace $unused");
106     $unused
107 }
108
109 sub get_ws {
110     my ($name) = @_;
111     my $i3 = i3(get_socket_path());
112     my $tree = $i3->get_tree->recv;
113
114     my @outputs = @{$tree->{nodes}};
115     my @workspaces;
116     for my $output (@outputs) {
117         # get the first CT_CON of each output
118         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
119         @workspaces = (@workspaces, @{$content->{nodes}});
120     }
121
122     # as there can only be one workspace with this name, we can safely
123     # return the first entry
124     return first { $_->{name} eq $name } @workspaces;
125 }
126
127 #
128 # returns the content (== tree, starting from the node of a workspace)
129 # of a workspace. If called in array context, also includes the focus
130 # stack of the workspace
131 #
132 sub get_ws_content {
133     my ($name) = @_;
134     my $con = get_ws($name);
135     return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
136 }
137
138 sub get_focused {
139     my ($ws) = @_;
140     my $con = get_ws($ws);
141
142     my @focused = @{$con->{focus}};
143     my $lf;
144     while (@focused > 0) {
145         $lf = $focused[0];
146         last unless defined($con->{focus});
147         @focused = @{$con->{focus}};
148         @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
149         $con = $cons[0];
150     }
151
152     return $lf;
153 }
154
155 sub get_dock_clients {
156     my $which = shift;
157
158     my $tree = i3(get_socket_path())->get_tree->recv;
159     my @outputs = @{$tree->{nodes}};
160     # Children of all dockareas
161     my @docked;
162     for my $output (@outputs) {
163         if (!defined($which)) {
164             @docked = (@docked, map { @{$_->{nodes}} }
165                                 grep { $_->{type} == 5 }
166                                 @{$output->{nodes}});
167         } elsif ($which eq 'top') {
168             my $first = first { $_->{type} == 5 } @{$output->{nodes}};
169             @docked = (@docked, @{$first->{nodes}});
170         } elsif ($which eq 'bottom') {
171             my $last = lastval { $_->{type} == 5 } @{$output->{nodes}};
172             @docked = (@docked, @{$last->{nodes}});
173         }
174     }
175     return @docked;
176 }
177
178 sub cmd {
179     i3(get_socket_path())->command(@_)->recv
180 }
181
182 sub workspace_exists {
183     my ($name) = @_;
184     ($name ~~ @{get_workspace_names()})
185 }
186
187 sub focused_ws {
188     my $i3 = i3(get_socket_path());
189     my $tree = $i3->get_tree->recv;
190     my @outputs = @{$tree->{nodes}};
191     my @cons;
192     for my $output (@outputs) {
193         # get the first CT_CON of each output
194         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
195         my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
196         return $first->{name}
197     }
198 }
199
200 sub does_i3_live {
201     my $tree = i3(get_socket_path())->get_tree->recv;
202     my @nodes = @{$tree->{nodes}};
203     my $ok = (@nodes > 0);
204     $tester->ok($ok, 'i3 still lives');
205     return $ok;
206 }
207
208 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
209 sub exit_gracefully {
210     my ($pid, $socketpath) = @_;
211     $socketpath ||= get_socket_path();
212
213     my $exited = 0;
214     try {
215         say "Exiting i3 cleanly...";
216         i3($socketpath)->command('exit')->recv;
217         $exited = 1;
218     };
219
220     if (!$exited) {
221         kill(9, $pid) or die "could not kill i3";
222     }
223 }
224
225 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
226 sub get_socket_path {
227     my ($cache) = @_;
228     $cache ||= 1;
229
230     if ($cache && defined($_cached_socket_path)) {
231         return $_cached_socket_path;
232     }
233
234     my $x = X11::XCB::Connection->new;
235     my $atom = $x->atom(name => 'I3_SOCKET_PATH');
236     my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
237     my $reply = $x->get_property_reply($cookie->{sequence});
238     my $socketpath = $reply->{value};
239     $_cached_socket_path = $socketpath;
240     return $socketpath;
241 }
242
243 #
244 # launches a new i3 process with the given string as configuration file.
245 # useful for tests which test specific config file directives.
246 #
247 # be sure to use !NO_I3_INSTANCE! somewhere in the file to signal
248 # complete-run.pl that it should not create an instance of i3
249 #
250 sub launch_with_config {
251     my ($config) = @_;
252
253     if (!defined($tmp_socket_path)) {
254         $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
255     }
256
257     my ($fh, $tmpfile) = tempfile('i3-test-config-XXXXX', UNLINK => 1);
258     say $fh $config;
259     say $fh "ipc-socket $tmp_socket_path";
260     close($fh);
261
262     my $i3cmd = "exec " . abs_path("../i3") . " -V -d all --disable-signalhandler -c $tmpfile >/dev/null 2>/dev/null";
263     my $process = Proc::Background->new($i3cmd);
264     sleep 1;
265
266     # force update of the cached socket path in lib/i3test
267     get_socket_path(0);
268
269     return $process;
270 }
271
272 1