blob: 83618b6a4440f97bd2ac5cab4fe07aba47cfc30a [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.
Benjamin Petersona5119352012-10-09 11:14:59 -040019 if ctypes.windll.winmm.mixerGetNumDevs() == 0:
Brian Curtinae16af22010-06-01 13:49:19 +000020 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))
Benjamin Petersona5119352012-10-09 11:14:59 -040024 return winreg.EnumValue(key, 0)[1] != ""
Andrew Svetlov2606a6f2012-12-19 14:33:35 +020025 except OSError:
Brian Curtin36067602010-04-13 02:32:40 +000026 return False
Walter Dörwald7fd94242003-05-18 00:47:47 +000027
28class BeepTest(unittest.TestCase):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000029 # As with PlaySoundTest, incorporate the _have_soundcard() check
30 # into our test methods. If there's no audio device present,
31 # winsound.Beep returns 0 and GetLastError() returns 127, which
32 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
33 # be found"). (FWIW, virtual/Hyper-V systems fall under this
34 # scenario as they have no sound devices whatsoever (not even
35 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000036
37 def test_errors(self):
38 self.assertRaises(TypeError, winsound.Beep)
39 self.assertRaises(ValueError, winsound.Beep, 36, 75)
40 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
41
42 def test_extremes(self):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000043 self._beep(37, 75)
44 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000045
46 def test_increasingfrequency(self):
Amaury Forgeot d'Arc0a665ce2008-03-20 01:02:48 +000047 for i in range(100, 2000, 100):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000048 self._beep(i, 75)
49
50 def _beep(self, *args):
51 # these tests used to use _have_soundcard(), but it's quite
52 # possible to have a soundcard, and yet have the beep driver
53 # disabled. So basically, we have no way of knowing whether
54 # a beep should be produced or not, so currently if these
55 # tests fail we're ignoring them
56 #
57 # XXX the right fix for this is to define something like
58 # _have_enabled_beep_driver() and use that instead of the
59 # try/except below
60 try:
61 winsound.Beep(*args)
62 except RuntimeError:
63 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000064
65class MessageBeepTest(unittest.TestCase):
66
67 def tearDown(self):
68 time.sleep(0.5)
69
70 def test_default(self):
71 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
72 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
73 winsound.MessageBeep()
74
75 def test_ok(self):
76 winsound.MessageBeep(winsound.MB_OK)
77
78 def test_asterisk(self):
79 winsound.MessageBeep(winsound.MB_ICONASTERISK)
80
81 def test_exclamation(self):
82 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
83
84 def test_hand(self):
85 winsound.MessageBeep(winsound.MB_ICONHAND)
86
87 def test_question(self):
88 winsound.MessageBeep(winsound.MB_ICONQUESTION)
89
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090
Walter Dörwald7fd94242003-05-18 00:47:47 +000091class PlaySoundTest(unittest.TestCase):
92
93 def test_errors(self):
94 self.assertRaises(TypeError, winsound.PlaySound)
95 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
96 self.assertRaises(
97 RuntimeError,
98 winsound.PlaySound,
99 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
100 )
101
Brian Curtin36067602010-04-13 02:32:40 +0000102 @unittest.skipUnless(has_sound("SystemAsterisk"),
103 "No default SystemAsterisk")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000104 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 if _have_soundcard():
106 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
107 else:
108 self.assertRaises(
109 RuntimeError,
110 winsound.PlaySound,
111 'SystemAsterisk', winsound.SND_ALIAS
112 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000113
Brian Curtin36067602010-04-13 02:32:40 +0000114 @unittest.skipUnless(has_sound("SystemExclamation"),
115 "No default SystemExclamation")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000116 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 if _have_soundcard():
118 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
119 else:
120 self.assertRaises(
121 RuntimeError,
122 winsound.PlaySound,
123 'SystemExclamation', winsound.SND_ALIAS
124 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000125
Brian Curtin36067602010-04-13 02:32:40 +0000126 @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000127 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 if _have_soundcard():
129 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
130 else:
131 self.assertRaises(
132 RuntimeError,
133 winsound.PlaySound,
134 'SystemExit', winsound.SND_ALIAS
135 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000136
Brian Curtin36067602010-04-13 02:32:40 +0000137 @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000138 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 if _have_soundcard():
140 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
141 else:
142 self.assertRaises(
143 RuntimeError,
144 winsound.PlaySound,
145 'SystemHand', winsound.SND_ALIAS
146 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000147
Brian Curtin36067602010-04-13 02:32:40 +0000148 @unittest.skipUnless(has_sound("SystemQuestion"),
149 "No default SystemQuestion")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000150 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000151 if _have_soundcard():
152 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
153 else:
154 self.assertRaises(
155 RuntimeError,
156 winsound.PlaySound,
157 'SystemQuestion', winsound.SND_ALIAS
158 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000159
160 def test_alias_fallback(self):
Zachary Ware10074322013-12-16 09:02:41 -0600161 # In the absense of the ability to tell if a sound was actually
162 # played, this test has two acceptable outcomes: success (no error,
163 # sound was theoretically played; although as issue #19987 shows
164 # a box without a soundcard can "succeed") or RuntimeError. Any
165 # other error is a failure.
166 try:
Zachary Ware50a5dad2013-11-27 23:56:04 -0600167 winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
Zachary Ware10074322013-12-16 09:02:41 -0600168 except RuntimeError:
169 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +0000170
171 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000200 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000201 winsound.PlaySound(
202 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000204 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 Krahaf9c3cf2010-04-12 15:33:12 +0000217 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
218 # does not raise on systems without a sound card.
219 pass
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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()
Brian Curtin7ef28e82010-12-29 02:41:07 +0000245 p.stdout.close()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 return __have_soundcard_cache
247
Walter Dörwald7fd94242003-05-18 00:47:47 +0000248
249def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000250 support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000251
252if __name__=="__main__":
253 test_main()