summaryrefslogtreecommitdiffstats
path: root/tests/choosedir.test
blob: f1147894bdfbc1188c177c26032688f20c70a5fe (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
# This file is a Tcl script to test out Tk's "tk_chooseDir".
#
# Copyright © 1996 Sun Microsystems, Inc.
# Copyright © 1998-1999 Scriptics Corporation.
# All rights reserved.

#
# TESTFILE INITIALIZATION
#

package require tcltest 2.2; # needed in mode -singleproc 0

# Load the main script main.tcl, which takes care of:
# - setup for the application and the root window
# - importing commands from the tcltest namespace
# - loading of the testutils mechanism along with its utility procs
# - loading of Tk specific test constraints (additionally to constraints
#   provided by the package tcltest)
source [file join [tcltest::configure -testdir] main.tcl]

# Ensure a pristine initial window state
resetWindows

# Import utility procs for specific functional areas
testutils import dialog

#
# LOCAL UTILITY PROCS
#

proc EnterDirsByKey {parent dirs} {
    if {$parent == "."} {
	set w .__tk_choosedir
    } else {
	set w $parent.__tk_choosedir
    }
    upvar ::tk::dialog::file::__tk_choosedir data

    foreach dir $dirs {
	$data(ent) delete 0 end
	$data(ent) insert 0 $dir
	update
	SendButtonPress $parent ok mouse
	after 50
    }
}

proc ToEnterDirsByKey {parent dirs} {
    after 100 [list EnterDirsByKey $parent $dirs]
}

#
# COMMON TEST SETUP
#
set parent .

# Make a dir for us to rely on for tests
set real [makeDirectory choosedirTest]
set fake [file join [file dirname $real] non-existant]

#
# TESTS
#

test choosedir-1.1 {tk_chooseDirectory command} -body {
    tk_chooseDirectory -initialdir
} -returnCodes error -result {value for "-initialdir" missing}
test choosedir-1.2 {tk_chooseDirectory command} -body {
    tk_chooseDirectory -mustexist
} -returnCodes error -result {value for "-mustexist" missing}
test choosedir-1.3 {tk_chooseDirectory command} -body {
    tk_chooseDirectory -parent
} -returnCodes error -result {value for "-parent" missing}
test choosedir-1.4 {tk_chooseDirectory command} -body {
    tk_chooseDirectory -title
} -returnCodes error -result {value for "-title" missing}
test choosedir-1.5.1 {tk_chooseDirectory command} -constraints notAqua -body {
    tk_chooseDirectory -foo bar
} -returnCodes error -result {bad option "-foo": must be -initialdir, -mustexist, -parent, or -title}
test choosedir-1.5.2 {tk_chooseDirectory command} -constraints aqua -body {
    tk_chooseDirectory -foo bar
} -returnCodes error -result {bad option "-foo": must be -command, -initialdir, -message, -mustexist, -parent, or -title}
test choosedir-1.6 {tk_chooseDirectory command} -body {
    tk_chooseDirectory -parent foo.bar
} -returnCodes error -result {bad window path name "foo.bar"}


test choosedir-2.1 {tk_chooseDirectory command, cancel gives null} -constraints {
	unix notAqua
} -body {
    ToPressButton $parent cancel
    tk_chooseDirectory -title "Press Cancel" -parent $parent
} -result {}


test choosedir-3.1 {tk_chooseDirectory -mustexist 1} -constraints {
	unix notAqua
} -body {
    # first enter a bogus dirname, then enter a real one.
    ToEnterDirsByKey $parent [list $fake $real $real]
    set result [tk_chooseDirectory \
	    -title "Enter \"$fake\", press OK, enter \"$real\", press OK" \
	    -parent $parent -mustexist 1]
    set result
} -result $real
test choosedir-3.2 {tk_chooseDirectory -mustexist 0} -constraints {
	unix notAqua
} -body {
    ToEnterDirsByKey $parent [list $fake $fake]
    tk_chooseDirectory -title "Enter \"$fake\", press OK" \
	    -parent $parent -mustexist 0
} -result $fake


test choosedir-4.1 {tk_chooseDirectory command, initialdir} -constraints {
	unix notAqua
} -body {
    ToPressButton $parent ok
    tk_chooseDirectory -title "Press Ok" -parent $parent -initialdir $real
} -result $real
test choosedir-4.2 {tk_chooseDirectory command, initialdir} -constraints {
	unix notAqua
} -body {
    ToEnterDirsByKey $parent [list $fake $fake]
    tk_chooseDirectory \
	    -title "Enter \"$fake\" and press Ok" \
	    -parent $parent -initialdir $real
} -result $fake
test choosedir-4.3 {tk_chooseDirectory command, {} initialdir} -constraints {
	unix notAqua
} -body {
    catch {unset ::tk::dialog::file::__tk_choosedir}
    ToPressButton $parent ok
    tk_chooseDirectory \
	    -title "Press OK" \
	    -parent $parent -initialdir ""
} -result [pwd]


test choosedir-5.1 {tk_chooseDirectory, handles {} entry text} -constraints {
	unix notAqua
} -body {
    ToEnterDirsByKey $parent [list "" $real $real]
    tk_chooseDirectory -title "Clear entry, Press OK; Enter $real, press OK" \
	    -parent $parent
} -result $real

#
# TESTFILE CLEANUP
#

unset fake parent real
removeDirectory choosedirTest
testutils forget dialog
cleanupTests