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