3 # Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
5 # Licensed under the Academic Free License version 2.1
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 from traceback
import print_exc
29 builddir
= os
.path
.normpath(os
.environ
["DBUS_TOP_BUILDDIR"])
30 pydir
= os
.path
.normpath(os
.environ
["DBUS_TOP_SRCDIR"])
34 import dbus
.types
as types
36 # Check that we're using the right versions
37 if not dbus
.__file
__.startswith(pydir
):
38 raise Exception("DBus modules (%s) are not being picked up from the package"%dbus
.__file
__)
39 if not _dbus_bindings
.__file
__.startswith(builddir
):
40 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings
.__file
__)
41 assert (_dbus_bindings
._python
_version
& 0xffff0000
42 == sys
.hexversion
& 0xffff0000), \
43 '_dbus_bindings was compiled for Python %x but this is Python %x, '\
44 'a different major version'\
45 % (_dbus_bindings
._python
_version
, sys
.hexversion
)
46 assert _dbus_bindings
.__version
__ == os
.environ
['DBUS_PYTHON_VERSION'], \
47 '_dbus_bindings was compiled as version %s but Automake says '\
48 'we should be version %s' \
49 % (_dbus_bindings
.__version
__, os
.environ
['DBUS_PYTHON_VERSION'])
51 class TestTypes(unittest
.TestCase
):
53 def test_Dictionary(self
):
54 self
.assertEquals(types
.Dictionary({'foo':'bar'}), {'foo':'bar'})
55 self
.assertEquals(types
.Dictionary({}, variant_level
=2), {})
56 self
.assertEquals(types
.Dictionary({}, variant_level
=2).variant_level
, 2)
59 self
.assertEquals(types
.Array(['foo','bar']), ['foo','bar'])
60 self
.assertEquals(types
.Array([], variant_level
=2), [])
61 self
.assertEquals(types
.Array([], variant_level
=2).variant_level
, 2)
63 def test_Double(self
):
64 self
.assertEquals(types
.Double(0.0), 0.0)
65 self
.assertEquals(types
.Double(0.125, variant_level
=2), 0.125)
66 self
.assertEquals(types
.Double(0.125, variant_level
=2).variant_level
, 2)
68 def test_Struct(self
):
69 x
= types
.Struct(('',))
70 self
.assertEquals(x
.variant_level
, 0)
71 self
.assertEquals(x
, ('',))
72 x
= types
.Struct('abc', variant_level
=42)
73 self
.assertEquals(x
.variant_level
, 42)
74 self
.assertEquals(x
, ('a','b','c'))
77 self
.assertEquals(types
.Byte('x', variant_level
=2), types
.Byte(ord('x')))
79 def test_integers(self
):
80 # This is an API guarantee. Note that exactly which of these types
81 # are ints and which of them are longs is *not* guaranteed.
82 for cls
in (types
.Int16
, types
.UInt16
, types
.Int32
, types
.UInt32
,
83 types
.Int64
, types
.UInt64
):
84 self
.assert_(issubclass(cls
, (int, long)))
85 self
.assert_(isinstance(cls(0), (int, long)))
86 self
.assertEquals(cls(0), 0)
87 self
.assertEquals(cls(23, variant_level
=1), 23)
88 self
.assertEquals(cls(23, variant_level
=1).variant_level
, 1)
90 def test_integer_limits_16(self
):
91 self
.assertEquals(types
.Int16(0x7fff), 0x7fff)
92 self
.assertEquals(types
.Int16(-0x8000), -0x8000)
93 self
.assertEquals(types
.UInt16(0xffff), 0xffff)
94 self
.assertRaises(Exception, types
.Int16
, 0x8000)
95 self
.assertRaises(Exception, types
.Int16
, -0x8001)
96 self
.assertRaises(Exception, types
.UInt16
, 0x10000)
98 def test_integer_limits_32(self
):
99 self
.assertEquals(types
.Int32(0x7fffffff), 0x7fffffff)
100 self
.assertEquals(types
.Int32(-0x80000000L
), -0x80000000L
)
101 self
.assertEquals(types
.UInt32(0xffffffffL
), 0xffffffffL
)
102 self
.assertRaises(Exception, types
.Int32
, 0x80000000L
)
103 self
.assertRaises(Exception, types
.Int32
, -0x80000001L
)
104 self
.assertRaises(Exception, types
.UInt32
, 0x100000000L
)
106 def test_integer_limits_64(self
):
107 self
.assertEquals(types
.Int64(0x7fffffffffffffffL
), 0x7fffffffffffffffL
)
108 self
.assertEquals(types
.Int64(-0x8000000000000000L
), -0x8000000000000000L
)
109 self
.assertEquals(types
.UInt64(0xffffffffffffffffL
), 0xffffffffffffffffL
)
110 self
.assertRaises(Exception, types
.Int16
, 0x8000000000000000L
)
111 self
.assertRaises(Exception, types
.Int16
, -0x8000000000000001L
)
112 self
.assertRaises(Exception, types
.UInt16
, 0x10000000000000000L
)
114 def test_Signature(self
):
115 self
.assertRaises(Exception, types
.Signature
, 'a')
116 self
.assertEquals(types
.Signature('ab', variant_level
=23), 'ab')
117 self
.assert_(isinstance(types
.Signature('ab'), str))
118 self
.assertEquals(tuple(types
.Signature('ab(xt)a{sv}')),
119 ('ab', '(xt)', 'a{sv}'))
120 self
.assert_(isinstance(tuple(types
.Signature('ab'))[0],
124 class TestMessageMarshalling(unittest
.TestCase
):
126 def test_append(self
):
127 aeq
= self
.assertEquals
128 from _dbus_bindings
import SignalMessage
129 s
= SignalMessage('/', 'foo.bar', 'baz')
130 s
.append([types
.Byte(1)], signature
='ay')
131 aeq(s
.get_signature(), 'ay')
132 aeq(s
.get_args_list(), [[types
.Byte(1)]])
134 s
= SignalMessage('/', 'foo.bar', 'baz')
135 s
.append([], signature
='ay')
136 aeq(s
.get_args_list(), [[]])
138 def test_append_ByteArray(self
):
139 aeq
= self
.assertEquals
140 from _dbus_bindings
import SignalMessage
141 s
= SignalMessage('/', 'foo.bar', 'baz')
142 s
.append(types
.ByteArray('ab'), signature
='ay')
143 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
144 s
= SignalMessage('/', 'foo.bar', 'baz')
145 s
.append(types
.ByteArray('ab'), signature
='av')
146 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
148 def test_append_Variant(self
):
150 aeq
= self
.assertEquals
151 from _dbus_bindings
import SignalMessage
152 s
= SignalMessage('/', 'foo.bar', 'baz')
153 s
.append(types
.Int32(1, variant_level
=0),
154 types
.String('a', variant_level
=42),
155 types
.Array([types
.Byte('a', variant_level
=1),
156 types
.UInt32(123, variant_level
=1)],
159 aeq(s
.get_signature(), 'vvv')
160 args
= s
.get_args_list()
161 aeq(args
[0].__class
__, types
.Int32
)
162 aeq(args
[0].variant_level
, 1)
163 aeq(args
[1].__class
__, types
.String
)
164 aeq(args
[1].variant_level
, 42)
165 aeq(args
[2].__class
__, types
.Array
)
166 aeq(args
[2].variant_level
, 1)
167 aeq(args
[2].signature
, 'v')
169 def test_guess_signature(self
):
170 aeq
= self
.assertEquals
171 from _dbus_bindings
import Message
172 aeq(Message
.guess_signature(('a','b')), '(ss)')
173 aeq(Message
.guess_signature('a','b'), 'ss')
174 aeq(Message
.guess_signature(['a','b']), 'as')
175 aeq(Message
.guess_signature(('a',)), '(s)')
176 aeq(Message
.guess_signature('abc'), 's')
177 aeq(Message
.guess_signature(types
.Int32(123)), 'i')
178 aeq(Message
.guess_signature(('a',)), '(s)')
179 aeq(Message
.guess_signature(['a']), 'as')
180 aeq(Message
.guess_signature({'a':'b'}), 'a{ss}')
182 def test_guess_signature_dbus_types(self
):
183 aeq
= self
.assertEquals
184 from _dbus_bindings
import Message
185 gs
= Message
.guess_signature
186 aeq(gs(types
.Dictionary({'a':'b'})), 'a{ss}')
187 aeq(gs(types
.Dictionary({'a':'b'}, signature
='sv')), 'a{sv}')
188 aeq(gs(types
.Dictionary({}, signature
='iu')), 'a{iu}')
189 aeq(gs(types
.Array([types
.Int32(1)])), 'ai')
190 aeq(gs(types
.Array([types
.Int32(1)], signature
='u')), 'au')
192 def test_get_args_options(self
):
193 aeq
= self
.assertEquals
194 s
= _dbus_bindings
.SignalMessage('/', 'foo.bar', 'baz')
195 s
.append('b', 'bytes', -1, 1, 'str', 'var', signature
='yayiusv')
196 aeq(s
.get_args_list(), [ord('b'),
197 [ord('b'),ord('y'),ord('t'),ord('e'), ord('s')],
198 -1, 1, u
'str', u
'var'])
199 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list()
200 aeq(byte
.__class
__, types
.Byte
)
201 aeq(bytes
.__class
__, types
.Array
)
202 aeq(bytes
[0].__class
__, types
.Byte
)
203 aeq(int32
.__class__
, types
.Int32
)
204 aeq(uint32
.__class__
, types
.UInt32
)
205 aeq(string
.__class
__, types
.String
)
206 aeq(string
.variant_level
, 0)
207 aeq(variant
.__class
__, types
.String
)
208 aeq(variant
.variant_level
, 1)
210 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
212 aeq(byte
.__class
__, types
.Byte
)
213 aeq(bytes
.__class
__, types
.ByteArray
)
215 aeq(bytes
[0].__class
__, types
.Byte
)
216 aeq(int32
.__class__
, types
.Int32
)
217 aeq(uint32
.__class__
, types
.UInt32
)
218 aeq(string
.__class
__, types
.String
)
219 aeq(variant
.__class
__, types
.String
)
220 aeq(variant
.variant_level
, 1)
222 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
224 aeq(byte
.__class
__, types
.Byte
)
225 aeq(bytes
.__class
__, types
.Array
)
226 aeq(bytes
[0].__class
__, types
.Byte
)
227 aeq(int32
.__class__
, types
.Int32
)
228 aeq(uint32
.__class__
, types
.UInt32
)
229 aeq(string
.__class
__, types
.UTF8String
)
231 aeq(variant
.__class
__, types
.UTF8String
)
232 aeq(variant
.variant_level
, 1)
236 if __name__
== '__main__':