| 1 |
#!/usr/bin/perl -w |
| 2 |
# -*- perl -*- |
| 3 |
|
| 4 |
# |
| 5 |
# $Id: $ |
| 6 |
# Author: Slaven Rezic |
| 7 |
# |
| 8 |
|
| 9 |
use strict; |
| 10 |
|
| 11 |
use Tk; |
| 12 |
|
| 13 |
BEGIN { |
| 14 |
if (!eval q{ |
| 15 |
use Test::More; |
| 16 |
1; |
| 17 |
}) { |
| 18 |
print "1..0 # skip: no Test::More module\n"; |
| 19 |
exit; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
plan tests => 5; |
| 24 |
|
| 25 |
use_ok 'Tk::ErrorDialog'; |
| 26 |
|
| 27 |
my $mw = tkinit; |
| 28 |
$mw->geometry("+10+10"); |
| 29 |
|
| 30 |
my $ed; |
| 31 |
|
| 32 |
my $errmsg = "Intentional error."; |
| 33 |
$mw->afterIdle(sub { |
| 34 |
$mw->after(100, \&first_error); |
| 35 |
die "$errmsg\n"; |
| 36 |
}); |
| 37 |
|
| 38 |
$mw->after(20*1000, sub { |
| 39 |
if (Tk::Exists($mw)) { |
| 40 |
$mw->destroy; |
| 41 |
fail "Timeout - destroyed main window"; |
| 42 |
} |
| 43 |
}); |
| 44 |
MainLoop; |
| 45 |
|
| 46 |
# fills $ed |
| 47 |
sub first_error { |
| 48 |
my $dialog = search_error_dialog($mw); |
| 49 |
isa_ok($dialog, "Tk::Dialog", "dialog"); |
| 50 |
$ed = $dialog; |
| 51 |
my $error_stacktrace_toplevel = search_error_stacktrace_toplevel($mw); |
| 52 |
isa_ok($error_stacktrace_toplevel, 'Tk::ErrorDialog', 'Found stacktrace window'); |
| 53 |
is($error_stacktrace_toplevel->state, 'withdrawn', 'Stacktrace not visible'); |
| 54 |
$error_stacktrace_toplevel->geometry('+0+0'); # for WMs with interactive placement |
| 55 |
$dialog->SelectButton('Stack trace'); |
| 56 |
second_error(); |
| 57 |
} |
| 58 |
|
| 59 |
sub second_error { |
| 60 |
$mw->afterIdle(sub { die "$errmsg\n" }); |
| 61 |
$mw->after(100, sub { |
| 62 |
my $dialog = search_error_dialog($mw); |
| 63 |
is($ed, $dialog, "ErrorDialog reused"); |
| 64 |
$dialog->Exit; |
| 65 |
$mw->after(100, sub { $mw->destroy }); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
sub search_error_dialog { |
| 70 |
my $w = shift; |
| 71 |
my $dialog; |
| 72 |
$w->Walk(sub { |
| 73 |
return if $dialog; |
| 74 |
for my $opt (qw(text message)) { |
| 75 |
my $val = eval { $_[0]->cget("-$opt") }; |
| 76 |
if (defined $val && $val =~ m{\Q$errmsg}) { |
| 77 |
$dialog = $_[0]->toplevel; |
| 78 |
} |
| 79 |
} |
| 80 |
}); |
| 81 |
$dialog; |
| 82 |
} |
| 83 |
|
| 84 |
sub search_error_stacktrace_toplevel { |
| 85 |
my $w = shift; |
| 86 |
my $toplevel; |
| 87 |
$w->Walk(sub { |
| 88 |
return if $toplevel; |
| 89 |
if ($_[0]->isa('Tk::ErrorDialog')) { |
| 90 |
$toplevel = $_[0]; |
| 91 |
} |
| 92 |
}); |
| 93 |
$toplevel; |
| 94 |
} |
| 95 |
|
| 96 |
__END__ |