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