aboutsummaryrefslogtreecommitdiff
path: root/testcases/t/219-ipc-window-focus.t
blob: 08ed7fc57ce148a7031aa170bfb3f2830cbe059f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!perl
# vim:ts=4:sw=4:expandtab
#
# Please read the following documents before working on tests:
# • https://build.i3wm.org/docs/testsuite.html
#   (or docs/testsuite)
#
# • https://build.i3wm.org/docs/lib-i3test.html
#   (alternatively: perldoc ./testcases/lib/i3test.pm)
#
# • https://build.i3wm.org/docs/ipc.html
#   (or docs/ipc)
#
# • https://i3wm.org/downloads/modern_perl_a4.pdf
#   (unless you are already familiar with Perl)

use i3test i3_config => <<EOT;
# i3 config file (v4)
font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1

# fake-1 under fake-0 to not interfere with left/right wrapping
fake-outputs 1024x768+0+0,1024x768+0+1024
workspace X output fake-1
EOT

################################
# Window focus event
################################

my $ws = fresh_workspace(output => 0);
my $win0 = open_window;
my $win1 = open_window;
my $win2 = open_window;

# ensure the rightmost window contains input focus
cmd '[id="' . $win2->id . '"] focus';
is($x->input_focus, $win2->id, "Window 2 focused");

sub focus_subtest {
    my ($cmd, $name) = @_;

    my $focus = AnyEvent->condvar;

    my @events = events_for(
	sub { cmd $cmd },
	'window');

    is(scalar @events, 1, 'Received 1 event');
    is($events[0]->{change}, 'focus', 'Focus event received');
    is($events[0]->{container}->{name}, $name, "$name focused");
}

sub kill_subtest {
    my ($cmd, $name) = @_;

    my $focus = AnyEvent->condvar;

    my @events = events_for(
	sub {
	    cmd $cmd;
	    # Sync to make sure x_window_kill() calls have taken effect.
	    sync_with_i3;
	},
	'window');

    is(scalar @events, 1, 'Received 1 event');
    is($events[0]->{change}, 'close', 'Close event received');
    is($events[0]->{container}->{name}, $name, "$name closed");
}

subtest 'focus left (1)', \&focus_subtest, 'focus left', $win1->name;
subtest 'focus left (2)', \&focus_subtest, 'focus left', $win0->name;
subtest 'focus right (1)', \&focus_subtest, 'focus right', $win1->name;
subtest 'focus right (2)', \&focus_subtest, 'focus right', $win2->name;
subtest 'focus right (3)', \&focus_subtest, 'focus right', $win0->name;
subtest 'focus left', \&focus_subtest, 'focus left', $win2->name;
subtest 'kill doesn\'t produce focus event', \&kill_subtest, '[id=' . $win1->id . '] kill', $win1->name;

# See issue #3562. We need to switch to an existing workspace on the second
# output to trigger the bug.
cmd 'workspace X';
subtest 'workspace focus', \&focus_subtest, "workspace $ws", $win2->name;

sub scratchpad_subtest {
    my ($cmd, $name) = @_;

    my $focus = AnyEvent->condvar;

    my @events = events_for(
	sub { cmd $cmd },
	'window');

    is(scalar @events, 2, 'Received 2 events');
    is($events[0]->{change}, 'move', 'Move event received');
    is($events[0]->{container}->{nodes}->[0]->{name}, $name, "$name moved");
    is($events[1]->{change}, 'focus', 'Focus event received');
    is($events[1]->{container}->{name}, $name, "$name focused");
}

fresh_workspace;
my $win = open_window;
cmd 'move scratchpad';
subtest 'scratchpad', \&scratchpad_subtest, '[id=' . $win->id . '] scratchpad show', $win->name;

done_testing;