forked from AndreMiras/pycaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_core.py
78 lines (68 loc) · 2.51 KB
/
test_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Verifies core features run as expected.
"""
from __future__ import print_function
import _ctypes
import sys
import unittest
from unittest import mock
from contextlib import contextmanager
from pycaw.pycaw import AudioDeviceState, AudioUtilities
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
@contextmanager
def captured_output():
new_out, new_err = StringIO(), StringIO()
old_out, old_err = sys.stdout, sys.stderr
try:
sys.stdout, sys.stderr = new_out, new_err
yield sys.stdout, sys.stderr
finally:
sys.stdout, sys.stderr = old_out, old_err
class TestCore(unittest.TestCase):
def test_session_unicode(self):
"""Makes sure printing a session doesn't crash."""
with captured_output() as (out, err):
sessions = AudioUtilities.GetAllSessions()
print("sessions: %s" % sessions)
for session in sessions:
print("session: %s" % session)
print("session.Process: %s" % session.Process)
def test_device_unicode(self):
"""Makes sure printing a device doesn't crash."""
with captured_output() as (out, err):
devices = AudioUtilities.GetAllDevices()
print("devices: %s" % devices)
for device in devices:
print("device: %s" % device)
def test_device_failed_properties(self):
"""Test that failed properties do not crash the script"""
dev = mock.Mock()
dev.GetId = mock.Mock(return_value="id")
dev.GetState = mock.Mock(return_value=AudioDeviceState.Active)
store = mock.Mock()
store.GetCount = mock.Mock(return_value=1)
store.GetAt = mock.Mock(return_value="pk")
store.GetValue = mock.Mock(
side_effect=_ctypes.COMError(None, None, None)
)
dev.OpenPropertyStore = mock.Mock(return_value=store)
with captured_output() as (out, err):
AudioUtilities.CreateDevice(dev)
self.assertTrue(
"UserWarning: COMError attempting to get property 0 from device"
in err.getvalue()
)
def test_getallsessions_reliability(self):
"""
Verifies AudioUtilities.GetAllSessions() is reliable
even calling it multiple times, refs:
https://github.com/AndreMiras/pycaw/issues/1
"""
for _ in range(100):
sessions = AudioUtilities.GetAllSessions()
self.assertTrue(len(sessions) > 0)
if __name__ == '__main__':
unittest.main()