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
.environ
["DBUS_TOP_BUILDDIR"]
32 sys
.path
.insert(0, pydir
)
33 sys
.path
.insert(0, pydir
+ 'dbus')
37 import dbus
.types
as types
40 if not pkg
.startswith(pydir
):
41 raise Exception("DBus modules (%s) are not being picked up from the package"%pkg
)
43 if not _dbus_bindings
.__file
__.startswith(pydir
):
44 raise Exception("DBus modules (%s) are not being picked up from the package"%_dbus_bindings
.__file
__)
46 class TestTypes(unittest
.TestCase
):
48 def test_Dictionary(self
):
49 self
.assertEquals(types
.Dictionary({'foo':'bar'}), {'foo':'bar'})
50 self
.assertEquals(types
.Dictionary({}, variant_level
=2), {})
51 self
.assertEquals(types
.Dictionary({}, variant_level
=2).variant_level
, 2)
54 self
.assertEquals(types
.Array(['foo','bar']), ['foo','bar'])
55 self
.assertEquals(types
.Array([], variant_level
=2), [])
56 self
.assertEquals(types
.Array([], variant_level
=2).variant_level
, 2)
58 def test_Double(self
):
59 self
.assertEquals(types
.Double(0.0), 0.0)
60 self
.assertEquals(types
.Double(0.125, variant_level
=2), 0.125)
61 self
.assertEquals(types
.Double(0.125, variant_level
=2).variant_level
, 2)
63 def test_Struct(self
):
64 x
= types
.Struct(('',))
65 self
.assertEquals(x
.variant_level
, 0)
66 self
.assertEquals(x
, ('',))
67 x
= types
.Struct('abc', variant_level
=42)
68 self
.assertEquals(x
.variant_level
, 42)
69 self
.assertEquals(x
, ('a','b','c'))
72 self
.assertEquals(types
.Byte('x', variant_level
=2), types
.Byte(ord('x')))
74 def test_integers(self
):
75 # This is an API guarantee. Note that exactly which of these types
76 # are ints and which of them are longs is *not* guaranteed.
77 for cls
in (types
.Int16
, types
.UInt16
, types
.Int32
, types
.UInt32
,
78 types
.Int64
, types
.UInt64
):
79 self
.assert_(issubclass(cls
, (int, long)))
80 self
.assert_(isinstance(cls(0), (int, long)))
81 self
.assertEquals(cls(0), 0)
82 self
.assertEquals(cls(23, variant_level
=1), 23)
83 self
.assertEquals(cls(23, variant_level
=1).variant_level
, 1)
85 def test_integer_limits_16(self
):
86 self
.assertEquals(types
.Int16(0x7fff), 0x7fff)
87 self
.assertEquals(types
.Int16(-0x8000), -0x8000)
88 self
.assertEquals(types
.UInt16(0xffff), 0xffff)
89 self
.assertRaises(Exception, types
.Int16
, 0x8000)
90 self
.assertRaises(Exception, types
.Int16
, -0x8001)
91 self
.assertRaises(Exception, types
.UInt16
, 0x10000)
93 def test_integer_limits_32(self
):
94 self
.assertEquals(types
.Int32(0x7fffffff), 0x7fffffff)
95 self
.assertEquals(types
.Int32(-0x80000000L
), -0x80000000L
)
96 self
.assertEquals(types
.UInt32(0xffffffffL
), 0xffffffffL
)
97 self
.assertRaises(Exception, types
.Int32
, 0x80000000L
)
98 self
.assertRaises(Exception, types
.Int32
, -0x80000001L
)
99 self
.assertRaises(Exception, types
.UInt32
, 0x100000000L
)
101 def test_integer_limits_64(self
):
102 self
.assertEquals(types
.Int64(0x7fffffffffffffffL
), 0x7fffffffffffffffL
)
103 self
.assertEquals(types
.Int64(-0x8000000000000000L
), -0x8000000000000000L
)
104 self
.assertEquals(types
.UInt64(0xffffffffffffffffL
), 0xffffffffffffffffL
)
105 self
.assertRaises(Exception, types
.Int16
, 0x8000000000000000L
)
106 self
.assertRaises(Exception, types
.Int16
, -0x8000000000000001L
)
107 self
.assertRaises(Exception, types
.UInt16
, 0x10000000000000000L
)
109 def test_Signature(self
):
110 self
.assertRaises(Exception, types
.Signature
, 'a')
111 self
.assertEquals(types
.Signature('ab', variant_level
=23), 'ab')
112 self
.assert_(isinstance(types
.Signature('ab'), str))
113 self
.assertEquals(tuple(types
.Signature('ab(xt)a{sv}')),
114 ('ab', '(xt)', 'a{sv}'))
115 self
.assert_(isinstance(tuple(types
.Signature('ab'))[0],
119 class TestMessageMarshalling(unittest
.TestCase
):
121 def test_append(self
):
122 aeq
= self
.assertEquals
123 from _dbus_bindings
import SignalMessage
124 s
= SignalMessage('/', 'foo.bar', 'baz')
125 s
.append([types
.Byte(1)], signature
='ay')
126 aeq(s
.get_signature(), 'ay')
127 aeq(s
.get_args_list(), [[types
.Byte(1)]])
129 s
= SignalMessage('/', 'foo.bar', 'baz')
130 s
.append([], signature
='ay')
131 aeq(s
.get_args_list(), [[]])
133 def test_append_ByteArray(self
):
134 aeq
= self
.assertEquals
135 from _dbus_bindings
import SignalMessage
136 s
= SignalMessage('/', 'foo.bar', 'baz')
137 s
.append(types
.ByteArray('ab'), signature
='ay')
138 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
139 s
= SignalMessage('/', 'foo.bar', 'baz')
140 s
.append(types
.ByteArray('ab'), signature
='av')
141 aeq(s
.get_args_list(), [[types
.Byte('a'), types
.Byte('b')]])
143 def test_append_Variant(self
):
145 aeq
= self
.assertEquals
146 from _dbus_bindings
import SignalMessage
147 s
= SignalMessage('/', 'foo.bar', 'baz')
148 s
.append(types
.Int32(1, variant_level
=0),
149 types
.String('a', variant_level
=42),
150 types
.Array([types
.Byte('a', variant_level
=1),
151 types
.UInt32(123, variant_level
=1)],
154 aeq(s
.get_signature(), 'vvv')
155 args
= s
.get_args_list()
156 aeq(args
[0].__class
__, types
.Int32
)
157 aeq(args
[0].variant_level
, 1)
158 aeq(args
[1].__class
__, types
.String
)
159 aeq(args
[1].variant_level
, 42)
160 aeq(args
[2].__class
__, types
.Array
)
161 aeq(args
[2].variant_level
, 1)
162 aeq(args
[2].signature
, 'v')
164 def test_guess_signature(self
):
165 aeq
= self
.assertEquals
166 from _dbus_bindings
import Message
167 aeq(Message
.guess_signature(('a','b')), '(ss)')
168 aeq(Message
.guess_signature('a','b'), 'ss')
169 aeq(Message
.guess_signature(['a','b']), 'as')
170 aeq(Message
.guess_signature(('a',)), '(s)')
171 aeq(Message
.guess_signature('abc'), 's')
172 aeq(Message
.guess_signature(types
.Int32(123)), 'i')
173 aeq(Message
.guess_signature(('a',)), '(s)')
174 aeq(Message
.guess_signature(['a']), 'as')
175 aeq(Message
.guess_signature({'a':'b'}), 'a{ss}')
177 def test_guess_signature_dbus_types(self
):
178 aeq
= self
.assertEquals
179 from _dbus_bindings
import Message
180 gs
= Message
.guess_signature
181 aeq(gs(types
.Dictionary({'a':'b'})), 'a{ss}')
182 aeq(gs(types
.Dictionary({'a':'b'}, signature
='sv')), 'a{sv}')
183 aeq(gs(types
.Dictionary({}, signature
='iu')), 'a{iu}')
184 aeq(gs(types
.Array([types
.Int32(1)])), 'ai')
185 aeq(gs(types
.Array([types
.Int32(1)], signature
='u')), 'au')
187 def test_get_args_options(self
):
188 aeq
= self
.assertEquals
189 s
= _dbus_bindings
.SignalMessage('/', 'foo.bar', 'baz')
190 s
.append('b', 'bytes', -1, 1, 'str', 'var', signature
='yayiusv')
191 aeq(s
.get_args_list(), [ord('b'),
192 [ord('b'),ord('y'),ord('t'),ord('e'), ord('s')],
193 -1, 1, u
'str', u
'var'])
194 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list()
195 aeq(byte
.__class
__, types
.Byte
)
196 aeq(bytes
.__class
__, types
.Array
)
197 aeq(bytes
[0].__class
__, types
.Byte
)
198 aeq(int32
.__class__
, types
.Int32
)
199 aeq(uint32
.__class__
, types
.UInt32
)
200 aeq(string
.__class
__, types
.String
)
201 aeq(string
.variant_level
, 0)
202 aeq(variant
.__class
__, types
.String
)
203 aeq(variant
.variant_level
, 1)
205 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
207 aeq(byte
.__class
__, types
.Byte
)
208 aeq(bytes
.__class
__, types
.ByteArray
)
210 aeq(bytes
[0].__class
__, types
.Byte
)
211 aeq(int32
.__class__
, types
.Int32
)
212 aeq(uint32
.__class__
, types
.UInt32
)
213 aeq(string
.__class
__, types
.String
)
214 aeq(variant
.__class
__, types
.String
)
215 aeq(variant
.variant_level
, 1)
217 byte
, bytes
, int32
, uint32
, string
, variant
= s
.get_args_list(
219 aeq(byte
.__class
__, types
.Byte
)
220 aeq(bytes
.__class
__, types
.Array
)
221 aeq(bytes
[0].__class
__, types
.Byte
)
222 aeq(int32
.__class__
, types
.Int32
)
223 aeq(uint32
.__class__
, types
.UInt32
)
224 aeq(string
.__class
__, types
.UTF8String
)
226 aeq(variant
.__class
__, types
.UTF8String
)
227 aeq(variant
.variant_level
, 1)
231 if __name__
== '__main__':