1*cdf0e10cSrcweir#************************************************************************* 2*cdf0e10cSrcweir# 3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir# 5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir# 7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir# 9*cdf0e10cSrcweir# This file is part of OpenOffice.org. 10*cdf0e10cSrcweir# 11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir# only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir# 15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir# 21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir# version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir# for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir# 26*cdf0e10cSrcweir#************************************************************************* 27*cdf0e10cSrcweir"tests bridging python implementations of UNO objects" 28*cdf0e10cSrcweirimport unittest 29*cdf0e10cSrcweirimport uno 30*cdf0e10cSrcweirimport unohelper 31*cdf0e10cSrcweirimport os 32*cdf0e10cSrcweirimport sys 33*cdf0e10cSrcweir 34*cdf0e10cSrcweirfrom com.sun.star.io import XOutputStream, XInputStream, typeOfXOutputStream, typeOfXInputStream 35*cdf0e10cSrcweirfrom com.sun.star.lang import XTypeProvider, typeOfXTypeProvider, XEventListener 36*cdf0e10cSrcweirfrom com.sun.star.uno import XCurrentContext 37*cdf0e10cSrcweir 38*cdf0e10cSrcweirclass SequenceOutputStream( unohelper.Base, XOutputStream ): 39*cdf0e10cSrcweir def __init__( self ): 40*cdf0e10cSrcweir self.s = uno.ByteSequence("") 41*cdf0e10cSrcweir self.closed = 0 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir def closeOutput(self): 44*cdf0e10cSrcweir self.closed = 1 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir def writeBytes( self, seq ): 47*cdf0e10cSrcweir self.s = self.s + seq 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir def flush( self ): 50*cdf0e10cSrcweir pass 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir def getSequence( self ): 53*cdf0e10cSrcweir return self.s 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir 56*cdf0e10cSrcweirclass SequenceInputStream( XInputStream, unohelper.Base ): 57*cdf0e10cSrcweir def __init__( self, seq ): 58*cdf0e10cSrcweir self.s = seq 59*cdf0e10cSrcweir self.nIndex = 0 60*cdf0e10cSrcweir self.closed = 0 61*cdf0e10cSrcweir 62*cdf0e10cSrcweir def closeInput( self): 63*cdf0e10cSrcweir self.closed = 1 64*cdf0e10cSrcweir self.s = None 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir def skipBytes( self, nByteCount ): 67*cdf0e10cSrcweir if( nByteCount + self.nIndex > len(self.s) ): 68*cdf0e10cSrcweir nByteCount = len(self.s) - self.nIndex 69*cdf0e10cSrcweir self.nIndex += nByteCount 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir def readBytes( self, retSeq, nByteCount ): 72*cdf0e10cSrcweir nRet = 0 73*cdf0e10cSrcweir if( self.nIndex + nByteCount > len(self.s) ): 74*cdf0e10cSrcweir nRet = len(self.s) - self.nIndex 75*cdf0e10cSrcweir else: 76*cdf0e10cSrcweir nRet = nByteCount 77*cdf0e10cSrcweir retSeq = uno.ByteSequence(self.s.value[self.nIndex : self.nIndex + nRet ]) 78*cdf0e10cSrcweir self.nIndex = self.nIndex + nRet 79*cdf0e10cSrcweir return nRet, retSeq 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir def readSomeBytes( self, retSeq , nByteCount ): 82*cdf0e10cSrcweir #as we never block ! 83*cdf0e10cSrcweir return readBytes( retSeq, nByteCount ) 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir def available( self ): 86*cdf0e10cSrcweir return len( self.s ) - self.nIndex 87*cdf0e10cSrcweir 88*cdf0e10cSrcweirclass SequenceInputStream2( SequenceInputStream ): 89*cdf0e10cSrcweir def __init__( self, seq ): 90*cdf0e10cSrcweir SequenceInputStream.__init__( self, seq ) 91*cdf0e10cSrcweir 92*cdf0e10cSrcweirclass TestCase(unittest.TestCase): 93*cdf0e10cSrcweir def __init__(self,method,ctx): 94*cdf0e10cSrcweir unittest.TestCase.__init__(self,method) 95*cdf0e10cSrcweir self.ctx = ctx 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir def setUp(self): 98*cdf0e10cSrcweir self.tobj = self.ctx.ServiceManager.createInstanceWithContext( \ 99*cdf0e10cSrcweir "com.sun.star.test.bridge.CppTestObject",self.ctx) 100*cdf0e10cSrcweir self.pipe = self.ctx.ServiceManager.createInstanceWithContext( \ 101*cdf0e10cSrcweir "com.sun.star.io.Pipe" , self.ctx ) 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir def testStandard( self ): 104*cdf0e10cSrcweir dataOut = self.ctx.ServiceManager.createInstanceWithContext( \ 105*cdf0e10cSrcweir "com.sun.star.io.DataOutputStream", self.ctx ) 106*cdf0e10cSrcweir streamOut = SequenceOutputStream() 107*cdf0e10cSrcweir dataOut.setOutputStream( streamOut ) 108*cdf0e10cSrcweir dataOut.writeShort( 42 ) 109*cdf0e10cSrcweir dataOut.writeLong( 43 ) 110*cdf0e10cSrcweir dataOut.closeOutput() 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir dataInput = self.ctx.ServiceManager.createInstanceWithContext( \ 113*cdf0e10cSrcweir "com.sun.star.io.DataInputStream", self.ctx ) 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir dataInput.setInputStream( SequenceInputStream2( streamOut.getSequence() ) ) 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir self.failUnless( 42 == dataInput.readShort() ) 118*cdf0e10cSrcweir self.failUnless( 43 == dataInput.readLong() ) 119*cdf0e10cSrcweir self.failUnless( self.tobj.transportAny( streamOut ) == streamOut ) 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir 122*cdf0e10cSrcweirclass NullDevice: 123*cdf0e10cSrcweir def write( self, string ): 124*cdf0e10cSrcweir pass 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir 127*cdf0e10cSrcweirclass EventListener( unohelper.Base, XEventListener ): 128*cdf0e10cSrcweir def __init__( self ): 129*cdf0e10cSrcweir self.disposingCalled = False 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir def disposing( self , eventObject ): 132*cdf0e10cSrcweir self.disposingCalled = True 133*cdf0e10cSrcweir 134*cdf0e10cSrcweirclass TestHelperCase( unittest.TestCase ): 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir def __init__(self,method): 137*cdf0e10cSrcweir unittest.TestCase.__init__(self,method) 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir def testUrlHelper( self ): 140*cdf0e10cSrcweir systemPath = os.getcwd() 141*cdf0e10cSrcweir if systemPath.startswith( "/" ): 142*cdf0e10cSrcweir self.failUnless( "/tmp" == unohelper.fileUrlToSystemPath( "file:///tmp" ) ) 143*cdf0e10cSrcweir self.failUnless( "file:///tmp" == unohelper.systemPathToFileUrl( "/tmp" )) 144*cdf0e10cSrcweir else: 145*cdf0e10cSrcweir self.failUnless( "c:\\temp" == unohelper.fileUrlToSystemPath( "file:///c:/temp" ) ) 146*cdf0e10cSrcweir self.failUnless( "file:///c:/temp" == unohelper.systemPathToFileUrl( "c:\\temp" ) ) 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir systemPath = unohelper.systemPathToFileUrl( systemPath ) 149*cdf0e10cSrcweir self.failUnless( systemPath + "/a" == unohelper.absolutize( systemPath, "a" ) ) 150*cdf0e10cSrcweir def testInspect( self ): 151*cdf0e10cSrcweir dev = NullDevice() 152*cdf0e10cSrcweir# dev = sys.stdout 153*cdf0e10cSrcweir unohelper.inspect( uno.getComponentContext() , dev ) 154*cdf0e10cSrcweir unohelper.inspect( uno.getComponentContext().ServiceManager , dev ) 155*cdf0e10cSrcweir unohelper.inspect( uno.getTypeByName( "com.sun.star.lang.XComponent" ) , dev ) 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir def testListener( self ): 158*cdf0e10cSrcweir smgr = uno.getComponentContext().ServiceManager.createInstance( 159*cdf0e10cSrcweir "com.sun.star.lang.ServiceManager" ) 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir # check, whether listeners 162*cdf0e10cSrcweir listener = EventListener() 163*cdf0e10cSrcweir smgr.addEventListener( listener ) 164*cdf0e10cSrcweir smgr.dispose() 165*cdf0e10cSrcweir self.failUnless( listener.disposingCalled ) 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir # check, whether listeners can be removed 168*cdf0e10cSrcweir smgr = uno.getComponentContext().ServiceManager.createInstance( 169*cdf0e10cSrcweir "com.sun.star.lang.ServiceManager" ) 170*cdf0e10cSrcweir listener = EventListener() 171*cdf0e10cSrcweir smgr.addEventListener( listener ) 172*cdf0e10cSrcweir smgr.removeEventListener( listener ) 173*cdf0e10cSrcweir smgr.dispose() 174*cdf0e10cSrcweir self.failUnless( not listener.disposingCalled ) 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir def testCurrentContext( self ): 177*cdf0e10cSrcweir oldContext = uno.getCurrentContext() 178*cdf0e10cSrcweir try: 179*cdf0e10cSrcweir uno.setCurrentContext( 180*cdf0e10cSrcweir unohelper.CurrentContext( oldContext,{"My42":42}) ) 181*cdf0e10cSrcweir self.failUnless( 42 == uno.getCurrentContext().getValueByName( "My42" ) ) 182*cdf0e10cSrcweir self.failUnless( None == uno.getCurrentContext().getValueByName( "My43" ) ) 183*cdf0e10cSrcweir finally: 184*cdf0e10cSrcweir uno.setCurrentContext( oldContext ) 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir 188*cdf0e10cSrcweirdef suite( ctx ): 189*cdf0e10cSrcweir suite = unittest.TestSuite() 190*cdf0e10cSrcweir suite.addTest(TestCase("testStandard",ctx)) 191*cdf0e10cSrcweir suite.addTest(TestHelperCase( "testUrlHelper" )) 192*cdf0e10cSrcweir suite.addTest(TestHelperCase( "testInspect" )) 193*cdf0e10cSrcweir suite.addTest(TestHelperCase( "testListener" ) ) 194*cdf0e10cSrcweir suite.addTest(TestHelperCase( "testCurrentContext" ) ) 195*cdf0e10cSrcweir return suite 196*cdf0e10cSrcweir 197