summaryrefslogtreecommitdiff
path: root/mpv-test.py
blob: 4fd18bd6dfbd62819d266c542bf0820fd696ace6 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3

import unittest
from unittest import mock
import math
import threading
from contextlib import contextmanager
import gc
import os.path
import time

import mpv


TESTVID = os.path.join(os.path.dirname(__file__), 'test.webm')
MPV_ERRORS = [ l(ec) for ec, l in mpv.ErrorCode.EXCEPTION_DICT.items() if l ]

class TestProperties(unittest.TestCase):
    @contextmanager
    def swallow_mpv_errors(self):
        try:
            yield
        except Exception as e:
            for ex in MPV_ERRORS:
                if e.args[:2] == ex.args:
                    break
            else:
                raise

    def setUp(self):
        self.m = mpv.MPV()

    def test_sanity(self):
        for name, (ptype, access) in mpv.ALL_PROPERTIES.items():
            self.assertTrue('r' in access or 'w' in access)
            self.assertRegex(name, '^[-0-9a-z]+$')
            self.assertIn(ptype, (int, float, str, mpv.ynbool))

    def test_read(self):
        for name, (ptype, access) in mpv.ALL_PROPERTIES.items():
            if 'r' in access:
                name =  name.replace('-', '_')
                with self.subTest(property_name=name):
                    with self.swallow_mpv_errors():
                        rv = getattr(self.m, name)
                if rv is not None: # Technically, any property can return None (even if of type e.g. int)
                    self.assertEqual(type(rv), ptype, msg=name)

    def test_write(self):
        for name, (ptype, access) in mpv.ALL_PROPERTIES.items():
            if 'w' in access:
                name =  name.replace('-', '_')
                with self.swallow_mpv_errors():
                    if ptype == int:
                        setattr(self.m, name, 0)
                        setattr(self.m, name, 1)
                        setattr(self.m, name, -1)
                    elif ptype == float:
                        setattr(self.m, name, 0.0)
                        setattr(self.m, name, 1)
                        setattr(self.m, name, 1.0)
                        setattr(self.m, name, -1.0)
                        setattr(self.m, name, math.nan)
                    elif ptype == str:
                        setattr(self.m, name, 'foo')
                        setattr(self.m, name, '')
                        setattr(self.m, name, 'bazbazbaz'*1000)
                    elif ptype == mpv.ynbool:
                        if 'r' in access:
                            setattr(self.m, name, 'yes')
                            self.assertTrue(getattr(self.m, name))
                            self.assertEqual(getattr(self.m, name), True)
                            setattr(self.m, name, b'yes')
                            self.assertTrue(getattr(self.m, name))
                            setattr(self.m, name, True)
                            self.assertTrue(getattr(self.m, name))

                            setattr(self.m, name, 'no')
                            self.assertFalse(getattr(self.m, name))
                            self.assertEqual(getattr(self.m, name), False)
                            setattr(self.m, name, b'no')
                            self.assertFalse(getattr(self.m, name))
                            setattr(self.m, name, False)
                            self.assertFalse(getattr(self.m, name))
                        else:
                            setattr(self.m, name, 'yes')
                            setattr(self.m, name, b'yes')
                            setattr(self.m, name, True)

    def tearDown(self):
        del self.m

class ObservePropertyTest(unittest.TestCase):
    def test_observe_property(self):
        handler = mock.Mock()

        m = mpv.MPV()
        m.loop = 'inf'

        m.observe_property('loop', handler)

        m.loop = 'no'
        self.assertEqual(m.loop, 'no')

        m.loop = 'inf'
        self.assertEqual(m.loop, 'inf')

        time.sleep(0.02)
        m.unobserve_property(handler)

        m.loop = 'no'
        m.loop = 'inf'
        m.terminate() # needed for synchronization of event thread
        handler.assert_has_calls([mock.call('loop', 'no'), mock.call('loop', 'inf')])


class TestLifecycle(unittest.TestCase):
    def test_create_destroy(self):
        thread_names = lambda: [ t.name for t in threading.enumerate() ]
        self.assertNotIn('MPVEventHandlerThread', thread_names())
        m = mpv.MPV()
        self.assertIn('MPVEventHandlerThread', thread_names())
        del m
        gc.collect()
        self.assertNotIn('MPVEventHandlerThread', thread_names())

    def test_flags(self):
        with self.assertRaises(AttributeError):
            mpv.MPV('this-option-does-not-exist')
        m = mpv.MPV('no-video', 'cursor-autohide-fs-only', 'fs')
        self.assertTrue(m.fullscreen)
        self.assertEqual(m.cursor_autohide, '1000')

    def test_options(self):
        with self.assertRaises(AttributeError):
            mpv.MPV(this_option_does_not_exists=23)
        m = mpv.MPV(osd_level=0, loop='inf', deinterlace='no')
        self.assertEqual(m.osd_level, 0)
        self.assertEqual(m.loop, 'inf')
        self.assertEqual(m.deinterlace, 'no')

    def test_event_callback(self):
        handler = mock.Mock()
        m = mpv.MPV('no-video')
        m.event_callbacks.append(handler)
        m.play(TESTVID)
        m.wait_for_playback()
        del m
        handler.assert_has_calls([
                mock.call({'reply_userdata': 0, 'error': 0, 'event_id': 6, 'event': None}),
                mock.call({'reply_userdata': 0, 'error': 0, 'event_id': 9, 'event': None}),
                mock.call({'reply_userdata': 0, 'error': 0, 'event_id': 7, 'event': {'reason': 4}}),
                mock.call({'reply_userdata': 0, 'error': 0, 'event_id': 11, 'event': None}),
                mock.call({'reply_userdata': 0, 'error': 0, 'event_id': 1, 'event': None})
            ], any_order=True)
    
    def test_log_handler(self):
        handler = mock.Mock()
        m = mpv.MPV('no-video', log_handler=handler)
        m.play(TESTVID)
        m.wait_for_playback()
        del m
        handler.assert_has_calls([
            mock.call('info', 'cplayer', 'Playing: test.webm'),
            mock.call('info', 'cplayer', '     Video --vid=1 (*) (vp8)'),
            mock.call('fatal', 'cplayer', 'No video or audio streams selected.'),
            mock.call('info', 'cplayer', '')])


if __name__ == '__main__':
    unittest.main()