blob: 0d6ddf9ca0905373f4e96bffa4e611f9091291a4 [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):
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 Heimesdd15f6c2008-03-16 00:07:10 +000026 if _have_soundcard():
27 winsound.Beep(37, 75)
28 winsound.Beep(32767, 75)
29 else:
30 self.assertRaises(RuntimeError, winsound.Beep, 37, 75)
31 self.assertRaises(RuntimeError, winsound.Beep, 32767, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000032
33 def test_increasingfrequency(self):
Christian Heimesdd15f6c2008-03-16 00:07:10 +000034 if _have_soundcard():
35 for i in range(100, 2000, 100):
36 winsound.Beep(i, 75)
Walter Dörwald7fd94242003-05-18 00:47:47 +000037
38class MessageBeepTest(unittest.TestCase):
39
40 def tearDown(self):
41 time.sleep(0.5)
42
43 def test_default(self):
44 self.assertRaises(TypeError, winsound.MessageBeep, "bad")
45 self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
46 winsound.MessageBeep()
47
48 def test_ok(self):
49 winsound.MessageBeep(winsound.MB_OK)
50
51 def test_asterisk(self):
52 winsound.MessageBeep(winsound.MB_ICONASTERISK)
53
54 def test_exclamation(self):
55 winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)
56
57 def test_hand(self):
58 winsound.MessageBeep(winsound.MB_ICONHAND)
59
60 def test_question(self):
61 winsound.MessageBeep(winsound.MB_ICONQUESTION)
62
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063
Walter Dörwald7fd94242003-05-18 00:47:47 +000064class PlaySoundTest(unittest.TestCase):
65
66 def test_errors(self):
67 self.assertRaises(TypeError, winsound.PlaySound)
68 self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
69 self.assertRaises(
70 RuntimeError,
71 winsound.PlaySound,
72 "none", winsound.SND_ASYNC | winsound.SND_MEMORY
73 )
74
75 def test_alias_asterisk(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076 if _have_soundcard():
77 winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS)
78 else:
79 self.assertRaises(
80 RuntimeError,
81 winsound.PlaySound,
82 'SystemAsterisk', winsound.SND_ALIAS
83 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000084
85 def test_alias_exclamation(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 if _have_soundcard():
87 winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS)
88 else:
89 self.assertRaises(
90 RuntimeError,
91 winsound.PlaySound,
92 'SystemExclamation', winsound.SND_ALIAS
93 )
Walter Dörwald7fd94242003-05-18 00:47:47 +000094
95 def test_alias_exit(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000096 if _have_soundcard():
97 winsound.PlaySound('SystemExit', winsound.SND_ALIAS)
98 else:
99 self.assertRaises(
100 RuntimeError,
101 winsound.PlaySound,
102 'SystemExit', winsound.SND_ALIAS
103 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000104
105 def test_alias_hand(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106 if _have_soundcard():
107 winsound.PlaySound('SystemHand', winsound.SND_ALIAS)
108 else:
109 self.assertRaises(
110 RuntimeError,
111 winsound.PlaySound,
112 'SystemHand', winsound.SND_ALIAS
113 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000114
115 def test_alias_question(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116 if _have_soundcard():
117 winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS)
118 else:
119 self.assertRaises(
120 RuntimeError,
121 winsound.PlaySound,
122 'SystemQuestion', winsound.SND_ALIAS
123 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000124
125 def test_alias_fallback(self):
Tim Peters086e5622003-09-22 18:38:53 +0000126 # This test can't be expected to work on all systems. The MS
127 # PlaySound() docs say:
128 #
129 # If it cannot find the specified sound, PlaySound uses the
130 # default system event sound entry instead. If the function
131 # can find neither the system default entry nor the default
132 # sound, it makes no sound and returns FALSE.
133 #
134 # It's known to return FALSE on some real systems.
135
136 # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
137 return
Walter Dörwald7fd94242003-05-18 00:47:47 +0000138
139 def test_alias_nofallback(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000140 if _have_soundcard():
141 # Note that this is not the same as asserting RuntimeError
142 # will get raised: you cannot convert this to
143 # self.assertRaises(...) form. The attempt may or may not
144 # raise RuntimeError, but it shouldn't raise anything other
145 # than RuntimeError, and that's all we're trying to test
146 # here. The MS docs aren't clear about whether the SDK
147 # PlaySound() with SND_ALIAS and SND_NODEFAULT will return
148 # True or False when the alias is unknown. On Tim's WinXP
149 # box today, it returns True (no exception is raised). What
150 # we'd really like to test is that no sound is played, but
151 # that requires first wiring an eardrum class into unittest
152 # <wink>.
153 try:
154 winsound.PlaySound(
155 '!"$%&/(#+*',
156 winsound.SND_ALIAS | winsound.SND_NODEFAULT
157 )
158 except RuntimeError:
159 pass
160 else:
161 self.assertRaises(
162 RuntimeError,
163 winsound.PlaySound,
164 '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT
Tim Petersad9a7c42004-05-16 05:36:30 +0000165 )
Walter Dörwald7fd94242003-05-18 00:47:47 +0000166
167 def test_stopasync(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000168 if _have_soundcard():
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000169 winsound.PlaySound(
170 'SystemQuestion',
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
Walter Dörwald8bcbe6a2003-06-30 11:57:52 +0000172 )
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173 time.sleep(0.5)
174 try:
175 winsound.PlaySound(
176 'SystemQuestion',
177 winsound.SND_ALIAS | winsound.SND_NOSTOP
178 )
179 except RuntimeError:
180 pass
181 else: # the first sound might already be finished
182 pass
183 winsound.PlaySound(None, winsound.SND_PURGE)
184 else:
185 self.assertRaises(
186 RuntimeError,
187 winsound.PlaySound,
188 None, winsound.SND_PURGE
189 )
190
191
192def _get_cscript_path():
193 """Return the full path to cscript.exe or None."""
194 for dir in os.environ.get("PATH", "").split(os.pathsep):
195 cscript_path = os.path.join(dir, "cscript.exe")
196 if os.path.exists(cscript_path):
197 return cscript_path
198
199__have_soundcard_cache = None
200def _have_soundcard():
201 """Return True iff this computer has a soundcard."""
202 global __have_soundcard_cache
203 if __have_soundcard_cache is None:
204 cscript_path = _get_cscript_path()
205 if cscript_path is None:
206 # Could not find cscript.exe to run our VBScript helper. Default
207 # to True: most computers these days *do* have a soundcard.
208 return True
209
210 check_script = os.path.join(os.path.dirname(__file__),
211 "check_soundcard.vbs")
212 p = subprocess.Popen([cscript_path, check_script],
213 stdout=subprocess.PIPE)
214 __have_soundcard_cache = not p.wait()
215 return __have_soundcard_cache
216
Walter Dörwald7fd94242003-05-18 00:47:47 +0000217
218def test_main():
219 test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest)
220
221if __name__=="__main__":
222 test_main()