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