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 Library General Public License as published by
13 # the Free Software Foundation; either version 2.1 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU Library General Public License for more details.
21 # You should have received a copy of the GNU Library General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 from traceback
import print_exc
31 builddir
= os
.path
.normpath(os
.environ
["DBUS_TOP_BUILDDIR"])
32 pydir
= os
.path
.normpath(os
.environ
["DBUS_TOP_SRCDIR"])
36 import dbus
.types
as types
38 # Check that we're using the right versions
39 if not dbus
.__file
__.startswith(pydir
):
40 raise Exception("DBus modules (%s) are not being picked up from the package"%dbus
.__file
__)
41 if not _dbus_bindings
.__file
__.startswith(builddir
):
42 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings
.__file
__)
43 assert (_dbus_bindings
._python
_version
& 0xffff0000
44 == sys
.hexversion
& 0xffff0000), \
45 '_dbus_bindings was compiled for Python %x but this is Python %x, '\
46 'a different major version'\
47 % (_dbus_bindings
._python
_version
, sys
.hexversion
)
48 assert _dbus_bindings
.__version
__ == os
.environ
['DBUS_PYTHON_VERSION'], \
49 '_dbus_bindings was compiled as version %s but Automake says '\
50 'we should be version %s' \
51 % (_dbus_bindings
.__version
__, os
.environ
['DBUS_PYTHON_VERSION'])
53 class TestTypes(unittest
.TestCase
):
55 def test_Dictionary(self
):
56 self
.assertEquals(types
.Dictionary({'foo':'bar'}), {'foo':'bar'})
57 self
.assertEquals(types
.Dictionary({}, variant_level
=2), {})
58 self
.assertEquals(types
.Dictionary({}, variant_level
=2).variant_level
, 2)
61 self
.assertEquals(types
.Array(['foo','bar']), ['foo','bar'])
62 self
.assertEquals(types
.Array([], variant_level
=2), [])
63 self
.assertEquals(types
.Array([], variant_level
=2).variant_level
, 2)
65 def test_Double(self
):
66 self
.assertEquals(types
.Double(0.0), 0.0)
67 self
.assertEquals(types
.Double(0.125, variant_level
=2), 0.125)
68 self
.assertEquals(types
.Double(0.125, variant_level
=2).variant_level
, 2)
70 def test_Struct(self
):
71 x
= types
.Struct(('',))
72 self
.assertEquals(x
.variant_level
, 0)
73 self
.assertEquals(x
, ('',))
74 x
= types
.Struct('abc', variant_level
=42)
75 self
.assertEquals(x
.variant_level
, 42)
76 self
.assertEquals(x
, ('a','b','c'))
79 self
.assertEquals(types
.Byte('x', variant_level
=2), types
.Byte(ord('x')))
81 def test_object_path_attr(self
):
82 class MyObject(object):
83 __dbus_object_path__
= '/foo'
84 from _dbus_bindings
import SignalMessage
85 self
.assertEquals(SignalMessage
.guess_signature(MyObject()), 'o')
87 def test_integers(self
):
88 # This is an API guarantee. Note that exactly which of these types
89 # are ints and which of them are longs is *not* guaranteed.
90 for cls
in (types
.Int16
, types
.UInt16
, types
.Int32
, types
.UInt32
,
91 types
.Int64
, types
.UInt64
):
92 self
.assert_(issubclass(cls
, (int, long)))
93 self
.assert_(isinstance(cls(0), (int, long)))
94 self
.assertEquals(cls(0), 0)
95 self
.assertEquals(cls(23, variant_level
=1), 23)
96 self
.assertEquals(cls(23, variant_level
=1).variant_level
, 1)
98 def test_integer_limits_16(self
):
99 self
.assertEquals(types
.Int16(0x7fff), 0x7fff)
100 self
.assertEquals(types
.Int16(-0x8000), -0x8000)
101 self
.assertEquals(types
.UInt16(0xffff), 0xffff)
102 self
.assertRaises(Exception, types
.Int16
, 0x8000)
103 self
.assertRaises(Exception, types
.Int16
, -0x8001)
104 self
.assertRaises(Exception, types
.UInt16
, 0x10000)
106 def test_integer_limits_32(self
):
107 self
.assertEquals(types
.Int32(0x7fffffff), 0x7fffffff)
108 self
.assertEquals(types
.Int32(-0x80000000L
), -0x80000000L
)
109 self
.assertEquals(types
.UInt32(0xffffffffL
), 0xffffffffL
)
110 self
.assertRaises(Exception, types
.Int32
, 0x80000000L
)
111 self
.assertRaises(Exception, types
.Int32
, -0x80000001L
)
112 self
.assertRaises(Exception, types
.UInt32
, 0x100000000L
)
114 def test_integer_limits_64(self
):
115 self
.assertEquals(types
.Int64(0x7fffffffffffffffL
), 0x7fffffffffffffffL
)
116 self
.assertEquals(types
.Int64(-0x8000000000000000L
), -0x8000000000000000L
)
117 self
.assertEquals(types
.UInt64(0xffffffffffffffffL
), 0xffffffffffffffffL
)
118 self
.assertRaises(Exception, types
.Int16
, 0x8000000000000000L
)
119 self
.assertRaises(Exception, types
.Int16
, -0x8000000000000001L
)
120 self
.assertRaises(Exception, types
.UInt16
, 0x10000000000000000L
)
122 def test_Signature(self
):
123 self
.assertRaises(Exception, types
.Signature
, 'a')
124 self
.assertEquals(types
.Signature('ab', variant_level
=23), 'ab')
125 self
.assert_(isinstance(types
.Signature('ab'), str))
126 self
.assertEquals(tuple(types
.Signature('ab(xt)a{sv}')),
127 ('ab', '(xt)', 'a{sv}'))
128 self
.assert_(isinstance(tuple(types
.Signature('ab'))[0],
132 class TestMessageMarshalling(unittest
.TestCase
):
134 def test_count(self
):
135 from _dbus_bindings
import SignalMessage
136 s
= SignalMessage('/', 'foo.bar', 'baz')
138 s
.append('a', signature
='ss')
142 raise AssertionError('Appending too few things in a message '
144 s
= SignalMessage('/', 'foo.bar', 'baz')
146 s
.append('a','b','c', signature
='ss')
150 raise AssertionError('Appending too many things in a message '
153 def test_append(self
):
154 aeq
= self
.assertEquals
155 from _dbus_bindings
import SignalMessage
156 s
= SignalMessage('/', 'foo.bar', 'baz')
157 s
.append([types
.Byte(1)], signature
='ay')
158 aeq(s
.get_signature(), 'ay')
159 aeq(s
.get_args_list(), [[types
.Byte(1)]])
161 s
= SignalMessage('/', 'foo.bar', 'baz')
162 s
.append([], signature
='ay')
163 aeq(s
.get_args_list(), [[]])
165 def test_append_ByteArray(self
):
166 aeq
= self
.assertEquals
167 from _dbus_bindings
import SignalMessage
168 s
= SignalMessage('/', 'foo.bar', 'baz')
169 s
.append(types
.ByteArray('ab'), signature
='ay')
170 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
171 s
= SignalMessage('/', 'foo.bar', 'baz')
172 s
.append(types
.ByteArray('ab'), signature
='av')
173 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
175 def test_append_Variant(self
):
177 aeq
= self
.assertEquals
178 from _dbus_bindings
import SignalMessage
179 s
= SignalMessage('/', 'foo.bar', 'baz')
180 s
.append(types
.Int32(1, variant_level
=0),
181 types
.String('a', variant_level
=42),
182 types
.Array([types
.Byte('a', variant_level
=1),
183 types
.UInt32(123, variant_level
=1)],
186 aeq(s
.get_signature(), 'vvv')
187 args
= s
.get_args_list()
188 aeq(args
[0].__class
__, types
.Int32
)
189 aeq(args
[0].variant_level
, 1)
190 aeq(args
[1].__class
__, types
.String
)
191 aeq(args
[1].variant_level
, 42)
192 aeq(args
[2].__class
__, types
.Array
)
193 aeq(args
[2].variant_level
, 1)
194 aeq(args
[2].signature
, 'v')
196 def test_guess_signature(self
):
197 aeq
= self
.assertEquals
198 from _dbus_bindings
import Message
199 aeq(Message
.guess_signature(('a','b')), '(ss)')
200 aeq(Message
.guess_signature('a','b'), 'ss')
201 aeq(Message
.guess_signature(['a','b']), 'as')
202 aeq(Message
.guess_signature(('a',)), '(s)')
203 aeq(Message
.guess_signature('abc'), 's')
204 aeq(Message
.guess_signature(types
.Int32(123)), 'i')
205 aeq(Message
.guess_signature(types
.ByteArray('abc')), 'ay')
206 aeq(Message
.guess_signature(('a',)), '(s)')
207 aeq(Message
.guess_signature(['a']), 'as')
208 aeq(Message
.guess_signature({'a':'b'}), 'a{ss}')
210 def test_guess_signature_dbus_types(self
):
211 aeq
= self
.assertEquals
212 from _dbus_bindings
import Message
213 gs
= Message
.guess_signature
214 aeq(gs(types
.Dictionary({'a':'b'})), 'a{ss}')
215 aeq(gs(types
.Dictionary({'a':'b'}, signature
='sv')), 'a{sv}')
216 aeq(gs(types
.Dictionary({}, signature
='iu')), 'a{iu}')
217 aeq(gs(types
.Array([types
.Int32(1)])), 'ai')
218 aeq(gs(types
.Array([types
.Int32(1)], signature
='u')), 'au')
220 def test_get_args_options(self
):
221 aeq
= self
.assertEquals
222 s
= _dbus_bindings
.SignalMessage('/', 'foo.bar', 'baz')
223 s
.append('b', 'bytes', -1, 1, 'str', 'var', signature
='yayiusv')
224 aeq(s
.get_args_list(), [ord('b'),
225 [ord('b'),ord('y'),ord('t'),ord('e'), ord('s')],
226 -1, 1, u
'str', u
'var'])
227 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list()
228 aeq(byte
.__class
__, types
.Byte
)
229 aeq(bytes
.__class
__, types
.Array
)
230 aeq(bytes
[0].__class
__, types
.Byte
)
231 aeq(int32
.__class__
, types
.Int32
)
232 aeq(uint32
.__class__
, types
.UInt32
)
233 aeq(string
.__class
__, types
.String
)
234 aeq(string
.variant_level
, 0)
235 aeq(variant
.__class
__, types
.String
)
236 aeq(variant
.variant_level
, 1)
238 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
240 aeq(byte
.__class
__, types
.Byte
)
241 aeq(bytes
.__class
__, types
.ByteArray
)
243 aeq(bytes
[0].__class
__, str)
244 aeq(int32
.__class__
, types
.Int32
)
245 aeq(uint32
.__class__
, types
.UInt32
)
246 aeq(string
.__class
__, types
.String
)
247 aeq(variant
.__class
__, types
.String
)
248 aeq(variant
.variant_level
, 1)
250 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
252 aeq(byte
.__class
__, types
.Byte
)
253 aeq(bytes
.__class
__, types
.Array
)
254 aeq(bytes
[0].__class
__, types
.Byte
)
255 aeq(int32
.__class__
, types
.Int32
)
256 aeq(uint32
.__class__
, types
.UInt32
)
257 aeq(string
.__class
__, types
.UTF8String
)
259 aeq(variant
.__class
__, types
.UTF8String
)
260 aeq(variant
.variant_level
, 1)
263 def test_object_path_attr(self
):
264 from _dbus_bindings
import SignalMessage
265 class MyObject(object):
266 __dbus_object_path__
= '/foo'
267 s
= SignalMessage('/', 'foo.bar', 'baz')
268 s
.append(MyObject(), signature
='o')
270 self
.assertEquals(s
.get_args_list(), ['/foo', '/foo'])
272 def test_struct(self
):
273 from _dbus_bindings
import SignalMessage
274 s
= SignalMessage('/', 'foo.bar', 'baz')
276 s
.append(('a',), signature
='(ss)')
280 raise AssertionError('Appending too few things in a struct '
282 s
= SignalMessage('/', 'foo.bar', 'baz')
284 s
.append(('a','b','c'), signature
='(ss)')
288 raise AssertionError('Appending too many things in a struct '
292 if __name__
== '__main__':