blob: 7cdc27534f386ebb5a4d516aceafed430e86edc0 [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
Walter Dörwald7fd94242003-05-18 00:47:47 +00003import unittest
4from test import test_support
Christian Heimes70021d72007-11-15 02:15:53 +00005test_support.requires('audio')
Guido van Rossum11d204c2003-04-09 19:57:06 +00006import winsound, time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007import os
8import subprocess
9
Walter Dörwald7fd94242003-05-18 00:47:47 +000010
11class BeepTest(unittest.TestCase):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000012 # As with PlaySoundTest, incorporate the _have_soundcard() check
13 # into our test methods. If there's no audio device present,
14 # winsound.Beep returns 0 and GetLastError() returns 127, which
15 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
16 # be found"). (FWIW, virtual/Hyper-V systems fall under this
17 # scenario as they have no sound devices whatsoever (not even
18 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000019
20 def test_errors(self):
21 self.assertRaises(TypeError, winsound.Beep)
22 self.assertRaises(ValueError, winsound.Beep, 36, 75)
23 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
24
25 def test_extremes(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000026 if _have_soundcard():
27 winsound.Beep(37, 75)
28 winsound.Beep(32767, 75)
29 else:
Christian Heimesb186d002008-03-18 15:15:01 +000030 # The behaviour of winsound.Beep() seems to differ between
31 # different versions of Windows when there's either a) no
32 # sound card entirely, b) legacy beep driver has been disabled,
33 # or c) the legacy beep driver has been uninstalled. Sometimes
34 # RuntimeErrors are raised, sometimes they're not. Meh.
35 try:
36 winsound.Beep(37, 75)
37 winsound.Beep(32767, 75)
38 except RuntimeError:
39 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000040
41 def test_increasingfrequency(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000042 if _have_soundcard():
43 for i in range(100, 2000, 100):
44 winsound.Beep(i, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000045
46class MessageBeepTest(unittest.TestCase):
47
48 def tearDown(self):
49 time.sleep(0.5)
50
51 def test_default(self):
52 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
53 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
54 winsound.MessageBeep()
55
56 def test_ok(self):
57 winsound.MessageBeep(winsound.MB_OK)
58
59 def test_asterisk(self):
60 winsound.MessageBeep(winsound.MB_ICONASTERISK)
61
62 def test_exclamation(self):
63 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
64
65 def test_hand(self):
66 winsound.MessageBeep(winsound.MB_ICONHAND)
67
68 def test_question(self):
69 winsound.MessageBeep(winsound.MB_ICONQUESTION)
70
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000071
Walter Dörwald7fd94242003-05-18 00:47:47 +000072class PlaySoundTest(unittest.TestCase):
73
74 def test_errors(self):
75 self.assertRaises(TypeError, winsound.PlaySound)
76 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
77 self.assertRaises(
78 RuntimeError,
79 winsound.PlaySound,
80 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
81 )
82
83 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 if _have_soundcard():
85 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
86 else:
87 self.assertRaises(
88 RuntimeError,
89 winsound.PlaySound,
90 'SystemAsterisk', winsound.SND_ALIAS
91 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000092
93 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 if _have_soundcard():
95 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
96 else:
97 self.assertRaises(
98 RuntimeError,
99 winsound.PlaySound,
100 'SystemExclamation', winsound.SND_ALIAS
101 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000102
103 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104 if _have_soundcard():
105 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
106 else:
107 self.assertRaises(
108 RuntimeError,
109 winsound.PlaySound,
110 'SystemExit', winsound.SND_ALIAS
111 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000112
113 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 if _have_soundcard():
115 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
116 else:
117 self.assertRaises(
118 RuntimeError,
119 winsound.PlaySound,
120 'SystemHand', winsound.SND_ALIAS
121 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000122
123 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000124 if _have_soundcard():
125 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
126 else:
127 self.assertRaises(
128 RuntimeError,
129 winsound.PlaySound,
130 'SystemQuestion', winsound.SND_ALIAS
131 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000132
133 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000134 # This test can't be expected to work on all systems. The MS
135 # PlaySound() docs say:
136 #
137 # If it cannot find the specified sound, PlaySound uses the
138 # default system event sound entry instead. If the function
139 # can find neither the system default entry nor the default
140 # sound, it makes no sound and returns FALSE.
141 #
142 # It's known to return FALSE on some real systems.
143
144 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
145 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000146
147 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 if _have_soundcard():
149 # Note that this is not the same as asserting RuntimeError
150 # will get raised: you cannot convert this to
151 # self.assertRaises(...) form. The attempt may or may not
152 # raise RuntimeError, but it shouldn't raise anything other
153 # than RuntimeError, and that's all we're trying to test
154 # here. The MS docs aren't clear about whether the SDK
155 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
156 # True or False when the alias is unknown. On Tim's WinXP
157 # box today, it returns True (no exception is raised). What
158 # we'd really like to test is that no sound is played, but
159 # that requires first wiring an eardrum class into unittest
160 # <wink>.
161 try:
162 winsound.PlaySound(
163 '!"$%&/(#+*',
164 winsound.SND_ALIAS | winsound.SND_NODEFAULT
165 )
166 except RuntimeError:
167 pass
168 else:
169 self.assertRaises(
170 RuntimeError,
171 winsound.PlaySound,
172 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000173 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000174
175 def test_stopasync(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000176 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000177 winsound.PlaySound(
178 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000180 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181 time.sleep(0.5)
182 try:
183 winsound.PlaySound(
184 'SystemQuestion',
185 winsound.SND_ALIAS | winsound.SND_NOSTOP
186 )
187 except RuntimeError:
188 pass
189 else: # the first sound might already be finished
190 pass
191 winsound.PlaySound(None, winsound.SND_PURGE)
192 else:
193 self.assertRaises(
194 RuntimeError,
195 winsound.PlaySound,
196 None, winsound.SND_PURGE
197 )
198
199
200def _get_cscript_path():
201 """Return the full path to cscript.exe or None."""
202 for dir in os.environ.get("PATH", "").split(os.pathsep):
203 cscript_path = os.path.join(dir, "cscript.exe")
204 if os.path.exists(cscript_path):
205 return cscript_path
206
207__have_soundcard_cache = None
208def _have_soundcard():
209 """Return True iff this computer has a soundcard."""
210 global __have_soundcard_cache
211 if __have_soundcard_cache is None:
212 cscript_path = _get_cscript_path()
213 if cscript_path is None:
214 # Could not find cscript.exe to run our VBScript helper. Default
215 # to True: most computers these days *do* have a soundcard.
216 return True
217
218 check_script = os.path.join(os.path.dirname(__file__),
219 "check_soundcard.vbs")
220 p = subprocess.Popen([cscript_path, check_script],
221 stdout=subprocess.PIPE)
222 __have_soundcard_cache = not p.wait()
223 return __have_soundcard_cache
224
Walter Dörwald7fd94242003-05-18 00:47:47 +0000225
226def test_main():
227 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
228
229if __name__=="__main__":
230 test_main()