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