/xbmc/lib/libPython/Python/Lib/test/test_winsound.py

https://github.com/tmacreturns/XBMC_wireless_setup · Python · 209 lines · 149 code · 33 blank · 27 comment · 25 complexity · f63e97c74606ed5298e73f1493f7c714 MD5 · raw file

  1. # Ridiculously simple test of the winsound module for Windows.
  2. import unittest
  3. from test import test_support
  4. import winsound, time
  5. import os
  6. import subprocess
  7. class BeepTest(unittest.TestCase):
  8. def test_errors(self):
  9. self.assertRaises(TypeError, winsound.Beep)
  10. self.assertRaises(ValueError, winsound.Beep, 36, 75)
  11. self.assertRaises(ValueError, winsound.Beep, 32768, 75)
  12. def test_extremes(self):
  13. winsound.Beep(37, 75)
  14. winsound.Beep(32767, 75)
  15. def test_increasingfrequency(self):
  16. for i in xrange(100, 2000, 100):
  17. winsound.Beep(i, 75)
  18. class MessageBeepTest(unittest.TestCase):
  19. def tearDown(self):
  20. time.sleep(0.5)
  21. def test_default(self):
  22. self.assertRaises(TypeError, winsound.MessageBeep, "bad")
  23. self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
  24. winsound.MessageBeep()
  25. def test_ok(self):
  26. winsound.MessageBeep(winsound.MB_OK)
  27. def test_asterisk(self):
  28. winsound.MessageBeep(winsound.MB_ICONASTERISK)
  29. def test_exclamation(self):
  30. winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
  31. def test_hand(self):
  32. winsound.MessageBeep(winsound.MB_ICONHAND)
  33. def test_question(self):
  34. winsound.MessageBeep(winsound.MB_ICONQUESTION)
  35. class PlaySoundTest(unittest.TestCase):
  36. def test_errors(self):
  37. self.assertRaises(TypeError, winsound.PlaySound)
  38. self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
  39. self.assertRaises(
  40. RuntimeError,
  41. winsound.PlaySound,
  42. "none", winsound.SND_ASYNC | winsound.SND_MEMORY
  43. )
  44. def test_alias_asterisk(self):
  45. if _have_soundcard():
  46. winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
  47. else:
  48. self.assertRaises(
  49. RuntimeError,
  50. winsound.PlaySound,
  51. 'SystemAsterisk', winsound.SND_ALIAS
  52. )
  53. def test_alias_exclamation(self):
  54. if _have_soundcard():
  55. winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
  56. else:
  57. self.assertRaises(
  58. RuntimeError,
  59. winsound.PlaySound,
  60. 'SystemExclamation', winsound.SND_ALIAS
  61. )
  62. def test_alias_exit(self):
  63. if _have_soundcard():
  64. winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
  65. else:
  66. self.assertRaises(
  67. RuntimeError,
  68. winsound.PlaySound,
  69. 'SystemExit', winsound.SND_ALIAS
  70. )
  71. def test_alias_hand(self):
  72. if _have_soundcard():
  73. winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
  74. else:
  75. self.assertRaises(
  76. RuntimeError,
  77. winsound.PlaySound,
  78. 'SystemHand', winsound.SND_ALIAS
  79. )
  80. def test_alias_question(self):
  81. if _have_soundcard():
  82. winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
  83. else:
  84. self.assertRaises(
  85. RuntimeError,
  86. winsound.PlaySound,
  87. 'SystemQuestion', winsound.SND_ALIAS
  88. )
  89. def test_alias_fallback(self):
  90. # This test can't be expected to work on all systems. The MS
  91. # PlaySound() docs say:
  92. #
  93. # If it cannot find the specified sound, PlaySound uses the
  94. # default system event sound entry instead. If the function
  95. # can find neither the system default entry nor the default
  96. # sound, it makes no sound and returns FALSE.
  97. #
  98. # It's known to return FALSE on some real systems.
  99. # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
  100. return
  101. def test_alias_nofallback(self):
  102. if _have_soundcard():
  103. # Note that this is not the same as asserting RuntimeError
  104. # will get raised: you cannot convert this to
  105. # self.assertRaises(...) form. The attempt may or may not
  106. # raise RuntimeError, but it shouldn't raise anything other
  107. # than RuntimeError, and that's all we're trying to test
  108. # here. The MS docs aren't clear about whether the SDK
  109. # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
  110. # True or False when the alias is unknown. On Tim's WinXP
  111. # box today, it returns True (no exception is raised). What
  112. # we'd really like to test is that no sound is played, but
  113. # that requires first wiring an eardrum class into unittest
  114. # <wink>.
  115. try:
  116. winsound.PlaySound(
  117. '!"$%&/(#+*',
  118. winsound.SND_ALIAS | winsound.SND_NODEFAULT
  119. )
  120. except RuntimeError:
  121. pass
  122. else:
  123. self.assertRaises(
  124. RuntimeError,
  125. winsound.PlaySound,
  126. '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
  127. )
  128. def test_stopasync(self):
  129. if _have_soundcard():
  130. winsound.PlaySound(
  131. 'SystemQuestion',
  132. winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
  133. )
  134. time.sleep(0.5)
  135. try:
  136. winsound.PlaySound(
  137. 'SystemQuestion',
  138. winsound.SND_ALIAS | winsound.SND_NOSTOP
  139. )
  140. except RuntimeError:
  141. pass
  142. else: # the first sound might already be finished
  143. pass
  144. winsound.PlaySound(None, winsound.SND_PURGE)
  145. else:
  146. self.assertRaises(
  147. RuntimeError,
  148. winsound.PlaySound,
  149. None, winsound.SND_PURGE
  150. )
  151. def _get_cscript_path():
  152. """Return the full path to cscript.exe or None."""
  153. for dir in os.environ.get("PATH", "").split(os.pathsep):
  154. cscript_path = os.path.join(dir, "cscript.exe")
  155. if os.path.exists(cscript_path):
  156. return cscript_path
  157. __have_soundcard_cache = None
  158. def _have_soundcard():
  159. """Return True iff this computer has a soundcard."""
  160. global __have_soundcard_cache
  161. if __have_soundcard_cache is None:
  162. cscript_path = _get_cscript_path()
  163. if cscript_path is None:
  164. # Could not find cscript.exe to run our VBScript helper. Default
  165. # to True: most computers these days *do* have a soundcard.
  166. return True
  167. check_script = os.path.join(os.path.dirname(__file__),
  168. "check_soundcard.vbs")
  169. p = subprocess.Popen([cscript_path, check_script],
  170. stdout=subprocess.PIPE)
  171. __have_soundcard_cache = not p.wait()
  172. return __have_soundcard_cache
  173. def test_main():
  174. test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
  175. if __name__=="__main__":
  176. test_main()