blob: 2d5614c12a7115e09f303f26c78a9a38b5924571 [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
Guido van Rossum11d204c2003-04-09 19:57:06 +00005import winsound, time
Trent Mickf8cf13e2006-03-16 17:34:41 +00006import os
7import subprocess
Brian Curtin56da87c2010-04-13 02:39:59 +00008import _winreg
Trent Mickf8cf13e2006-03-16 17:34:41 +00009
Walter Dörwald7fd94242003-05-18 00:47:47 +000010
Brian Curtin56da87c2010-04-13 02:39:59 +000011def has_sound(sound):
12 """Find out if a particular event is configured with a default sound"""
13 try:
14 key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,
15 "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
16 value = _winreg.EnumValue(key, 0)[1]
17 if value is not u"":
18 return True
19 else:
20 return False
21 except WindowsError:
22 return False
23
Walter Dörwald7fd94242003-05-18 00:47:47 +000024class BeepTest(unittest.TestCase):
Neal Norwitz21807252008-03-05 05:14:18 +000025 # As with PlaySoundTest, incorporate the _have_soundcard() check
26 # into our test methods. If there's no audio device present,
27 # winsound.Beep returns 0 and GetLastError() returns 127, which
28 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
29 # be found"). (FWIW, virtual/Hyper-V systems fall under this
30 # scenario as they have no sound devices whatsoever (not even
31 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000032
33 def test_errors(self):
34 self.assertRaises(TypeError, winsound.Beep)
35 self.assertRaises(ValueError, winsound.Beep, 36, 75)
36 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
37
38 def test_extremes(self):
Steven Bethard89065752008-03-18 19:04:32 +000039 self._beep(37, 75)
40 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000041
42 def test_increasingfrequency(self):
Steven Bethard89065752008-03-18 19:04:32 +000043 for i in xrange(100, 2000, 100):
44 self._beep(i, 75)
45
46 def _beep(self, *args):
47 # these tests used to use _have_soundcard(), but it's quite
48 # possible to have a soundcard, and yet have the beep driver
49 # disabled. So basically, we have no way of knowing whether
50 # a beep should be produced or not, so currently if these
51 # tests fail we're ignoring them
52 #
53 # XXX the right fix for this is to define something like
54 # _have_enabled_beep_driver() and use that instead of the
55 # try/except below
56 try:
57 winsound.Beep(*args)
58 except RuntimeError:
59 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000060
61class MessageBeepTest(unittest.TestCase):
62
63 def tearDown(self):
64 time.sleep(0.5)
65
66 def test_default(self):
67 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
68 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
69 winsound.MessageBeep()
70
71 def test_ok(self):
72 winsound.MessageBeep(winsound.MB_OK)
73
74 def test_asterisk(self):
75 winsound.MessageBeep(winsound.MB_ICONASTERISK)
76
77 def test_exclamation(self):
78 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
79
80 def test_hand(self):
81 winsound.MessageBeep(winsound.MB_ICONHAND)
82
83 def test_question(self):
84 winsound.MessageBeep(winsound.MB_ICONQUESTION)
85
Trent Mickf8cf13e2006-03-16 17:34:41 +000086
Walter Dörwald7fd94242003-05-18 00:47:47 +000087class PlaySoundTest(unittest.TestCase):
88
89 def test_errors(self):
90 self.assertRaises(TypeError, winsound.PlaySound)
91 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
92 self.assertRaises(
93 RuntimeError,
94 winsound.PlaySound,
95 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
96 )
97
98 def test_alias_asterisk(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +000099 if not has_sound("SystemAsterisk"):
100 raise test_support.TestSkipped("No default SystemAsterisk")
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
110 def test_alias_exclamation(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000111 if not has_sound("SystemExclamation"):
112 raise test_support.TestSkipped("No default SystemExclamation")
Trent Mickf8cf13e2006-03-16 17:34:41 +0000113 if _have_soundcard():
114 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
115 else:
116 self.assertRaises(
117 RuntimeError,
118 winsound.PlaySound,
119 'SystemExclamation', winsound.SND_ALIAS
120 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000121
122 def test_alias_exit(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000123 if not has_sound("SystemExit"):
124 raise test_support.TestSkipped("No default SystemExit")
Trent Mickf8cf13e2006-03-16 17:34:41 +0000125 if _have_soundcard():
126 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
127 else:
128 self.assertRaises(
129 RuntimeError,
130 winsound.PlaySound,
131 'SystemExit', winsound.SND_ALIAS
132 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000133
134 def test_alias_hand(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000135 if not has_sound("SystemHand"):
136 raise test_support.TestSkipped("No default SystemHand")
Trent Mickf8cf13e2006-03-16 17:34:41 +0000137 if _have_soundcard():
138 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
139 else:
140 self.assertRaises(
141 RuntimeError,
142 winsound.PlaySound,
143 'SystemHand', winsound.SND_ALIAS
144 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000145
146 def test_alias_question(self):
Victor Stinnerb46d7ba2010-04-19 12:14:14 +0000147 if not has_sound("SystemQuestion"):
148 raise test_support.TestSkipped("No default SystemQuestion")
Trent Mickf8cf13e2006-03-16 17:34:41 +0000149 if _have_soundcard():
150 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
151 else:
152 self.assertRaises(
153 RuntimeError,
154 winsound.PlaySound,
155 'SystemQuestion', winsound.SND_ALIAS
156 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000157
158 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000159 # This test can't be expected to work on all systems. The MS
160 # PlaySound() docs say:
161 #
162 # If it cannot find the specified sound, PlaySound uses the
163 # default system event sound entry instead. If the function
164 # can find neither the system default entry nor the default
165 # sound, it makes no sound and returns FALSE.
166 #
167 # It's known to return FALSE on some real systems.
168
169 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
170 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000171
172 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000173 if _have_soundcard():
174 # Note that this is not the same as asserting RuntimeError
175 # will get raised: you cannot convert this to
176 # self.assertRaises(...) form. The attempt may or may not
177 # raise RuntimeError, but it shouldn't raise anything other
178 # than RuntimeError, and that's all we're trying to test
179 # here. The MS docs aren't clear about whether the SDK
180 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
181 # True or False when the alias is unknown. On Tim's WinXP
182 # box today, it returns True (no exception is raised). What
183 # we'd really like to test is that no sound is played, but
184 # that requires first wiring an eardrum class into unittest
185 # <wink>.
186 try:
187 winsound.PlaySound(
188 '!"$%&/(#+*',
189 winsound.SND_ALIAS | winsound.SND_NODEFAULT
190 )
191 except RuntimeError:
192 pass
193 else:
194 self.assertRaises(
195 RuntimeError,
196 winsound.PlaySound,
197 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000198 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000199
200 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000201 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000202 winsound.PlaySound(
203 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000204 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000205 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000206 time.sleep(0.5)
207 try:
208 winsound.PlaySound(
209 'SystemQuestion',
210 winsound.SND_ALIAS | winsound.SND_NOSTOP
211 )
212 except RuntimeError:
213 pass
214 else: # the first sound might already be finished
215 pass
216 winsound.PlaySound(None, winsound.SND_PURGE)
217 else:
Stefan Krah833c7a32010-04-12 15:29:47 +0000218 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
219 # does not raise on systems without a sound card.
220 pass
Trent Mickf8cf13e2006-03-16 17:34:41 +0000221
222
223def _get_cscript_path():
224 """Return the full path to cscript.exe or None."""
225 for dir in os.environ.get("PATH", "").split(os.pathsep):
226 cscript_path = os.path.join(dir, "cscript.exe")
227 if os.path.exists(cscript_path):
228 return cscript_path
229
230__have_soundcard_cache = None
231def _have_soundcard():
232 """Return True iff this computer has a soundcard."""
233 global __have_soundcard_cache
234 if __have_soundcard_cache is None:
235 cscript_path = _get_cscript_path()
236 if cscript_path is None:
237 # Could not find cscript.exe to run our VBScript helper. Default
238 # to True: most computers these days *do* have a soundcard.
239 return True
240
241 check_script = os.path.join(os.path.dirname(__file__),
242 "check_soundcard.vbs")
243 p = subprocess.Popen([cscript_path, check_script],
244 stdout=subprocess.PIPE)
245 __have_soundcard_cache = not p.wait()
246 return __have_soundcard_cache
247
Walter Dörwald7fd94242003-05-18 00:47:47 +0000248
249def test_main():
250 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
251
252if __name__=="__main__":
253 test_main()