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