/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
CPPUNIT_TEST_FIXTURE(VBAMacroTest, testMultiDocumentCopyAndPaste)
{ // Creates a new workbook (document) and copy-pastes values // between the documents.
CPPUNIT_TEST_FIXTURE(VBAMacroTest, testPrintArea)
{ // Sets the print area to A1:B5 // ActiveSheet.PageSetup.PrintArea = "$A$1:$B$5"
loadFromFile(u"VariousTestMacros.xlsm");
CPPUNIT_TEST_FIXTURE(VBAMacroTest, testVba)
{ // FIXME: the DPI check should be removed when either (1) the test is fixed to work with // non-default DPI; or (2) unit tests on Windows are made to use svp VCL plugin. if (!IsDefaultDPI()) return;
TestMacroInfo testInfo[] = {
{ u"TestAddress.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{
u"vba.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document"_ustr,
},
{ u"MiscRangeTests.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"bytearraystring.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacro.test?language=Basic&location=document"_ustr },
{ u"AutoFilter.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"CalcFont.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"TestIntersection.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"TestUnion.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"range-4.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr }, // FIXME: sometimes it fails on Windows with // Failed: : Test change event for Range.Clear set: // Failed: : Test change event for Range.ClearContents set: // Failed: : Test change event for Range.Replace: // Failed: : Test change event for Range.FillRight: // Tests passed: 4 // Tests failed: 4 #if !defined(_WIN32)
{ u"Ranges-3.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr }, #endif
{ u"TestCalc_Rangetest.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"TestCalc_Rangetest2.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"Ranges-2.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"pagesetup.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"Window.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"window2.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"PageBreaks.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"Shapes.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"Ranges.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"CheckOptionToggleValue.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"GeneratedEventTest.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"MiscControlTests.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"Workbooks.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"Names.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"NamesSheetLocal.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"vba_endFunction.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"vba_findFunction.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"_ustr },
{ u"BGR-RGBTest.xls"_ustr,
u"vnd.sun.Star.script:VBAProject.Module1.test?language=Basic&location=document"_ustr }
};
OUString sTempDir;
OUString sTempDirURL;
osl::FileBase::getTempDirURL(sTempDirURL);
osl::FileBase::getSystemPathFromFileURL(sTempDirURL, sTempDir);
sTempDir += OUStringChar(SAL_PATHDELIMITER);
OUString sTestFileName(u"My Test WorkBook.xls"_ustr);
uno::Sequence<uno::Any> aParams; for (constauto& rTestInfo : testInfo)
{
OUString aFileName = loadFromFile(rTestInfo.sFileBaseName);
// process all events such as OnLoad events etc. otherwise they tend // to arrive later at a random time - while processing other StarBasic // methods.
Scheduler::ProcessEventsToIdle();
// Without the fix in place, this call would have crashed in debug builds with failed assertion
executeMacro(
u"vnd.sun.Star.script:TestLibrary.TestModule.TestTdf149579?language=Basic&location=" "document"_ustr); // Without the fix in place, this test would have failed with // - Expected: 1 // - Actual : 5
CPPUNIT_ASSERT_EQUAL(1.0, rDoc.GetValue(ScAddress(0, 0, 0)));
CPPUNIT_ASSERT_EQUAL(5.0, rDoc.GetValue(ScAddress(0, 1, 0)));
CPPUNIT_ASSERT_EQUAL(10.0, rDoc.GetValue(ScAddress(0, 2, 0)));
}
// Without the fix in place, this call would have crashed in debug builds with failed assertion
executeMacro(
u"vnd.sun.Star.script:TestLibrary.TestModule.TestRangeSort?language=Basic&location=" "document"_ustr);
// Without the fix in place, this test would have failed in non-debug builds with // - Expected: 2 // - Actual : 0.5
CPPUNIT_ASSERT_EQUAL(2.0, rDoc.GetValue(ScAddress(0, 0, 0)));
CPPUNIT_ASSERT_EQUAL(1.0, rDoc.GetValue(ScAddress(0, 1, 0)));
CPPUNIT_ASSERT_EQUAL(0.5, rDoc.GetValue(ScAddress(0, 2, 0)));
}
// Call auto filter macro using a string condition
executeMacro(
u"vnd.sun.Star.script:VBAProject.Module1.AFString?language=Basic&location=document"_ustr);
//Without the fix in place, all rows in autofilter would have been hidden
CPPUNIT_ASSERT(rDoc.RowHidden(1, 0));
CPPUNIT_ASSERT(!rDoc.RowHidden(2, 0));
CPPUNIT_ASSERT(!rDoc.RowHidden(3, 0));
CPPUNIT_ASSERT(!rDoc.RowHidden(4, 0));
// Call auto filter macro using a numeric condition without any locale
executeMacro(
u"vnd.sun.Star.script:VBAProject.Module1.AFNumeric?language=Basic&location=document"_ustr);
//Without the fix in place, it would have failed with 'Unexpected dialog: Error: BASIC runtime error.'
executeMacro(u"vnd.sun.Star.script:VBAProject.Modul1.Test_NumberFormat_DateTime?language=Basic&" "location=document"_ustr);
//Without the fix in place, it would have failed with 'Unexpected dialog: Error: BASIC runtime error.'
executeMacro(
u"vnd.sun.Star.script:VBAProject.Module1.AF?language=Basic&location=document"_ustr);
//Check the autofilter was created const ScPatternAttr* pPattern = rDoc.GetPattern(0, 0, 0);
CPPUNIT_ASSERT(pPattern);
const ScMergeFlagAttr& rAttr = pPattern->GetItem(ATTR_MERGE_FLAG);
CPPUNIT_ASSERT_MESSAGE("Autofilter was not created", rAttr.HasAutoFilter());
//Check the last row is hidden
CPPUNIT_ASSERT(!rDoc.RowHidden(0, 0));
CPPUNIT_ASSERT(!rDoc.RowHidden(1, 0));
CPPUNIT_ASSERT(!rDoc.RowHidden(2, 0));
CPPUNIT_ASSERT(!rDoc.RowHidden(3, 0));
CPPUNIT_ASSERT(rDoc.RowHidden(4, 0));
}
// Without the fix in place, changing the border weight // would cause a Basic exception/error in the following script.
uno::Any aRet = executeMacro(
u"vnd.sun.Star.script:VBAProject.Module1.BorderWeight?language=Basic&location=document"_ustr);
// Check the border weight of the corresponding cell in the test document
sal_Int32 aReturnValue;
aRet >>= aReturnValue;
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aReturnValue);
}
for (sal_Int32 i = 0; i < 5; ++i)
{ // Without the fix in place, this test would have crashed // also check the test doesn't crash when the macro is executed a few times in a row
executeMacro(u"vnd.sun.Star.script:VBAProject.Module1.SetColumnWidth?language=Basic&" "location=document"_ustr);
}
// Save a copy of the file to get its URL
uno::Reference<frame::XStorable> xDocStorable(mxComponent, uno::UNO_QUERY);
utl::TempFileNamed aTempFile(u"testWindowsActivate", true, u".ods");
aTempFile.EnableKillingFile();
uno::Sequence<beans::PropertyValue> descSaveAs(
comphelper::InitPropertySequence({ { "FilterName", uno::Any(u"calc8"_ustr) } }));
xDocStorable->storeAsURL(aTempFile.GetURL(), descSaveAs);
// Insert initial library
css::uno::Reference<css::document::XEmbeddedScripts> xDocScr(mxComponent, uno::UNO_QUERY_THROW); auto xLibs = xDocScr->getBasicLibraries(); auto xLibrary = xLibs->createLibrary(u"TestLibrary"_ustr);
xLibrary->insertByName(
u"TestModule"_ustr,
uno::Any(u"Option VBASupport 1\n" "Function TestWindowsActivate\n" " dirName = Workbooks(1).Path\n" " workbookName = Workbooks(1).Name\n" " fileName = dirName + Application.PathSeparator + workbookName\n" " Workbooks.Open Filename := fileName\n" " On Error Goto handler\n" // activate window using its URL " Windows(fileName).Activate\n" // activate window using its caption name " Windows(workbookName).Activate\n" // activate window using a newly generated window caption " newCaption = \"New Window Caption\"\n" " Windows(fileName).Caption = newCaption\n" " Windows(newCaption).Activate\n" " TestWindowsActivate = 0\n" " Exit Function\n" "handler:\n" " TestWindowsActivate = 1\n" "End Function\n"_ustr));
ErrCode result
= SfxObjectShell::CallXScript(mxComponent,
u"vnd.sun.Star.script:TestLibrary.TestModule." "TestWindowsActivate?language=Basic&location=document"_ustr,
{}, aRet, aOutParamIndex, aOutParam);
// Without the fix in place, the windows could not be activated in the macro
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, result);
sal_Int16 nReturnValue;
aRet >>= nReturnValue;
CPPUNIT_ASSERT_EQUAL(sal_Int16(0), nReturnValue);
}
// Save a copy of the file to get its URL
uno::Reference<frame::XStorable> xDocStorable(mxComponent, uno::UNO_QUERY);
utl::TempFileNamed aTempFile(u"testVBA_PDF_Export", true, u".ods");
aTempFile.EnableKillingFile();
uno::Sequence<beans::PropertyValue> descSaveAs(
comphelper::InitPropertySequence({ { "FilterName", uno::Any(u"calc8"_ustr) } }));
xDocStorable->storeAsURL(aTempFile.GetURL(), descSaveAs);
// Parse the export result.
vcl::filter::PDFDocument aDocument;
SvFileStream aStream(aTempPdfFile.GetURL(), StreamMode::READ);
CPPUNIT_ASSERT_MESSAGE("Failed to get the pdf document", aDocument.Read(aStream));
}
// tdf#153724: without the fix, this would fail with // assertion failed // - Expression: false // - Unexpected dialog: Error: BASIC runtime error. // '13' // Data type mismatch.
executeMacro(u"vnd.sun.Star.script:Standard.Module1.TestForEachInSelection?" "language=Basic&location=document"_ustr);
auto ret
= dispatchCommand(mxComponent, u"macro://./Standard.Module1.NonAsciiName_αβγ"_ustr, {});
css::frame::DispatchResultEvent retEvent;
CPPUNIT_ASSERT(ret >>= retEvent); // tdf#153752: without the fix, this would fail with // equality assertion failed // - Expected: 1 // - Actual : 0
CPPUNIT_ASSERT_EQUAL(css::frame::DispatchResultState::SUCCESS, retEvent.State);
css::uno::Reference<css::document::XEmbeddedScripts> xDocScr(mxComponent, uno::UNO_QUERY_THROW); auto xLibs = xDocScr->getBasicLibraries(); auto xLibrary = xLibs->createLibrary(u"TestLibrary"_ustr);
// Execute a macro that assigns a Date variable to a cell value
OUString sMacro = uR"vba(
Option VBASupport 1
Sub TestSetDate
Dim d As Date
d = #2025-07-04# ' numeric value 45842
Cells(1, 1).Value = d
End Sub
)vba"_ustr;
xLibrary->insertByName(u"TestModule"_ustr, uno::Any(sMacro));
SfxObjectShell* pObjectShell = SfxObjectShell::GetShellFromComponent(mxComponent);
CPPUNIT_ASSERT(pObjectShell);
ScDocument& rDoc = static_cast<ScDocShell*>(pObjectShell)->GetDocument(); // Without a fix, this was 0, because oleautomation::Date wasn't handled
CPPUNIT_ASSERT_EQUAL(45842.0, rDoc.GetValue(0, 0, 0));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.