blob: 4208fe71c4a7248d87acb3a5282ddb90381887a1 [file] [log] [blame]
Guido van Rossum2512d6d2000-04-24 14:01:51 +00001# Ridiculously simple test of the winsound module for Windows.
Guido van Rossumcdd092f2000-04-21 21:28:47 +00002
Walter Dörwald7fd94242003-05-18 00:47:47 +00003import unittest
4from test import test_support
Antoine Pitrou508d7d32012-11-24 18:25:25 +01005test_support.requires('audio')
R. David Murray59beec32009-03-30 19:04:00 +00006import time
Trent Mickf8cf13e2006-03-16 17:34:41 +00007import os
8import subprocess
9
R. David Murray59beec32009-03-30 19:04:00 +000010winsound = test_support.import_module('winsound')
Antoine Pitrou920e7e92010-08-10 00:47:48 +000011ctypes = test_support.import_module('ctypes')
Mark Dickinson2c4798b2010-04-13 11:37:23 +000012import _winreg
R. David Murray59beec32009-03-30 19:04:00 +000013
Brian Curtind5c50b32010-04-13 02:25:20 +000014def has_sound(sound):
15 """Find out if a particular event is configured with a default sound"""
16 try:
Brian Curtin824912e2010-06-01 13:29:13 +000017 # Ask the mixer API for the number of devices it knows about.
18 # When there are no devices, PlaySound will fail.
19 if ctypes.windll.winmm.mixerGetNumDevs() is 0:
20 return False
21
Brian Curtind5c50b32010-04-13 02:25:20 +000022 key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
23 "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
24 value = _winreg.EnumValue(key, 0)[1]
25 if value is not u"":
26 return True
27 else:
28 return False
29 except WindowsError:
30 return False
Walter Dörwald7fd94242003-05-18 00:47:47 +000031
32class BeepTest(unittest.TestCase):
Neal Norwitz21807252008-03-05 05:14:18 +000033 # As with PlaySoundTest, incorporate the _have_soundcard() check
34 # into our test methods. If there's no audio device present,
35 # winsound.Beep returns 0 and GetLastError() returns 127, which
36 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
37 # be found"). (FWIW, virtual/Hyper-V systems fall under this
38 # scenario as they have no sound devices whatsoever (not even
39 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000040
41 def test_errors(self):
42 self.assertRaises(TypeError, winsound.Beep)
43 self.assertRaises(ValueError, winsound.Beep, 36, 75)
44 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
45
46 def test_extremes(self):
Steven Bethard89065752008-03-18 19:04:32 +000047 self._beep(37, 75)
48 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000049
50 def test_increasingfrequency(self):
Steven Bethard89065752008-03-18 19:04:32 +000051 for i in xrange(100, 2000, 100):
52 self._beep(i, 75)
53
54 def _beep(self, *args):
55 # these tests used to use _have_soundcard(), but it's quite
56 # possible to have a soundcard, and yet have the beep driver
57 # disabled. So basically, we have no way of knowing whether
58 # a beep should be produced or not, so currently if these
59 # tests fail we're ignoring them
60 #
61 # XXX the right fix for this is to define something like
62 # _have_enabled_beep_driver() and use that instead of the
63 # try/except below
64 try:
65 winsound.Beep(*args)
66 except RuntimeError:
67 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000068
69class MessageBeepTest(unittest.TestCase):
70
71 def tearDown(self):
72 time.sleep(0.5)
73
74 def test_default(self):
75 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
76 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
77 winsound.MessageBeep()
78
79 def test_ok(self):
80 winsound.MessageBeep(winsound.MB_OK)
81
82 def test_asterisk(self):
83 winsound.MessageBeep(winsound.MB_ICONASTERISK)
84
85 def test_exclamation(self):
86 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
87
88 def test_hand(self):
89 winsound.MessageBeep(winsound.MB_ICONHAND)
90
91 def test_question(self):
92 winsound.MessageBeep(winsound.MB_ICONQUESTION)
93
Trent Mickf8cf13e2006-03-16 17:34:41 +000094
Walter Dörwald7fd94242003-05-18 00:47:47 +000095class PlaySoundTest(unittest.TestCase):
96
97 def test_errors(self):
98 self.assertRaises(TypeError, winsound.PlaySound)
99 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
100 self.assertRaises(
101 RuntimeError,
102 winsound.PlaySound,
103 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
104 )
105
Brian Curtind5c50b32010-04-13 02:25:20 +0000106 @unittest.skipUnless(has_sound("SystemAsterisk"), "No default SystemAsterisk")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000107 def test_alias_asterisk(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000108 if _have_soundcard():
109 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
110 else:
111 self.assertRaises(
112 RuntimeError,
113 winsound.PlaySound,
114 'SystemAsterisk', winsound.SND_ALIAS
115 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000116
Brian Curtind5c50b32010-04-13 02:25:20 +0000117 @unittest.skipUnless(has_sound("SystemExclamation"), "No default SystemExclamation")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000118 def test_alias_exclamation(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000119 if _have_soundcard():
120 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
121 else:
122 self.assertRaises(
123 RuntimeError,
124 winsound.PlaySound,
125 'SystemExclamation', winsound.SND_ALIAS
126 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000127
Brian Curtind5c50b32010-04-13 02:25:20 +0000128 @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000129 def test_alias_exit(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000130 if _have_soundcard():
131 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
132 else:
133 self.assertRaises(
134 RuntimeError,
135 winsound.PlaySound,
136 'SystemExit', winsound.SND_ALIAS
137 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000138
Brian Curtind5c50b32010-04-13 02:25:20 +0000139 @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000140 def test_alias_hand(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000141 if _have_soundcard():
142 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
143 else:
144 self.assertRaises(
145 RuntimeError,
146 winsound.PlaySound,
147 'SystemHand', winsound.SND_ALIAS
148 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000149
Brian Curtind5c50b32010-04-13 02:25:20 +0000150 @unittest.skipUnless(has_sound("SystemQuestion"), "No default SystemQuestion")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000151 def test_alias_question(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000152 if _have_soundcard():
153 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
154 else:
155 self.assertRaises(
156 RuntimeError,
157 winsound.PlaySound,
158 'SystemQuestion', winsound.SND_ALIAS
159 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000160
161 def test_alias_fallback(self):
Zachary Ware51e90bc2013-12-16 08:58:10 -0600162 # In the absense of the ability to tell if a sound was actually
163 # played, this test has two acceptable outcomes: success (no error,
164 # sound was theoretically played; although as issue #19987 shows
165 # a box without a soundcard can "succeed") or RuntimeError. Any
166 # other error is a failure.
167 try:
Zachary Ware72a01b22013-11-27 23:56:04 -0600168 winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
Zachary Ware51e90bc2013-12-16 08:58:10 -0600169 except RuntimeError:
170 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +0000171
172 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000173 if _have_soundcard():
174 # Note that this is not the same as asserting RuntimeError
175 # will get raised: you cannot convert this to
176 # self.assertRaises(...) form. The attempt may or may not
177 # raise RuntimeError, but it shouldn't raise anything other
178 # than RuntimeError, and that's all we're trying to test
179 # here. The MS docs aren't clear about whether the SDK
180 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
181 # True or False when the alias is unknown. On Tim's WinXP
182 # box today, it returns True (no exception is raised). What
183 # we'd really like to test is that no sound is played, but
184 # that requires first wiring an eardrum class into unittest
185 # <wink>.
186 try:
187 winsound.PlaySound(
188 '!"$%&/(#+*',
189 winsound.SND_ALIAS | winsound.SND_NODEFAULT
190 )
191 except RuntimeError:
192 pass
193 else:
194 self.assertRaises(
195 RuntimeError,
196 winsound.PlaySound,
197 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000198 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000199
200 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000201 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000202 winsound.PlaySound(
203 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000204 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000205 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000206 time.sleep(0.5)
207 try:
208 winsound.PlaySound(
209 'SystemQuestion',
210 winsound.SND_ALIAS | winsound.SND_NOSTOP
211 )
212 except RuntimeError:
213 pass
214 else: # the first sound might already be finished
215 pass
216 winsound.PlaySound(None, winsound.SND_PURGE)
217 else:
Stefan Krah9aca91d2010-04-12 15:21:25 +0000218 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
219 # does not raise on systems without a sound card.
220 pass
Trent Mickf8cf13e2006-03-16 17:34:41 +0000221
222
223def _get_cscript_path():
224 """Return the full path to cscript.exe or None."""
225 for dir in os.environ.get("PATH", "").split(os.pathsep):
226 cscript_path = os.path.join(dir, "cscript.exe")
227 if os.path.exists(cscript_path):
228 return cscript_path
229
230__have_soundcard_cache = None
231def _have_soundcard():
232 """Return True iff this computer has a soundcard."""
233 global __have_soundcard_cache
234 if __have_soundcard_cache is None:
235 cscript_path = _get_cscript_path()
236 if cscript_path is None:
237 # Could not find cscript.exe to run our VBScript helper. Default
238 # to True: most computers these days *do* have a soundcard.
239 return True
240
241 check_script = os.path.join(os.path.dirname(__file__),
242 "check_soundcard.vbs")
243 p = subprocess.Popen([cscript_path, check_script],
244 stdout=subprocess.PIPE)
245 __have_soundcard_cache = not p.wait()
246 return __have_soundcard_cache
247
Walter Dörwald7fd94242003-05-18 00:47:47 +0000248
249def test_main():
250 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
251
252if __name__=="__main__":
253 test_main()