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