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