PyXR

c:\python24\lib \ test \ test_winsound.py



0001 # Ridiculously simple test of the winsound module for Windows.
0002 
0003 import unittest
0004 from test import test_support
0005 import winsound, time
0006 
0007 class BeepTest(unittest.TestCase):
0008 
0009     def test_errors(self):
0010         self.assertRaises(TypeError, winsound.Beep)
0011         self.assertRaises(ValueError, winsound.Beep, 36, 75)
0012         self.assertRaises(ValueError, winsound.Beep, 32768, 75)
0013 
0014     def test_extremes(self):
0015         winsound.Beep(37, 75)
0016         winsound.Beep(32767, 75)
0017 
0018     def test_increasingfrequency(self):
0019         for i in xrange(100, 2000, 100):
0020             winsound.Beep(i, 75)
0021 
0022 class MessageBeepTest(unittest.TestCase):
0023 
0024     def tearDown(self):
0025         time.sleep(0.5)
0026 
0027     def test_default(self):
0028         self.assertRaises(TypeError, winsound.MessageBeep, "bad")
0029         self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
0030         winsound.MessageBeep()
0031 
0032     def test_ok(self):
0033         winsound.MessageBeep(winsound.MB_OK)
0034 
0035     def test_asterisk(self):
0036         winsound.MessageBeep(winsound.MB_ICONASTERISK)
0037 
0038     def test_exclamation(self):
0039         winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
0040 
0041     def test_hand(self):
0042         winsound.MessageBeep(winsound.MB_ICONHAND)
0043 
0044     def test_question(self):
0045         winsound.MessageBeep(winsound.MB_ICONQUESTION)
0046 
0047 class PlaySoundTest(unittest.TestCase):
0048 
0049     def test_errors(self):
0050         self.assertRaises(TypeError, winsound.PlaySound)
0051         self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
0052         self.assertRaises(
0053             RuntimeError,
0054             winsound.PlaySound,
0055             "none", winsound.SND_ASYNC | winsound.SND_MEMORY
0056         )
0057 
0058     def test_alias_asterisk(self):
0059         winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
0060 
0061     def test_alias_exclamation(self):
0062         winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
0063 
0064     def test_alias_exit(self):
0065         winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
0066 
0067     def test_alias_hand(self):
0068         winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
0069 
0070     def test_alias_question(self):
0071         winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
0072 
0073     def test_alias_fallback(self):
0074         # This test can't be expected to work on all systems.  The MS
0075         # PlaySound() docs say:
0076         #
0077         #     If it cannot find the specified sound, PlaySound uses the
0078         #     default system event sound entry instead.  If the function
0079         #     can find neither the system default entry nor the default
0080         #     sound, it makes no sound and returns FALSE.
0081         #
0082         # It's known to return FALSE on some real systems.
0083 
0084         # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
0085         return
0086 
0087     def test_alias_nofallback(self):
0088         # Note that this is not the same as asserting RuntimeError
0089         # will get raised:  you cannot convert this to
0090         # self.assertRaises(...) form.  The attempt may or may not
0091         # raise RuntimeError, but it shouldn't raise anything other
0092         # than RuntimeError, and that's all we're trying to test here.
0093         # The MS docs aren't clear about whether the SDK PlaySound()
0094         # with SND_ALIAS and SND_NODEFAULT will return True or False when
0095         # the alias is unknown.  On Tim's WinXP box today, it returns
0096         # True (no exception is raised).  What we'd really like to test
0097         # is that no sound is played, but that requires first wiring an
0098         # eardrum class into unittest <wink>.
0099         try:
0100             winsound.PlaySound(
0101                 '!"$%&/(#+*',
0102                 winsound.SND_ALIAS | winsound.SND_NODEFAULT
0103             )
0104         except RuntimeError:
0105             pass
0106 
0107     def test_stopasync(self):
0108         winsound.PlaySound(
0109             'SystemQuestion',
0110             winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
0111         )
0112         time.sleep(0.5)
0113         try:
0114             winsound.PlaySound(
0115                 'SystemQuestion',
0116                 winsound.SND_ALIAS | winsound.SND_NOSTOP
0117             )
0118         except RuntimeError:
0119             pass
0120         else: # the first sound might already be finished
0121             pass
0122         winsound.PlaySound(None, winsound.SND_PURGE)
0123 
0124 def test_main():
0125     test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
0126 
0127 if __name__=="__main__":
0128     test_main()
0129 

Generated by PyXR 0.9.4
SourceForge.net Logo