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