1#!/usr/bin/perl -w
2# vim: ts=2 sw=2 expandtab
3
4# Exercises Wheel::Curses
5
6use strict;
7use lib qw(./mylib ../mylib);
8
9sub POE::Kernel::ASSERT_DEFAULT () { 1 }
10sub POE::Kernel::TRACE_DEFAULT  () { 0 }
11
12use Test::More;
13use Symbol qw(gensym);
14
15BEGIN {
16  if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
17    plan skip_all => "Can't multiplex consoles in $^O";
18  }
19
20  eval "use IO::Pty";
21  plan skip_all => 'IO::Pty not available' if $@;
22
23  eval { require Curses };
24  plan skip_all => 'Curses not available' if $@;
25}
26
27my ($saved_stdin, $saved_stdout, $pty_master, $pty_slave);
28BEGIN {
29  # Redirect STDIN and STDOUT to temporary handles for the duration of
30  # this test.
31
32  $saved_stdin = gensym();
33  open($saved_stdin, "<&STDIN") or die "can't save stdin: $!";
34  $saved_stdout = gensym();
35  open($saved_stdout, ">&STDOUT") or die "can't save stdout: $!";
36
37  # Create a couple one-way pipes for our new stdin and stdout.
38
39  $pty_master = IO::Pty->new() or die "pty: $!";
40  select $pty_master; $| = 1;
41
42  $pty_slave = $pty_master->slave();
43  select $pty_slave; $| = 1;
44
45  # Redirect our STDIN and STDOUT to the pipes.
46
47  open(STDIN, "<&=" . fileno($pty_slave)) or die "stdin pipe redir: $!";
48  open(STDOUT, ">&=" . fileno($pty_slave)) or die "stdout pipe redir: $!";
49  select STDOUT; $| = 1;
50}
51
52BEGIN {
53  plan skip_all => "Need help with Curses functions blocking under ptys";
54  plan tests => 5;
55  use_ok('POE');
56  use_ok('POE::Wheel::Curses');
57  use_ok('POE::Filter::Stream');
58  use_ok('POE::Wheel::ReadWrite');
59}
60
61# Restore the original stdio at the end of the run.
62
63END {
64  if ($saved_stdin) {
65    open(STDIN, "<&=" . fileno($saved_stdin)) or die "stdin restore: $!";
66    $saved_stdin = undef;
67  }
68
69  if ($saved_stdout) {
70    open(STDOUT, ">&=" . fileno($saved_stdout)) or die "stdout restore: $!";
71    $saved_stdout = undef;
72  }
73}
74
75### Session to drive the tests.
76
77POE::Session->create(
78  inline_states => {
79    _start                => \&test_start,
80    got_keystroke         => \&test_keystroke,
81    got_readwrite_input   => sub { },
82    _stop                 => sub { },
83  },
84);
85
86### main loop
87
88POE::Kernel->run();
89
90### Event handlers from here on.
91
92sub test_start {
93  my ($kernel, $heap) = @_[KERNEL, HEAP];
94
95  $heap->{child_input} = "";
96
97  $heap->{curses} = POE::Wheel::Curses->new(
98    InputEvent => "got_keystroke"
99  );
100
101  $heap->{readwrite} = POE::Wheel::ReadWrite->new(
102    Handle => $pty_master,
103    Filter => POE::Filter::Stream->new(),
104    InputEvent => "got_readwrite_input",
105  );
106
107  $heap->{readwrite}->put("this is a test!");
108}
109
110sub test_keystroke {
111  my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0];
112
113  $heap->{child_input} .= $input;
114  if ($heap->{child_input} =~ /!/) {
115    delete $heap->{curses};
116    delete $heap->{readwrite};
117    ok( $heap->{child_input} eq "this is a test!", "got keystrokes" );
118  }
119}
120
1211;
122