# vim:ts=4:sw=4:expandtab
#
# Please read the following documents before working on tests:
-# • http://build.i3wm.org/docs/testsuite.html
+# • https://build.i3wm.org/docs/testsuite.html
# (or docs/testsuite)
#
-# • http://build.i3wm.org/docs/lib-i3test.html
+# • https://build.i3wm.org/docs/lib-i3test.html
# (alternatively: perldoc ./testcases/lib/i3test.pm)
#
-# • http://build.i3wm.org/docs/ipc.html
+# • https://build.i3wm.org/docs/ipc.html
# (or docs/ipc)
#
# • http://onyxneon.com/books/modern_perl/modern_perl_a4.pdf
workspace ws-right output fake-1
EOT
-my $i3 = i3(get_socket_path());
-$i3->connect()->recv;
-
-# subscribe to the 'focus' ipc event
-my $focus = AnyEvent->condvar;
-$i3->subscribe({
- workspace => sub {
- my ($event) = @_;
- if ($event->{change} eq 'focus') {
- $focus->send($event);
- }
- }
-})->recv;
-
-# give up after 0.5 seconds
-my $timer = AnyEvent->timer(
- after => 0.5,
- cb => sub {
- $focus->send(0);
- }
-);
-
# open two windows on the left output
cmd 'workspace ws-left';
open_window;
open_window;
-# move a window over to the right output
-cmd 'move right';
-my $event = $focus->recv;
+sub focus_subtest {
+ my ($cmd, $want) = @_;
-ok($event, 'moving from workspace with two windows triggered focus ipc event');
-is($event->{current}->{name}, 'ws-right', 'focus event gave the right workspace');
-is(@{$event->{current}->{nodes}}, 1, 'focus event gave the right number of windows on the workspace');
+ my @events = events_for(
+ sub { cmd $cmd },
+ 'workspace');
-# reset and try again
-$focus = AnyEvent->condvar;
-cmd 'workspace ws-left';
-$focus->recv;
+ my @focus = grep { $_->{change} eq 'focus' } @events;
+ is(scalar @focus, 1, 'Received 1 workspace::focus event');
+ is($focus[0]->{current}->{name}, 'ws-right', 'focus event gave the right workspace');
+ is(@{$focus[0]->{current}->{nodes}}, $want, 'focus event gave the right number of windows on the workspace');
+}
+
+# move a window over to the right output
+subtest 'move right (1)', \&focus_subtest, 'move right', 1;
-$focus = AnyEvent->condvar;
-cmd 'move right';
-$event = $focus->recv;
-ok($event, 'moving from workspace with one window triggered focus ipc event');
-is($event->{current}->{name}, 'ws-right', 'focus event gave the right workspace');
-is(@{$event->{current}->{nodes}}, 2, 'focus event gave the right number of windows on the workspace');
+# move another window
+cmd 'workspace ws-left';
+subtest 'move right (2)', \&focus_subtest, 'move right', 2;
done_testing;