PageRenderTime 26ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/kbe/res/scripts/common/Lib/test/test_winsound.py

https://bitbucket.org/kbengine/kbengine
Python | 260 lines | 189 code | 30 blank | 41 comment | 26 complexity | 675d5c8947f8c4d8cc047e1b5e454d1b MD5 | raw file
  1. # Ridiculously simple test of the winsound module for Windows.
  2. import unittest
  3. from test import support
  4. support.requires('audio')
  5. import time
  6. import os
  7. import subprocess
  8. winsound = support.import_module('winsound')
  9. ctypes = support.import_module('ctypes')
  10. import winreg
  11. def has_sound(sound):
  12. """Find out if a particular event is configured with a default sound"""
  13. try:
  14. # Ask the mixer API for the number of devices it knows about.
  15. # When there are no devices, PlaySound will fail.
  16. if ctypes.windll.winmm.mixerGetNumDevs() is 0:
  17. return False
  18. key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER,
  19. "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound))
  20. value = winreg.EnumValue(key, 0)[1]
  21. if value is not "":
  22. return True
  23. else:
  24. return False
  25. except WindowsError:
  26. return False
  27. class BeepTest(unittest.TestCase):
  28. # As with PlaySoundTest, incorporate the _have_soundcard() check
  29. # into our test methods. If there's no audio device present,
  30. # winsound.Beep returns 0 and GetLastError() returns 127, which
  31. # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
  32. # be found"). (FWIW, virtual/Hyper-V systems fall under this
  33. # scenario as they have no sound devices whatsoever (not even
  34. # a legacy Beep device).)
  35. def test_errors(self):
  36. self.assertRaises(TypeError, winsound.Beep)
  37. self.assertRaises(ValueError, winsound.Beep, 36, 75)
  38. self.assertRaises(ValueError, winsound.Beep, 32768, 75)
  39. def test_extremes(self):
  40. self._beep(37, 75)
  41. self._beep(32767, 75)
  42. def test_increasingfrequency(self):
  43. for i in range(100, 2000, 100):
  44. self._beep(i, 75)
  45. def _beep(self, *args):
  46. # these tests used to use _have_soundcard(), but it's quite
  47. # possible to have a soundcard, and yet have the beep driver
  48. # disabled. So basically, we have no way of knowing whether
  49. # a beep should be produced or not, so currently if these
  50. # tests fail we're ignoring them
  51. #
  52. # XXX the right fix for this is to define something like
  53. # _have_enabled_beep_driver() and use that instead of the
  54. # try/except below
  55. try:
  56. winsound.Beep(*args)
  57. except RuntimeError:
  58. pass
  59. class MessageBeepTest(unittest.TestCase):
  60. def tearDown(self):
  61. time.sleep(0.5)
  62. def test_default(self):
  63. self.assertRaises(TypeError, winsound.MessageBeep, "bad")
  64. self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
  65. winsound.MessageBeep()
  66. def test_ok(self):
  67. winsound.MessageBeep(winsound.MB_OK)
  68. def test_asterisk(self):
  69. winsound.MessageBeep(winsound.MB_ICONASTERISK)
  70. def test_exclamation(self):
  71. winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
  72. def test_hand(self):
  73. winsound.MessageBeep(winsound.MB_ICONHAND)
  74. def test_question(self):
  75. winsound.MessageBeep(winsound.MB_ICONQUESTION)
  76. class PlaySoundTest(unittest.TestCase):
  77. def test_errors(self):
  78. self.assertRaises(TypeError, winsound.PlaySound)
  79. self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
  80. self.assertRaises(
  81. RuntimeError,
  82. winsound.PlaySound,
  83. "none", winsound.SND_ASYNC | winsound.SND_MEMORY
  84. )
  85. @unittest.skipUnless(has_sound("SystemAsterisk"),
  86. "No default SystemAsterisk")
  87. def test_alias_asterisk(self):
  88. 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. )
  96. @unittest.skipUnless(has_sound("SystemExclamation"),
  97. "No default SystemExclamation")
  98. def test_alias_exclamation(self):
  99. if _have_soundcard():
  100. winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
  101. else:
  102. self.assertRaises(
  103. RuntimeError,
  104. winsound.PlaySound,
  105. 'SystemExclamation', winsound.SND_ALIAS
  106. )
  107. @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
  108. def test_alias_exit(self):
  109. if _have_soundcard():
  110. winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
  111. else:
  112. self.assertRaises(
  113. RuntimeError,
  114. winsound.PlaySound,
  115. 'SystemExit', winsound.SND_ALIAS
  116. )
  117. @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
  118. def test_alias_hand(self):
  119. if _have_soundcard():
  120. winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
  121. else:
  122. self.assertRaises(
  123. RuntimeError,
  124. winsound.PlaySound,
  125. 'SystemHand', winsound.SND_ALIAS
  126. )
  127. @unittest.skipUnless(has_sound("SystemQuestion"),
  128. "No default SystemQuestion")
  129. def test_alias_question(self):
  130. if _have_soundcard():
  131. winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
  132. else:
  133. self.assertRaises(
  134. RuntimeError,
  135. winsound.PlaySound,
  136. 'SystemQuestion', winsound.SND_ALIAS
  137. )
  138. def test_alias_fallback(self):
  139. # This test can't be expected to work on all systems. The MS
  140. # PlaySound() docs say:
  141. #
  142. # If it cannot find the specified sound, PlaySound uses the
  143. # default system event sound entry instead. If the function
  144. # can find neither the system default entry nor the default
  145. # sound, it makes no sound and returns FALSE.
  146. #
  147. # It's known to return FALSE on some real systems.
  148. # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
  149. return
  150. def test_alias_nofallback(self):
  151. if _have_soundcard():
  152. # Note that this is not the same as asserting RuntimeError
  153. # will get raised: you cannot convert this to
  154. # self.assertRaises(...) form. The attempt may or may not
  155. # raise RuntimeError, but it shouldn't raise anything other
  156. # than RuntimeError, and that's all we're trying to test
  157. # here. The MS docs aren't clear about whether the SDK
  158. # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
  159. # True or False when the alias is unknown. On Tim's WinXP
  160. # box today, it returns True (no exception is raised). What
  161. # we'd really like to test is that no sound is played, but
  162. # that requires first wiring an eardrum class into unittest
  163. # <wink>.
  164. try:
  165. winsound.PlaySound(
  166. '!"$%&/(#+*',
  167. winsound.SND_ALIAS | winsound.SND_NODEFAULT
  168. )
  169. except RuntimeError:
  170. pass
  171. else:
  172. self.assertRaises(
  173. RuntimeError,
  174. winsound.PlaySound,
  175. '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
  176. )
  177. def test_stopasync(self):
  178. if _have_soundcard():
  179. winsound.PlaySound(
  180. 'SystemQuestion',
  181. winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
  182. )
  183. time.sleep(0.5)
  184. try:
  185. winsound.PlaySound(
  186. 'SystemQuestion',
  187. winsound.SND_ALIAS | winsound.SND_NOSTOP
  188. )
  189. except RuntimeError:
  190. pass
  191. else: # the first sound might already be finished
  192. pass
  193. winsound.PlaySound(None, winsound.SND_PURGE)
  194. else:
  195. # Issue 8367: PlaySound(None, winsound.SND_PURGE)
  196. # does not raise on systems without a sound card.
  197. pass
  198. def _get_cscript_path():
  199. """Return the full path to cscript.exe or None."""
  200. for dir in os.environ.get("PATH", "").split(os.pathsep):
  201. cscript_path = os.path.join(dir, "cscript.exe")
  202. if os.path.exists(cscript_path):
  203. return cscript_path
  204. __have_soundcard_cache = None
  205. def _have_soundcard():
  206. """Return True iff this computer has a soundcard."""
  207. global __have_soundcard_cache
  208. if __have_soundcard_cache is None:
  209. cscript_path = _get_cscript_path()
  210. if cscript_path is None:
  211. # Could not find cscript.exe to run our VBScript helper. Default
  212. # to True: most computers these days *do* have a soundcard.
  213. return True
  214. check_script = os.path.join(os.path.dirname(__file__),
  215. "check_soundcard.vbs")
  216. p = subprocess.Popen([cscript_path, check_script],
  217. stdout=subprocess.PIPE)
  218. __have_soundcard_cache = not p.wait()
  219. p.stdout.close()
  220. return __have_soundcard_cache
  221. def test_main():
  222. support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
  223. if __name__=="__main__":
  224. test_main()