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