blob: 6d8649abb914952210d9ba1a0ef333663dd43a36 [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 Ware72a01b22013-11-27 23:56:04 -0600162 if _have_soundcard():
163 winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
164 else:
165 self.assertRaises(
166 RuntimeError,
167 winsound.PlaySound,
168 '!"$%&/(#+*', winsound.SND_ALIAS
169 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000170
171 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000172 if _have_soundcard():
173 # Note that this is not the same as asserting RuntimeError
174 # will get raised: you cannot convert this to
175 # self.assertRaises(...) form. The attempt may or may not
176 # raise RuntimeError, but it shouldn't raise anything other
177 # than RuntimeError, and that's all we're trying to test
178 # here. The MS docs aren't clear about whether the SDK
179 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
180 # True or False when the alias is unknown. On Tim's WinXP
181 # box today, it returns True (no exception is raised). What
182 # we'd really like to test is that no sound is played, but
183 # that requires first wiring an eardrum class into unittest
184 # <wink>.
185 try:
186 winsound.PlaySound(
187 '!"$%&/(#+*',
188 winsound.SND_ALIAS | winsound.SND_NODEFAULT
189 )
190 except RuntimeError:
191 pass
192 else:
193 self.assertRaises(
194 RuntimeError,
195 winsound.PlaySound,
196 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000197 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000198
199 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000200 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000201 winsound.PlaySound(
202 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000203 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000204 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000205 time.sleep(0.5)
206 try:
207 winsound.PlaySound(
208 'SystemQuestion',
209 winsound.SND_ALIAS | winsound.SND_NOSTOP
210 )
211 except RuntimeError:
212 pass
213 else: # the first sound might already be finished
214 pass
215 winsound.PlaySound(None, winsound.SND_PURGE)
216 else:
Stefan Krah9aca91d2010-04-12 15:21:25 +0000217 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
218 # does not raise on systems without a sound card.
219 pass
Trent Mickf8cf13e2006-03-16 17:34:41 +0000220
221
222def _get_cscript_path():
223 """Return the full path to cscript.exe or None."""
224 for dir in os.environ.get("PATH", "").split(os.pathsep):
225 cscript_path = os.path.join(dir, "cscript.exe")
226 if os.path.exists(cscript_path):
227 return cscript_path
228
229__have_soundcard_cache = None
230def _have_soundcard():
231 """Return True iff this computer has a soundcard."""
232 global __have_soundcard_cache
233 if __have_soundcard_cache is None:
234 cscript_path = _get_cscript_path()
235 if cscript_path is None:
236 # Could not find cscript.exe to run our VBScript helper. Default
237 # to True: most computers these days *do* have a soundcard.
238 return True
239
240 check_script = os.path.join(os.path.dirname(__file__),
241 "check_soundcard.vbs")
242 p = subprocess.Popen([cscript_path, check_script],
243 stdout=subprocess.PIPE)
244 __have_soundcard_cache = not p.wait()
245 return __have_soundcard_cache
246
Walter Dörwald7fd94242003-05-18 00:47:47 +0000247
248def test_main():
249 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
250
251if __name__=="__main__":
252 test_main()