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