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