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