• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Ridiculously simple test of the winsound module for Windows.
2
3import functools
4import os
5import subprocess
6import time
7import unittest
8
9from test import support
10
11support.requires('audio')
12winsound = support.import_module('winsound')
13
14
15# Unless we actually have an ear in the room, we have no idea whether a sound
16# actually plays, and it's incredibly flaky trying to figure out if a sound
17# even *should* play.  Instead of guessing, just call the function and assume
18# it either passed or raised the RuntimeError we expect in case of failure.
19def sound_func(func):
20    @functools.wraps(func)
21    def wrapper(*args, **kwargs):
22        try:
23            ret = func(*args, **kwargs)
24        except RuntimeError as e:
25            if support.verbose:
26                print(func.__name__, 'failed:', e)
27        else:
28            if support.verbose:
29                print(func.__name__, 'returned')
30            return ret
31    return wrapper
32
33
34safe_Beep = sound_func(winsound.Beep)
35safe_MessageBeep = sound_func(winsound.MessageBeep)
36safe_PlaySound = sound_func(winsound.PlaySound)
37
38
39class BeepTest(unittest.TestCase):
40
41    def test_errors(self):
42        self.assertRaises(TypeError, winsound.Beep)
43        self.assertRaises(ValueError, winsound.Beep, 36, 75)
44        self.assertRaises(ValueError, winsound.Beep, 32768, 75)
45
46    def test_extremes(self):
47        safe_Beep(37, 75)
48        safe_Beep(32767, 75)
49
50    def test_increasingfrequency(self):
51        for i in range(100, 2000, 100):
52            safe_Beep(i, 75)
53
54    def test_keyword_args(self):
55        safe_Beep(duration=75, frequency=2000)
56
57
58class MessageBeepTest(unittest.TestCase):
59
60    def tearDown(self):
61        time.sleep(0.5)
62
63    def test_default(self):
64        self.assertRaises(TypeError, winsound.MessageBeep, "bad")
65        self.assertRaises(TypeError, winsound.MessageBeep, 42, 42)
66        safe_MessageBeep()
67
68    def test_ok(self):
69        safe_MessageBeep(winsound.MB_OK)
70
71    def test_asterisk(self):
72        safe_MessageBeep(winsound.MB_ICONASTERISK)
73
74    def test_exclamation(self):
75        safe_MessageBeep(winsound.MB_ICONEXCLAMATION)
76
77    def test_hand(self):
78        safe_MessageBeep(winsound.MB_ICONHAND)
79
80    def test_question(self):
81        safe_MessageBeep(winsound.MB_ICONQUESTION)
82
83    def test_keyword_args(self):
84        safe_MessageBeep(type=winsound.MB_OK)
85
86
87class PlaySoundTest(unittest.TestCase):
88
89    def test_errors(self):
90        self.assertRaises(TypeError, winsound.PlaySound)
91        self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad")
92        self.assertRaises(
93            RuntimeError,
94            winsound.PlaySound,
95            "none", winsound.SND_ASYNC | winsound.SND_MEMORY
96        )
97        self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0)
98        self.assertRaises(TypeError, winsound.PlaySound, "bad",
99                          winsound.SND_MEMORY)
100        self.assertRaises(TypeError, winsound.PlaySound, 1, 0)
101
102    def test_keyword_args(self):
103        safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit")
104
105    def test_snd_memory(self):
106        with open(support.findfile('pluck-pcm8.wav',
107                                   subdir='audiodata'), 'rb') as f:
108            audio_data = f.read()
109        safe_PlaySound(audio_data, winsound.SND_MEMORY)
110        audio_data = bytearray(audio_data)
111        safe_PlaySound(audio_data, winsound.SND_MEMORY)
112
113    def test_snd_filename(self):
114        fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
115        safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
116
117    def test_aliases(self):
118        aliases = [
119            "SystemAsterisk",
120            "SystemExclamation",
121            "SystemExit",
122            "SystemHand",
123            "SystemQuestion",
124        ]
125        for alias in aliases:
126            with self.subTest(alias=alias):
127                safe_PlaySound(alias, winsound.SND_ALIAS)
128
129    def test_alias_fallback(self):
130        safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS)
131
132    def test_alias_nofallback(self):
133        safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT)
134
135    def test_stopasync(self):
136        safe_PlaySound(
137            'SystemQuestion',
138            winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP
139        )
140        time.sleep(0.5)
141        safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP)
142        # Issue 8367: PlaySound(None, winsound.SND_PURGE)
143        # does not raise on systems without a sound card.
144        winsound.PlaySound(None, winsound.SND_PURGE)
145
146
147if __name__ == "__main__":
148    unittest.main()
149