Guido van Rossum | 2512d6d | 2000-04-24 14:01:51 +0000 | [diff] [blame] | 1 | # Ridiculously simple test of the winsound module for Windows. |
Guido van Rossum | cdd092f | 2000-04-21 21:28:47 +0000 | [diff] [blame] | 2 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
| 5 | support.requires('audio') |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 6 | import time |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | import os |
| 8 | import subprocess |
Brian Curtin | ae16af2 | 2010-06-01 13:49:19 +0000 | [diff] [blame] | 9 | import ctypes |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 11 | winsound = support.import_module('winsound') |
Mark Dickinson | a09543c | 2010-04-13 11:43:10 +0000 | [diff] [blame] | 12 | import winreg |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 13 | |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 14 | def has_sound(sound): |
| 15 | """Find out if a particular event is configured with a default sound""" |
| 16 | try: |
Brian Curtin | ae16af2 | 2010-06-01 13:49:19 +0000 | [diff] [blame] | 17 | # Ask the mixer API for the number of devices it knows about. |
| 18 | # When there are no devices, PlaySound will fail. |
| 19 | if ctypes.windll.winmm.mixerGetNumDevs() is 0: |
| 20 | return False |
| 21 | |
Brian Curtin | f140718 | 2010-04-13 12:43:07 +0000 | [diff] [blame] | 22 | key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 23 | "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound)) |
| 24 | value = winreg.EnumValue(key, 0)[1] |
Mark Dickinson | 141b72e | 2010-04-13 11:54:59 +0000 | [diff] [blame] | 25 | if value is not "": |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 26 | return True |
| 27 | else: |
| 28 | return False |
| 29 | except WindowsError: |
| 30 | return False |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 31 | |
| 32 | class BeepTest(unittest.TestCase): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 33 | # As with PlaySoundTest, incorporate the _have_soundcard() check |
| 34 | # into our test methods. If there's no audio device present, |
| 35 | # winsound.Beep returns 0 and GetLastError() returns 127, which |
| 36 | # is: ERROR_PROC_NOT_FOUND ("The specified procedure could not |
| 37 | # be found"). (FWIW, virtual/Hyper-V systems fall under this |
| 38 | # scenario as they have no sound devices whatsoever (not even |
| 39 | # a legacy Beep device).) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 40 | |
| 41 | def test_errors(self): |
| 42 | self.assertRaises(TypeError, winsound.Beep) |
| 43 | self.assertRaises(ValueError, winsound.Beep, 36, 75) |
| 44 | self.assertRaises(ValueError, winsound.Beep, 32768, 75) |
| 45 | |
| 46 | def test_extremes(self): |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 47 | self._beep(37, 75) |
| 48 | self._beep(32767, 75) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 49 | |
| 50 | def test_increasingfrequency(self): |
Amaury Forgeot d'Arc | 0a665ce | 2008-03-20 01:02:48 +0000 | [diff] [blame] | 51 | for i in range(100, 2000, 100): |
Christian Heimes | d5e2b6f | 2008-03-19 21:50:51 +0000 | [diff] [blame] | 52 | self._beep(i, 75) |
| 53 | |
| 54 | def _beep(self, *args): |
| 55 | # these tests used to use _have_soundcard(), but it's quite |
| 56 | # possible to have a soundcard, and yet have the beep driver |
| 57 | # disabled. So basically, we have no way of knowing whether |
| 58 | # a beep should be produced or not, so currently if these |
| 59 | # tests fail we're ignoring them |
| 60 | # |
| 61 | # XXX the right fix for this is to define something like |
| 62 | # _have_enabled_beep_driver() and use that instead of the |
| 63 | # try/except below |
| 64 | try: |
| 65 | winsound.Beep(*args) |
| 66 | except RuntimeError: |
| 67 | pass |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 68 | |
| 69 | class MessageBeepTest(unittest.TestCase): |
| 70 | |
| 71 | def tearDown(self): |
| 72 | time.sleep(0.5) |
| 73 | |
| 74 | def test_default(self): |
| 75 | self.assertRaises(TypeError, winsound.MessageBeep, "bad") |
| 76 | self.assertRaises(TypeError, winsound.MessageBeep, 42, 42) |
| 77 | winsound.MessageBeep() |
| 78 | |
| 79 | def test_ok(self): |
| 80 | winsound.MessageBeep(winsound.MB_OK) |
| 81 | |
| 82 | def test_asterisk(self): |
| 83 | winsound.MessageBeep(winsound.MB_ICONASTERISK) |
| 84 | |
| 85 | def test_exclamation(self): |
| 86 | winsound.MessageBeep(winsound.MB_ICONEXCLAMATION) |
| 87 | |
| 88 | def test_hand(self): |
| 89 | winsound.MessageBeep(winsound.MB_ICONHAND) |
| 90 | |
| 91 | def test_question(self): |
| 92 | winsound.MessageBeep(winsound.MB_ICONQUESTION) |
| 93 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 94 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 95 | class PlaySoundTest(unittest.TestCase): |
| 96 | |
| 97 | def test_errors(self): |
| 98 | self.assertRaises(TypeError, winsound.PlaySound) |
| 99 | self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad") |
| 100 | self.assertRaises( |
| 101 | RuntimeError, |
| 102 | winsound.PlaySound, |
| 103 | "none", winsound.SND_ASYNC | winsound.SND_MEMORY |
| 104 | ) |
| 105 | |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 106 | @unittest.skipUnless(has_sound("SystemAsterisk"), |
| 107 | "No default SystemAsterisk") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 108 | def test_alias_asterisk(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 109 | if _have_soundcard(): |
| 110 | winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS) |
| 111 | else: |
| 112 | self.assertRaises( |
| 113 | RuntimeError, |
| 114 | winsound.PlaySound, |
| 115 | 'SystemAsterisk', winsound.SND_ALIAS |
| 116 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 117 | |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 118 | @unittest.skipUnless(has_sound("SystemExclamation"), |
| 119 | "No default SystemExclamation") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 120 | def test_alias_exclamation(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 121 | if _have_soundcard(): |
| 122 | winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS) |
| 123 | else: |
| 124 | self.assertRaises( |
| 125 | RuntimeError, |
| 126 | winsound.PlaySound, |
| 127 | 'SystemExclamation', winsound.SND_ALIAS |
| 128 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 129 | |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 130 | @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 131 | def test_alias_exit(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 132 | if _have_soundcard(): |
| 133 | winsound.PlaySound('SystemExit', winsound.SND_ALIAS) |
| 134 | else: |
| 135 | self.assertRaises( |
| 136 | RuntimeError, |
| 137 | winsound.PlaySound, |
| 138 | 'SystemExit', winsound.SND_ALIAS |
| 139 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 140 | |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 141 | @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 142 | def test_alias_hand(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 143 | if _have_soundcard(): |
| 144 | winsound.PlaySound('SystemHand', winsound.SND_ALIAS) |
| 145 | else: |
| 146 | self.assertRaises( |
| 147 | RuntimeError, |
| 148 | winsound.PlaySound, |
| 149 | 'SystemHand', winsound.SND_ALIAS |
| 150 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 151 | |
Brian Curtin | 3606760 | 2010-04-13 02:32:40 +0000 | [diff] [blame] | 152 | @unittest.skipUnless(has_sound("SystemQuestion"), |
| 153 | "No default SystemQuestion") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 154 | def test_alias_question(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 155 | if _have_soundcard(): |
| 156 | winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS) |
| 157 | else: |
| 158 | self.assertRaises( |
| 159 | RuntimeError, |
| 160 | winsound.PlaySound, |
| 161 | 'SystemQuestion', winsound.SND_ALIAS |
| 162 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 163 | |
| 164 | def test_alias_fallback(self): |
Tim Peters | 086e562 | 2003-09-22 18:38:53 +0000 | [diff] [blame] | 165 | # This test can't be expected to work on all systems. The MS |
| 166 | # PlaySound() docs say: |
| 167 | # |
| 168 | # If it cannot find the specified sound, PlaySound uses the |
| 169 | # default system event sound entry instead. If the function |
| 170 | # can find neither the system default entry nor the default |
| 171 | # sound, it makes no sound and returns FALSE. |
| 172 | # |
| 173 | # It's known to return FALSE on some real systems. |
| 174 | |
| 175 | # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) |
| 176 | return |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 177 | |
| 178 | def test_alias_nofallback(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 179 | if _have_soundcard(): |
| 180 | # Note that this is not the same as asserting RuntimeError |
| 181 | # will get raised: you cannot convert this to |
| 182 | # self.assertRaises(...) form. The attempt may or may not |
| 183 | # raise RuntimeError, but it shouldn't raise anything other |
| 184 | # than RuntimeError, and that's all we're trying to test |
| 185 | # here. The MS docs aren't clear about whether the SDK |
| 186 | # PlaySound() with SND_ALIAS and SND_NODEFAULT will return |
| 187 | # True or False when the alias is unknown. On Tim's WinXP |
| 188 | # box today, it returns True (no exception is raised). What |
| 189 | # we'd really like to test is that no sound is played, but |
| 190 | # that requires first wiring an eardrum class into unittest |
| 191 | # <wink>. |
| 192 | try: |
| 193 | winsound.PlaySound( |
| 194 | '!"$%&/(#+*', |
| 195 | winsound.SND_ALIAS | winsound.SND_NODEFAULT |
| 196 | ) |
| 197 | except RuntimeError: |
| 198 | pass |
| 199 | else: |
| 200 | self.assertRaises( |
| 201 | RuntimeError, |
| 202 | winsound.PlaySound, |
| 203 | '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT |
Tim Peters | ad9a7c4 | 2004-05-16 05:36:30 +0000 | [diff] [blame] | 204 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 205 | |
| 206 | def test_stopasync(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 207 | if _have_soundcard(): |
Walter Dörwald | 8bcbe6a | 2003-06-30 11:57:52 +0000 | [diff] [blame] | 208 | winsound.PlaySound( |
| 209 | 'SystemQuestion', |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 210 | winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP |
Walter Dörwald | 8bcbe6a | 2003-06-30 11:57:52 +0000 | [diff] [blame] | 211 | ) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 212 | time.sleep(0.5) |
| 213 | try: |
| 214 | winsound.PlaySound( |
| 215 | 'SystemQuestion', |
| 216 | winsound.SND_ALIAS | winsound.SND_NOSTOP |
| 217 | ) |
| 218 | except RuntimeError: |
| 219 | pass |
| 220 | else: # the first sound might already be finished |
| 221 | pass |
| 222 | winsound.PlaySound(None, winsound.SND_PURGE) |
| 223 | else: |
Stefan Krah | af9c3cf | 2010-04-12 15:33:12 +0000 | [diff] [blame] | 224 | # Issue 8367: PlaySound(None, winsound.SND_PURGE) |
| 225 | # does not raise on systems without a sound card. |
| 226 | pass |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | def _get_cscript_path(): |
| 230 | """Return the full path to cscript.exe or None.""" |
| 231 | for dir in os.environ.get("PATH", "").split(os.pathsep): |
| 232 | cscript_path = os.path.join(dir, "cscript.exe") |
| 233 | if os.path.exists(cscript_path): |
| 234 | return cscript_path |
| 235 | |
| 236 | __have_soundcard_cache = None |
| 237 | def _have_soundcard(): |
| 238 | """Return True iff this computer has a soundcard.""" |
| 239 | global __have_soundcard_cache |
| 240 | if __have_soundcard_cache is None: |
| 241 | cscript_path = _get_cscript_path() |
| 242 | if cscript_path is None: |
| 243 | # Could not find cscript.exe to run our VBScript helper. Default |
| 244 | # to True: most computers these days *do* have a soundcard. |
| 245 | return True |
| 246 | |
| 247 | check_script = os.path.join(os.path.dirname(__file__), |
| 248 | "check_soundcard.vbs") |
| 249 | p = subprocess.Popen([cscript_path, check_script], |
| 250 | stdout=subprocess.PIPE) |
| 251 | __have_soundcard_cache = not p.wait() |
| 252 | return __have_soundcard_cache |
| 253 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 254 | |
| 255 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 256 | support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 257 | |
| 258 | if __name__=="__main__": |
| 259 | test_main() |