summaryrefslogtreecommitdiffstats
path: root/tests/visual.test
blob: ee13af24b16810284fbcc48f7785cdf191f7b4a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# This file is a Tcl script to test the visual- and colormap-handling
# procedures in the file tkVisual.c.  It is organized in the standard
# fashion for Tcl tests.
#
# Copyright (c) 1994 The Regents of the University of California.
# Copyright (c) 1994-1995 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
# All rights reserved.
#
# RCS: @(#) $Id: visual.test,v 1.6 2002/07/14 05:48:46 dgp Exp $

package require tcltest 2.1
namespace import -force tcltest::configure
namespace import -force tcltest::testsDirectory
configure -testdir [file join [pwd] [file dirname [info script]]]
configure -loadfile [file join [testsDirectory] constraints.tcl]
tcltest::loadTestedCommands

update

# eatColors --
# Creates a toplevel window and allocates enough colors in it to
# use up all the slots in the colormap.
#
# Arguments:
# w -		Name of toplevel window to create.

proc eatColors {w} {
    catch {destroy $w}
    toplevel $w
    wm geom $w +0+0
    canvas $w.c -width 400 -height 200 -bd 0
    pack $w.c
    for {set y 0} {$y < 8} {incr y} {
	for {set x 0} {$x < 40} {incr x} {
	    set color [format #%02x%02x%02x [expr $x*6] [expr $y*30] 0]
	    $w.c create rectangle [expr 10*$x] [expr 20*$y] \
		    [expr 10*$x + 10] [expr 20*$y + 20] -outline {} \
		    -fill $color
	}
    }
    update
}

# colorsFree --
#
# Returns 1 if there appear to be free colormap entries in a window,
# 0 otherwise.
#
# Arguments:
# w -			Name of window in which to check.
# red, green, blue -	Intensities to use in a trial color allocation
#			to see if there are colormap entries free.

proc colorsFree {w {red 31} {green 245} {blue 192}} {
    set vals [winfo rgb $w [format #%02x%02x%02x $red $green $blue]]
    expr ([lindex $vals 0]/256 == $red) && ([lindex $vals 1]/256 == $green) \
	    && ([lindex $vals 2]/256 == $blue)
}

# If more than one visual type is available for the screen, pick one
# that is *not* the default.

set default "[winfo visual .] [winfo depth .]"
set avail [winfo visualsavailable .]
set other {}
if {[llength $avail] > 1} {
    foreach visual $avail {
	if {$visual != $default} {
	    set other $visual
	    break
	}
    }
}

test visual-1.1 {Tk_GetVisual, copying from other window} {
    list [catch {toplevel .t -visual .foo.bar} msg] $msg
} {1 {bad window path name ".foo.bar"}}
if {$other != ""} {
    test visual-1.2 {Tk_GetVisual, copying from other window} {nonPortable} {
	catch {destroy .t1}
	catch {destroy .t2}
	toplevel .t1 -width 250 -height 100 -visual $other
	wm geom .t1 +0+0
	toplevel .t2 -width 200 -height 80 -visual .t1
	wm geom .t2 +5+5
	concat "[winfo visual .t2] [winfo depth .t2]"
    } $other
    test visual-1.3 {Tk_GetVisual, copying from other window} {
	catch {destroy .t1}
	catch {destroy .t2}
	toplevel .t1 -width 250 -height 100 -visual $other
	wm geom .t1 +0+0
	toplevel .t2 -width 200 -height 80 -visual .
	wm geom .t2 +5+5
	concat "[winfo visual .t2] [winfo depth .t2]"
    } $default

    # Make sure reference count is incremented when copying visual (the
    # following test will cause the colormap to be freed prematurely if
    # the reference count isn't incremented).
    test visual-1.4 {Tk_GetVisual, colormap reference count} {
	catch {destroy .t1}
	catch {destroy .t2}
	toplevel .t1 -width 250 -height 100 -visual $other
	wm geom .t1 +0+0
	set result [list [catch {toplevel .t2 -gorp 80 -visual .t1} msg] $msg]
	update
	set result
    } {1 {unknown option "-gorp"}}
}
test visual-1.5 {Tk_GetVisual, default colormap} {
    catch {destroy .t1}
    toplevel .t1 -width 250 -height 100 -visual default
    wm geometry .t1 +0+0
    update
    concat "[winfo visual .t1] [winfo depth .t1]"
} $default

set i 1
foreach visual $avail {
    test visual-2.$i {Tk_GetVisual, different visual types} {nonPortable} {
	catch {destroy .t1}
	toplevel .t1 -width 250 -height 100 -visual $visual
	wm geometry .t1 +0+0
	update
	concat "[winfo visual .t1] [winfo depth .t1]"
    } $visual
    incr i
}

test visual-3.1 {Tk_GetVisual, parsing visual string} {
    catch {destroy .t1}
    toplevel .t1 -width 250 -height 100 \
	    -visual "[winfo visual .][winfo depth .]"
    wm geometry .t1 +0+0
    update
    concat "[winfo visual .t1] [winfo depth .t1]"
} $default
test visual-3.2 {Tk_GetVisual, parsing visual string} {
    catch {destroy .t1}
    list [catch {
	toplevel .t1 -width 250 -height 100 -visual goop20
	wm geometry .t1 +0+0
    } msg] $msg
} {1 {unknown or ambiguous visual name "goop20": class must be best, directcolor, grayscale, greyscale, pseudocolor, staticcolor, staticgray, staticgrey, truecolor, or default}}
test visual-3.3 {Tk_GetVisual, parsing visual string} {
    catch {destroy .t1}
    list [catch {
	toplevel .t1 -width 250 -height 100 -visual d
	wm geometry .t1 +0+0
    } msg] $msg
} {1 {unknown or ambiguous visual name "d": class must be best, directcolor, grayscale, greyscale, pseudocolor, staticcolor, staticgray, staticgrey, truecolor, or default}}
test visual-3.4 {Tk_GetVisual, parsing visual string} {
    catch {destroy .t1}
    list [catch {
	toplevel .t1 -width 250 -height 100 -visual static
	wm geometry .t1 +0+0
    } msg] $msg
} {1 {unknown or ambiguous visual name "static": class must be best, directcolor, grayscale, greyscale, pseudocolor, staticcolor, staticgray, staticgrey, truecolor, or default}}
test visual-3.5 {Tk_GetVisual, parsing visual string} {
    catch {destroy .t1}
    list [catch {
	toplevel .t1 -width 250 -height 100 -visual "pseudocolor 48x"
	wm geometry .t1 +0+0
    } msg] $msg
} {1 {expected integer but got "48x"}}

if {$other != ""} {
    catch {destroy .t1}
    catch {destroy .t2}
    catch {destroy .t3}
    toplevel .t1 -width 250 -height 100 -visual $other
    wm geom .t1 +0+0
    toplevel .t2 -width 200 -height 80 -visual [winfo visual .]
    wm geom .t2 +5+5
    toplevel .t3 -width 150 -height 250 -visual [winfo visual .t1]
    wm geom .t3 +10+10
    test visual-4.1 {Tk_GetVisual, numerical visual id} nonPortable {
	list [winfo visualid .t2] [winfo visualid .t3]
    } [list [winfo visualid .] [winfo visualid .t1]]
    destroy .t1 .t2 .t3
}
test visual-4.2 {Tk_GetVisual, numerical visual id} {
    catch {destroy .t1}
    list [catch {toplevel .t1 -visual 12xyz} msg] $msg
} {1 {bad X identifier for visual: 12xyz"}}
test visual-4.3 {Tk_GetVisual, numerical visual id} {
    catch {destroy .t1}
    list [catch {toplevel .t1 -visual 1291673} msg] $msg
} {1 {couldn't find an appropriate visual}}

if ![string match *pseudocolor* $avail] {
    test visual-5.1 {Tk_GetVisual, no matching visual} {
	catch {destroy .t1}
	list [catch {
	    toplevel .t1 -width 250 -height 100 -visual "pseudocolor 8"
	    wm geometry .t1 +0+0
	} msg] $msg
    } {1 {couldn't find an appropriate visual}}
}

if {[string match *pseudocolor* $avail] && ([llength $avail] > 1)} {
    test visual-6.1 {Tk_GetVisual, no matching visual} {nonPortable} {
	catch {destroy .t1}
	toplevel .t1 -width 250 -height 100 -visual "best"
	wm geometry .t1 +0+0
	update
	winfo visual .t1
    } {pseudocolor}
}

# These tests are non-portable due to variations in how many colors
# are already in use on the screen.

if {([winfo visual .] == "pseudocolor") && ([winfo depth .] == 8)} {
    eatColors .t1
    test visual-7.1 {Tk_GetColormap, "new"} {nonPortable} {
	toplevel .t2 -width 30 -height 20
	wm geom .t2 +0+0
	update
	colorsFree .t2
    } {0}
    test visual-7.2 {Tk_GetColormap, "new"} {nonPortable} {
	catch {destroy .t2}
	toplevel .t2 -width 30 -height 20 -colormap new
	wm geom .t2 +0+0
	update
	colorsFree .t2
    } {1}
    test visual-7.3 {Tk_GetColormap, copy from other window} {nonPortable} {
	catch {destroy .t2}
	toplevel .t3 -width 400 -height 50 -colormap new
	wm geom .t3 +0+0
	catch {destroy .t2}
	toplevel .t2 -width 30 -height 20 -colormap .t3
	wm geom .t2 +0+0
	update
	destroy .t3
	colorsFree .t2
    } {1}
    test visual-7.4 {Tk_GetColormap, copy from other window} {nonPortable} {
	catch {destroy .t2}
	toplevel .t3 -width 400 -height 50 -colormap new
	wm geom .t3 +0+0
	catch {destroy .t2}
	toplevel .t2 -width 30 -height 20 -colormap .
	wm geom .t2 +0+0
	update
	destroy .t3
	colorsFree .t2
    } {0}
    test visual-7.5 {Tk_GetColormap, copy from other window} {nonPortable} {
	catch {destroy .t1}
	list [catch {toplevel .t1 -width 400 -height 50 \
		-colormap .choke.lots} msg] $msg
    } {1 {bad window path name ".choke.lots"}}
    if {$other != {}} {
	test visual-7.6 {Tk_GetColormap, copy from other window} {nonPortable} {
	    catch {destroy .t1}
	    catch {destroy .t2}
	    toplevel .t1 -width 300 -height 150 -visual $other
	    wm geometry .t1 +0+0
	    list [catch {toplevel .t2 -width 400 -height 50 \
		    -colormap .t1} msg] $msg
	} {1 {can't use colormap for .t1: incompatible visuals}}
    }
    catch {destroy .t1}
    catch {destroy .t2}
}

test visual-8.1 {Tk_FreeColormap procedure} {
    deleteWindows
    toplevel .t1 -width 300 -height 180 -colormap new
    wm geometry .t1 +0+0
    foreach i {.t2 .t3 .t4} {
	toplevel $i -width 250 -height 150 -colormap .t1
	wm geometry $i +0+0
    }
    destroy .t1
    destroy .t3
    destroy .t4
    update
} {}
if {$other != {}} {
    test visual-8.2 {Tk_FreeColormap procedure} {
	deleteWindows
	toplevel .t1 -width 300 -height 180 -visual $other
	wm geometry .t1 +0+0
	foreach i {.t2 .t3 .t4} {
	    toplevel $i -width 250 -height 150 -visual $other
	    wm geometry $i +0+0
	}
	destroy .t2
	destroy .t3
	destroy .t4
	update
    } {}
}

deleteWindows
rename eatColors {}
rename colorsFree {}

# cleanup
::tcltest::cleanupTests
return