Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2010 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
17 : * (initial developer)
18 : * Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com>
19 : *
20 : * All Rights Reserved.
21 : *
22 : * For minor contributions see the git repository.
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
26 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
27 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
28 : * instead of those above.
29 : */
30 :
31 : #include <sal/config.h>
32 : #include <test/bootstrapfixture.hxx>
33 : #include <unotest/macros_test.hxx>
34 : #include <rtl/strbuf.hxx>
35 : #include <osl/file.hxx>
36 :
37 : #include <sfx2/app.hxx>
38 : #include <sfx2/docfilt.hxx>
39 : #include <sfx2/docfile.hxx>
40 : #include <sfx2/sfxmodelfactory.hxx>
41 : #include <svl/intitem.hxx>
42 :
43 : #include <basic/sbxdef.hxx>
44 :
45 : #include "docsh.hxx"
46 : #include "patattr.hxx"
47 : #include "scitems.hxx"
48 : #include "document.hxx"
49 :
50 : using namespace ::com::sun::star;
51 : using namespace ::com::sun::star::uno;
52 :
53 : /* Implementation of Macros test */
54 :
55 8 : class ScMacrosTest : public test::BootstrapFixture, public unotest::MacrosTest
56 : {
57 : public:
58 : ScMacrosTest();
59 :
60 : void createFileURL(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath);
61 :
62 : virtual void setUp();
63 : virtual void tearDown();
64 :
65 : void testStarBasic();
66 : void testVba();
67 4 : CPPUNIT_TEST_SUITE(ScMacrosTest);
68 : #if !defined(MACOSX) && !defined(WNT)
69 : //enable this test if you want to play with star basic macros in unit tests
70 : //works but does nothing useful yet
71 2 : CPPUNIT_TEST(testStarBasic);
72 2 : CPPUNIT_TEST(testVba);
73 : #endif
74 :
75 4 : CPPUNIT_TEST_SUITE_END();
76 :
77 : private:
78 : uno::Reference<uno::XInterface> m_xCalcComponent;
79 : rtl::OUString m_aBaseString;
80 : };
81 :
82 :
83 6 : void ScMacrosTest::createFileURL(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath)
84 : {
85 6 : rtl::OUString aSep(RTL_CONSTASCII_USTRINGPARAM("/"));
86 6 : rtl::OUStringBuffer aBuffer( getSrcRootURL() );
87 6 : aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension);
88 6 : aBuffer.append(aSep).append(aFileBase).append(aFileExtension);
89 6 : rFilePath = aBuffer.makeStringAndClear();
90 6 : }
91 :
92 2 : void ScMacrosTest::testStarBasic()
93 : {
94 2 : const rtl::OUString aFileNameBase(RTL_CONSTASCII_USTRINGPARAM("StarBasic."));
95 2 : rtl::OUString aFileExtension("ods");
96 2 : rtl::OUString aFileName;
97 2 : createFileURL(aFileNameBase, aFileExtension, aFileName);
98 2 : std::cout << "StarBasic test" << std::endl;
99 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName);
100 :
101 2 : CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
102 :
103 2 : rtl::OUString aURL(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document"));
104 2 : String sUrl = aURL;
105 2 : Any aRet;
106 2 : Sequence< sal_Int16 > aOutParamIndex;
107 2 : Sequence< Any > aOutParam;
108 2 : Sequence< uno::Any > aParams;
109 :
110 2 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
111 :
112 2 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
113 2 : ScDocShell* xDocSh = static_cast<ScDocShell*>(pFoundShell);
114 2 : ScDocument* pDoc = xDocSh->GetDocument();
115 :
116 2 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
117 : double aValue;
118 2 : pDoc->GetValue(0,0,0,aValue);
119 2 : std::cout << "returned value = " << aValue << std::endl;
120 2 : CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue, 0.00001);
121 2 : xDocSh->DoClose();
122 2 : }
123 :
124 :
125 2 : void ScMacrosTest::testVba()
126 : {
127 : TestMacroInfo testInfo[] = {
128 : {
129 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TestAddress.")),
130 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"))
131 : },
132 : {
133 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vba.")),
134 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document")),
135 : }
136 6 : };
137 :
138 2 : rtl::OUString aFileExtension("xls");
139 6 : for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
140 : {
141 4 : rtl::OUString aFileName;
142 4 : createFileURL(testInfo[i].sFileBaseName, aFileExtension, aFileName);
143 4 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName);
144 4 : rtl::OUString sMsg( "Failed to load " + aFileName );
145 4 : CPPUNIT_ASSERT_MESSAGE( rtl::OUStringToOString( sMsg, RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
146 :
147 4 : String sUrl = testInfo[i].sMacroUrl;
148 4 : Any aRet;
149 4 : Sequence< sal_Int16 > aOutParamIndex;
150 4 : Sequence< Any > aOutParam;
151 4 : Sequence< uno::Any > aParams;
152 :
153 4 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
154 :
155 4 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
156 4 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
157 4 : rtl::OUString aStringRes;
158 4 : aRet >>= aStringRes;
159 4 : std::cout << "value of Ret " << rtl::OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
160 4 : CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
161 4 : pFoundShell->DoClose();
162 10 : }
163 2 : }
164 :
165 4 : ScMacrosTest::ScMacrosTest()
166 4 : : m_aBaseString(RTL_CONSTASCII_USTRINGPARAM("/sc/qa/unit/data"))
167 : {
168 4 : }
169 :
170 4 : void ScMacrosTest::setUp()
171 : {
172 4 : test::BootstrapFixture::setUp();
173 :
174 : // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
175 : // which is a private symbol to us, gets called
176 : m_xCalcComponent =
177 8 : getMultiServiceFactory()->createInstance(rtl::OUString(
178 4 : RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.SpreadsheetDocument")));
179 4 : CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xCalcComponent.is());
180 8 : mxDesktop = Reference<com::sun::star::frame::XDesktop>( getMultiServiceFactory()->createInstance(
181 4 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ))), UNO_QUERY );
182 4 : CPPUNIT_ASSERT(mxDesktop.is());
183 4 : }
184 :
185 4 : void ScMacrosTest::tearDown()
186 : {
187 4 : uno::Reference< lang::XComponent >( m_xCalcComponent, UNO_QUERY_THROW )->dispose();
188 4 : test::BootstrapFixture::tearDown();
189 4 : }
190 :
191 2 : CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest);
192 :
193 8 : CPPUNIT_PLUGIN_IMPLEMENT();
194 :
195 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|