blob: 32b49d3081a9958b060e37d70357f26f6d1caf6f [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):
12
13 def test_errors(self):
14 self.assertRaises(TypeError, winsound.Beep)
15 self.assertRaises(ValueError, winsound.Beep, 36, 75)
16 self.assertRaises(ValueError, winsound.Beep, 32768, 75)
17
18 def test_extremes(self):
19 winsound.Beep(37, 75)
20 winsound.Beep(32767, 75)
21
22 def test_increasingfrequency(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000023 for i in range(100, 2000, 100):
Walter Dörwald7fd94242003-05-18 00:47:47 +000024 winsound.Beep(i, 75)
25
26class MessageBeepTest(unittest.TestCase):
27
28 def tearDown(self):
29 time.sleep(0.5)
30
31 def test_default(self):
32 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
33 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
34 winsound.MessageBeep()
35
36 def test_ok(self):
37 winsound.MessageBeep(winsound.MB_OK)
38
39 def test_asterisk(self):
40 winsound.MessageBeep(winsound.MB_ICONASTERISK)
41
42 def test_exclamation(self):
43 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
44
45 def test_hand(self):
46 winsound.MessageBeep(winsound.MB_ICONHAND)
47
48 def test_question(self):
49 winsound.MessageBeep(winsound.MB_ICONQUESTION)
50
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051
Walter Dörwald7fd94242003-05-18 00:47:47 +000052class PlaySoundTest(unittest.TestCase):
53
54 def test_errors(self):
55 self.assertRaises(TypeError, winsound.PlaySound)
56 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
57 self.assertRaises(
58 RuntimeError,
59 winsound.PlaySound,
60 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
61 )
62
63 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000064 if _have_soundcard():
65 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
66 else:
67 self.assertRaises(
68 RuntimeError,
69 winsound.PlaySound,
70 'SystemAsterisk', winsound.SND_ALIAS
71 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000072
73 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074 if _have_soundcard():
75 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
76 else:
77 self.assertRaises(
78 RuntimeError,
79 winsound.PlaySound,
80 'SystemExclamation', winsound.SND_ALIAS
81 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000082
83 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000084 if _have_soundcard():
85 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
86 else:
87 self.assertRaises(
88 RuntimeError,
89 winsound.PlaySound,
90 'SystemExit', winsound.SND_ALIAS
91 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000092
93 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 if _have_soundcard():
95 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
96 else:
97 self.assertRaises(
98 RuntimeError,
99 winsound.PlaySound,
100 'SystemHand', winsound.SND_ALIAS
101 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000102
103 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104 if _have_soundcard():
105 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
106 else:
107 self.assertRaises(
108 RuntimeError,
109 winsound.PlaySound,
110 'SystemQuestion', winsound.SND_ALIAS
111 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000112
113 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000114 # This test can't be expected to work on all systems. The MS
115 # PlaySound() docs say:
116 #
117 # If it cannot find the specified sound, PlaySound uses the
118 # default system event sound entry instead. If the function
119 # can find neither the system default entry nor the default
120 # sound, it makes no sound and returns FALSE.
121 #
122 # It's known to return FALSE on some real systems.
123
124 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
125 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000126
127 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 if _have_soundcard():
129 # Note that this is not the same as asserting RuntimeError
130 # will get raised: you cannot convert this to
131 # self.assertRaises(...) form. The attempt may or may not
132 # raise RuntimeError, but it shouldn't raise anything other
133 # than RuntimeError, and that's all we're trying to test
134 # here. The MS docs aren't clear about whether the SDK
135 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
136 # True or False when the alias is unknown. On Tim's WinXP
137 # box today, it returns True (no exception is raised). What
138 # we'd really like to test is that no sound is played, but
139 # that requires first wiring an eardrum class into unittest
140 # <wink>.
141 try:
142 winsound.PlaySound(
143 '!"$%&/(#+*',
144 winsound.SND_ALIAS | winsound.SND_NODEFAULT
145 )
146 except RuntimeError:
147 pass
148 else:
149 self.assertRaises(
150 RuntimeError,
151 winsound.PlaySound,
152 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000153 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000154
155 def test_stopasync(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000157 winsound.PlaySound(
158 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000160 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 time.sleep(0.5)
162 try:
163 winsound.PlaySound(
164 'SystemQuestion',
165 winsound.SND_ALIAS | winsound.SND_NOSTOP
166 )
167 except RuntimeError:
168 pass
169 else: # the first sound might already be finished
170 pass
171 winsound.PlaySound(None, winsound.SND_PURGE)
172 else:
173 self.assertRaises(
174 RuntimeError,
175 winsound.PlaySound,
176 None, winsound.SND_PURGE
177 )
178
179
180def _get_cscript_path():
181 """Return the full path to cscript.exe or None."""
182 for dir in os.environ.get("PATH", "").split(os.pathsep):
183 cscript_path = os.path.join(dir, "cscript.exe")
184 if os.path.exists(cscript_path):
185 return cscript_path
186
187__have_soundcard_cache = None
188def _have_soundcard():
189 """Return True iff this computer has a soundcard."""
190 global __have_soundcard_cache
191 if __have_soundcard_cache is None:
192 cscript_path = _get_cscript_path()
193 if cscript_path is None:
194 # Could not find cscript.exe to run our VBScript helper. Default
195 # to True: most computers these days *do* have a soundcard.
196 return True
197
198 check_script = os.path.join(os.path.dirname(__file__),
199 "check_soundcard.vbs")
200 p = subprocess.Popen([cscript_path, check_script],
201 stdout=subprocess.PIPE)
202 __have_soundcard_cache = not p.wait()
203 return __have_soundcard_cache
204
Walter Dörwald7fd94242003-05-18 00:47:47 +0000205
206def test_main():
207 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
208
209if __name__=="__main__":
210 test_main()