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
29 builddir
= os
.path
.normpath(os
.environ
["DBUS_TOP_BUILDDIR"])
30 pydir
= os
.path
.normpath(os
.environ
["DBUS_TOP_SRCDIR"])
40 logging
.getLogger().setLevel(1)
41 logger
= logging
.getLogger('test-signals')
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
):
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")
59 def signal_test_impl(self
, name
, test_removal
=False):
61 # using append rather than assignment here to avoid scoping issues
64 def _timeout_handler():
65 logger
.debug('_timeout_handler for %s: current state %s', name
, self
.in_test
)
66 if self
.in_test
== name
:
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'):
72 logger
.info('Received signal from %s:%s, argument is %r',
74 result
.append('received')
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':
81 logger
.info('Testing %s', name
)
82 match
= self
.iface
.connect_to_signal('SignalOneString', _signal_handler
,
83 sender_keyword
='sender',
85 logger
.info('Waiting for signal...')
86 self
.iface
.EmitSignal('SignalOneString', 0)
87 source_id
= gobject
.timeout_add(1000, _timeout_handler
)
90 raise AssertionError('Signal did not arrive within 1 second')
91 logger
.debug('Removing match')
93 gobject
.source_remove(source_id
)
95 self
.in_test
= name
+ '+removed'
96 logger
.info('Testing %s', name
)
98 self
.iface
.EmitSignal('SignalOneString', 0)
99 source_id
= gobject
.timeout_add(1000, _rm_timeout_handler
)
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')