Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <sal/config.h>
11 : #include <test/unoapi_test.hxx>
12 : #include <rtl/strbuf.hxx>
13 : #include <osl/file.hxx>
14 :
15 : #include <sfx2/app.hxx>
16 : #include <sfx2/docfilt.hxx>
17 : #include <sfx2/docfile.hxx>
18 : #include <sfx2/sfxmodelfactory.hxx>
19 : #include <svl/intitem.hxx>
20 : #include <com/sun/star/frame/Desktop.hpp>
21 :
22 : #include <basic/sbxdef.hxx>
23 :
24 : #include "docsh.hxx"
25 : #include "patattr.hxx"
26 : #include "scitems.hxx"
27 : #include "document.hxx"
28 :
29 : using namespace ::com::sun::star;
30 : using namespace ::com::sun::star::uno;
31 :
32 : /* Implementation of Macros test */
33 :
34 6 : class ScMacrosTest : public UnoApiTest
35 : {
36 : public:
37 : ScMacrosTest();
38 :
39 : void testStarBasic();
40 : void testVba();
41 : void testMSP();
42 2 : CPPUNIT_TEST_SUITE(ScMacrosTest);
43 : #if !defined(MACOSX)
44 : //enable this test if you want to play with star basic macros in unit tests
45 : //works but does nothing useful yet
46 1 : CPPUNIT_TEST(testStarBasic);
47 1 : CPPUNIT_TEST(testMSP);
48 1 : CPPUNIT_TEST(testVba);
49 : #endif
50 :
51 2 : CPPUNIT_TEST_SUITE_END();
52 :
53 : private:
54 : uno::Reference<uno::XInterface> m_xCalcComponent;
55 : };
56 :
57 : // I suppose you could say this test doesn't really belong here, OTOH
58 : // we need a full document to run the test ( it related originally to an
59 : // imported Excel VBA macro ) It's convenient and fast to unit test
60 : // this the problem this way. Perhaps in the future there will be some sort
61 : // of slowcheck tests ( requiring a full document environment in the scripting
62 : // module, we could move the test there then ) - relates to fdo#67547
63 1 : void ScMacrosTest::testMSP()
64 : {
65 1 : const OUString aFileNameBase("MasterScriptProviderProblem.ods");
66 2 : OUString aFileName;
67 1 : createFileURL(aFileNameBase, aFileName);
68 1 : std::cout << "MasterScriptProviderProblem (fdo#67547) test" << std::endl;
69 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
70 :
71 1 : CPPUNIT_ASSERT_MESSAGE("Failed to load MasterScriptProviderProblem.ods", xComponent.is());
72 :
73 2 : Any aRet;
74 2 : Sequence< sal_Int16 > aOutParamIndex;
75 2 : Sequence< Any > aOutParam;
76 2 : Sequence< uno::Any > aParams;
77 :
78 1 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
79 :
80 1 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
81 1 : ScDocShell* xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
82 1 : CPPUNIT_ASSERT(xDocSh != NULL);
83 :
84 : pFoundShell->CallXScript(
85 : xComponent,
86 : "vnd.sun.Star.script:Standard.Module1.TestMSP?language=Basic&location=document",
87 1 : aParams, aRet, aOutParamIndex, aOutParam);
88 2 : OUString sResult;
89 1 : aRet >>= sResult;
90 :
91 1 : std::cout << "Result is " << sResult << std::endl;
92 1 : CPPUNIT_ASSERT_MESSAGE("TestMSP ( for fdo#67547) failed", sResult == "OK" );
93 2 : xDocSh->DoClose();
94 1 : }
95 :
96 1 : void ScMacrosTest::testStarBasic()
97 : {
98 1 : const OUString aFileNameBase("StarBasic.ods");
99 2 : OUString aFileName;
100 1 : createFileURL(aFileNameBase, aFileName);
101 1 : std::cout << "StarBasic test" << std::endl;
102 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
103 :
104 1 : CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
105 :
106 2 : Any aRet;
107 2 : Sequence< sal_Int16 > aOutParamIndex;
108 2 : Sequence< Any > aOutParam;
109 2 : Sequence< uno::Any > aParams;
110 :
111 1 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
112 :
113 1 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
114 1 : ScDocShell* xDocSh = static_cast<ScDocShell*>(pFoundShell);
115 1 : ScDocument* pDoc = xDocSh->GetDocument();
116 :
117 : pFoundShell->CallXScript(
118 : xComponent,
119 : "vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document",
120 1 : aParams, aRet, aOutParamIndex, aOutParam);
121 : double aValue;
122 1 : pDoc->GetValue(0,0,0,aValue);
123 1 : std::cout << "returned value = " << aValue << std::endl;
124 1 : CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue, 0.00001);
125 2 : xDocSh->DoClose();
126 1 : }
127 :
128 1 : void ScMacrosTest::testVba()
129 : {
130 : TestMacroInfo testInfo[] = {
131 : {
132 : OUString("TestAddress."),
133 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
134 : },
135 : {
136 : OUString("vba."),
137 : OUString("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document"),
138 : },
139 : {
140 : OUString("MiscRangeTests."),
141 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
142 : },
143 : {
144 : OUString("bytearraystring."),
145 : OUString("vnd.sun.Star.script:VBAProject.testMacro.test?language=Basic&location=document")
146 : },
147 : {
148 : OUString("AutoFilter."),
149 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
150 : },
151 : {
152 : OUString("CalcFont."),
153 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
154 : },
155 : {
156 : OUString("TestIntersection."),
157 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
158 : },
159 : {
160 : OUString("TestUnion."),
161 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
162 : },
163 : {
164 : OUString("range-4."),
165 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
166 : },
167 : {
168 : OUString("Ranges-3."),
169 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
170 : },
171 : {
172 : OUString("TestCalc_Rangetest."),
173 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
174 : },
175 : {
176 : OUString("TestCalc_Rangetest2."),
177 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
178 : },
179 : {
180 : OUString("Ranges-2."),
181 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
182 : },
183 : {
184 : OUString("pagesetup."),
185 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
186 : },
187 : {
188 : OUString("Window."),
189 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
190 : },
191 : {
192 : OUString("window2."),
193 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
194 : },
195 : {
196 : OUString("PageBreaks."),
197 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
198 : },
199 : {
200 : OUString("Shapes."),
201 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
202 : },
203 : {
204 : OUString("Ranges."),
205 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
206 : },
207 : {
208 : OUString("CheckOptionToggleValue."),
209 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
210 : },
211 : {
212 : OUString("GeneratedEventTest."),
213 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
214 : },
215 : {
216 : OUString("MiscControlTests."),
217 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
218 : },
219 : {
220 : OUString("Workbooks."),
221 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
222 : },
223 : {
224 : OUString("Names."),
225 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
226 : },
227 25 : };
228 2 : OUString sTempDir;
229 2 : OUString sTempDirURL;
230 1 : osl::FileBase:: getTempDirURL( sTempDirURL );
231 1 : osl::FileBase::getSystemPathFromFileURL( sTempDirURL, sTempDir );
232 1 : sTempDir += OUString( SAL_PATHDELIMITER );
233 2 : OUString sTestFileName("My Test WorkBook.xls");
234 2 : Sequence< uno::Any > aParams;
235 25 : for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
236 : {
237 24 : OUString aFileName;
238 24 : createFileURL(testInfo[i].sFileBaseName + "xls", aFileName);
239 48 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
240 48 : OUString sMsg( "Failed to load " + aFileName );
241 24 : CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg, RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
242 :
243 48 : Any aRet;
244 48 : Sequence< sal_Int16 > aOutParamIndex;
245 48 : Sequence< Any > aOutParam;
246 24 : bool bWorkbooksHandling = OUString( testInfo[i].sFileBaseName ).equalsAscii("Workbooks.") && !sTempDir.isEmpty() ;
247 :
248 24 : if ( bWorkbooksHandling )
249 : {
250 1 : aParams.realloc(2);
251 1 : aParams[ 0 ] <<= sTempDir;
252 1 : aParams[ 1 ] <<= sTestFileName;
253 : }
254 :
255 24 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
256 :
257 24 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
258 24 : std::cout << "about to invoke vba test in " << OUStringToOString( aFileName, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
259 :
260 : pFoundShell->CallXScript(
261 : xComponent, testInfo[i].sMacroUrl, aParams, aRet, aOutParamIndex,
262 24 : aOutParam);
263 48 : OUString aStringRes;
264 24 : aRet >>= aStringRes;
265 24 : std::cout << "value of Ret " << OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
266 24 : CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
267 24 : pFoundShell->DoClose();
268 24 : if ( bWorkbooksHandling )
269 : {
270 1 : OUString sFileUrl;
271 2 : OUString sFilePath = sTempDir + sTestFileName;
272 1 : osl::FileBase::getFileURLFromSystemPath( sFilePath, sFileUrl );
273 1 : if ( !sFileUrl.isEmpty() )
274 2 : osl::File::remove( sFileUrl );
275 : }
276 49 : }
277 1 : }
278 :
279 3 : ScMacrosTest::ScMacrosTest()
280 3 : : UnoApiTest("/sc/qa/extras/testdocuments")
281 : {
282 3 : }
283 :
284 1 : CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest);
285 :
286 4 : CPPUNIT_PLUGIN_IMPLEMENT();
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|