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 <unotest/macros_test.hxx>
13 : #include <rtl/strbuf.hxx>
14 : #include <osl/file.hxx>
15 :
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>
22 :
23 : #include <basic/sbxdef.hxx>
24 :
25 : #include "docsh.hxx"
26 : #include "patattr.hxx"
27 : #include "scitems.hxx"
28 : #include "document.hxx"
29 :
30 : using namespace ::com::sun::star;
31 : using namespace ::com::sun::star::uno;
32 :
33 : /* Implementation of Macros test */
34 :
35 4 : class ScMacrosTest : public UnoApiTest
36 : {
37 : public:
38 : ScMacrosTest();
39 :
40 : void testStarBasic();
41 : void testVba();
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(testVba);
48 : #endif
49 :
50 2 : CPPUNIT_TEST_SUITE_END();
51 :
52 : private:
53 : uno::Reference<uno::XInterface> m_xCalcComponent;
54 : };
55 :
56 1 : void ScMacrosTest::testStarBasic()
57 : {
58 1 : const OUString aFileNameBase("StarBasic.ods");
59 2 : OUString aFileName;
60 1 : createFileURL(aFileNameBase, aFileName);
61 1 : std::cout << "StarBasic test" << std::endl;
62 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
63 :
64 1 : CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
65 :
66 2 : OUString aURL("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document");
67 2 : String sUrl = aURL;
68 2 : Any aRet;
69 2 : Sequence< sal_Int16 > aOutParamIndex;
70 2 : Sequence< Any > aOutParam;
71 2 : Sequence< uno::Any > aParams;
72 :
73 1 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
74 :
75 1 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
76 1 : ScDocShell* xDocSh = static_cast<ScDocShell*>(pFoundShell);
77 1 : ScDocument* pDoc = xDocSh->GetDocument();
78 :
79 1 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
80 : double aValue;
81 1 : pDoc->GetValue(0,0,0,aValue);
82 1 : std::cout << "returned value = " << aValue << std::endl;
83 1 : CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue, 0.00001);
84 2 : xDocSh->DoClose();
85 1 : }
86 :
87 1 : void ScMacrosTest::testVba()
88 : {
89 : TestMacroInfo testInfo[] = {
90 : {
91 : OUString("TestAddress."),
92 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
93 : },
94 : {
95 : OUString("vba."),
96 : OUString("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document"),
97 : },
98 : {
99 : OUString("MiscRangeTests."),
100 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
101 : },
102 : {
103 : OUString("bytearraystring."),
104 : OUString("vnd.sun.Star.script:VBAProject.testMacro.test?language=Basic&location=document")
105 : },
106 : {
107 : OUString("AutoFilter."),
108 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
109 : },
110 : {
111 : OUString("CalcFont."),
112 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
113 : },
114 : {
115 : OUString("TestIntersection."),
116 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
117 : },
118 : {
119 : OUString("TestUnion."),
120 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
121 : },
122 : {
123 : OUString("range-4."),
124 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
125 : },
126 : {
127 : OUString("Ranges-3."),
128 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
129 : },
130 : {
131 : OUString("TestCalc_Rangetest."),
132 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
133 : },
134 : {
135 : OUString("TestCalc_Rangetest2."),
136 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
137 : },
138 : {
139 : OUString("Ranges-2."),
140 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
141 : },
142 : {
143 : OUString("pagesetup."),
144 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
145 : },
146 : {
147 : OUString("Window."),
148 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
149 : },
150 : {
151 : OUString("window2."),
152 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
153 : },
154 : {
155 : OUString("PageBreaks."),
156 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
157 : },
158 : {
159 : OUString("Shapes."),
160 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
161 : },
162 : {
163 : OUString("Ranges."),
164 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
165 : },
166 : {
167 : OUString("CheckOptionToggleValue."),
168 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
169 : },
170 : {
171 : OUString("GeneratedEventTest."),
172 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
173 : },
174 : {
175 : OUString("MiscControlTests."),
176 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
177 : },
178 : {
179 : OUString("Workbooks."),
180 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
181 : },
182 : {
183 : OUString("Names."),
184 : OUString("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document")
185 : },
186 25 : };
187 2 : OUString sTempDir;
188 2 : OUString sTempDirURL;
189 1 : osl::FileBase:: getTempDirURL( sTempDirURL );
190 1 : osl::FileBase::getSystemPathFromFileURL( sTempDirURL, sTempDir );
191 1 : sTempDir += OUString( SAL_PATHDELIMITER );
192 2 : OUString sTestFileName("My Test WorkBook.xls");
193 2 : Sequence< uno::Any > aParams;
194 25 : for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
195 : {
196 24 : OUString aFileName;
197 24 : createFileURL(testInfo[i].sFileBaseName + "xls", aFileName);
198 48 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.sheet.SpreadsheetDocument");
199 48 : OUString sMsg( "Failed to load " + aFileName );
200 24 : CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg, RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
201 :
202 48 : String sUrl = testInfo[i].sMacroUrl;
203 48 : Any aRet;
204 48 : Sequence< sal_Int16 > aOutParamIndex;
205 48 : Sequence< Any > aOutParam;
206 24 : bool bWorkbooksHandling = OUString( testInfo[i].sFileBaseName ).equalsAscii("Workbooks.") && !sTempDir.isEmpty() ;
207 :
208 24 : if ( bWorkbooksHandling )
209 : {
210 1 : aParams.realloc(2);
211 1 : aParams[ 0 ] <<= sTempDir;
212 1 : aParams[ 1 ] <<= sTestFileName;
213 : }
214 :
215 24 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
216 :
217 24 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
218 24 : std::cout << "about to invoke vba test in " << OUStringToOString( aFileName, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
219 :
220 24 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
221 48 : OUString aStringRes;
222 24 : aRet >>= aStringRes;
223 24 : std::cout << "value of Ret " << OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
224 24 : CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
225 24 : pFoundShell->DoClose();
226 24 : if ( bWorkbooksHandling )
227 : {
228 1 : OUString sFileUrl;
229 2 : OUString sFilePath = sTempDir + sTestFileName;
230 1 : osl::FileBase::getFileURLFromSystemPath( sFilePath, sFileUrl );
231 1 : if ( !sFileUrl.isEmpty() )
232 2 : osl::File::remove( sFileUrl );
233 : }
234 49 : }
235 1 : }
236 :
237 2 : ScMacrosTest::ScMacrosTest()
238 2 : : UnoApiTest("/sc/qa/extras/testdocuments")
239 : {
240 2 : }
241 :
242 1 : CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest);
243 :
244 4 : CPPUNIT_PLUGIN_IMPLEMENT();
245 :
246 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|