Guido van Rossum | 2512d6d | 2000-04-24 14:01:51 +0000 | [diff] [blame] | 1 | # Ridiculously simple test of the winsound module for Windows. |
Guido van Rossum | cdd092f | 2000-04-21 21:28:47 +0000 | [diff] [blame] | 2 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 3 | import unittest |
| 4 | from test import test_support |
Guido van Rossum | 11d204c | 2003-04-09 19:57:06 +0000 | [diff] [blame] | 5 | import winsound, time |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 6 | |
| 7 | class BeepTest(unittest.TestCase): |
| 8 | |
| 9 | def test_errors(self): |
| 10 | self.assertRaises(TypeError, winsound.Beep) |
| 11 | self.assertRaises(ValueError, winsound.Beep, 36, 75) |
| 12 | self.assertRaises(ValueError, winsound.Beep, 32768, 75) |
| 13 | |
| 14 | def test_extremes(self): |
| 15 | winsound.Beep(37, 75) |
| 16 | winsound.Beep(32767, 75) |
| 17 | |
| 18 | def test_increasingfrequency(self): |
| 19 | for i in xrange(100, 2000, 100): |
| 20 | winsound.Beep(i, 75) |
| 21 | |
| 22 | class MessageBeepTest(unittest.TestCase): |
| 23 | |
| 24 | def tearDown(self): |
| 25 | time.sleep(0.5) |
| 26 | |
| 27 | def test_default(self): |
| 28 | self.assertRaises(TypeError, winsound.MessageBeep, "bad") |
| 29 | self.assertRaises(TypeError, winsound.MessageBeep, 42, 42) |
| 30 | winsound.MessageBeep() |
| 31 | |
| 32 | def test_ok(self): |
| 33 | winsound.MessageBeep(winsound.MB_OK) |
| 34 | |
| 35 | def test_asterisk(self): |
| 36 | winsound.MessageBeep(winsound.MB_ICONASTERISK) |
| 37 | |
| 38 | def test_exclamation(self): |
| 39 | winsound.MessageBeep(winsound.MB_ICONEXCLAMATION) |
| 40 | |
| 41 | def test_hand(self): |
| 42 | winsound.MessageBeep(winsound.MB_ICONHAND) |
| 43 | |
| 44 | def test_question(self): |
| 45 | winsound.MessageBeep(winsound.MB_ICONQUESTION) |
| 46 | |
| 47 | class PlaySoundTest(unittest.TestCase): |
| 48 | |
| 49 | def test_errors(self): |
| 50 | self.assertRaises(TypeError, winsound.PlaySound) |
| 51 | self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad") |
| 52 | self.assertRaises( |
| 53 | RuntimeError, |
| 54 | winsound.PlaySound, |
| 55 | "none", winsound.SND_ASYNC | winsound.SND_MEMORY |
| 56 | ) |
| 57 | |
| 58 | def test_alias_asterisk(self): |
| 59 | winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS) |
| 60 | |
| 61 | def test_alias_exclamation(self): |
| 62 | winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS) |
| 63 | |
| 64 | def test_alias_exit(self): |
| 65 | winsound.PlaySound('SystemExit', winsound.SND_ALIAS) |
| 66 | |
| 67 | def test_alias_hand(self): |
| 68 | winsound.PlaySound('SystemHand', winsound.SND_ALIAS) |
| 69 | |
| 70 | def test_alias_question(self): |
| 71 | winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS) |
| 72 | |
| 73 | def test_alias_fallback(self): |
Tim Peters | 086e562 | 2003-09-22 18:38:53 +0000 | [diff] [blame] | 74 | # This test can't be expected to work on all systems. The MS |
| 75 | # PlaySound() docs say: |
| 76 | # |
| 77 | # If it cannot find the specified sound, PlaySound uses the |
| 78 | # default system event sound entry instead. If the function |
| 79 | # can find neither the system default entry nor the default |
| 80 | # sound, it makes no sound and returns FALSE. |
| 81 | # |
| 82 | # It's known to return FALSE on some real systems. |
| 83 | |
| 84 | # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) |
| 85 | return |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 86 | |
| 87 | def test_alias_nofallback(self): |
Tim Peters | b8b60ea | 2003-09-22 18:41:53 +0000 | [diff] [blame] | 88 | self.assertRaises(RuntimeError, winsound.PlaySound, '!"$%&/(#+*', |
| 89 | winsound.SND_ALIAS | winsound.SND_NODEFAULT) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 90 | |
| 91 | def test_stopasync(self): |
| 92 | winsound.PlaySound( |
| 93 | 'SystemQuestion', |
| 94 | winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP |
| 95 | ) |
| 96 | time.sleep(0.5) |
Walter Dörwald | 8bcbe6a | 2003-06-30 11:57:52 +0000 | [diff] [blame] | 97 | try: |
| 98 | winsound.PlaySound( |
| 99 | 'SystemQuestion', |
| 100 | winsound.SND_ALIAS | winsound.SND_NOSTOP |
| 101 | ) |
| 102 | except RuntimeError: |
| 103 | pass |
| 104 | else: # the first sound might already be finished |
| 105 | pass |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 106 | winsound.PlaySound(None, winsound.SND_PURGE) |
| 107 | |
| 108 | def test_main(): |
| 109 | test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest) |
| 110 | |
| 111 | if __name__=="__main__": |
| 112 | test_main() |