File: test_uiaction.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (200 lines) | stat: -rw-r--r-- 7,044 bytes parent folder | download | duplicates (2)
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
import unittest
from unittests import wtc
import wx
import sys, os

WAIT=50

#---------------------------------------------------------------------------

class MouseEventsPanel(wx.Panel):
    def __init__(self, parent, eventBinders):
        wx.Panel.__init__(self, parent, size=parent.GetClientSize())
        self.SetBackgroundColour('pink')
        self.events = list()
        if not isinstance(eventBinders, (list, tuple)):
            eventBinders = [eventBinders]
        for binder in eventBinders:
            self.Bind(binder, self.onMouseEvent)

    def onMouseEvent(self, evt):
        self.events.append( (evt.EventType, evt.Position) )
        print( (evt.EventType, evt.Position) )
        evt.Skip()



class uiaction_MouseTests(wtc.WidgetTestCase):


    def cmp(self, info, evtType, pos):
        if isinstance(evtType, tuple):
            if info[0] not in evtType:
                return False
        else:
            if info[0] != evtType:
                return False
        # TODO: The mouse pos may be off by 1 on MSW, is this expected?
        if abs(info[1].x - pos[0]) > 1:
            return False
        if abs(info[1].y - pos[1]) > 1:
            return False
        return True

    def setUp(self):
        super(uiaction_MouseTests, self).setUp()
        self.frame.WarpPointer(-10,-10)


    def test_uiactionMouseMotion(self):
        p = MouseEventsPanel(self.frame, wx.EVT_MOTION)
        self.waitFor(WAIT)

        uia = wx.UIActionSimulator()
        uia.MouseMove(p.ClientToScreen((1,1)));   self.waitFor(WAIT)
        uia.MouseMove(p.ClientToScreen((5,5)));   self.waitFor(WAIT)
        uia.MouseMove(p.ClientToScreen((10,10)).x, p.ClientToScreen((10,10)).y)
        self.waitFor(WAIT)
        self.waitFor(WAIT)

        self.assertEqual(len(p.events), 3)
        self.assertTrue(self.cmp(p.events[0], wx.wxEVT_MOTION, (1,1)))
        self.assertTrue(self.cmp(p.events[1], wx.wxEVT_MOTION, (5,5)))
        self.assertTrue(self.cmp(p.events[2], wx.wxEVT_MOTION, (10,10)))


    def test_uiactionMouseLeftDownUp(self):
        p = MouseEventsPanel(self.frame, [wx.EVT_LEFT_DOWN, wx.EVT_LEFT_UP])
        self.waitFor(WAIT)

        uia = wx.UIActionSimulator()
        uia.MouseMove(p.ClientToScreen((10,10)));   self.waitFor(WAIT)
        uia.MouseDown();                            self.waitFor(WAIT)
        uia.MouseUp();                              self.waitFor(WAIT)
        self.waitFor(WAIT)

        self.assertTrue(len(p.events) == 2)
        self.assertTrue(self.cmp(p.events[0], wx.wxEVT_LEFT_DOWN, (10,10)))
        self.assertTrue(self.cmp(p.events[1], wx.wxEVT_LEFT_UP, (10,10)))


    def test_uiactionMouseRightDownUp(self):
        p = MouseEventsPanel(self.frame, [wx.EVT_RIGHT_DOWN, wx.EVT_RIGHT_UP])
        self.waitFor(WAIT)

        uia = wx.UIActionSimulator()
        uia.MouseMove(p.ClientToScreen((10,10)));  self.waitFor(WAIT)
        uia.MouseDown(wx.MOUSE_BTN_RIGHT);         self.waitFor(WAIT)
        uia.MouseUp(wx.MOUSE_BTN_RIGHT);           self.waitFor(WAIT)
        self.waitFor(WAIT)

        self.assertTrue(len(p.events) == 2)
        self.assertTrue(self.cmp(p.events[0], wx.wxEVT_RIGHT_DOWN, (10,10)))
        self.assertTrue(self.cmp(p.events[1], wx.wxEVT_RIGHT_UP, (10,10)))


    def test_uiactionMouseLeftClick(self):
        p = MouseEventsPanel(self.frame, [wx.EVT_LEFT_DOWN, wx.EVT_LEFT_UP])
        self.waitFor(WAIT)

        uia = wx.UIActionSimulator()
        uia.MouseMove(p.ClientToScreen((10,10)));  self.waitFor(WAIT)
        uia.MouseClick();                          self.waitFor(WAIT)
        self.waitFor(WAIT)

        self.assertTrue(len(p.events) == 2)
        self.assertTrue(self.cmp(p.events[0], wx.wxEVT_LEFT_DOWN, (10,10)))
        self.assertTrue(self.cmp(p.events[1], wx.wxEVT_LEFT_UP, (10,10)))


    def test_uiactionMouseLeftDClick(self):
        p = MouseEventsPanel(self.frame, [wx.EVT_LEFT_DOWN, wx.EVT_LEFT_UP, wx.EVT_LEFT_DCLICK])
        self.waitFor(WAIT)

        uia = wx.UIActionSimulator()
        uia.MouseMove(p.ClientToScreen((10,10)));  self.waitFor(WAIT)
        uia.MouseDblClick();                       self.waitFor(WAIT)
        self.waitFor(WAIT)

        self.assertTrue(len(p.events) == 4)
        self.assertTrue(self.cmp(p.events[0], wx.wxEVT_LEFT_DOWN, (10,10)))
        self.assertTrue(self.cmp(p.events[1], wx.wxEVT_LEFT_UP, (10,10)))
        self.assertTrue(self.cmp(p.events[2], (wx.wxEVT_LEFT_DOWN, wx.wxEVT_LEFT_DCLICK), (10,10)))
        self.assertTrue(self.cmp(p.events[3], wx.wxEVT_LEFT_UP, (10,10)))


    def test_uiactionMouseDD(self):
        p = MouseEventsPanel(self.frame, [wx.EVT_MOTION, wx.EVT_LEFT_DOWN, wx.EVT_LEFT_UP])
        self.waitFor(WAIT)

        x1, y1 = p.ClientToScreen((10,10))
        x2 = x1 + 20
        y2 = y1 + 20

        uia = wx.UIActionSimulator()
        uia.MouseDragDrop(x1,y1, x2,y2);  self.waitFor(WAIT)
        self.waitFor(WAIT)

        self.assertEqual(len(p.events), 4)
        self.assertTrue(self.cmp(p.events[0], wx.wxEVT_MOTION, (10,10)))
        self.assertTrue(self.cmp(p.events[1], wx.wxEVT_LEFT_DOWN, (10,10)))
        self.assertTrue(self.cmp(p.events[2], wx.wxEVT_MOTION, (30,30)))
        self.assertTrue(self.cmp(p.events[3], wx.wxEVT_LEFT_UP, (30,30)))


