Debian Codegraph @

t/photo.t

raw
1 BEGIN { $^W = 1; $| = 1;}
2 use strict;
3 use File::Temp qw(tempfile);
4 use Test::More;
5 use Tk;
6 use Tk::Photo;
7
8 my $numFormats = 5;
9 eval { require Tk::JPEG };
10 $numFormats++ unless $@;
11
12 eval { require Tk::PNG };
13 $numFormats++ unless $@;
14
15 my $mw = MainWindow->new();
16 $mw->geometry('+100+100');
17
18 plan tests => (2*(7 * $numFormats) + 2 + 2 + 1 + 2 + 2 + 2 + 2);
19
20 my @files = ();
21
22 my $row = 0;
23 foreach my $leaf('Tk.xbm','Xcamel.gif')
24 {
25 my $file = Tk->findINC($leaf);
26 my $src = $mw->Photo(-file => $file);
27 ok defined($src), "Load $file";
28 my $kind = 'Initial';
29 my $col = 0;
30 $mw->Label(-text => 'Initial')->grid(-row => $row, -column => $col);
31 $mw->Label(-background => 'white',-image => $src)->grid(-row => $row+1, -column => $col++);
32 $mw->update;
33
34 foreach $kind ($src->formats)
35 {
36 my $f = lc("t/test.$kind");
37 my $p = $f;
38 push(@files,$f);
39 eval { $src->write($f, -format => "$kind") };
40 is $@, '', "No error writing $f as $kind";
41 is $p, $f, "File name is not corrupted ($f)";
42 ok -f $f, "File $f was created";
43 my $new;
44 eval { $new = $mw->Photo(-file => $f, -format => "$kind") };
45 is $@, '', "No error loading $f as $kind";
46 ok defined($new), "Loading $f ($kind)";
47
48 my $skip_unsupported_data_format = $kind =~ /^(PPM|gif)$/ ? "$kind is not supported" : "";
49
50 my $data;
51 my $new2;
52 if ($skip_unsupported_data_format)
53 {
54 Tk::catch { $data = $src->data(-format => $kind) };
55 like $@, qr/image string format "$kind" is not supported/, "Error message for $kind";
56 SKIP: { skip "No data for $kind", 1 }
57 }
58 else
59 {
60 $data = $src->data(-format => $kind);
61 ok defined($data) && $data ne "", "$kind returns data";
62 if (defined $data)
63 {
64 $new2 = $mw->Photo(-data => $data, -format => $kind) if defined $data;
65 ok defined $new2, "Data back to image";
66 }
67 else
68 {
69 SKIP: { skip "No data was returned", 1 }
70 }
71 }
72
73 $mw->Label(-text => $kind)->grid(-row => $row, -column => $col);
74 $mw->Label(-background => 'white', -image => $new)->grid(-row => $row+1, -column => $col);
75 if (defined $new2) {
76 $mw->Label(-background => 'white', -image => $new2)->grid(-row => $row+2, -column => $col);
77 }
78 $mw->update;
79 $col++;
80 }
81 $row += 3;
82 }
83
84 # Extra tests
85 my $col = 0;
86 $mw->Label(-text => "Extra tests")->grid(-row => $row++, -column => $col);
87 my $file = Tk->findINC('Xcamel.gif');
88 my $data = do { open my $fh, $file or die $!; binmode $fh; local $/; <$fh> };
89
90 SKIP:
91 {
92 skip "Binary GIF data not supported", 1
93 if $Tk::VERSION <= 804.027;
94
95 my $image = $mw->Photo(-data => $data);
96 ok defined $image, "Read binary GIF data";
97 $mw->Label(-background => 'white', -image => $image)->grid(-row => $row, -column => $col);
98 $mw->update;
99 }
100 $col++;
101
102 SKIP:
103 {
104 skip "Need MIME::Base64 module", 1
105 if !eval { require MIME::Base64; 1 };
106
107 my $image = $mw->Photo(-data => MIME::Base64::encode_base64($data));
108 ok defined $image, "Read base64 encoded GIF data";
109 $mw->Label(-background => 'white', -image => $image)->grid(-row => $row, -column => $col);
110 $mw->update;
111 }
112 $col++;
113
114 {
115 # RT #70429: correct file name in error message
116 eval { $mw->Photo(-file => $0) };
117 like $@, qr{\Q$0\E}, 'File name appears in error message';
118 }
119
120 {
121 my($tmpfh,$tmpfile) = tempfile(SUFFIX => ".gif", UNLINK => 1)
122 or die "Cannot create temporary file: $!";
123 print $tmpfh "GIF89a\0\0\0\0";
124 close $tmpfh or die $!;
125
126 eval { $mw->Photo(-file => $tmpfile, -format => 'gif') };
127 like $@, qr{\Q$tmpfile\E}, 'File name appears in error message';
128 like $@, qr{\Qhas dimension(s) <= 0}, 'No dimensions error message';
129 }
130
131 {
132 # Test case for RT #128103
133 my $image = eval { $mw->Photo(-data => <<'EOF') };
134 /* XPM */
135 static char *noname[] = {
136 /* width height ncolors chars_per_pixel */
137 "80 1 4 1",
138 /* colors */
139 " c #000000",
140 ". c #914800",
141 "/ c #482455",
142 "* c #6D4800",
143 /* pixels */
144 "innyymtytnnt>qqqqqq=qcickkk,rwq,qrr,rir,rq>qrrrrjrxiriitq=@/*/@o^#=r,rtifqq@#@#="
145 };
146 EOF
147 is $@, '', 'No error';
148 ok $image, "'/*' in XPM data caused no problem";
149 }
150
151 {
152 # Similar test case for RT #128103 (comment marker in colors section)
153 my $image = eval { $mw->Photo(-data => <<'EOF') };
154 /* XPM */
155 static char *noname[] = {
156 /* width height ncolors chars_per_pixel */
157 "80 1 4 1",
158 /* colors */
159 " c #000000",
160 ". c #914800",
161 "/ c #482455", /* weird color */
162 "* c #6D4800",
163 /* pixels */
164 "................................................................................"
165 };
166 EOF
167 is $@, '', 'No error';
168 ok $image, "'/*' in XPM data caused no problem";
169 }
170
171 {
172 # Error case: short read
173 my $image = eval { $mw->Photo(-data => <<'EOF') };
174 /* XPM */
175 static char *noname[] = {
176 /* width height ncolors chars_per_pixel */
177 "2 2 2 1",
178 /* colors */
179 " c #000000",
180 ". c #914800",
181 /* pixels */
182 ".."
183 };
184 EOF
185 like $@, qr{^\QXPM image file is truncated; still 1 line(s) need to be read}, 'Expected error message about truncation';
186 ok !$image, "Truncated image was not created";
187 }
188
189 $mw->after(2500,[destroy => $mw]);
190 MainLoop;
191
192 foreach (@files)
193 {
194 unlink($_) if -f $_;
195 }
196