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/.
10 #include <sal/config.h>
11 #include <test/unoapi_test.hxx>
12 #include <unotest/macros_test.hxx>
13 #include <rtl/strbuf.hxx>
14 #include <osl/file.hxx>
16 #include <sfx2/app.hxx>
17 #include <sfx2/docfilt.hxx>
18 #include <sfx2/docfile.hxx>
19 #include <sfx2/sfxmodelfactory.hxx>
20 #include <svl/intitem.hxx>
21 #include <com/sun/star/frame/Desktop.hpp>
23 #include <basic/sbxdef.hxx>
26 #include "patattr.hxx"
27 #include "scitems.hxx"
28 #include "document.hxx"
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::uno
;
33 /* Implementation of Macros test */
35 class ScMacrosTest
: public UnoApiTest
42 CPPUNIT_TEST_SUITE(ScMacrosTest
);
44 //enable this test if you want to play with star basic macros in unit tests
45 //works but does nothing useful yet
46 CPPUNIT_TEST(testStarBasic
);
47 CPPUNIT_TEST(testVba
);
50 CPPUNIT_TEST_SUITE_END();
53 uno::Reference
<uno::XInterface
> m_xCalcComponent
;
56 void ScMacrosTest::testStarBasic()
58 const OUString
aFileNameBase("StarBasic.ods");
60 createFileURL(aFileNameBase
, aFileName
);
61 std::cout
<< "StarBasic test" << std::endl
;
62 uno::Reference
< com::sun::star::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
, "com.sun.star.sheet.SpreadsheetDocument");
64 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent
.is());
66 OUString
aURL("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document");
69 Sequence
< sal_Int16
> aOutParamIndex
;
70 Sequence
< Any
> aOutParam
;
71 Sequence
< uno::Any
> aParams
;
73 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
75 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
76 ScDocShell
* xDocSh
= static_cast<ScDocShell
*>(pFoundShell
);
77 ScDocument
* pDoc
= xDocSh
->GetDocument();
79 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
81 pDoc
->GetValue(0,0,0,aValue
);
82 std::cout
<< "returned value = " << aValue
<< std::endl
;
83 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue
, 0.00001);
87 void ScMacrosTest::testVba()
89 TestMacroInfo testInfo
[] = {
91 OUString("TestAddress."),
92 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
96 OUString("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document"),
99 OUString("MiscRangeTests."),
100 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
103 OUString("bytearraystring."),
104 OUString("vnd.sun.Star.script:VBAProject.testMacro.test?language=Basic&location=document")
107 OUString("AutoFilter."),
108 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
111 OUString("CalcFont."),
112 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
115 OUString("TestIntersection."),
116 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
119 OUString("TestUnion."),
120 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
123 OUString("range-4."),
124 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
127 OUString("Ranges-3."),
128 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
131 OUString("TestCalc_Rangetest."),
132 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
135 OUString("TestCalc_Rangetest2."),
136 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
139 OUString("Ranges-2."),
140 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
143 OUString("pagesetup."),
144 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
148 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
151 OUString("window2."),
152 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
155 OUString("PageBreaks."),
156 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
160 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
164 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
167 OUString("CheckOptionToggleValue."),
168 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
171 OUString("GeneratedEventTest."),
172 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
175 OUString("MiscControlTests."),
176 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
179 OUString("Workbooks."),
180 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
184 OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
188 OUString sTempDirURL
;
189 osl::FileBase:: getTempDirURL( sTempDirURL
);
190 osl::FileBase::getSystemPathFromFileURL( sTempDirURL
, sTempDir
);
191 sTempDir
+= OUString( SAL_PATHDELIMITER
);
192 OUString
sTestFileName("My Test WorkBook.xls");
193 Sequence
< uno::Any
> aParams
;
194 for ( sal_uInt32 i
=0; i
<SAL_N_ELEMENTS( testInfo
); ++i
)
197 createFileURL(testInfo
[i
].sFileBaseName
+ "xls", aFileName
);
198 uno::Reference
< com::sun::star::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
, "com.sun.star.sheet.SpreadsheetDocument");
199 OUString
sMsg( "Failed to load " + aFileName
);
200 CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg
, RTL_TEXTENCODING_UTF8
).getStr(), xComponent
.is() );
202 String sUrl
= testInfo
[i
].sMacroUrl
;
204 Sequence
< sal_Int16
> aOutParamIndex
;
205 Sequence
< Any
> aOutParam
;
206 bool bWorkbooksHandling
= OUString( testInfo
[i
].sFileBaseName
).equalsAscii("Workbooks.") && !sTempDir
.isEmpty() ;
208 if ( bWorkbooksHandling
)
211 aParams
[ 0 ] <<= sTempDir
;
212 aParams
[ 1 ] <<= sTestFileName
;
215 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
217 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
218 std::cout
<< "about to invoke vba test in " << OUStringToOString( aFileName
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
220 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
223 std::cout
<< "value of Ret " << OUStringToOString( aStringRes
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
224 CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes
== "OK" );
225 pFoundShell
->DoClose();
226 if ( bWorkbooksHandling
)
229 OUString sFilePath
= sTempDir
+ sTestFileName
;
230 osl::FileBase::getFileURLFromSystemPath( sFilePath
, sFileUrl
);
231 if ( !sFileUrl
.isEmpty() )
232 osl::File::remove( sFileUrl
);
237 ScMacrosTest::ScMacrosTest()
238 : UnoApiTest("/sc/qa/extras/testdocuments")
242 CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest
);
244 CPPUNIT_PLUGIN_IMPLEMENT();
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */