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