update dev300-m58
[ooovba.git] / sc / source / ui / vba / testvba / testvba.cxx
blob058c874067666e72db0764916966ef1bb94b6c39
1 #include "cppuhelper/bootstrap.hxx"
3 #include <com/sun/star/beans/Property.hpp>
4 #include <com/sun/star/beans/XPropertySet.hpp>
5 #include <com/sun/star/beans/XPropertySetInfo.hpp>
6 #include <com/sun/star/container/XNameAccess.hpp>
7 #include <com/sun/star/container/XNameContainer.hpp>
8 #include <com/sun/star/frame/XComponentLoader.hpp>
9 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
10 #include <com/sun/star/sheet/XSpreadsheet.hpp>
11 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
12 #include <com/sun/star/util/XCloseable.hpp>
13 #include <com/sun/star/uno/XComponentContext.hpp>
14 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
15 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
16 #include <com/sun/star/document/XTypeDetection.hpp>
18 #include <tools/urlobj.hxx>
19 #include <osl/file.hxx>
21 #include <memory>
22 #include <iostream>
24 using namespace ::com::sun::star;
25 using namespace ::com::sun::star::sheet;
27 using ::com::sun::star::beans::Property;
28 using ::com::sun::star::beans::PropertyValue;
29 using ::com::sun::star::beans::XPropertySet;
30 using ::com::sun::star::beans::XPropertySetInfo;
31 using ::com::sun::star::container::XNameContainer;
32 using ::com::sun::star::lang::XComponent;
33 using ::com::sun::star::lang::XMultiComponentFactory;
34 using ::com::sun::star::frame::XComponentLoader;
35 using ::com::sun::star::uno::Reference;
36 using ::com::sun::star::uno::Sequence;
37 using ::com::sun::star::uno::UNO_QUERY;
38 using ::com::sun::star::uno::UNO_QUERY_THROW;
39 using ::com::sun::star::uno::XComponentContext;
40 using ::com::sun::star::uno::XInterface;
41 using ::com::sun::star::ucb::XSimpleFileAccess;
42 using ::com::sun::star::document::XTypeDetection;
43 using ::rtl::OUString;
45 using ::std::auto_ptr;
47 const OUString EXTN = rtl::OUString::createFromAscii(".xls");
49 OUString convertToURL( const OUString& rPath )
51 rtl::OUString aURL;
52 INetURLObject aObj;
53 aObj.SetURL( rPath );
54 bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
55 if ( bIsURL )
56 aURL = rPath;
57 else
59 osl::FileBase::getFileURLFromSystemPath( rPath, aURL );
60 if ( aURL.equals( rPath ) )
61 throw uno::RuntimeException( rtl::OUString::createFromAscii( "could'nt convert " ).concat( rPath ).concat( rtl::OUString::createFromAscii( " to a URL, is it a fully qualified path name? " ) ), Reference< uno::XInterface >() );
63 return aURL;
66 OUString ascii(const sal_Char* cstr)
68 return OUString::createFromAscii(cstr);
71 const sal_Char* getStr(const OUString& ou)
73 return OUStringToOString(ou, RTL_TEXTENCODING_UTF8).getStr();
77 int usage( const char* pName )
79 std::cerr << "usage: " << pName << "<path to testdocument dir> <output_directory>" << std::endl;
80 return 1;
84 class TestVBA
86 private:
87 Reference< XComponentContext > mxContext;
88 Reference< XMultiComponentFactory > mxMCF;
89 Reference< XComponentLoader > mxCompLoader;
90 Reference< XSimpleFileAccess > mxSFA;
91 rtl::OUString msOutDirPath;
92 protected:
93 public:
94 TestVBA( const Reference< XComponentContext >& _xContext,
95 const Reference< XMultiComponentFactory >& _xMCF,
96 const Reference< XComponentLoader >& _xCompLoader,
97 const rtl::OUString& _outDirPath ) : mxContext( _xContext ), mxMCF( _xMCF ),
98 mxCompLoader( _xCompLoader ), msOutDirPath( convertToURL( _outDirPath ) )
100 mxSFA.set( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ), mxContext), uno::UNO_QUERY_THROW );
103 rtl::OUString getLogLocation() throw ( beans::UnknownPropertyException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::Exception )
105 rtl::OUString sLogLocation;
106 Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
107 pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
108 sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( rtl::OUString::createFromAscii( "HelperAPI-test.log" ) );
109 return sLogLocation;
111 rtl::OUString getLogLocationWithName( OUString fileName ) throw ( beans::UnknownPropertyException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::Exception )
113 printf("%s\n", getenv("HOME") );
114 printf("file name %s\n", rtl::OUStringToOString( fileName, RTL_TEXTENCODING_UTF8 ).getStr() );
115 //rtl::OUString sLogLocation( rtl::OUString::createFromAscii( getenv("HOME") ) );
116 rtl::OUString sLogLocation;
117 Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
118 pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
119 sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( fileName.copy ( 0, fileName.lastIndexOf( EXTN ) ) + rtl::OUString::createFromAscii( ".log" ) );
120 return sLogLocation;
123 void init()
125 // blow away previous logs?
128 void proccessDocument( const rtl::OUString& sUrl )
130 if ( !mxSFA->isFolder( sUrl ) && sUrl.endsWithIgnoreAsciiCaseAsciiL( ".xls", 4 ) )
133 try
135 OSL_TRACE( "processing %s", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
136 printf( "processing %s\n", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
137 // Loading the wanted document
138 Sequence< PropertyValue > propertyValues(1);
139 propertyValues[0].Name = rtl::OUString::createFromAscii( "Hidden" );
140 propertyValues[0].Value <<= sal_False;
142 rtl::OUString sfileUrl = convertToURL( sUrl );
143 printf( "try to get xDoc %s\n", rtl::OUStringToOString( sfileUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
144 Reference< uno::XInterface > xDoc =
145 mxCompLoader->loadComponentFromURL( sfileUrl, rtl::OUString::createFromAscii( "_blank" ), 0, propertyValues);
146 printf( "got xDoc\n" );
148 OUString logFileURL = convertToURL( getLogLocation() );
151 Reference< script::provider::XScriptProviderSupplier > xSupplier( xDoc, uno::UNO_QUERY_THROW ) ;
152 if ( mxSFA->exists( logFileURL ) )
153 mxSFA->kill( logFileURL );
155 printf("try to get the ScriptProvider\n");
156 Reference< script::provider::XScriptProvider > xProv = xSupplier->getScriptProvider();
157 printf("get the ScriptProvider\n");
158 printf("try to get the Script\n");
159 Reference< script::provider::XScript > xScript;
162 xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:VBAProject.TestMacros.Main?language=Basic&location=document" ));
163 } catch ( uno::Exception& e )
167 xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:VBAProject.testMacro.Main?language=Basic&location=document" ));
168 } catch ( uno::Exception& e2 )
170 xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:VBAProject.testMain.Main?language=Basic&location=document" ));
173 OSL_TRACE("Got script for doc %s", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
174 printf("get the Script\n");
175 Sequence< uno::Any > aArgs;
176 Sequence< sal_Int16 > aOutArgsIndex;
177 Sequence< uno::Any > aOutArgs;
179 xScript->invoke(aArgs, aOutArgsIndex, aOutArgs);
181 OUString fileName = sUrl.copy ( sUrl.lastIndexOf( '/' ) );
182 OUString newLocation = msOutDirPath + fileName.copy ( 0, fileName.lastIndexOf( EXTN ) ) + rtl::OUString::createFromAscii( ".log" );
185 printf("move log file\n");
186 mxSFA->move( logFileURL, newLocation );
187 OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
188 printf("moved to new location\n");
190 catch ( uno::Exception& e )
192 logFileURL = convertToURL( getLogLocationWithName( fileName ) );
193 printf("move log file from %s\n", rtl::OUStringToOString( logFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
194 mxSFA->move( logFileURL, newLocation );
195 OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
196 printf("moved to new location\n");
200 catch ( uno::Exception& e )
202 std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
205 // interface is supported, otherwise use XComponent.dispose
206 Reference< util::XCloseable > xCloseable ( xDoc, uno::UNO_QUERY );
208 if ( xCloseable.is() )
210 printf("try to close\n");
211 // will close application. and only run a test case for 3.0
212 // maybe it is a bug. yes, it is a bug
213 // if only one frame and model, click a button which related will colse.
214 // will make a crash. It related with window listener.
215 // so, for run all test cases, it should not close the document at this moment.
216 xCloseable->close(sal_False);
217 printf("closed\n");
219 else
221 printf("try to dispose\n");
222 Reference< XComponent > xComp( xDoc, uno::UNO_QUERY_THROW );
223 // same as close.
224 xComp->dispose();
225 printf("disposed\n");
228 catch( uno::Exception& e )
230 std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
234 printf("complete processing %s\n", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
237 void traverse( const rtl::OUString& sFileDirectory )
239 rtl::OUString sFileDirectoryURL = convertToURL( sFileDirectory );
240 if ( !mxSFA->isFolder( sFileDirectoryURL) )
242 throw lang::IllegalArgumentException( rtl::OUString::createFromAscii( "not a directory: ").concat( sFileDirectoryURL ), Reference<uno::XInterface>(), 1 );
244 // Getting all files and directories in the current directory
245 Sequence<OUString> entries = mxSFA->getFolderContents( sFileDirectoryURL, sal_False );
247 // Iterating for each file and directory
248 printf( "Entries %d\n", (int)entries.getLength() );
249 for ( sal_Int32 i = 0; i < entries.getLength(); ++i )
251 proccessDocument( entries[ i ] );
256 void tryDispose( Reference< uno::XInterface > xIF, const char* sComp )
258 Reference< lang::XComponent > xComponent( xIF, uno::UNO_QUERY );
259 if ( xComponent.is() )
263 xComponent->dispose();
265 catch( uno::Exception& e )
267 std::cerr << "tryDispose caught exception " <<rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << " while disposing " << sComp << std::endl;
271 int main( int argv, char** argc )
273 if ( !( argv > 2 ) )
274 return usage( argc[0] );
278 OSL_TRACE("Attempting to bootstrap normal");
279 Reference<XComponentContext> xCC = ::cppu::bootstrap();
280 Reference<XMultiComponentFactory> xFactory = xCC->getServiceManager();
281 OSL_TRACE("got servicemanager");
282 std::cout << "got servicemanager" << std::endl;
283 Reference<XInterface> desktop = xFactory->createInstanceWithContext(
284 ascii("com.sun.star.frame.Desktop"), xCC);
285 OSL_TRACE("got desktop");
286 std::cout << "got desktop" << std::endl;
287 Reference<frame::XComponentLoader> xLoader(desktop, UNO_QUERY_THROW);
288 TestVBA* dTest = new TestVBA( xCC, xFactory, xLoader, ascii( argc[ 2 ] ) );
289 if ( argv == 4 )
291 std::cout << "before process" << std::endl;
292 dTest->proccessDocument( ascii( argc[ 3 ] ) );
293 std::cout << "after process" << std::endl;
295 else
297 dTest->traverse( ascii( argc[ 1 ] ) );
299 delete dTest;
300 // tryDispose( xLoader, "desktop" );
301 // tryDispose( xCC, "remote context" );
304 catch( uno::Exception& e )
306 std::cerr << "Caught Exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;