nss: upgrade to release 3.73
[LibreOffice.git] / sc / qa / extras / macros-test.cxx
blob557fdf5dd3d1363d36f41f6b386f2df3d74afc53
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include <sal/config.h>
11 #include <test/unoapi_test.hxx>
12 #include <osl/file.hxx>
13 #include <sal/log.hxx>
14 #include <unotools/tempfile.hxx>
15 #include <vcl/svapp.hxx>
16 #include <editeng/borderline.hxx>
18 #include <docsh.hxx>
19 #include <document.hxx>
20 #include <attrib.hxx>
21 #include <scitems.hxx>
23 #include <com/sun/star/sheet/XSpreadsheet.hpp>
25 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
26 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
28 using namespace ::com::sun::star;
29 using namespace ::com::sun::star::uno;
31 /* Implementation of Macros test */
33 class ScMacrosTest : public UnoApiTest
35 public:
36 ScMacrosTest();
37 void saveAndReload(css::uno::Reference<css::lang::XComponent>& xComponent,
38 const OUString& rFilter);
40 void testStarBasic();
41 void testVba();
42 void testMSP();
43 void testPasswordProtectedStarBasic();
44 void testTdf114427();
45 void testTdf107885();
46 void testRowColumn();
47 void testTdf131562();
48 void testPasswordProtectedUnicodeString();
49 void testPasswordProtectedArrayInUserType();
50 void testTdf107902();
51 void testTdf131296_legacy();
52 void testTdf131296_new();
53 void testTdf128218();
54 void testTdf71271();
55 void testTdf43003();
56 void testTdf138646();
57 void testTdf90278();
58 void testTdf143582();
60 CPPUNIT_TEST_SUITE(ScMacrosTest);
61 CPPUNIT_TEST(testStarBasic);
62 CPPUNIT_TEST(testMSP);
63 CPPUNIT_TEST(testVba);
64 CPPUNIT_TEST(testPasswordProtectedStarBasic);
65 CPPUNIT_TEST(testTdf114427);
66 CPPUNIT_TEST(testTdf107885);
67 CPPUNIT_TEST(testRowColumn);
68 CPPUNIT_TEST(testTdf131562);
69 CPPUNIT_TEST(testPasswordProtectedUnicodeString);
70 CPPUNIT_TEST(testPasswordProtectedArrayInUserType);
71 CPPUNIT_TEST(testTdf107902);
72 CPPUNIT_TEST(testTdf131296_legacy);
73 CPPUNIT_TEST(testTdf131296_new);
74 CPPUNIT_TEST(testTdf128218);
75 CPPUNIT_TEST(testTdf71271);
76 CPPUNIT_TEST(testTdf43003);
77 CPPUNIT_TEST(testTdf138646);
78 CPPUNIT_TEST(testTdf90278);
79 CPPUNIT_TEST(testTdf143582);
81 CPPUNIT_TEST_SUITE_END();
84 void ScMacrosTest::saveAndReload(css::uno::Reference<css::lang::XComponent>& xComponent,
85 const OUString& rFilter)
87 utl::TempFile aTempFile;
88 aTempFile.EnableKillingFile();
89 css::uno::Sequence<css::beans::PropertyValue> aArgs(1);
90 aArgs[0].Name = "FilterName";
91 aArgs[0].Value <<= rFilter;
92 css::uno::Reference<css::frame::XStorable> xStorable(xComponent, css::uno::UNO_QUERY_THROW);
93 xStorable->storeAsURL(aTempFile.GetURL(), aArgs);
94 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
95 xCloseable->close(true);
97 xComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.sheet.SpreadsheetDocument");
100 // I suppose you could say this test doesn't really belong here, OTOH
101 // we need a full document to run the test ( it related originally to an
102 // imported Excel VBA macro ) It's convenient and fast to unit test
103 // this the problem this way. Perhaps in the future there will be some sort
104 // of slowcheck tests ( requiring a full document environment in the scripting
105 // module, we could move the test there then ) - relates to fdo#67547
106 void ScMacrosTest::testMSP()
108 OUString aFileName;
109 createFileURL("MasterScriptProviderProblem.ods", aFileName);
110 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
112 CPPUNIT_ASSERT_MESSAGE("Failed to load MasterScriptProviderProblem.ods", xComponent.is());
114 Any aRet;
115 Sequence< sal_Int16 > aOutParamIndex;
116 Sequence< Any > aOutParam;
117 Sequence< uno::Any > aParams;
119 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
121 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
122 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
123 CPPUNIT_ASSERT(pDocSh != nullptr);
125 SfxObjectShell::CallXScript(
126 xComponent,
127 "vnd.sun.Star.script:Standard.Module1.TestMSP?language=Basic&location=document",
128 aParams, aRet, aOutParamIndex, aOutParam);
129 OUString sResult;
130 aRet >>= sResult;
132 SAL_INFO("sc.qa", "Result is " << sResult );
133 CPPUNIT_ASSERT_EQUAL_MESSAGE("TestMSP ( for fdo#67547) failed", OUString("OK"), sResult);
134 pDocSh->DoClose();
137 void ScMacrosTest::testPasswordProtectedStarBasic()
139 OUString aFileName;
140 createFileURL("testTypePassword.ods", aFileName);
141 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
143 CPPUNIT_ASSERT_MESSAGE("Failed to load testTypePassword.ods", xComponent.is());
145 Any aRet;
146 Sequence< sal_Int16 > aOutParamIndex;
147 Sequence< Any > aOutParam;
148 Sequence< uno::Any > aParams;
150 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
152 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
153 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
154 ScDocument& rDoc = pDocSh->GetDocument();
157 // User defined types
159 SfxObjectShell::CallXScript(
160 xComponent,
161 "vnd.sun.Star.script:Standard.Module1.LoadAndExecuteTest?language=Basic&location=document",
162 aParams, aRet, aOutParamIndex, aOutParam);
164 OUString aValue = rDoc.GetString(0,0,0);
165 CPPUNIT_ASSERT_EQUAL_MESSAGE("User defined types script did not change the value of Sheet1.A1", OUString("success"), aValue);
167 // Big Module
169 SfxObjectShell::CallXScript(
170 xComponent,
171 "vnd.sun.Star.script:MyLibrary.BigModule.bigMethod?language=Basic&location=document",
172 aParams, aRet, aOutParamIndex, aOutParam);
174 aValue = rDoc.GetString(1,0,0);
175 CPPUNIT_ASSERT_EQUAL_MESSAGE("Big module script did not change the value of Sheet1.B1", OUString("success"), aValue);
177 // far big method tdf#94617
179 SfxObjectShell::CallXScript(
180 xComponent,
181 "vnd.sun.Star.script:MyLibrary.BigModule.farBigMethod?language=Basic&location=document",
182 aParams, aRet, aOutParamIndex, aOutParam);
184 aValue = rDoc.GetString(2,0,0);
185 CPPUNIT_ASSERT_EQUAL_MESSAGE("Far Method script did not change the value of Sheet1.C1", OUString("success"), aValue);
188 pDocSh->DoClose();
191 void ScMacrosTest::testStarBasic()
193 OUString aFileName;
194 createFileURL("StarBasic.ods", aFileName);
195 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
197 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
199 Any aRet;
200 Sequence< sal_Int16 > aOutParamIndex;
201 Sequence< Any > aOutParam;
202 Sequence< uno::Any > aParams;
204 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
206 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
207 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
208 ScDocument& rDoc = pDocSh->GetDocument();
210 SfxObjectShell::CallXScript(
211 xComponent,
212 "vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document",
213 aParams, aRet, aOutParamIndex, aOutParam);
214 double aValue;
215 rDoc.GetValue(0,0,0,aValue);
216 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue, 0.00001);
217 pDocSh->DoClose();
220 void ScMacrosTest::testVba()
222 TestMacroInfo testInfo[] = {
224 OUString("TestAddress."),
225 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
228 OUString("vba."),
229 OUString("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document"),
232 OUString("MiscRangeTests."),
233 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
236 OUString("bytearraystring."),
237 OUString("vnd.sun.Star.script:VBAProject.testMacro.test?language=Basic&location=document")
240 OUString("AutoFilter."),
241 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
244 OUString("CalcFont."),
245 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
248 OUString("TestIntersection."),
249 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
252 OUString("TestUnion."),
253 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
256 OUString("range-4."),
257 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
260 OUString("Ranges-3."),
261 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
264 OUString("TestCalc_Rangetest."),
265 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
268 OUString("TestCalc_Rangetest2."),
269 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
272 OUString("Ranges-2."),
273 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
276 OUString("pagesetup."),
277 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
280 OUString("Window."),
281 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
284 OUString("window2."),
285 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
288 OUString("PageBreaks."),
289 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
292 OUString("Shapes."),
293 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
296 OUString("Ranges."),
297 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
300 OUString("CheckOptionToggleValue."),
301 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
304 OUString("GeneratedEventTest."),
305 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
308 OUString("MiscControlTests."),
309 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
312 OUString("Workbooks."),
313 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
316 OUString("Names."),
317 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
320 OUString("NamesSheetLocal."),
321 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
324 OUString("vba_endFunction."),
325 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
328 OUString("vba_findFunction."),
329 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
332 OUString sTempDir;
333 OUString sTempDirURL;
334 osl::FileBase:: getTempDirURL( sTempDirURL );
335 osl::FileBase::getSystemPathFromFileURL( sTempDirURL, sTempDir );
336 sTempDir += OUStringChar(SAL_PATHDELIMITER);
337 OUString sTestFileName("My Test WorkBook.xls");
338 Sequence< uno::Any > aParams;
339 for (const auto& rTestInfo : testInfo)
341 OUString aFileName;
342 createFileURL(rTestInfo.sFileBaseName + "xls", aFileName);
343 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
344 OUString sMsg( "Failed to load " + aFileName );
345 CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg, RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
347 // process all events such as OnLoad events etc.
348 // otherwise the tend to arrive later at a random
349 // time - while processing other StarBasic methods.
350 Application::Reschedule(true);
352 Any aRet;
353 Sequence< sal_Int16 > aOutParamIndex;
354 Sequence< Any > aOutParam;
355 bool bWorkbooksHandling = rTestInfo.sFileBaseName == "Workbooks." && !sTempDir.isEmpty() ;
357 if ( bWorkbooksHandling )
359 aParams.realloc(2);
360 aParams[ 0 ] <<= sTempDir;
361 aParams[ 1 ] <<= sTestFileName;
364 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
366 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
367 SAL_INFO("sc.qa", "about to invoke vba test in " << aFileName << " with url " << rTestInfo.sMacroUrl);
369 SfxObjectShell::CallXScript(
370 xComponent, rTestInfo.sMacroUrl, aParams, aRet, aOutParamIndex,
371 aOutParam);
372 OUString aStringRes;
373 aRet >>= aStringRes;
374 CPPUNIT_ASSERT_EQUAL_MESSAGE(
375 OUString("script reported failure in file " + rTestInfo.sFileBaseName + "xls")
376 .toUtf8()
377 .getStr(),
378 OUString("OK"), aStringRes);
379 pFoundShell->DoClose();
380 if ( bWorkbooksHandling )
382 OUString sFileUrl;
383 OUString sFilePath = sTempDir + sTestFileName;
384 osl::FileBase::getFileURLFromSystemPath( sFilePath, sFileUrl );
385 if ( !sFileUrl.isEmpty() )
386 osl::File::remove( sFileUrl );
391 void ScMacrosTest::testTdf107885()
393 OUString aFileName;
394 createFileURL("tdf107885.xlsm", aFileName);
395 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
397 CPPUNIT_ASSERT_MESSAGE("Failed to load the doc", xComponent.is());
399 Any aRet;
400 Sequence< sal_Int16 > aOutParamIndex;
401 Sequence< Any > aOutParam;
402 Sequence< uno::Any > aParams;
404 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
406 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
407 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
408 ScDocument& rDoc = pDocSh->GetDocument();
410 CPPUNIT_ASSERT(!rDoc.RowHidden(1,0));
411 CPPUNIT_ASSERT(!rDoc.RowHidden(2,0));
412 CPPUNIT_ASSERT(!rDoc.RowHidden(3,0));
413 CPPUNIT_ASSERT(!rDoc.RowHidden(4,0));
415 // Call auto filter macro using a string condition
416 SfxObjectShell::CallXScript(
417 xComponent,
418 "vnd.sun.Star.script:VBAProject.Module1.AFString?language=Basic&location=document",
419 aParams, aRet, aOutParamIndex, aOutParam);
421 //Without the fix in place, all rows in autofilter would have been hidden
422 CPPUNIT_ASSERT(rDoc.RowHidden(1,0));
423 CPPUNIT_ASSERT(!rDoc.RowHidden(2,0));
424 CPPUNIT_ASSERT(!rDoc.RowHidden(3,0));
425 CPPUNIT_ASSERT(!rDoc.RowHidden(4,0));
427 // Call auto filter macro using a numeric condition without any locale
428 SfxObjectShell::CallXScript(
429 xComponent,
430 "vnd.sun.Star.script:VBAProject.Module1.AFNumeric?language=Basic&location=document",
431 aParams, aRet, aOutParamIndex, aOutParam);
433 CPPUNIT_ASSERT(rDoc.RowHidden(1,0));
434 CPPUNIT_ASSERT(rDoc.RowHidden(2,0));
435 CPPUNIT_ASSERT(!rDoc.RowHidden(3,0));
436 CPPUNIT_ASSERT(!rDoc.RowHidden(4,0));
438 pDocSh->DoClose();
441 void ScMacrosTest::testRowColumn()
443 OUString aFileName;
444 createFileURL("StarBasic.ods", aFileName);
445 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
447 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
449 Any aRet;
450 Sequence< sal_Int16 > aOutParamIndex;
451 Sequence< Any > aOutParam;
452 Sequence< uno::Any > aParams;
454 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
456 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
457 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
458 ScDocument& rDoc = pDocSh->GetDocument();
460 SfxObjectShell::CallXScript(
461 xComponent,
462 "vnd.sun.Star.script:Standard.Module1.Macro_RowHeight?language=Basic&location=document",
463 aParams, aRet, aOutParamIndex, aOutParam);
465 sal_uInt16 nHeight = rDoc.GetRowHeight(0, 0) * HMM_PER_TWIPS;
466 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(2000), nHeight);
468 SfxObjectShell::CallXScript(
469 xComponent,
470 "vnd.sun.Star.script:Standard.Module1.Macro_ColumnWidth?language=Basic&location=document",
471 aParams, aRet, aOutParamIndex, aOutParam);
472 sal_uInt16 nWidth = rDoc.GetColWidth(0, 0) * HMM_PER_TWIPS;
473 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(4000), nWidth);
475 pDocSh->DoClose();
478 void ScMacrosTest::testTdf131562()
480 OUString aFileName;
481 createFileURL("tdf131562.xlsm", aFileName);
482 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
484 CPPUNIT_ASSERT_MESSAGE("Failed to load the doc", xComponent.is());
486 Any aRet;
487 Sequence< sal_Int16 > aOutParamIndex;
488 Sequence< Any > aOutParam;
489 Sequence< uno::Any > aParams;
491 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
493 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
494 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
495 ScDocument& rDoc = pDocSh->GetDocument();
497 CPPUNIT_ASSERT_EQUAL(OUString("1"), rDoc.GetString(ScAddress(0,2,0)));
498 CPPUNIT_ASSERT_EQUAL(OUString(""), rDoc.GetString(ScAddress(0,3,0)));
500 SfxObjectShell::CallXScript(
501 xComponent,
502 "vnd.sun.Star.script:VBAProject.Munka1.numberconcat?language=Basic&location=document",
503 aParams, aRet, aOutParamIndex, aOutParam);
505 //Without the fix in place, the macro wouldn't have concatenated 1 and " ."
506 CPPUNIT_ASSERT_EQUAL(OUString("1 ."), rDoc.GetString(ScAddress(0,2,0)));
507 CPPUNIT_ASSERT_EQUAL(OUString("1 .cat"), rDoc.GetString(ScAddress(0,3,0)));
509 pDocSh->DoClose();
512 void ScMacrosTest::testPasswordProtectedUnicodeString()
514 const OUString sCorrectString(u"English Русский 中文");
515 const OUString sMacroURL(
516 "vnd.sun.Star.script:Protected.Module1.TestUnicodeString?language=Basic&location=document");
517 const OUString sLibName("Protected");
519 OUString aFileName;
520 createFileURL("tdf57113.ods", aFileName);
521 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
522 CPPUNIT_ASSERT(xComponent);
524 // Check that loading password-protected macro image correctly loads Unicode strings
526 Any aRet;
527 Sequence<sal_Int16> aOutParamIndex;
528 Sequence<Any> aOutParam;
529 Sequence<uno::Any> aParams;
531 SfxObjectShell::CallXScript(xComponent, sMacroURL, aParams, aRet, aOutParamIndex,
532 aOutParam);
534 OUString aReturnValue;
535 aRet >>= aReturnValue;
536 CPPUNIT_ASSERT_EQUAL(sCorrectString, aReturnValue);
539 // Unlock and load the library, to regenerate the image on save
540 css::uno::Reference<css::document::XEmbeddedScripts> xES(xComponent, UNO_QUERY_THROW);
541 css::uno::Reference<css::script::XLibraryContainer> xLC(xES->getBasicLibraries(),
542 UNO_QUERY_THROW);
543 css::uno::Reference<css::script::XLibraryContainerPassword> xPasswd(xLC, UNO_QUERY_THROW);
544 CPPUNIT_ASSERT(xPasswd->isLibraryPasswordProtected(sLibName));
545 CPPUNIT_ASSERT(!xPasswd->isLibraryPasswordVerified(sLibName));
546 CPPUNIT_ASSERT(xPasswd->verifyLibraryPassword(sLibName, "password"));
547 xLC->loadLibrary(sLibName);
548 CPPUNIT_ASSERT(xLC->isLibraryLoaded(sLibName));
550 // Now check that saving stores Unicode data correctly in image's string pool
551 saveAndReload(xComponent, "calc8");
552 CPPUNIT_ASSERT(xComponent);
555 Any aRet;
556 Sequence<sal_Int16> aOutParamIndex;
557 Sequence<Any> aOutParam;
558 Sequence<uno::Any> aParams;
560 SfxObjectShell::CallXScript(xComponent, sMacroURL, aParams, aRet, aOutParamIndex,
561 aOutParam);
563 OUString aReturnValue;
564 aRet >>= aReturnValue;
565 CPPUNIT_ASSERT_EQUAL(sCorrectString, aReturnValue);
568 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
569 xCloseable->close(true);
572 void ScMacrosTest::testPasswordProtectedArrayInUserType()
574 const OUString sMacroURL(
575 "vnd.sun.Star.script:Protected.Module1.TestMyType?language=Basic&location=document");
576 const OUString sLibName("Protected");
578 OUString aFileName;
579 createFileURL("ProtectedArrayInCustomType.ods", aFileName);
580 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
581 CPPUNIT_ASSERT(xComponent);
583 // Check that loading password-protected macro image correctly loads array bounds
585 Any aRet;
586 Sequence<sal_Int16> aOutParamIndex;
587 Sequence<Any> aOutParam;
588 Sequence<uno::Any> aParams;
590 SfxObjectShell::CallXScript(xComponent, sMacroURL, aParams, aRet, aOutParamIndex,
591 aOutParam);
593 sal_Int16 nReturnValue;
594 aRet >>= nReturnValue;
595 CPPUNIT_ASSERT_EQUAL(sal_Int16(1), nReturnValue);
598 // Unlock and load the library, to regenerate the image on save
599 css::uno::Reference<css::document::XEmbeddedScripts> xES(xComponent, UNO_QUERY_THROW);
600 css::uno::Reference<css::script::XLibraryContainer> xLC(xES->getBasicLibraries(),
601 UNO_QUERY_THROW);
602 css::uno::Reference<css::script::XLibraryContainerPassword> xPasswd(xLC, UNO_QUERY_THROW);
603 CPPUNIT_ASSERT(xPasswd->isLibraryPasswordProtected(sLibName));
604 CPPUNIT_ASSERT(!xPasswd->isLibraryPasswordVerified(sLibName));
605 CPPUNIT_ASSERT(xPasswd->verifyLibraryPassword(sLibName, "password"));
606 xLC->loadLibrary(sLibName);
607 CPPUNIT_ASSERT(xLC->isLibraryLoaded(sLibName));
609 // Now check that saving stores array bounds correctly
610 saveAndReload(xComponent, "calc8");
611 CPPUNIT_ASSERT(xComponent);
614 Any aRet;
615 Sequence<sal_Int16> aOutParamIndex;
616 Sequence<Any> aOutParam;
617 Sequence<uno::Any> aParams;
619 SfxObjectShell::CallXScript(xComponent, sMacroURL, aParams, aRet, aOutParamIndex,
620 aOutParam);
622 sal_Int16 nReturnValue;
623 aRet >>= nReturnValue;
624 CPPUNIT_ASSERT_EQUAL(sal_Int16(1), nReturnValue);
627 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
628 xCloseable->close(true);
631 void ScMacrosTest::testTdf114427()
633 OUString aFileName;
634 createFileURL("tdf114427.ods", aFileName);
635 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
637 CPPUNIT_ASSERT_MESSAGE("Failed to load the doc", xComponent.is());
639 Any aRet;
640 Sequence< sal_Int16 > aOutParamIndex;
641 Sequence< Any > aOutParam;
642 Sequence< uno::Any > aParams;
644 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
646 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
647 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
649 uno::Reference< frame::XModel > xModel = pDocSh->GetModel();
650 uno::Reference< sheet::XSpreadsheetDocument > xDoc(xModel, UNO_QUERY_THROW);
651 uno::Reference< container::XIndexAccess > xIA(xDoc->getSheets(), UNO_QUERY_THROW);
652 uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupplier( xIA->getByIndex(0), UNO_QUERY_THROW);
653 uno::Reference< container::XIndexAccess > xDraws(xDrawPageSupplier->getDrawPage(), UNO_QUERY_THROW);
654 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), xDraws->getCount());
656 // Without the fix in place, it would have crashed here
657 SfxObjectShell::CallXScript(
658 xComponent,
659 "vnd.sun.Star.script:Standard.Module1.DeletingFrame?language=Basic&location=document",
660 aParams, aRet, aOutParamIndex, aOutParam);
662 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), xDraws->getCount());
664 pDocSh->DoClose();
667 void ScMacrosTest::testTdf107902()
669 OUString aFileName;
670 createFileURL("tdf107902.xlsm", aFileName);
671 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
673 CPPUNIT_ASSERT_MESSAGE("Failed to load the doc", xComponent.is());
675 Any aRet;
676 Sequence< sal_Int16 > aOutParamIndex;
677 Sequence< Any > aOutParam;
678 Sequence< uno::Any > aParams;
680 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
682 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
683 ScDocShell* pDocSh = static_cast<ScDocShell*>(pFoundShell);
684 ScDocument& rDoc = pDocSh->GetDocument();
686 //Without the fix in place, it would have failed with 'Unexpected dialog: Error: BASIC runtime error.'
687 SfxObjectShell::CallXScript(
688 xComponent,
689 "vnd.sun.Star.script:VBAProject.Module1.AF?language=Basic&location=document",
690 aParams, aRet, aOutParamIndex, aOutParam);
692 //Check the autofilter was created
693 const ScPatternAttr* pPattern = rDoc.GetPattern(0, 0, 0);
694 CPPUNIT_ASSERT(pPattern);
696 const ScMergeFlagAttr& rAttr = pPattern->GetItem(ATTR_MERGE_FLAG);
697 CPPUNIT_ASSERT_MESSAGE("Autofilter was not created", rAttr.HasAutoFilter());
699 //Check the last row is hidden
700 CPPUNIT_ASSERT(!rDoc.RowHidden(0,0));
701 CPPUNIT_ASSERT(!rDoc.RowHidden(1,0));
702 CPPUNIT_ASSERT(!rDoc.RowHidden(2,0));
703 CPPUNIT_ASSERT(!rDoc.RowHidden(3,0));
704 CPPUNIT_ASSERT(rDoc.RowHidden(4,0));
706 pDocSh->DoClose();
709 void ScMacrosTest::testTdf131296_legacy()
711 // For legacy password-protected library images, we must correctly get the constants' values,
712 // and also - for Integer - the type.
713 const std::vector<std::pair<OUString, OUString>> aTests({
714 { "TestIntConst", "Integer: 123" },
715 { "TestLongConst", "Double: 123" },
716 { "TestSingleConst", "Double: 123" },
717 { "TestDoubleConst", "Double: 123" },
720 OUString aFileName;
721 createFileURL("tdf131296_legacy.ods", aFileName);
722 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
723 CPPUNIT_ASSERT(xComponent);
726 Any aRet;
727 Sequence<sal_Int16> aOutParamIndex;
728 Sequence<Any> aOutParam;
729 Sequence<uno::Any> aParams;
731 for (auto& [sTestName, sExpected] : aTests)
733 SfxObjectShell::CallXScript(xComponent,
734 "vnd.sun.Star.script:Protected.Module1." + sTestName
735 + "?language=Basic&location=document",
736 aParams, aRet, aOutParamIndex, aOutParam);
738 OUString aReturnValue;
739 aRet >>= aReturnValue;
740 CPPUNIT_ASSERT_EQUAL_MESSAGE(sTestName.toUtf8().getStr(), sExpected, aReturnValue);
744 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
745 xCloseable->close(true);
748 void ScMacrosTest::testTdf131296_new()
750 // For new password-protected library images, we must correctly get both the constants' values
751 // and their types.
752 const std::vector<std::pair<OUString, OUString>> aTests({
753 { "TestIntConst", "Integer: 123" },
754 { "TestLongConst", "Long: 123" },
755 { "TestSingleConst", "Single: 123" },
756 { "TestDoubleConst", "Double: 123" },
757 { "TestCurrencyConst", "Currency: 123.0000" },
760 OUString aFileName;
761 createFileURL("tdf131296_new.ods", aFileName);
762 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
763 CPPUNIT_ASSERT(xComponent);
766 Any aRet;
767 Sequence<sal_Int16> aOutParamIndex;
768 Sequence<Any> aOutParam;
769 Sequence<uno::Any> aParams;
771 for (auto& [sTestName, sExpected] : aTests)
773 SfxObjectShell::CallXScript(xComponent,
774 "vnd.sun.Star.script:Protected.Module1." + sTestName
775 + "?language=Basic&location=document",
776 aParams, aRet, aOutParamIndex, aOutParam);
778 OUString aReturnValue;
779 aRet >>= aReturnValue;
780 CPPUNIT_ASSERT_EQUAL_MESSAGE(sTestName.toUtf8().getStr(), sExpected, aReturnValue);
784 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
785 xCloseable->close(true);
788 void ScMacrosTest::testTdf128218()
790 OUString aFileName;
791 createFileURL("tdf128218.ods", aFileName);
792 uno::Reference< css::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
794 CPPUNIT_ASSERT_MESSAGE("Failed to load the doc", xComponent.is());
796 Any aRet;
797 Sequence< sal_Int16 > aOutParamIndex;
798 Sequence< Any > aOutParam;
799 Sequence< uno::Any > aParams;
801 SfxObjectShell::CallXScript(
802 xComponent,
803 "vnd.sun.Star.script:Standard.Module1.TestRAND?language=Basic&location=document",
804 aParams, aRet, aOutParamIndex, aOutParam);
806 OUString aReturnValue;
807 aRet >>= aReturnValue;
809 // Without the fix in place, this test would have failed with
810 // - Expected: Double
811 // - Actual : Object()
813 CPPUNIT_ASSERT_EQUAL(OUString("Double"), aReturnValue);
815 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
816 xCloseable->close(true);
819 void ScMacrosTest::testTdf71271()
821 uno::Reference<lang::XComponent> xComponent = loadFromDesktop("private:factory/scalc");
822 CPPUNIT_ASSERT(xComponent);
825 uno::Reference<sheet::XSpreadsheetDocument> xDoc(xComponent, uno::UNO_QUERY_THROW);
826 uno::Reference<container::XIndexAccess> xIndex(xDoc->getSheets(), uno::UNO_QUERY_THROW);
827 uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW);
828 uno::Reference<beans::XPropertySet> xProps(xSheet, uno::UNO_QUERY_THROW);
829 xProps->setPropertyValue("CodeName", uno::Any(OUString("NewCodeName")));
832 saveAndReload(xComponent, "");
833 CPPUNIT_ASSERT(xComponent);
836 uno::Reference<sheet::XSpreadsheetDocument> xDoc(xComponent, uno::UNO_QUERY_THROW);
837 uno::Reference<container::XIndexAccess> xIndex(xDoc->getSheets(), uno::UNO_QUERY_THROW);
838 uno::Reference<sheet::XSpreadsheet> xSheet(xIndex->getByIndex(0), uno::UNO_QUERY_THROW);
839 OUString sCodeName;
840 uno::Reference<beans::XPropertySet> xProps(xSheet, uno::UNO_QUERY_THROW);
841 // Without the fix in place the codename would not have been saved
842 xProps->getPropertyValue("CodeName") >>= sCodeName;
843 CPPUNIT_ASSERT_EQUAL(OUString("NewCodeName"), sCodeName);
846 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
847 xCloseable->close(true);
850 void ScMacrosTest::testTdf43003()
852 OUString aFileName;
853 createFileURL("tdf43003.ods", aFileName);
854 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
855 CPPUNIT_ASSERT(xComponent);
857 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
858 CPPUNIT_ASSERT(pFoundShell);
860 ScDocShellRef xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
861 CPPUNIT_ASSERT(xDocSh);
863 ScDocument& rDoc = xDocSh->GetDocument();
865 // Without the fix in place, the values of the specified cells won't be changed
866 rDoc.SetValue(ScAddress(0, 0, 0), 2);
867 CPPUNIT_ASSERT_EQUAL(3.0, rDoc.GetValue(ScAddress(1, 0, 0)));
868 CPPUNIT_ASSERT_EQUAL(4.0, rDoc.GetValue(ScAddress(2, 0, 0)));
870 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
871 xCloseable->close(true);
874 void ScMacrosTest::testTdf143582()
876 OUString aFileName;
877 createFileURL(u"tdf143582.ods", aFileName);
878 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
880 css::uno::Any aRet;
881 css::uno::Sequence<css::uno::Any> aParams;
882 css::uno::Sequence<css::uno::Any> aOutParam;
883 css::uno::Sequence<sal_Int16> aOutParamIndex;
885 SfxObjectShell::CallXScript(
886 xComponent,
887 "vnd.sun.Star.script:Standard.Module1.TestScriptInvoke?language=Basic&location=document",
888 aParams, aRet, aOutParamIndex, aOutParam);
890 OUString aReturnValue;
891 aRet >>= aReturnValue;
893 // Without the fix in place, this test would have failed with
894 // - Expected: Test
895 // - Actual : TeTest
896 CPPUNIT_ASSERT_EQUAL(OUString("Test"), aReturnValue);
898 css::uno::Reference<css::util::XCloseable> xCloseable(xComponent, css::uno::UNO_QUERY_THROW);
899 xCloseable->close(true);
902 void ScMacrosTest::testTdf138646()
904 OUString aFileName;
905 createFileURL(u"tdf138646.ods", aFileName);
906 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
907 CPPUNIT_ASSERT_MESSAGE("Failed to load the doc", xComponent.is());
909 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
910 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
912 ScDocShell* pDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
913 CPPUNIT_ASSERT(pDocSh);
915 // Without the fix in place, changing the grammar from GRAM_NATIVE to either GRAM_NATIVE_XL_A1
916 // or GRAM_NATIVE_XL_R1C1 would cause a Basic exception/error in the following script.
917 pDocSh->GetDocument().SetGrammar(formula::FormulaGrammar::Grammar::GRAM_NATIVE_XL_R1C1);
919 const std::vector<std::pair<OUString, OUString>> aTests({
920 { "GlobalNamedCell", "GlobalNamedCell" },
921 { "GlobalNamedCellSheet", "GlobalNamedCell" },
922 { "LocalNamedCell", "LocalNamedCell" },
923 { "LocalNamedCellAccessError", "Exception" }
927 Any aRet;
928 Sequence<sal_Int16> aOutParamIndex;
929 Sequence<Any> aOutParam;
930 Sequence<uno::Any> aParams;
932 for (auto& [sTestName, sExpected] : aTests)
934 SfxObjectShell::CallXScript(xComponent,
935 "vnd.sun.Star.script:Standard.Module1." + sTestName
936 + "?language=Basic&location=document",
937 aParams, aRet, aOutParamIndex, aOutParam);
939 OUString aReturnValue;
940 aRet >>= aReturnValue;
941 CPPUNIT_ASSERT_EQUAL_MESSAGE(sTestName.toUtf8().getStr(), sExpected, aReturnValue);
945 pDocSh->DoClose();
948 void ScMacrosTest::testTdf90278()
950 OUString aFileName;
951 createFileURL(u"tdf90278.xls", aFileName);
952 auto xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
953 CPPUNIT_ASSERT(xComponent);
955 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
956 CPPUNIT_ASSERT(pFoundShell);
958 ScDocShellRef xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
959 CPPUNIT_ASSERT(xDocSh);
961 Any aRet;
962 Sequence<sal_Int16> aOutParamIndex;
963 Sequence<Any> aOutParam;
964 Sequence<uno::Any> aParams;
966 // Without the fix in place, changing the border weight
967 // would cause a Basic exception/error in the following script.
968 SfxObjectShell::CallXScript(
969 xComponent,
970 "vnd.sun.Star.script:VBAProject.Module1.BorderWeight?language=Basic&location=document",
971 aParams, aRet, aOutParamIndex, aOutParam);
973 // Check the border weight of the corresponding cell in the test document
974 sal_Int32 aReturnValue;
975 aRet >>= aReturnValue;
976 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aReturnValue);
978 xDocSh->DoClose();
981 ScMacrosTest::ScMacrosTest()
982 : UnoApiTest("/sc/qa/extras/testdocuments")
986 CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest);
988 CPPUNIT_PLUGIN_IMPLEMENT();
990 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */