tests: Extract focused_output in library

This commit is contained in:
Orestis Floros 2021-11-04 19:56:53 +01:00
parent bc4f35a6bb
commit 34ef7f8c33
No known key found for this signature in database
GPG Key ID: A09DBD7D3222C1C3
3 changed files with 29 additions and 25 deletions

View File

@ -42,6 +42,7 @@ our @EXPORT = qw(
exit_forcefully
workspace_exists
focused_ws
focused_output
get_socket_path
launch_with_config
get_i3_log
@ -664,6 +665,25 @@ sub workspace_exists {
(scalar grep { $_ eq $name } @{get_workspace_names()}) > 0;
}
=head2 focused_output
Returns the name of the currently focused output.
is(focused_output, 'fake-0', 'i3 starts on output 0');
=cut
sub _focused_output {
my $i3 = i3(get_socket_path());
my $tree = $i3->get_tree->recv;
my $focused = $tree->{focus}->[0];
my $output = first { $_->{id} == $focused } @{$tree->{nodes}};
return $output;
}
sub focused_output {
return _focused_output->{name}
}
=head2 focused_ws
Returns the name of the currently focused workspace.
@ -672,11 +692,9 @@ Returns the name of the currently focused workspace.
is($ws, '1', 'i3 starts on workspace 1');
=cut
sub focused_ws {
my $i3 = i3(get_socket_path());
my $tree = $i3->get_tree->recv;
my $focused = $tree->{focus}->[0];
my $output = first { $_->{id} == $focused } @{$tree->{nodes}};
my $output = _focused_output;
my $content = first { $_->{type} eq 'con' } @{$output->{nodes}};
my $first = first { $_->{fullscreen_mode} == 1 } @{$content->{nodes}};
return $first->{name}

View File

@ -31,13 +31,6 @@ my $i3 = i3(get_socket_path());
# use 'focus output' and verify that focus gets changed appropriately
################################################################################
sub focused_output {
my $tree = $i3->get_tree->recv;
my $focused = $tree->{focus}->[0];
my $output = first { $_->{id} == $focused } @{$tree->{nodes}};
return $output->{name};
}
sync_with_i3;
$x->root->warp_pointer(0, 0);
sync_with_i3;

View File

@ -149,32 +149,25 @@ sync_with_i3;
# Ensure that focusing right/left works in the expected order.
############################################################################
sub get_focused_output {
my $tree = i3(get_socket_path())->get_tree->recv;
my ($focused_id) = @{$tree->{focus}};
my ($output) = grep { $_->{id} == $focused_id } @{$tree->{nodes}};
return $output->{name};
}
is(get_focused_output(), 'fake-0', 'focus on fake-0');
is(focused_output, 'fake-0', 'focus on fake-0');
cmd 'focus output right';
is(get_focused_output(), 'fake-1', 'focus on fake-1');
is(focused_output, 'fake-1', 'focus on fake-1');
cmd 'focus output right';
is(get_focused_output(), 'fake-2', 'focus on fake-2');
is(focused_output, 'fake-2', 'focus on fake-2');
cmd 'focus output left';
is(get_focused_output(), 'fake-1', 'focus on fake-1');
is(focused_output, 'fake-1', 'focus on fake-1');
cmd 'focus output left';
is(get_focused_output(), 'fake-0', 'focus on fake-0');
is(focused_output, 'fake-0', 'focus on fake-0');
cmd 'focus output left';
is(get_focused_output(), 'fake-2', 'focus on fake-2 (wrapping)');
is(focused_output, 'fake-2', 'focus on fake-2 (wrapping)');
cmd 'focus output right';
is(get_focused_output(), 'fake-0', 'focus on fake-0 (wrapping)');
is(focused_output, 'fake-0', 'focus on fake-0 (wrapping)');
exit_gracefully($pid);