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 : #if !defined MACOSX
40 : void testStarBasic();
41 : void testVba();
42 : void testMSP();
43 : #endif
44 2 : 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 1 : CPPUNIT_TEST(testStarBasic);
49 1 : CPPUNIT_TEST(testMSP);
50 1 : CPPUNIT_TEST(testVba);
51 : #endif
52 :
53 5 : 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 1 : void ScMacrosTest::testMSP()
68 : {
69 1 : const OUString aFileNameBase("MasterScriptProviderProblem.ods");
70 2 : OUString aFileName;
71 1 : createFileURL(aFileNameBase, aFileName);
72 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
73 :
74 1 : CPPUNIT_ASSERT_MESSAGE("Failed to load MasterScriptProviderProblem.ods", xComponent.is());
75 :
76 2 : Any aRet;
77 2 : Sequence< sal_Int16 > aOutParamIndex;
78 2 : Sequence< Any > aOutParam;
79 2 : Sequence< uno::Any > aParams;
80 :
81 1 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
82 :
83 1 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
84 1 : ScDocShell* xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
85 1 : CPPUNIT_ASSERT(xDocSh != NULL);
86 :
87 : SfxObjectShell::CallXScript(
88 : xComponent,
89 : "vnd.sun.Star.script:Standard.Module1.TestMSP?language=Basic&location=document",
90 1 : aParams, aRet, aOutParamIndex, aOutParam);
91 2 : OUString sResult;
92 1 : aRet >>= sResult;
93 :
94 : SAL_INFO("sc.qa", "Result is " << sResult );
95 1 : CPPUNIT_ASSERT_MESSAGE("TestMSP ( for fdo#67547) failed", sResult == "OK" );
96 2 : xDocSh->DoClose();
97 1 : }
98 :
99 1 : void ScMacrosTest::testStarBasic()
100 : {
101 1 : const OUString aFileNameBase("StarBasic.ods");
102 2 : OUString aFileName;
103 1 : createFileURL(aFileNameBase, aFileName);
104 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
105 :
106 1 : CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
107 :
108 2 : Any aRet;
109 2 : Sequence< sal_Int16 > aOutParamIndex;
110 2 : Sequence< Any > aOutParam;
111 2 : Sequence< uno::Any > aParams;
112 :
113 1 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
114 :
115 1 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
116 1 : ScDocShell* xDocSh = static_cast<ScDocShell*>(pFoundShell);
117 1 : ScDocument& rDoc = xDocSh->GetDocument();
118 :
119 : SfxObjectShell::CallXScript(
120 : xComponent,
121 : "vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document",
122 1 : aParams, aRet, aOutParamIndex, aOutParam);
123 : double aValue;
124 1 : rDoc.GetValue(0,0,0,aValue);
125 1 : CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue, 0.00001);
126 2 : xDocSh->DoClose();
127 1 : }
128 :
129 1 : void ScMacrosTest::testVba()
130 : {
131 : TestMacroInfo testInfo[] = {
132 : {
133 : OUString("TestAddress."),
134 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
135 : },
136 : {
137 : OUString("vba."),
138 : OUString("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document"),
139 : },
140 : {
141 : OUString("MiscRangeTests."),
142 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
143 : },
144 : {
145 : OUString("bytearraystring."),
146 : OUString("vnd.sun.Star.script:VBAProject.testMacro.test?language=Basic&location=document")
147 : },
148 : {
149 : OUString("AutoFilter."),
150 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
151 : },
152 : {
153 : OUString("CalcFont."),
154 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
155 : },
156 : {
157 : OUString("TestIntersection."),
158 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
159 : },
160 : {
161 : OUString("TestUnion."),
162 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
163 : },
164 : {
165 : OUString("range-4."),
166 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
167 : },
168 : {
169 : OUString("Ranges-3."),
170 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
171 : },
172 : {
173 : OUString("TestCalc_Rangetest."),
174 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
175 : },
176 : {
177 : OUString("TestCalc_Rangetest2."),
178 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
179 : },
180 : {
181 : OUString("Ranges-2."),
182 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
183 : },
184 : {
185 : OUString("pagesetup."),
186 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
187 : },
188 : {
189 : OUString("Window."),
190 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
191 : },
192 : {
193 : OUString("window2."),
194 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
195 : },
196 : {
197 : OUString("PageBreaks."),
198 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
199 : },
200 : {
201 : OUString("Shapes."),
202 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
203 : },
204 : {
205 : OUString("Ranges."),
206 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
207 : },
208 : {
209 : OUString("CheckOptionToggleValue."),
210 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
211 : },
212 : {
213 : OUString("GeneratedEventTest."),
214 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
215 : },
216 : {
217 : OUString("MiscControlTests."),
218 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
219 : },
220 : {
221 : OUString("Workbooks."),
222 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
223 : },
224 : {
225 : OUString("Names."),
226 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
227 : },
228 : {
229 : OUString("vba_endFunction."),
230 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
231 : },
232 : {
233 : OUString("vba_findFunction."),
234 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
235 : },
236 27 : };
237 2 : OUString sTempDir;
238 2 : OUString sTempDirURL;
239 1 : osl::FileBase:: getTempDirURL( sTempDirURL );
240 1 : osl::FileBase::getSystemPathFromFileURL( sTempDirURL, sTempDir );
241 1 : sTempDir += OUString( SAL_PATHDELIMITER );
242 2 : OUString sTestFileName("My Test WorkBook.xls");
243 2 : Sequence< uno::Any > aParams;
244 27 : for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
245 : {
246 26 : OUString aFileName;
247 26 : createFileURL(testInfo[i].sFileBaseName + "xls", aFileName);
248 52 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
249 52 : OUString sMsg( "Failed to load " + aFileName );
250 26 : CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg, RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
251 :
252 52 : Any aRet;
253 52 : Sequence< sal_Int16 > aOutParamIndex;
254 52 : Sequence< Any > aOutParam;
255 26 : bool bWorkbooksHandling = OUString( testInfo[i].sFileBaseName ) == "Workbooks." && !sTempDir.isEmpty() ;
256 :
257 26 : if ( bWorkbooksHandling )
258 : {
259 1 : aParams.realloc(2);
260 1 : aParams[ 0 ] <<= sTempDir;
261 1 : aParams[ 1 ] <<= sTestFileName;
262 : }
263 :
264 26 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
265 :
266 26 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
267 : SAL_INFO("sc.qa", "about to invoke vba test in " << aFileName);
268 :
269 : SfxObjectShell::CallXScript(
270 : xComponent, testInfo[i].sMacroUrl, aParams, aRet, aOutParamIndex,
271 26 : aOutParam);
272 52 : OUString aStringRes;
273 26 : aRet >>= aStringRes;
274 : SAL_INFO("sc.qa", "value of Ret " << aStringRes);
275 26 : CPPUNIT_ASSERT_MESSAGE( "script reported failure", aStringRes == "OK" );
276 26 : pFoundShell->DoClose();
277 26 : if ( bWorkbooksHandling )
278 : {
279 1 : OUString sFileUrl;
280 2 : OUString sFilePath = sTempDir + sTestFileName;
281 1 : osl::FileBase::getFileURLFromSystemPath( sFilePath, sFileUrl );
282 1 : if ( !sFileUrl.isEmpty() )
283 2 : osl::File::remove( sFileUrl );
284 : }
285 53 : }
286 1 : }
287 :
288 : #endif
289 :
290 3 : ScMacrosTest::ScMacrosTest()
291 3 : : UnoApiTest("/sc/qa/extras/testdocuments")
292 : {
293 3 : }
294 :
295 1 : CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest);
296 :
297 4 : CPPUNIT_PLUGIN_IMPLEMENT();
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|