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