2 # vim:ts=4:sw=4:expandtab
4 use File::Temp qw(tmpnam tempfile tempdir);
10 use List::Util qw(first);
11 use List::MoreUtils qw(lastval);
12 use Time::HiRes qw(sleep);
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);
22 my $tester = Test::Builder->new();
23 my $_cached_socket_path = undef;
24 my $tmp_socket_path = undef;
29 return $window_count++;
37 use Test::Most" . (@_ > 0 ? " qw(@_)" : "") . ";
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);
47 goto \&Exporter::import;
50 sub open_standard_window {
51 my ($x, $color, $floating) = @_;
55 # We cannot use a hashref here because create_child expands the arguments into an array
57 class => WINDOW_CLASS_INPUT_OUTPUT,
58 rect => X11::XCB::Rect->new(x => 0, y => 0, width => 30, height => 30 ),
59 background_color => $color,
62 if (defined($floating) && $floating) {
63 @args = (@args, window_type => $x->atom(name => '_NET_WM_WINDOW_TYPE_UTILITY'));
66 my $window = $x->root->create_child(@args);
68 $window->name('Window ' . counter_window());
79 my $reply = $i3->command('open')->recv;
83 sub get_workspace_names {
84 my $i3 = i3(get_socket_path());
85 my $tree = $i3->get_tree->recv;
86 my @outputs = @{$tree->{nodes}};
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}});
93 [ map { $_->{name} } @cons ]
96 sub get_unused_workspace {
97 my @names = get_workspace_names();
99 do { $tmp = tmpnam() } while ($tmp ~~ @names);
103 sub fresh_workspace {
104 my $unused = get_unused_workspace;
105 cmd("workspace $unused");
111 my $i3 = i3(get_socket_path());
112 my $tree = $i3->get_tree->recv;
114 my @outputs = @{$tree->{nodes}};
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}});
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;
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
134 my $con = get_ws($name);
135 return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
140 my $con = get_ws($ws);
142 my @focused = @{$con->{focus}};
144 while (@focused > 0) {
146 last unless defined($con->{focus});
147 @focused = @{$con->{focus}};
148 @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
155 sub get_dock_clients {
158 my $tree = i3(get_socket_path())->get_tree->recv;
159 my @outputs = @{$tree->{nodes}};
160 # Children of all dockareas
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}});
179 i3(get_socket_path())->command(@_)->recv
182 sub workspace_exists {
184 ($name ~~ @{get_workspace_names()})
188 my $i3 = i3(get_socket_path());
189 my $tree = $i3->get_tree->recv;
190 my @outputs = @{$tree->{nodes}};
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}
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');
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();
215 say "Exiting i3 cleanly...";
216 i3($socketpath)->command('exit')->recv;
221 kill(9, $pid) or die "could not kill i3";
225 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
226 sub get_socket_path {
230 if ($cache && defined($_cached_socket_path)) {
231 return $_cached_socket_path;
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;
244 # launches a new i3 process with the given string as configuration file.
245 # useful for tests which test specific config file directives.
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
250 sub launch_with_config {
253 if (!defined($tmp_socket_path)) {
254 $tmp_socket_path = File::Temp::tempnam('/tmp', 'i3-test-socket-');
257 my ($fh, $tmpfile) = tempfile('i3-test-config-XXXXX', UNLINK => 1);
259 say $fh "ipc-socket $tmp_socket_path";
262 # Use $ENV{LOGPATH}, gets set in complete-run.pl. We append instead of
263 # overwrite because there might be multiple instances of i3 running during
265 my $i3cmd = "exec " . abs_path("../i3") . " -V -d all --disable-signalhandler -c $tmpfile >>$ENV{LOGPATH} 2>&1";
266 my $process = Proc::Background->new($i3cmd);
269 # force update of the cached socket path in lib/i3test