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