Debian Codegraph @

t/canvas.t

raw
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 #
5 # $Id: $
6 # Translated by: Slaven Rezic
7 #
8 # Original copyright from tk/tests/canvas.test, version 1.23 from
9 # tktoolkit CVS on sourceforge:
10
11 # This file is a Tcl script to test out the procedures in tkCanvas.c,
12 # which implements generic code for canvases. It is organized in the
13 # standard fashion for Tcl tests.
14 #
15 # Copyright (c) 1995-1996 Sun Microsystems, Inc.
16 # Copyright (c) 1998-2000 Ajuba Solutions.
17 # All rights reserved.
18 #
19 # RCS: @(#) $Id: canvas.test,v 1.23 2004/12/07 21:22:19 dgp Exp $
20
21 use strict;
22 use FindBin;
23 use lib $FindBin::RealBin;
24
25 use Getopt::Long;
26 use Tk;
27
28 BEGIN {
29 if (!eval q{
30 use Test::More;
31 1;
32 }) {
33 print "1..0 # skip: no Test::More module\n";
34 exit;
35 }
36 }
37
38 use TkTest qw(is_float_pair);
39
40 plan tests => 166;
41
42 use_ok("Tk::Canvas");
43
44 my $verbose = 0;
45 GetOptions("v" => \$verbose)
46 or die "usage: $0 [-v]";
47
48 # XXX - This test file is woefully incomplete. At present, only a
49 # few of the features are tested.
50
51 my $mw = MainWindow->new;
52 $mw->geometry("+10+10");
53
54 if ($^O eq 'darwin') {
55 # Under some newer MacOSX versions it seems that there are
56 # problems by creating and destroying widgets with different
57 # widths. The canvas widget with requested -width 100 created
58 # after the one with -width 60 will only be 60px wide. This is
59 # probably some wm-related bug, but as we test here for canvas
60 # features, we need to workaround this bug. This is just done with
61 # a dummy frame, which just makes sure that the mainwindow width
62 # is at least 100 pixels.
63 $mw->Frame(-width => 100, -height => 1)->pack;
64 }
65
66 my $c = $mw->Canvas;
67 isa_ok($c, "Tk::Canvas");
68 isa_ok($c, "Tk::Widget");
69 $c->pack;
70 $c->update;
71
72 sub deleteWindows () {
73 eval { $_->destroy } for $mw->children;
74 }
75
76 use constant SKIP_CGET => 5;
77 use constant SKIP_CONF => 6;
78 use constant SKIP_ERROR => 7;
79 use constant SKIP_RESTORE => 8;
80
81 my @tests =
82 (
83 ['-background', '#ff0000', '#ff0000', 'non-existent',
84 'unknown color name "non-existent"'],
85 ['-bg', '#ff0000', '#ff0000', 'non-existent',
86 'unknown color name "non-existent"'],
87 [qw(-bd 4 4 badValue), 'bad screen distance "badValue"'],
88 [qw(-borderwidth 1.3 1 badValue), 'bad screen distance "badValue"'],
89 [qw(-closeenough 24 24 bogus), q{'bogus' isn't numeric}],
90 [qw(-confine true 1 silly), 'expected boolean value but got "silly"', 0,0,1,0], # probably auto-converted to some boolean value?
91 [qw(-cursor arrow arrow badValue), 'bad cursor spec "badValue"'],
92 [qw(-height 2.1 2 x42), 'bad screen distance "x42"'],
93 [qw(-highlightbackground), '#112233', '#112233', 'ugly', 'unknown color name "ugly"'],
94 [qw(-highlightcolor), '#110022', '#110022', 'bogus', 'unknown color name "bogus"'],
95 [qw(-highlightthickness 18 18 badValue), 'bad screen distance "badValue"'],
96 [qw(-insertbackground), '#110022', '#110022', 'bogus', 'unknown color name "bogus"'],
97 [qw(-insertborderwidth 1.3 1 2.6x), 'bad screen distance "2.6x"'],
98 [qw(-insertofftime 100 100 3.2), q{expected integer but got "3.2"}, 0,0,1,0], # probably auto-converted to integer?
99 [qw(-insertontime 100 100 3.2), q{expected integer but got "3.2"}, 0,0,1,0], # probably auto-converted to integer?
100 [qw(-insertwidth 1.3 1 6x), q{bad screen distance "6x"}],
101 [qw(-relief groove groove 1.5), q{bad relief type "1.5": must be flat, groove, raised, ridge, solid, or sunken}],
102 [qw(-selectbackground), '#110022', '#110022', 'bogus', q{unknown color name "bogus"}],
103 [qw(-selectborderwidth 1.3 1 badValue), q{bad screen distance "badValue"}],
104 [qw(-selectforeground), '#654321', '#654321', 'bogus', q{unknown color name "bogus"}],
105 [qw(-takefocus), "any string", "any string", undef, undef],
106 [qw(-width 402 402 xyz), q{bad screen distance "xyz"}],
107 [qw(-xscrollcommand), q{Some command}, q{Some command}, undef, undef, 1],
108 [qw(-yscrollcommand), q{Another command}, q{Another command}, undef, undef, 1],
109 );
110
111 foreach my $test (@tests) {
112 my $name = $test->[0];
113 $c->configure($name, $test->[1]);
114 if (!$test->[SKIP_CGET]) {
115 is($c->cget($name), $test->[2], "cget $name");
116 }
117 if (!$test->[SKIP_CONF]) {
118 is(($c->configure($name))[4], $c->cget($name), "Comparing configure and cget values for $name");
119 }
120
121 if (defined $test->[4]) {
122 if (!$test->[SKIP_ERROR]) {
123 eval { $c->configure($name, $test->[3]) };
124 like($@, qr/$test->[4]/, "Expected error message for $name");
125 }
126 }
127 if (!$test->[SKIP_RESTORE]) {
128 $c->configure($name, ($c->configure($name))[3]);
129 }
130 }
131
132 eval { $c->configure(-gorp => "foo") };
133 like($@, qr{Bad option `-gorp'}, "configure throws error on bad option");
134 $c->create("rect",10,10,100,100);
135 eval { $c->configure(-gorp => "foo") };
136 like($@, qr{Bad option `-gorp'}, "configure throws error on bad option");
137
138 eval { $c->destroy };
139 $c = $mw->Canvas(qw(-width 60 -height 40),
140 -scrollregion => [qw(0 0 200 150)],
141 -bd => 0,
142 -highlightthickness => 0,
143 )->pack;
144 $c->update;
145
146 {
147 my $i = $c->createRectangle(10,10,100,100);
148 eval { $c->bind($i, "<a>") };
149 is($@, "", "bind method");
150 }
151
152 {
153 my $i = $c->create('rect',10,10,100,100);
154 eval { $c->bind($i, "<") };
155 like($@, qr{no event type or button # or keysym}, "bind method with failure");
156 }
157
158 {
159 $c->configure(-xscrollincrement => 40, -yscrollincrement => 5);
160 $c->xview('moveto', 0);
161 $c->update;
162 is_float_pair([$c->xview], [0, 0.3], "xview method");
163 $c->xview('scroll', 2, 'units');
164 $c->update;
165 is_float_pair([$c->xview], [0.4, 0.7], "xview method after scroll");
166 }
167
168 {
169 # Tcl/Tk comment:
170 # This test gives slightly different results on platforms such
171 # as NetBSD. I don't know why...
172 # Perl/Tk comment:
173 # Everything's ok on a FreeBSD machine.
174 $c->configure(-xscrollincrement => 0, -yscrollincrement => 5);
175 $c->xviewMoveto(0.6);
176 $c->update;
177 is_float_pair([$c->xview], [0.6, 0.9], "xview method (2)");
178 $c->xviewScroll(2, 'units');
179 $c->update;
180 is_float_pair([$c->xview], [0.66, 0.96], "xview method after scroll (2)");
181 }
182
183 eval { $c->destroy };
184 $c = $mw->Canvas(qw(-width 60 -height 40),
185 -scrollregion => [qw(0 0 200 80)],
186 -borderwidth => 0,
187 -highlightthickness => 0,
188 )->pack;
189 $c->update;
190
191 {
192 $c->configure(qw(-xscrollincrement 40 -yscrollincrement 5));
193 $c->yview('moveto', 0);
194 $c->update;
195 is_float_pair([$c->yview], [0, 0.5], "yview method");
196 $c->yview('scroll', 3, 'units');
197 $c->update;
198 is_float_pair([$c->yview], [0.1875, 0.6875], "yview method after scroll");
199 }
200
201 {
202 $c->configure(qw(-xscrollincrement 40 -yscrollincrement 0));
203 $c->yviewMoveto(0);
204 $c->update;
205 is_float_pair([$c->yview], [0, 0.5], "yview method (2)");
206 $c->yviewScroll(2, 'units');
207 $c->update;
208 is_float_pair([$c->yview], [0.1, 0.6], "yview method after scroll (2)");
209 }
210
211 {
212 eval { $c->destroy };
213 $c = $mw->Canvas(qw(-width 100 -height 50),
214 -scrollregion => [qw(-200 -100 305 102)],
215 -borderwidth => 2,
216 -highlightthickness => 3,
217 )->pack;
218 $c->update;
219 $c->configure(qw(-xscrollincrement 0 -yscrollincrement 0));
220 $c->xview('moveto', 0);
221 $c->yview('moveto', 0);
222 $c->update;
223 is($c->canvasx(0), -205, "canvasx after scrolling to origin");
224 is($c->canvasy(0), -105, "canvasy after scrolling to origin");
225 }
226
227 {
228 $c->configure(qw(-xscrollincrement 20 -yscrollincrement 10));
229 my @x;
230 for my $i (qw(.08 .10 .48 .50)) {
231 $c->xviewMoveto($i);
232 $c->update;
233 push @x, $c->canvasx(0);
234 }
235 is_deeply(\@x, [-165, -145, 35, 55], "canvasx after multiple scroll");
236 }
237
238 {
239 $c->configure(qw(-xscrollincrement 20 -yscrollincrement 10));
240 my @x;
241 for my $i (qw(.06 .08 .70 .72)) {
242 $c->yviewMoveto($i);
243 $c->update;
244 push @x, $c->canvasy(0);
245 }
246 is_deeply(\@x, [-95, -85, 35, 45], "canvasy after multiple scroll");
247 }
248
249 {
250 $c->configure(qw(-xscrollincrement 20 -yscrollincrement 10));
251 $c->xview('moveto', 1.0);
252 is($c->canvasx(0), 215);
253 }
254
255 {
256 $c->configure(qw(-xscrollincrement 20 -yscrollincrement 10));
257 $c->yview(moveto => 1.0);
258 is($c->canvasy(0), 55);
259 }
260
261 deleteWindows;
262
263 {
264 eval { $c->destroy } if Tk::Exists($c); # without existence test dumps SV contents ...
265 $c = $mw->Canvas;
266 $c->create(qw(arc -100 10 100 210 -start 10 -extent 50 -style arc -tags arc1));
267 is_deeply([$c->bbox("arc1")], [qw(48 21 100 94)], "BBox of arc");
268 $c->createArc(qw(100 10 300 210 -start 10 -extent 50 -style chord -tags arc2));
269 is_deeply([$c->bbox("arc2")], [qw(248 21 300 94)], "BBox of chord");
270 $c->create(qw(arc 300 10 500 210 -start 10 -extent 50 -style pieslice -tags arc3));
271 is_deeply([$c->bbox("arc3")], [qw(398 21 500 112)], "BBox of pieslice");
272 }
273
274 {
275 eval { $c->destroy } if Tk::Exists($c); # without existence test dumps SV contents ...
276 $c = $mw->Canvas;
277
278 # With Tk 8.0.4 the ids are now stored in a hash table. You
279 # can use this test as a performance test with older versions
280 # by changing the value of size.
281 my $size = 15;
282
283 for my $i (0 .. $size-1) {
284 my $x = -10 + 3*$i;
285 for(my $j = 0, my $y = -10; $j < 10; $j++, $y+=3) {
286 $c->create('rect', "${x}c", "${y}c", ($x+2)."c", ($y+2)."c",
287 qw(-outline black -fill blue -tags rect));
288 $c->create('text', ($x+1)."c", ($y+1)."c", -text => "$i,$j",
289 qw(-anchor center -tags text));
290 }
291 }
292
293 # The actual bench mark - this code also exercises all the hash
294 # table changes.
295
296 my $time = Tk::timeofday();
297 foreach my $id ($c->find(withtag => "all")) {
298 $c->lower($id);
299 $c->raise($id);
300 $c->find(withtag => $id);
301 $c->bind('<Return>', $id, '');
302 $c->delete($id);
303 }
304 my $delta = Tk::timeofday() - $time;
305 diag "Canvas creation and deletion test needed $delta s"
306 if $verbose;
307 }
308
309 {
310 eval { $c->destroy } if Tk::Exists($c); # without existence test dumps SV contents ...
311 $c = $mw->Canvas;
312
313 $c->create(qw(oval 20 20 40 40 -fill red -tag) , [qw(a b c d)]);
314 $c->create(qw(oval 20 60 40 80 -fill yellow -tag), [qw(b a)]);
315 $c->create(qw(oval 20 100 40 120 -fill green -tag), [qw(c b)]);
316 $c->create(qw(oval 20 140 40 160 -fill blue -tag), [qw(b)]);
317 $c->create(qw(oval 20 180 40 200 -fill bisque -tag), [qw(a d e)]);
318 $c->create(qw(oval 20 220 40 240 -fill bisque -tag b));
319 $c->create(qw(oval 20 260 40 280 -fill bisque -tag), ['d', "tag with spaces"]);
320
321 is_deeply([$c->find(withtag => q{!a})],[qw(3 4 6 7)], "Tag expressions");
322 is_deeply([$c->find(withtag => q{b&&c})],[qw(1 3)]);
323 is_deeply([$c->find(withtag => q{b||c})],[qw(1 2 3 4 6)]);
324 is_deeply([$c->find(withtag => q{a&&!b})],[qw(5)]);
325 is_deeply([$c->find(withtag => q{!b&&!c})],[qw(5 7)]);
326 is_deeply([$c->find(withtag => q{d&&a&&c&&b})],[qw(1)]);
327 is_deeply([$c->find(withtag => q{b^a})],[qw(3 4 5 6)]);
328 is_deeply([$c->find(withtag => q{(a&&!b)||(!a&&b)})],[qw(3 4 5 6)]);
329 is_deeply([$c->find(withtag => q{ ( a && ! b ) || ( ! a && b ) })],[qw(3 4 5 6)]);
330 is_deeply([$c->find(withtag => q{a&&!(c||d)})],[qw(2)]);
331 is_deeply([$c->find(withtag => q{d&&"tag with spaces"})],[qw(7)], "Tag with spaces");
332 is_deeply([$c->find(withtag => q"tag with spaces")],[qw(7)]);
333 }
334
335 for my $testdef (
336 [q{&&c}, qr{Unexpected operator in tag search expression}],
337 [q{!!c}, qr{Too many '!' in tag search expression}],
338 [q{b||}, qr{Missing tag in tag search expression}],
339 [q{b&&(c||)}, qr{Unexpected operator in tag search expression}],
340 [q{d&&""}, qr{Null quoted tag string in tag search expression}],
341 [q"d&&\"tag with spaces", qr{Missing endquote in tag search expression}],
342 [q{a&&"tag with spaces"z}, qr{Invalid boolean operator in tag search expression}],
343 [q{a&&b&c}, qr{Singleton '&' in tag search expression}],
344 [q{a||b|c}, qr{Singleton '|' in tag search expression}],
345 ) {
346 my($tag_expr, $error_rx) = @$testdef;
347
348 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
349 $c = $mw->Canvas;
350
351 $c->create(qw(oval 20 20 40 40 -fill red -tag), [qw(a b c d)]);
352 $c->create(qw(oval 20 260 40 280 -fill bisque -tag), ['d', "tag with spaces"]);
353 eval { $c->find(withtag => $tag_expr) };
354 like($@, $error_rx, "Tag expression error ($tag_expr)");
355 }
356
357 {
358 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
359 $c = $mw->Canvas;
360
361 $c->create(qw(oval 20 20 40 40 -fill red -tag), [q{ strange tag(xxx&yyy|zzz) " && \" || ! ^ " }]);
362 ok($c->find(withtag => q{ strange tag(xxx&yyy|zzz) " && \" || ! ^ " }),
363 q{backward compatility - strange tags that are not expressions});
364 }
365
366 {
367 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
368 $c = $mw->Canvas;
369
370 $c->bind(q{a && b}, '<Enter>' => sub {warn "Enter"});
371 $c->bind(q{a && b}, '<Leave>' => sub {warn "Leave"});
372
373 pass(q{multple events bound to same tag expr});
374 }
375
376 {
377 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
378 $c = $mw->Canvas->pack;
379
380 # This would crash in 8.3.0 and 8.3.1
381 $c->create(qw(polygon 0 0 100 100 200 50),
382 -fill => undef,
383 qw(-stipple gray50 -outline black));
384 pass(q{canvas poly fill check, bug 5783});
385 }
386
387 {
388 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
389 $c = $mw->Canvas->pack;
390
391 $c->create(qw(poly 30 30 90 90 30 90 90 30));
392 ok($c->find(qw(over 40 40 45 45)), "rect region inc. edge; canvas poly overlap fill check, bug 226357");
393 ok($c->find(qw(over 60 40 60 40)), "top-center point");
394 ok(!$c->find(qw(over 0 0 0 0)), "not on poly");
395 {
396 # The failure only occurs with the real X server, but not
397 # with Xnest and Xvfb, and it seems to occur only on some
398 # X server versions (maybe driver dependent?). An equivalent
399 # wish8.4 program had the same problem.
400 local $TODO;
401 $TODO = "Failure observed under some conditions on Debian"
402 if $^O eq 'linux';
403 ok($c->find(qw(over 60 60 60 60)), "center-point");
404 }
405 ok(!$c->find(qw(over 45 50 45 50)), "outside poly");
406
407 $c->itemconfigure(1, -fill => "", -outline => "black");
408 ok($c->find(qw(over 40 40 45 45)), "rect region inc. edge");
409 ok($c->find(qw(over 60 40 60 40)), "top-center point");
410 ok(!$c->find(qw(over 0 0 0 0)), "not on poly");
411 ok($c->find(qw(over 60 60 60 60)), "center-point");
412 ok(!$c->find(qw(over 45 50 45 50)), "outside poly");
413
414 $c->itemconfigure(1, -width => 8);
415 ok($c->find(qw(over 45 50 45 50)), "outside poly?");
416 }
417
418 {
419 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
420 $c = $mw->Canvas->pack;
421
422 my $qx = 1.+1.;
423 # qx has type double and no string representation (in Tcl?)
424 $c->scale('all', $qx, 0, 1., 1.);
425 # qx has now type MMRep and no string representation (in Tcl?);
426 is($qx, 2, q{canvas mm obj, patch SF-403327, 102471});
427 }
428
429 {
430 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
431 $c = $mw->Canvas->pack;
432
433 my $val = 10;
434 $val++;
435 # qx has type double and no string representation (in Tcl?)
436 $c->scale('all', $val, 0, 1, 1);
437 # qx has now type MMRep and no string representation (in Tcl?)
438 $val++;
439 is($val, 12, q{canvas mm obj, patch SF-403327, 102471});
440 }
441
442 {
443 my $x = "";
444
445 my $kill_canvas = sub {
446 my $w = shift;
447 $w->destroy;
448 $w = $mw->Canvas(qw(-height 200 -width 200))->pack(qw(-fill both -expand yes));
449 $mw->idletasks;
450 $w->create('rectangle', qw(80 80 120 120 -fill blue -tags blue));
451 # bind a button press to re-build the canvas
452 $w->bind('blue', '<ButtonRelease-1>' => sub { $x .= "ok" });
453 $w;
454 };
455
456 $c = $kill_canvas->($c);
457
458 # do this many times to improve chances of triggering the crash
459 for my $i (0 .. 29) {
460 $c->eventGenerate('<1>', qw(-x 100 -y 100));
461 $c->eventGenerate('<ButtonRelease-1>', qw(-x 100 -y 100));
462 }
463 is($x, "okokokokokokokokokokokokokokokokokokokokokokokokokokokokokok",
464 q{canvas delete during event, SF bug-228024});
465 }
466
467 {
468 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
469 $c = $mw->Canvas->pack;
470
471 eval { $c->scan };
472 like($@, qr{\Qwrong # args: should be ".canvas scan mark|dragto x y ?dragGain?"\E},
473 q{canvas scan SF bug 581560});
474
475 eval { $c->scan("bogus") };
476 like($@, qr{\Qwrong # args: should be ".canvas scan mark|dragto x y ?dragGain?"\E},
477 "canvas scan");
478
479 eval { $c->scan("mark") };
480 like($@, qr{\Qwrong # args: should be ".canvas scan mark|dragto x y ?dragGain?"\E});
481
482 $c->scan(qw(mark 10 10));
483 pass("correct canvas scan mark");
484
485 eval { $c->scan(qw(mark 10 10 5)) };
486 like($@, qr{wrong # args: should be ".canvas scan mark x y"});
487
488 $c->scan(qw(dragto 10 10 5));
489 pass("correct canvas scan dragto");
490 }
491
492 {
493 foreach my $type (qw{arc bitmap image line oval polygon rect text window}) {
494 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
495 $c = $mw->Canvas->pack;
496
497 eval { $c->create($type) };
498 like($@, qr{wrong # args: should be ".canvas create $type coords \Q?arg arg ...?"\E},
499 "basic types check: $type requires coords");
500
501 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
502 $c = $mw->Canvas->pack;
503
504 eval { $c->create($type, 0) };
505 like($@, qr{wrong # coordinates: expected},
506 "basic coords check: $type coords are paired");
507 }
508 }
509
510 {
511 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
512 $c = $mw->Canvas->pack;
513
514 my $id = $c->createArc(qw(0 10 20 30 -start 33));
515 is($c->itemcget($id, "-start"), 33, "arc coords check");
516 }
517
518 {
519 local $TODO = "Decide whether test failures are expected or not...";
520
521 eval { $c->destroy } if Tk::Exists($c); # without existence test may dump SV contents ...
522 $c = $mw->Canvas->pack;
523
524 my $id = $c->createLine(qw{0 0 1 1 2 2 3 3 4 4 5 5 6 6});
525 is($c->itemcget($id, '-smooth'), 0);
526
527 foreach my $smoothtest (
528 ['yes', 'true'],
529 [1, 'true'],
530 ['bezier', 'true'],
531 ['raw', 'raw'],
532 ['r', 'raw'],
533 ['b', 'b']
534 ) {
535 my($smoother, $expected) = @$smoothtest;
536 $c->itemconfigure($id, -smooth => $smoother);
537 is($c->itemcget($id, '-smooth'), $expected, "smooth test");
538 }
539 }
540
541 __END__