* python/dbus_bindings.pyx (MessageIter::append_strict): check for
[platform/upstream/dbus.git] / test / python / test-client.py
1 #!/usr/bin/env python
2 import sys
3 import os
4 import unittest
5 import time
6
7 builddir = os.environ["DBUS_TOP_BUILDDIR"]
8 pydir = builddir + "/python"
9
10 sys.path.insert(0, pydir)
11 sys.path.insert(0, pydir + "/.libs")
12
13 import dbus
14 import dbus_bindings
15 import gobject
16 import dbus.glib
17
18 if not dbus.__file__.startswith(pydir):
19     raise Exception("DBus modules are not being picked up from the package")
20
21 if not dbus_bindings.__file__.startswith(pydir):
22     raise Exception("DBus modules are not being picked up from the package")
23
24 test_types_vals = [1, 12323231, 3.14159265, 99999999.99,
25                  "dude", "123", "What is all the fuss about?", "gob@gob.com",
26                  [1,2,3], ["how", "are", "you"], [1.23,2.3], [1], ["Hello"],
27                  (1,2,3), (1,), (1,"2",3), ("2", "what"), ("you", 1.2),
28                  {1:"a", 2:"b"}, {"a":1, "b":2}, {1:1.1, 2:2.2}, {1.1:"a", 1.2:"b"}, 
29                  [[1,2,3],[2,3,4]], [["a","b"],["c","d"]],
30                  ([1,2,3],"c", 1.2, ["a","b","c"], {"a": (1,"v"), "b": (2,"d")})
31                  ]
32
33 class TestDBusBindings(unittest.TestCase):
34     def setUp(self):
35         self.bus = dbus.SessionBus()
36         self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuitePythonService", "/org/freedesktop/DBus/TestSuitePythonObject")
37         self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.TestSuiteInterface")
38
39     def testInterfaceKeyword(self):
40         #test dbus_interface parameter
41         print self.remote_object.Echo("dbus_interface on Proxy test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
42         print self.iface.Echo("dbus_interface on Interface test Passed", dbus_interface = "org.freedesktop.DBus.TestSuiteInterface")
43         self.assert_(True)
44         
45     def testIntrospection(self):
46         #test introspection
47         print "\n********* Introspection Test ************"
48         print self.remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
49         print "Introspection test passed"
50         self.assert_(True)
51
52     def testPythonTypes(self):
53         #test sending python types and getting them back
54         print "\n********* Testing Python Types ***********"
55                  
56         for send_val in test_types_vals:
57             print "Testing %s"% str(send_val)
58             recv_val = self.iface.Echo(send_val)
59             self.assertEquals(send_val, recv_val)
60
61     def testBenchmarkIntrospect(self):
62         print "\n********* Benchmark Introspect ************"
63         a = time.time()
64         print a
65         print self.iface.GetComplexArray()
66         b = time.time()
67         print b
68         print "Delta: %f" % (b - a)
69         self.assert_(True)
70
71     def testAsyncCalls(self):
72         #test sending python types and getting them back async
73         print "\n********* Testing Async Calls ***********"
74
75         
76         main_loop = gobject.MainLoop()
77         class async_check:
78             def __init__(self, test_controler, expected_result, do_exit):
79                 self.expected_result = expected_result
80                 self.do_exit = do_exit
81                 self.test_controler = test_controler
82
83             def callback(self, val):
84                 if self.do_exit:
85                     main_loop.quit()
86
87                 self.test_controler.assertEquals(val, self.expected_result)
88                 
89             def error_handler(error):
90                 print error
91                 if self.do_exit:
92                     main_loop.quit()
93
94                 self.test_controler.assert_(val, False)
95         
96         last_type = test_types_vals[-1]
97         for send_val in test_types_vals:
98             print "Testing %s"% str(send_val)
99             check = async_check(self, send_val, last_type == send_val) 
100             recv_val = self.iface.Echo(send_val, 
101                                        reply_handler = check.callback,
102                                        error_handler = check.error_handler)
103             
104         main_loop.run()
105
106 class TestDBusPythonToGLibBindings(unittest.TestCase):
107     def setUp(self):
108         self.bus = dbus.SessionBus()
109         self.remote_object = self.bus.get_object("org.freedesktop.DBus.TestSuiteGLibService", "/org/freedesktop/DBus/Tests/MyTestObject")
110         self.iface = dbus.Interface(self.remote_object, "org.freedesktop.DBus.Tests.MyObject")
111                             
112     def testIntrospection(self):
113         #test introspection
114         print "\n********* Introspection Test ************"
115         print self.remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable")
116         print "Introspection test passed"
117         self.assert_(True)
118
119     def testCalls(self):
120         print "\n********* Call Test ************"
121         result =  self.iface.ManyArgs(1000, 'Hello GLib', 2)
122         print result
123         self.assert_(result == [2002.0, 'HELLO GLIB'])
124         
125         arg0 = {"Dude": 1, "john": "palmieri", "python": 2.4}
126         result = self.iface.ManyStringify(arg0)
127         print result
128        
129         print "Call test passed"
130         self.assert_(True)
131
132     #this crashes glib so disable it for now
133     #until glib is fixed
134     """
135     def testPythonTypes(self):
136         print "\n********* Testing Python Types ***********"
137                  
138         for send_val in test_types_vals:
139             print "Testing %s"% str(send_val)
140             recv_val = self.iface.EchoVariant(send_val)
141             self.assertEquals(send_val, recv_val)
142     """
143
144 if __name__ == '__main__':
145     gobject.threads_init()
146     dbus.glib.init_threads()
147
148     unittest.main()
149