test_autotextctrl.py 6.29 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/env python
#
# test_autotextctrl.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#


9
from . import run_with_wx, simclick, simtext, simkey, realYield, addall
10
11
12
13
14
15
16
17
18
19
20

import wx
import fsleyes_widgets.autotextctrl as autott


def sendEvent(target, evType, source=None):

    if source is None:
        source = target

    target.ProcessEvent(wx.CommandEvent(evType, source.GetId()))
21
    realYield()
22
23
24

# Simple test - programmatically
# set, then retrieve the value
25
26
27
def test_getSet():
    run_with_wx( _test_getSet)
def _test_getSet():
28
29

    parent = wx.GetApp().GetTopWindow()
30
    atc = autott.AutoTextCtrl(parent, modal=False)
31
32
33
34
35
36
37
38

    atc.ChangeValue('a')
    assert atc.GetValue() == 'a'

    atc.SetValue('b')
    assert atc.GetValue() == 'b'


39
40
41
def test_event():
    run_with_wx( _test_event)
def _test_event():
Paul McCarthy's avatar
Paul McCarthy committed
42
43
44
45
46
47
48
49

    called = [None]

    def handler(ev):
        called[0] = ev.text

    sim    = wx.UIActionSimulator()
    parent = wx.GetApp().GetTopWindow()
50
    atc    = autott.AutoTextCtrl(parent, modal=False)
Paul McCarthy's avatar
Paul McCarthy committed
51
52

    atc.Bind(autott.EVT_ATC_TEXT_ENTER, handler)
53
54
55
56

    addall(parent, [atc])

    simtext(sim, atc.textCtrl, 'abc')
Paul McCarthy's avatar
Paul McCarthy committed
57
58
59
60
61

    assert atc.GetValue() == 'abc'
    assert called[0]      == 'abc'


62
63
# Make sure that when the cotrol receives
# focus, its insertion point is at the end
64
65
66
def test_onFocus():
    run_with_wx(_test_onFocus)
def _test_onFocus():
67
68

    parent = wx.GetApp().GetTopWindow()
69
    atc = autott.AutoTextCtrl(parent, modal=False)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])

    atc.ChangeValue('a')
    atc.SetInsertionPoint(0)

    sendEvent(atc, wx.wxEVT_KILL_FOCUS)

    assert atc.GetInsertionPoint() == 0

    sendEvent(atc, wx.wxEVT_SET_FOCUS)
    assert atc.GetInsertionPoint() == 1


# Test showing the popup and selecting a value
85
86
87
def test_popup_select1():
    run_with_wx(_test_popup_select1)
def _test_popup_select1():
88
89
90
91

    sim = wx.UIActionSimulator()

    parent = wx.GetApp().GetTopWindow()
92
    atc = autott.AutoTextCtrl(parent, modal=False)
93

94
95
    addall(parent, [atc])

96
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
97
98
99
100
101

    simkey(sim, atc.textCtrl,       wx.WXK_RETURN)
    simkey(sim, atc.popup.textCtrl, wx.WXK_DOWN)
    atc.popup.listBox.SetSelection(1)
    simkey(sim, atc.popup.listBox,  wx.WXK_RETURN)
102
103
104
105

    assert atc.GetValue() == 'aab'


106
107
108
def test_popup_select2():
    run_with_wx(_test_popup_select2)
def _test_popup_select2():
109
110
111
112

    sim = wx.UIActionSimulator()

    parent = wx.GetApp().GetTopWindow()
113
    atc = autott.AutoTextCtrl(parent, modal=False)
114

115
    addall(parent, [atc])
116

117
118
119
120
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
    simtext(sim, atc.textCtrl, 'b', enter=False)
    simkey( sim, atc.popup.textCtrl, wx.WXK_DOWN)
    simkey( sim, atc.popup.listBox, wx.WXK_RETURN)
121
122
123
124

    assert atc.GetValue() == 'bcc'


125
126
127
def test_popup_select3():
    run_with_wx(_test_popup_select3)
def _test_popup_select3():
128
129
130

    sim = wx.UIActionSimulator()
    parent = wx.GetApp().GetTopWindow()
131
    atc = autott.AutoTextCtrl(parent, modal=False)
132

133
134
    addall(parent, [atc])

135
136
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])

