blob: 34c3deaf7eb3c0c21c7d3a25f83b166c5e0b0f47 [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
5support.requires('audio')
R. David Murraya21e4ca2009-03-31 23:16:50 +00006import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007import os
8import subprocess
9
R. David Murraya21e4ca2009-03-31 23:16:50 +000010winsound = support.import_module('winsound')
Antoine Pitrou817c9df2010-08-10 00:45:32 +000011ctypes = support.import_module('ctypes')
Mark Dickinsona09543c2010-04-13 11:43:10 +000012import winreg
R. David Murraya21e4ca2009-03-31 23:16:50 +000013
Brian Curtin36067602010-04-13 02:32:40 +000014def has_sound(sound):
15 """Find out if a particular event is configured with a default sound"""
16 try:
Brian Curtinae16af22010-06-01 13:49:19 +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 Curtinf1407182010-04-13 12:43:07 +000022 key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER,
Brian Curtin36067602010-04-13 02:32:40 +000023 "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
24 value = winreg.EnumValue(key, 0)[1]
Mark Dickinson141b72e2010-04-13 11:54:59 +000025 if value is not "":
Brian Curtin36067602010-04-13 02:32:40 +000026 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):
Christian Heimesdd15f6c2008-03-16 00:07:10 +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):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000047 self._beep(37, 75)
48 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000049
50 def test_increasingfrequency(self):
Amaury Forgeot d'Arc0a665ce2008-03-20 01:02:48 +000051 for i in range(100, 2000, 100):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000052 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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 Curtin36067602010-04-13 02:32:40 +0000106 @unittest.skipUnless(has_sound("SystemAsterisk"),
107 "No default SystemAsterisk")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000108 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109 if _have_soundcard():
110 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
111 else:
112 self.assertRaises(
113 RuntimeError,
114 winsound.PlaySound,
115 'SystemAsterisk', winsound.SND_ALIAS
116 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000117
Brian Curtin36067602010-04-13 02:32:40 +0000118 @unittest.skipUnless(has_sound("SystemExclamation"),
119 "No default SystemExclamation")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000120 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121 if _have_soundcard():
122 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
123 else:
124 self.assertRaises(
125 RuntimeError,
126 winsound.PlaySound,
127 'SystemExclamation', winsound.SND_ALIAS
128 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000129
Brian Curtin36067602010-04-13 02:32:40 +0000130 @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000131 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 if _have_soundcard():
133 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
134 else:
135 self.assertRaises(
136 RuntimeError,
137 winsound.PlaySound,
138 'SystemExit', winsound.SND_ALIAS
139 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000140
Brian Curtin36067602010-04-13 02:32:40 +0000141 @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000142 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 if _have_soundcard():
144 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
145 else:
146 self.assertRaises(
147 RuntimeError,
148 winsound.PlaySound,
149 'SystemHand', winsound.SND_ALIAS
150 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000151
Brian Curtin36067602010-04-13 02:32:40 +0000152 @unittest.skipUnless(has_sound("SystemQuestion"),
153 "No default SystemQuestion")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000154 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155 if _have_soundcard():
156 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
157 else:
158 self.assertRaises(
159 RuntimeError,
160 winsound.PlaySound,
161 'SystemQuestion', winsound.SND_ALIAS
162 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000163
164 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000165 # This test can't be expected to work on all systems. The MS
166 # PlaySound() docs say:
167 #
168 # If it cannot find the specified sound, PlaySound uses the
169 # default system event sound entry instead. If the function
170 # can find neither the system default entry nor the default
171 # sound, it makes no sound and returns FALSE.
172 #
173 # It's known to return FALSE on some real systems.
174
175 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
176 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000177
178 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 if _have_soundcard():
180 # Note that this is not the same as asserting RuntimeError
181 # will get raised: you cannot convert this to
182 # self.assertRaises(...) form. The attempt may or may not
183 # raise RuntimeError, but it shouldn't raise anything other
184 # than RuntimeError, and that's all we're trying to test
185 # here. The MS docs aren't clear about whether the SDK
186 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
187 # True or False when the alias is unknown. On Tim's WinXP
188 # box today, it returns True (no exception is raised). What
189 # we'd really like to test is that no sound is played, but
190 # that requires first wiring an eardrum class into unittest
191 # <wink>.
192 try:
193 winsound.PlaySound(
194 '!"$%&/(#+*',
195 winsound.SND_ALIAS | winsound.SND_NODEFAULT
196 )
197 except RuntimeError:
198 pass
199 else:
200 self.assertRaises(
201 RuntimeError,
202 winsound.PlaySound,
203 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000204 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000205
206 def test_stopasync(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000208 winsound.PlaySound(
209 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000211 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212 time.sleep(0.5)
213 try:
214 winsound.PlaySound(
215 'SystemQuestion',
216 winsound.SND_ALIAS | winsound.SND_NOSTOP
217 )
218 except RuntimeError:
219 pass
220 else: # the first sound might already be finished
221 pass
222 winsound.PlaySound(None, winsound.SND_PURGE)
223 else:
Stefan Krahaf9c3cf2010-04-12 15:33:12 +0000224 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
225 # does not raise on systems without a sound card.
226 pass
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227
228
229def _get_cscript_path():
230 """Return the full path to cscript.exe or None."""
231 for dir in os.environ.get("PATH", "").split(os.pathsep):
232 cscript_path = os.path.join(dir, "cscript.exe")
233 if os.path.exists(cscript_path):
234 return cscript_path
235
236__have_soundcard_cache = None
237def _have_soundcard():
238 """Return True iff this computer has a soundcard."""
239 global __have_soundcard_cache
240 if __have_soundcard_cache is None:
241 cscript_path = _get_cscript_path()
242 if cscript_path is None:
243 # Could not find cscript.exe to run our VBScript helper. Default
244 # to True: most computers these days *do* have a soundcard.
245 return True
246
247 check_script = os.path.join(os.path.dirname(__file__),
248 "check_soundcard.vbs")
249 p = subprocess.Popen([cscript_path, check_script],
250 stdout=subprocess.PIPE)
251 __have_soundcard_cache = not p.wait()
Brian Curtin7ef28e82010-12-29 02:41:07 +0000252 p.stdout.close()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253 return __have_soundcard_cache
254
Walter Dörwald7fd94242003-05-18 00:47:47 +0000255
256def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000257 support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000258
259if __name__=="__main__":
260 test_main()