1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
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/
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
15 * Major Contributor(s):
16 * Copyright (C) 2010 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
18 * Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com>
20 * All Rights Reserved.
22 * For minor contributions see the git repository.
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.
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>
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>
43 #include <basic/sbxdef.hxx>
46 #include "patattr.hxx"
47 #include "scitems.hxx"
48 #include "document.hxx"
50 using namespace ::com::sun::star
;
51 using namespace ::com::sun::star::uno
;
53 /* Implementation of Macros test */
55 class ScMacrosTest
: public test::BootstrapFixture
, public unotest::MacrosTest
60 void createFileURL(const rtl::OUString
& aFileBase
, const rtl::OUString
& aFileExtension
, rtl::OUString
& rFilePath
);
63 virtual void tearDown();
67 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 CPPUNIT_TEST(testStarBasic
);
72 CPPUNIT_TEST(testVba
);
75 CPPUNIT_TEST_SUITE_END();
78 uno::Reference
<uno::XInterface
> m_xCalcComponent
;
79 rtl::OUString m_aBaseString
;
83 void ScMacrosTest::createFileURL(const rtl::OUString
& aFileBase
, const rtl::OUString
& aFileExtension
, rtl::OUString
& rFilePath
)
85 rtl::OUString
aSep(RTL_CONSTASCII_USTRINGPARAM("/"));
86 rtl::OUStringBuffer
aBuffer( getSrcRootURL() );
87 aBuffer
.append(m_aBaseString
).append(aSep
).append(aFileExtension
);
88 aBuffer
.append(aSep
).append(aFileBase
).append(aFileExtension
);
89 rFilePath
= aBuffer
.makeStringAndClear();
92 void ScMacrosTest::testStarBasic()
94 const rtl::OUString
aFileNameBase(RTL_CONSTASCII_USTRINGPARAM("StarBasic."));
95 rtl::OUString
aFileExtension("ods");
96 rtl::OUString aFileName
;
97 createFileURL(aFileNameBase
, aFileExtension
, aFileName
);
98 std::cout
<< "StarBasic test" << std::endl
;
99 uno::Reference
< com::sun::star::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
);
101 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent
.is());
103 rtl::OUString
aURL(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document"));
106 Sequence
< sal_Int16
> aOutParamIndex
;
107 Sequence
< Any
> aOutParam
;
108 Sequence
< uno::Any
> aParams
;
110 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
112 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
113 ScDocShell
* xDocSh
= static_cast<ScDocShell
*>(pFoundShell
);
114 ScDocument
* pDoc
= xDocSh
->GetDocument();
116 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
118 pDoc
->GetValue(0,0,0,aValue
);
119 std::cout
<< "returned value = " << aValue
<< std::endl
;
120 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("script did not change the value of Sheet1.A1",2.0, aValue
, 0.00001);
125 void ScMacrosTest::testVba()
127 TestMacroInfo testInfo
[] = {
129 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TestAddress.")),
130 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:VBAProject.testMacros.test?language=Basic&location=document"))
133 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vba.")),
134 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:VBAProject.Modul1.Modul1?language=Basic&location=document")),
138 rtl::OUString
aFileExtension("xls");
139 for ( sal_uInt32 i
=0; i
<SAL_N_ELEMENTS( testInfo
); ++i
)
141 rtl::OUString aFileName
;
142 createFileURL(testInfo
[i
].sFileBaseName
, aFileExtension
, aFileName
);
143 uno::Reference
< com::sun::star::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
);
144 rtl::OUString
sMsg( RTL_CONSTASCII_USTRINGPARAM("Failed to load ") );
145 sMsg
.concat( aFileName
);
146 CPPUNIT_ASSERT_MESSAGE( rtl::OUStringToOString( sMsg
, RTL_TEXTENCODING_UTF8
).getStr(), xComponent
.is() );
148 String sUrl
= testInfo
[i
].sMacroUrl
;
150 Sequence
< sal_Int16
> aOutParamIndex
;
151 Sequence
< Any
> aOutParam
;
152 Sequence
< uno::Any
> aParams
;
154 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
156 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
157 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
158 rtl::OUString aStringRes
;
160 std::cout
<< "value of Ret " << rtl::OUStringToOString( aStringRes
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
161 CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes
== "OK" );
162 pFoundShell
->DoClose();
166 ScMacrosTest::ScMacrosTest()
167 : m_aBaseString(RTL_CONSTASCII_USTRINGPARAM("/sc/qa/unit/data"))
171 void ScMacrosTest::setUp()
173 test::BootstrapFixture::setUp();
175 // This is a bit of a fudge, we do this to ensure that ScGlobals::ensure,
176 // which is a private symbol to us, gets called
178 getMultiServiceFactory()->createInstance(rtl::OUString(
179 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.SpreadsheetDocument")));
180 CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xCalcComponent
.is());
181 mxDesktop
= Reference
<com::sun::star::frame::XDesktop
>( getMultiServiceFactory()->createInstance(
182 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ))), UNO_QUERY
);
183 CPPUNIT_ASSERT(mxDesktop
.is());
186 void ScMacrosTest::tearDown()
188 uno::Reference
< lang::XComponent
>( m_xCalcComponent
, UNO_QUERY_THROW
)->dispose();
189 test::BootstrapFixture::tearDown();
192 CPPUNIT_TEST_SUITE_REGISTRATION(ScMacrosTest
);
194 CPPUNIT_PLUGIN_IMPLEMENT();
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */