2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 suite
= unittest
.TestSuite()
26 suite
.addTest(TestCase("testErrors",ctx
))
27 suite
.addTest(TestCase("testBaseTypes",ctx
))
28 suite
.addTest(TestCase("testOutparam",ctx
))
29 suite
.addTest(TestCase("testStruct",ctx
))
30 suite
.addTest(TestCase("testType",ctx
))
31 suite
.addTest(TestCase("testEnum",ctx
))
32 suite
.addTest(TestCase("testBool",ctx
))
33 suite
.addTest(TestCase("testChar",ctx
))
34 suite
.addTest(TestCase("testUnicode",ctx
))
35 suite
.addTest(TestCase("testConstant",ctx
))
36 suite
.addTest(TestCase("testExceptions",ctx
))
37 suite
.addTest(TestCase("testInterface",ctx
))
38 suite
.addTest(TestCase("testByteSequence",ctx
))
39 suite
.addTest(TestCase("testInvoke",ctx
))
42 def equalsEps( a
,b
,eps
):
43 if a
- eps
<= b
and a
+eps
>= b
:
47 def assign( rData
, bBool
, cChar
, nByte
, nShort
, nUShort
, nLong
, nULong
, nHyper
,\
48 nUHyper
, fFloat
, fDouble
, eEnum
, rStr
, xTest
, rAny
):
53 rData
.UShort
= nUShort
57 rData
.UHyper
= nUHyper
59 rData
.Double
= fDouble
62 rData
.Interface
= xTest
66 class PythonTransporter
:
70 def transportAny( self
, arg
):
73 class TestCase( unittest
.TestCase
):
75 def __init__(self
,method
,ctx
):
76 unittest
.TestCase
.__init
__(self
,method
)
80 # the testcomponent from the testtools project
81 self
.tobj
= self
.ctx
.ServiceManager
.createInstanceWithContext(
82 'com.sun.star.test.bridge.CppTestObject' , self
.ctx
)
94 self
.tobj
.Double
= 4.2
96 self
.tobj
.String
= "yabadabadoo"
97 self
.tobj
.Interface
= self
.ctx
98 self
.tobj
.Any
= self
.tobj
.String
99 mystruct
= uno
.createUnoStruct( "test.testtools.bridgetest.TestData" )
100 assign( mystruct
, 1, 'h', 43, -42,44,42,41,46,47,4.3,4.2,4,"yabadabadoo",self
.ctx
,"yabadabadoo")
101 self
.tobj
.Struct
= mystruct
103 self
.testElement
= uno
.createUnoStruct( "test.testtools.bridgetest.TestElement" )
104 self
.testElement
.String
= "foo"
105 self
.testElement2
= uno
.createUnoStruct( "test.testtools.bridgetest.TestElement" )
106 self
.testElement2
.String
= "42"
107 self
.tobj
.Sequence
= (self
.testElement
,self
.testElement2
)
109 def testBaseTypes(self
):
110 self
.assertTrue( 42 == self
.tobj
.Long
, "Long attribute" )
111 self
.assertTrue( 41 == self
.tobj
.ULong
, "ULong attribute" )
112 self
.assertTrue( 43 == self
.tobj
.Byte
, "Byte attribute" )
113 self
.assertTrue( 44 == self
.tobj
.UShort
, "UShort attribute" )
114 self
.assertTrue( -42 == self
.tobj
.Short
, "Short attribute" )
115 self
.assertTrue( 46 == self
.tobj
.Hyper
, "Hyper attribute" )
116 self
.assertTrue( 47 == self
.tobj
.UHyper
, "UHyper attribute" )
117 self
.assertTrue( self
.tobj
.Bool
, "Bool attribute2" )
118 self
.assertTrue( "yabadabadoo" == self
.tobj
.String
, "String attribute" )
119 self
.assertTrue( self
.tobj
.Sequence
[0] == self
.testElement
, "Sequence test")
120 self
.assertTrue( self
.tobj
.Sequence
[1] == self
.testElement2
, "Sequence2 test")
121 self
.assertTrue( equalsEps( 4.3,self
.tobj
.Float
,0.0001) , "float test" )
122 self
.assertTrue( 4.2 == self
.tobj
.Double
, "double test" )
123 self
.assertTrue( self
.ctx
== self
.tobj
.Interface
,
124 "object identity test with C++ object" )
125 self
.assertTrue( not self
.ctx
== self
.tobj
, "object not identical test " )
126 self
.assertTrue( 42 == self
.tobj
.transportAny( 42 ), "transportAny long" )
127 self
.assertTrue( "woo, this is python" == self
.tobj
.transportAny( "woo, this is python" ), \
128 "string roundtrip via any test" )
130 def testEnum( self
):
131 e1
= uno
.Enum( "com.sun.star.uno.TypeClass" , "LONG" )
132 e2
= uno
.Enum( "com.sun.star.uno.TypeClass" , "LONG" )
133 e3
= uno
.Enum( "com.sun.star.uno.TypeClass" , "UNSIGNED_LONG" )
134 e4
= uno
.Enum( "test.testtools.bridgetest.TestEnum" , "TWO" )
135 self
.assertTrue( e1
== e2
, "equal enum test" )
136 self
.assertTrue( not (e1
== e3
) , "different enums test" )
137 self
.assertTrue( self
.tobj
.transportAny( e3
) == e3
, "enum roundtrip test" )
139 self
.assertTrue( e4
== self
.tobj
.Enum
, "enum assignment failed" )
142 t1
= uno
.getTypeByName( "com.sun.star.lang.XComponent" )
143 t2
= uno
.getTypeByName( "com.sun.star.lang.XComponent" )
144 t3
= uno
.getTypeByName( "com.sun.star.lang.EventObject" )
145 self
.assertTrue( t1
.typeClass
== \
146 uno
.Enum( "com.sun.star.uno.TypeClass", "INTERFACE" ), "typeclass of type test" )
147 self
.assertTrue( t3
.typeClass
== \
148 uno
.Enum( "com.sun.star.uno.TypeClass", "STRUCT" ), "typeclass of type test")
149 self
.assertTrue( t1
== t2
, "equal type test" )
150 self
.assertTrue( t1
== t2
, "equal type test" )
151 self
.assertTrue( t1
== self
.tobj
.transportAny( t1
), "type roundtrip test" )
153 def testBool( self
):
154 self
.assertTrue( uno
.Bool(1) , "uno.Bool true test" )
155 self
.assertTrue( not uno
.Bool(0) , "uno.Bool false test" )
156 self
.assertTrue( uno
.Bool( "true") , "uno.Bool true1 test" )
157 self
.assertTrue( not uno
.Bool( "false") , "uno.Bool true1 test" )
159 self
.tobj
.Bool
= uno
.Bool(1)
160 self
.assertTrue( self
.tobj
.Bool
, "bool true attribute test" )
161 self
.tobj
.Bool
= uno
.Bool(0)
162 self
.assertTrue( not self
.tobj
.Bool
, "bool true attribute test" )
164 # new boolean semantic
165 self
.assertTrue( id( self
.tobj
.transportAny( True ) ) == id(True) , "boolean preserve test")
166 self
.assertTrue( id( self
.tobj
.transportAny( False ) ) == id(False) , "boolean preserve test" )
167 self
.assertTrue( id( self
.tobj
.transportAny(1) ) != id( True ), "boolean preserve test" )
168 self
.assertTrue( id( self
.tobj
.transportAny(0) ) != id( False ), "boolean preserve test" )
170 def testChar( self
):
171 self
.tobj
.Char
= uno
.Char( u
'h' )
172 self
.assertTrue( self
.tobj
.Char
== uno
.Char( u
'h' ), "char type test" )
173 self
.assertTrue( isinstance( self
.tobj
.transportAny( uno
.Char(u
'h') ),uno
.Char
),"char preserve test" )
175 def testStruct( self
):
176 mystruct
= uno
.createUnoStruct( "test.testtools.bridgetest.TestData" )
177 assign( mystruct
, 1, 'h', 43, -42,44,42,41,46,47,4.3,4.2,4,"yabadabadoo",self
.ctx
,"yabadabadoo")
178 self
.tobj
.Struct
= mystruct
179 aSecondStruct
= self
.tobj
.Struct
181 self
.assertTrue( self
.tobj
.Struct
== mystruct
, "struct roundtrip for equality test" )
182 self
.assertTrue( aSecondStruct
== mystruct
, "struct roundtrip for equality test2" )
183 aSecondStruct
.Short
= 720
184 self
.assertTrue( not aSecondStruct
== mystruct
, "different structs equality test" )
185 self
.assertTrue( not self
.ctx
== mystruct
, "object is not equal to struct test" )
186 self
.assertTrue( mystruct
== self
.tobj
.transportAny( mystruct
), "struct roundtrip with any test" )
187 my2ndstruct
= uno
.createUnoStruct( "test.testtools.bridgetest.TestData", \
188 1, 'h', 43, -42,44,42,41,46,47,4.3,4.2,4,"yabadabadoo",self
.ctx
,"yabadabadoo",())
189 self
.assertTrue( my2ndstruct
== mystruct
, "struct non-default ctor test" )
190 def testUnicode( self
):
192 self
.tobj
.String
= uni
193 self
.assertTrue( uni
== self
.tobj
.String
)
196 self
.tobj
.String
= u
'dubidu'
197 self
.assertTrue( u
'dubidu' == self
.tobj
.String
, "unicode comparison test")
198 self
.assertTrue( 'dubidu' == self
.tobj
.String
, "unicode vs. string comparison test" )
200 def testConstant( self
):
201 self
.assertTrue( uno
.getConstantByName( "com.sun.star.beans.PropertyConcept.ATTRIBUTES" ) == 4,\
202 "constant retrieval test" )
204 def testExceptions( self
):
205 unoExc
= uno
.getClass( "com.sun.star.uno.Exception" )
206 ioExc
= uno
.getClass( "com.sun.star.io.IOException" )
207 dispExc
= uno
.getClass( "com.sun.star.lang.DisposedException" )
210 raise ioExc( "huhuh" , self
.tobj
)
213 self
.assertTrue( wasHere
, "exception test 1" )
221 self
.assertTrue( wasHere
, "exception test 2" )
230 self
.assertTrue(wasHere
, "exception test 3")
232 illegalArg
= uno
.getClass( "com.sun.star.lang.IllegalArgumentException" )
235 self
.tobj
.raiseException( 1 , "foo" , self
.tobj
)
236 self
.assertTrue( 0 , "exception test 5a" )
238 self
.assertTrue( 0 , "exception test 5b" )
239 except illegalArg
as i
:
240 self
.assertTrue( 1 == i
.ArgumentPosition
, "exception member test" )
241 self
.assertTrue( "foo" == i
.Message
, "exception member test 2 " )
244 self
.assertTrue( 0, "except test 5c" )
245 self
.assertTrue( wasHere
, "illegal argument exception test failed" )
247 def testInterface(self
):
248 clazz
= uno
.getClass( "com.sun.star.lang.XComponent" )
249 self
.assertTrue( "com.sun.star.lang.XComponent" == clazz
.__pyunointerface
__ )
250 self
.assertTrue( issubclass( clazz
, uno
.getClass( "com.sun.star.uno.XInterface" ) ) )
251 self
.tobj
.Interface
= None
254 def testOutparam( self
):
256 struct
, mybool
,mychar
,mybyte
,myshort
,myushort
,mylong
,myulong
,myhyper
,myuhyper
,myfloat
, \
257 mydouble
,myenum
,mystring
,myinterface
,myany
,myseq
,my2ndstruct
= self
.tobj
.getValues( \
258 None,None,None,None,None,None,None,None,None,None, \
259 None,None,None,None,None,None,None)
260 self
.assertTrue(struct
== self
.tobj
.Struct
, "outparam 1 test")
261 self
.assertTrue(self
.tobj
.Bool
, "outparam 2 test")
262 self
.assertTrue(mychar
== self
.tobj
.Char
, "outparam 3 test")
263 self
.assertTrue(mybyte
== self
.tobj
.Byte
, "outparam 4 test")
264 self
.assertTrue(myshort
== self
.tobj
.Short
, "outparam 5 test")
265 self
.assertTrue(myushort
== self
.tobj
.UShort
, "outparam 6 test")
266 self
.assertTrue(mylong
== self
.tobj
.Long
, "outparam 7 test")
267 self
.assertTrue(myulong
== self
.tobj
.ULong
, "outparam 8 test")
268 self
.assertTrue(myhyper
== self
.tobj
.Hyper
, "outparam 9 test")
269 self
.assertTrue(myuhyper
== self
.tobj
.UHyper
, "outparam 10 test")
270 self
.assertTrue(myfloat
== self
.tobj
.Float
, "outparam 11 test")
271 self
.assertTrue(mydouble
== self
.tobj
.Double
, "outparam 12 test")
272 self
.assertTrue(myenum
== self
.tobj
.Enum
, "outparam 13 test")
273 self
.assertTrue(mystring
== self
.tobj
.String
, "outparam 14 test")
274 self
.assertTrue(myinterface
== self
.tobj
.Interface
, "outparam 15 test")
275 self
.assertTrue(myany
== self
.tobj
.Any
, "outparam 16 test")
276 self
.assertTrue(myseq
== self
.tobj
.Sequence
, "outparam 17 test")
277 self
.assertTrue(my2ndstruct
== struct
, "outparam 18 test")
279 # should work, debug on windows, why not
280 # struct, mybool,mychar,mybyte,myshort,myushort,mylong,myulong,myhyper,myuhyper,myfloat,\
281 # mydouble,myenum,mystring,myinterface,myany,myseq,my2ndstruct = self.tobj.setValues2( \
282 # mybool,mychar,mybyte,myshort,myushort,mylong,myulong,myhyper,myuhyper,myfloat,\
283 # mydouble,myenum,mystring,myinterface,myany,myseq,my2ndstruct)
284 # self.assertTrue(struct == self.tobj.Struct, "outparam 1 test")
285 # self.assertTrue( mybool and self.tobj.Bool, "outparam 2 test")
286 # self.assertTrue(mychar == self.tobj.Char, "outparam 3 test")
287 # self.assertTrue(mybyte == self.tobj.Byte, "outparam 4 test")
288 # self.assertTrue(myshort == self.tobj.Short, "outparam 5 test")
289 # self.assertTrue(myushort == self.tobj.UShort, "outparam 6 test")
290 # self.assertTrue(mylong == self.tobj.Long, "outparam 7 test")
291 # self.assertTrue(myulong == self.tobj.ULong, "outparam 8 test")
292 # self.assertTrue(myhyper == self.tobj.Hyper, "outparam 9 test")
293 # self.assertTrue(myuhyper == self.tobj.UHyper, "outparam 10 test")
294 # self.assertTrue(myfloat == self.tobj.Float, "outparam 11 test")
295 # self.assertTrue(mydouble == self.tobj.Double, "outparam 12 test")
296 # self.assertTrue(myenum == self.tobj.Enum, "outparam 13 test")
297 # self.assertTrue(mystring == self.tobj.String, "outparam 14 test")
298 # self.assertTrue(myinterface == self.tobj.Interface, "outparam 15 test")
299 # self.assertTrue(myany == self.tobj.Any, "outparam 16 test")
300 # self.assertTrue(myseq == self.tobj.Sequence, "outparam 17 test")
301 # self.assertTrue(my2ndstruct == struct, "outparam 18 test")
303 def testErrors( self
):
308 self
.fail("attribute a shouldn't exist")
309 except AttributeError:
311 except IllegalArgumentException
:
313 self
.assertTrue( wasHere
, "wrong attribute test" )
315 IllegalArgumentException
= uno
.getClass("com.sun.star.lang.IllegalArgumentException" )
316 RuntimeException
= uno
.getClass("com.sun.star.uno.RuntimeException" )
318 # TODO: Remove this once it is done
319 # wrong number of arguments bug !?
320 self
.assertRaises( IllegalArgumentException
, self
.tobj
.transportAny
, 42, 43 )
321 self
.assertRaises( IllegalArgumentException
, self
.tobj
.transportAny
)
322 self
.assertRaises( RuntimeException
, uno
.getClass
, "a.b" )
323 self
.assertRaises( RuntimeException
, uno
.getClass
, "com.sun.star.uno.TypeClass" )
325 self
.assertRaises( RuntimeException
, uno
.Enum
, "a" , "b" )
326 self
.assertRaises( RuntimeException
, uno
.Enum
, "com.sun.star.uno.TypeClass" , "b" )
327 self
.assertRaises( RuntimeException
, uno
.Enum
, "com.sun.star.uno.XInterface" , "b" )
329 tcInterface
=uno
.Enum( "com.sun.star.uno.TypeClass" , "INTERFACE" )
330 self
.assertRaises( RuntimeException
, uno
.Type
, "a", tcInterface
)
331 self
.assertRaises( RuntimeException
, uno
.Type
, "com.sun.star.uno.Exception", tcInterface
)
333 self
.assertRaises( (RuntimeException
,exceptions
.RuntimeError), uno
.getTypeByName
, "a" )
335 self
.assertRaises( (RuntimeException
), uno
.getConstantByName
, "a" )
336 self
.assertRaises( (RuntimeException
), uno
.getConstantByName
, "com.sun.star.uno.XInterface" )
338 def testByteSequence( self
):
339 s
= uno
.ByteSequence( b
"ab" )
340 self
.assertTrue( s
== uno
.ByteSequence( b
"ab" ) )
341 self
.assertTrue( uno
.ByteSequence( b
"abc" ) == s
+ uno
.ByteSequence( b
"c" ) )
342 self
.assertTrue( uno
.ByteSequence( b
"abc" ) == s
+ "c" )
343 self
.assertTrue( s
+ "c" == "abc" )
344 self
.assertTrue( s
== uno
.ByteSequence( s
) )
345 self
.assertTrue( s
[0] == 'a' )
346 self
.assertTrue( s
[1] == 'b' )
349 def testInvoke( self
):
350 self
.assertTrue( 5 == uno
.invoke( self
.tobj
, "transportAny" , (uno
.Any("byte", 5),) ) )
351 self
.assertTrue( 5 == uno
.invoke(
352 PythonTransporter(), "transportAny" , (uno
.Any( "byte", 5 ),) ) )
353 t
= uno
.getTypeByName( "long" )
354 mystruct
= uno
.createUnoStruct(
355 "com.sun.star.beans.PropertyValue", "foo",0,uno
.Any(t
,2),0 )
356 mystruct
.Value
= uno
.Any(t
, 1)