summaryrefslogtreecommitdiff
path: root/src/tests/tests.py.in
blob: de40cf36e28c3a351f05476204aab012bc21ee6b (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
#!@PYTHON@
# -*- coding: utf-8 -*-
import sys
import os
import tempfile
sys.path.insert(0, os.path.abspath(os.path.join(__file__, "@SRC_DIR@")))

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GLib
from dfeet.settings import Settings
from dfeet.introspection import AddressInfo
from dfeet.introspection_helper import DBusNode
from dfeet.introspection_helper import DBusInterface
from dfeet.introspection_helper import DBusProperty
from dfeet.introspection_helper import DBusSignal
from dfeet.introspection_helper import DBusMethod
import unittest

XML = """
<node>
  <interface name='org.gnome.dfeet.TestInterface'>
    <method name='HelloWorld'>
      <arg type='s' name='greeting' direction='in'/>
      <arg type='s' name='response' direction='out'/>
    </method>
    <property name="TestString" type="s" access="readwrite"/>
    <property name="TestBool" type="b" access="read"/>
    <property name="TestStruct" type="(suu)" access="read"/>
    <signal name="TestSignal">
      <arg name="SigString" type="s"/>
      <arg name="SigBool" type="b"/>
    </signal>
  </interface>
</node>
"""

DATA_DIR = os.path.abspath("@DATA_DIR@")


class IntrospectionHelperTest(unittest.TestCase):
    """tests for the introspection helper classes"""
    def setUp(self):
        self.name = "org.gnome.dfeet"
        self.object_path = "/org/gnome/dfeet"
        self.node_info = Gio.DBusNodeInfo.new_for_xml(XML)

    def test_dbus_node_info(self):
        """test DBusNode class"""
        obj_node = DBusNode(self.name, self.object_path, self.node_info)
        self.assertEqual(obj_node.name, self.name)
        self.assertEqual(obj_node.object_path, self.object_path)
        self.assertEqual(len(obj_node.node_info.interfaces), 1)

    def test_dbus_interface(self):
        """test DBusInterface class"""
        obj_node = DBusNode(self.name, self.object_path, self.node_info)
        obj_iface = DBusInterface(obj_node, obj_node.node_info.interfaces[0])
        self.assertEqual(obj_iface.name, self.name)
        self.assertEqual(obj_iface.object_path, self.object_path)
        self.assertEqual(len(obj_iface.iface_info.methods), 1)
        self.assertEqual(len(obj_iface.iface_info.properties), 3)
        self.assertEqual(len(obj_iface.iface_info.signals), 1)

    def test_dbus_property(self):
        """test DBusProperty class"""
        obj_node = DBusNode(self.name, self.object_path, self.node_info)
        obj_iface = DBusInterface(obj_node, obj_node.node_info.interfaces[0])
        obj_prop = DBusProperty(obj_iface, obj_iface.iface_info.properties[0])
        self.assertEqual(obj_prop.name, self.name)
        self.assertEqual(obj_prop.object_path, self.object_path)
        # get the markup string with value for struct prop (see bgo #702593)
        obj_prop = DBusProperty(obj_iface, obj_iface.iface_info.properties[2])
        obj_prop.value = ("string", 1, 2)
        self.assertIn("'string', 1, 2", obj_prop.markup_str)

    def test_dbus_signal(self):
        """test DBusSignal class"""
        obj_node = DBusNode(self.name, self.object_path, self.node_info)
        obj_iface = DBusInterface(obj_node, obj_node.node_info.interfaces[0])
        obj_sig = DBusSignal(obj_iface, obj_iface.iface_info.signals[0])
        self.assertEqual(obj_sig.name, self.name)
        self.assertEqual(obj_sig.object_path, self.object_path)


class AddressInfoTest(unittest.TestCase):
    """tests for the AddressInfo class and the introspection stuff"""

    def setUp(self):
        self.bus = Gio.TestDBus()
        self.bus.unset()
        self.bus.up()

    def tearDown(self):
        self.bus.stop()

    def test_bus(self):
        """introspect a name on the system bus"""
        ai = AddressInfo(DATA_DIR, self.bus.get_bus_address(), None, "org.freedesktop.DBus")

    @unittest.skip("TODO:peer to peer test not implemented")
    def test_peer_to_peer(self):
        """test a p2p connection"""
        # TODO: setup a gdbus server and test a peer to peer connection
        # (see http://developer.gnome.org/gio/unstable/GDBusServer.html#gdbus-peer-to-peer)
        pass


class SettingsTest(unittest.TestCase):
    """Tests for the Settings class.

    Why does it reimplement list values around ConfigParser rather than using
    GKeyFile which supports list values directly? Nobody knows.
    """

    def setUp(self):
        self.tempfile = tempfile.NamedTemporaryFile()

    def tearDown(self):
        self.tempfile.close()

    def test_read_empty(self):
        settings = Settings(self.tempfile.name)

        self.assertIn('windowwidth', settings.general)
        self.assertIn('windowheight', settings.general)
        self.assertEqual([], settings.general['addbus_list'])

    def test_write(self):
        settings = Settings(self.tempfile.name)
        settings.general['windowwidth'] = 800
        settings.general['windowheight'] = 600
        settings.general['addbus_list'] = ['a', 'b']
        settings.write()

        # Reload with a new instance
        settings = Settings(self.tempfile.name)
        # FIXME: yes, these come back out as strings
        self.assertEqual('800', settings.general['windowwidth'])
        self.assertEqual('600', settings.general['windowheight'])
        self.assertEqual(['a', 'b'], settings.general['addbus_list'])


if __name__ == "__main__":
    # run tests
    unittest.main()