File: 27window-input.t

package info (click to toggle)
libtickit-perl 0.73-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 660 kB
  • sloc: perl: 4,944; makefile: 5
file content (200 lines) | stat: -rw-r--r-- 5,186 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl

use v5.14;
use warnings;

use Test::More;

use Tickit::Test;

my $rootwin = mk_window;

my $win = $rootwin->make_sub( 3, 10, 4, 20 );

$win->focus( 0, 0 );
flush_tickit;

my $keyev;
my @key_events;
my $bind_id_key = $win->bind_event( key => sub {
   ( my $_win, undef, $keyev ) = @_;
   is( $_win, $win, '$win for key event' );
   push @key_events, [ $keyev->type => $keyev->str ];
   return 1;
} );

# Simple key events
{
   presskey( text => "A" );

   is_deeply( \@key_events, [ [ text => "A" ] ], 'on_key A' );
   cmp_ok( $key_events[0][0], '==', Tickit::KEYEV_TEXT, 'event type numerically ==' );
   cmp_ok( $key_events[0][0], 'eq', "text",             'event type stringily eq' );

   ok( !$keyev->mod_is_shift, 'A key is not shift' );
   ok( !$keyev->mod_is_ctrl,  'A key is not ctrl' );
   ok( !$keyev->mod_is_alt,   'A key is not alt' );

   undef @key_events;

   presskey( key => "C-a", 4 );

   is_deeply( \@key_events, [ [ key => "C-a" ] ], 'on_key C-a' );

   ok( !$keyev->mod_is_shift, 'C-a key is not shift' );
   ok(  $keyev->mod_is_ctrl,  'C-a key is ctrl' );
   ok( !$keyev->mod_is_alt,   'C-a key is not alt' );
}

my @mouse_events;
my $bind_id_mouse = $win->bind_event( mouse => sub {
   my ( $win, undef, $info ) = @_;
   push @mouse_events, [ $info->type => $info->button, $info->line, $info->col ];
   return 1;
} );

# Simple mouse events
{
   undef @mouse_events;
   pressmouse( press => 1, 5, 15 );

   is_deeply( \@mouse_events, [ [ press => 1, 2, 5 ] ], 'on_mouse abs@15,5' );

   undef @mouse_events;
   pressmouse( press => 1, 1, 2 );

   is_deeply( \@mouse_events, [], 'no event for mouse abs@2,1' );
}

# Event passing to subwindows
{
   my $subwin = $win->make_sub( 2, 2, 1, 10 );

   $subwin->focus( 0, 0 );
   flush_tickit;

   my @subkey_events;
   my @submouse_events;
   my $subret = 1;
   $subwin->bind_event( key => sub {
      my ( $win, undef, $ev ) = @_;
      push @subkey_events, [ $ev->type => $ev->str ];
      return $subret;
   } );
   $subwin->bind_event( mouse => sub {
      my ( $win, undef, $ev ) = @_;
      push @submouse_events, [ $ev->type => $ev->button, $ev->line, $ev->col ];
      return $subret;
   } );

   undef @key_events;

   presskey( text => "B" );

   is_deeply( \@subkey_events, [ [ text => "B" ] ], 'on_key B on subwin' );
   is_deeply( \@key_events,    [ ],                 'on_key B on win' );

   undef @mouse_events;

   pressmouse( press => 1, 5, 15 );

   is_deeply( \@submouse_events, [ [ press => 1, 0, 3 ] ], 'on_mouse abs@15,5 on subwin' );
   is_deeply( \@mouse_events,    [ ],                      'on_mouse abs@15,5 on win' );

   $subret = 0;

   undef @key_events;
   undef @subkey_events;

   presskey( text => "C" );

   is_deeply( \@subkey_events, [ [ text => "C" ] ], 'on_key C on subwin' );
   is_deeply( \@key_events,    [ [ text => "C" ] ], 'on_key C on win' );

   undef @mouse_events;
   undef @submouse_events;

   pressmouse( press => 1, 5, 15 );

   is_deeply( \@submouse_events, [ [ press => 1, 0, 3 ] ], 'on_mouse abs@15,5 on subwin' );
   is_deeply( \@mouse_events,    [ [ press => 1, 2, 5 ] ], 'on_mouse abs@15,5 on win' );

   my $otherwin = $rootwin->make_sub( 10, 10, 4, 20 );
   flush_tickit;

   my @handlers;
   $win->unbind_event_id( $bind_id_key );
   $bind_id_key = $win->bind_event( key => sub { push @handlers, "win";      return 0 } );
   $subwin->bind_event            ( key => sub { push @handlers, "subwin";   return 0 } );
   $otherwin->bind_event          ( key => sub { push @handlers, "otherwin"; return 0 } );

   presskey( text => "D" );

   is_deeply( \@handlers, [qw( subwin win otherwin )], 'on_key D propagates to otherwin after win' );

   $subwin->hide;

   undef @handlers;

   presskey( text => "E" );

   is_deeply( \@handlers, [qw( win otherwin )], 'hidden windows do not receive input events' );

   $subwin->close;
   flush_tickit;
}

# Windows created in input event handlers don't receive events
{
   my $childwin;
   my $childmouse;
   $win->unbind_event_id( $bind_id_mouse );
   $bind_id_mouse = $win->bind_event( mouse => sub {
      return if $childwin;

      $childwin = $win->make_sub( 0, 0, 2, 2 );
      $childwin->bind_event( mouse => sub { $childmouse++ } );
   } );

   pressmouse( press => 1, 3, 10 );

   ok( defined $childwin, '$childwin created' );
   ok( !$childmouse,      '$childwin has not yet received mouse event' );

   flush_tickit;

   pressmouse( press => 1, 3, 10 );

   is( $childmouse, 1, '$childwin has now received a mouse event after flush' );

   $childwin->close;
   flush_tickit;
}

{
   my $sibwin;
   my $sibmouse;
   $win->unbind_event_id( $bind_id_mouse );
   $bind_id_mouse = $win->bind_event( mouse => sub {
      return if $sibwin;

      $sibwin = $win->make_float( 0, 0, 2, 2 );
      $sibwin->bind_event( mouse => sub { $sibmouse++ });
   } );

   pressmouse( press => 1, 3, 10 );

   ok( defined $sibwin, '$sibwin created' );
   ok( !$sibmouse,      '$sibwin has not yet received mouse event' );

   flush_tickit;

   pressmouse( press => 1, 3, 10 );

   is( $sibmouse, 1, '$sibwin has now received a mouse event after flush' );

   $sibwin->close;
   flush_tickit;
}

done_testing;