File: test_util_dbusutils.py

package info (click to toggle)
quodlibet 4.6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,016 kB
  • sloc: python: 85,817; sh: 385; xml: 110; makefile: 91
file content (133 lines) | stat: -rw-r--r-- 4,728 bytes parent folder | download | duplicates (3)
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
# Copyright 2012 Christoph Reiter <reiter.christoph@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

from tests import TestCase, skipUnless

try:
    import dbus
except ImportError:
    dbus = None
else:
    from quodlibet.util.dbusutils import apply_signature, list_spec_properties
    from quodlibet.util.dbusutils import filter_property_spec, DBusProperty
    from quodlibet.util.dbusutils import dbus_unicode_validate


ANN1 = """
<property name="Position" type="s" access="read">
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" \
value="false"/>
</property>
<property name="MinimumRate" type="s" access="read"/>
"""

ANN2 = """
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" \
value="false"/>
<property name="Foobar" type="s" access="read">
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" \
value="invalidates"/>
</property>
<property name="XXX" type="s" access="read"/>
"""


@skipUnless(dbus, "dbus missing")
class TDbusUtils(TestCase):

    def test_prop_sig(self):
        value = apply_signature(2, "u")
        self.failUnless(isinstance(value, dbus.UInt32))

        value = apply_signature({"a": "b"}, "a{ss}")
        self.failUnlessEqual(value.signature, "ss")
        self.failUnless(isinstance(value, dbus.Dictionary))

        value = apply_signature(("a",), "a(s)")
        self.failUnlessEqual(value.signature, "s")
        self.failUnless(isinstance(value, dbus.Struct))

        value = apply_signature(("a", "b"), "as")
        self.failUnlessEqual(value.signature, "s")
        self.failUnless(isinstance(value, dbus.Array))

        self.failUnlessRaises(TypeError, apply_signature, 2, "a(s)")

        text = b'\xc3\xb6\xc3\xa4\xc3\xbc'
        value = apply_signature(text, "s", utf8_strings=True)
        self.failUnless(isinstance(value, str))
        value = apply_signature(text, "s")
        self.failUnless(isinstance(value, str))

        text = u"öäü"
        value = apply_signature(text, "s", utf8_strings=True)
        self.failUnless(isinstance(value, str))
        value = apply_signature(text, "s")
        self.failUnless(isinstance(value, str))

    def test_list_props(self):
        props = list_spec_properties(ANN1)
        self.failUnlessEqual(props["Position"]["access"], "read")
        self.failUnlessEqual(props["Position"]["emit"], "false")
        self.failUnlessEqual(props["Position"]["type"], "s")

        self.failUnlessEqual(props["MinimumRate"]["emit"], "true")

        props = list_spec_properties(ANN2)
        self.failUnlessEqual(props["Foobar"]["emit"], "invalidates")
        self.failUnlessEqual(props["XXX"]["emit"], "false")

    def test_filter_props(self):
        spec = filter_property_spec(ANN1, wl=["Position"])
        self.failUnlessEqual(
            list(list_spec_properties(spec).keys()), ["Position"])
        props = list_spec_properties(spec)
        self.failUnlessEqual(props["Position"]["emit"], "false")

        spec = filter_property_spec(ANN1, bl=["Position"])
        self.failUnlessEqual(list(list_spec_properties(spec).keys()),
                             ["MinimumRate"])

        spec = filter_property_spec(ANN1)
        self.failUnlessEqual(len(list_spec_properties(spec).keys()), 2)

    def test_validate_utf8(self):
        self.failUnlessEqual(dbus_unicode_validate(u'X\ufffeX'), u"X\ufffdX")
        self.failUnlessEqual(dbus_unicode_validate(b'X\xef\xbf\xbeX'),
                             u"X\ufffdX")

    def test_property_mixin(self):

        class X(DBusProperty):
            SUPPORTS_MULTIPLE_OBJECT_PATHS = False

            def set_introspection(self, *args):
                pass

            def get_property(self, interface, name):
                return interface

            def set_property(self, interface, name, value):
                pass

        x = X()
        x.set_properties("a1", ANN1)
        x.set_properties("a2", ANN2)
        x.implement_interface("a1", "a2")

        props = x.get_properties("a1")
        self.failUnless(("a1", "Position") in props)
        self.failUnless(("a2", "XXX") in props)
        props = x.get_properties("a2")
        self.failIf(("a1", "Position") in props)

        self.failUnlessEqual(x.get_interface("a2", "XXX"), "a2")
        self.failUnlessEqual(x.get_interface("a1", "XXX"), "a2")

        self.failUnlessEqual(x.get_value("a2", "XXX"), "a2")
        self.failUnlessEqual(x.get_value("a1", "XXX"), "a2")
        self.failUnlessEqual(x.get_value("a1", "Position"), "a1")