]> git.sur5r.net Git - i3/i3/blob - testcases/t/lib/i3test.pm
Merge branch 'tree' into next
[i3/i3] / testcases / t / lib / i3test.pm
1 package i3test;
2 # vim:ts=4:sw=4:expandtab
3
4 use File::Temp qw(tmpnam);
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 v5.10;
15
16 use Exporter ();
17 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);
18
19 my $tester = Test::Builder->new();
20 my $_cached_socket_path = undef;
21
22 BEGIN {
23     my $window_count = 0;
24     sub counter_window {
25         return $window_count++;
26     }
27 }
28
29 sub import {
30     my $class = shift;
31     my $pkg = caller;
32     eval "package $pkg;
33 use Test::Most" . (@_ > 0 ? " qw(@_)" : "") . ";
34 use Data::Dumper;
35 use AnyEvent::I3;
36 use Time::HiRes qw(sleep);
37 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);
38 use v5.10;
39 use strict;
40 use warnings;
41 ";
42     @_ = ($class);
43     goto \&Exporter::import;
44 }
45
46 sub open_standard_window {
47     my ($x, $color) = @_;
48
49     $color ||= '#c0c0c0';
50
51     my $window = $x->root->create_child(
52         class => WINDOW_CLASS_INPUT_OUTPUT,
53         rect => [ 0, 0, 30, 30 ],
54         background_color => $color,
55     );
56
57     $window->name('Window ' . counter_window());
58     $window->map;
59
60     sleep(0.25);
61
62     return $window;
63 }
64
65 sub open_empty_con {
66     my ($i3) = @_;
67
68     my $reply = $i3->command('open')->recv;
69     return $reply->{id};
70 }
71
72 sub get_workspace_names {
73     my $i3 = i3(get_socket_path());
74     my $tree = $i3->get_tree->recv;
75     my @outputs = @{$tree->{nodes}};
76     my @cons;
77     for my $output (@outputs) {
78         # get the first CT_CON of each output
79         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
80         @cons = (@cons, @{$content->{nodes}});
81     }
82     [ map { $_->{name} } @cons ]
83 }
84
85 sub get_unused_workspace {
86     my @names = get_workspace_names();
87     my $tmp;
88     do { $tmp = tmpnam() } while ($tmp ~~ @names);
89     $tmp
90 }
91
92 sub fresh_workspace {
93     my $unused = get_unused_workspace;
94     cmd("workspace $unused");
95     $unused
96 }
97
98 sub get_ws {
99     my ($name) = @_;
100     my $i3 = i3(get_socket_path());
101     my $tree = $i3->get_tree->recv;
102
103     my @outputs = @{$tree->{nodes}};
104     my @workspaces;
105     for my $output (@outputs) {
106         # get the first CT_CON of each output
107         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
108         @workspaces = (@workspaces, @{$content->{nodes}});
109     }
110
111     # as there can only be one workspace with this name, we can safely
112     # return the first entry
113     return first { $_->{name} eq $name } @workspaces;
114 }
115
116 #
117 # returns the content (== tree, starting from the node of a workspace)
118 # of a workspace. If called in array context, also includes the focus
119 # stack of the workspace
120 #
121 sub get_ws_content {
122     my ($name) = @_;
123     my $con = get_ws($name);
124     return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
125 }
126
127 sub get_focused {
128     my ($ws) = @_;
129     my $con = get_ws($ws);
130
131     my @focused = @{$con->{focus}};
132     my $lf;
133     while (@focused > 0) {
134         $lf = $focused[0];
135         last unless defined($con->{focus});
136         @focused = @{$con->{focus}};
137         @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating_nodes'}});
138         $con = $cons[0];
139     }
140
141     return $lf;
142 }
143
144 sub get_dock_clients {
145     my $which = shift;
146
147     my $tree = i3(get_socket_path())->get_tree->recv;
148     my @outputs = @{$tree->{nodes}};
149     # Children of all dockareas
150     my @docked;
151     for my $output (@outputs) {
152         if (!defined($which)) {
153             @docked = (@docked, map { @{$_->{nodes}} }
154                                 grep { $_->{type} == 5 }
155                                 @{$output->{nodes}});
156         } elsif ($which eq 'top') {
157             my $first = first { $_->{type} == 5 } @{$output->{nodes}};
158             @docked = (@docked, @{$first->{nodes}});
159         } elsif ($which eq 'bottom') {
160             my $last = lastval { $_->{type} == 5 } @{$output->{nodes}};
161             @docked = (@docked, @{$last->{nodes}});
162         }
163     }
164     return @docked;
165 }
166
167 sub cmd {
168     i3(get_socket_path())->command(@_)->recv
169 }
170
171 sub workspace_exists {
172     my ($name) = @_;
173     ($name ~~ @{get_workspace_names()})
174 }
175
176 sub focused_ws {
177     my $i3 = i3(get_socket_path());
178     my $tree = $i3->get_tree->recv;
179     my @outputs = @{$tree->{nodes}};
180     my @cons;
181     for my $output (@outputs) {
182         # get the first CT_CON of each output
183         my $content = first { $_->{type} == 2 } @{$output->{nodes}};
184         my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
185         return $first->{name}
186     }
187 }
188
189 sub does_i3_live {
190     my $tree = i3(get_socket_path())->get_tree->recv;
191     my @nodes = @{$tree->{nodes}};
192     my $ok = (@nodes > 0);
193     $tester->ok($ok, 'i3 still lives');
194     return $ok;
195 }
196
197 # Tries to exit i3 gracefully (with the 'exit' cmd) or kills the PID if that fails
198 sub exit_gracefully {
199     my ($pid, $socketpath) = @_;
200     $socketpath ||= get_socket_path();
201
202     my $exited = 0;
203     try {
204         say "Exiting i3 cleanly...";
205         i3($socketpath)->command('exit')->recv;
206         $exited = 1;
207     };
208
209     if (!$exited) {
210         kill(9, $pid) or die "could not kill i3";
211     }
212 }
213
214 # Gets the socket path from the I3_SOCKET_PATH atom stored on the X11 root window
215 sub get_socket_path {
216     my ($cache) = @_;
217     $cache ||= 1;
218
219     if ($cache && defined($_cached_socket_path)) {
220         return $_cached_socket_path;
221     }
222
223     my $x = X11::XCB::Connection->new;
224     my $atom = $x->atom(name => 'I3_SOCKET_PATH');
225     my $cookie = $x->get_property(0, $x->get_root_window(), $atom->id, GET_PROPERTY_TYPE_ANY, 0, 256);
226     my $reply = $x->get_property_reply($cookie->{sequence});
227     my $socketpath = $reply->{value};
228     $_cached_socket_path = $socketpath;
229     return $socketpath;
230 }
231
232 1