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