137
138
139
140
    simtext(sim, atc, 'ab', enter=False)

    simkey(sim, atc.popup.textCtrl, wx.WXK_DOWN)
    simkey(sim, atc.popup.listBox,  wx.WXK_RETURN)
141
142
143
144

    assert atc.GetValue() == 'aba'


145
146
147
def test_popup_cancel():
    run_with_wx(_test_popup_cancel)
def _test_popup_cancel():
148
149
150
151

    sim = wx.UIActionSimulator()

    parent = wx.GetApp().GetTopWindow()
152
    atc = autott.AutoTextCtrl(parent, modal=False)
153

154
155
    addall(parent, [atc])

156
157
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])

158
159
160
161
    simkey(sim, atc.textCtrl,       wx.WXK_RETURN)
    simkey(sim, atc.popup.textCtrl, wx.WXK_DOWN)
    simkey(sim, atc.popup.listBox,  wx.WXK_DOWN)
    simkey(sim, atc.popup.listBox,  wx.WXK_ESCAPE)
162
163
164
165

    assert atc.GetValue() == ''


166
167
168
def test_popup_focusback():
    run_with_wx(_test_popup_focusback)
def _test_popup_focusback():
169
170
171
172

    sim = wx.UIActionSimulator()

    parent = wx.GetApp().GetTopWindow()
173
    atc = autott.AutoTextCtrl(parent, modal=False)
174

175
    addall(parent, [atc])
176

177
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
178

179
180
181
182
    simkey(sim,  atc.textCtrl, wx.WXK_RETURN)
    simkey(sim,  atc.popup.textCtrl, wx.WXK_DOWN)
    simkey(sim,  atc.popup.listBox,  wx.WXK_UP)
    simtext(sim, atc.popup.textCtrl, 'abc')
183
184

    assert atc.GetValue() == 'abc'
185
186
187
188
189


def test_popup_dblclick():
    run_with_wx(_test_popup_dblclick)
def _test_popup_dblclick():
190
191

    class FakeEv:
192
193
194
195
        def __init__(self, keycode=None):
            self.keycode = keycode
        def GetKeyCode(self):
            return self.keycode
196
197
        def Skip(self):
            pass
198
199
        def ResumePropagation(self, a):
            pass
200

201
    parent = wx.GetApp().GetTopWindow()
202
    atc    = autott.AutoTextCtrl(parent, modal=False)
203
204
205

    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])

206
    addall(parent, [atc])
207

208
    atc._AutoTextCtrl__onKeyDown(FakeEv(wx.WXK_RETURN))
209
210
211
    atc.popup.listBox.SetSelection(0)
    atc.popup._AutoCompletePopup__onListMouseDblClick(FakeEv())
    realYield()
212
213

    assert atc.GetValue() == 'aaa'
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


def test_popup_propagate_enter():
    run_with_wx(_test_popup_propagate_enter)
def _test_popup_propagate_enter():

    sim    = wx.UIActionSimulator()
    parent = wx.GetApp().GetTopWindow()
    atc    = autott.AutoTextCtrl(parent, modal=False)

    called = [False]
    def atcEnter(ev):
        called[0] = True

    addall(parent, [atc])

    atc.Bind(autott.EVT_ATC_TEXT_ENTER, atcEnter)
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])

    simtext(sim, atc, 'ab', enter=False)

    simkey(sim, atc.popup.textCtrl, wx.WXK_DOWN)
    simkey(sim, atc.popup.listBox,  wx.WXK_RETURN)

    assert atc.GetValue() == 'aba'
    assert called[0]


def test_popup_no_propagate_enter():
    run_with_wx(_test_popup_no_propagate_enter)
def _test_popup_no_propagate_enter():

    sim = wx.UIActionSimulator()
    parent = wx.GetApp().GetTopWindow()
    atc = autott.AutoTextCtrl(parent,
                              modal=False,
                              style=autott.ATC_NO_PROPAGATE_ENTER)

    called = [False]
    def atcEnter(ev):
        called[0] = True


    addall(parent, [atc])

    atc.Bind(autott.EVT_ATC_TEXT_ENTER, atcEnter)
    atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])

    simtext(sim, atc, 'ab', enter=False)

    simkey(sim, atc.popup.textCtrl, wx.WXK_DOWN)
    simkey(sim, atc.popup.listBox,  wx.WXK_RETURN)

    assert atc.GetValue() == 'aba'
    assert not called[0]