1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 #include "basictest.hxx"
10 #include <vcl/svapp.hxx>
11 #include <comphelper/processfactory.hxx>
12 using namespace ::com::sun::star
;
18 class VBATest
: public test::BootstrapFixture
22 VBATest() : BootstrapFixture(true, false) {}
24 void testMiscVBAFunctions();
25 void testMiscOLEStuff();
26 // Adds code needed to register the test suite
27 CPPUNIT_TEST_SUITE(VBATest
);
29 // Declares the method as a test to call
30 CPPUNIT_TEST(testMiscVBAFunctions
);
31 // not much point even trying to run except on windows
33 CPPUNIT_TEST(testMiscOLEStuff
);
36 // End of test suite definition
37 CPPUNIT_TEST_SUITE_END();
41 bool VBATest::hasOLEEnv()
43 // test if we have the necessary runtime environment
44 // to run the OLE tests.
45 static uno::Reference
< lang::XMultiServiceFactory
> xOLEFactory
;
46 if ( !xOLEFactory
.is() )
48 uno::Reference
< uno::XComponentContext
> xContext(
49 comphelper::getProcessComponentContext() );
52 uno::Reference
<lang::XMultiComponentFactory
> xSMgr
= xContext
->getServiceManager();
53 xOLEFactory
= uno::Reference
<lang::XMultiServiceFactory
>(
54 xSMgr
->createInstanceWithContext(
55 "com.sun.star.bridge.OleObjectFactory",
56 xContext
), uno::UNO_QUERY
);
60 if( xOLEFactory
.is() )
62 uno::Reference
< uno::XInterface
> xExcel
= xOLEFactory
->createInstance( "Excel.Application" );
63 uno::Reference
< uno::XInterface
> xADODB
= xOLEFactory
->createInstance( "ADODB.Connection" );
64 bOk
= xExcel
.is() && xADODB
.is();
69 void VBATest::testMiscVBAFunctions()
71 const char* macroSource
[] = {
73 // datevalue test seems to depend on both locale and language
74 // settings, should try and rewrite the test to deal with that
75 // for some reason tinderboxes don't seem to complain leaving enabled
85 OUString sMacroPathURL
= getURLFromSrc("/basic/qa/vba_tests/");
86 // Some test data expects the uk locale
87 AllSettings aSettings
= Application::GetSettings();
88 aSettings
.SetLanguageTag( LanguageTag( LANGUAGE_ENGLISH_UK
) );
89 Application::SetSettings( aSettings
);
90 for ( sal_uInt32 i
=0; i
<SAL_N_ELEMENTS( macroSource
); ++i
)
92 OUString
sMacroURL( sMacroPathURL
);
93 sMacroURL
+= OUString::createFromAscii( macroSource
[ i
] );
96 myMacro
.LoadSourceFromFile( sMacroURL
);
97 SbxVariableRef pReturn
= myMacro
.Run();
100 fprintf(stderr
, "macro result for %s\n", macroSource
[ i
] );
101 fprintf(stderr
, "macro returned:\n%s\n", OUStringToOString( pReturn
->GetOUString(), RTL_TEXTENCODING_UTF8
).getStr() );
103 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn
!= NULL
);
104 CPPUNIT_ASSERT_MESSAGE("Result not as expected", pReturn
->GetOUString() == "OK" );
108 void VBATest::testMiscOLEStuff()
110 bool bCanRunOleTests
= hasOLEEnv();
111 if ( !bCanRunOleTests
)
112 return; // can't do anything, skip test
114 const char* macroSource
[] = {
115 "ole_ObjAssignNoDflt.vb",
116 "ole_ObjAssignToNothing.vb",
117 "ole_dfltObjDflMethod.vb",
120 OUString sMacroPathURL
= getURLFromSrc("/basic/qa/vba_tests/");
122 uno::Sequence
< uno::Any
> aArgs(1);
123 // path to test document
124 OUString sPath
= getPathFromSrc("/basic/qa/vba_tests/data/");
125 sPath
+= "ADODBdata.xls";
126 sPath
= sPath
.replaceAll( "/", "\\" );
128 aArgs
[ 0 ] = uno::makeAny( sPath
);
130 for ( sal_uInt32 i
=0; i
<SAL_N_ELEMENTS( macroSource
); ++i
)
132 OUString
sMacroURL( sMacroPathURL
);
133 sMacroURL
+= OUString::createFromAscii( macroSource
[ i
] );
134 MacroSnippet myMacro
;
135 myMacro
.LoadSourceFromFile( sMacroURL
);
136 SbxVariableRef pReturn
= myMacro
.Run( aArgs
);
139 fprintf(stderr
, "macro result for %s\n", macroSource
[ i
] );
140 fprintf(stderr
, "macro returned:\n%s\n", OUStringToOString( pReturn
->GetOUString(), RTL_TEXTENCODING_UTF8
).getStr() );
142 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn
!= NULL
);
143 CPPUNIT_ASSERT_MESSAGE("Result not as expected", pReturn
->GetOUString() == "OK" );
147 // Put the test suite in the registry
149 // Put the test suite in the registry
150 CPPUNIT_TEST_SUITE_REGISTRATION(VBATest
);
152 CPPUNIT_PLUGIN_IMPLEMENT();
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */