1# This file is a Tcl script to test out images of type "bitmap" (i.e.,
2# the procedures in the file tkImgBmap.c).  It is organized in the
3# standard fashion for Tcl tests.
4#
5# Copyright (c) 1994 The Regents of the University of California.
6# Copyright (c) 1994-1995 Sun Microsystems, Inc.
7# Copyright (c) 1998-1999 by Scriptics Corporation.
8# All rights reserved.
9#
10# RCS: @(#) $Id: imgBmap.test,v 1.4 2002/07/13 21:52:34 dgp Exp $
11
12package require tcltest 2.1
13namespace import -force tcltest::configure
14namespace import -force tcltest::testsDirectory
15configure -testdir [file join [pwd] [file dirname [info script]]]
16configure -loadfile [file join [testsDirectory] constraints.tcl]
17tcltest::loadTestedCommands
18
19namespace import -force tcltest::makeFile
20namespace import -force tcltest::removeFile
21
22set data1 {#define foo_width 16
23#define foo_height 16
24#define foo_x_hot 3
25#define foo_y_hot 3
26static unsigned char foo_bits[] = {
27   0xff, 0xff, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
28   0x81, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
29   0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0xff, 0xff};
30}
31set data2 {
32    #define foo2_width 16
33    #define foo2_height 16
34    static char foo2_bits[] = {
35       0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
36       0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
37       0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0xff};
38}
39makeFile $data1 foo.bm
40makeFile $data2 foo2.bm
41
42eval image delete [image names]
43canvas .c
44pack .c
45update
46image create bitmap i1
47.c create image 200 100 -image i1
48update
49proc bgerror msg {
50    global errMsg
51    set errMsg $msg
52}
53test imageBmap-1.1 {options for bitmap images} {
54    image create bitmap i1 -background #123456
55    lindex [i1 configure -background] 4
56} {#123456}
57test imageBmap-1.2 {options for bitmap images} {
58    set errMsg {}
59    image create bitmap i1 -background lousy
60    update
61    list $errMsg $errorInfo
62} {{unknown color name "lousy"} {unknown color name "lousy"
63    (while configuring image "i1")}}
64test imageBmap-1.3 {options for bitmap images} {
65    image create bitmap i1 -data $data1
66    lindex [i1 configure -data] 4
67} $data1
68test imageBmap-1.4 {options for bitmap images} {
69    list [catch {image create bitmap i1 -data bogus} msg] $msg
70} {1 {format error in bitmap data}}
71test imageBmap-1.5 {options for bitmap images} {
72    image create bitmap i1 -file foo.bm
73    lindex [i1 configure -file] 4
74} foo.bm
75test imageBmap-1.6 {options for bitmap images} {
76    list [catch {image create bitmap i1 -file bogus} msg] [string tolower $msg]
77} {1 {couldn't read bitmap file "bogus": no such file or directory}}
78test imageBmap-1.7 {options for bitmap images} {
79    image create bitmap i1 -foreground #00ff00
80    lindex [i1 configure -foreground] 4
81} {#00ff00}
82test imageBmap-1.8 {options for bitmap images} {
83    set errMsg {}
84    image create bitmap i1 -foreground bad_color
85    update
86    list $errMsg $errorInfo
87} {{unknown color name "bad_color"} {unknown color name "bad_color"
88    (while configuring image "i1")}}
89test imageBmap-1.9 {options for bitmap images} {
90    image create bitmap i1 -data $data1 -maskdata $data2
91    lindex [i1 configure -maskdata] 4
92} $data2
93test imageBmap-1.10 {options for bitmap images} {
94    list [catch {image create bitmap i1 -data $data1 -maskdata bogus} msg] $msg
95} {1 {format error in bitmap data}}
96test imageBmap-1.11 {options for bitmap images} {
97    image create bitmap i1 -file foo.bm -maskfile foo2.bm
98    lindex [i1 configure -maskfile] 4
99} foo2.bm
100test imageBmap-1.12 {options for bitmap images} {
101    list [catch {image create bitmap i1 -data $data1 -maskfile bogus} msg] \
102	    [string tolower $msg]
103} {1 {couldn't read bitmap file "bogus": no such file or directory}}
104rename bgerror {}
105
106test imageBmap-2.1 {ImgBmapCreate procedure} {
107    eval image delete [image names]
108    .c delete all
109    list [catch {image create bitmap -gorp dum} msg] $msg [image names]
110} {1 {unknown option "-gorp"} {}}
111test imageBmap-2.2 {ImgBmapCreate procedure} {
112    eval image delete [image names]
113    .c delete all
114    image create bitmap image1
115    list [info commands image1] [image names] \
116	    [image width image1] [image height image1] \
117	    [lindex [image1 configure -foreground] 4] \
118	    [lindex [image1 configure -background] 4]
119} {image1 image1 0 0 #000000 {}}
120
121test imageBmap-3.1 {ImgBmapConfigureMaster procedure, memory de-allocation} {
122    image create bitmap i1 -data $data1
123    i1 configure -data $data1
124} {}
125test imageBmap-3.2 {ImgBmapConfigureMaster procedure} {
126    image create bitmap i1 -data $data1
127    list [catch {i1 configure -data bogus} msg] $msg [image width i1] \
128	    [image height i1]
129} {1 {format error in bitmap data} 16 16}
130test imageBmap-3.3 {ImgBmapConfigureMaster procedure, memory de-allocation} {
131    image create bitmap i1 -data $data1 -maskdata $data2
132    i1 configure -maskdata $data2
133} {}
134test imageBmap-3.4 {ImgBmapConfigureMaster procedure} {
135    image create bitmap i1
136    list [catch {i1 configure -maskdata $data2} msg] $msg
137} {1 {can't have mask without bitmap}}
138test imageBmap-3.5 {ImgBmapConfigureMaster procedure} {
139    list [catch {image create bitmap i1 -data $data1 -maskdata {
140	#define foo_width 8
141	#define foo_height 16
142	static char foo_bits[] = {
143	   0xff, 0xff, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
144	   0x81, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0x81};
145	}
146    } msg] $msg
147} {1 {bitmap and mask have different sizes}}
148test imageBmap-3.6 {ImgBmapConfigureMaster procedure} {
149    list [catch {image create bitmap i1 -data $data1 -maskdata {
150	#define foo_width 16
151	#define foo_height 8
152	static char foo_bits[] = {
153	   0xff, 0xff, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
154	   0x81, 0x81, 0xff, 0xff, 0xff, 0xff, 0x81, 0x81};
155	}
156    } msg] $msg
157} {1 {bitmap and mask have different sizes}}
158test imageBmap-3.7 {ImgBmapConfigureMaster procedure} {
159    image create bitmap i1 -data $data1
160    .c create image 100 100 -image i1 -tags i1.1 -anchor nw
161    .c create image 200 100 -image i1 -tags i1.2 -anchor nw
162    update
163    i1 configure -data {
164	#define foo2_height 14
165	#define foo2_width 15
166	static char foo2_bits[] = {
167	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
168	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
169	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
170	   0xff, 0xff};
171    }
172    update
173    list [image width i1] [image height i1] [.c bbox i1.1] [.c bbox i1.2]
174} {15 14 {100 100 115 114} {200 100 215 114}}
175
176test imageBmap-4.1 {ImgBmapConfigureInstance procedure: check error handling} {
177    proc bgerror args {}
178    .c delete all
179    image create bitmap i1 -file foo.bm
180    .c create image 100 100 -image i1
181    update
182    i1 configure -foreground bogus
183    update
184} {}
185
186test imageBmap-5.1 {GetBitmapData procedure} {
187    list [catch {image create bitmap -file ~bad_user/a/b} msg] \
188	    [string tolower $msg]
189} {1 {user "bad_user" doesn't exist}}
190test imageBmap-5.2 {GetBitmapData procedure} {
191    list [catch {image create bitmap -file bad_name} msg] [string tolower $msg]
192} {1 {couldn't read bitmap file "bad_name": no such file or directory}}
193test imageBmap-5.3 {GetBitmapData procedure} {
194    eval image delete [image names]
195    .c delete all
196    list [catch {image create bitmap -data { }} msg] $msg
197} {1 {format error in bitmap data}}
198test imageBmap-5.4 {GetBitmapData procedure} {
199    eval image delete [image names]
200    .c delete all
201    list [catch {image create bitmap -data {#define foo2_width}} msg] $msg
202} {1 {format error in bitmap data}}
203test imageBmap-5.5 {GetBitmapData procedure} {
204    eval image delete [image names]
205    .c delete all
206    list [catch {image create bitmap -data {#define foo2_width gorp}} msg] $msg
207} {1 {format error in bitmap data}}
208test imageBmap-5.6 {GetBitmapData procedure} {
209    eval image delete [image names]
210    .c delete all
211    list [catch {image create bitmap -data {#define foo2_width 1.4}} msg] $msg
212} {1 {format error in bitmap data}}
213test imageBmap-5.7 {GetBitmapData procedure} {
214    eval image delete [image names]
215    .c delete all
216    list [catch {image create bitmap -data {#define foo2_height}} msg] $msg
217} {1 {format error in bitmap data}}
218test imageBmap-5.8 {GetBitmapData procedure} {
219    eval image delete [image names]
220    .c delete all
221    list [catch {image create bitmap -data {#define foo2_height gorp}} msg] $msg
222} {1 {format error in bitmap data}}
223test imageBmap-5.9 {GetBitmapData procedure} {
224    eval image delete [image names]
225    .c delete all
226    list [catch {image create bitmap -data {#define foo2_height 1.4}} msg] $msg
227} {1 {format error in bitmap data}}
228test imageBmap-5.10 {GetBitmapData procedure} {
229    eval image delete [image names]
230    .c delete all
231    image create bitmap i1 -data {
232	#define foo2_height 14
233	#define foo2_width 15 xx _widtg 18 xwidth 18 _heighz 18 xheight 18
234	static char foo2_bits[] = {
235	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
236	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
237	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
238	   0xff, 0xff};
239    }
240    list [image width i1] [image height i1]
241} {15 14}
242test imageBmap-5.11 {GetBitmapData procedure} {
243    eval image delete [image names]
244    .c delete all
245    image create bitmap i1 -data {
246	_height 14 _width 15
247	char {
248	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
249	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
250	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
251	   0xff, 0xff}
252    }
253    list [image width i1] [image height i1]
254} {15 14}
255test imageBmap-5.12 {GetBitmapData procedure} {
256    eval image delete [image names]
257    .c delete all
258    list [catch {image create bitmap i1 -data {
259	#define foo2_height 14
260	#define foo2_width 15
261	static short foo2_bits[] = {
262	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
263	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
264	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
265	   0xff, 0xff};
266    }} msg] $msg
267} {1 {format error in bitmap data; looks like it's an obsolete X10 bitmap file}}
268test imageBmap-5.13 {GetBitmapData procedure} {
269    eval image delete [image names]
270    .c delete all
271    list [catch {image create bitmap i1 -data {
272	#define foo2_height 16
273	#define foo2_width 16
274	static char foo2_bits[] =
275	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
276	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
277	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
278	   0xff, 0xff;
279    }} msg] $msg
280} {1 {format error in bitmap data}}
281test imageBmap-5.14 {GetBitmapData procedure} {
282    eval image delete [image names]
283    .c delete all
284    list [catch {image create bitmap i1 -data {
285	#define foo2_width 16
286	static char foo2_bits[] = {
287	   0xff, 0xff, 0xff, }}} msg] $msg
288} {1 {format error in bitmap data}}
289test imageBmap-5.15 {GetBitmapData procedure} {
290    eval image delete [image names]
291    .c delete all
292    list [catch {image create bitmap i1 -data {
293	#define foo2_height 16
294	static char foo2_bits[] = {
295	   0xff, 0xff, 0xff, }}} msg] $msg
296} {1 {format error in bitmap data}}
297test imageBmap-5.16 {GetBitmapData procedure} {
298    eval image delete [image names]
299    .c delete all
300    list [catch {image create bitmap i1 -data {
301	#define foo2_height 16
302	#define foo2_width 16
303	static char foo2_bits[] = {
304	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
305	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
306	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
307	   0xff, foo};
308    }} msg] $msg
309} {1 {format error in bitmap data}}
310test imageBmap-5.17 {GetBitmapData procedure} {
311    eval image delete [image names]
312    .c delete all
313    list [catch {image create bitmap i1 -data "
314	#define foo2_height 16
315	#define foo2_width 16
316	static char foo2_bits[] = \{
317	   0xff, 0xff, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
318	   0xff, 0x81, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x81,
319	   0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81, 0xff, 0x81,
320	   0xff
321    "} msg] $msg
322} {1 {format error in bitmap data}}
323
324test imageBmap-6.1 {NextBitmapWord procedure} {
325    eval image delete [image names]
326    .c delete all
327    list [catch {image create bitmap i1 -data {1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890}} msg] $msg
328} {1 {format error in bitmap data}}
329test imageBmap-6.2 {NextBitmapWord procedure} {
330    eval image delete [image names]
331    .c delete all
332    makeFile {1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890} foo3.bm
333    list [catch {image create bitmap i1 -file foo3.bm} msg] $msg
334} {1 {format error in bitmap data}}
335test imageBmap-6.3 {NextBitmapWord procedure} {
336    eval image delete [image names]
337    .c delete all
338    makeFile {   } foo3.bm
339    list [catch {image create bitmap i1 -file foo3.bm} msg] $msg
340} {1 {format error in bitmap data}}
341removeFile foo3.bm
342
343eval image delete [image names]
344.c delete all
345image create bitmap i1
346test imageBmap-7.1 {ImgBmapCmd procedure} {
347    list [catch {i1} msg] $msg
348} {1 {wrong # args: should be "i1 option ?arg arg ...?"}}
349test imageBmap-7.2 {ImgBmapCmd procedure, "cget" option} {
350    list [catch {i1 cget} msg] $msg
351} {1 {wrong # args: should be "i1 cget option"}}
352test imageBmap-7.3 {ImgBmapCmd procedure, "cget" option} {
353    list [catch {i1 cget a b} msg] $msg
354} {1 {wrong # args: should be "i1 cget option"}}
355test imageBmap-7.4 {ImgBmapCmd procedure, "cget" option} {
356    i1 co -foreground #123456
357    i1 cget -foreground
358} {#123456}
359test imageBmap-7.5 {ImgBmapCmd procedure, "cget" option} {
360    list [catch {i1 cget -stupid} msg] $msg
361} {1 {unknown option "-stupid"}}
362test imageBmap-7.6 {ImgBmapCmd procedure} {
363    llength [i1 configure]
364} {6}
365test imageBmap-7.7 {ImgBmapCmd procedure} {
366    i1 co -foreground #001122
367    i1 configure -foreground
368} {-foreground {} {} #000000 #001122}
369test imageBmap-7.8 {ImgBmapCmd procedure} {
370    list [catch {i1 configure -gorp} msg] $msg
371} {1 {unknown option "-gorp"}}
372test imageBmap-7.9 {ImgBmapCmd procedure} {
373    list [catch {i1 configure -foreground #221100 -background} msg] $msg
374} {1 {value for "-background" missing}}
375test imageBmap-7.10 {ImgBmapCmd procedure} {
376    list [catch {i1 gorp} msg] $msg
377} {1 {bad option "gorp": must be cget or configure}}
378
379test imageBmap-8.1 {ImgBmapGet/Free procedures, shared instances} {
380    eval image delete [image names]
381    .c delete all
382    image create bitmap i1 -data $data1
383    .c create image 50 100 -image i1 -tags i1.1
384    .c create image 150 100 -image i1 -tags i1.2
385    .c create image 250 100 -image i1 -tags i1.3
386    update
387    .c delete i1.1
388    i1 configure -background black
389    update
390    .c delete i1.2
391    i1 configure -background white
392    update
393    .c delete i1.3
394    i1 configure -background black
395    update
396    image delete i1
397} {}
398
399test imageBmap-9.1 {ImgBmapDisplay procedure, nothing to display} {
400    proc bgerror args {}
401    eval image delete [image names]
402    .c delete all
403    image create bitmap i1 -data $data1
404    .c create image 50 100 -image i1 -tags i1.1
405    i1 configure -data {}
406    update
407} {}
408test imageBmap-9.2 {ImgBmapDisplay procedure, nothing to display} {
409    proc bgerror args {}
410    eval image delete [image names]
411    .c delete all
412    image create bitmap i1 -data $data1
413    .c create image 50 100 -image i1 -tags i1.1
414    i1 configure -foreground bogus
415    update
416} {}
417if {[info exists bgerror]} {
418    rename bgerror {}
419}
420
421test imageBmap-10.1 {ImgBmapFree procedure, resource freeing} {
422    eval image delete [image names]
423    .c delete all
424    image create bitmap i1 -data $data1 -maskdata $data2 -foreground #112233 \
425	    -background #445566
426    .c create image 100 100 -image i1
427    update
428    .c delete all
429    image delete i1
430} {}
431test imageBmap-10.2 {ImgBmapFree procedures, unlinking} {
432    eval image delete [image names]
433    .c delete all
434    image create bitmap i1 -data $data1 -maskdata $data2 -foreground #112233 \
435	    -background #445566
436    .c create image 100 100 -image i1
437    button .b1 -image i1
438    button .b2 -image i1
439    button .b3 -image i1
440    pack .b1 .b2 .b3
441    update
442    destroy .b2
443    update
444    destroy .b3
445    update
446    destroy .b1
447    update
448    .c delete all
449} {}
450
451test imageBmap-11.1 {ImgBmapDelete procedure} {
452    image create bitmap i2 -file foo.bm -maskfile foo2.bm
453    image delete i2
454    info command i2
455} {}
456test imageBmap-11.2 {ImgBmapDelete procedure} {
457    image create bitmap i2 -file foo.bm -maskfile foo2.bm
458    rename i2 newi2
459    set x [list [info command i2] [info command new*] [newi2 cget -file]]
460    image delete i2
461    lappend x [info command new*]
462} {{} newi2 foo.bm {}}
463
464test imageBmap-12.1 {ImgBmapCmdDeletedProc procedure} {
465    image create bitmap i2 -file foo.bm -maskfile foo2.bm
466    rename i2 {}
467    list [lsearch -exact [image names] i2] [catch {i2 foo} msg] $msg
468} {-1 1 {invalid command name "i2"}}
469
470removeFile foo.bm
471removeFile foo2.bm
472destroy .c
473eval image delete [image names]
474
475# cleanup
476::tcltest::cleanupTests
477return
478
479
480
481
482
483
484
485
486
487
488
489
490
491