dbus.service: Make it possible to unexport objects (fd.o#10457)
[dbus-python-phuang.git] / test / test-signals.py
blob797f70ccbcf644fd26a475bde2b33b45487d128c
1 #!/usr/bin/env python
3 # Copyright (C) 2004 Red Hat Inc. <http://www.redhat.com/>
4 # Copyright (C) 2005, 2006 Collabora Ltd. <http://www.collabora.co.uk/>
6 # Licensed under the Academic Free License version 2.1
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 import sys
24 import os
25 import unittest
26 import time
27 import logging
29 builddir = os.path.normpath(os.environ["DBUS_TOP_BUILDDIR"])
30 pydir = os.path.normpath(os.environ["DBUS_TOP_SRCDIR"])
32 import dbus
33 import _dbus_bindings
34 import gobject
35 import dbus.glib
36 import dbus.service
39 logging.basicConfig()
40 logging.getLogger().setLevel(1)
41 logger = logging.getLogger('test-signals')
44 pkg = dbus.__file__
45 if not pkg.startswith(pydir):
46 raise Exception("DBus modules (%s) are not being picked up from the package"%pkg)
48 if not _dbus_bindings.__file__.startswith(builddir):
49 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings.__file__)
51 class TestSignals(unittest.TestCase):
52 def setUp(self):
53 logger.info('setUp()')
54 self.bus = dbus.SessionBus()
55 self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuitePythonService", "/org/freedesktop/DBus/TestSuitePythonObject")
56 self.remote_object_follow = self.bus.get_object("org.freedesktop.DBus.TestSuitePythonService", "/org/freedesktop/DBus/TestSuitePythonObject", follow_name_owner_changes=True)
57 self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.TestSuiteInterface")
58 self.iface_follow = dbus.Interface(self.remote_object_follow, "org.freedesktop.DBus.TestSuiteInterface")
59 self.in_test = None
61 def signal_test_impl(self, iface, name, test_removal=False):
62 self.in_test = name
63 # using append rather than assignment here to avoid scoping issues
64 result = []
66 def _timeout_handler():
67 logger.debug('_timeout_handler for %s: current state %s', name, self.in_test)
68 if self.in_test == name:
69 main_loop.quit()
70 def _signal_handler(s, sender, path):
71 logger.debug('_signal_handler for %s: current state %s', name, self.in_test)
72 if self.in_test not in (name, name + '+removed'):
73 return
74 logger.info('Received signal from %s:%s, argument is %r',
75 sender, path, s)
76 result.append('received')
77 main_loop.quit()
78 def _rm_timeout_handler():
79 logger.debug('_timeout_handler for %s: current state %s', name, self.in_test)
80 if self.in_test == name + '+removed':
81 main_loop.quit()
83 logger.info('Testing %s', name)
84 match = iface.connect_to_signal('SignalOneString', _signal_handler,
85 sender_keyword='sender',
86 path_keyword='path')
87 logger.info('Waiting for signal...')
88 iface.EmitSignal('SignalOneString', 0)
89 source_id = gobject.timeout_add(1000, _timeout_handler)
90 main_loop.run()
91 if not result:
92 raise AssertionError('Signal did not arrive within 1 second')
93 logger.debug('Removing match')
94 match.remove()
95 gobject.source_remove(source_id)
96 if test_removal:
97 self.in_test = name + '+removed'
98 logger.info('Testing %s', name)
99 result = []
100 iface.EmitSignal('SignalOneString', 0)
101 source_id = gobject.timeout_add(1000, _rm_timeout_handler)
102 main_loop.run()
103 if result:
104 raise AssertionError('Signal should not have arrived, but did')
105 gobject.source_remove(source_id)
107 def testSignal(self):
108 self.signal_test_impl(self.iface, 'Signal')
110 def testRemoval(self):
111 self.signal_test_impl(self.iface, 'Removal', True)
113 def testSignalAgain(self):
114 self.signal_test_impl(self.iface, 'SignalAgain')
116 def testRemovalAgain(self):
117 self.signal_test_impl(self.iface, 'RemovalAgain', True)
119 def testSignalF(self):
120 self.signal_test_impl(self.iface_follow, 'Signal')
122 def testRemovalF(self):
123 self.signal_test_impl(self.iface_follow, 'Removal', True)
125 def testSignalAgainF(self):
126 self.signal_test_impl(self.iface_follow, 'SignalAgain')
128 def testRemovalAgainF(self):
129 self.signal_test_impl(self.iface_follow, 'RemovalAgain', True)
131 if __name__ == '__main__':
132 main_loop = gobject.MainLoop()
133 gobject.threads_init()
134 dbus.glib.init_threads()
136 logger.info('Starting unit test')
137 unittest.main()