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: impl.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 #*************************************************************************
31 "tests bridging python implementations of UNO objects"
38 from com
.sun
.star
.io
import XOutputStream
, XInputStream
, typeOfXOutputStream
, typeOfXInputStream
39 from com
.sun
.star
.lang
import XTypeProvider
, typeOfXTypeProvider
, XEventListener
40 from com
.sun
.star
.uno
import XCurrentContext
42 class SequenceOutputStream( unohelper
.Base
, XOutputStream
):
44 self
.s
= uno
.ByteSequence("")
47 def closeOutput(self
):
50 def writeBytes( self
, seq
):
56 def getSequence( self
):
60 class SequenceInputStream( XInputStream
, unohelper
.Base
):
61 def __init__( self
, seq
):
66 def closeInput( self
):
70 def skipBytes( self
, nByteCount
):
71 if( nByteCount
+ self
.nIndex
> len(self
.s
) ):
72 nByteCount
= len(self
.s
) - self
.nIndex
73 self
.nIndex
+= nByteCount
75 def readBytes( self
, retSeq
, nByteCount
):
77 if( self
.nIndex
+ nByteCount
> len(self
.s
) ):
78 nRet
= len(self
.s
) - self
.nIndex
81 retSeq
= uno
.ByteSequence(self
.s
.value
[self
.nIndex
: self
.nIndex
+ nRet
])
82 self
.nIndex
= self
.nIndex
+ nRet
85 def readSomeBytes( self
, retSeq
, nByteCount
):
87 return readBytes( retSeq
, nByteCount
)
89 def available( self
):
90 return len( self
.s
) - self
.nIndex
92 class SequenceInputStream2( SequenceInputStream
):
93 def __init__( self
, seq
):
94 SequenceInputStream
.__init
__( self
, seq
)
96 class TestCase(unittest
.TestCase
):
97 def __init__(self
,method
,ctx
):
98 unittest
.TestCase
.__init
__(self
,method
)
102 self
.tobj
= self
.ctx
.ServiceManager
.createInstanceWithContext( \
103 "com.sun.star.test.bridge.CppTestObject",self
.ctx
)
104 self
.pipe
= self
.ctx
.ServiceManager
.createInstanceWithContext( \
105 "com.sun.star.io.Pipe" , self
.ctx
)
107 def testStandard( self
):
108 dataOut
= self
.ctx
.ServiceManager
.createInstanceWithContext( \
109 "com.sun.star.io.DataOutputStream", self
.ctx
)
110 streamOut
= SequenceOutputStream()
111 dataOut
.setOutputStream( streamOut
)
112 dataOut
.writeShort( 42 )
113 dataOut
.writeLong( 43 )
114 dataOut
.closeOutput()
116 dataInput
= self
.ctx
.ServiceManager
.createInstanceWithContext( \
117 "com.sun.star.io.DataInputStream", self
.ctx
)
119 dataInput
.setInputStream( SequenceInputStream2( streamOut
.getSequence() ) )
121 self
.failUnless( 42 == dataInput
.readShort() )
122 self
.failUnless( 43 == dataInput
.readLong() )
123 self
.failUnless( self
.tobj
.transportAny( streamOut
) == streamOut
)
127 def write( self
, string
):
131 class EventListener( unohelper
.Base
, XEventListener
):
132 def __init__( self
):
133 self
.disposingCalled
= False
135 def disposing( self
, eventObject
):
136 self
.disposingCalled
= True
138 class TestHelperCase( unittest
.TestCase
):
140 def __init__(self
,method
):
141 unittest
.TestCase
.__init
__(self
,method
)
143 def testUrlHelper( self
):
144 systemPath
= os
.getcwd()
145 if systemPath
.startswith( "/" ):
146 self
.failUnless( "/tmp" == unohelper
.fileUrlToSystemPath( "file:///tmp" ) )
147 self
.failUnless( "file:///tmp" == unohelper
.systemPathToFileUrl( "/tmp" ))
149 self
.failUnless( "c:\\temp" == unohelper
.fileUrlToSystemPath( "file:///c:/temp" ) )
150 self
.failUnless( "file:///c:/temp" == unohelper
.systemPathToFileUrl( "c:\\temp" ) )
152 systemPath
= unohelper
.systemPathToFileUrl( systemPath
)
153 self
.failUnless( systemPath
+ "/a" == unohelper
.absolutize( systemPath
, "a" ) )
154 def testInspect( self
):
157 unohelper
.inspect( uno
.getComponentContext() , dev
)
158 unohelper
.inspect( uno
.getComponentContext().ServiceManager
, dev
)
159 unohelper
.inspect( uno
.getTypeByName( "com.sun.star.lang.XComponent" ) , dev
)
161 def testListener( self
):
162 smgr
= uno
.getComponentContext().ServiceManager
.createInstance(
163 "com.sun.star.lang.ServiceManager" )
165 # check, whether listeners
166 listener
= EventListener()
167 smgr
.addEventListener( listener
)
169 self
.failUnless( listener
.disposingCalled
)
171 # check, whether listeners can be removed
172 smgr
= uno
.getComponentContext().ServiceManager
.createInstance(
173 "com.sun.star.lang.ServiceManager" )
174 listener
= EventListener()
175 smgr
.addEventListener( listener
)
176 smgr
.removeEventListener( listener
)
178 self
.failUnless( not listener
.disposingCalled
)
180 def testCurrentContext( self
):
181 oldContext
= uno
.getCurrentContext()
183 uno
.setCurrentContext(
184 unohelper
.CurrentContext( oldContext
,{"My42":42}) )
185 self
.failUnless( 42 == uno
.getCurrentContext().getValueByName( "My42" ) )
186 self
.failUnless( None == uno
.getCurrentContext().getValueByName( "My43" ) )
188 uno
.setCurrentContext( oldContext
)
193 suite
= unittest
.TestSuite()
194 suite
.addTest(TestCase("testStandard",ctx
))
195 suite
.addTest(TestHelperCase( "testUrlHelper" ))
196 suite
.addTest(TestHelperCase( "testInspect" ))
197 suite
.addTest(TestHelperCase( "testListener" ) )
198 suite
.addTest(TestHelperCase( "testCurrentContext" ) )