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 | |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 3 | from __future__ import print_function |
| 4 | |
| 5 | import functools |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 6 | import os |
| 7 | import subprocess |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 8 | import time |
| 9 | import unittest |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 10 | |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 11 | from test import test_support as support |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 12 | |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 13 | support.requires('audio') |
| 14 | winsound = support.import_module('winsound') |
Brian Curtin | 824912e | 2010-06-01 13:29:13 +0000 | [diff] [blame] | 15 | |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 16 | # Unless we actually have an ear in the room, we have no idea whether a sound |
| 17 | # actually plays, and it's incredibly flaky trying to figure out if a sound |
| 18 | # even *should* play. Instead of guessing, just call the function and assume |
| 19 | # it either passed or raised the RuntimeError we expect in case of failure. |
| 20 | def sound_func(func): |
| 21 | @functools.wraps(func) |
| 22 | def wrapper(*args, **kwargs): |
| 23 | try: |
| 24 | ret = func(*args, **kwargs) |
| 25 | except RuntimeError as e: |
| 26 | if support.verbose: |
| 27 | print(func.__name__, 'failed:', e) |
Brian Curtin | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 28 | else: |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 29 | if support.verbose: |
| 30 | print(func.__name__, 'returned') |
| 31 | return ret |
| 32 | return wrapper |
| 33 | |
| 34 | safe_Beep = sound_func(winsound.Beep) |
| 35 | safe_MessageBeep = sound_func(winsound.MessageBeep) |
| 36 | safe_PlaySound = sound_func(winsound.PlaySound) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 37 | |
| 38 | class BeepTest(unittest.TestCase): |
| 39 | |
| 40 | def test_errors(self): |
| 41 | self.assertRaises(TypeError, winsound.Beep) |
| 42 | self.assertRaises(ValueError, winsound.Beep, 36, 75) |
| 43 | self.assertRaises(ValueError, winsound.Beep, 32768, 75) |
| 44 | |
| 45 | def test_extremes(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 46 | safe_Beep(37, 75) |
| 47 | safe_Beep(32767, 75) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 48 | |
| 49 | def test_increasingfrequency(self): |
Steven Bethard | 8906575 | 2008-03-18 19:04:32 +0000 | [diff] [blame] | 50 | for i in xrange(100, 2000, 100): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 51 | safe_Beep(i, 75) |
Steven Bethard | 8906575 | 2008-03-18 19:04:32 +0000 | [diff] [blame] | 52 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 53 | |
| 54 | class MessageBeepTest(unittest.TestCase): |
| 55 | |
| 56 | def tearDown(self): |
| 57 | time.sleep(0.5) |
| 58 | |
| 59 | def test_default(self): |
| 60 | self.assertRaises(TypeError, winsound.MessageBeep, "bad") |
| 61 | self.assertRaises(TypeError, winsound.MessageBeep, 42, 42) |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 62 | safe_MessageBeep() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 63 | |
| 64 | def test_ok(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 65 | safe_MessageBeep(winsound.MB_OK) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 66 | |
| 67 | def test_asterisk(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 68 | safe_MessageBeep(winsound.MB_ICONASTERISK) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 69 | |
| 70 | def test_exclamation(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 71 | safe_MessageBeep(winsound.MB_ICONEXCLAMATION) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 72 | |
| 73 | def test_hand(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 74 | safe_MessageBeep(winsound.MB_ICONHAND) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 75 | |
| 76 | def test_question(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 77 | safe_MessageBeep(winsound.MB_ICONQUESTION) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 78 | |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 79 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 80 | class PlaySoundTest(unittest.TestCase): |
| 81 | |
| 82 | def test_errors(self): |
| 83 | self.assertRaises(TypeError, winsound.PlaySound) |
| 84 | self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad") |
| 85 | self.assertRaises( |
| 86 | RuntimeError, |
| 87 | winsound.PlaySound, |
| 88 | "none", winsound.SND_ASYNC | winsound.SND_MEMORY |
| 89 | ) |
| 90 | |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 91 | def test_aliases(self): |
| 92 | aliases = [ |
| 93 | "SystemAsterisk", |
| 94 | "SystemExclamation", |
| 95 | "SystemExit", |
| 96 | "SystemHand", |
| 97 | "SystemQuestion", |
| 98 | ] |
| 99 | for alias in aliases: |
| 100 | safe_PlaySound(alias, winsound.SND_ALIAS) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 101 | |
| 102 | def test_alias_fallback(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 103 | safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 104 | |
| 105 | def test_alias_nofallback(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 106 | safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 107 | |
| 108 | def test_stopasync(self): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 109 | safe_PlaySound( |
| 110 | 'SystemQuestion', |
| 111 | winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP |
| 112 | ) |
| 113 | time.sleep(0.5) |
| 114 | safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP) |
| 115 | # Issue 8367: PlaySound(None, winsound.SND_PURGE) |
| 116 | # does not raise on systems without a sound card. |
| 117 | winsound.PlaySound(None, winsound.SND_PURGE) |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 118 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 119 | |
| 120 | def test_main(): |
Zachary Ware | 3445827 | 2016-09-05 15:09:41 -0500 | [diff] [blame^] | 121 | support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest) |
| 122 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 123 | |
| 124 | if __name__=="__main__": |
| 125 | test_main() |