1*a0428e9eSAndrew Rist#************************************************************** 2*a0428e9eSAndrew Rist# 3*a0428e9eSAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 4*a0428e9eSAndrew Rist# or more contributor license agreements. See the NOTICE file 5*a0428e9eSAndrew Rist# distributed with this work for additional information 6*a0428e9eSAndrew Rist# regarding copyright ownership. The ASF licenses this file 7*a0428e9eSAndrew Rist# to you under the Apache License, Version 2.0 (the 8*a0428e9eSAndrew Rist# "License"); you may not use this file except in compliance 9*a0428e9eSAndrew Rist# with the License. You may obtain a copy of the License at 10*a0428e9eSAndrew Rist# 11*a0428e9eSAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 12*a0428e9eSAndrew Rist# 13*a0428e9eSAndrew Rist# Unless required by applicable law or agreed to in writing, 14*a0428e9eSAndrew Rist# software distributed under the License is distributed on an 15*a0428e9eSAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a0428e9eSAndrew Rist# KIND, either express or implied. See the License for the 17*a0428e9eSAndrew Rist# specific language governing permissions and limitations 18*a0428e9eSAndrew Rist# under the License. 19*a0428e9eSAndrew Rist# 20*a0428e9eSAndrew Rist#************************************************************** 21cdf0e10cSrcweir"tests bridging python implementations of UNO objects" 22cdf0e10cSrcweirimport unittest 23cdf0e10cSrcweirimport uno 24cdf0e10cSrcweirimport unohelper 25cdf0e10cSrcweirimport os 26cdf0e10cSrcweirimport sys 27cdf0e10cSrcweir 28cdf0e10cSrcweirfrom com.sun.star.io import XOutputStream, XInputStream, typeOfXOutputStream, typeOfXInputStream 29cdf0e10cSrcweirfrom com.sun.star.lang import XTypeProvider, typeOfXTypeProvider, XEventListener 30cdf0e10cSrcweirfrom com.sun.star.uno import XCurrentContext 31cdf0e10cSrcweir 32cdf0e10cSrcweirclass SequenceOutputStream( unohelper.Base, XOutputStream ): 33cdf0e10cSrcweir def __init__( self ): 34cdf0e10cSrcweir self.s = uno.ByteSequence("") 35cdf0e10cSrcweir self.closed = 0 36cdf0e10cSrcweir 37cdf0e10cSrcweir def closeOutput(self): 38cdf0e10cSrcweir self.closed = 1 39cdf0e10cSrcweir 40cdf0e10cSrcweir def writeBytes( self, seq ): 41cdf0e10cSrcweir self.s = self.s + seq 42cdf0e10cSrcweir 43cdf0e10cSrcweir def flush( self ): 44cdf0e10cSrcweir pass 45cdf0e10cSrcweir 46cdf0e10cSrcweir def getSequence( self ): 47cdf0e10cSrcweir return self.s 48cdf0e10cSrcweir 49cdf0e10cSrcweir 50cdf0e10cSrcweirclass SequenceInputStream( XInputStream, unohelper.Base ): 51cdf0e10cSrcweir def __init__( self, seq ): 52cdf0e10cSrcweir self.s = seq 53cdf0e10cSrcweir self.nIndex = 0 54cdf0e10cSrcweir self.closed = 0 55cdf0e10cSrcweir 56cdf0e10cSrcweir def closeInput( self): 57cdf0e10cSrcweir self.closed = 1 58cdf0e10cSrcweir self.s = None 59cdf0e10cSrcweir 60cdf0e10cSrcweir def skipBytes( self, nByteCount ): 61cdf0e10cSrcweir if( nByteCount + self.nIndex > len(self.s) ): 62cdf0e10cSrcweir nByteCount = len(self.s) - self.nIndex 63cdf0e10cSrcweir self.nIndex += nByteCount 64cdf0e10cSrcweir 65cdf0e10cSrcweir def readBytes( self, retSeq, nByteCount ): 66cdf0e10cSrcweir nRet = 0 67cdf0e10cSrcweir if( self.nIndex + nByteCount > len(self.s) ): 68cdf0e10cSrcweir nRet = len(self.s) - self.nIndex 69cdf0e10cSrcweir else: 70cdf0e10cSrcweir nRet = nByteCount 71cdf0e10cSrcweir retSeq = uno.ByteSequence(self.s.value[self.nIndex : self.nIndex + nRet ]) 72cdf0e10cSrcweir self.nIndex = self.nIndex + nRet 73cdf0e10cSrcweir return nRet, retSeq 74cdf0e10cSrcweir 75cdf0e10cSrcweir def readSomeBytes( self, retSeq , nByteCount ): 76cdf0e10cSrcweir #as we never block ! 77cdf0e10cSrcweir return readBytes( retSeq, nByteCount ) 78cdf0e10cSrcweir 79cdf0e10cSrcweir def available( self ): 80cdf0e10cSrcweir return len( self.s ) - self.nIndex 81cdf0e10cSrcweir 82cdf0e10cSrcweirclass SequenceInputStream2( SequenceInputStream ): 83cdf0e10cSrcweir def __init__( self, seq ): 84cdf0e10cSrcweir SequenceInputStream.__init__( self, seq ) 85cdf0e10cSrcweir 86cdf0e10cSrcweirclass TestCase(unittest.TestCase): 87cdf0e10cSrcweir def __init__(self,method,ctx): 88cdf0e10cSrcweir unittest.TestCase.__init__(self,method) 89cdf0e10cSrcweir self.ctx = ctx 90cdf0e10cSrcweir 91cdf0e10cSrcweir def setUp(self): 92cdf0e10cSrcweir self.tobj = self.ctx.ServiceManager.createInstanceWithContext( \ 93cdf0e10cSrcweir "com.sun.star.test.bridge.CppTestObject",self.ctx) 94cdf0e10cSrcweir self.pipe = self.ctx.ServiceManager.createInstanceWithContext( \ 95cdf0e10cSrcweir "com.sun.star.io.Pipe" , self.ctx ) 96cdf0e10cSrcweir 97cdf0e10cSrcweir def testStandard( self ): 98cdf0e10cSrcweir dataOut = self.ctx.ServiceManager.createInstanceWithContext( \ 99cdf0e10cSrcweir "com.sun.star.io.DataOutputStream", self.ctx ) 100cdf0e10cSrcweir streamOut = SequenceOutputStream() 101cdf0e10cSrcweir dataOut.setOutputStream( streamOut ) 102cdf0e10cSrcweir dataOut.writeShort( 42 ) 103cdf0e10cSrcweir dataOut.writeLong( 43 ) 104cdf0e10cSrcweir dataOut.closeOutput() 105cdf0e10cSrcweir 106cdf0e10cSrcweir dataInput = self.ctx.ServiceManager.createInstanceWithContext( \ 107cdf0e10cSrcweir "com.sun.star.io.DataInputStream", self.ctx ) 108cdf0e10cSrcweir 109cdf0e10cSrcweir dataInput.setInputStream( SequenceInputStream2( streamOut.getSequence() ) ) 110cdf0e10cSrcweir 111cdf0e10cSrcweir self.failUnless( 42 == dataInput.readShort() ) 112cdf0e10cSrcweir self.failUnless( 43 == dataInput.readLong() ) 113cdf0e10cSrcweir self.failUnless( self.tobj.transportAny( streamOut ) == streamOut ) 114cdf0e10cSrcweir 115cdf0e10cSrcweir 116cdf0e10cSrcweirclass NullDevice: 117cdf0e10cSrcweir def write( self, string ): 118cdf0e10cSrcweir pass 119cdf0e10cSrcweir 120cdf0e10cSrcweir 121cdf0e10cSrcweirclass EventListener( unohelper.Base, XEventListener ): 122cdf0e10cSrcweir def __init__( self ): 123cdf0e10cSrcweir self.disposingCalled = False 124cdf0e10cSrcweir 125cdf0e10cSrcweir def disposing( self , eventObject ): 126cdf0e10cSrcweir self.disposingCalled = True 127cdf0e10cSrcweir 128cdf0e10cSrcweirclass TestHelperCase( unittest.TestCase ): 129cdf0e10cSrcweir 130cdf0e10cSrcweir def __init__(self,method): 131cdf0e10cSrcweir unittest.TestCase.__init__(self,method) 132cdf0e10cSrcweir 133cdf0e10cSrcweir def testUrlHelper( self ): 134cdf0e10cSrcweir systemPath = os.getcwd() 135cdf0e10cSrcweir if systemPath.startswith( "/" ): 136cdf0e10cSrcweir self.failUnless( "/tmp" == unohelper.fileUrlToSystemPath( "file:///tmp" ) ) 137cdf0e10cSrcweir self.failUnless( "file:///tmp" == unohelper.systemPathToFileUrl( "/tmp" )) 138cdf0e10cSrcweir else: 139cdf0e10cSrcweir self.failUnless( "c:\\temp" == unohelper.fileUrlToSystemPath( "file:///c:/temp" ) ) 140cdf0e10cSrcweir self.failUnless( "file:///c:/temp" == unohelper.systemPathToFileUrl( "c:\\temp" ) ) 141cdf0e10cSrcweir 142cdf0e10cSrcweir systemPath = unohelper.systemPathToFileUrl( systemPath ) 143cdf0e10cSrcweir self.failUnless( systemPath + "/a" == unohelper.absolutize( systemPath, "a" ) ) 144cdf0e10cSrcweir def testInspect( self ): 145cdf0e10cSrcweir dev = NullDevice() 146cdf0e10cSrcweir# dev = sys.stdout 147cdf0e10cSrcweir unohelper.inspect( uno.getComponentContext() , dev ) 148cdf0e10cSrcweir unohelper.inspect( uno.getComponentContext().ServiceManager , dev ) 149cdf0e10cSrcweir unohelper.inspect( uno.getTypeByName( "com.sun.star.lang.XComponent" ) , dev ) 150cdf0e10cSrcweir 151cdf0e10cSrcweir def testListener( self ): 152cdf0e10cSrcweir smgr = uno.getComponentContext().ServiceManager.createInstance( 153cdf0e10cSrcweir "com.sun.star.lang.ServiceManager" ) 154cdf0e10cSrcweir 155cdf0e10cSrcweir # check, whether listeners 156cdf0e10cSrcweir listener = EventListener() 157cdf0e10cSrcweir smgr.addEventListener( listener ) 158cdf0e10cSrcweir smgr.dispose() 159cdf0e10cSrcweir self.failUnless( listener.disposingCalled ) 160cdf0e10cSrcweir 161cdf0e10cSrcweir # check, whether listeners can be removed 162cdf0e10cSrcweir smgr = uno.getComponentContext().ServiceManager.createInstance( 163cdf0e10cSrcweir "com.sun.star.lang.ServiceManager" ) 164cdf0e10cSrcweir listener = EventListener() 165cdf0e10cSrcweir smgr.addEventListener( listener ) 166cdf0e10cSrcweir smgr.removeEventListener( listener ) 167cdf0e10cSrcweir smgr.dispose() 168cdf0e10cSrcweir self.failUnless( not listener.disposingCalled ) 169cdf0e10cSrcweir 170cdf0e10cSrcweir def testCurrentContext( self ): 171cdf0e10cSrcweir oldContext = uno.getCurrentContext() 172cdf0e10cSrcweir try: 173cdf0e10cSrcweir uno.setCurrentContext( 174cdf0e10cSrcweir unohelper.CurrentContext( oldContext,{"My42":42}) ) 175cdf0e10cSrcweir self.failUnless( 42 == uno.getCurrentContext().getValueByName( "My42" ) ) 176cdf0e10cSrcweir self.failUnless( None == uno.getCurrentContext().getValueByName( "My43" ) ) 177cdf0e10cSrcweir finally: 178cdf0e10cSrcweir uno.setCurrentContext( oldContext ) 179cdf0e10cSrcweir 180cdf0e10cSrcweir 181cdf0e10cSrcweir 182cdf0e10cSrcweirdef suite( ctx ): 183cdf0e10cSrcweir suite = unittest.TestSuite() 184cdf0e10cSrcweir suite.addTest(TestCase("testStandard",ctx)) 185cdf0e10cSrcweir suite.addTest(TestHelperCase( "testUrlHelper" )) 186cdf0e10cSrcweir suite.addTest(TestHelperCase( "testInspect" )) 187cdf0e10cSrcweir suite.addTest(TestHelperCase( "testListener" ) ) 188cdf0e10cSrcweir suite.addTest(TestHelperCase( "testCurrentContext" ) ) 189cdf0e10cSrcweir return suite 190cdf0e10cSrcweir 191