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 |
| 4 | from test import test_support |
Antoine Pitrou | 508d7d3 | 2012-11-24 18:25:25 +0100 | [diff] [blame] | 5 | test_support.requires('audio') |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 6 | import time |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 7 | import os |
| 8 | import subprocess |
| 9 | |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 10 | winsound = test_support.import_module('winsound') |
Antoine Pitrou | 920e7e9 | 2010-08-10 00:47:48 +0000 | [diff] [blame] | 11 | ctypes = test_support.import_module('ctypes') |
Mark Dickinson | 2c4798b | 2010-04-13 11:37:23 +0000 | [diff] [blame] | 12 | import _winreg |
R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 13 | |
Brian Curtin | d5c50b3 | 2010-04-13 02:25:20 +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 | 824912e | 2010-06-01 13:29:13 +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 | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 22 | key = _winreg.OpenKeyEx(_winreg.HKEY_CURRENT_USER, |
| 23 | "AppEvents\Schemes\Apps\.Default\{0}\.Default".format(sound)) |
| 24 | value = _winreg.EnumValue(key, 0)[1] |
| 25 | if value is not u"": |
| 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): |
Neal Norwitz | 2180725 | 2008-03-05 05:14:18 +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): |
Steven Bethard | 8906575 | 2008-03-18 19:04:32 +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): |
Steven Bethard | 8906575 | 2008-03-18 19:04:32 +0000 | [diff] [blame] | 51 | for i in xrange(100, 2000, 100): |
| 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 | |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +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 | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 106 | @unittest.skipUnless(has_sound("SystemAsterisk"), "No default SystemAsterisk") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 107 | def test_alias_asterisk(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 108 | if _have_soundcard(): |
| 109 | winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS) |
| 110 | else: |
| 111 | self.assertRaises( |
| 112 | RuntimeError, |
| 113 | winsound.PlaySound, |
| 114 | 'SystemAsterisk', winsound.SND_ALIAS |
| 115 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 116 | |
Brian Curtin | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 117 | @unittest.skipUnless(has_sound("SystemExclamation"), "No default SystemExclamation") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 118 | def test_alias_exclamation(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 119 | if _have_soundcard(): |
| 120 | winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS) |
| 121 | else: |
| 122 | self.assertRaises( |
| 123 | RuntimeError, |
| 124 | winsound.PlaySound, |
| 125 | 'SystemExclamation', winsound.SND_ALIAS |
| 126 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 127 | |
Brian Curtin | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 128 | @unittest.skipUnless(has_sound("SystemExit"), "No default SystemExit") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 129 | def test_alias_exit(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 130 | if _have_soundcard(): |
| 131 | winsound.PlaySound('SystemExit', winsound.SND_ALIAS) |
| 132 | else: |
| 133 | self.assertRaises( |
| 134 | RuntimeError, |
| 135 | winsound.PlaySound, |
| 136 | 'SystemExit', winsound.SND_ALIAS |
| 137 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 138 | |
Brian Curtin | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 139 | @unittest.skipUnless(has_sound("SystemHand"), "No default SystemHand") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 140 | def test_alias_hand(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 141 | if _have_soundcard(): |
| 142 | winsound.PlaySound('SystemHand', winsound.SND_ALIAS) |
| 143 | else: |
| 144 | self.assertRaises( |
| 145 | RuntimeError, |
| 146 | winsound.PlaySound, |
| 147 | 'SystemHand', winsound.SND_ALIAS |
| 148 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 149 | |
Brian Curtin | d5c50b3 | 2010-04-13 02:25:20 +0000 | [diff] [blame] | 150 | @unittest.skipUnless(has_sound("SystemQuestion"), "No default SystemQuestion") |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 151 | def test_alias_question(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 152 | if _have_soundcard(): |
| 153 | winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS) |
| 154 | else: |
| 155 | self.assertRaises( |
| 156 | RuntimeError, |
| 157 | winsound.PlaySound, |
| 158 | 'SystemQuestion', winsound.SND_ALIAS |
| 159 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 160 | |
| 161 | def test_alias_fallback(self): |
Tim Peters | 086e562 | 2003-09-22 18:38:53 +0000 | [diff] [blame] | 162 | # This test can't be expected to work on all systems. The MS |
| 163 | # PlaySound() docs say: |
| 164 | # |
| 165 | # If it cannot find the specified sound, PlaySound uses the |
| 166 | # default system event sound entry instead. If the function |
| 167 | # can find neither the system default entry nor the default |
| 168 | # sound, it makes no sound and returns FALSE. |
| 169 | # |
| 170 | # It's known to return FALSE on some real systems. |
| 171 | |
| 172 | # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) |
| 173 | return |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 174 | |
| 175 | def test_alias_nofallback(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 176 | if _have_soundcard(): |
| 177 | # Note that this is not the same as asserting RuntimeError |
| 178 | # will get raised: you cannot convert this to |
| 179 | # self.assertRaises(...) form. The attempt may or may not |
| 180 | # raise RuntimeError, but it shouldn't raise anything other |
| 181 | # than RuntimeError, and that's all we're trying to test |
| 182 | # here. The MS docs aren't clear about whether the SDK |
| 183 | # PlaySound() with SND_ALIAS and SND_NODEFAULT will return |
| 184 | # True or False when the alias is unknown. On Tim's WinXP |
| 185 | # box today, it returns True (no exception is raised). What |
| 186 | # we'd really like to test is that no sound is played, but |
| 187 | # that requires first wiring an eardrum class into unittest |
| 188 | # <wink>. |
| 189 | try: |
| 190 | winsound.PlaySound( |
| 191 | '!"$%&/(#+*', |
| 192 | winsound.SND_ALIAS | winsound.SND_NODEFAULT |
| 193 | ) |
| 194 | except RuntimeError: |
| 195 | pass |
| 196 | else: |
| 197 | self.assertRaises( |
| 198 | RuntimeError, |
| 199 | winsound.PlaySound, |
| 200 | '!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT |
Tim Peters | ad9a7c4 | 2004-05-16 05:36:30 +0000 | [diff] [blame] | 201 | ) |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 202 | |
| 203 | def test_stopasync(self): |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 204 | if _have_soundcard(): |
Walter Dörwald | 8bcbe6a | 2003-06-30 11:57:52 +0000 | [diff] [blame] | 205 | winsound.PlaySound( |
| 206 | 'SystemQuestion', |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 207 | winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP |
Walter Dörwald | 8bcbe6a | 2003-06-30 11:57:52 +0000 | [diff] [blame] | 208 | ) |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 209 | time.sleep(0.5) |
| 210 | try: |
| 211 | winsound.PlaySound( |
| 212 | 'SystemQuestion', |
| 213 | winsound.SND_ALIAS | winsound.SND_NOSTOP |
| 214 | ) |
| 215 | except RuntimeError: |
| 216 | pass |
| 217 | else: # the first sound might already be finished |
| 218 | pass |
| 219 | winsound.PlaySound(None, winsound.SND_PURGE) |
| 220 | else: |
Stefan Krah | 9aca91d | 2010-04-12 15:21:25 +0000 | [diff] [blame] | 221 | # Issue 8367: PlaySound(None, winsound.SND_PURGE) |
| 222 | # does not raise on systems without a sound card. |
| 223 | pass |
Trent Mick | f8cf13e | 2006-03-16 17:34:41 +0000 | [diff] [blame] | 224 | |
| 225 | |
| 226 | def _get_cscript_path(): |
| 227 | """Return the full path to cscript.exe or None.""" |
| 228 | for dir in os.environ.get("PATH", "").split(os.pathsep): |
| 229 | cscript_path = os.path.join(dir, "cscript.exe") |
| 230 | if os.path.exists(cscript_path): |
| 231 | return cscript_path |
| 232 | |
| 233 | __have_soundcard_cache = None |
| 234 | def _have_soundcard(): |
| 235 | """Return True iff this computer has a soundcard.""" |
| 236 | global __have_soundcard_cache |
| 237 | if __have_soundcard_cache is None: |
| 238 | cscript_path = _get_cscript_path() |
| 239 | if cscript_path is None: |
| 240 | # Could not find cscript.exe to run our VBScript helper. Default |
| 241 | # to True: most computers these days *do* have a soundcard. |
| 242 | return True |
| 243 | |
| 244 | check_script = os.path.join(os.path.dirname(__file__), |
| 245 | "check_soundcard.vbs") |
| 246 | p = subprocess.Popen([cscript_path, check_script], |
| 247 | stdout=subprocess.PIPE) |
| 248 | __have_soundcard_cache = not p.wait() |
| 249 | return __have_soundcard_cache |
| 250 | |
Walter Dörwald | 7fd9424 | 2003-05-18 00:47:47 +0000 | [diff] [blame] | 251 | |
| 252 | def test_main(): |
| 253 | test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest) |
| 254 | |
| 255 | if __name__=="__main__": |
| 256 | test_main() |