Debian Codegraph @

Lib/test/test_winsound.py

raw
1 # Ridiculously simple test of the winsound module for Windows.
2
3 import functools
4 import os
5 import time
6 import unittest
7
8 from test import support
9 from test.support import import_helper
10 from test.support import os_helper
11
12
13 support.requires('audio')
14 winsound = import_helper.import_module('winsound')
15
16
17 # Unless we actually have an ear in the room, we have no idea whether a sound
18 # actually plays, and it's incredibly flaky trying to figure out if a sound
19 # even *should* play. Instead of guessing, just call the function and assume
20 # it either passed or raised the RuntimeError we expect in case of failure.
21 def sound_func(func):
22 @functools.wraps(func)
23 def wrapper(*args, **kwargs):
24 try:
25 ret = func(*args, **kwargs)
26 except RuntimeError as e:
27 if support.verbose:
28 print(func.__name__, 'failed:', e)
29 else:
30 if support.verbose:
31 print(func.__name__, 'returned')
32 return ret
33 return wrapper
34
35
36 safe_Beep = sound_func(winsound.Beep)
37 safe_MessageBeep = sound_func(winsound.MessageBeep)
38 safe_PlaySound = sound_func(winsound.PlaySound)
39
40
41 class BeepTest(unittest.TestCase):
42
43 def test_errors(self):
44 self.assertRaises(TypeError, winsound.Beep)
45 self.assertRaises(ValueError, winsound.Beep, 36, 75)
46 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
47
48 def test_extremes(self):
49 safe_Beep(37, 75)
50 safe_Beep(32767, 75)
51
52 def test_increasingfrequency(self):
53 for i in range(100, 2000, 100):
54 safe_Beep(i, 75)
55
56 def test_keyword_args(self):
57 safe_Beep(duration=75, frequency=2000)
58
59
60 class MessageBeepTest(unittest.TestCase):
61
62 def tearDown(self):
63 time.sleep(0.5)
64
65 def test_default(self):
66 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
67 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
68 safe_MessageBeep()
69
70 def test_ok(self):
71 safe_MessageBeep(winsound.MB_OK)
72
73 def test_asterisk(self):
74 safe_MessageBeep(winsound.MB_ICONASTERISK)
75
76 def test_exclamation(self):
77 safe_MessageBeep(winsound.MB_ICONEXCLAMATION)
78
79 def test_hand(self):
80 safe_MessageBeep(winsound.MB_ICONHAND)
81
82 def test_question(self):
83 safe_MessageBeep(winsound.MB_ICONQUESTION)
84
85 def test_error(self):
86 safe_MessageBeep(winsound.MB_ICONERROR)
87
88 def test_information(self):
89 safe_MessageBeep(winsound.MB_ICONINFORMATION)
90
91 def test_stop(self):
92 safe_MessageBeep(winsound.MB_ICONSTOP)
93
94 def test_warning(self):
95 safe_MessageBeep(winsound.MB_ICONWARNING)
96
97 def test_keyword_args(self):
98 safe_MessageBeep(type=winsound.MB_OK)
99
100
101 class PlaySoundTest(unittest.TestCase):
102
103 def test_errors(self):
104 self.assertRaises(TypeError, winsound.PlaySound)
105 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
106 self.assertRaises(
107 RuntimeError,
108 winsound.PlaySound,
109 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
110 )
111 self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0)
112 self.assertRaises(TypeError, winsound.PlaySound, "bad",
113 winsound.SND_MEMORY)
114 self.assertRaises(TypeError, winsound.PlaySound, 1, 0)
115 # embedded null character
116 self.assertRaises(ValueError, winsound.PlaySound, 'bad\0', 0)
117
118 def test_keyword_args(self):
119 safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit")
120
121 def test_snd_memory(self):
122 with open(support.findfile('pluck-pcm8.wav',
123 subdir='audiodata'), 'rb') as f:
124 audio_data = f.read()
125 safe_PlaySound(audio_data, winsound.SND_MEMORY)
126 audio_data = bytearray(audio_data)
127 safe_PlaySound(audio_data, winsound.SND_MEMORY)
128
129 def test_snd_filename(self):
130 fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
131 safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
132
133 def test_snd_filepath(self):
134 fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
135 path = os_helper.FakePath(fn)
136 safe_PlaySound(path, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
137
138 def test_snd_filepath_as_bytes(self):
139 fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
140 self.assertRaises(
141 TypeError,
142 winsound.PlaySound,
143 os_helper.FakePath(os.fsencode(fn)),
144 winsound.SND_FILENAME | winsound.SND_NODEFAULT
145 )
146
147 def test_aliases(self):
148 aliases = [
149 "SystemAsterisk",
150 "SystemExclamation",
151 "SystemExit",
152 "SystemHand",
153 "SystemQuestion",
154 ]
155 for alias in aliases:
156 with self.subTest(alias=alias):
157 safe_PlaySound(alias, winsound.SND_ALIAS)
158
159 def test_alias_fallback(self):
160 safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
161
162 def test_alias_nofallback(self):
163 safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT)
164
165 def test_stopasync(self):
166 safe_PlaySound(
167 'SystemQuestion',
168 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
169 )
170 time.sleep(0.5)
171 safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP)
172 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
173 # does not raise on systems without a sound card.
174 winsound.PlaySound(None, winsound.SND_PURGE)
175
176 def test_sound_sentry(self):
177 safe_PlaySound("SystemExit", winsound.SND_ALIAS | winsound.SND_SENTRY)
178
179 def test_sound_sync(self):
180 safe_PlaySound("SystemExit", winsound.SND_ALIAS | winsound.SND_SYNC)
181
182 def test_sound_system(self):
183 safe_PlaySound("SystemExit", winsound.SND_ALIAS | winsound.SND_SYSTEM)
184
185
186 if __name__ == "__main__":
187 unittest.main()