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