blob: 1cfef779d67eccea7b656299782c354984cf38fe [file] [log] [blame]
Guido van Rossum2512d6d2000-04-24 14:01:51 +00001# Ridiculously simple test of the winsound module for Windows.
Guido van Rossumcdd092f2000-04-21 21:28:47 +00002
Zachary Wareb3b7a5a2016-09-05 16:06:56 -05003import functools
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004import os
5import subprocess
Zachary Wareb3b7a5a2016-09-05 16:06:56 -05006import time
7import unittest
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008
Zachary Wareb3b7a5a2016-09-05 16:06:56 -05009from test import support
10
11support.requires('audio')
R. David Murraya21e4ca2009-03-31 23:16:50 +000012winsound = support.import_module('winsound')
13
Brian Curtinae16af22010-06-01 13:49:19 +000014
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050015# 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.
19def 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
34safe_Beep = sound_func(winsound.Beep)
35safe_MessageBeep = sound_func(winsound.MessageBeep)
36safe_PlaySound = sound_func(winsound.PlaySound)
37
Walter Dörwald7fd94242003-05-18 00:47:47 +000038
39class 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 Wareb3b7a5a2016-09-05 16:06:56 -050047 safe_Beep(37, 75)
48 safe_Beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000049
50 def test_increasingfrequency(self):
Amaury Forgeot d'Arc0a665ce2008-03-20 01:02:48 +000051 for i in range(100, 2000, 100):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050052 safe_Beep(i, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000053
54class 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 Wareb3b7a5a2016-09-05 16:06:56 -050062 safe_MessageBeep()
Walter Dörwald7fd94242003-05-18 00:47:47 +000063
64 def test_ok(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050065 safe_MessageBeep(winsound.MB_OK)
Walter Dörwald7fd94242003-05-18 00:47:47 +000066
67 def test_asterisk(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050068 safe_MessageBeep(winsound.MB_ICONASTERISK)
Walter Dörwald7fd94242003-05-18 00:47:47 +000069
70 def test_exclamation(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050071 safe_MessageBeep(winsound.MB_ICONEXCLAMATION)
Walter Dörwald7fd94242003-05-18 00:47:47 +000072
73 def test_hand(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050074 safe_MessageBeep(winsound.MB_ICONHAND)
Walter Dörwald7fd94242003-05-18 00:47:47 +000075
76 def test_question(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -050077 safe_MessageBeep(winsound.MB_ICONQUESTION)
Walter Dörwald7fd94242003-05-18 00:47:47 +000078
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000079
Walter Dörwald7fd94242003-05-18 00:47:47 +000080class 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 )
Zachary Wareae8298b2016-09-05 16:31:21 -050090 self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0)
91 self.assertRaises(TypeError, winsound.PlaySound, "bad",
92 winsound.SND_MEMORY)
93 self.assertRaises(TypeError, winsound.PlaySound, 1, 0)
94
95 def test_snd_memory(self):
96 with open(support.findfile('pluck-pcm8.wav',
97 subdir='audiodata'), 'rb') as f:
98 audio_data = f.read()
99 safe_PlaySound(audio_data, winsound.SND_MEMORY)
100 audio_data = bytearray(audio_data)
101 safe_PlaySound(audio_data, winsound.SND_MEMORY)
102
103 def test_snd_filename(self):
104 fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
105 safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000106
Zachary Wareb3b7a5a2016-09-05 16:06:56 -0500107 def test_aliases(self):
108 aliases = [
109 "SystemAsterisk",
110 "SystemExclamation",
111 "SystemExit",
112 "SystemHand",
113 "SystemQuestion",
114 ]
115 for alias in aliases:
116 with self.subTest(alias=alias):
117 safe_PlaySound(alias, winsound.SND_ALIAS)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000118
119 def test_alias_fallback(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -0500120 safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000121
122 def test_alias_nofallback(self):
Zachary Waref4a73812016-09-05 18:08:27 -0500123 safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000124
125 def test_stopasync(self):
Zachary Wareb3b7a5a2016-09-05 16:06:56 -0500126 safe_PlaySound(
127 'SystemQuestion',
128 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
129 )
130 time.sleep(0.5)
131 safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP)
132 # Issue 8367: PlaySound(None, winsound.SND_PURGE)
133 # does not raise on systems without a sound card.
134 winsound.PlaySound(None, winsound.SND_PURGE)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135
Walter Dörwald7fd94242003-05-18 00:47:47 +0000136
Zachary Ware38c707e2015-04-13 15:00:43 -0500137if __name__ == "__main__":
138 unittest.main()