1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: core.py,v $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 #*************************************************************************
38 suite
= unittest
.TestSuite()
39 suite
.addTest(TestCase("testErrors",ctx
))
40 suite
.addTest(TestCase("testBaseTypes",ctx
))
41 suite
.addTest(TestCase("testOutparam",ctx
))
42 suite
.addTest(TestCase("testStruct",ctx
))
43 suite
.addTest(TestCase("testType",ctx
))
44 suite
.addTest(TestCase("testEnum",ctx
))
45 suite
.addTest(TestCase("testBool",ctx
))
46 suite
.addTest(TestCase("testChar",ctx
))
47 suite
.addTest(TestCase("testUnicode",ctx
))
48 suite
.addTest(TestCase("testConstant",ctx
))
49 suite
.addTest(TestCase("testExceptions",ctx
))
50 suite
.addTest(TestCase("testInterface",ctx
))
51 suite
.addTest(TestCase("testByteSequence",ctx
))
52 suite
.addTest(TestCase("testInvoke",ctx
))
55 def equalsEps( a
,b
,eps
):
56 if a
- eps
<= b
and a
+eps
>= b
:
60 def assign( rData
, bBool
, cChar
, nByte
, nShort
, nUShort
, nLong
, nULong
, nHyper
,\
61 nUHyper
, fFloat
, fDouble
, eEnum
, rStr
, xTest
, rAny
):
66 rData
.UShort
= nUShort
;
70 rData
.UHyper
= nUHyper
;
72 rData
.Double
= fDouble
;
75 rData
.Interface
= xTest
;
79 class PythonTransporter
:
83 def transportAny( self
, arg
):
86 class TestCase( unittest
.TestCase
):
88 def __init__(self
,method
,ctx
):
89 unittest
.TestCase
.__init
__(self
,method
)
93 # the testcomponent from the testtools project
94 self
.tobj
= self
.ctx
.ServiceManager
.createInstanceWithContext(
95 'com.sun.star.test.bridge.CppTestObject' , self
.ctx
)
100 self
.tobj
.Short
= -42
101 self
.tobj
.UShort
= 44
105 self
.tobj
.UHyper
= 47
106 self
.tobj
.Float
= 4.3
107 self
.tobj
.Double
= 4.2
109 self
.tobj
.String
= "yabadabadoo"
110 self
.tobj
.Interface
= self
.ctx
111 self
.tobj
.Any
= self
.tobj
.String
112 mystruct
= uno
.createUnoStruct( "test.testtools.bridgetest.TestData" )
113 assign( mystruct
, 1, 'h', 43, -42,44,42,41,46,47,4.3,4.2,4,"yabadabadoo",self
.ctx
,"yabadabadoo")
114 self
.tobj
.Struct
= mystruct
116 self
.testElement
= uno
.createUnoStruct( "test.testtools.bridgetest.TestElement" )
117 self
.testElement
.String
= "foo"
118 self
.testElement2
= uno
.createUnoStruct( "test.testtools.bridgetest.TestElement" )
119 self
.testElement2
.String
= "42"
120 self
.tobj
.Sequence
= (self
.testElement
,self
.testElement2
)
122 def testBaseTypes(self
):
123 self
.failUnless( 42 == self
.tobj
.Long
, "Long attribute" )
124 self
.failUnless( 41 == self
.tobj
.ULong
, "ULong attribute" )
125 self
.failUnless( 43 == self
.tobj
.Byte
, "Byte attribute" )
126 self
.failUnless( 44 == self
.tobj
.UShort
, "UShort attribute" )
127 self
.failUnless( -42 == self
.tobj
.Short
, "Short attribute" )
128 self
.failUnless( 46 == self
.tobj
.Hyper
, "Hyper attribute" )
129 self
.failUnless( 47 == self
.tobj
.UHyper
, "UHyper attribute" )
130 self
.failUnless( self
.tobj
.Bool
, "Bool attribute2" )
131 self
.failUnless( "yabadabadoo" == self
.tobj
.String
, "String attribute" )
132 self
.failUnless( self
.tobj
.Sequence
[0] == self
.testElement
, "Sequence test")
133 self
.failUnless( self
.tobj
.Sequence
[1] == self
.testElement2
, "Sequence2 test")
134 self
.failUnless( equalsEps( 4.3,self
.tobj
.Float
,0.0001) , "float test" )
135 self
.failUnless( 4.2 == self
.tobj
.Double
, "double test" )
136 self
.failUnless( self
.ctx
== self
.tobj
.Interface
,
137 "object identity test with C++ object" )
138 self
.failUnless( not self
.ctx
== self
.tobj
, "object not identical test " )
139 self
.failUnless( 42 == self
.tobj
.transportAny( 42 ), "transportAny long" )
140 self
.failUnless( "woo, this is python" == self
.tobj
.transportAny( "woo, this is python" ), \
141 "string roundtrip via any test" )
143 def testEnum( self
):
144 e1
= uno
.Enum( "com.sun.star.uno.TypeClass" , "LONG" )
145 e2
= uno
.Enum( "com.sun.star.uno.TypeClass" , "LONG" )
146 e3
= uno
.Enum( "com.sun.star.uno.TypeClass" , "UNSIGNED_LONG" )
147 e4
= uno
.Enum( "test.testtools.bridgetest.TestEnum" , "TWO" )
148 self
.failUnless( e1
== e2
, "equal enum test" )
149 self
.failUnless( not (e1
== e3
) , "different enums test" )
150 self
.failUnless( self
.tobj
.transportAny( e3
) == e3
, "enum roundtrip test" )
152 self
.failUnless( e4
== self
.tobj
.Enum
, "enum assignment failed" )
155 t1
= uno
.getTypeByName( "com.sun.star.lang.XComponent" )
156 t2
= uno
.getTypeByName( "com.sun.star.lang.XComponent" )
157 t3
= uno
.getTypeByName( "com.sun.star.lang.EventObject" )
158 self
.failUnless( t1
.typeClass
== \
159 uno
.Enum( "com.sun.star.uno.TypeClass", "INTERFACE" ), "typeclass of type test" )
160 self
.failUnless( t3
.typeClass
== \
161 uno
.Enum( "com.sun.star.uno.TypeClass", "STRUCT" ), "typeclass of type test")
162 self
.failUnless( t1
== t2
, "equal type test" )
163 self
.failUnless( t1
== t2
, "equal type test" )
164 self
.failUnless( t1
== self
.tobj
.transportAny( t1
), "type rountrip test" )
166 def testBool( self
):
167 self
.failUnless( uno
.Bool(1) , "uno.Bool true test" )
168 self
.failUnless( not uno
.Bool(0) , "uno.Bool false test" )
169 self
.failUnless( uno
.Bool( "true") , "uno.Bool true1 test" )
170 self
.failUnless( not uno
.Bool( "false") , "uno.Bool true1 test" )
172 self
.tobj
.Bool
= uno
.Bool(1)
173 self
.failUnless( self
.tobj
.Bool
, "bool true attribute test" )
174 self
.tobj
.Bool
= uno
.Bool(0)
175 self
.failUnless( not self
.tobj
.Bool
, "bool true attribute test" )
177 # new boolean semantic
178 self
.failUnless( id( self
.tobj
.transportAny( True ) ) == id(True) , "boolean preserve test")
179 self
.failUnless( id( self
.tobj
.transportAny( False ) ) == id(False) , "boolean preserve test" )
180 self
.failUnless( id( self
.tobj
.transportAny(1) ) != id( True ), "boolean preserve test" )
181 self
.failUnless( id( self
.tobj
.transportAny(0) ) != id( False ), "boolean preserve test" )
183 def testChar( self
):
184 self
.tobj
.Char
= uno
.Char( u
'h' )
185 self
.failUnless( self
.tobj
.Char
== uno
.Char( u
'h' ), "char type test" )
186 self
.failUnless( isinstance( self
.tobj
.transportAny( uno
.Char(u
'h') ),uno
.Char
),"char preserve test" )
188 def testStruct( self
):
189 mystruct
= uno
.createUnoStruct( "test.testtools.bridgetest.TestData" )
190 assign( mystruct
, 1, 'h', 43, -42,44,42,41,46,47,4.3,4.2,4,"yabadabadoo",self
.ctx
,"yabadabadoo")
191 self
.tobj
.Struct
= mystruct
192 aSecondStruct
= self
.tobj
.Struct
194 self
.failUnless( self
.tobj
.Struct
== mystruct
, "struct roundtrip for equality test" )
195 self
.failUnless( aSecondStruct
== mystruct
, "struct roundtrip for equality test2" )
196 aSecondStruct
.Short
= 720
197 self
.failUnless( not aSecondStruct
== mystruct
, "different structs equality test" )
198 self
.failUnless( not self
.ctx
== mystruct
, "object is not equal to struct test" )
199 self
.failUnless( mystruct
== self
.tobj
.transportAny( mystruct
), "struct roundtrip with any test" )
200 my2ndstruct
= uno
.createUnoStruct( "test.testtools.bridgetest.TestData", \
201 1, 'h', 43, -42,44,42,41,46,47,4.3,4.2,4,"yabadabadoo",self
.ctx
,"yabadabadoo",())
202 self
.failUnless( my2ndstruct
== mystruct
, "struct non-default ctor test" )
203 def testUnicode( self
):
205 self
.tobj
.String
= uni
206 self
.failUnless( uni
== self
.tobj
.String
)
209 self
.tobj
.String
= u
'dubidu'
210 self
.failUnless( u
'dubidu' == self
.tobj
.String
, "unicode comparison test")
211 self
.failUnless( 'dubidu' == self
.tobj
.String
, "unicode vs. string comparison test" )
213 def testConstant( self
):
214 self
.failUnless( uno
.getConstantByName( "com.sun.star.beans.PropertyConcept.ATTRIBUTES" ) == 4,\
215 "constant retrieval test" )
217 def testExceptions( self
):
218 unoExc
= uno
.getClass( "com.sun.star.uno.Exception" )
219 ioExc
= uno
.getClass( "com.sun.star.io.IOException" )
220 dispExc
= uno
.getClass( "com.sun.star.lang.DisposedException" )
223 raise ioExc( "huhuh" , self
.tobj
)
224 except unoExc
, instance
:
226 self
.failUnless( wasHere
, "exceptiont test 1" )
234 self
.failUnless( wasHere
, "exception test 2" )
243 self
.failUnless(wasHere
, "exception test 3")
245 illegalArg
= uno
.getClass( "com.sun.star.lang.IllegalArgumentException" )
248 self
.tobj
.raiseException( 1 , "foo" , self
.tobj
)
249 self
.failUnless( 0 , "exception test 5a" )
251 self
.failUnless( 0 , "exception test 5b" )
252 except illegalArg
, i
:
253 self
.failUnless( 1 == i
.ArgumentPosition
, "exception member test" )
254 self
.failUnless( "foo" == i
.Message
, "exception member test 2 " )
257 self
.failUnless( 0, "except test 5c" )
258 self
.failUnless( wasHere
, "illegal argument exception test failed" )
260 def testInterface(self
):
261 clazz
= uno
.getClass( "com.sun.star.lang.XComponent" )
262 self
.failUnless( "com.sun.star.lang.XComponent" == clazz
.__pyunointerface
__ )
263 self
.failUnless( issubclass( clazz
, uno
.getClass( "com.sun.star.uno.XInterface" ) ) )
264 self
.tobj
.Interface
= None
267 def testOutparam( self
):
269 struct
, mybool
,mychar
,mybyte
,myshort
,myushort
,mylong
,myulong
,myhyper
,myuhyper
,myfloat
, \
270 mydouble
,myenum
,mystring
,myinterface
,myany
,myseq
,my2ndstruct
= self
.tobj
.getValues( \
271 None,None,None,None,None,None,None,None,None,None, \
272 None,None,None,None,None,None,None)
273 self
.failUnless(struct
== self
.tobj
.Struct
, "outparam 1 test")
274 self
.failUnless(self
.tobj
.Bool
, "outparam 2 test")
275 self
.failUnless(mychar
== self
.tobj
.Char
, "outparam 3 test")
276 self
.failUnless(mybyte
== self
.tobj
.Byte
, "outparam 4 test")
277 self
.failUnless(myshort
== self
.tobj
.Short
, "outparam 5 test")
278 self
.failUnless(myushort
== self
.tobj
.UShort
, "outparam 6 test")
279 self
.failUnless(mylong
== self
.tobj
.Long
, "outparam 7 test")
280 self
.failUnless(myulong
== self
.tobj
.ULong
, "outparam 8 test")
281 self
.failUnless(myhyper
== self
.tobj
.Hyper
, "outparam 9 test")
282 self
.failUnless(myuhyper
== self
.tobj
.UHyper
, "outparam 10 test")
283 self
.failUnless(myfloat
== self
.tobj
.Float
, "outparam 11 test")
284 self
.failUnless(mydouble
== self
.tobj
.Double
, "outparam 12 test")
285 self
.failUnless(myenum
== self
.tobj
.Enum
, "outparam 13 test")
286 self
.failUnless(mystring
== self
.tobj
.String
, "outparam 14 test")
287 self
.failUnless(myinterface
== self
.tobj
.Interface
, "outparam 15 test")
288 self
.failUnless(myany
== self
.tobj
.Any
, "outparam 16 test")
289 self
.failUnless(myseq
== self
.tobj
.Sequence
, "outparam 17 test")
290 self
.failUnless(my2ndstruct
== struct
, "outparam 18 test")
292 # should work, debug on windows, why not
293 # struct, mybool,mychar,mybyte,myshort,myushort,mylong,myulong,myhyper,myuhyper,myfloat,\
294 # mydouble,myenum,mystring,myinterface,myany,myseq,my2ndstruct = self.tobj.setValues2( \
295 # mybool,mychar,mybyte,myshort,myushort,mylong,myulong,myhyper,myuhyper,myfloat,\
296 # mydouble,myenum,mystring,myinterface,myany,myseq,my2ndstruct)
297 # self.failUnless(struct == self.tobj.Struct, "outparam 1 test")
298 # self.failUnless( mybool and self.tobj.Bool, "outparam 2 test")
299 # self.failUnless(mychar == self.tobj.Char, "outparam 3 test")
300 # self.failUnless(mybyte == self.tobj.Byte, "outparam 4 test")
301 # self.failUnless(myshort == self.tobj.Short, "outparam 5 test")
302 # self.failUnless(myushort == self.tobj.UShort, "outparam 6 test")
303 # self.failUnless(mylong == self.tobj.Long, "outparam 7 test")
304 # self.failUnless(myulong == self.tobj.ULong, "outparam 8 test")
305 # self.failUnless(myhyper == self.tobj.Hyper, "outparam 9 test")
306 # self.failUnless(myuhyper == self.tobj.UHyper, "outparam 10 test")
307 # self.failUnless(myfloat == self.tobj.Float, "outparam 11 test")
308 # self.failUnless(mydouble == self.tobj.Double, "outparam 12 test")
309 # self.failUnless(myenum == self.tobj.Enum, "outparam 13 test")
310 # self.failUnless(mystring == self.tobj.String, "outparam 14 test")
311 # self.failUnless(myinterface == self.tobj.Interface, "outparam 15 test")
312 # self.failUnless(myany == self.tobj.Any, "outparam 16 test")
313 # self.failUnless(myseq == self.tobj.Sequence, "outparam 17 test")
314 # self.failUnless(my2ndstruct == struct, "outparam 18 test")
316 def testErrors( self
):
321 self
.fail("attribute a shouldn't exist")
322 except AttributeError:
324 except IllegalArgumentException
:
326 self
.failUnless( wasHere
, "wrong attribute test" )
328 IllegalArgumentException
= uno
.getClass("com.sun.star.lang.IllegalArgumentException" )
329 RuntimeException
= uno
.getClass("com.sun.star.uno.RuntimeException" )
331 # TODO: Remove this once it is done
332 # wrong number of arguments bug !?
333 self
.failUnlessRaises( IllegalArgumentException
, self
.tobj
.transportAny
, 42, 43 )
334 self
.failUnlessRaises( IllegalArgumentException
, self
.tobj
.transportAny
)
335 self
.failUnlessRaises( RuntimeException
, uno
.getClass
, "a.b" )
336 self
.failUnlessRaises( RuntimeException
, uno
.getClass
, "com.sun.star.uno.TypeClass" )
338 self
.failUnlessRaises( RuntimeException
, uno
.Enum
, "a" , "b" )
339 self
.failUnlessRaises( RuntimeException
, uno
.Enum
, "com.sun.star.uno.TypeClass" , "b" )
340 self
.failUnlessRaises( RuntimeException
, uno
.Enum
, "com.sun.star.uno.XInterface" , "b" )
342 tcInterface
=uno
.Enum( "com.sun.star.uno.TypeClass" , "INTERFACE" )
343 self
.failUnlessRaises( RuntimeException
, uno
.Type
, "a", tcInterface
)
344 self
.failUnlessRaises( RuntimeException
, uno
.Type
, "com.sun.star.uno.Exception", tcInterface
)
346 self
.failUnlessRaises( (RuntimeException
,exceptions
.RuntimeError), uno
.getTypeByName
, "a" )
348 self
.failUnlessRaises( (RuntimeException
), uno
.getConstantByName
, "a" )
349 self
.failUnlessRaises( (RuntimeException
), uno
.getConstantByName
, "com.sun.star.uno.XInterface" )
351 def testByteSequence( self
):
352 s
= uno
.ByteSequence( "ab" )
353 self
.failUnless( s
== uno
.ByteSequence( "ab" ) )
354 self
.failUnless( uno
.ByteSequence( "abc" ) == s
+ uno
.ByteSequence( "c" ) )
355 self
.failUnless( uno
.ByteSequence( "abc" ) == s
+ "c" )
356 self
.failUnless( s
+ "c" == "abc" )
357 self
.failUnless( s
== uno
.ByteSequence( s
) )
358 self
.failUnless( s
[0] == 'a' )
359 self
.failUnless( s
[1] == 'b' )
362 def testInvoke( self
):
363 self
.failUnless( 5 == uno
.invoke( self
.tobj
, "transportAny" , (uno
.Any("byte", 5),) ) )
364 self
.failUnless( 5 == uno
.invoke(
365 PythonTransporter(), "transportAny" , (uno
.Any( "byte", 5 ),) ) )
366 t
= uno
.getTypeByName( "long" )
367 mystruct
= uno
.createUnoStruct(
368 "com.sun.star.beans.PropertyValue", "foo",0,uno
.Any(t
,2),0 )
369 mystruct
.Value
= uno
.Any(t
, 1)