Simplify dbus_bindings backwards compatibility glue
[dbus-python-phuang.git] / test / test-signals.py
blobf496de9f8bec33b75556cfcb3218d41ae5ac9b1e
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.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.TestSuiteInterface")
57 self.in_test = None
59 def signal_test_impl(self, name, test_removal=False):
60 self.in_test = name
61 # using append rather than assignment here to avoid scoping issues
62 result = []
64 def _timeout_handler():
65 logger.debug('_timeout_handler for %s: current state %s', name, self.in_test)
66 if self.in_test == name:
67 main_loop.quit()
68 def _signal_handler(s, sender, path):
69 logger.debug('_signal_handler for %s: current state %s', name, self.in_test)
70 if self.in_test not in (name, name + '+removed'):
71 return
72 logger.info('Received signal from %s:%s, argument is %r',
73 sender, path, s)
74 result.append('received')
75 main_loop.quit()
76 def _rm_timeout_handler():
77 logger.debug('_timeout_handler for %s: current state %s', name, self.in_test)
78 if self.in_test == name + '+removed':
79 main_loop.quit()
81 logger.info('Testing %s', name)
82 match = self.iface.connect_to_signal('SignalOneString', _signal_handler,
83 sender_keyword='sender',
84 path_keyword='path')
85 logger.info('Waiting for signal...')
86 self.iface.EmitSignal('SignalOneString', 0)
87 source_id = gobject.timeout_add(1000, _timeout_handler)
88 main_loop.run()
89 if not result:
90 raise AssertionError('Signal did not arrive within 1 second')
91 logger.debug('Removing match')
92 match.remove()
93 gobject.source_remove(source_id)
94 if test_removal:
95 self.in_test = name + '+removed'
96 logger.info('Testing %s', name)
97 result = []
98 self.iface.EmitSignal('SignalOneString', 0)
99 source_id = gobject.timeout_add(1000, _rm_timeout_handler)
100 main_loop.run()
101 if result:
102 raise AssertionError('Signal should not have arrived, but did')
103 gobject.source_remove(source_id)
105 def testSignal(self):
106 self.signal_test_impl('Signal')
108 def testRemoval(self):
109 self.signal_test_impl('Removal', True)
111 def testSignalAgain(self):
112 self.signal_test_impl('SignalAgain')
114 def testRemovalAgain(self):
115 self.signal_test_impl('RemovalAgain', True)
117 if __name__ == '__main__':
118 main_loop = gobject.MainLoop()
119 gobject.threads_init()
120 dbus.glib.init_threads()
122 logger.info('Starting unit test')
123 unittest.main()