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