1 # Ridiculously simple test of the winsound module for Windows.
4 from test
import test_support
10 class BeepTest(unittest
.TestCase
):
11 # As with PlaySoundTest, incorporate the _have_soundcard() check
12 # into our test methods. If there's no audio device present,
13 # winsound.Beep returns 0 and GetLastError() returns 127, which
14 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
15 # be found"). (FWIW, virtual/Hyper-V systems fall under this
16 # scenario as they have no sound devices whatsoever (not even
17 # a legacy Beep device).)
19 def test_errors(self
):
20 self
.assertRaises(TypeError, winsound
.Beep
)
21 self
.assertRaises(ValueError, winsound
.Beep
, 36, 75)
22 self
.assertRaises(ValueError, winsound
.Beep
, 32768, 75)
24 def test_extremes(self
):
28 def test_increasingfrequency(self
):
29 for i
in xrange(100, 2000, 100):
32 def _beep(self
, *args
):
33 # these tests used to use _have_soundcard(), but it's quite
34 # possible to have a soundcard, and yet have the beep driver
35 # disabled. So basically, we have no way of knowing whether
36 # a beep should be produced or not, so currently if these
37 # tests fail we're ignoring them
39 # XXX the right fix for this is to define something like
40 # _have_enabled_beep_driver() and use that instead of the
47 class MessageBeepTest(unittest
.TestCase
):
52 def test_default(self
):
53 self
.assertRaises(TypeError, winsound
.MessageBeep
, "bad")
54 self
.assertRaises(TypeError, winsound
.MessageBeep
, 42, 42)
55 winsound
.MessageBeep()
58 winsound
.MessageBeep(winsound
.MB_OK
)
60 def test_asterisk(self
):
61 winsound
.MessageBeep(winsound
.MB_ICONASTERISK
)
63 def test_exclamation(self
):
64 winsound
.MessageBeep(winsound
.MB_ICONEXCLAMATION
)
67 winsound
.MessageBeep(winsound
.MB_ICONHAND
)
69 def test_question(self
):
70 winsound
.MessageBeep(winsound
.MB_ICONQUESTION
)
73 class PlaySoundTest(unittest
.TestCase
):
75 def test_errors(self
):
76 self
.assertRaises(TypeError, winsound
.PlaySound
)
77 self
.assertRaises(TypeError, winsound
.PlaySound
, "bad", "bad")
81 "none", winsound
.SND_ASYNC | winsound
.SND_MEMORY
84 def test_alias_asterisk(self
):
86 winsound
.PlaySound('SystemAsterisk', winsound
.SND_ALIAS
)
91 'SystemAsterisk', winsound
.SND_ALIAS
94 def test_alias_exclamation(self
):
96 winsound
.PlaySound('SystemExclamation', winsound
.SND_ALIAS
)
101 'SystemExclamation', winsound
.SND_ALIAS
104 def test_alias_exit(self
):
105 if _have_soundcard():
106 winsound
.PlaySound('SystemExit', winsound
.SND_ALIAS
)
111 'SystemExit', winsound
.SND_ALIAS
114 def test_alias_hand(self
):
115 if _have_soundcard():
116 winsound
.PlaySound('SystemHand', winsound
.SND_ALIAS
)
121 'SystemHand', winsound
.SND_ALIAS
124 def test_alias_question(self
):
125 if _have_soundcard():
126 winsound
.PlaySound('SystemQuestion', winsound
.SND_ALIAS
)
131 'SystemQuestion', winsound
.SND_ALIAS
134 def test_alias_fallback(self
):
135 # This test can't be expected to work on all systems. The MS
136 # PlaySound() docs say:
138 # If it cannot find the specified sound, PlaySound uses the
139 # default system event sound entry instead. If the function
140 # can find neither the system default entry nor the default
141 # sound, it makes no sound and returns FALSE.
143 # It's known to return FALSE on some real systems.
145 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
148 def test_alias_nofallback(self
):
149 if _have_soundcard():
150 # Note that this is not the same as asserting RuntimeError
151 # will get raised: you cannot convert this to
152 # self.assertRaises(...) form. The attempt may or may not
153 # raise RuntimeError, but it shouldn't raise anything other
154 # than RuntimeError, and that's all we're trying to test
155 # here. The MS docs aren't clear about whether the SDK
156 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
157 # True or False when the alias is unknown. On Tim's WinXP
158 # box today, it returns True (no exception is raised). What
159 # we'd really like to test is that no sound is played, but
160 # that requires first wiring an eardrum class into unittest
165 winsound
.SND_ALIAS | winsound
.SND_NODEFAULT
173 '!"$%&/(#+*', winsound
.SND_ALIAS | winsound
.SND_NODEFAULT
176 def test_stopasync(self
):
177 if _have_soundcard():
180 winsound
.SND_ALIAS | winsound
.SND_ASYNC | winsound
.SND_LOOP
186 winsound
.SND_ALIAS | winsound
.SND_NOSTOP
190 else: # the first sound might already be finished
192 winsound
.PlaySound(None, winsound
.SND_PURGE
)
197 None, winsound
.SND_PURGE
201 def _get_cscript_path():
202 """Return the full path to cscript.exe or None."""
203 for dir in os
.environ
.get("PATH", "").split(os
.pathsep
):
204 cscript_path
= os
.path
.join(dir, "cscript.exe")
205 if os
.path
.exists(cscript_path
):
208 __have_soundcard_cache
= None
209 def _have_soundcard():
210 """Return True iff this computer has a soundcard."""
211 global __have_soundcard_cache
212 if __have_soundcard_cache
is None:
213 cscript_path
= _get_cscript_path()
214 if cscript_path
is None:
215 # Could not find cscript.exe to run our VBScript helper. Default
216 # to True: most computers these days *do* have a soundcard.
219 check_script
= os
.path
.join(os
.path
.dirname(__file__
),
220 "check_soundcard.vbs")
221 p
= subprocess
.Popen([cscript_path
, check_script
],
222 stdout
=subprocess
.PIPE
)
223 __have_soundcard_cache
= not p
.wait()
224 return __have_soundcard_cache
228 test_support
.run_unittest(BeepTest
, MessageBeepTest
, PlaySoundTest
)
230 if __name__
=="__main__":