blob: 2950bb7d88cba1fd171f0a88f31e38c089e4a936 [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')
11
Walter Dörwald7fd94242003-05-18 00:47:47 +000012
13class BeepTest(unittest.TestCase):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000014 # As with PlaySoundTest, incorporate the _have_soundcard() check
15 # into our test methods. If there's no audio device present,
16 # winsound.Beep returns 0 and GetLastError() returns 127, which
17 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
18 # be found"). (FWIW, virtual/Hyper-V systems fall under this
19 # scenario as they have no sound devices whatsoever (not even
20 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000021
22 def test_errors(self):
23 self.assertRaises(TypeError, winsound.Beep)
24 self.assertRaises(ValueError, winsound.Beep, 36, 75)
25 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
26
27 def test_extremes(self):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000028 self._beep(37, 75)
29 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000030
31 def test_increasingfrequency(self):
Amaury Forgeot d'Arc0a665ce2008-03-20 01:02:48 +000032 for i in range(100, 2000, 100):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000033 self._beep(i, 75)
34
35 def _beep(self, *args):
36 # these tests used to use _have_soundcard(), but it's quite
37 # possible to have a soundcard, and yet have the beep driver
38 # disabled. So basically, we have no way of knowing whether
39 # a beep should be produced or not, so currently if these
40 # tests fail we're ignoring them
41 #
42 # XXX the right fix for this is to define something like
43 # _have_enabled_beep_driver() and use that instead of the
44 # try/except below
45 try:
46 winsound.Beep(*args)
47 except RuntimeError:
48 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000049
50class MessageBeepTest(unittest.TestCase):
51
52 def tearDown(self):
53 time.sleep(0.5)
54
55 def test_default(self):
56 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
57 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
58 winsound.MessageBeep()
59
60 def test_ok(self):
61 winsound.MessageBeep(winsound.MB_OK)
62
63 def test_asterisk(self):
64 winsound.MessageBeep(winsound.MB_ICONASTERISK)
65
66 def test_exclamation(self):
67 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
68
69 def test_hand(self):
70 winsound.MessageBeep(winsound.MB_ICONHAND)
71
72 def test_question(self):
73 winsound.MessageBeep(winsound.MB_ICONQUESTION)
74
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000075
Walter Dörwald7fd94242003-05-18 00:47:47 +000076class PlaySoundTest(unittest.TestCase):
77
78 def test_errors(self):
79 self.assertRaises(TypeError, winsound.PlaySound)
80 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
81 self.assertRaises(
82 RuntimeError,
83 winsound.PlaySound,
84 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
85 )
86
87 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 if _have_soundcard():
89 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
90 else:
91 self.assertRaises(
92 RuntimeError,
93 winsound.PlaySound,
94 'SystemAsterisk', winsound.SND_ALIAS
95 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000096
97 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 if _have_soundcard():
99 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
100 else:
101 self.assertRaises(
102 RuntimeError,
103 winsound.PlaySound,
104 'SystemExclamation', winsound.SND_ALIAS
105 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000106
107 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108 if _have_soundcard():
109 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
110 else:
111 self.assertRaises(
112 RuntimeError,
113 winsound.PlaySound,
114 'SystemExit', winsound.SND_ALIAS
115 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000116
117 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 if _have_soundcard():
119 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
120 else:
121 self.assertRaises(
122 RuntimeError,
123 winsound.PlaySound,
124 'SystemHand', winsound.SND_ALIAS
125 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000126
127 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 if _have_soundcard():
129 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
130 else:
131 self.assertRaises(
132 RuntimeError,
133 winsound.PlaySound,
134 'SystemQuestion', winsound.SND_ALIAS
135 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000136
137 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000138 # This test can't be expected to work on all systems. The MS
139 # PlaySound() docs say:
140 #
141 # If it cannot find the specified sound, PlaySound uses the
142 # default system event sound entry instead. If the function
143 # can find neither the system default entry nor the default
144 # sound, it makes no sound and returns FALSE.
145 #
146 # It's known to return FALSE on some real systems.
147
148 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
149 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000150
151 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 if _have_soundcard():
153 # Note that this is not the same as asserting RuntimeError
154 # will get raised: you cannot convert this to
155 # self.assertRaises(...) form. The attempt may or may not
156 # raise RuntimeError, but it shouldn't raise anything other
157 # than RuntimeError, and that's all we're trying to test
158 # here. The MS docs aren't clear about whether the SDK
159 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
160 # True or False when the alias is unknown. On Tim's WinXP
161 # box today, it returns True (no exception is raised). What
162 # we'd really like to test is that no sound is played, but
163 # that requires first wiring an eardrum class into unittest
164 # <wink>.
165 try:
166 winsound.PlaySound(
167 '!"$%&/(#+*',
168 winsound.SND_ALIAS | winsound.SND_NODEFAULT
169 )
170 except RuntimeError:
171 pass
172 else:
173 self.assertRaises(
174 RuntimeError,
175 winsound.PlaySound,
176 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000177 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000178
179 def test_stopasync(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000180 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000181 winsound.PlaySound(
182 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000184 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185 time.sleep(0.5)
186 try:
187 winsound.PlaySound(
188 'SystemQuestion',
189 winsound.SND_ALIAS | winsound.SND_NOSTOP
190 )
191 except RuntimeError:
192 pass
193 else: # the first sound might already be finished
194 pass
195 winsound.PlaySound(None, winsound.SND_PURGE)
196 else:
197 self.assertRaises(
198 RuntimeError,
199 winsound.PlaySound,
200 None, winsound.SND_PURGE
201 )
202
203
204def _get_cscript_path():
205 """Return the full path to cscript.exe or None."""
206 for dir in os.environ.get("PATH", "").split(os.pathsep):
207 cscript_path = os.path.join(dir, "cscript.exe")
208 if os.path.exists(cscript_path):
209 return cscript_path
210
211__have_soundcard_cache = None
212def _have_soundcard():
213 """Return True iff this computer has a soundcard."""
214 global __have_soundcard_cache
215 if __have_soundcard_cache is None:
216 cscript_path = _get_cscript_path()
217 if cscript_path is None:
218 # Could not find cscript.exe to run our VBScript helper. Default
219 # to True: most computers these days *do* have a soundcard.
220 return True
221
222 check_script = os.path.join(os.path.dirname(__file__),
223 "check_soundcard.vbs")
224 p = subprocess.Popen([cscript_path, check_script],
225 stdout=subprocess.PIPE)
226 __have_soundcard_cache = not p.wait()
227 return __have_soundcard_cache
228
Walter Dörwald7fd94242003-05-18 00:47:47 +0000229
230def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000231 support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000232
233if __name__=="__main__":
234 test_main()