bump product version to 5.0.4.1
[LibreOffice.git] / testtools / source / bridgetest / pyuno / core.py
blobdbba0b4ec57add6c6510603850975a2c2c072b64
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 .
18 import pyuno
19 import uno
20 import unittest
21 import exceptions
22 import types
24 def suite(ctx):
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))
40 return suite
42 def equalsEps( a,b,eps ):
43 if a - eps <= b and a+eps >= b:
44 return 1
45 return 0
47 def assign( rData, bBool, cChar, nByte, nShort, nUShort, nLong, nULong, nHyper,\
48 nUHyper, fFloat, fDouble, eEnum, rStr, xTest, rAny ):
49 rData.Bool = bBool;
50 rData.Char = cChar;
51 rData.Byte = nByte;
52 rData.Short = nShort;
53 rData.UShort = nUShort;
54 rData.Long = nLong;
55 rData.ULong = nULong;
56 rData.Hyper = nHyper;
57 rData.UHyper = nUHyper;
58 rData.Float = fFloat;
59 rData.Double = fDouble;
60 rData.Enum = eEnum;
61 rData.String = rStr;
62 rData.Interface = xTest;
63 rData.Any = rAny;
66 class PythonTransporter:
67 def __init__( self ):
68 pass
70 def transportAny( self, arg ):
71 return arg
73 class TestCase( unittest.TestCase):
75 def __init__(self,method,ctx):
76 unittest.TestCase.__init__(self,method)
77 self.ctx = ctx
79 def setUp(self):
80 # the testcomponent from the testtools project
81 self.tobj = self.ctx.ServiceManager.createInstanceWithContext(
82 'com.sun.star.test.bridge.CppTestObject' , self.ctx )
84 self.tobj.Bool = 1
85 self.tobj.Char = 'h'
86 self.tobj.Byte = 43
87 self.tobj.Short = -42
88 self.tobj.UShort = 44
89 self.tobj.Long = 42
90 self.tobj.ULong = 41
91 self.tobj.Hyper = 46
92 self.tobj.UHyper = 47
93 self.tobj.Float = 4.3
94 self.tobj.Double = 4.2
95 self.tobj.Enum = 4
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.failUnless( 42 == self.tobj.Long , "Long attribute" )
111 self.failUnless( 41 == self.tobj.ULong , "ULong attribute" )
112 self.failUnless( 43 == self.tobj.Byte , "Byte attribute" )
113 self.failUnless( 44 == self.tobj.UShort , "UShort attribute" )
114 self.failUnless( -42 == self.tobj.Short , "Short attribute" )
115 self.failUnless( 46 == self.tobj.Hyper , "Hyper attribute" )
116 self.failUnless( 47 == self.tobj.UHyper , "UHyper attribute" )
117 self.failUnless( self.tobj.Bool , "Bool attribute2" )
118 self.failUnless( "yabadabadoo" == self.tobj.String , "String attribute" )
119 self.failUnless( self.tobj.Sequence[0] == self.testElement , "Sequence test")
120 self.failUnless( self.tobj.Sequence[1] == self.testElement2 , "Sequence2 test")
121 self.failUnless( equalsEps( 4.3,self.tobj.Float,0.0001) , "float test" )
122 self.failUnless( 4.2 == self.tobj.Double , "double test" )
123 self.failUnless( self.ctx == self.tobj.Interface ,
124 "object identity test with C++ object" )
125 self.failUnless( not self.ctx == self.tobj , "object not identical test " )
126 self.failUnless( 42 == self.tobj.transportAny( 42 ), "transportAny long" )
127 self.failUnless( "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.failUnless( e1 == e2 , "equal enum test" )
136 self.failUnless( not (e1 == e3) , "different enums test" )
137 self.failUnless( self.tobj.transportAny( e3 ) == e3, "enum roundtrip test" )
138 self.tobj.Enum = e4
139 self.failUnless( e4 == self.tobj.Enum , "enum assignment failed" )
141 def testType(self ):
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.failUnless( t1.typeClass == \
146 uno.Enum( "com.sun.star.uno.TypeClass", "INTERFACE" ), "typeclass of type test" )
147 self.failUnless( t3.typeClass == \
148 uno.Enum( "com.sun.star.uno.TypeClass", "STRUCT" ), "typeclass of type test")
149 self.failUnless( t1 == t2 , "equal type test" )
150 self.failUnless( t1 == t2 , "equal type test" )
151 self.failUnless( t1 == self.tobj.transportAny( t1 ), "type rountrip test" )
153 def testBool( self ):
154 self.failUnless( uno.Bool(1) , "uno.Bool true test" )
155 self.failUnless( not uno.Bool(0) , "uno.Bool false test" )
156 self.failUnless( uno.Bool( "true") , "uno.Bool true1 test" )
157 self.failUnless( not uno.Bool( "false") , "uno.Bool true1 test" )
159 self.tobj.Bool = uno.Bool(1)
160 self.failUnless( self.tobj.Bool , "bool true attribute test" )
161 self.tobj.Bool = uno.Bool(0)
162 self.failUnless( not self.tobj.Bool , "bool true attribute test" )
164 # new boolean semantic
165 self.failUnless( id( self.tobj.transportAny( True ) ) == id(True) , "boolean preserve test")
166 self.failUnless( id( self.tobj.transportAny( False ) ) == id(False) , "boolean preserve test" )
167 self.failUnless( id( self.tobj.transportAny(1) ) != id( True ), "boolean preserve test" )
168 self.failUnless( id( self.tobj.transportAny(0) ) != id( False ), "boolean preserve test" )
170 def testChar( self ):
171 self.tobj.Char = uno.Char( u'h' )
172 self.failUnless( self.tobj.Char == uno.Char( u'h' ), "char type test" )
173 self.failUnless( 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.failUnless( self.tobj.Struct == mystruct, "struct roundtrip for equality test" )
182 self.failUnless( aSecondStruct == mystruct, "struct roundtrip for equality test2" )
183 aSecondStruct.Short = 720
184 self.failUnless( not aSecondStruct == mystruct , "different structs equality test" )
185 self.failUnless( not self.ctx == mystruct , "object is not equal to struct test" )
186 self.failUnless( 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.failUnless( my2ndstruct == mystruct, "struct non-default ctor test" )
190 def testUnicode( self ):
191 uni = u'\0148'
192 self.tobj.String = uni
193 self.failUnless( uni == self.tobj.String )
196 self.tobj.String = u'dubidu'
197 self.failUnless( u'dubidu' == self.tobj.String , "unicode comparison test")
198 self.failUnless( 'dubidu' == self.tobj.String , "unicode vs. string comparison test" )
200 def testConstant( self ):
201 self.failUnless( 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" )
208 wasHere = 0
209 try:
210 raise ioExc( "huhuh" , self.tobj )
211 except unoExc , instance:
212 wasHere = 1
213 self.failUnless( wasHere , "exceptiont test 1" )
215 wasHere = 0
216 try:
217 raise ioExc
218 except ioExc:
219 wasHere = 1
220 else:
221 self.failUnless( wasHere, "exception test 2" )
223 wasHere = 0
224 try:
225 raise dispExc
226 except ioExc:
227 pass
228 except unoExc:
229 wasHere = 1
230 self.failUnless(wasHere, "exception test 3")
232 illegalArg = uno.getClass( "com.sun.star.lang.IllegalArgumentException" )
233 wasHere = 0
234 try:
235 self.tobj.raiseException( 1 , "foo" , self.tobj )
236 self.failUnless( 0 , "exception test 5a" )
237 except ioExc:
238 self.failUnless( 0 , "exception test 5b" )
239 except illegalArg, i:
240 self.failUnless( 1 == i.ArgumentPosition , "exception member test" )
241 self.failUnless( "foo" == i.Message , "exception member test 2 " )
242 wasHere = 1
243 else:
244 self.failUnless( 0, "except test 5c" )
245 self.failUnless( wasHere, "illegal argument exception test failed" )
247 def testInterface(self):
248 clazz = uno.getClass( "com.sun.star.lang.XComponent" )
249 self.failUnless( "com.sun.star.lang.XComponent" == clazz.__pyunointerface__ )
250 self.failUnless( issubclass( clazz, uno.getClass( "com.sun.star.uno.XInterface" ) ) )
251 self.tobj.Interface = None
254 def testOutparam( self):
255 # outparameter
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.failUnless(struct == self.tobj.Struct, "outparam 1 test")
261 self.failUnless(self.tobj.Bool, "outparam 2 test")
262 self.failUnless(mychar == self.tobj.Char, "outparam 3 test")
263 self.failUnless(mybyte == self.tobj.Byte, "outparam 4 test")
264 self.failUnless(myshort == self.tobj.Short, "outparam 5 test")
265 self.failUnless(myushort == self.tobj.UShort, "outparam 6 test")
266 self.failUnless(mylong == self.tobj.Long, "outparam 7 test")
267 self.failUnless(myulong == self.tobj.ULong, "outparam 8 test")
268 self.failUnless(myhyper == self.tobj.Hyper, "outparam 9 test")
269 self.failUnless(myuhyper == self.tobj.UHyper, "outparam 10 test")
270 self.failUnless(myfloat == self.tobj.Float, "outparam 11 test")
271 self.failUnless(mydouble == self.tobj.Double, "outparam 12 test")
272 self.failUnless(myenum == self.tobj.Enum, "outparam 13 test")
273 self.failUnless(mystring == self.tobj.String, "outparam 14 test")
274 self.failUnless(myinterface == self.tobj.Interface, "outparam 15 test")
275 self.failUnless(myany == self.tobj.Any, "outparam 16 test")
276 self.failUnless(myseq == self.tobj.Sequence, "outparam 17 test")
277 self.failUnless(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.failUnless(struct == self.tobj.Struct, "outparam 1 test")
285 # self.failUnless( mybool and self.tobj.Bool, "outparam 2 test")
286 # self.failUnless(mychar == self.tobj.Char, "outparam 3 test")
287 # self.failUnless(mybyte == self.tobj.Byte, "outparam 4 test")
288 # self.failUnless(myshort == self.tobj.Short, "outparam 5 test")
289 # self.failUnless(myushort == self.tobj.UShort, "outparam 6 test")
290 # self.failUnless(mylong == self.tobj.Long, "outparam 7 test")
291 # self.failUnless(myulong == self.tobj.ULong, "outparam 8 test")
292 # self.failUnless(myhyper == self.tobj.Hyper, "outparam 9 test")
293 # self.failUnless(myuhyper == self.tobj.UHyper, "outparam 10 test")
294 # self.failUnless(myfloat == self.tobj.Float, "outparam 11 test")
295 # self.failUnless(mydouble == self.tobj.Double, "outparam 12 test")
296 # self.failUnless(myenum == self.tobj.Enum, "outparam 13 test")
297 # self.failUnless(mystring == self.tobj.String, "outparam 14 test")
298 # self.failUnless(myinterface == self.tobj.Interface, "outparam 15 test")
299 # self.failUnless(myany == self.tobj.Any, "outparam 16 test")
300 # self.failUnless(myseq == self.tobj.Sequence, "outparam 17 test")
301 # self.failUnless(my2ndstruct == struct, "outparam 18 test")
303 def testErrors( self ):
305 wasHere = 0
306 try:
307 self.tobj.a = 5
308 self.fail("attribute a shouldn't exist")
309 except AttributeError:
310 wasHere = 1
311 except IllegalArgumentException:
312 wasHere = 1
313 self.failUnless( 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.failUnlessRaises( IllegalArgumentException, self.tobj.transportAny, 42, 43 )
321 self.failUnlessRaises( IllegalArgumentException, self.tobj.transportAny )
322 self.failUnlessRaises( RuntimeException, uno.getClass, "a.b" )
323 self.failUnlessRaises( RuntimeException, uno.getClass, "com.sun.star.uno.TypeClass" )
325 self.failUnlessRaises( RuntimeException, uno.Enum, "a" , "b" )
326 self.failUnlessRaises( RuntimeException, uno.Enum, "com.sun.star.uno.TypeClass" , "b" )
327 self.failUnlessRaises( RuntimeException, uno.Enum, "com.sun.star.uno.XInterface" , "b" )
329 tcInterface =uno.Enum( "com.sun.star.uno.TypeClass" , "INTERFACE" )
330 self.failUnlessRaises( RuntimeException, uno.Type, "a", tcInterface )
331 self.failUnlessRaises( RuntimeException, uno.Type, "com.sun.star.uno.Exception", tcInterface )
333 self.failUnlessRaises( (RuntimeException,exceptions.RuntimeError), uno.getTypeByName, "a" )
335 self.failUnlessRaises( (RuntimeException), uno.getConstantByName, "a" )
336 self.failUnlessRaises( (RuntimeException), uno.getConstantByName, "com.sun.star.uno.XInterface" )
338 def testByteSequence( self ):
339 s = uno.ByteSequence( "ab" )
340 self.failUnless( s == uno.ByteSequence( "ab" ) )
341 self.failUnless( uno.ByteSequence( "abc" ) == s + uno.ByteSequence( "c" ) )
342 self.failUnless( uno.ByteSequence( "abc" ) == s + "c" )
343 self.failUnless( s + "c" == "abc" )
344 self.failUnless( s == uno.ByteSequence( s ) )
345 self.failUnless( s[0] == 'a' )
346 self.failUnless( s[1] == 'b' )
349 def testInvoke( self ):
350 self.failUnless( 5 == uno.invoke( self.tobj , "transportAny" , (uno.Any("byte", 5),) ) )
351 self.failUnless( 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)