blob: cba6ee6d36811805b15df61af5bc4200e48945ba [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
R. David Murray59beec32009-03-30 19:04:00 +00005import time
Trent Mickf8cf13e2006-03-16 17:34:41 +00006import os
7import subprocess
8
R. David Murray59beec32009-03-30 19:04:00 +00009winsound = test_support.import_module('winsound')
10
Walter Dörwald7fd94242003-05-18 00:47:47 +000011
12class BeepTest(unittest.TestCase):
Neal Norwitz21807252008-03-05 05:14:18 +000013 # As with PlaySoundTest, incorporate the _have_soundcard() check
14 # into our test methods. If there's no audio device present,
15 # winsound.Beep returns 0 and GetLastError() returns 127, which
16 # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not
17 # be found"). (FWIW, virtual/Hyper-V systems fall under this
18 # scenario as they have no sound devices whatsoever (not even
19 # a legacy Beep device).)
Walter Dörwald7fd94242003-05-18 00:47:47 +000020
21 def test_errors(self):
22 self.assertRaises(TypeError, winsound.Beep)
23 self.assertRaises(ValueError, winsound.Beep, 36, 75)
24 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
25
26 def test_extremes(self):
Steven Bethard89065752008-03-18 19:04:32 +000027 self._beep(37, 75)
28 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000029
30 def test_increasingfrequency(self):
Steven Bethard89065752008-03-18 19:04:32 +000031 for i in xrange(100, 2000, 100):
32 self._beep(i, 75)
33
34 def _beep(self, *args):
35 # these tests used to use _have_soundcard(), but it's quite
36 # possible to have a soundcard, and yet have the beep driver
37 # disabled. So basically, we have no way of knowing whether
38 # a beep should be produced or not, so currently if these
39 # tests fail we're ignoring them
40 #
41 # XXX the right fix for this is to define something like
42 # _have_enabled_beep_driver() and use that instead of the
43 # try/except below
44 try:
45 winsound.Beep(*args)
46 except RuntimeError:
47 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000048
49class MessageBeepTest(unittest.TestCase):
50
51 def tearDown(self):
52 time.sleep(0.5)
53
54 def test_default(self):
55 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
56 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
57 winsound.MessageBeep()
58
59 def test_ok(self):
60 winsound.MessageBeep(winsound.MB_OK)
61
62 def test_asterisk(self):
63 winsound.MessageBeep(winsound.MB_ICONASTERISK)
64
65 def test_exclamation(self):
66 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
67
68 def test_hand(self):
69 winsound.MessageBeep(winsound.MB_ICONHAND)
70
71 def test_question(self):
72 winsound.MessageBeep(winsound.MB_ICONQUESTION)
73
Trent Mickf8cf13e2006-03-16 17:34:41 +000074
Walter Dörwald7fd94242003-05-18 00:47:47 +000075class PlaySoundTest(unittest.TestCase):
76
77 def test_errors(self):
78 self.assertRaises(TypeError, winsound.PlaySound)
79 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
80 self.assertRaises(
81 RuntimeError,
82 winsound.PlaySound,
83 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
84 )
85
86 def test_alias_asterisk(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +000087 if _have_soundcard():
88 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
89 else:
90 self.assertRaises(
91 RuntimeError,
92 winsound.PlaySound,
93 'SystemAsterisk', winsound.SND_ALIAS
94 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000095
96 def test_alias_exclamation(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +000097 if _have_soundcard():
98 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
99 else:
100 self.assertRaises(
101 RuntimeError,
102 winsound.PlaySound,
103 'SystemExclamation', winsound.SND_ALIAS
104 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000105
106 def test_alias_exit(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000107 if _have_soundcard():
108 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
109 else:
110 self.assertRaises(
111 RuntimeError,
112 winsound.PlaySound,
113 'SystemExit', winsound.SND_ALIAS
114 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000115
116 def test_alias_hand(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000117 if _have_soundcard():
118 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
119 else:
120 self.assertRaises(
121 RuntimeError,
122 winsound.PlaySound,
123 'SystemHand', winsound.SND_ALIAS
124 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000125
126 def test_alias_question(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000127 if _have_soundcard():
128 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
129 else:
130 self.assertRaises(
131 RuntimeError,
132 winsound.PlaySound,
133 'SystemQuestion', winsound.SND_ALIAS
134 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000135
136 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000137 # This test can't be expected to work on all systems. The MS
138 # PlaySound() docs say:
139 #
140 # If it cannot find the specified sound, PlaySound uses the
141 # default system event sound entry instead. If the function
142 # can find neither the system default entry nor the default
143 # sound, it makes no sound and returns FALSE.
144 #
145 # It's known to return FALSE on some real systems.
146
147 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
148 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000149
150 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000151 if _have_soundcard():
152 # Note that this is not the same as asserting RuntimeError
153 # will get raised: you cannot convert this to
154 # self.assertRaises(...) form. The attempt may or may not
155 # raise RuntimeError, but it shouldn't raise anything other
156 # than RuntimeError, and that's all we're trying to test
157 # here. The MS docs aren't clear about whether the SDK
158 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
159 # True or False when the alias is unknown. On Tim's WinXP
160 # box today, it returns True (no exception is raised). What
161 # we'd really like to test is that no sound is played, but
162 # that requires first wiring an eardrum class into unittest
163 # <wink>.
164 try:
165 winsound.PlaySound(
166 '!"$%&/(#+*',
167 winsound.SND_ALIAS | winsound.SND_NODEFAULT
168 )
169 except RuntimeError:
170 pass
171 else:
172 self.assertRaises(
173 RuntimeError,
174 winsound.PlaySound,
175 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000176 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000177
178 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000179 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000180 winsound.PlaySound(
181 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000182 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000183 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000184 time.sleep(0.5)
185 try:
186 winsound.PlaySound(
187 'SystemQuestion',
188 winsound.SND_ALIAS | winsound.SND_NOSTOP
189 )
190 except RuntimeError:
191 pass
192 else: # the first sound might already be finished
193 pass
194 winsound.PlaySound(None, winsound.SND_PURGE)
195 else:
196 self.assertRaises(
197 RuntimeError,
198 winsound.PlaySound,
199 None, winsound.SND_PURGE
200 )
201
202
203def _get_cscript_path():
204 """Return the full path to cscript.exe or None."""
205 for dir in os.environ.get("PATH", "").split(os.pathsep):
206 cscript_path = os.path.join(dir, "cscript.exe")
207 if os.path.exists(cscript_path):
208 return cscript_path
209
210__have_soundcard_cache = None
211def _have_soundcard():
212 """Return True iff this computer has a soundcard."""
213 global __have_soundcard_cache
214 if __have_soundcard_cache is None:
215 cscript_path = _get_cscript_path()
216 if cscript_path is None:
217 # Could not find cscript.exe to run our VBScript helper. Default
218 # to True: most computers these days *do* have a soundcard.
219 return True
220
221 check_script = os.path.join(os.path.dirname(__file__),
222 "check_soundcard.vbs")
223 p = subprocess.Popen([cscript_path, check_script],
224 stdout=subprocess.PIPE)
225 __have_soundcard_cache = not p.wait()
226 return __have_soundcard_cache
227
Walter Dörwald7fd94242003-05-18 00:47:47 +0000228
229def test_main():
230 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
231
232if __name__=="__main__":
233 test_main()