From 8fb641cca47eda94e9f4f9c2347210350ae5ff7a Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 27 Mar 2010 15:20:38 +0100 Subject: [PATCH] make testcases use AnyEvent::I3 --- testcases/t/05-ipc.t | 16 ++++--------- testcases/t/06-focus.t | 23 +++++++------------ testcases/t/07-move.t | 22 +++++++----------- testcases/t/08-focus-stack.t | 13 ++++------- testcases/t/09-stacking.t | 14 ++++-------- testcases/t/10-dock.t | 1 - testcases/t/11-goto.t | 16 +++++-------- testcases/t/12-floating-resize.t | 17 +++++--------- testcases/t/13-urgent.t | 23 +++++++------------ testcases/t/14-client-leader.t | 18 +++++---------- testcases/t/15-ipc-workspaces.t | 39 ++++---------------------------- testcases/t/lib/i3test.pm | 29 ------------------------ 12 files changed, 61 insertions(+), 170 deletions(-) diff --git a/testcases/t/05-ipc.t b/testcases/t/05-ipc.t index ac52911a..8f427938 100644 --- a/testcases/t/05-ipc.t +++ b/testcases/t/05-ipc.t @@ -1,7 +1,7 @@ #!perl # vim:ts=4:sw=4:expandtab -use Test::More tests => 4; +use Test::More tests => 3; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -9,6 +9,7 @@ use Time::HiRes qw(sleep); use FindBin; use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); @@ -17,19 +18,14 @@ BEGIN { my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); - -isa_ok($sock, 'IO::Socket::UNIX'); - +my $i3 = i3; ##################################################################### # Ensure IPC works by switching workspaces ##################################################################### # Switch to the first workspace to get a clean testing environment -$sock->write(i3test::format_ipc_command("1")); - -sleep(0.25); +$i3->command('1')->recv; # Create a window so we can get a focus different from NULL my $window = i3test::open_standard_window($x); @@ -41,9 +37,7 @@ my $focus = $x->input_focus; diag("old focus = $focus"); # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep(0.25); +$i3->command('9')->recv; my $new_focus = $x->input_focus; isnt($focus, $new_focus, "Focus changed"); diff --git a/testcases/t/06-focus.t b/testcases/t/06-focus.t index 5ca3e062..a95e0e40 100644 --- a/testcases/t/06-focus.t +++ b/testcases/t/06-focus.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 14; +use Test::More tests => 13; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -12,29 +12,25 @@ use Time::HiRes qw(sleep); use FindBin; use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep(0.25); +$i3->command('9')->recv; ##################################################################### # Create two windows and make sure focus switching works ##################################################################### # Change mode of the container to "default" for following tests -$sock->write(i3test::format_ipc_command("d")); -sleep(0.25); +$i3->command('d')->recv; my $top = i3test::open_standard_window($x); my $mid = i3test::open_standard_window($x); @@ -52,8 +48,7 @@ diag("bottom id = " . $bottom->id); sub focus_after { my $msg = shift; - $sock->write(i3test::format_ipc_command($msg)); - sleep(0.5); + $i3->command($msg)->recv; return $x->input_focus; } @@ -81,8 +76,7 @@ is($focus, $top->id, "Top window focused (wrapping to the bottom works)"); ############################################### # Switch to the 10. workspace -$sock->write(i3test::format_ipc_command("10")); -sleep 0.25; +$i3->command('10')->recv; $top = i3test::open_standard_window($x); $bottom = i3test::open_standard_window($x); @@ -102,8 +96,7 @@ is($focus, $top->id, "Top window focused"); # Same thing, but left/right instead of top/bottom # Switch to the 11. workspace -$sock->write(i3test::format_ipc_command("11")); -sleep 0.25; +$i3->command('11')->recv; my $left = i3test::open_standard_window($x); my $right = i3test::open_standard_window($x); diff --git a/testcases/t/07-move.t b/testcases/t/07-move.t index 10cb9830..efd3df15 100644 --- a/testcases/t/07-move.t +++ b/testcases/t/07-move.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 10; +use Test::More tests => 8; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -12,21 +12,18 @@ use Time::HiRes qw(sleep); use FindBin; use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep(0.25); +$i3->command('9')->recv; ##################################################################### # Create two windows and make sure focus switching works @@ -50,8 +47,7 @@ diag("bottom id = " . $bottom->id); sub focus_after { my $msg = shift; - $sock->write(i3test::format_ipc_command($msg)); - sleep(0.5); + $i3->command($msg)->recv; return $x->input_focus; } @@ -82,9 +78,7 @@ is($focus, $top->id, "Top window focused"); # Move window cross-workspace ##################################################################### -$sock->write(i3test::format_ipc_command("m12")); -$sock->write(i3test::format_ipc_command("t")); -$sock->write(i3test::format_ipc_command("m13")); -$sock->write(i3test::format_ipc_command("12")); -$sock->write(i3test::format_ipc_command("13")); +for my $cmd (qw(m12 t m13 12 13)) { + $i3->command($cmd)->recv; +} ok(1, "Still living"); diff --git a/testcases/t/08-focus-stack.t b/testcases/t/08-focus-stack.t index 370369d8..4ae92407 100644 --- a/testcases/t/08-focus-stack.t +++ b/testcases/t/08-focus-stack.t @@ -3,7 +3,7 @@ # Checks if the focus is correctly restored, when creating a floating client # over an unfocused tiling client and destroying the floating one again. -use Test::More tests => 6; +use Test::More tests => 4; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -11,28 +11,25 @@ use Time::HiRes qw(sleep); use FindBin; use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Window') or BAIL_OUT('Could not load X11::XCB::Window'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep(0.25); +$i3->command('9')->recv; my $tiled_left = i3test::open_standard_window($x); my $tiled_right = i3test::open_standard_window($x); sleep(0.25); -$sock->write(i3test::format_ipc_command("ml")); +$i3->command('ml')->recv; # Get input focus before creating the floating window my $focus = $x->input_focus; diff --git a/testcases/t/09-stacking.t b/testcases/t/09-stacking.t index 8f40047e..59d2e6f4 100644 --- a/testcases/t/09-stacking.t +++ b/testcases/t/09-stacking.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 24; +use Test::More tests => 22; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -12,21 +12,18 @@ use Time::HiRes qw(sleep); use FindBin; use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep(0.25); +$i3->command('9')->recv; ##################################################################### # Create two windows and make sure focus switching works @@ -50,8 +47,7 @@ diag("bottom id = " . $bottom->id); sub focus_after { my $msg = shift; - $sock->write(i3test::format_ipc_command($msg)); - sleep(0.25); + $i3->command($msg)->recv; return $x->input_focus; } diff --git a/testcases/t/10-dock.t b/testcases/t/10-dock.t index b1b7bfcb..52063131 100644 --- a/testcases/t/10-dock.t +++ b/testcases/t/10-dock.t @@ -12,7 +12,6 @@ use i3test; use List::Util qw(first); BEGIN { - #use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } diff --git a/testcases/t/11-goto.t b/testcases/t/11-goto.t index 47675903..9b06112b 100644 --- a/testcases/t/11-goto.t +++ b/testcases/t/11-goto.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 9; +use Test::More tests => 7; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -13,21 +13,18 @@ use FindBin; use Digest::SHA1 qw(sha1_base64); use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep(0.25); +$i3->command('9')->recv; ##################################################################### # Create two windows and make sure focus switching works @@ -51,8 +48,7 @@ diag("bottom id = " . $bottom->id); sub focus_after { my $msg = shift; - $sock->write(i3test::format_ipc_command($msg)); - sleep(0.5); + $i3->command($msg)->recv; return $x->input_focus; } @@ -74,7 +70,7 @@ my $random_mark = sha1_base64(rand()); $focus = focus_after("goto $random_mark"); is($focus, $mid->id, "focus unchanged"); -$sock->write(i3test::format_ipc_command("mark $random_mark")); +$i3->command("mark $random_mark")->recv; $focus = focus_after("k"); is($focus, $top->id, "Top window focused"); diff --git a/testcases/t/12-floating-resize.t b/testcases/t/12-floating-resize.t index d908d345..74f66535 100644 --- a/testcases/t/12-floating-resize.t +++ b/testcases/t/12-floating-resize.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 17; +use Test::More tests => 15; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -13,21 +13,18 @@ use FindBin; use Digest::SHA1 qw(sha1_base64); use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep 0.25; +$i3->command('9')->recv; ##################################################################### # Create a floating window and see if resizing works @@ -78,13 +75,11 @@ sub test_resize { test_resize; # Test borderless -$sock->write(i3test::format_ipc_command("bb")); -sleep 0.25; +$i3->command('bb')->recv; test_resize; # Test with 1-px-border -$sock->write(i3test::format_ipc_command("bp")); -sleep 0.25; +$i3->command('bp')->recv; test_resize; diff --git a/testcases/t/13-urgent.t b/testcases/t/13-urgent.t index 7dee21c6..5fce6aee 100644 --- a/testcases/t/13-urgent.t +++ b/testcases/t/13-urgent.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 9; +use Test::More tests => 7; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -13,21 +13,18 @@ use FindBin; use Digest::SHA1 qw(sha1_base64); use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep 0.25; +$i3->command('9')->recv; ##################################################################### # Create two windows and put them in stacking mode @@ -38,8 +35,7 @@ sleep 0.25; my $bottom = i3test::open_standard_window($x); sleep 0.25; -$sock->write(i3test::format_ipc_command("s")); -sleep 0.25; +$i3->command('s')->recv; ##################################################################### # Add the urgency hint, switch to a different workspace and back again @@ -47,12 +43,9 @@ sleep 0.25; $top->add_hint('urgency'); sleep 1; -$sock->write(i3test::format_ipc_command("1")); -sleep 0.25; -$sock->write(i3test::format_ipc_command("9")); -sleep 0.25; -$sock->write(i3test::format_ipc_command("1")); -sleep 0.25; +$i3->command('1')->recv; +$i3->command('9')->recv; +$i3->command('1')->recv; my $std = i3test::open_standard_window($x); sleep 0.25; diff --git a/testcases/t/14-client-leader.t b/testcases/t/14-client-leader.t index ead52764..b9160131 100644 --- a/testcases/t/14-client-leader.t +++ b/testcases/t/14-client-leader.t @@ -4,7 +4,7 @@ # the workspace to be empty). # TODO: skip it by default? -use Test::More tests => 5; +use Test::More tests => 3; use Test::Deep; use X11::XCB qw(:all); use Data::Dumper; @@ -13,21 +13,17 @@ use FindBin; use Digest::SHA1 qw(sha1_base64); use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); } my $x = X11::XCB::Connection->new; - -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; # Switch to the nineth workspace -$sock->write(i3test::format_ipc_command("9")); - -sleep 0.25; +$i3->command('9')->recv; ##################################################################### # Create a parent window @@ -48,8 +44,7 @@ sleep 0.25; # Switch workspace to 10 and open a child window. It should be positioned # on workspace 9. ######################################################################### -$sock->write(i3test::format_ipc_command("10")); -sleep 0.25; +$i3->command('10')->recv; my $child = $x->root->create_child( class => WINDOW_CLASS_INPUT_OUTPUT, @@ -66,7 +61,6 @@ sleep 0.25; isnt($x->input_focus, $child->id, "Child window focused"); # Switch back -$sock->write(i3test::format_ipc_command("9")); -sleep 0.25; +$i3->command('9')->recv; is($x->input_focus, $child->id, "Child window focused"); diff --git a/testcases/t/15-ipc-workspaces.t b/testcases/t/15-ipc-workspaces.t index 01947094..4e2c0e8d 100644 --- a/testcases/t/15-ipc-workspaces.t +++ b/testcases/t/15-ipc-workspaces.t @@ -1,52 +1,21 @@ #!perl # vim:ts=4:sw=4:expandtab -use Test::More tests => 8; +use Test::More tests => 3; use Test::Exception; -use Data::Dumper; -use JSON::XS; use List::MoreUtils qw(all); use FindBin; use lib "$FindBin::Bin/lib"; use i3test; +use AnyEvent::I3; -BEGIN { - use_ok('IO::Socket::UNIX') or BAIL_OUT('Cannot load IO::Socket::UNIX'); - use_ok('X11::XCB::Connection') or BAIL_OUT('Cannot load X11::XCB::Connection'); -} - -my $sock = IO::Socket::UNIX->new(Peer => '/tmp/i3-ipc.sock'); -isa_ok($sock, 'IO::Socket::UNIX'); +my $i3 = i3; #################### # Request workspaces #################### -# message type 1 is GET_WORKSPACES -my $message = "i3-ipc" . pack("LL", 0, 1); -$sock->write($message); - -####################################### -# Test the reply format for correctness -####################################### - -# The following lines duplicate functionality from recv_ipc_command -# to have it included in the test-suite. -my $buffer; -$sock->read($buffer, length($message)); -is(substr($buffer, 0, length("i3-ipc")), "i3-ipc", "ipc message received"); -my ($len, $type) = unpack("LL", substr($buffer, 6)); -is($type, 1, "correct reply type"); - -# read the payload -$sock->read($buffer, $len); -my $workspaces; - -######################### -# Actually test the reply -######################### - -lives_ok { $workspaces = decode_json($buffer) } 'JSON could be decoded'; +my $workspaces = $i3->get_workspaces->recv; ok(@{$workspaces} > 0, "More than zero workspaces found"); diff --git a/testcases/t/lib/i3test.pm b/testcases/t/lib/i3test.pm index 540551b0..eb4167a8 100644 --- a/testcases/t/lib/i3test.pm +++ b/testcases/t/lib/i3test.pm @@ -29,33 +29,4 @@ sub open_standard_window { return $window; } -sub format_ipc_command { - my $msg = shift; - my $len; - - { use bytes; $len = length($msg); } - - my $message = "i3-ipc" . pack("LL", $len, 0) . $msg; - - return $message; -} - -sub recv_ipc_command { - my ($sock, $expected) = @_; - - my $buffer; - # header is 14 bytes ("i3-ipc" + 32 bit + 32 bit) - $sock->read($buffer, 14); - return undef unless substr($buffer, 0, length("i3-ipc")) eq "i3-ipc"; - - my ($len, $type) = unpack("LL", substr($buffer, 6)); - - return undef unless $type == $expected; - - # read the payload - $sock->read($buffer, $len); - - decode_json($buffer) -} - 1 -- 2.39.5