| 
									
										
										
										
											2000-04-24 14:01:51 +00:00
										 |  |  | # Ridiculously simple test of the winsound module for Windows. | 
					
						
							| 
									
										
										
										
											2000-04-21 21:28:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  | import functools | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2020-08-06 19:51:29 +08:00
										 |  |  | from test.support import import_helper | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  | support.requires('audio') | 
					
						
							| 
									
										
										
										
											2020-08-06 19:51:29 +08:00
										 |  |  | winsound = import_helper.import_module('winsound') | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Unless we actually have an ear in the room, we have no idea whether a sound | 
					
						
							|  |  |  | # actually plays, and it's incredibly flaky trying to figure out if a sound | 
					
						
							|  |  |  | # even *should* play.  Instead of guessing, just call the function and assume | 
					
						
							|  |  |  | # it either passed or raised the RuntimeError we expect in case of failure. | 
					
						
							|  |  |  | def sound_func(func): | 
					
						
							|  |  |  |     @functools.wraps(func) | 
					
						
							|  |  |  |     def wrapper(*args, **kwargs): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             ret = func(*args, **kwargs) | 
					
						
							|  |  |  |         except RuntimeError as e: | 
					
						
							|  |  |  |             if support.verbose: | 
					
						
							|  |  |  |                 print(func.__name__, 'failed:', e) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if support.verbose: | 
					
						
							|  |  |  |                 print(func.__name__, 'returned') | 
					
						
							|  |  |  |             return ret | 
					
						
							|  |  |  |     return wrapper | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | safe_Beep = sound_func(winsound.Beep) | 
					
						
							|  |  |  | safe_MessageBeep = sound_func(winsound.MessageBeep) | 
					
						
							|  |  |  | safe_PlaySound = sound_func(winsound.PlaySound) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BeepTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_errors(self): | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.Beep) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, winsound.Beep, 36, 75) | 
					
						
							|  |  |  |         self.assertRaises(ValueError, winsound.Beep, 32768, 75) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_extremes(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_Beep(37, 75) | 
					
						
							|  |  |  |         safe_Beep(32767, 75) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_increasingfrequency(self): | 
					
						
							| 
									
										
										
										
											2008-03-20 01:02:48 +00:00
										 |  |  |         for i in range(100, 2000, 100): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |             safe_Beep(i, 75) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:32:43 -05:00
										 |  |  |     def test_keyword_args(self): | 
					
						
							|  |  |  |         safe_Beep(duration=75, frequency=2000) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | class MessageBeepTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def tearDown(self): | 
					
						
							|  |  |  |         time.sleep(0.5) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_default(self): | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.MessageBeep, "bad") | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.MessageBeep, 42, 42) | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_MessageBeep() | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_ok(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_MessageBeep(winsound.MB_OK) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_asterisk(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_MessageBeep(winsound.MB_ICONASTERISK) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_exclamation(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_MessageBeep(winsound.MB_ICONEXCLAMATION) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_hand(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_MessageBeep(winsound.MB_ICONHAND) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_question(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_MessageBeep(winsound.MB_ICONQUESTION) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:32:43 -05:00
										 |  |  |     def test_keyword_args(self): | 
					
						
							|  |  |  |         safe_MessageBeep(type=winsound.MB_OK) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | class PlaySoundTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_errors(self): | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.PlaySound) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.PlaySound, "bad", "bad") | 
					
						
							|  |  |  |         self.assertRaises( | 
					
						
							|  |  |  |             RuntimeError, | 
					
						
							|  |  |  |             winsound.PlaySound, | 
					
						
							|  |  |  |             "none", winsound.SND_ASYNC | winsound.SND_MEMORY | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2016-09-05 16:31:21 -05:00
										 |  |  |         self.assertRaises(TypeError, winsound.PlaySound, b"bad", 0) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.PlaySound, "bad", | 
					
						
							|  |  |  |                           winsound.SND_MEMORY) | 
					
						
							|  |  |  |         self.assertRaises(TypeError, winsound.PlaySound, 1, 0) | 
					
						
							| 
									
										
										
										
											2017-06-28 08:30:06 +03:00
										 |  |  |         # embedded null character | 
					
						
							|  |  |  |         self.assertRaises(ValueError, winsound.PlaySound, 'bad\0', 0) | 
					
						
							| 
									
										
										
										
											2016-09-05 16:31:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:32:43 -05:00
										 |  |  |     def test_keyword_args(self): | 
					
						
							|  |  |  |         safe_PlaySound(flags=winsound.SND_ALIAS, sound="SystemExit") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 16:31:21 -05:00
										 |  |  |     def test_snd_memory(self): | 
					
						
							|  |  |  |         with open(support.findfile('pluck-pcm8.wav', | 
					
						
							|  |  |  |                                    subdir='audiodata'), 'rb') as f: | 
					
						
							|  |  |  |             audio_data = f.read() | 
					
						
							|  |  |  |         safe_PlaySound(audio_data, winsound.SND_MEMORY) | 
					
						
							|  |  |  |         audio_data = bytearray(audio_data) | 
					
						
							|  |  |  |         safe_PlaySound(audio_data, winsound.SND_MEMORY) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_snd_filename(self): | 
					
						
							|  |  |  |         fn = support.findfile('pluck-pcm8.wav', subdir='audiodata') | 
					
						
							|  |  |  |         safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |     def test_aliases(self): | 
					
						
							|  |  |  |         aliases = [ | 
					
						
							|  |  |  |             "SystemAsterisk", | 
					
						
							|  |  |  |             "SystemExclamation", | 
					
						
							|  |  |  |             "SystemExit", | 
					
						
							|  |  |  |             "SystemHand", | 
					
						
							|  |  |  |             "SystemQuestion", | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         for alias in aliases: | 
					
						
							|  |  |  |             with self.subTest(alias=alias): | 
					
						
							|  |  |  |                 safe_PlaySound(alias, winsound.SND_ALIAS) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_alias_fallback(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_alias_nofallback(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 18:08:27 -05:00
										 |  |  |         safe_PlaySound('!"$%&/(#+*', winsound.SND_ALIAS | winsound.SND_NODEFAULT) | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_stopasync(self): | 
					
						
							| 
									
										
										
										
											2016-09-05 16:06:56 -05:00
										 |  |  |         safe_PlaySound( | 
					
						
							|  |  |  |             'SystemQuestion', | 
					
						
							|  |  |  |             winsound.SND_ALIAS | winsound.SND_ASYNC | winsound.SND_LOOP | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         time.sleep(0.5) | 
					
						
							|  |  |  |         safe_PlaySound('SystemQuestion', winsound.SND_ALIAS | winsound.SND_NOSTOP) | 
					
						
							|  |  |  |         # Issue 8367: PlaySound(None, winsound.SND_PURGE) | 
					
						
							|  |  |  |         # does not raise on systems without a sound card. | 
					
						
							|  |  |  |         winsound.PlaySound(None, winsound.SND_PURGE) | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-05-18 00:47:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 15:00:43 -05:00
										 |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     unittest.main() |