2 # vim:ts=4:sw=4:expandtab
3 use strict; use warnings;
5 use File::Temp qw(tmpnam tempfile tempdir);
12 use List::Util qw(first);
13 use Time::HiRes qw(sleep);
45 my $tester = Test::Builder->new();
46 my $_cached_socket_path = undef;
47 my $_sync_window = undef;
48 my $tmp_socket_path = undef;
55 return $window_count++;
63 my $test_most_args = @_ ? "qw(@_)" : "";
67 use Test::Most $test_most_args;
70 use Time::HiRes qw(sleep);
71 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);
73 $tester->bail_out("$@") if $@;
74 feature->import(":5.10");
78 $x ||= i3test::X11->new;
80 goto \&Exporter::import;
84 # Waits for the next event and calls the given callback for every event to
85 # determine if this is the event we are waiting for.
87 # Can be used to wait until a window is mapped, until a ClientMessage is
90 # wait_for_event $x, 0.25, sub { $_[0]->{response_type} == MAP_NOTIFY };
93 my ($x, $timeout, $cb) = @_;
97 my $prep = EV::prepare sub {
101 my $check = EV::check sub {
102 while (defined(my $event = $x->poll_for_event)) {
110 my $watcher = EV::io $x->get_file_descriptor, EV::READ, sub {
111 # do nothing, we only need this watcher so that EV picks up the events
114 # Trigger timeout after $timeout seconds (can be fractional)
115 my $t = AE::timer $timeout, 0, sub { warn "timeout ($timeout secs)"; $cv->send(0) };
117 my $result = $cv->recv;
122 # thin wrapper around wait_for_event which waits for MAP_NOTIFY
123 # make sure to include 'structure_notify' in the window’s event_mask attribute
126 wait_for_event $x, 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
129 # Wrapper around wait_for_event which waits for UNMAP_NOTIFY. Also calls
130 # sync_with_i3 to make sure i3 also picked up and processed the UnmapNotify
134 wait_for_event $x, 2, sub { $_[0]->{response_type} == UNMAP_NOTIFY };
139 # Opens a new window (see X11::XCB::Window), maps it, waits until it got mapped
140 # and synchronizes with i3.
142 # set dont_map to a true value to avoid mapping
145 # class => WINDOW_CLASS_INPUT_OUTPUT
146 # rect => [ 0, 0, 30, 30 ]
147 # background_color => '#c0c0c0'
148 # event_mask => [ 'structure_notify' ]
149 # name => 'Window <n>'
153 my %args = ($args ? %$args : ());
155 my $dont_map = delete $args{dont_map};
157 $args{class} //= WINDOW_CLASS_INPUT_OUTPUT;
158 $args{rect} //= [ 0, 0, 30, 30 ];
159 $args{background_color} //= '#c0c0c0';
160 $args{event_mask} //= [ 'structure_notify' ];
161 $args{name} //= 'Window ' . counter_window();
163 my $window = $x->root->create_child(%args);
165 return $window if $dont_map;
169 # We sync with i3 here to make sure $x->input_focus is updated.
174 # Thin wrapper around open_window which sets window_type to
175 # _NET_WM_WINDOW_TYPE_UTILITY to make the window floating.
176 sub open_floating_window {
178 my %args = ($args ? %$args : ());
180 $args{window_type} = $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY');
182 return open_window($x, \%args);
188 my $reply = $i3->command('open')->recv;
192 sub get_workspace_names {
193 my $i3 = i3(get_socket_path());
194 my $tree = $i3->get_tree->recv;
195 my @outputs = @{$tree->{nodes}};
197 for my $output (@outputs) {
198 # get the first CT_CON of each output
199 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
200 @cons = (@cons, @{$content->{nodes}});
202 [ map { $_->{name} } @cons ]
205 sub get_unused_workspace {
206 my @names = get_workspace_names();
208 do { $tmp = tmpnam() } while ($tmp ~~ @names);
212 sub fresh_workspace {
213 my $unused = get_unused_workspace;
214 cmd("workspace $unused");
220 my $i3 = i3(get_socket_path());
221 my $tree = $i3->get_tree->recv;
223 my @outputs = @{$tree->{nodes}};
225 for my $output (@outputs) {
226 # get the first CT_CON of each output
227 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
228 @workspaces = (@workspaces, @{$content->{nodes}});
231 # as there can only be one workspace with this name, we can safely
232 # return the first entry
233 return first { $_->{name} eq $name } @workspaces;
237 # returns the content (== tree, starting from the node of a workspace)
238 # of a workspace. If called in array context, also includes the focus
239 # stack of the workspace
243 my $con = get_ws($name);
244 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
249 my $con = get_ws($ws);
251 my @focused = @{$con->{focus}};
253 while (@focused > 0) {
255 last unless defined($con->{focus});
256 @focused = @{$con->{focus}};
257 my @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
264 sub get_dock_clients {
267 my $tree = i3(get_socket_path())->get_tree->recv;
268 my @outputs = @{$tree->{nodes}};
269 # Children of all dockareas
271 for my $output (@outputs) {
272 if (!defined($which)) {
273 @docked = (@docked, map { @{$_->{nodes}} }
274 grep { $_->{type} == 5 }
275 @{$output->{nodes}});
276 } elsif ($which eq 'top') {
277 my $first = first { $_->{type} == 5 } @{$output->{nodes}};
278 @docked = (@docked, @{$first->{nodes}});
279 } elsif ($which eq 'bottom') {
280 my @matching = grep { $_->{type} == 5 } @{$output->{nodes}};
281 my $last = $matching[-1];
282 @docked = (@docked, @{$last->{nodes}});
289 i3(get_socket_path())->command(@_)->recv
292 sub workspace_exists {
294 ($name ~~ @{get_workspace_names()})
298 my $i3 = i3(get_socket_path());
299 my $tree = $i3->get_tree->recv;
300 my @outputs = @{$tree->{nodes}};
302 for my $output (@outputs) {
303 # get the first CT_CON of each output
304 my $content = first { $_->{type} == 2 } @{$output->{nodes}};
305 my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
306 return $first->{name}
311 # Sends an I3_SYNC ClientMessage with a random value to the root window.
312 # i3 will reply with the same value, but, due to the order of events it
313 # processes, only after all other events are done.
315 # This can be used to ensure the results of a cmd 'focus left' are pushed to
316 # X11 and that $x->input_focus returns the correct value afterwards.
318 # See also docs/testsuite for a long explanation
323 # Since we need a (mapped) window for receiving a ClientMessage, we create
324 # one on the first call of sync_with_i3. It will be re-used in all
326 if (!defined($_sync_window)) {
327 $_sync_window = $x->root->create_child(
328 class => WINDOW_CLASS_INPUT_OUTPUT,
329 rect => X11::XCB::Rect->new(x => -15, y => -15, width => 10, height => 10 ),
330 override_redirect => 1,
331 background_color => '#ff0000',
332 event_mask => [ 'structure_notify' ],
337 wait_for_event $x, 2, sub { $_[0]->{response_type} == MAP_NOTIFY };
340 my $root = $x->get_root_window();
341 # Generate a random number to identify this particular ClientMessage.
342 my $myrnd = int(rand(255)) + 1;
344 # Generate a ClientMessage, see xcb_client_message_t
345 my $msg = pack "CCSLLLLLLL",
346 CLIENT_MESSAGE, # response_type
349 $root, # destination window
350 $x->atom(name => 'I3_SYNC')->id,
352 $_sync_window->id, # data[0]: our own window id
353 $myrnd, # data[1]: a random value to identify the request
358 # Send it to the root window -- since i3 uses the SubstructureRedirect
359 # event mask, it will get the ClientMessage.
360 $x->send_event(0, $root, EVENT_MASK_SUBSTRUCTURE_REDIRECT, $msg);
362 # now wait until the reply is here
363 return wait_for_event $x, 2, sub {
366 return 0 unless $event->{response_type} == 161;
368 my ($win, $rnd) = unpack "LL", $event->{data};
369 return ($rnd == $myrnd);
374 my $tree = i3(get_socket_path())->get_tree->recv;
375 my @nodes = @{$tree->{nodes}};
376 my $ok = (@nodes > 0);
377 $tester->ok($ok, 'i3 still lives');
381 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
382 sub exit_gracefully {
383 my ($pid, $socketpath) = @_;
384 $socketpath ||= get_socket_path();
388 say "Exiting i3 cleanly...";
389 i3($socketpath)->command('exit')->recv;
394 kill(9, $pid) or die "could not kill i3";
397 if ($socketpath =~ m,^/tmp/i3-test-socket-,) {
402 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
403 sub get_socket_path {
407 if ($cache && defined($_cached_socket_path)) {
408 return $_cached_socket_path;
411 my $atom = $x->atom(name => 'I3_SOCKET_PATH');
412 my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
413 my $reply = $x->get_property_reply($cookie->{sequence});
414 my $socketpath = $reply->{value};
415 $_cached_socket_path = $socketpath;
420 # launches a new i3 process with the given string as configuration file.
421 # useful for tests which test specific config file directives.
423 # be sure to use !NO_I3_INSTANCE! somewhere in the file to signal
424 # complete-run.pl that it should not create an instance of i3
426 sub launch_with_config {
427 my ($config, $dont_add_socket_path) = @_;
429 $dont_add_socket_path //= 0;
431 if (!defined($tmp_socket_path)) {
432 $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
435 my ($fh, $tmpfile) = tempfile('/tmp/i3-test-config-XXXXX', UNLINK => 1);
437 say $fh "ipc-socket $tmp_socket_path" unless $dont_add_socket_path;
440 my $cv = AnyEvent->condvar;
441 my $pid = activate_i3(
442 unix_socket_path => "$tmp_socket_path-activation",
443 display => $ENV{DISPLAY},
444 configfile => $tmpfile,
445 outdir => $ENV{OUTDIR},
446 testname => $ENV{TESTNAME},
447 valgrind => $ENV{VALGRIND},
448 strace => $ENV{STRACE},
452 # blockingly wait until i3 is ready
455 # force update of the cached socket path in lib/i3test
462 use parent 'X11::XCB::Connection';
466 i3test::sync_with_i3($self);
468 return $self->SUPER::input_focus(@_);