File: test_util_dbusutils.py

package info (click to toggle)
quodlibet 4.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,228 kB
  • sloc: python: 89,728; sh: 381; xml: 110; makefile: 91
file content (128 lines) | stat: -rw-r--r-- 4,465 bytes parent folder | download
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
# 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")
        assert isinstance(value, dbus.UInt32)

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

        value = apply_signature(("a",), "a(s)")
        self.assertEqual(value.signature, "s")
        assert isinstance(value, dbus.Struct)

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

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

        text = b"\xc3\xb6\xc3\xa4\xc3\xbc"
        value = apply_signature(text, "s", utf8_strings=True)
        assert isinstance(value, str)
        value = apply_signature(text, "s")
        assert isinstance(value, str)

        text = "öäü"
        value = apply_signature(text, "s", utf8_strings=True)
        assert isinstance(value, str)
        value = apply_signature(text, "s")
        assert isinstance(value, str)

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

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

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

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

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

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

    def test_validate_utf8(self):
        self.assertEqual(dbus_unicode_validate("X\ufffeX"), "X\ufffdX")
        self.assertEqual(dbus_unicode_validate(b"X\xef\xbf\xbeX"), "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")
        assert ("a1", "Position") in props
        assert ("a2", "XXX") in props
        props = x.get_properties("a2")
        assert ("a1", "Position") not in props

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

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