blob: 10e01b41cc2b58db8596bb8dd656d47ad3a5bdfd [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
R. David Murray59beec32009-03-30 19:04:00 +00005import time
Trent Mickf8cf13e2006-03-16 17:34:41 +00006import os
7import subprocess
Brian Curtind5c50b32010-04-13 02:25:20 +00008import _winreg
Trent Mickf8cf13e2006-03-16 17:34:41 +00009
R. David Murray59beec32009-03-30 19:04:00 +000010winsound = test_support.import_module('winsound')
11
Brian Curtind5c50b32010-04-13 02:25:20 +000012def has_sound(sound):
13 """Find out if a particular event is configured with a default sound"""
14 try:
15 key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
16 "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
17 value = _winreg.EnumValue(key, 0)[1]
18 if value is not u"":
19 return True
20 else:
21 return False
22 except WindowsError:
23 return False
Walter Dörwald7fd94242003-05-18 00:47:47 +000024
25class BeepTest(unittest.TestCase):
Neal Norwitz21807252008-03-05 05:14:18 +000026 # As with PlaySoundTest, incorporate the _have_soundcard() check
27 # into our test methods. If there's no audio device present,
28 # winsound.Beep returns 0 and GetLastError() returns 127, which
29 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
30 # be found"). (FWIW, virtual/Hyper-V systems fall under this
31 # scenario as they have no sound devices whatsoever (not even
32 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000033
34 def test_errors(self):
35 self.assertRaises(TypeError, winsound.Beep)
36 self.assertRaises(ValueError, winsound.Beep, 36, 75)
37 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
38
39 def test_extremes(self):
Steven Bethard89065752008-03-18 19:04:32 +000040 self._beep(37, 75)
41 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000042
43 def test_increasingfrequency(self):
Steven Bethard89065752008-03-18 19:04:32 +000044 for i in xrange(100, 2000, 100):
45 self._beep(i, 75)
46
47 def _beep(self, *args):
48 # these tests used to use _have_soundcard(), but it's quite
49 # possible to have a soundcard, and yet have the beep driver
50 # disabled. So basically, we have no way of knowing whether
51 # a beep should be produced or not, so currently if these
52 # tests fail we're ignoring them
53 #
54 # XXX the right fix for this is to define something like
55 # _have_enabled_beep_driver() and use that instead of the
56 # try/except below
57 try:
58 winsound.Beep(*args)
59 except RuntimeError:
60 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000061
62class MessageBeepTest(unittest.TestCase):
63
64 def tearDown(self):
65 time.sleep(0.5)
66
67 def test_default(self):
68 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
69 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
70 winsound.MessageBeep()
71
72 def test_ok(self):
73 winsound.MessageBeep(winsound.MB_OK)
74
75 def test_asterisk(self):
76 winsound.MessageBeep(winsound.MB_ICONASTERISK)
77
78 def test_exclamation(self):
79 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
80
81 def test_hand(self):
82 winsound.MessageBeep(winsound.MB_ICONHAND)
83
84 def test_question(self):
85 winsound.MessageBeep(winsound.MB_ICONQUESTION)
86
Trent Mickf8cf13e2006-03-16 17:34:41 +000087
Walter Dörwald7fd94242003-05-18 00:47:47 +000088class PlaySoundTest(unittest.TestCase):
89
90 def test_errors(self):
91 self.assertRaises(TypeError, winsound.PlaySound)
92 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
93 self.assertRaises(
94 RuntimeError,
95 winsound.PlaySound,
96 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
97 )
98
Brian Curtind5c50b32010-04-13 02:25:20 +000099 @unittest.skipUnless(has_sound("SystemAsterisk"), "No default SystemAsterisk")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000100 def test_alias_asterisk(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000101 if _have_soundcard():
102 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
103 else:
104 self.assertRaises(
105 RuntimeError,
106 winsound.PlaySound,
107 'SystemAsterisk', winsound.SND_ALIAS
108 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000109
Brian Curtind5c50b32010-04-13 02:25:20 +0000110 @unittest.skipUnless(has_sound("SystemExclamation"), "No default SystemExclamation")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000111 def test_alias_exclamation(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000112 if _have_soundcard():
113 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
114 else:
115 self.assertRaises(
116 RuntimeError,
117 winsound.PlaySound,
118 'SystemExclamation', winsound.SND_ALIAS
119 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000120
Brian Curtind5c50b32010-04-13 02:25:20 +0000121 @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000122 def test_alias_exit(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000123 if _have_soundcard():
124 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
125 else:
126 self.assertRaises(
127 RuntimeError,
128 winsound.PlaySound,
129 'SystemExit', winsound.SND_ALIAS
130 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000131
Brian Curtind5c50b32010-04-13 02:25:20 +0000132 @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000133 def test_alias_hand(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000134 if _have_soundcard():
135 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
136 else:
137 self.assertRaises(
138 RuntimeError,
139 winsound.PlaySound,
140 'SystemHand', winsound.SND_ALIAS
141 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000142
Brian Curtind5c50b32010-04-13 02:25:20 +0000143 @unittest.skipUnless(has_sound("SystemQuestion"), "No default SystemQuestion")
Walter Dörwald7fd94242003-05-18 00:47:47 +0000144 def test_alias_question(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000145 if _have_soundcard():
146 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
147 else:
148 self.assertRaises(
149 RuntimeError,
150 winsound.PlaySound,
151 'SystemQuestion', winsound.SND_ALIAS
152 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000153
154 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000155 # This test can't be expected to work on all systems. The MS
156 # PlaySound() docs say:
157 #
158 # If it cannot find the specified sound, PlaySound uses the
159 # default system event sound entry instead. If the function
160 # can find neither the system default entry nor the default
161 # sound, it makes no sound and returns FALSE.
162 #
163 # It's known to return FALSE on some real systems.
164
165 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
166 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000167
168 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000169 if _have_soundcard():
170 # Note that this is not the same as asserting RuntimeError
171 # will get raised: you cannot convert this to
172 # self.assertRaises(...) form. The attempt may or may not
173 # raise RuntimeError, but it shouldn't raise anything other
174 # than RuntimeError, and that's all we're trying to test
175 # here. The MS docs aren't clear about whether the SDK
176 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
177 # True or False when the alias is unknown. On Tim's WinXP
178 # box today, it returns True (no exception is raised). What
179 # we'd really like to test is that no sound is played, but
180 # that requires first wiring an eardrum class into unittest
181 # <wink>.
182 try:
183 winsound.PlaySound(
184 '!"$%&/(#+*',
185 winsound.SND_ALIAS | winsound.SND_NODEFAULT
186 )
187 except RuntimeError:
188 pass
189 else:
190 self.assertRaises(
191 RuntimeError,
192 winsound.PlaySound,
193 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000194 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000195
196 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000197 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000198 winsound.PlaySound(
199 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000200 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000201 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000202 time.sleep(0.5)
203 try:
204 winsound.PlaySound(
205 'SystemQuestion',
206 winsound.SND_ALIAS | winsound.SND_NOSTOP
207 )
208 except RuntimeError:
209 pass
210 else: # the first sound might already be finished
211 pass
212 winsound.PlaySound(None, winsound.SND_PURGE)
213 else:
Stefan Krah9aca91d2010-04-12 15:21:25 +0000214 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
215 # does not raise on systems without a sound card.
216 pass
Trent Mickf8cf13e2006-03-16 17:34:41 +0000217
218
219def _get_cscript_path():
220 """Return the full path to cscript.exe or None."""
221 for dir in os.environ.get("PATH", "").split(os.pathsep):
222 cscript_path = os.path.join(dir, "cscript.exe")
223 if os.path.exists(cscript_path):
224 return cscript_path
225
226__have_soundcard_cache = None
227def _have_soundcard():
228 """Return True iff this computer has a soundcard."""
229 global __have_soundcard_cache
230 if __have_soundcard_cache is None:
231 cscript_path = _get_cscript_path()
232 if cscript_path is None:
233 # Could not find cscript.exe to run our VBScript helper. Default
234 # to True: most computers these days *do* have a soundcard.
235 return True
236
237 check_script = os.path.join(os.path.dirname(__file__),
238 "check_soundcard.vbs")
239 p = subprocess.Popen([cscript_path, check_script],
240 stdout=subprocess.PIPE)
241 __have_soundcard_cache = not p.wait()
242 return __have_soundcard_cache
243
Walter Dörwald7fd94242003-05-18 00:47:47 +0000244
245def test_main():
246 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
247
248if __name__=="__main__":
249 test_main()