3 """Tests that don't need an active D-Bus connection to run, but can be
7 # Copyright (C) 2006, 2007 Collabora Ltd. <http://www.collabora.co.uk/>
9 # Licensed under the Academic Free License version 2.1
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 from traceback
import print_exc
33 builddir
= os
.path
.normpath(os
.environ
["DBUS_TOP_BUILDDIR"])
34 pydir
= os
.path
.normpath(os
.environ
["DBUS_TOP_SRCDIR"])
38 import dbus
.types
as types
40 # Check that we're using the right versions
41 if not dbus
.__file
__.startswith(pydir
):
42 raise Exception("DBus modules (%s) are not being picked up from the package"%dbus
.__file
__)
43 if not _dbus_bindings
.__file
__.startswith(builddir
):
44 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings
.__file
__)
45 assert (_dbus_bindings
._python
_version
& 0xffff0000
46 == sys
.hexversion
& 0xffff0000), \
47 '_dbus_bindings was compiled for Python %x but this is Python %x, '\
48 'a different major version'\
49 % (_dbus_bindings
._python
_version
, sys
.hexversion
)
50 assert _dbus_bindings
.__version
__ == os
.environ
['DBUS_PYTHON_VERSION'], \
51 '_dbus_bindings was compiled as version %s but Automake says '\
52 'we should be version %s' \
53 % (_dbus_bindings
.__version
__, os
.environ
['DBUS_PYTHON_VERSION'])
55 class TestTypes(unittest
.TestCase
):
57 def test_Dictionary(self
):
58 self
.assertEquals(types
.Dictionary({'foo':'bar'}), {'foo':'bar'})
59 self
.assertEquals(types
.Dictionary({}, variant_level
=2), {})
60 self
.assertEquals(types
.Dictionary({}, variant_level
=2).variant_level
, 2)
63 self
.assertEquals(types
.Array(['foo','bar']), ['foo','bar'])
64 self
.assertEquals(types
.Array([], variant_level
=2), [])
65 self
.assertEquals(types
.Array([], variant_level
=2).variant_level
, 2)
67 def test_Double(self
):
68 self
.assertEquals(types
.Double(0.0), 0.0)
69 self
.assertEquals(types
.Double(0.125, variant_level
=2), 0.125)
70 self
.assertEquals(types
.Double(0.125, variant_level
=2).variant_level
, 2)
72 def test_Struct(self
):
73 x
= types
.Struct(('',))
74 self
.assertEquals(x
.variant_level
, 0)
75 self
.assertEquals(x
, ('',))
76 x
= types
.Struct('abc', variant_level
=42)
77 self
.assertEquals(x
.variant_level
, 42)
78 self
.assertEquals(x
, ('a','b','c'))
81 self
.assertEquals(types
.Byte('x', variant_level
=2), types
.Byte(ord('x')))
83 def test_object_path_attr(self
):
84 class MyObject(object):
85 __dbus_object_path__
= '/foo'
86 from _dbus_bindings
import SignalMessage
87 self
.assertEquals(SignalMessage
.guess_signature(MyObject()), 'o')
89 def test_integers(self
):
90 # This is an API guarantee. Note that exactly which of these types
91 # are ints and which of them are longs is *not* guaranteed.
92 for cls
in (types
.Int16
, types
.UInt16
, types
.Int32
, types
.UInt32
,
93 types
.Int64
, types
.UInt64
):
94 self
.assert_(issubclass(cls
, (int, long)))
95 self
.assert_(isinstance(cls(0), (int, long)))
96 self
.assertEquals(cls(0), 0)
97 self
.assertEquals(cls(23, variant_level
=1), 23)
98 self
.assertEquals(cls(23, variant_level
=1).variant_level
, 1)
100 def test_integer_limits_16(self
):
101 self
.assertEquals(types
.Int16(0x7fff), 0x7fff)
102 self
.assertEquals(types
.Int16(-0x8000), -0x8000)
103 self
.assertEquals(types
.UInt16(0xffff), 0xffff)
104 self
.assertRaises(Exception, types
.Int16
, 0x8000)
105 self
.assertRaises(Exception, types
.Int16
, -0x8001)
106 self
.assertRaises(Exception, types
.UInt16
, 0x10000)
108 def test_integer_limits_32(self
):
109 self
.assertEquals(types
.Int32(0x7fffffff), 0x7fffffff)
110 self
.assertEquals(types
.Int32(-0x80000000L
), -0x80000000L
)
111 self
.assertEquals(types
.UInt32(0xffffffffL
), 0xffffffffL
)
112 self
.assertRaises(Exception, types
.Int32
, 0x80000000L
)
113 self
.assertRaises(Exception, types
.Int32
, -0x80000001L
)
114 self
.assertRaises(Exception, types
.UInt32
, 0x100000000L
)
116 def test_integer_limits_64(self
):
117 self
.assertEquals(types
.Int64(0x7fffffffffffffffL
), 0x7fffffffffffffffL
)
118 self
.assertEquals(types
.Int64(-0x8000000000000000L
), -0x8000000000000000L
)
119 self
.assertEquals(types
.UInt64(0xffffffffffffffffL
), 0xffffffffffffffffL
)
120 self
.assertRaises(Exception, types
.Int16
, 0x8000000000000000L
)
121 self
.assertRaises(Exception, types
.Int16
, -0x8000000000000001L
)
122 self
.assertRaises(Exception, types
.UInt16
, 0x10000000000000000L
)
124 def test_Signature(self
):
125 self
.assertRaises(Exception, types
.Signature
, 'a')
126 self
.assertEquals(types
.Signature('ab', variant_level
=23), 'ab')
127 self
.assert_(isinstance(types
.Signature('ab'), str))
128 self
.assertEquals(tuple(types
.Signature('ab(xt)a{sv}')),
129 ('ab', '(xt)', 'a{sv}'))
130 self
.assert_(isinstance(tuple(types
.Signature('ab'))[0],
134 class TestMessageMarshalling(unittest
.TestCase
):
136 def test_count(self
):
137 from _dbus_bindings
import SignalMessage
138 s
= SignalMessage('/', 'foo.bar', 'baz')
140 s
.append('a', signature
='ss')
144 raise AssertionError('Appending too few things in a message '
146 s
= SignalMessage('/', 'foo.bar', 'baz')
148 s
.append('a','b','c', signature
='ss')
152 raise AssertionError('Appending too many things in a message '
155 def test_append(self
):
156 aeq
= self
.assertEquals
157 from _dbus_bindings
import SignalMessage
158 s
= SignalMessage('/', 'foo.bar', 'baz')
159 s
.append([types
.Byte(1)], signature
='ay')
160 aeq(s
.get_signature(), 'ay')
161 aeq(s
.get_args_list(), [[types
.Byte(1)]])
163 s
= SignalMessage('/', 'foo.bar', 'baz')
164 s
.append([], signature
='ay')
165 aeq(s
.get_args_list(), [[]])
167 def test_append_ByteArray(self
):
168 aeq
= self
.assertEquals
169 from _dbus_bindings
import SignalMessage
170 s
= SignalMessage('/', 'foo.bar', 'baz')
171 s
.append(types
.ByteArray('ab'), signature
='ay')
172 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
173 s
= SignalMessage('/', 'foo.bar', 'baz')
174 s
.append(types
.ByteArray('ab'), signature
='av')
175 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
177 def test_append_Variant(self
):
179 aeq
= self
.assertEquals
180 from _dbus_bindings
import SignalMessage
181 s
= SignalMessage('/', 'foo.bar', 'baz')
182 s
.append(types
.Int32(1, variant_level
=0),
183 types
.String('a', variant_level
=42),
184 types
.Array([types
.Byte('a', variant_level
=1),
185 types
.UInt32(123, variant_level
=1)],
188 aeq(s
.get_signature(), 'vvv')
189 args
= s
.get_args_list()
190 aeq(args
[0].__class
__, types
.Int32
)
191 aeq(args
[0].variant_level
, 1)
192 aeq(args
[1].__class
__, types
.String
)
193 aeq(args
[1].variant_level
, 42)
194 aeq(args
[2].__class
__, types
.Array
)
195 aeq(args
[2].variant_level
, 1)
196 aeq(args
[2].signature
, 'v')
198 def test_guess_signature(self
):
199 aeq
= self
.assertEquals
200 from _dbus_bindings
import Message
201 aeq(Message
.guess_signature(('a','b')), '(ss)')
202 aeq(Message
.guess_signature('a','b'), 'ss')
203 aeq(Message
.guess_signature(['a','b']), 'as')
204 aeq(Message
.guess_signature(('a',)), '(s)')
205 aeq(Message
.guess_signature('abc'), 's')
206 aeq(Message
.guess_signature(types
.Int32(123)), 'i')
207 aeq(Message
.guess_signature(types
.ByteArray('abc')), 'ay')
208 aeq(Message
.guess_signature(('a',)), '(s)')
209 aeq(Message
.guess_signature(['a']), 'as')
210 aeq(Message
.guess_signature({'a':'b'}), 'a{ss}')
212 def test_guess_signature_dbus_types(self
):
213 aeq
= self
.assertEquals
214 from _dbus_bindings
import Message
215 gs
= Message
.guess_signature
216 aeq(gs(types
.Dictionary({'a':'b'})), 'a{ss}')
217 aeq(gs(types
.Dictionary({'a':'b'}, signature
='sv')), 'a{sv}')
218 aeq(gs(types
.Dictionary({}, signature
='iu')), 'a{iu}')
219 aeq(gs(types
.Array([types
.Int32(1)])), 'ai')
220 aeq(gs(types
.Array([types
.Int32(1)], signature
='u')), 'au')
222 def test_get_args_options(self
):
223 aeq
= self
.assertEquals
224 s
= _dbus_bindings
.SignalMessage('/', 'foo.bar', 'baz')
225 s
.append('b', 'bytes', -1, 1, 'str', 'var', signature
='yayiusv')
226 aeq(s
.get_args_list(), [ord('b'),
227 [ord('b'),ord('y'),ord('t'),ord('e'), ord('s')],
228 -1, 1, u
'str', u
'var'])
229 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list()
230 aeq(byte
.__class
__, types
.Byte
)
231 aeq(bytes
.__class
__, types
.Array
)
232 aeq(bytes
[0].__class
__, types
.Byte
)
233 aeq(int32
.__class__
, types
.Int32
)
234 aeq(uint32
.__class__
, types
.UInt32
)
235 aeq(string
.__class
__, types
.String
)
236 aeq(string
.variant_level
, 0)
237 aeq(variant
.__class
__, types
.String
)
238 aeq(variant
.variant_level
, 1)
240 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
242 aeq(byte
.__class
__, types
.Byte
)
243 aeq(bytes
.__class
__, types
.ByteArray
)
245 aeq(bytes
[0].__class
__, str)
246 aeq(int32
.__class__
, types
.Int32
)
247 aeq(uint32
.__class__
, types
.UInt32
)
248 aeq(string
.__class
__, types
.String
)
249 aeq(variant
.__class
__, types
.String
)
250 aeq(variant
.variant_level
, 1)
252 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
254 aeq(byte
.__class
__, types
.Byte
)
255 aeq(bytes
.__class
__, types
.Array
)
256 aeq(bytes
[0].__class
__, types
.Byte
)
257 aeq(int32
.__class__
, types
.Int32
)
258 aeq(uint32
.__class__
, types
.UInt32
)
259 aeq(string
.__class
__, types
.UTF8String
)
261 aeq(variant
.__class
__, types
.UTF8String
)
262 aeq(variant
.variant_level
, 1)
265 def test_object_path_attr(self
):
266 from _dbus_bindings
import SignalMessage
267 class MyObject(object):
268 __dbus_object_path__
= '/foo'
269 s
= SignalMessage('/', 'foo.bar', 'baz')
270 s
.append(MyObject(), signature
='o')
272 self
.assertEquals(s
.get_args_list(), ['/foo', '/foo'])
274 def test_struct(self
):
275 from _dbus_bindings
import SignalMessage
276 s
= SignalMessage('/', 'foo.bar', 'baz')
278 s
.append(('a',), signature
='(ss)')
282 raise AssertionError('Appending too few things in a struct '
284 s
= SignalMessage('/', 'foo.bar', 'baz')
286 s
.append(('a','b','c'), signature
='(ss)')
290 raise AssertionError('Appending too many things in a struct '
294 if __name__
== '__main__':