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 | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 3 | import functools |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 4 | import time |
| 5 | import unittest |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 7 | from test import support |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame^] | 8 | from test.support import import_helper |
| 9 | |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 10 | |
| 11 | support.requires('audio') |
Hai Shi | 79bb2c9 | 2020-08-06 19:51:29 +0800 | [diff] [blame^] | 12 | winsound = import_helper.import_module('winsound') |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 13 | |
Brian Curtin | ae16af2 | 2010-06-01 13:49:19 +0000 | [diff] [blame] | 14 | |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 15 | # Unless we actually have an ear in the room, we have no idea whether a sound |
| 16 | # actually plays, and it's incredibly flaky trying to figure out if a sound |
| 17 | # even *should* play. Instead of guessing, just call the function and assume |
| 18 | # it either passed or raised the RuntimeError we expect in case of failure. |
| 19 | def sound_func(func): |
| 20 | @functools.wraps(func) |
| 21 | def wrapper(*args, **kwargs): |
| 22 | try: |
| 23 | ret = func(*args, **kwargs) |
| 24 | except RuntimeError as e: |
| 25 | if support.verbose: |
| 26 | print(func.__name__, 'failed:', e) |
| 27 | else: |
| 28 | if support.verbose: |
| 29 | print(func.__name__, 'returned') |
| 30 | return ret |
| 31 | return wrapper |
| 32 | |
| 33 | |
| 34 | safe_Beep = sound_func(winsound.Beep) |
| 35 | safe_MessageBeep = sound_func(winsound.MessageBeep) |
| 36 | safe_PlaySound = sound_func(winsound.PlaySound) |
| 37 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 38 | |
| 39 | class BeepTest(unittest.TestCase): |
| 40 | |
| 41 | def test_errors(self): |
| 42 | self.assertRaises(TypeError, winsound.Beep) |
| 43 | self.assertRaises(ValueError, winsound.Beep, 36, 75) |
| 44 | self.assertRaises(ValueError, winsound.Beep, 32768, 75) |
| 45 | |
| 46 | def test_extremes(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 47 | safe_Beep(37, 75) |
| 48 | safe_Beep(32767, 75) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 49 | |
| 50 | def test_increasingfrequency(self): |
Amaury Forgeot d'Arc | 0a665ce | 2008-03-20 01:02:48 +0000 | [diff] [blame] | 51 | for i in range(100, 2000, 100): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 52 | safe_Beep(i, 75) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 53 | |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 54 | def test_keyword_args(self): |
| 55 | safe_Beep(duration=75, frequency=2000) |
| 56 | |
| 57 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 58 | class MessageBeepTest(unittest.TestCase): |
| 59 | |
| 60 | def tearDown(self): |
| 61 | time.sleep(0.5) |
| 62 | |
| 63 | def test_default(self): |
| 64 | self.assertRaises(TypeError, winsound.MessageBeep, "bad") |
| 65 | self.assertRaises(TypeError, winsound.MessageBeep, 42, 42) |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 66 | safe_MessageBeep() |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 67 | |
| 68 | def test_ok(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 69 | safe_MessageBeep(winsound.MB_OK) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 70 | |
| 71 | def test_asterisk(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 72 | safe_MessageBeep(winsound.MB_ICONASTERISK) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 73 | |
| 74 | def test_exclamation(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 75 | safe_MessageBeep(winsound.MB_ICONEXCLAMATION) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 76 | |
| 77 | def test_hand(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 78 | safe_MessageBeep(winsound.MB_ICONHAND) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 79 | |
| 80 | def test_question(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 81 | safe_MessageBeep(winsound.MB_ICONQUESTION) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 82 | |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 83 | def test_keyword_args(self): |
| 84 | safe_MessageBeep(type=winsound.MB_OK) |
| 85 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 86 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 87 | class PlaySoundTest(unittest.TestCase): |
| 88 | |
| 89 | def test_errors(self): |
| 90 | self.assertRaises(TypeError, winsound.PlaySound) |
| 91 | self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad") |
| 92 | self.assertRaises( |
| 93 | RuntimeError, |
| 94 | winsound.PlaySound, |
| 95 | "none", winsound.SND_ASYNC | winsound.SND_MEMORY |
| 96 | ) |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 97 | self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0) |
| 98 | self.assertRaises(TypeError, winsound.PlaySound, "bad", |
| 99 | winsound.SND_MEMORY) |
| 100 | self.assertRaises(TypeError, winsound.PlaySound, 1, 0) |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 101 | # embedded null character |
| 102 | self.assertRaises(ValueError, winsound.PlaySound, 'bad\0', 0) |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 103 | |
Zachary Ware | c401881 | 2016-09-06 16:32:43 -0500 | [diff] [blame] | 104 | def test_keyword_args(self): |
| 105 | safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit") |
| 106 | |
Zachary Ware | ae8298b | 2016-09-05 16:31:21 -0500 | [diff] [blame] | 107 | def test_snd_memory(self): |
| 108 | with open(support.findfile('pluck-pcm8.wav', |
| 109 | subdir='audiodata'), 'rb') as f: |
| 110 | audio_data = f.read() |
| 111 | safe_PlaySound(audio_data, winsound.SND_MEMORY) |
| 112 | audio_data = bytearray(audio_data) |
| 113 | safe_PlaySound(audio_data, winsound.SND_MEMORY) |
| 114 | |
| 115 | def test_snd_filename(self): |
| 116 | fn = support.findfile('pluck-pcm8.wav', subdir='audiodata') |
| 117 | safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 118 | |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 119 | def test_aliases(self): |
| 120 | aliases = [ |
| 121 | "SystemAsterisk", |
| 122 | "SystemExclamation", |
| 123 | "SystemExit", |
| 124 | "SystemHand", |
| 125 | "SystemQuestion", |
| 126 | ] |
| 127 | for alias in aliases: |
| 128 | with self.subTest(alias=alias): |
| 129 | safe_PlaySound(alias, winsound.SND_ALIAS) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 130 | |
| 131 | def test_alias_fallback(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 132 | safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 133 | |
| 134 | def test_alias_nofallback(self): |
Zachary Ware | f4a7381 | 2016-09-05 18:08:27 -0500 | [diff] [blame] | 135 | safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 136 | |
| 137 | def test_stopasync(self): |
Zachary Ware | b3b7a5a | 2016-09-05 16:06:56 -0500 | [diff] [blame] | 138 | safe_PlaySound( |
| 139 | 'SystemQuestion', |
| 140 | winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP |
| 141 | ) |
| 142 | time.sleep(0.5) |
| 143 | safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP) |
| 144 | # Issue 8367: PlaySound(None, winsound.SND_PURGE) |
| 145 | # does not raise on systems without a sound card. |
| 146 | winsound.PlaySound(None, winsound.SND_PURGE) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 147 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 148 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 149 | if __name__ == "__main__": |
| 150 | unittest.main() |