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