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 <unotools/syslocaleoptions.hxx>
14 #include <comphelper/processfactory.hxx>
15 #include <o3tl/char16_t2wchar_t.hxx>
17 #include <systools/win32/odbccp32.hxx>
20 using namespace ::com::sun::star
;
24 class VBATest
: public test::BootstrapFixture
27 VBATest() : BootstrapFixture(true, false) {}
28 void testMiscVBAFunctions();
29 void testMiscOLEStuff();
30 // Adds code needed to register the test suite
31 CPPUNIT_TEST_SUITE(VBATest
);
33 // Declares the method as a test to call
34 CPPUNIT_TEST(testMiscVBAFunctions
);
35 CPPUNIT_TEST(testMiscOLEStuff
);
37 // End of test suite definition
38 CPPUNIT_TEST_SUITE_END();
42 void VBATest::testMiscVBAFunctions()
44 const char* macroSource
[] = {
47 "cdec.vb", // currently CDec is implemented only on Windows
50 // datevalue test seems to depend on both locale and language
51 // settings, should try and rewrite the test to deal with that
52 // for some reason tinderboxes don't seem to complain leaving enabled
60 "stringplusdouble.vb",
122 "optional_paramters.vb",
139 "tdf147529_optional_parameters_msgbox.vb",
140 "tdf148358_non_ascii_names.vb",
152 #ifndef _WIN32 // missing 64bit Currency marshalling.
153 "win32compat.vb", // windows compatibility hooks.
155 "win32compatb.vb" // same methods, different signatures.
157 OUString sMacroPathURL
= m_directories
.getURLFromSrc(u
"/basic/qa/vba_tests/");
158 OUString sMacroUtilsURL
= m_directories
.getURLFromSrc(u
"/basic/qa/cppunit/_test_asserts.vb");
159 // Some test data expects the uk locale
160 LanguageTag
aLocale(LANGUAGE_ENGLISH_UK
);
161 SvtSysLocaleOptions aLocalOptions
;
162 aLocalOptions
.SetLocaleConfigString( aLocale
.getBcp47() );
164 for ( size_t i
=0; i
<std::size( macroSource
); ++i
)
166 OUString sMacroURL
= sMacroPathURL
167 + OUString::createFromAscii( macroSource
[ i
] );
169 MacroSnippet myMacro
;
170 myMacro
.LoadSourceFromFile(u
"TestUtil"_ustr
, sMacroUtilsURL
);
171 myMacro
.LoadSourceFromFile(u
"TestModule"_ustr
, sMacroURL
);
172 SbxVariableRef pReturn
= myMacro
.Run();
173 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn
.is());
174 fprintf(stderr
, "macro result for %s\n", macroSource
[i
]);
175 fprintf(stderr
, "macro returned:\n%s\n",
176 OUStringToOString(pReturn
->GetOUString(), RTL_TEXTENCODING_UTF8
).getStr());
177 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", u
"OK"_ustr
,
178 pReturn
->GetOUString());
182 void VBATest::testMiscOLEStuff()
184 // Not much point even trying to run except on Windows.
185 // (Without Excel doesn't really do anything anyway,
186 // see "so skip test" below.)
188 // Since some time, on a properly updated Windows 10, this works
189 // only with a 64-bit LibreOffice
191 #if defined _WIN32 && defined _ARM64_
192 // skip for windows arm64 build
193 // Avoid "this method is empty and should be removed" warning
195 #elif defined(_WIN64)
196 // test if we have the necessary runtime environment
197 // to run the OLE tests.
198 uno::Reference
< lang::XMultiServiceFactory
> xOLEFactory
;
199 uno::Reference
< uno::XComponentContext
> xContext(
200 comphelper::getProcessComponentContext() );
203 uno::Reference
<lang::XMultiComponentFactory
> xSMgr
= xContext
->getServiceManager();
204 xOLEFactory
.set( xSMgr
->createInstanceWithContext( "com.sun.star.bridge.OleObjectFactory", xContext
),
208 if( xOLEFactory
.is() )
210 uno::Reference
< uno::XInterface
> xADODB
= xOLEFactory
->createInstance( "ADODB.Connection" );
214 return; // can't do anything, skip test
216 const int nBufSize
= 1024 * 4;
217 wchar_t sBuf
[nBufSize
];
218 if (!sal::systools::odbccp32().SQLGetInstalledDrivers(sBuf
, nBufSize
))
221 const wchar_t *pODBCDriverName
= sBuf
;
223 for (; wcslen( pODBCDriverName
) != 0; pODBCDriverName
+= wcslen( pODBCDriverName
) + 1 ) {
224 if( wcscmp( pODBCDriverName
, L
"Microsoft Excel Driver (*.xls)" ) == 0 ||
225 wcscmp( pODBCDriverName
, L
"Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)" ) == 0 ) {
231 return; // can't find ODBC driver needed test, so skip test
233 const char* macroSource
[] = {
234 "ole_ObjAssignNoDflt.vb",
235 "ole_ObjAssignToNothing.vb",
238 OUString sMacroPathURL
= m_directories
.getURLFromSrc(u
"/basic/qa/vba_tests/");
240 // path to test document
241 OUString sPath
= m_directories
.getPathFromSrc(u
"/basic/qa/vba_tests/data/ADODBdata.xls");
242 sPath
= sPath
.replaceAll( "/", "\\" );
244 uno::Sequence
< uno::Any
> aArgs
247 uno::Any(OUString(o3tl::toU(pODBCDriverName
)))
250 for ( sal_uInt32 i
=0; i
<std::size( macroSource
); ++i
)
252 OUString sMacroURL
= sMacroPathURL
253 + OUString::createFromAscii( macroSource
[ i
] );
254 MacroSnippet myMacro
;
255 myMacro
.LoadSourceFromFile("TestModule", sMacroURL
);
256 SbxVariableRef pReturn
= myMacro
.Run( aArgs
);
257 CPPUNIT_ASSERT_MESSAGE("No return variable huh?", pReturn
.is());
258 fprintf(stderr
, "macro result for %s\n", macroSource
[i
]);
259 fprintf(stderr
, "macro returned:\n%s\n",
260 OUStringToOString(pReturn
->GetOUString(), RTL_TEXTENCODING_UTF8
).getStr());
261 CPPUNIT_ASSERT_EQUAL_MESSAGE("Result not as expected", OUString("OK"),
262 pReturn
->GetOUString());
265 // Avoid "this method is empty and should be removed" warning
270 // Put the test suite in the registry
271 CPPUNIT_TEST_SUITE_REGISTRATION(VBATest
);
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */