summaryrefslogtreecommitdiffstats
path: root/tests/filebox.test
blob: 2f87c3e79cfadcd493d273841acbe440f1987200 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# This file is a Tcl script to test out Tk's "tk_getOpenFile" and
# "tk_getSaveFile" commands. It is organized in the standard fashion
# for Tcl tests.
#
# Copyright (c) 1996 Sun Microsystems, Inc.
# Copyright (c) 1998-1999 by Scriptics Corporation.
# All rights reserved.

package require tcltest 2.1
eval tcltest::configure $argv
tcltest::loadTestedCommands

test fileDialog-0.1 {GetFileName: file types: MakeFilter() fails} {
    # MacOS type that is too long

    set res [list [catch {tk_getSaveFile -filetypes {{"foo" .foo {\0\0\0\0\0}}}} msg] $msg]
    regsub -all "\0" $res {\\0}
} {1 {bad Macintosh file type "\0\0\0\0\0"}}
test fileDialog-0.2 {GetFileName: file types: MakeFilter() fails} {
    # MacOS type that is too short, but looks ok in utf (4 bytes).

    set x [catch {tk_getSaveFile -filetypes {{"foo" .foo {\0\0}}}} msg]
    regsub -all "\0" $msg {\\0} msg
    list $x $msg
} {1 {bad Macintosh file type "\0\0"}}

set tk_strictMotif_old $tk_strictMotif

#----------------------------------------------------------------------
#
# Procedures needed by this test file
#
#----------------------------------------------------------------------

proc ToPressButton {parent btn} {
    global isNative
    if {!$isNative} {
	after 100 SendButtonPress $parent $btn mouse
    }
}

proc ToEnterFileByKey {parent fileName fileDir} {
    global isNative
    if {!$isNative} {
	after 100 EnterFileByKey $parent [list $fileName] [list $fileDir]
    }
}

proc PressButton {btn} {
    event generate $btn <Enter>
    event generate $btn <1> -x 5 -y 5
    event generate $btn <ButtonRelease-1> -x 5 -y 5
}

proc EnterFileByKey {parent fileName fileDir} {
    global tk_strictMotif
    if {$parent == "."} {
	set w .__tk_filedialog
    } else {
	set w $parent.__tk_filedialog
    }
    upvar ::tk::dialog::file::__tk_filedialog data

    if {$tk_strictMotif} {
	$data(sEnt) delete 0 end
	$data(sEnt) insert 0 [file join $fileDir $fileName]
    } else {
	$data(ent) delete 0 end
	$data(ent) insert 0 $fileName
    }

    update
    SendButtonPress $parent ok mouse
}

proc SendButtonPress {parent btn type} {
    global tk_strictMotif
    if {$parent == "."} {
	set w .__tk_filedialog
    } else {
	set w $parent.__tk_filedialog
    }
    upvar ::tk::dialog::file::__tk_filedialog data

    set button $data($btn\Btn)
    if ![winfo ismapped $button] {
	update
    }

    if {$type == "mouse"} {
	PressButton $button
    } else {
	event generate $w <Enter>
	focus $w
	event generate $button <Enter>
	event generate $w <KeyPress> -keysym Return
    }
}


#----------------------------------------------------------------------
#
# The test suite proper
#
#----------------------------------------------------------------------

if {$tcl_platform(platform) == "unix"} {
    set modes "0 1"
} else {
    set modes 1
}

set unknownOptionsMsg(tk_getOpenFile) {bad option "-foo": must be -defaultextension, -filetypes, -initialdir, -initialfile, -multiple, -parent, -title, or -typevariable}
set unknownOptionsMsg(tk_getSaveFile) {bad option "-foo": must be -confirmoverwrite, -defaultextension, -filetypes, -initialdir, -initialfile, -parent, -title, or -typevariable}

set tmpFile "filebox.tmp"
makeFile {
    # this file can be empty!
} $tmpFile

array set filters {
    1 {}
    2 {
	{"Text files"		{.txt .doc}	}
	{"Text files"		{}		TEXT}
	{"Tcl Scripts"		{.tcl}		TEXT}
	{"C Source Files"	{.c .h}		}
	{"All Source Files"	{.tcl .c .h}	}
	{"Image Files"		{.gif}		}
	{"Image Files"		{.jpeg .jpg}	}
	{"Image Files"		""		{GIFF JPEG}}
	{"All files"		*}
    }
    3 {
	{"Text files"		{.txt .doc}	TEXT}
	{"Foo"			{""}		TEXT}
    }
}

foreach mode $modes {
    #
    # Test both the motif version and the "tk" version of the file dialog
    # box on Unix.
    #
    # Note that this means that test names are unusually complex.
    #

    set addedExtensions {}
    if {$tcl_platform(platform) == "unix"} {
	set tk_strictMotif $mode
	# Extension adding is only done when using the non-motif file
	# box with an extension-less filename
	if {!$mode} {
	    set addedExtensions {NONE {} .txt .txt}
	}
    }

    test filebox-1.1-$mode "tk_getOpenFile command" -body {
	tk_getOpenFile -foo
    } -returnCodes error -result $unknownOptionsMsg(tk_getOpenFile)

    catch {tk_getOpenFile -foo 1} msg
    regsub -all ,      $msg "" options
    regsub \"-foo\" $options "" options
    
    foreach option $options {
        if {[string index $option 0] eq "-"} {
	    test filebox-1.2-$mode$option "tk_getOpenFile command" -body {
		tk_getOpenFile $option
	    } -returnCodes error -result "value for \"$option\" missing"
        }
    }

    test filebox-1.3-$mode "tk_getOpenFile command" -body {
        tk_getOpenFile -foo bar
    } -returnCodes error -result $unknownOptionsMsg(tk_getOpenFile)
    test filebox-1.4-$mode "tk_getOpenFile command" -body {
        tk_getOpenFile -initialdir
    } -returnCodes error -result {value for "-initialdir" missing}
    test filebox-1.5-$mode "tk_getOpenFile command" -body {
        tk_getOpenFile -parent foo.bar
    } -returnCodes error -result {bad window path name "foo.bar"}
    test filebox-1.6-$mode "tk_getOpenFile command" -body {
        tk_getOpenFile -filetypes {Foo}
    } -returnCodes error -result {bad file type "Foo", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"}

    set isNative [expr {
	[info commands ::tk::MotifFDialog] eq ""   &&
	[info commands ::tk::dialog::file::] eq ""
    }]

    set parent .

    set verylongstring longstring:
    set verylongstring $verylongstring$verylongstring
    set verylongstring $verylongstring$verylongstring
    set verylongstring $verylongstring$verylongstring
    set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring

    set color #404040
    test filebox-2.1-$mode "tk_getOpenFile command" nonUnixUserInteraction {
        ToPressButton $parent cancel
        tk_getOpenFile -title "Press Cancel ($verylongstring)" -parent $parent
    } ""

    set fileName $tmpFile
    set fileDir [tcltest::temporaryDirectory]
    set pathName [file join $fileDir $fileName]

    test filebox-2.2-$mode "tk_getOpenFile command" nonUnixUserInteraction {
        ToPressButton $parent ok
        set choice [tk_getOpenFile -title "Press Ok" \
		-parent $parent -initialfile $fileName -initialdir $fileDir]
    } $pathName
    test filebox-2.3-$mode "tk_getOpenFile command" nonUnixUserInteraction {
        ToEnterFileByKey $parent $fileName $fileDir
        set choice [tk_getOpenFile -title "Enter \"$fileName\" and press Ok" \
		-parent $parent -initialdir $fileDir]
    } $pathName
    test filebox-2.4-$mode "tk_getOpenFile command" nonUnixUserInteraction {
        cd $fileDir
        ToPressButton $parent ok
        set choice [tk_getOpenFile -title "Enter \"$fileName\" and press Ok" \
		-parent $parent -initialdir . -initialfile $fileName]
    } $pathName
    test filebox-2.5-$mode "tk_getOpenFile command" nonUnixUserInteraction {
        ToPressButton $parent ok
        set choice [tk_getOpenFile -title "Enter \"$fileName\" and press Ok" \
		-parent $parent -initialdir /badpath -initialfile $fileName]
    } $pathName
    test filebox-2.6-$mode "tk_getOpenFile command" -setup {
        toplevel .t1; toplevel .t2
        wm geometry .t1 +0+0
        wm geometry .t2 +0+0
    } -constraints nonUnixUserInteraction -body {
        set choice {}
        ToPressButton .t1 ok
        lappend choice [tk_getOpenFile \
		-title "Enter \"$fileName\" and press Ok" \
		-parent .t1 -initialdir $fileDir \
		-initialfile $fileName]
        ToPressButton .t2 ok
        lappend choice [tk_getOpenFile \
		-title "Enter \"$fileName\" and press Ok" \
		-parent .t2 -initialdir $fileDir \
		-initialfile $fileName]
        ToPressButton .t1 ok
        lappend choice [tk_getOpenFile \
		-title "Enter \"$fileName\" and press Ok" \
		-parent .t1 -initialdir $fileDir \
		-initialfile $fileName]
    } -result [list $pathName $pathName $pathName] -cleanup {
        destroy .t1
        destroy .t2
    }

    foreach x [lsort -integer [array names filters]] {
        test filebox-3.$x-$mode "tk_getOpenFile command" nonUnixUserInteraction {
	    ToPressButton $parent ok
	    set choice [tk_getOpenFile -title "Press Ok" \
		    -filetypes $filters($x) -parent $parent \
		    -initialfile $fileName -initialdir $fileDir]
        } $pathName
    }
    foreach {x res} [list 1 "-unset-" 2 "Text files"] {
	set t [expr {$x + [llength [array names filters]]}]
        test filebox-3.$t-$mode "tk_getOpenFile command" nonUnixUserInteraction {
	    catch {unset tv}
	    catch {unset typeName}
	    ToPressButton $parent ok
	    if {[info exists tv]} {
	    } else {
	    }
	    set choice [tk_getOpenFile -title "Press Ok" \
		    -filetypes $filters($x) -parent $parent \
		    -initialfile $fileName -initialdir $fileDir \
		    -typevariable tv]
	    if {[info exists tv]} {
		set typeName $tv
	    } else {
		set typeName "-unset-"
	    }
	    set typeName
        } $res
    }

    test filebox-4.1-$mode "tk_getSaveFile command" -body {
	tk_getSaveFile -foo
    } -returnCodes error -result $unknownOptionsMsg(tk_getSaveFile)

    catch {tk_getSaveFile -foo 1} msg
    regsub -all ,      $msg "" options
    regsub \"-foo\" $options "" options

    foreach option $options {
	if {[string index $option 0] eq "-"} {
	    test filebox-4.2-$mode$option "tk_getSaveFile command" -body {
		tk_getSaveFile $option
	    } -returnCodes error -result "value for \"$option\" missing"
	}
    }

    test filebox-4.3-$mode "tk_getSaveFile command" -body {
	tk_getSaveFile -foo bar
    } -returnCodes error -result $unknownOptionsMsg(tk_getSaveFile)
    test filebox-4.4-$mode "tk_getSaveFile command" -body {
	tk_getSaveFile -initialdir
    } -returnCodes error -result {value for "-initialdir" missing}
    test filebox-4.5-$mode "tk_getSaveFile command" -body {
	tk_getSaveFile -parent foo.bar
    } -returnCodes error -result {bad window path name "foo.bar"}
    test filebox-4.6-$mode "tk_getSaveFile command" -body {
	tk_getSaveFile -filetypes {Foo}
    } -returnCodes error -result {bad file type "Foo", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"}

    set isNative [expr {
	[info commands ::tk::MotifFDialog] eq "" &&
	[info commands ::tk::dialog::file::] eq ""
    }]

    set parent .

    set verylongstring longstring:
    set verylongstring $verylongstring$verylongstring
    set verylongstring $verylongstring$verylongstring
    set verylongstring $verylongstring$verylongstring
    set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring
    # set verylongstring $verylongstring$verylongstring

    set color #404040
    test filebox-5.1-$mode "tk_getSaveFile command" nonUnixUserInteraction {
	ToPressButton $parent cancel
	tk_getSaveFile -title "Press Cancel ($verylongstring)" -parent $parent
    } ""

    set fileName "12x 455"
    set fileDir [pwd]
    set pathName [file join [pwd] $fileName]

    test filebox-5.2-$mode "tk_getSaveFile command" nonUnixUserInteraction {
	ToPressButton $parent ok
	set choice [tk_getSaveFile -title "Press Ok" \
		-parent $parent -initialfile $fileName -initialdir $fileDir]
    } $pathName
    test filebox-5.3-$mode "tk_getSaveFile command" nonUnixUserInteraction {
	ToEnterFileByKey $parent $fileName $fileDir
	set choice [tk_getSaveFile -title "Enter \"$fileName\" and press Ok" \
		-parent $parent -initialdir $fileDir]
    } $pathName
    test filebox-5.4-$mode "tk_getSaveFile command" nonUnixUserInteraction {
	ToPressButton $parent ok
	set choice [tk_getSaveFile -title "Enter \"$fileName\" and press Ok" \
		-parent $parent -initialdir . -initialfile $fileName]
    } $pathName
    test filebox-5.5-$mode "tk_getSaveFile command" nonUnixUserInteraction {
	ToPressButton $parent ok
	set choice [tk_getSaveFile -title "Enter \"$fileName\" and press Ok" \
		-parent $parent -initialdir /badpath -initialfile $fileName]
    } $pathName

    test filebox-5.6-$mode "tk_getSaveFile command" -setup {
	toplevel .t1; toplevel .t2
	wm geometry .t1 +0+0
	wm geometry .t2 +0+0
    } -constraints nonUnixUserInteraction -body {
	set choice {}
	ToPressButton .t1 ok
	lappend choice [tk_getSaveFile \
		-title "Enter \"$fileName\" and press Ok" \
		-parent .t1 -initialdir $fileDir -initialfile $fileName]
	ToPressButton .t2 ok
	lappend choice [tk_getSaveFile \
		-title "Enter \"$fileName\" and press Ok" \
		-parent .t2 -initialdir $fileDir -initialfile $fileName]
	ToPressButton .t1 ok
	lappend choice [tk_getSaveFile \
		-title "Enter \"$fileName\" and press Ok" \
		-parent .t1 -initialdir $fileDir -initialfile $fileName]
    } -result [list $pathName $pathName $pathName] -cleanup {
	destroy .t1
	destroy .t2
    }

    foreach x [lsort -integer [array names filters]] {
	test filebox-6.$x-$mode "tk_getSaveFile command" nonUnixUserInteraction {
	    ToPressButton $parent ok
	    set choice [tk_getSaveFile -title "Press Ok" \
		    -filetypes $filters($x) -parent $parent \
		    -initialfile $fileName -initialdir $fileDir]
	} $pathName[lindex $addedExtensions $x]
    }

    if {!$mode} {

	test filebox-7.1-$mode "tk_getOpenFile - directory not readable" \
	    -constraints nonUnixUserInteraction \
	    -setup {
		rename ::tk_messageBox ::saved_messageBox
		set ::gotmessage {}
		proc tk_messageBox args {
		    set ::gotmessage $args
		}
		toplevel .t1
		file mkdir [file join $fileDir NOTREADABLE]
		file attributes [file join $fileDir NOTREADABLE] \
		    -permissions 300
	    } \
	    -cleanup {
		rename ::tk_messageBox {}
		rename ::saved_messageBox ::tk_messageBox
		unset ::gotmessage
		destroy .t1
		file delete -force [file join $fileDir NOTREADABLE]
	    } \
	    -body {
		ToEnterFileByKey .t1 NOTREADABLE $fileDir
		ToPressButton .t1 ok
		ToPressButton .t1 cancel
		tk_getOpenFile -parent .t1 \
		    -title "Please select the NOTREADABLE directory" \
		    -initialdir $fileDir
		set gotmessage
	    } \
	    -match glob \
	    -result "*NOTREADABLE*"

	test filebox-7.2-$mode "tk_getOpenFile - bad file name" \
	    -constraints nonUnixUserInteraction \
	    -setup {
		rename ::tk_messageBox ::saved_messageBox
		set ::gotmessage {}
		proc tk_messageBox args {
		    set ::gotmessage $args
		}
		toplevel .t1
	    } \
	    -cleanup {
		rename ::tk_messageBox {}
		rename ::saved_messageBox ::tk_messageBox
		unset ::gotmessage
		destroy .t1
	    } \
	    -body {
		ToEnterFileByKey .t1 RUBBISH $fileDir
		ToPressButton .t1 ok
		ToPressButton .t1 cancel
		tk_getOpenFile -parent .t1 \
		    -title "Please enter RUBBISH as a file name" \
		    -initialdir $fileDir
		set gotmessage
	    } \
	    -match glob \
	    -result "*RUBBISH*"
    }

    # The rest of the tests need to be executed on Unix only.
    # They test whether the dialog box widgets were implemented correctly.
    # These tests are not
    # needed on the other platforms because they use native file dialogs.
}

set tk_strictMotif $tk_strictMotif_old

# cleanup
removeFile filebox.tmp
cleanupTests
return