forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.lua
More file actions
261 lines (219 loc) · 8.16 KB
/
Copy pathdialogs.lua
File metadata and controls
261 lines (219 loc) · 8.16 KB
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
local gui = require('gui')
local function send_keys(...)
local keys = {...}
for _,key in ipairs(keys) do
gui.simulateInput(dfhack.gui.getCurViewscreen(true), key)
end
end
local xtest = {} -- use to temporarily disable tests (change `function test.somename` to `function xtest.somename`)
local wait = function(n)
--delay(n or 30) -- enable for debugging the tests
end
local dialogs = require('gui.dialogs')
function test.ListBox_opens_and_closes()
local before_scr = dfhack.gui.getCurViewscreen(true)
local choices = {{
text = 'ListBox_opens_and_closes'
}}
local lb = dialogs.ListBox({choices = choices})
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "creating a ListBox object should not change CurViewscreen")
lb:show()
wait()
expect.ne(before_scr, dfhack.gui.getCurViewscreen(true), "ListBox:show should change CurViewscreen")
send_keys('LEAVESCREEN')
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Pressing LEAVESCREEN should return us to previous screen")
end
function test.ListBox_closes_on_select()
local before_scr = dfhack.gui.getCurViewscreen(true)
local args = {}
local mock_cb = mock.func()
local mock_cb2 = mock.func()
args.on_select = mock_cb
args.on_select2 = mock_cb2
args.choices = {{
text = 'ListBox_closes_on_select'
}}
local lb = dialogs.ListBox(args)
lb:show()
wait()
send_keys('SELECT')
expect.eq(1, mock_cb.call_count)
expect.eq(0, mock_cb2.call_count)
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Selecting an item should return us to previous screen")
end
function test.ListBox_closes_on_select2()
local before_scr = dfhack.gui.getCurViewscreen(true)
local args = {}
local mock_cb = mock.func()
local mock_cb2 = mock.func()
args.on_select = mock_cb
args.on_select2 = mock_cb2
args.choices = {{
text = 'ListBox_closes_on_select2'
}}
local lb = dialogs.ListBox(args)
lb:show()
wait()
send_keys('SEC_SELECT')
expect.eq(0, mock_cb.call_count)
expect.eq(1, mock_cb2.call_count)
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Selecting an item should return us to previous screen")
end
function test.ListBox_stays_open_with_multi_select()
local before_scr = dfhack.gui.getCurViewscreen(true)
local args = {}
local mock_cb = mock.func()
local mock_cb2 = mock.func()
args.on_select = mock_cb
args.on_select2 = mock_cb2
args.dismiss_on_select = false
args.choices = {{
text = 'ListBox_stays_open_with_multi_select'
}}
local lb = dialogs.ListBox(args)
lb:show()
local lb_scr = dfhack.gui.getCurViewscreen(true)
wait()
send_keys('SELECT')
expect.eq(lb_scr, dfhack.gui.getCurViewscreen(true), "Selecting an item should NOT close the ListBox")
send_keys('SEC_SELECT')
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "With default dismiss_on_select2 it should return us to previous screen")
expect.eq(1, mock_cb.call_count)
expect.eq(1, mock_cb2.call_count)
end
function test.ListBox_stays_open_with_multi_select2()
local before_scr = dfhack.gui.getCurViewscreen(true)
local args = {}
local mock_cb = mock.func()
local mock_cb2 = mock.func()
args.on_select = mock_cb
args.on_select2 = mock_cb2
args.dismiss_on_select2 = false
args.choices = {{
text = 'ListBox_stays_open_with_multi_select2'
}}
local lb = dialogs.ListBox(args)
lb:show()
local lb_scr = dfhack.gui.getCurViewscreen(true)
wait()
send_keys('SEC_SELECT')
expect.eq(lb_scr, dfhack.gui.getCurViewscreen(true), "Sec-selecting an item should NOT close the ListBox")
send_keys('SELECT')
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "With default dismiss_on_select it should return us to previous screen")
expect.eq(1, mock_cb.call_count)
expect.eq(1, mock_cb2.call_count)
end
function test.ListBox_with_multi_select()
local before_scr = dfhack.gui.getCurViewscreen(true)
local args = {}
local mock_cb = mock.func()
local mock_cb2 = mock.func()
args.on_select = mock_cb
args.on_select2 = mock_cb2
args.dismiss_on_select = false
args.dismiss_on_select2 = false
args.choices = {{
text = 'ListBox_with_multi_select'
},{
text = 'item2'
},{
text = 'item3'
}
}
local lb = dialogs.ListBox(args)
lb:show()
local lb_scr = dfhack.gui.getCurViewscreen(true)
wait()
send_keys('SELECT')
send_keys('STANDARDSCROLL_DOWN')
wait()
send_keys('SEC_SELECT')
send_keys('STANDARDSCROLL_DOWN')
wait()
send_keys('SELECT')
expect.eq(2, mock_cb.call_count)
expect.eq(1, mock_cb2.call_count)
expect.eq(lb_scr, dfhack.gui.getCurViewscreen(true), "With both dismiss_on_select and dismiss_on_select2 false the ListBox should stay open")
send_keys('LEAVESCREEN')
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Pressing LEAVESCREEN should still return us to previous screen")
end
-- this test also demonstrates actual (minimal) example usage
function test.ListBox_with_multi_select_and_visual_indicator()
local before_scr = dfhack.gui.getCurViewscreen(true)
-- configure colors
local pen_active = COLOR_LIGHTCYAN
local dpen_active = COLOR_CYAN
local pen_not_active = COLOR_LIGHTRED
local dpen_not_active = COLOR_RED
local pen_cb = function(args, fnc)
if not (args and fnc) then return COLOR_YELLOW end
return fnc(args) and pen_active or pen_not_active
end
local pen_d_cb = function(args, fnc)
if not (args and fnc) then return COLOR_YELLOW end
return fnc(args) and dpen_active or dpen_not_active
end
local args = {}
local choices = {}
args.choices = choices
local mock_cb = mock.func()
local mock_cb2 = mock.func()
args.on_select = function(ix, choice)
mock_cb()
local item = choice.item
item.active = not item.active
end
args.on_select2 = mock_cb2
args.dismiss_on_select = false
local mock_is_active_cb_counter = mock.func()
local is_active = function (args)
mock_is_active_cb_counter()
return args.item.active
end
local items = {
{text = 'ListBox_with_multi_select', active = true},
{text = 'and_visual_indicator', active = true},
{text = 'item3', active = false}
}
for _, item in pairs(items) do
local args = {item=item}
table.insert(choices, {
text = {{text = item.text,
pen = curry(pen_cb, args, is_active),
dpen = curry(pen_d_cb, args, is_active),
}},
item = item
})
end
local lb = dialogs.ListBox(args)
lb:show()
local lb_scr = dfhack.gui.getCurViewscreen(true)
expect.eq(pen_active, choices[1].text[1].pen(), "Pen of the first item should be the pen_active")
expect.eq(pen_not_active, choices[3].text[1].pen(), "Pen of the third item should be the pen_not_active")
wait()
send_keys('SELECT')
send_keys('STANDARDSCROLL_DOWN')
wait()
send_keys('SELECT')
send_keys('STANDARDSCROLL_DOWN')
wait()
send_keys('SELECT')
wait(100)
expect.eq(3, mock_cb.call_count)
expect.eq(0, mock_cb2.call_count)
expect.lt(0, mock_is_active_cb_counter.call_count, "is_active should be called at least once")
expect.table_eq(
{
{text = 'ListBox_with_multi_select', active = false},
{text = 'and_visual_indicator', active = false},
{text = 'item3', active = true}
},
items,
"the active status should've been toggled"
)
expect.eq(pen_not_active, choices[1].text[1].pen(), "Pen of the first now not active item should be the pen_not_active")
expect.eq(pen_active, choices[3].text[1].pen(), "Pen of the third now active item should be the pen_active")
expect.eq(lb_scr, dfhack.gui.getCurViewscreen(true), "With both dismiss_on_select and dismiss_on_select2 false the ListBox should stay open")
send_keys('LEAVESCREEN')
expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Pressing LEAVESCREEN should still return us to previous screen")
end