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

/contrib/python-x86_64/lib/python2.7/test/test_winsound.py

https://gitlab.com/atom-k/android-plus-plus
Python | 256 lines | 185 code | 30 blank | 41 comment | 26 complexity | f7162ac2da3b72986d6955b01f9c468f MD5 | raw file
  1. # Ridiculously simple test of the winsound module for Windows.
  2. import unittest
  3. from test import test_support
  4. test_support.requires('audio')
  5. import time
  6. import os
  7. import subprocess
  8. winsound = test_support.import_module('winsound')
  9. ctypes = test_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 u"":
  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 xrange(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"), "No default SystemAsterisk")
  86. def test_alias_asterisk(self):
  87. if _have_soundcard():
  88. winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
  89. else:
  90. self.assertRaises(
  91. RuntimeError,
  92. winsound.PlaySound,
  93. 'SystemAsterisk', winsound.SND_ALIAS
  94. )
  95. @unittest.skipUnless(has_sound("SystemExclamation"), "No default SystemExclamation")
  96. def test_alias_exclamation(self):
  97. if _have_soundcard():
  98. winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
  99. else:
  100. self.assertRaises(
  101. RuntimeError,
  102. winsound.PlaySound,
  103. 'SystemExclamation', winsound.SND_ALIAS
  104. )
  105. @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit")
  106. def test_alias_exit(self):
  107. if _have_soundcard():
  108. winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
  109. else:
  110. self.assertRaises(
  111. RuntimeError,
  112. winsound.PlaySound,
  113. 'SystemExit', winsound.SND_ALIAS
  114. )
  115. @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand")
  116. def test_alias_hand(self):
  117. if _have_soundcard():
  118. winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
  119. else:
  120. self.assertRaises(
  121. RuntimeError,
  122. winsound.PlaySound,
  123. 'SystemHand', winsound.SND_ALIAS
  124. )
  125. @unittest.skipUnless(has_sound("SystemQuestion"), "No default SystemQuestion")
  126. def test_alias_question(self):
  127. if _have_soundcard():
  128. winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
  129. else:
  130. self.assertRaises(
  131. RuntimeError,
  132. winsound.PlaySound,
  133. 'SystemQuestion', winsound.SND_ALIAS
  134. )
  135. def test_alias_fallback(self):
  136. # This test can't be expected to work on all systems. The MS
  137. # PlaySound() docs say:
  138. #
  139. # If it cannot find the specified sound, PlaySound uses the
  140. # default system event sound entry instead. If the function
  141. # can find neither the system default entry nor the default
  142. # sound, it makes no sound and returns FALSE.
  143. #
  144. # It's known to return FALSE on some real systems.
  145. # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
  146. return
  147. def test_alias_nofallback(self):
  148. if _have_soundcard():
  149. # Note that this is not the same as asserting RuntimeError
  150. # will get raised: you cannot convert this to
  151. # self.assertRaises(...) form. The attempt may or may not
  152. # raise RuntimeError, but it shouldn't raise anything other
  153. # than RuntimeError, and that's all we're trying to test
  154. # here. The MS docs aren't clear about whether the SDK
  155. # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
  156. # True or False when the alias is unknown. On Tim's WinXP
  157. # box today, it returns True (no exception is raised). What
  158. # we'd really like to test is that no sound is played, but
  159. # that requires first wiring an eardrum class into unittest
  160. # <wink>.
  161. try:
  162. winsound.PlaySound(
  163. '!"$%&/(#+*',
  164. winsound.SND_ALIAS | winsound.SND_NODEFAULT
  165. )
  166. except RuntimeError:
  167. pass
  168. else:
  169. self.assertRaises(
  170. RuntimeError,
  171. winsound.PlaySound,
  172. '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
  173. )
  174. def test_stopasync(self):
  175. if _have_soundcard():
  176. winsound.PlaySound(
  177. 'SystemQuestion',
  178. winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
  179. )
  180. time.sleep(0.5)
  181. try:
  182. winsound.PlaySound(
  183. 'SystemQuestion',
  184. winsound.SND_ALIAS | winsound.SND_NOSTOP
  185. )
  186. except RuntimeError:
  187. pass
  188. else: # the first sound might already be finished
  189. pass
  190. winsound.PlaySound(None, winsound.SND_PURGE)
  191. else:
  192. # Issue 8367: PlaySound(None, winsound.SND_PURGE)
  193. # does not raise on systems without a sound card.
  194. pass
  195. def _get_cscript_path():
  196. """Return the full path to cscript.exe or None."""
  197. for dir in os.environ.get("PATH", "").split(os.pathsep):
  198. cscript_path = os.path.join(dir, "cscript.exe")
  199. if os.path.exists(cscript_path):
  200. return cscript_path
  201. __have_soundcard_cache = None
  202. def _have_soundcard():
  203. """Return True iff this computer has a soundcard."""
  204. global __have_soundcard_cache
  205. if __have_soundcard_cache is None:
  206. cscript_path = _get_cscript_path()
  207. if cscript_path is None:
  208. # Could not find cscript.exe to run our VBScript helper. Default
  209. # to True: most computers these days *do* have a soundcard.
  210. return True
  211. check_script = os.path.join(os.path.dirname(__file__),
  212. "check_soundcard.vbs")
  213. p = subprocess.Popen([cscript_path, check_script],
  214. stdout=subprocess.PIPE)
  215. __have_soundcard_cache = not p.wait()
  216. return __have_soundcard_cache
  217. def test_main():
  218. test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
  219. if __name__=="__main__":
  220. test_main()