#---------------------------------------------------------------------------


class uiaction_KeyboardTests(wtc.WidgetTestCase):

    def setUp(self):
        super(uiaction_KeyboardTests, self).setUp()
        pnl = wx.Panel(self.frame)
        pnl.SetBackgroundColour('pink')
        self.tc = wx.TextCtrl(pnl)
        self.tc.SetFocus()
        self.waitFor(WAIT)


    def test_uiactionKeyboardKeyDownUp(self):
        uia = wx.UIActionSimulator()
        for c in "This is a test":
            if c.isupper():
                uia.KeyDown(wx.WXK_SHIFT);  self.waitFor(WAIT)
            uia.KeyDown(ord(c));            self.waitFor(WAIT)
            uia.KeyUp(ord(c));              self.waitFor(WAIT)
            if c.isupper():
                uia.KeyUp(wx.WXK_SHIFT);    self.waitFor(WAIT)
        self.waitFor(WAIT*2)

        self.assertEqual(self.tc.GetValue(), "This is a test")


    @unittest.skipIf(sys.platform == 'darwin', 'wx.UIActionSimulator.Char needs work...')
    def test_uiactionKeyboardChar(self):
        uia = wx.UIActionSimulator()
        for c in "This is a test":
            mod = wx.MOD_NONE
            if c.isupper():
                mod = wx.MOD_SHIFT
            uia.Char(ord(c), mod);
            self.waitFor(WAIT)
        self.waitFor(WAIT*2)

        self.assertEqual(self.tc.GetValue(), "This is a test")


    @unittest.skipIf(sys.platform == 'darwin', 'wx.UIActionSimulator.Text needs work...')
    def test_uiactionKeyboardText(self):
        uia = wx.UIActionSimulator()
        uia.Text(b"This is a test")
        self.waitFor(WAIT*2)

        self.assertEqual(self.tc.GetValue(), "This is a test")


#---------------------------------------------------------------------------


if __name__ == '__main__':
    unittest.main()