7 builddir = os.environ["DBUS_TOP_BUILDDIR"]
8 pydir = builddir + "/python"
10 sys.path.insert(0, pydir)
11 sys.path.insert(0, pydir + "/.libs")
19 if not dbus.__file__.startswith(pydir):
20 raise Exception("DBus modules are not being picked up from the package")
22 if not dbus_bindings.__file__.startswith(pydir):
23 raise Exception("DBus modules are not being picked up from the package")
25 test_types_vals = [1, 12323231, 3.14159265, 99999999.99,
26 "dude", "123", "What is all the fuss about?", "gob@gob.com",
27 u'\\u310c\\u310e\\u3114', u'\\u0413\\u0414\\u0415',
28 u'\\u2200software \\u2203crack', u'\\xf4\\xe5\\xe8',
29 [1,2,3], ["how", "are", "you"], [1.23,2.3], [1], ["Hello"],
30 (1,2,3), (1,), (1,"2",3), ("2", "what"), ("you", 1.2),
31 {1:"a", 2:"b"}, {"a":1, "b":2}, #{"a":(1,"B")},
32 {1:1.1, 2:2.2}, [[1,2,3],[2,3,4]], [["a","b"],["c","d"]],
34 #([1,2,3],"c", 1.2, ["a","b","c"], {"a": (1,"v"), "b": (2,"d")})
37 class TestDBusBindings(unittest.TestCase):
39 self.bus = dbus.SessionBus()
40 self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuitePythonService", "/org/freedesktop/DBus/TestSuitePythonObject")
41 self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.TestSuiteInterface")
43 def testInterfaceKeyword(self):
44 #test dbus_interface parameter
45 print self.remote_object.Echo("dbus_interface on Proxy test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
46 print self.iface.Echo("dbus_interface on Interface test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
49 def testIntrospection(self):
51 print "\n********* Introspection Test ************"
52 print self.remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
53 print "Introspection test passed"
56 def testPythonTypes(self):
57 #test sending python types and getting them back
58 print "\n********* Testing Python Types ***********"
60 for send_val in test_types_vals:
61 print "Testing %s"% str(send_val)
62 recv_val = self.iface.Echo(send_val)
63 self.assertEquals(send_val, recv_val)
65 def testBenchmarkIntrospect(self):
66 print "\n********* Benchmark Introspect ************"
69 print self.iface.GetComplexArray()
72 print "Delta: %f" % (b - a)
75 def testAsyncCalls(self):
76 #test sending python types and getting them back async
77 print "\n********* Testing Async Calls ***********"
80 main_loop = gobject.MainLoop()
82 def __init__(self, test_controler, expected_result, do_exit):
83 self.expected_result = expected_result
84 self.do_exit = do_exit
85 self.test_controler = test_controler
87 def callback(self, val):
92 self.test_controler.assertEquals(val, self.expected_result)
94 print "%s:\n%s" % (e.__class__, e)
96 def error_handler(self, error):
101 self.test_controler.assert_(val, False)
103 last_type = test_types_vals[-1]
104 for send_val in test_types_vals:
105 print "Testing %s"% str(send_val)
106 check = async_check(self, send_val, last_type == send_val)
107 recv_val = self.iface.Echo(send_val,
108 reply_handler = check.callback,
109 error_handler = check.error_handler)
113 def testReturnMarshalling(self):
114 print "\n********* Testing return marshalling ***********"
116 # these values are the same as in the server, and the
117 # methods should only succeed when they are called with
118 # the right value number, because they have out_signature
119 # decorations, and return an unmatching type when called
120 # with a different number
121 values = ["", ("",""), ("","",""), [], {}, ["",""], ["","",""]]
123 (self.iface.ReturnOneString, set([0]), set([0])),
124 (self.iface.ReturnTwoStrings, set([1, 5]), set([5])),
125 (self.iface.ReturnStruct, set([1, 5]), set([1])),
126 # all of our test values are sequences so will marshall correctly into an array :P
127 (self.iface.ReturnArray, set(range(len(values))), set([3, 5, 6])),
128 (self.iface.ReturnDict, set([0, 3, 4]), set([4]))
131 for (method, success_values, return_values) in methods:
132 print "\nTrying correct behaviour of", method._method_name
133 for value in range(len(values)):
137 print "%s(%s) raised %s" % (method._method_name, repr(values[value]), e.__class__)
139 # should fail if it tried to marshal the wrong type
140 self.assert_(value not in success_values, "%s should succeed when we ask it to return %s\n%s" % (method._method_name, repr(values[value]), e))
142 print "%s(%s) returned %s" % (method._method_name, repr(values[value]), repr(ret))
144 # should only succeed if it's the right return type
145 self.assert_(value in success_values, "%s should fail when we ask it to return %s" % (method._method_name, repr(values[value])))
147 # check the value is right too :D
148 returns = map(lambda n: values[n], return_values)
149 self.assert_(ret in returns, "%s should return one of %s" % (method._method_name, repr(returns)))
152 def testInheritance(self):
153 print "\n********* Testing inheritance from dbus.method.Interface ***********"
154 ret = self.iface.CheckInheritance()
155 print "CheckInheritance returned %s" % ret
156 self.assert_(ret, "overriding CheckInheritance from TestInterface failed")
158 def testAsyncMethods(self):
159 print "\n********* Testing asynchronous method implementation *******"
160 for (async, fail) in ((False, False), (False, True), (True, False), (True, True)):
162 val = ('a', 1, False, [1,2], {1:2})
163 print "calling AsynchronousMethod with %s %s %s" % (async, fail, val)
164 ret = self.iface.AsynchronousMethod(async, fail, val)
166 print "%s:\n%s" % (e.__class__, e)
169 self.assert_(not fail)
171 self.assert_(val == ret)
173 def testBusInstanceCaching(self):
174 print "\n********* Testing dbus.Bus instance sharing *********"
176 # unfortunately we can't test the system bus here
177 # but the codepaths are the same
178 for (cls, type, func) in ((dbus.SessionBus, dbus.Bus.TYPE_SESSION, dbus.Bus.get_session), (dbus.StarterBus, dbus.Bus.TYPE_STARTER, dbus.Bus.get_starter)):
179 print "\nTesting %s:" % cls.__name__
182 share_type = dbus.Bus(bus_type=type)
185 private_cls = cls(private=True)
186 private_type = dbus.Bus(bus_type=type, private=True)
187 private_func = func(private=True)
189 print " - checking shared instances are the same..."
190 self.assert_(share_cls == share_type, '%s should equal %s' % (share_cls, share_type))
191 self.assert_(share_type == share_func, '%s should equal %s' % (share_type, share_func))
193 print " - checking private instances are distinct from the shared instance..."
194 self.assert_(share_cls != private_cls, '%s should not equal %s' % (share_cls, private_cls))
195 self.assert_(share_type != private_type, '%s should not equal %s' % (share_type, private_type))
196 self.assert_(share_func != private_func, '%s should not equal %s' % (share_func, private_func))
198 print " - checking private instances are distinct from each other..."
199 self.assert_(private_cls != private_type, '%s should not equal %s' % (private_cls, private_type))
200 self.assert_(private_type != private_func, '%s should not equal %s' % (private_type, private_func))
201 self.assert_(private_func != private_cls, '%s should not equal %s' % (private_func, private_cls))
203 def testBusNameCreation(self):
204 print '\n******** Testing BusName creation ********'
205 test = [('org.freedesktop.DBus.Python.TestName', True),
206 ('org.freedesktop.DBus.Python.TestName', True),
207 ('org.freedesktop.DBus.Python.InvalidName&^*%$', False),
208 ('org.freedesktop.DBus.TestSuitePythonService', False)]
209 # For some reason this actually succeeds
210 # ('org.freedesktop.DBus', False)]
212 # make a method call to ensure the test service is active
213 self.iface.Echo("foo")
216 for (name, succeed) in test:
218 print "requesting %s" % name
219 busname = dbus.service.BusName(name)
221 print "%s:\n%s" % (e.__class__, e)
222 self.assert_(not succeed, 'did not expect registering bus name %s to fail' % name)
225 self.assert_(succeed, 'expected registering bus name %s to fail'% name)
227 self.assert_(names[name] == busname, 'got a new instance for same name %s' % name)
228 print "instance of %s re-used, good!" % name
230 names[name] = busname
232 class TestDBusPythonToGLibBindings(unittest.TestCase):
234 self.bus = dbus.SessionBus()
235 self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuiteGLibService", "/org/freedesktop/DBus/Tests/MyTestObject")
236 self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.Tests.MyObject")
238 def testIntrospection(self):
240 print "\n********* Introspection Test ************"
241 print self.remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
242 print "Introspection test passed"
246 print "\n********* Call Test ************"
247 result = self.iface.ManyArgs(1000, 'Hello GLib', 2)
249 self.assert_(result == [2002.0, 'HELLO GLIB'])
251 arg0 = {"Dude": 1, "john": "palmieri", "python": 2.4}
252 result = self.iface.ManyStringify(arg0)
255 print "Call test passed"
258 def testPythonTypes(self):
259 print "\n********* Testing Python Types ***********"
261 for send_val in test_types_vals:
262 print "Testing %s"% str(send_val)
263 recv_val = self.iface.EchoVariant(send_val)
264 self.assertEquals(send_val, recv_val)
266 if __name__ == '__main__':
267 gobject.threads_init()
268 dbus.glib.init_threads()