File: 85tickit-widget.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 (94 lines) | stat: -rw-r--r-- 1,975 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
#!/usr/bin/perl

use v5.14;
use warnings;

BEGIN {
   # We have some unit tests of terminal control strings. Best to be running
   # on a known terminal
   $ENV{TERM} = "xterm";
}

use Test::More;
use Test::HexString;

use Errno qw( EAGAIN );

use Tickit;

pipe my( $my_rd, $term_wr ) or die "Cannot pipepair - $!";

sub stream_is
{
   my ( $expect, $name ) = @_;

   my $stream = "";
   while(1) {
      my $ret = sysread( $my_rd, $stream, 8192, length $stream );
      defined $ret or
         ( $! == EAGAIN and last ) or
         die "sysread() - $!";

      $ret or die "sysread() - EOF";

      last if length $stream >= length $expect or
              $stream ne substr( $expect, 0, length $stream );
   }

   is_hexstr( substr( $stream, 0, length $expect, "" ), $expect, $name );
}

my $tickit = Tickit->new(
   UTF8     => 1,
   term_out => $term_wr,
   root     => TestWidget->new,
);

#$tickit->setup_term;
$tickit->watch_later( sub { $tickit->stop } );
$tickit->run;

# There might be some terminal setup code here... Flush it
$my_rd->blocking( 0 );
sysread( $my_rd, my $buffer, 8192 );

#$tickit->rootwin->flush;
$tickit->watch_later( sub { $tickit->stop } );
$tickit->run;

# These strings are fragile but there's not much else we can do for an end-to-end
# test. If this unit test breaks likely these strings need updating. Sorry.
stream_is( "\e[13;38HHello", 'root widget rendered' );

done_testing;

package TestWidget;

sub new { bless {}, shift }

sub window { shift->{window} }

sub set_window
{
   my $self = shift;
   ( $self->{window} ) = @_;

   if( my $window = $self->{window} ) {
      $window->bind_event( expose => sub {
         my ( $win, undef, $info ) = @_;
         $self->render_to_rb( $info->rb, $info->rect );
      } );
      $window->expose;
   }
}

sub render_to_rb
{
   my $self = shift;
   my ( $rb, $rect ) = @_;
   my $win = $self->window or return;

   $rb->text_at( $win->lines / 2, ( $win->cols - 5 ) / 2,
      "Hello"
   );
}