File: test_pymsgbox.py

package info (click to toggle)
pymsgbox 1.0.9-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 196 kB
  • sloc: python: 635; makefile: 147
file content (334 lines) | stat: -rw-r--r-- 13,499 bytes parent folder | download
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import unittest
import sys
import os
import time
import threading
import inspect

import pymsgbox

# Note: Yes, PyAutoGUI does have PyMsgBox itself as a dependency, but we won't be using that part of PyAutoGUI for this testing.
import pyautogui # PyAutoGUI simulates key presses on the message boxes.
pyautogui.PAUSE = 0.1


GUI_WAIT = 1 # if tests start failing, maybe try bumping this up a bit (though that'll slow the tests down)


"""
NOTE: You will often see this code in this test:

    print('Line', inspect.currentframe().f_lineno);

This is because due to the GUI nature of these tests, if something messes up
and PyAutoGUI is unable to click on the message box, this program will get
held up. By printing out the line number, you will at least be able to see
which line displayed the message box that is held up.

This is a bit unorthodox, and I'm welcome to other suggestions about how to
deal with this possible scenario.
"""

class KeyPresses(threading.Thread):
    def __init__(self, keyPresses):
        super(KeyPresses, self).__init__()
        self.keyPresses = keyPresses

    def run(self):
        time.sleep(GUI_WAIT)
        pyautogui.typewrite(self.keyPresses, interval=0.05)


class AlertTests(unittest.TestCase):
    def test_alert(self):
        for func in (pymsgbox._alertTkinter, pymsgbox.alert):
            if func is pymsgbox._alertTkinter:
                print('Testing tkinter alert()')
            elif func is pymsgbox.alert:
                print('Testing native alert()')
            # no text
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(), 'OK')

            # text
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello'), 'OK')

            # text and title
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title'), 'OK')

            # text, title, and custom button
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', 'Button'), 'Button')

            # using keyword arguments
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(text='Hello', title='Title', button='Button'), 'Button')


