| 1 |
# -*- perl -*- |
| 2 |
BEGIN { $|=1; $^W=1; } |
| 3 |
use strict; |
| 4 |
use FindBin; |
| 5 |
use lib $FindBin::RealBin; |
| 6 |
|
| 7 |
BEGIN { |
| 8 |
if (!eval q{ |
| 9 |
use Test::More; |
| 10 |
1; |
| 11 |
}) { |
| 12 |
print "1..0 # skip: no Test::More module\n"; |
| 13 |
exit; |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
use Getopt::Long; |
| 18 |
|
| 19 |
use TkTest qw(catch_grabs); |
| 20 |
|
| 21 |
plan tests => 15; |
| 22 |
|
| 23 |
if (!defined $ENV{BATCH}) { $ENV{BATCH} = 1 } |
| 24 |
|
| 25 |
use_ok("Tk"); |
| 26 |
use_ok("Tk::FBox"); |
| 27 |
|
| 28 |
my $top = new MainWindow; |
| 29 |
eval { $top->geometry('+10+10'); }; # This works for mwm and interactivePlacement |
| 30 |
|
| 31 |
my $f; |
| 32 |
|
| 33 |
my $delay = 250; |
| 34 |
|
| 35 |
GetOptions("delay=i" => \$delay) |
| 36 |
or die "usage: $0 [-delay ...ms]"; |
| 37 |
|
| 38 |
###################################################################### |
| 39 |
# open |
| 40 |
eval { |
| 41 |
$f = $top->FBox(-defaultextension => ".PL", |
| 42 |
-filetypes => [ |
| 43 |
['Text Files', ['.txt', '.text']], |
| 44 |
['TCL Scripts', '.tcl' ], |
| 45 |
['C Source Files', '.c', 'TEXT'], |
| 46 |
['GIF Files', '.gif', ], |
| 47 |
['GIF Files', '', 'GIFF'], |
| 48 |
['All Files', '*', ], |
| 49 |
], |
| 50 |
-initialdir => ".", |
| 51 |
-initialfile => "Makefile.PL", |
| 52 |
-title => "Load file (with filetypes)", |
| 53 |
-type => "open", |
| 54 |
-filter => "*.PL", # ignored |
| 55 |
-font => "Helvetica 14", |
| 56 |
); |
| 57 |
}; |
| 58 |
is($@, "", "creating Tk::FBox widget"); |
| 59 |
|
| 60 |
catch_grabs { |
| 61 |
destroy_if_visible($f) if $ENV{BATCH}; |
| 62 |
my $result = $f->Show; |
| 63 |
if (!$ENV{BATCH}) { |
| 64 |
diag "Result is <$result>"; |
| 65 |
} |
| 66 |
pass("After showing FBox") |
| 67 |
} 1; |
| 68 |
|
| 69 |
###################################################################### |
| 70 |
# open without filetypes |
| 71 |
eval { |
| 72 |
$f = $top->FBox(-defaultextension => ".PL", |
| 73 |
-initialdir => ".", |
| 74 |
-initialfile => "Makefile.PL", |
| 75 |
-title => "Load file (without filetypes, with filter)", |
| 76 |
-type => "open", |
| 77 |
-filter => "*.PL", |
| 78 |
-font => "Helvetica 14", |
| 79 |
); |
| 80 |
}; |
| 81 |
is($@, "", "creating Tk::FBox widget"); |
| 82 |
|
| 83 |
catch_grabs { |
| 84 |
destroy_if_visible($f) if $ENV{BATCH}; |
| 85 |
my $result = $f->Show; |
| 86 |
if (!$ENV{BATCH}) { |
| 87 |
diag "Result is <$result>"; |
| 88 |
} |
| 89 |
pass("After showing FBox") |
| 90 |
} 1; |
| 91 |
|
| 92 |
###################################################################### |
| 93 |
# save |
| 94 |
eval { |
| 95 |
$f = $top->FBox(-defaultextension => ".PL", |
| 96 |
-filetypes => [ |
| 97 |
['Text Files', ['.txt', '.text']], |
| 98 |
['TCL Scripts', '.tcl' ], |
| 99 |
['C Source Files', '.c', 'TEXT'], |
| 100 |
['GIF Files', '.gif', ], |
| 101 |
['GIF Files', '', 'GIFF'], |
| 102 |
['All Files', '*', ], |
| 103 |
], |
| 104 |
-initialdir => ".", |
| 105 |
-initialfile => "Makefile.PL", |
| 106 |
-title => "Save file", |
| 107 |
-type => "save", |
| 108 |
-filter => "*.PL", # ignored |
| 109 |
-font => "Helvetica 14", |
| 110 |
); |
| 111 |
}; |
| 112 |
is($@, "", "creating Tk::FBox widget for save"); |
| 113 |
|
| 114 |
catch_grabs { |
| 115 |
destroy_if_visible($f) if $ENV{BATCH}; |
| 116 |
my $result = $f->Show; |
| 117 |
if (!$ENV{BATCH}) { |
| 118 |
diag "Result is <$result>"; |
| 119 |
} |
| 120 |
pass("After showing FBox"); |
| 121 |
} 1; |
| 122 |
|
| 123 |
###################################################################### |
| 124 |
# dir |
| 125 |
eval { |
| 126 |
$f = $top->FBox(-initialdir => ".", |
| 127 |
-title => "Choose directory", |
| 128 |
-type => "dir", |
| 129 |
-font => "Helvetica 14", |
| 130 |
); |
| 131 |
}; |
| 132 |
is($@, "", "creating Tk::FBox widget for choosing directories"); |
| 133 |
|
| 134 |
catch_grabs { |
| 135 |
destroy_if_visible($f) if $ENV{BATCH}; |
| 136 |
my $result = $f->Show; |
| 137 |
if (!$ENV{BATCH}) { |
| 138 |
diag "Result is <$result>"; |
| 139 |
} |
| 140 |
pass("After showing FBox"); |
| 141 |
} 1; |
| 142 |
|
| 143 |
###################################################################### |
| 144 |
# getOpenFile etc. |
| 145 |
TODO: { |
| 146 |
## XXX works everywhere? |
| 147 |
# skip("getOpenFile etc. only on X11", 3) |
| 148 |
# if $Tk::platform ne 'unix'; |
| 149 |
todo_skip("known coredumps with multiple MainWindows on some systems", 5) |
| 150 |
if 1; |
| 151 |
|
| 152 |
catch_grabs { |
| 153 |
my $mw = MainWindow->new; |
| 154 |
$mw->geometry('+0+0'); |
| 155 |
$mw->after($delay, sub { $mw->destroy }) if $ENV{BATCH}; |
| 156 |
my $result = $mw->getOpenFile; |
| 157 |
if (!$ENV{BATCH}) { |
| 158 |
diag "Result is <$result>"; |
| 159 |
} |
| 160 |
pass("called getOpenFile"); |
| 161 |
}; |
| 162 |
|
| 163 |
catch_grabs { |
| 164 |
my $mw = MainWindow->new; |
| 165 |
$mw->geometry('+0+0'); |
| 166 |
$mw->after($delay, sub { $mw->destroy }) if $ENV{BATCH}; |
| 167 |
my $result = $mw->getSaveFile; |
| 168 |
if (!$ENV{BATCH}) { |
| 169 |
diag "Result is <$result>"; |
| 170 |
} |
| 171 |
pass("called getSaveFile"); |
| 172 |
}; |
| 173 |
|
| 174 |
catch_grabs { |
| 175 |
my $mw = MainWindow->new; |
| 176 |
$mw->geometry('+0+0'); |
| 177 |
$mw->after($delay, sub { $mw->destroy }) if $ENV{BATCH}; |
| 178 |
my $result = $mw->chooseDirectory; |
| 179 |
if (!$ENV{BATCH}) { |
| 180 |
diag "Result is <$result>"; |
| 181 |
} |
| 182 |
pass("called chooseDirectory"); |
| 183 |
}; |
| 184 |
|
| 185 |
catch_grabs { |
| 186 |
my $mw = MainWindow->new; |
| 187 |
$mw->geometry('+0+0'); |
| 188 |
$mw->after($delay, sub { $mw->destroy }) if $ENV{BATCH}; |
| 189 |
my $result = $mw->getOpenFile(-multiple => 1, -title => "getOpenFile with -multiple"); |
| 190 |
ok(!defined $result || ref($result) eq "ARRAY", "Result of -multiple is an array reference or undef"); |
| 191 |
if (!$ENV{BATCH}) { |
| 192 |
diag "Result is <@$result>" if $result; |
| 193 |
} |
| 194 |
pass("called getOpenFile with -multiple"); |
| 195 |
}; |
| 196 |
} |
| 197 |
|
| 198 |
# Tk::FBox is internally using waitVisibility. The test may however |
| 199 |
# try to destroy the window before the visibility ever changed, |
| 200 |
# leading to errors like |
| 201 |
# |
| 202 |
# window ".fbox" was deleted before its visibility changed |
| 203 |
# |
| 204 |
# To prevent this, the <Visibility> events are trapped. If there |
| 205 |
# was no such event when the window is about to be destroyed, |
| 206 |
# then the script waits another second, up to a maximum of |
| 207 |
# 10 seconds. |
| 208 |
sub destroy_if_visible { |
| 209 |
my $w = shift; |
| 210 |
my $visibility_changed = 0; |
| 211 |
$w->bind('<Visibility>' => sub { $visibility_changed = 1 }); |
| 212 |
my $trials = 0; |
| 213 |
my $destroy_if_visibility_changed; |
| 214 |
$destroy_if_visibility_changed = |
| 215 |
sub { |
| 216 |
if ($visibility_changed) { |
| 217 |
$w->destroy; |
| 218 |
} else { |
| 219 |
$trials++; |
| 220 |
if ($trials > 10) { |
| 221 |
diag "Window never got visible, destroying nevertheless..."; |
| 222 |
$w->destroy; |
| 223 |
} else { |
| 224 |
if ($trials == 1) { |
| 225 |
diag "Slow delivery of <Visibility> event, waiting..."; |
| 226 |
} |
| 227 |
$w->after(1000, $destroy_if_visibility_changed); |
| 228 |
} |
| 229 |
} |
| 230 |
}; |
| 231 |
$w->after($delay, $destroy_if_visibility_changed); |
| 232 |
} |
| 233 |
|
| 234 |
1; |
| 235 |
__END__ |