forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.EditField.lua
More file actions
83 lines (68 loc) · 2.73 KB
/
Copy pathwidgets.EditField.lua
File metadata and controls
83 lines (68 loc) · 2.73 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
config.target = 'core'
local widgets = require('gui.widgets')
function test.editfield_cursor()
local e = widgets.EditField{}
-- cursor is normally set in `postUpdateLayout`, hard to test in unit tests
e:setCursor(1)
e:setFocus(true)
expect.eq(1, e.cursor, 'cursor should be after the empty string')
e:onInput{_STRING=string.byte('a')}
expect.eq('a', e.text)
expect.eq(2, e.cursor)
e:setText('one two three')
expect.eq(14, e.cursor, 'cursor should be after the last char')
e:onInput{_STRING=string.byte('s')}
expect.eq('one two threes', e.text)
expect.eq(15, e.cursor)
e:setCursor(4)
e:onInput{_STRING=string.byte('s')}
expect.eq('ones two threes', e.text)
expect.eq(5, e.cursor)
e:onInput{KEYBOARD_CURSOR_LEFT=true}
expect.eq(4, e.cursor)
e:onInput{KEYBOARD_CURSOR_RIGHT=true}
expect.eq(5, e.cursor)
e:onInput{CUSTOM_HOME=true}
expect.eq(1, e.cursor, 'cursor should be at beginning of string')
e:onInput{CUSTOM_CTRL_RIGHT=true}
expect.eq(5, e.cursor, 'goto end of current word')
e:onInput{CUSTOM_END=true}
expect.eq(16, e.cursor, 'cursor should be at end of string')
e:onInput{CUSTOM_CTRL_LEFT=true}
expect.eq(10, e.cursor, 'goto beginning of current word')
end
function test.editfield_click()
local e = widgets.EditField{text='word'}
e:setFocus(true)
expect.eq(5, e.cursor)
local text_area_content = e.text_area.text_area
mock.patch(text_area_content, 'getMousePos', mock.func(0, 0), function()
e:onInput{_MOUSE_L_DOWN=true, _MOUSE_L=true}
e:onInput{_MOUSE_L_DOWN=true}
expect.eq(1, e.cursor)
end)
mock.patch(text_area_content, 'getMousePos', mock.func(20, 0), function()
e:onInput{_MOUSE_L_DOWN=true, _MOUSE_L=true}
e:onInput{_MOUSE_L_DOWN=true}
expect.eq(5, e.cursor, 'should only seek to end of text')
end)
mock.patch(text_area_content, 'getMousePos', mock.func(2, 0), function()
e:onInput{_MOUSE_L_DOWN=true, _MOUSE_L=true}
e:onInput{_MOUSE_L_DOWN=true}
expect.eq(3, e.cursor)
end)
end
function test.editfield_ignore_keys()
local e = widgets.EditField{ignore_keys={'CUSTOM_B', 'CUSTOM_C'}}
-- cursor is normally set in `postUpdateLayout`, hard to test in unit tests
e:setCursor(1)
e:setFocus(true)
e:onInput{_STRING=string.byte('a'), CUSTOM_A=true}
expect.eq('a', e.text, '"a" should be accepted')
e:onInput{_STRING=string.byte('b'), CUSTOM_B=true}
expect.eq('a', e.text, '"b" should be rejected')
e:onInput{_STRING=string.byte('c'), CUSTOM_C=true}
expect.eq('a', e.text, '"c" should be rejected')
e:onInput{_STRING=string.byte('d'), CUSTOM_D=true}
expect.eq('ad', e.text, '"d" should be accepted')
end