class ConfirmTests(unittest.TestCase):
    def test_confirm(self):
        for func in (pymsgbox._confirmTkinter, pymsgbox.confirm):
            if func is pymsgbox._confirmTkinter:
                print('Testing tkinter confirm()')
            elif func is pymsgbox.confirm:
                print('Testing native confirm()')

            # press enter on OK
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(), 'OK')

            # press right, enter on Cancel
            t = KeyPresses(['right', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(), 'Cancel')

            # press right, left, right, enter on Cancel
            t = KeyPresses(['right', 'left', 'right', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(), 'Cancel')

            # press tab, enter on Cancel
            t = KeyPresses(['tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(), 'Cancel')

            # press tab, tab, enter on OK
            t = KeyPresses(['tab', 'tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(), 'OK')

            # with text
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello'), 'OK')

            # with text, title
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title'), 'OK')

            # with text, title, and one custom button
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A']), 'A')

            # with text, title, and one custom blank button
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['']), '')

            # with text, title, and two custom buttons
            t = KeyPresses(['enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B']), 'A')

            t = KeyPresses(['right', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B']), 'B')

            t = KeyPresses(['right', 'left', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B']), 'A')

            t = KeyPresses(['tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B']), 'B')

            t = KeyPresses(['tab', 'tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B']), 'A')

            # with text, title, and three custom buttons
            t = KeyPresses(['tab', 'tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B', 'C']), 'C')

            # with text, title, and four custom buttons
            t = KeyPresses(['tab', 'tab', 'tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B', 'C', 'D']), 'D')

            # with text, title, and five custom buttons
            t = KeyPresses(['tab', 'tab', 'tab', 'tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func('Hello', 'Title', ['A', 'B', 'C', 'D', 'E']), 'E')

            # with text, title, and three custom buttons specified with keyword arguments
            t = KeyPresses(['tab', 'tab', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(text='Hello', title='Title', buttons=['A', 'B', 'C']), 'C')

            # test that pressing Esc is the same as clicking Cancel (but only when there is a cancel button)
            t = KeyPresses(['escape'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(text='Escape button press test'), 'Cancel')

            # Make sure that Esc keypress does nothing if there is no Cancel button.
            t = KeyPresses(['escape', 'enter'])
            t.start()
            print('Line', inspect.currentframe().f_lineno)
            self.assertEqual(func(text='Escape button press test', buttons=['OK', 'Not OK']), 'OK')

class PromptPasswordTests(unittest.TestCase):
    def test_prompt(self):
        self._prompt_and_password_tests(pymsgbox._promptTkinter, 'prompt()')

    def test_password(self):
        # NOTE: Currently there is no way to test the appearance of the * or custom mask characters.
        self._prompt_and_password_tests(pymsgbox._passwordTkinter, 'password()')

    def _prompt_and_password_tests(self, msgBoxFunc, msgBoxFuncName):
        # entering nothing
        t = KeyPresses(['enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc()), (msgBoxFuncName, ''))

        # entering text
        t = KeyPresses(['a', 'b', 'c', 'enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc()), (msgBoxFuncName, 'abc'))

        # entering text, tabbing to the Ok key
        t = KeyPresses(['a', 'b', 'c', 'tab', 'enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc()), (msgBoxFuncName, 'abc'))

        # entering text but hitting cancel
        t = KeyPresses(['a', 'b', 'c', 'tab', 'tab', 'enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc()), (msgBoxFuncName, None))

        # with text
        t = KeyPresses(['a', 'b', 'c', 'enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc('Hello')), (msgBoxFuncName, 'abc'))

        # with text and title
        t = KeyPresses(['a', 'b', 'c', 'enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc('Hello', 'Title')), (msgBoxFuncName, 'abc'))

        # with text, title and default value
        t = KeyPresses(['enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc('Hello', 'Title', 'default')), (msgBoxFuncName, 'default'))

        # with text, title and default value specified by keyword arguments
        t = KeyPresses(['enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual((msgBoxFuncName, msgBoxFunc(text='Hello', title='Title', default='default')), (msgBoxFuncName, 'default'))

class TimeoutTests(unittest.TestCase):
    def test_timeout(self):
        # Note: If these test's fail, the unit tests will hang.
        self.assertEqual(pymsgbox._alertTkinter('timeout test', timeout=300), pymsgbox.TIMEOUT_RETURN_VALUE)
        self.assertEqual(pymsgbox._confirmTkinter('timeout test', timeout=300), pymsgbox.TIMEOUT_RETURN_VALUE)
        self.assertEqual(pymsgbox.prompt('timeout test', timeout=300), pymsgbox.TIMEOUT_RETURN_VALUE)
        self.assertEqual(pymsgbox.password('timeout test', timeout=300), pymsgbox.TIMEOUT_RETURN_VALUE)


""""
# NOTE: This is weird. This test fails (the additional typed in text gets added
# to the end of the default string, instead of replacing it), but when I run
# this same code using PyAutoGUI from the interactive shell (on Win7 Py3.3) it
# works. It also works when I type it in myself.
# Commenting this out for now.

class DefaultValueOverwriteTests(unittest.TestCase):
    def test_prompt(self):
        self._prompt_and_password_tests(pymsgbox.prompt, 'prompt()')

    def test_password(self):
        # NOTE: Currently there is no way to test the appearance of the * or custom mask characters.
        self._prompt_and_password_tests(pymsgbox.password, 'password()')

    def _prompt_and_password_tests(self, msgBoxFunc, msgBoxFuncName):
        # with text, title and default value that is typed over
        t = KeyPresses(['a', 'b', 'c', 'enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno); self.assertEqual((msgBoxFuncName, msgBoxFunc('Hello', 'Title', 'default')), (msgBoxFuncName, 'abc'))
"""


class WindowsNativeAlertTests(unittest.TestCase):
    def test_alert(self):
        if sys.platform != 'win32':
            return

        # TODO - We need some way of determining if the tkinter or native message box appeared.

        # test passing True for _tkinter
        t = KeyPresses(['enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual(pymsgbox.alert(_tkinter=True), pymsgbox.OK_TEXT)

        # test passing timeout
        t = KeyPresses(['enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual(pymsgbox.alert(timeout=300), pymsgbox.OK_TEXT)

        # test non-ok button to check that it falls back to tkinter
        t = KeyPresses(['enter'])
        t.start()
        print('Line', inspect.currentframe().f_lineno)
        self.assertEqual(pymsgbox.alert(button='Test'), 'Test')


class WindowsNativeConfirmTests(unittest.TestCase):
    def test_confirm(self):
        if sys.platform != 'win32':
            return

        # TODO - We need some way of determining if the tkinter or native message box appeared.

        # press enter on OK
        #t = KeyPresses(['enter'])
        #t.start()
        #print('Line', inspect.currentframe().f_lineno)
        #self.assertEqual(pymsgbox.confirm(), pymsgbox.OK_TEXT)


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