]> git.sur5r.net Git - i3/i3/blob - testcases/t/lib/i3test.pm
t/lib/i3test.pm: refactor get_ws and get_ws_content using List::Util’s first
[i3/i3] / testcases / t / lib / i3test.pm
1 package i3test;
2 # vim:ts=4:sw=4:expandtab
3
4 use Test::Kit qw(
5     Test::Exception
6     Data::Dumper
7     AnyEvent::I3
8 ),
9     'Test::Deep' => {
10         exclude => [ qw(all) ],
11     };
12
13 use File::Temp qw(tmpnam);
14 use X11::XCB::Rect;
15 use X11::XCB::Window;
16 use X11::XCB qw(:all);
17 use AnyEvent::I3;
18 use List::Util qw(first);
19 # Test::Kit already uses Exporter
20 #use Exporter qw(import);
21 use base 'Exporter';
22
23 our @EXPORT = qw(get_workspace_names get_unused_workspace get_ws_content get_ws get_focused);
24
25 BEGIN {
26     my $window_count = 0;
27     sub counter_window {
28         return $window_count++;
29     }
30 }
31
32 sub open_standard_window {
33     my ($x) = @_;
34
35     my $window = $x->root->create_child(
36         class => WINDOW_CLASS_INPUT_OUTPUT,
37         rect => [ 0, 0, 30, 30 ],
38         background_color => '#C0C0C0',
39     );
40
41     $window->name('Window ' . counter_window());
42     $window->map;
43
44     sleep(0.25);
45
46     return $window;
47 }
48
49 sub get_workspace_names {
50     my $i3 = i3("/tmp/nestedcons");
51     # TODO: use correct command as soon as AnyEvent::i3 is updated
52     my $tree = $i3->get_workspaces->recv;
53     my @workspaces = map { @{$_->{nodes}} } @{$tree->{nodes}};
54     [ map { $_->{name} } @workspaces ]
55 }
56
57 sub get_unused_workspace {
58     my @names = get_workspace_names();
59     my $tmp;
60     do { $tmp = tmpnam() } while ($tmp ~~ @names);
61     $tmp
62 }
63
64 sub get_ws {
65     my ($name) = @_;
66     my $i3 = i3("/tmp/nestedcons");
67     my $tree = $i3->get_workspaces->recv;
68     my @ws = map { @{$_->{nodes}} } @{$tree->{nodes}};
69
70     # as there can only be one workspace with this name, we can safely
71     # return the first entry
72     return first { $_->{name} eq $name } @ws;
73 }
74
75 #
76 # returns the content (== tree, starting from the node of a workspace)
77 # of a workspace. If called in array context, also includes the focus
78 # stack of the workspace
79 #
80 sub get_ws_content {
81     my ($name) = @_;
82     my $con = get_ws($name);
83     return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes};
84 }
85
86 sub get_focused {
87     my ($ws) = @_;
88     my $i3 = i3("/tmp/nestedcons");
89     my $tree = $i3->get_workspaces->recv;
90
91     my @ws = map { @{$_->{nodes}} } @{$tree->{nodes}};
92     my @cons = grep { $_->{name} eq $ws } @ws;
93     my $con = $cons[0];
94
95     my @focused = @{$con->{focus}};
96     my $lf;
97     while (@focused > 0) {
98         $lf = $focused[0];
99         last unless defined($con->{focus});
100         @focused = @{$con->{focus}};
101         @cons = grep { $_->{id} == $lf } (@{$con->{nodes}}, @{$con->{'floating-nodes'}});
102         $con = $cons[0];
103     }
104
105     return $lf;
106 }
107
108 1