cpython/Lib/test/test_winsound.py

123 lines
3.5 KiB
Python
Raw Normal View History

# Ridiculously simple test of the winsound module for Windows.
2000-04-21 18:28:47 -03:00
import functools
import os
import subprocess
import time
import unittest
from test import support
support.requires('audio')
Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge ........ r70734 | r.david.murray | 2009-03-30 15:04:00 -0400 (Mon, 30 Mar 2009) | 7 lines Add import_function method to test.test_support, and modify a number of tests that expect to be skipped if imports fail or functions don't exist to use import_function and import_module. The ultimate goal is to change regrtest to not skip automatically on ImportError. Checking in now to make sure the buldbots don't show any errors on platforms I can't direct test on. ........ r70775 | r.david.murray | 2009-03-30 19:05:48 -0400 (Mon, 30 Mar 2009) | 4 lines Change more tests to use import_module for the modules that should cause tests to be skipped. Also rename import_function to the more descriptive get_attribute and add a docstring. ........ r70856 | r.david.murray | 2009-03-31 14:32:17 -0400 (Tue, 31 Mar 2009) | 7 lines A few more test skips via import_module, and change import_module to return the error message produced by importlib, so that if an import in the package whose import is being wrapped is what failed the skip message will contain the name of that module instead of the name of the wrapped module. Also fixed formatting of some previous comments. ........ r70874 | r.david.murray | 2009-03-31 15:33:15 -0400 (Tue, 31 Mar 2009) | 5 lines Improve test_support.import_module docstring, remove deprecated flag from get_attribute since it isn't likely to do anything useful. ........ r70876 | r.david.murray | 2009-03-31 15:49:15 -0400 (Tue, 31 Mar 2009) | 4 lines Remove the regrtest check that turns any ImportError into a skipped test. Hopefully all modules whose imports legitimately result in a skipped test have been properly wrapped by the previous commits. ........ r70877 | r.david.murray | 2009-03-31 15:57:24 -0400 (Tue, 31 Mar 2009) | 2 lines Add NEWS entry for regrtest change. ........
2009-03-31 20:16:50 -03:00
winsound = support.import_module('winsound')
# Unless we actually have an ear in the room, we have no idea whether a sound
# actually plays, and it's incredibly flaky trying to figure out if a sound
# even *should* play. Instead of guessing, just call the function and assume
# it either passed or raised the RuntimeError we expect in case of failure.
def sound_func(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
ret = func(*args, **kwargs)
except RuntimeError as e:
if support.verbose:
print(func.__name__, 'failed:', e)
else:
if support.verbose:
print(func.__name__, 'returned')
return ret
return wrapper
safe_Beep = sound_func(winsound.Beep)
safe_MessageBeep = sound_func(winsound.MessageBeep)
safe_PlaySound = sound_func(winsound.PlaySound)
class BeepTest(unittest.TestCase):
def test_errors(self):
self.assertRaises(TypeError, winsound.Beep)
self.assertRaises(ValueError, winsound.Beep, 36, 75)
self.assertRaises(ValueError, winsound.Beep, 32768, 75)
def test_extremes(self):
safe_Beep(37, 75)
safe_Beep(32767, 75)
def test_increasingfrequency(self):
2008-03-19 22:02:48 -03:00
for i in range(100, 2000, 100):
safe_Beep(i, 75)
class MessageBeepTest(unittest.TestCase):
def tearDown(self):
time.sleep(0.5)
def test_default(self):
self.assertRaises(TypeError, winsound.MessageBeep, "bad")
self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
safe_MessageBeep()
def test_ok(self):
safe_MessageBeep(winsound.MB_OK)
def test_asterisk(self):
safe_MessageBeep(winsound.MB_ICONASTERISK)
def test_exclamation(self):
safe_MessageBeep(winsound.MB_ICONEXCLAMATION)
def test_hand(self):
safe_MessageBeep(winsound.MB_ICONHAND)
def test_question(self):
safe_MessageBeep(winsound.MB_ICONQUESTION)
class PlaySoundTest(unittest.TestCase):
def test_errors(self):
self.assertRaises(TypeError, winsound.PlaySound)
self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
self.assertRaises(
RuntimeError,
winsound.PlaySound,
"none", winsound.SND_ASYNC | winsound.SND_MEMORY
)
def test_aliases(self):
aliases = [
"SystemAsterisk",
"SystemExclamation",
"SystemExit",
"SystemHand",
"SystemQuestion",
]
for alias in aliases:
with self.subTest(alias=alias):
safe_PlaySound(alias, winsound.SND_ALIAS)
def test_alias_fallback(self):
safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
def test_alias_nofallback(self):
safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT)
def test_stopasync(self):
safe_PlaySound(
'SystemQuestion',
winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
)
time.sleep(0.5)
safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP)
# Issue 8367: PlaySound(None, winsound.SND_PURGE)
# does not raise on systems without a sound card.
winsound.PlaySound(None, winsound.SND_PURGE)
if __name__ == "__main__":
unittest.main()