]> git.sur5r.net Git - i3/i3/blob - testcases/t/15-ipc-workspaces.t
ipc: implement GET_WORKSPACES message type
[i3/i3] / testcases / t / 15-ipc-workspaces.t
1 #!perl
2 # vim:ts=4:sw=4:expandtab
3
4 use Test::More tests => 8;
5 use Test::Exception;
6 use Data::Dumper;
7 use JSON::XS;
8 use List::MoreUtils qw(all);
9 use FindBin;
10 use lib "$FindBin::Bin/lib";
11 use i3test;
12
13 BEGIN {
14     use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX');
15     use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection');
16 }
17
18 my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock');
19 isa_ok($sock, 'IO::Socket::UNIX');
20
21 ####################
22 # Request workspaces
23 ####################
24
25 # message type 1 is GET_WORKSPACES
26 my $message = "i3-ipc" . pack("LL", 0, 1);
27 $sock->write($message);
28
29 #######################################
30 # Test the reply format for correctness
31 #######################################
32
33 # The following lines duplicate functionality from recv_ipc_command
34 # to have it included in the test-suite.
35 my $buffer;
36 $sock->read($buffer, length($message));
37 is(substr($buffer, 0, length("i3-ipc")), "i3-ipc", "ipc message received");
38 my ($len, $type) = unpack("LL", substr($buffer, 6));
39 is($type, 1, "correct reply type");
40
41 # read the payload
42 $sock->read($buffer, $len);
43 my $workspaces;
44
45 #########################
46 # Actually test the reply
47 #########################
48
49 lives_ok { $workspaces = decode_json($buffer) } 'JSON could be decoded';
50
51 ok(@{$workspaces} > 0, "More than zero workspaces found");
52
53 my $name_exists = all { defined($_->{name}) } @{$workspaces};
54 ok($name_exists, "All workspaces have a name");
55
56 diag( "Testing i3, Perl $], $^X" );