+++ /dev/null
-# Ridiculously simple test of the winsound module for Windows.\r
-\r
-import unittest\r
-from test import test_support\r
-import time\r
-import os\r
-import subprocess\r
-\r
-winsound = test_support.import_module('winsound')\r
-ctypes = test_support.import_module('ctypes')\r
-import _winreg\r
-\r
-def has_sound(sound):\r
- """Find out if a particular event is configured with a default sound"""\r
- try:\r
- # Ask the mixer API for the number of devices it knows about.\r
- # When there are no devices, PlaySound will fail.\r
- if ctypes.windll.winmm.mixerGetNumDevs() is 0:\r
- return False\r
-\r
- key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER,\r
- "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))\r
- value = _winreg.EnumValue(key, 0)[1]\r
- if value is not u"":\r
- return True\r
- else:\r
- return False\r
- except WindowsError:\r
- return False\r
-\r
-class BeepTest(unittest.TestCase):\r
- # As with PlaySoundTest, incorporate the _have_soundcard() check\r
- # into our test methods. If there's no audio device present,\r
- # winsound.Beep returns 0 and GetLastError() returns 127, which\r
- # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not\r
- # be found"). (FWIW, virtual/Hyper-V systems fall under this\r
- # scenario as they have no sound devices whatsoever (not even\r
- # a legacy Beep device).)\r
-\r
- def test_errors(self):\r
- self.assertRaises(TypeError, winsound.Beep)\r
- self.assertRaises(ValueError, winsound.Beep, 36, 75)\r
- self.assertRaises(ValueError, winsound.Beep, 32768, 75)\r
-\r
- def test_extremes(self):\r
- self._beep(37, 75)\r
- self._beep(32767, 75)\r
-\r
- def test_increasingfrequency(self):\r
- for i in xrange(100, 2000, 100):\r
- self._beep(i, 75)\r
-\r
- def _beep(self, *args):\r
- # these tests used to use _have_soundcard(), but it's quite\r
- # possible to have a soundcard, and yet have the beep driver\r
- # disabled. So basically, we have no way of knowing whether\r
- # a beep should be produced or not, so currently if these\r
- # tests fail we're ignoring them\r
- #\r
- # XXX the right fix for this is to define something like\r
- # _have_enabled_beep_driver() and use that instead of the\r
- # try/except below\r
- try:\r
- winsound.Beep(*args)\r
- except RuntimeError:\r
- pass\r
-\r
-class MessageBeepTest(unittest.TestCase):\r
-\r
- def tearDown(self):\r
- time.sleep(0.5)\r
-\r
- def test_default(self):\r
- self.assertRaises(TypeError, winsound.MessageBeep, "bad")\r
- self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)\r
- winsound.MessageBeep()\r
-\r
- def test_ok(self):\r
- winsound.MessageBeep(winsound.MB_OK)\r
-\r
- def test_asterisk(self):\r
- winsound.MessageBeep(winsound.MB_ICONASTERISK)\r
-\r
- def test_exclamation(self):\r
- winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)\r
-\r
- def test_hand(self):\r
- winsound.MessageBeep(winsound.MB_ICONHAND)\r
-\r
- def test_question(self):\r
- winsound.MessageBeep(winsound.MB_ICONQUESTION)\r
-\r
-\r
-class PlaySoundTest(unittest.TestCase):\r
-\r
- def test_errors(self):\r
- self.assertRaises(TypeError, winsound.PlaySound)\r
- self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- "none", winsound.SND_ASYNC | winsound.SND_MEMORY\r
- )\r
-\r
- @unittest.skipUnless(has_sound("SystemAsterisk"), "No default SystemAsterisk")\r
- def test_alias_asterisk(self):\r
- if _have_soundcard():\r
- winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)\r
- else:\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- 'SystemAsterisk', winsound.SND_ALIAS\r
- )\r
-\r
- @unittest.skipUnless(has_sound("SystemExclamation"), "No default SystemExclamation")\r
- def test_alias_exclamation(self):\r
- if _have_soundcard():\r
- winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)\r
- else:\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- 'SystemExclamation', winsound.SND_ALIAS\r
- )\r
-\r
- @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")\r
- def test_alias_exit(self):\r
- if _have_soundcard():\r
- winsound.PlaySound('SystemExit', winsound.SND_ALIAS)\r
- else:\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- 'SystemExit', winsound.SND_ALIAS\r
- )\r
-\r
- @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")\r
- def test_alias_hand(self):\r
- if _have_soundcard():\r
- winsound.PlaySound('SystemHand', winsound.SND_ALIAS)\r
- else:\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- 'SystemHand', winsound.SND_ALIAS\r
- )\r
-\r
- @unittest.skipUnless(has_sound("SystemQuestion"), "No default SystemQuestion")\r
- def test_alias_question(self):\r
- if _have_soundcard():\r
- winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)\r
- else:\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- 'SystemQuestion', winsound.SND_ALIAS\r
- )\r
-\r
- def test_alias_fallback(self):\r
- # This test can't be expected to work on all systems. The MS\r
- # PlaySound() docs say:\r
- #\r
- # If it cannot find the specified sound, PlaySound uses the\r
- # default system event sound entry instead. If the function\r
- # can find neither the system default entry nor the default\r
- # sound, it makes no sound and returns FALSE.\r
- #\r
- # It's known to return FALSE on some real systems.\r
-\r
- # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)\r
- return\r
-\r
- def test_alias_nofallback(self):\r
- if _have_soundcard():\r
- # Note that this is not the same as asserting RuntimeError\r
- # will get raised: you cannot convert this to\r
- # self.assertRaises(...) form. The attempt may or may not\r
- # raise RuntimeError, but it shouldn't raise anything other\r
- # than RuntimeError, and that's all we're trying to test\r
- # here. The MS docs aren't clear about whether the SDK\r
- # PlaySound() with SND_ALIAS and SND_NODEFAULT will return\r
- # True or False when the alias is unknown. On Tim's WinXP\r
- # box today, it returns True (no exception is raised). What\r
- # we'd really like to test is that no sound is played, but\r
- # that requires first wiring an eardrum class into unittest\r
- # <wink>.\r
- try:\r
- winsound.PlaySound(\r
- '!"$%&/(#+*',\r
- winsound.SND_ALIAS | winsound.SND_NODEFAULT\r
- )\r
- except RuntimeError:\r
- pass\r
- else:\r
- self.assertRaises(\r
- RuntimeError,\r
- winsound.PlaySound,\r
- '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT\r
- )\r
-\r
- def test_stopasync(self):\r
- if _have_soundcard():\r
- winsound.PlaySound(\r
- 'SystemQuestion',\r
- winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP\r
- )\r
- time.sleep(0.5)\r
- try:\r
- winsound.PlaySound(\r
- 'SystemQuestion',\r
- winsound.SND_ALIAS | winsound.SND_NOSTOP\r
- )\r
- except RuntimeError:\r
- pass\r
- else: # the first sound might already be finished\r
- pass\r
- winsound.PlaySound(None, winsound.SND_PURGE)\r
- else:\r
- # Issue 8367: PlaySound(None, winsound.SND_PURGE)\r
- # does not raise on systems without a sound card.\r
- pass\r
-\r
-\r
-def _get_cscript_path():\r
- """Return the full path to cscript.exe or None."""\r
- for dir in os.environ.get("PATH", "").split(os.pathsep):\r
- cscript_path = os.path.join(dir, "cscript.exe")\r
- if os.path.exists(cscript_path):\r
- return cscript_path\r
-\r
-__have_soundcard_cache = None\r
-def _have_soundcard():\r
- """Return True iff this computer has a soundcard."""\r
- global __have_soundcard_cache\r
- if __have_soundcard_cache is None:\r
- cscript_path = _get_cscript_path()\r
- if cscript_path is None:\r
- # Could not find cscript.exe to run our VBScript helper. Default\r
- # to True: most computers these days *do* have a soundcard.\r
- return True\r
-\r
- check_script = os.path.join(os.path.dirname(__file__),\r
- "check_soundcard.vbs")\r
- p = subprocess.Popen([cscript_path, check_script],\r
- stdout=subprocess.PIPE)\r
- __have_soundcard_cache = not p.wait()\r
- return __have_soundcard_cache\r
-\r
-\r
-def test_main():\r
- test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)\r
-\r
-if __name__=="__main__":\r
- test_main()\r