blob: 5606c44cc09eccb6a8ef41ef8eb5c26c34514aa1 [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:
29 self.assertRaises(RuntimeError, winsound.Beep, 37, 75)
30 self.assertRaises(RuntimeError, winsound.Beep, 32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000031
32 def test_increasingfrequency(self):
Neal Norwitz21807252008-03-05 05:14:18 +000033 if _have_soundcard():
34 for i in xrange(100, 2000, 100):
35 winsound.Beep(i, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000036
37class MessageBeepTest(unittest.TestCase):
38
39 def tearDown(self):
40 time.sleep(0.5)
41
42 def test_default(self):
43 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
44 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
45 winsound.MessageBeep()
46
47 def test_ok(self):
48 winsound.MessageBeep(winsound.MB_OK)
49
50 def test_asterisk(self):
51 winsound.MessageBeep(winsound.MB_ICONASTERISK)
52
53 def test_exclamation(self):
54 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
55
56 def test_hand(self):
57 winsound.MessageBeep(winsound.MB_ICONHAND)
58
59 def test_question(self):
60 winsound.MessageBeep(winsound.MB_ICONQUESTION)
61
Trent Mickf8cf13e2006-03-16 17:34:41 +000062
Walter Dörwald7fd94242003-05-18 00:47:47 +000063class PlaySoundTest(unittest.TestCase):
64
65 def test_errors(self):
66 self.assertRaises(TypeError, winsound.PlaySound)
67 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
68 self.assertRaises(
69 RuntimeError,
70 winsound.PlaySound,
71 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
72 )
73
74 def test_alias_asterisk(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +000075 if _have_soundcard():
76 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
77 else:
78 self.assertRaises(
79 RuntimeError,
80 winsound.PlaySound,
81 'SystemAsterisk', winsound.SND_ALIAS
82 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000083
84 def test_alias_exclamation(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +000085 if _have_soundcard():
86 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
87 else:
88 self.assertRaises(
89 RuntimeError,
90 winsound.PlaySound,
91 'SystemExclamation', winsound.SND_ALIAS
92 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000093
94 def test_alias_exit(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +000095 if _have_soundcard():
96 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
97 else:
98 self.assertRaises(
99 RuntimeError,
100 winsound.PlaySound,
101 'SystemExit', winsound.SND_ALIAS
102 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000103
104 def test_alias_hand(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000105 if _have_soundcard():
106 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
107 else:
108 self.assertRaises(
109 RuntimeError,
110 winsound.PlaySound,
111 'SystemHand', winsound.SND_ALIAS
112 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000113
114 def test_alias_question(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000115 if _have_soundcard():
116 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
117 else:
118 self.assertRaises(
119 RuntimeError,
120 winsound.PlaySound,
121 'SystemQuestion', winsound.SND_ALIAS
122 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000123
124 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000125 # This test can't be expected to work on all systems. The MS
126 # PlaySound() docs say:
127 #
128 # If it cannot find the specified sound, PlaySound uses the
129 # default system event sound entry instead. If the function
130 # can find neither the system default entry nor the default
131 # sound, it makes no sound and returns FALSE.
132 #
133 # It's known to return FALSE on some real systems.
134
135 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
136 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000137
138 def test_alias_nofallback(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000139 if _have_soundcard():
140 # Note that this is not the same as asserting RuntimeError
141 # will get raised: you cannot convert this to
142 # self.assertRaises(...) form. The attempt may or may not
143 # raise RuntimeError, but it shouldn't raise anything other
144 # than RuntimeError, and that's all we're trying to test
145 # here. The MS docs aren't clear about whether the SDK
146 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
147 # True or False when the alias is unknown. On Tim's WinXP
148 # box today, it returns True (no exception is raised). What
149 # we'd really like to test is that no sound is played, but
150 # that requires first wiring an eardrum class into unittest
151 # <wink>.
152 try:
153 winsound.PlaySound(
154 '!"$%&/(#+*',
155 winsound.SND_ALIAS | winsound.SND_NODEFAULT
156 )
157 except RuntimeError:
158 pass
159 else:
160 self.assertRaises(
161 RuntimeError,
162 winsound.PlaySound,
163 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000164 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000165
166 def test_stopasync(self):
Trent Mickf8cf13e2006-03-16 17:34:41 +0000167 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000168 winsound.PlaySound(
169 'SystemQuestion',
Trent Mickf8cf13e2006-03-16 17:34:41 +0000170 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000171 )
Trent Mickf8cf13e2006-03-16 17:34:41 +0000172 time.sleep(0.5)
173 try:
174 winsound.PlaySound(
175 'SystemQuestion',
176 winsound.SND_ALIAS | winsound.SND_NOSTOP
177 )
178 except RuntimeError:
179 pass
180 else: # the first sound might already be finished
181 pass
182 winsound.PlaySound(None, winsound.SND_PURGE)
183 else:
184 self.assertRaises(
185 RuntimeError,
186 winsound.PlaySound,
187 None, winsound.SND_PURGE
188 )
189
190
191def _get_cscript_path():
192 """Return the full path to cscript.exe or None."""
193 for dir in os.environ.get("PATH", "").split(os.pathsep):
194 cscript_path = os.path.join(dir, "cscript.exe")
195 if os.path.exists(cscript_path):
196 return cscript_path
197
198__have_soundcard_cache = None
199def _have_soundcard():
200 """Return True iff this computer has a soundcard."""
201 global __have_soundcard_cache
202 if __have_soundcard_cache is None:
203 cscript_path = _get_cscript_path()
204 if cscript_path is None:
205 # Could not find cscript.exe to run our VBScript helper. Default
206 # to True: most computers these days *do* have a soundcard.
207 return True
208
209 check_script = os.path.join(os.path.dirname(__file__),
210 "check_soundcard.vbs")
211 p = subprocess.Popen([cscript_path, check_script],
212 stdout=subprocess.PIPE)
213 __have_soundcard_cache = not p.wait()
214 return __have_soundcard_cache
215
Walter Dörwald7fd94242003-05-18 00:47:47 +0000216
217def test_main():
218 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
219
220if __name__=="__main__":
221 test_main()