blob: de444220c6e11530a63e1416d6247dcb072158b3 [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
Brian Curtin360878f2010-06-01 13:42:48 +00005import time
Trent Mickf8cf13e2006-03-16 17:34:41 +00006import os
7import subprocess
Brian Curtin360878f2010-06-01 13:42:48 +00008import ctypes
9
10winsound = test_support.import_module('winsound')
Brian Curtin56da87c2010-04-13 02:39:59 +000011import _winreg
Trent Mickf8cf13e2006-03-16 17:34:41 +000012
Walter Dörwald7fd94242003-05-18 00:47:47 +000013
Brian Curtin56da87c2010-04-13 02:39:59 +000014def has_sound(sound):
15 """Find out if a particular event is configured with a default sound"""
16 try:
Brian Curtin360878f2010-06-01 13:42:48 +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 Curtin56da87c2010-04-13 02:39:59 +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
31
Walter Dörwald7fd94242003-05-18 00:47:47 +000032class 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
106 def test_alias_asterisk(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000107 if not has_sound("SystemAsterisk"):
Victor Stinnerb2a28c32010-04-19 15:18:07 +0000108 return
Trent Mickf8cf13e2006-03-16 17:34:41 +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
118 def test_alias_exclamation(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000119 if not has_sound("SystemExclamation"):
Victor Stinnerb2a28c32010-04-19 15:18:07 +0000120 return
Trent Mickf8cf13e2006-03-16 17:34:41 +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
130 def test_alias_exit(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000131 if not has_sound("SystemExit"):
Victor Stinnerb2a28c32010-04-19 15:18:07 +0000132 return
Trent Mickf8cf13e2006-03-16 17:34:41 +0000133 if _have_soundcard():
134 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
135 else:
136 self.assertRaises(
137 RuntimeError,
138 winsound.PlaySound,
139 'SystemExit', winsound.SND_ALIAS
140 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000141
142 def test_alias_hand(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000143 if not has_sound("SystemHand"):
Victor Stinnerb2a28c32010-04-19 15:18:07 +0000144 return
Trent Mickf8cf13e2006-03-16 17:34:41 +0000145 if _have_soundcard():
146 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
147 else:
148 self.assertRaises(
149 RuntimeError,
150 winsound.PlaySound,
151 'SystemHand', winsound.SND_ALIAS
152 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000153
154 def test_alias_question(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000155 if not has_sound("SystemQuestion"):
Victor Stinnerb2a28c32010-04-19 15:18:07 +0000156 return
Trent Mickf8cf13e2006-03-16 17:34:41 +0000157 if _have_soundcard():
158 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
159 else:
160 self.assertRaises(
161 RuntimeError,
162 winsound.PlaySound,
163 'SystemQuestion', winsound.SND_ALIAS
164 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000165
166 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000167 # This test can't be expected to work on all systems. The MS
168 # PlaySound() docs say:
169 #
170 # If it cannot find the specified sound, PlaySound uses the
171 # default system event sound entry instead. If the function
172 # can find neither the system default entry nor the default
173 # sound, it makes no sound and returns FALSE.
174 #
175 # It's known to return FALSE on some real systems.
176
177 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
178 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000179
180 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000181 if _have_soundcard():
182 # Note that this is not the same as asserting RuntimeError
183 # will get raised: you cannot convert this to
184 # self.assertRaises(...) form. The attempt may or may not
185 # raise RuntimeError, but it shouldn't raise anything other
186 # than RuntimeError, and that's all we're trying to test
187 # here. The MS docs aren't clear about whether the SDK
188 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
189 # True or False when the alias is unknown. On Tim's WinXP
190 # box today, it returns True (no exception is raised). What
191 # we'd really like to test is that no sound is played, but
192 # that requires first wiring an eardrum class into unittest
193 # <wink>.
194 try:
195 winsound.PlaySound(
196 '!"$%&/(#+*',
197 winsound.SND_ALIAS | winsound.SND_NODEFAULT
198 )
199 except RuntimeError:
200 pass
201 else:
202 self.assertRaises(
203 RuntimeError,
204 winsound.PlaySound,
205 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000206 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000207
208 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000209 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000210 winsound.PlaySound(
211 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000212 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000213 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000214 time.sleep(0.5)
215 try:
216 winsound.PlaySound(
217 'SystemQuestion',
218 winsound.SND_ALIAS | winsound.SND_NOSTOP
219 )
220 except RuntimeError:
221 pass
222 else: # the first sound might already be finished
223 pass
224 winsound.PlaySound(None, winsound.SND_PURGE)
225 else:
Stefan Krah833c7a32010-04-12 15:29:47 +0000226 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
227 # does not raise on systems without a sound card.
228 pass
Trent Mickf8cf13e2006-03-16 17:34:41 +0000229
230
231def _get_cscript_path():
232 """Return the full path to cscript.exe or None."""
233 for dir in os.environ.get("PATH", "").split(os.pathsep):
234 cscript_path = os.path.join(dir, "cscript.exe")
235 if os.path.exists(cscript_path):
236 return cscript_path
237
238__have_soundcard_cache = None
239def _have_soundcard():
240 """Return True iff this computer has a soundcard."""
241 global __have_soundcard_cache
242 if __have_soundcard_cache is None:
243 cscript_path = _get_cscript_path()
244 if cscript_path is None:
245 # Could not find cscript.exe to run our VBScript helper. Default
246 # to True: most computers these days *do* have a soundcard.
247 return True
248
249 check_script = os.path.join(os.path.dirname(__file__),
250 "check_soundcard.vbs")
251 p = subprocess.Popen([cscript_path, check_script],
252 stdout=subprocess.PIPE)
253 __have_soundcard_cache = not p.wait()
254 return __have_soundcard_cache
255
Walter Dörwald7fd94242003-05-18 00:47:47 +0000256
257def test_main():
258 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
259
260if __name__=="__main__":
261 test_main()