blob: c39a233ca8fd52fc4f4afd9c7eef54547f1d6052 [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
5support.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 Heimesd5e2b6f2008-03-19 21:50:51 +000026 self._beep(37, 75)
27 self._beep(32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000028
29 def test_increasingfrequency(self):
Amaury Forgeot d'Arc0a665ce2008-03-20 01:02:48 +000030 for i in range(100, 2000, 100):
Christian Heimesd5e2b6f2008-03-19 21:50:51 +000031 self._beep(i, 75)
32
33 def _beep(self, *args):
34 # these tests used to use _have_soundcard(), but it's quite
35 # possible to have a soundcard, and yet have the beep driver
36 # disabled. So basically, we have no way of knowing whether
37 # a beep should be produced or not, so currently if these
38 # tests fail we're ignoring them
39 #
40 # XXX the right fix for this is to define something like
41 # _have_enabled_beep_driver() and use that instead of the
42 # try/except below
43 try:
44 winsound.Beep(*args)
45 except RuntimeError:
46 pass
Walter Dörwald7fd94242003-05-18 00:47:47 +000047
48class MessageBeepTest(unittest.TestCase):
49
50 def tearDown(self):
51 time.sleep(0.5)
52
53 def test_default(self):
54 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
55 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
56 winsound.MessageBeep()
57
58 def test_ok(self):
59 winsound.MessageBeep(winsound.MB_OK)
60
61 def test_asterisk(self):
62 winsound.MessageBeep(winsound.MB_ICONASTERISK)
63
64 def test_exclamation(self):
65 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
66
67 def test_hand(self):
68 winsound.MessageBeep(winsound.MB_ICONHAND)
69
70 def test_question(self):
71 winsound.MessageBeep(winsound.MB_ICONQUESTION)
72
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000073
Walter Dörwald7fd94242003-05-18 00:47:47 +000074class PlaySoundTest(unittest.TestCase):
75
76 def test_errors(self):
77 self.assertRaises(TypeError, winsound.PlaySound)
78 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
79 self.assertRaises(
80 RuntimeError,
81 winsound.PlaySound,
82 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
83 )
84
85 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 if _have_soundcard():
87 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
88 else:
89 self.assertRaises(
90 RuntimeError,
91 winsound.PlaySound,
92 'SystemAsterisk', winsound.SND_ALIAS
93 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000094
95 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096 if _have_soundcard():
97 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
98 else:
99 self.assertRaises(
100 RuntimeError,
101 winsound.PlaySound,
102 'SystemExclamation', winsound.SND_ALIAS
103 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000104
105 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106 if _have_soundcard():
107 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
108 else:
109 self.assertRaises(
110 RuntimeError,
111 winsound.PlaySound,
112 'SystemExit', winsound.SND_ALIAS
113 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000114
115 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 if _have_soundcard():
117 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
118 else:
119 self.assertRaises(
120 RuntimeError,
121 winsound.PlaySound,
122 'SystemHand', winsound.SND_ALIAS
123 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000124
125 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000126 if _have_soundcard():
127 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
128 else:
129 self.assertRaises(
130 RuntimeError,
131 winsound.PlaySound,
132 'SystemQuestion', winsound.SND_ALIAS
133 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000134
135 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000136 # This test can't be expected to work on all systems. The MS
137 # PlaySound() docs say:
138 #
139 # If it cannot find the specified sound, PlaySound uses the
140 # default system event sound entry instead. If the function
141 # can find neither the system default entry nor the default
142 # sound, it makes no sound and returns FALSE.
143 #
144 # It's known to return FALSE on some real systems.
145
146 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
147 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000148
149 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 if _have_soundcard():
151 # Note that this is not the same as asserting RuntimeError
152 # will get raised: you cannot convert this to
153 # self.assertRaises(...) form. The attempt may or may not
154 # raise RuntimeError, but it shouldn't raise anything other
155 # than RuntimeError, and that's all we're trying to test
156 # here. The MS docs aren't clear about whether the SDK
157 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
158 # True or False when the alias is unknown. On Tim's WinXP
159 # box today, it returns True (no exception is raised). What
160 # we'd really like to test is that no sound is played, but
161 # that requires first wiring an eardrum class into unittest
162 # <wink>.
163 try:
164 winsound.PlaySound(
165 '!"$%&/(#+*',
166 winsound.SND_ALIAS | winsound.SND_NODEFAULT
167 )
168 except RuntimeError:
169 pass
170 else:
171 self.assertRaises(
172 RuntimeError,
173 winsound.PlaySound,
174 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000175 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000176
177 def test_stopasync(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000179 winsound.PlaySound(
180 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000182 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183 time.sleep(0.5)
184 try:
185 winsound.PlaySound(
186 'SystemQuestion',
187 winsound.SND_ALIAS | winsound.SND_NOSTOP
188 )
189 except RuntimeError:
190 pass
191 else: # the first sound might already be finished
192 pass
193 winsound.PlaySound(None, winsound.SND_PURGE)
194 else:
195 self.assertRaises(
196 RuntimeError,
197 winsound.PlaySound,
198 None, winsound.SND_PURGE
199 )
200
201
202def _get_cscript_path():
203 """Return the full path to cscript.exe or None."""
204 for dir in os.environ.get("PATH", "").split(os.pathsep):
205 cscript_path = os.path.join(dir, "cscript.exe")
206 if os.path.exists(cscript_path):
207 return cscript_path
208
209__have_soundcard_cache = None
210def _have_soundcard():
211 """Return True iff this computer has a soundcard."""
212 global __have_soundcard_cache
213 if __have_soundcard_cache is None:
214 cscript_path = _get_cscript_path()
215 if cscript_path is None:
216 # Could not find cscript.exe to run our VBScript helper. Default
217 # to True: most computers these days *do* have a soundcard.
218 return True
219
220 check_script = os.path.join(os.path.dirname(__file__),
221 "check_soundcard.vbs")
222 p = subprocess.Popen([cscript_path, check_script],
223 stdout=subprocess.PIPE)
224 __have_soundcard_cache = not p.wait()
225 return __have_soundcard_cache
226
Walter Dörwald7fd94242003-05-18 00:47:47 +0000227
228def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000229 support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
Walter Dörwald7fd94242003-05-18 00:47:47 +0000230
231if __name__=="__main__":
232 test_main()