Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | # Ridiculously simple test of the winsound module for Windows. |
2 | ||
3 | import unittest | |
4 | from test import test_support | |
5 | import winsound, time | |
6 | ||
7 | class BeepTest(unittest.TestCase): | |
8 | ||
9 | def test_errors(self): | |
10 | self.assertRaises(TypeError, winsound.Beep) | |
11 | self.assertRaises(ValueError, winsound.Beep, 36, 75) | |
12 | self.assertRaises(ValueError, winsound.Beep, 32768, 75) | |
13 | ||
14 | def test_extremes(self): | |
15 | winsound.Beep(37, 75) | |
16 | winsound.Beep(32767, 75) | |
17 | ||
18 | def test_increasingfrequency(self): | |
19 | for i in xrange(100, 2000, 100): | |
20 | winsound.Beep(i, 75) | |
21 | ||
22 | class MessageBeepTest(unittest.TestCase): | |
23 | ||
24 | def tearDown(self): | |
25 | time.sleep(0.5) | |
26 | ||
27 | def test_default(self): | |
28 | self.assertRaises(TypeError, winsound.MessageBeep, "bad") | |
29 | self.assertRaises(TypeError, winsound.MessageBeep, 42, 42) | |
30 | winsound.MessageBeep() | |
31 | ||
32 | def test_ok(self): | |
33 | winsound.MessageBeep(winsound.MB_OK) | |
34 | ||
35 | def test_asterisk(self): | |
36 | winsound.MessageBeep(winsound.MB_ICONASTERISK) | |
37 | ||
38 | def test_exclamation(self): | |
39 | winsound.MessageBeep(winsound.MB_ICONEXCLAMATION) | |
40 | ||
41 | def test_hand(self): | |
42 | winsound.MessageBeep(winsound.MB_ICONHAND) | |
43 | ||
44 | def test_question(self): | |
45 | winsound.MessageBeep(winsound.MB_ICONQUESTION) | |
46 | ||
47 | class PlaySoundTest(unittest.TestCase): | |
48 | ||
49 | def test_errors(self): | |
50 | self.assertRaises(TypeError, winsound.PlaySound) | |
51 | self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad") | |
52 | self.assertRaises( | |
53 | RuntimeError, | |
54 | winsound.PlaySound, | |
55 | "none", winsound.SND_ASYNC | winsound.SND_MEMORY | |
56 | ) | |
57 | ||
58 | def test_alias_asterisk(self): | |
59 | winsound.PlaySound('SystemAsterisk', winsound.SND_ALIAS) | |
60 | ||
61 | def test_alias_exclamation(self): | |
62 | winsound.PlaySound('SystemExclamation', winsound.SND_ALIAS) | |
63 | ||
64 | def test_alias_exit(self): | |
65 | winsound.PlaySound('SystemExit', winsound.SND_ALIAS) | |
66 | ||
67 | def test_alias_hand(self): | |
68 | winsound.PlaySound('SystemHand', winsound.SND_ALIAS) | |
69 | ||
70 | def test_alias_question(self): | |
71 | winsound.PlaySound('SystemQuestion', winsound.SND_ALIAS) | |
72 | ||
73 | def test_alias_fallback(self): | |
74 | # This test can't be expected to work on all systems. The MS | |
75 | # PlaySound() docs say: | |
76 | # | |
77 | # If it cannot find the specified sound, PlaySound uses the | |
78 | # default system event sound entry instead. If the function | |
79 | # can find neither the system default entry nor the default | |
80 | # sound, it makes no sound and returns FALSE. | |
81 | # | |
82 | # It's known to return FALSE on some real systems. | |
83 | ||
84 | # winsound.PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) | |
85 | return | |
86 | ||
87 | def test_alias_nofallback(self): | |
88 | # Note that this is not the same as asserting RuntimeError | |
89 | # will get raised: you cannot convert this to | |
90 | # self.assertRaises(...) form. The attempt may or may not | |
91 | # raise RuntimeError, but it shouldn't raise anything other | |
92 | # than RuntimeError, and that's all we're trying to test here. | |
93 | # The MS docs aren't clear about whether the SDK PlaySound() | |
94 | # with SND_ALIAS and SND_NODEFAULT will return True or False when | |
95 | # the alias is unknown. On Tim's WinXP box today, it returns | |
96 | # True (no exception is raised). What we'd really like to test | |
97 | # is that no sound is played, but that requires first wiring an | |
98 | # eardrum class into unittest <wink>. | |
99 | try: | |
100 | winsound.PlaySound( | |
101 | '!"$%&/(#+*', | |
102 | winsound.SND_ALIAS | winsound.SND_NODEFAULT | |
103 | ) | |
104 | except RuntimeError: | |
105 | pass | |
106 | ||
107 | def test_stopasync(self): | |
108 | winsound.PlaySound( | |
109 | 'SystemQuestion', | |
110 | winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP | |
111 | ) | |
112 | time.sleep(0.5) | |
113 | try: | |
114 | winsound.PlaySound( | |
115 | 'SystemQuestion', | |
116 | winsound.SND_ALIAS | winsound.SND_NOSTOP | |
117 | ) | |
118 | except RuntimeError: | |
119 | pass | |
120 | else: # the first sound might already be finished | |
121 | pass | |
122 | winsound.PlaySound(None, winsound.SND_PURGE) | |
123 | ||
124 | def test_main(): | |
125 | test_support.run_unittest(BeepTest, MessageBeepTest, PlaySoundTest) | |
126 | ||
127 | if __name__=="__main__": | |
128 | test_main() |