Update test_winsound to check for a configured sound card (using a VBScript
helper written by Roger Upole and Mark Hammond) and adjust the expected PlaySoundTest case results accordingly.
This commit is contained in:
parent
4fe4ed2525
commit
f8cf13eeb7
|
@ -0,0 +1,13 @@
|
||||||
|
rem Check for a working sound-card - exit with 0 if OK, 1 otherwise.
|
||||||
|
set wmi = GetObject("winmgmts:")
|
||||||
|
set scs = wmi.InstancesOf("win32_sounddevice")
|
||||||
|
for each sc in scs
|
||||||
|
set status = sc.Properties_("Status")
|
||||||
|
wscript.Echo(sc.Properties_("Name") + "/" + status)
|
||||||
|
if status = "OK" then
|
||||||
|
wscript.Quit 0 rem normal exit
|
||||||
|
end if
|
||||||
|
next
|
||||||
|
rem No sound card found - exit with status code of 1
|
||||||
|
wscript.Quit 1
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
import unittest
|
import unittest
|
||||||
from test import test_support
|
from test import test_support
|
||||||
import winsound, time
|
import winsound, time
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
class BeepTest(unittest.TestCase):
|
class BeepTest(unittest.TestCase):
|
||||||
|
|
||||||
|
@ -44,6 +47,7 @@ class MessageBeepTest(unittest.TestCase):
|
||||||
def test_question(self):
|
def test_question(self):
|
||||||
winsound.MessageBeep(winsound.MB_ICONQUESTION)
|
winsound.MessageBeep(winsound.MB_ICONQUESTION)
|
||||||
|
|
||||||
|
|
||||||
class PlaySoundTest(unittest.TestCase):
|
class PlaySoundTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_errors(self):
|
def test_errors(self):
|
||||||
|
@ -56,19 +60,54 @@ class PlaySoundTest(unittest.TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_alias_asterisk(self):
|
def test_alias_asterisk(self):
|
||||||
winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
|
if _have_soundcard():
|
||||||
|
winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
'SystemAsterisk', winsound.SND_ALIAS
|
||||||
|
)
|
||||||
|
|
||||||
def test_alias_exclamation(self):
|
def test_alias_exclamation(self):
|
||||||
winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
|
if _have_soundcard():
|
||||||
|
winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
'SystemExclamation', winsound.SND_ALIAS
|
||||||
|
)
|
||||||
|
|
||||||
def test_alias_exit(self):
|
def test_alias_exit(self):
|
||||||
winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
|
if _have_soundcard():
|
||||||
|
winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
'SystemExit', winsound.SND_ALIAS
|
||||||
|
)
|
||||||
|
|
||||||
def test_alias_hand(self):
|
def test_alias_hand(self):
|
||||||
winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
|
if _have_soundcard():
|
||||||
|
winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
'SystemHand', winsound.SND_ALIAS
|
||||||
|
)
|
||||||
|
|
||||||
def test_alias_question(self):
|
def test_alias_question(self):
|
||||||
winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
|
if _have_soundcard():
|
||||||
|
winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
'SystemQuestion', winsound.SND_ALIAS
|
||||||
|
)
|
||||||
|
|
||||||
def test_alias_fallback(self):
|
def test_alias_fallback(self):
|
||||||
# This test can't be expected to work on all systems. The MS
|
# This test can't be expected to work on all systems. The MS
|
||||||
|
@ -85,41 +124,83 @@ class PlaySoundTest(unittest.TestCase):
|
||||||
return
|
return
|
||||||
|
|
||||||
def test_alias_nofallback(self):
|
def test_alias_nofallback(self):
|
||||||
# Note that this is not the same as asserting RuntimeError
|
if _have_soundcard():
|
||||||
# will get raised: you cannot convert this to
|
# Note that this is not the same as asserting RuntimeError
|
||||||
# self.assertRaises(...) form. The attempt may or may not
|
# will get raised: you cannot convert this to
|
||||||
# raise RuntimeError, but it shouldn't raise anything other
|
# self.assertRaises(...) form. The attempt may or may not
|
||||||
# than RuntimeError, and that's all we're trying to test here.
|
# raise RuntimeError, but it shouldn't raise anything other
|
||||||
# The MS docs aren't clear about whether the SDK PlaySound()
|
# than RuntimeError, and that's all we're trying to test
|
||||||
# with SND_ALIAS and SND_NODEFAULT will return True or False when
|
# here. The MS docs aren't clear about whether the SDK
|
||||||
# the alias is unknown. On Tim's WinXP box today, it returns
|
# PlaySound() with SND_ALIAS and SND_NODEFAULT will return
|
||||||
# True (no exception is raised). What we'd really like to test
|
# True or False when the alias is unknown. On Tim's WinXP
|
||||||
# is that no sound is played, but that requires first wiring an
|
# box today, it returns True (no exception is raised). What
|
||||||
# eardrum class into unittest <wink>.
|
# we'd really like to test is that no sound is played, but
|
||||||
try:
|
# that requires first wiring an eardrum class into unittest
|
||||||
winsound.PlaySound(
|
# <wink>.
|
||||||
'!"$%&/(#+*',
|
try:
|
||||||
winsound.SND_ALIAS | winsound.SND_NODEFAULT
|
winsound.PlaySound(
|
||||||
|
'!"$%&/(#+*',
|
||||||
|
winsound.SND_ALIAS | winsound.SND_NODEFAULT
|
||||||
|
)
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
'!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
|
||||||
)
|
)
|
||||||
except RuntimeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def test_stopasync(self):
|
def test_stopasync(self):
|
||||||
winsound.PlaySound(
|
if _have_soundcard():
|
||||||
'SystemQuestion',
|
|
||||||
winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
|
|
||||||
)
|
|
||||||
time.sleep(0.5)
|
|
||||||
try:
|
|
||||||
winsound.PlaySound(
|
winsound.PlaySound(
|
||||||
'SystemQuestion',
|
'SystemQuestion',
|
||||||
winsound.SND_ALIAS | winsound.SND_NOSTOP
|
winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
|
||||||
)
|
)
|
||||||
except RuntimeError:
|
time.sleep(0.5)
|
||||||
pass
|
try:
|
||||||
else: # the first sound might already be finished
|
winsound.PlaySound(
|
||||||
pass
|
'SystemQuestion',
|
||||||
winsound.PlaySound(None, winsound.SND_PURGE)
|
winsound.SND_ALIAS | winsound.SND_NOSTOP
|
||||||
|
)
|
||||||
|
except RuntimeError:
|
||||||
|
pass
|
||||||
|
else: # the first sound might already be finished
|
||||||
|
pass
|
||||||
|
winsound.PlaySound(None, winsound.SND_PURGE)
|
||||||
|
else:
|
||||||
|
self.assertRaises(
|
||||||
|
RuntimeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
None, winsound.SND_PURGE
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_cscript_path():
|
||||||
|
"""Return the full path to cscript.exe or None."""
|
||||||
|
for dir in os.environ.get("PATH", "").split(os.pathsep):
|
||||||
|
cscript_path = os.path.join(dir, "cscript.exe")
|
||||||
|
if os.path.exists(cscript_path):
|
||||||
|
return cscript_path
|
||||||
|
|
||||||
|
__have_soundcard_cache = None
|
||||||
|
def _have_soundcard():
|
||||||
|
"""Return True iff this computer has a soundcard."""
|
||||||
|
global __have_soundcard_cache
|
||||||
|
if __have_soundcard_cache is None:
|
||||||
|
cscript_path = _get_cscript_path()
|
||||||
|
if cscript_path is None:
|
||||||
|
# Could not find cscript.exe to run our VBScript helper. Default
|
||||||
|
# to True: most computers these days *do* have a soundcard.
|
||||||
|
return True
|
||||||
|
|
||||||
|
check_script = os.path.join(os.path.dirname(__file__),
|
||||||
|
"check_soundcard.vbs")
|
||||||
|
p = subprocess.Popen([cscript_path, check_script],
|
||||||
|
stdout=subprocess.PIPE)
|
||||||
|
__have_soundcard_cache = not p.wait()
|
||||||
|
return __have_soundcard_cache
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
|
test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
|
||||||
|
|
Loading…
Reference in New Issue