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 <unotest/macros_test.hxx>
33 #include <test/bootstrapfixture.hxx>
34 #include <rtl/strbuf.hxx>
35 #include <osl/file.hxx>
37 #include <com/sun/star/frame/XDesktop.hpp>
39 #include <com/sun/star/lang/XComponent.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <com/sun/star/frame/XComponentLoader.hpp>
43 #include <com/sun/star/document/MacroExecMode.hpp>
44 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
45 #include <com/sun/star/drawing/XShapes.hpp>
46 #include <com/sun/star/drawing/XShape.hpp>
47 #include <com/sun/star/text/XTextDocument.hpp>
48 #include <com/sun/star/text/TextContentAnchorType.hpp>
50 #include <sfx2/app.hxx>
51 #include <sfx2/docfilt.hxx>
52 #include <sfx2/docfile.hxx>
53 #include <sfx2/sfxmodelfactory.hxx>
54 #include <svl/intitem.hxx>
56 #include <basic/sbxdef.hxx>
61 SO2_DECL_REF(SwDocShell
)
62 SO2_IMPL_REF(SwDocShell
)
64 using namespace ::com::sun::star
;
65 using namespace ::com::sun::star::uno
;
67 /* Implementation of Macros test */
69 class SwMacrosTest
: public test::BootstrapFixture
, public unotest::MacrosTest
74 SwDocShellRef
load(const rtl::OUString
&rFilter
, const rtl::OUString
&rURL
,
75 const rtl::OUString
&rUserData
, const rtl::OUString
& rTypeName
, sal_uLong nFormatType
=0);
77 void createFileURL(const rtl::OUString
& aFileBase
, const rtl::OUString
& aFileExtension
, rtl::OUString
& rFilePath
);
80 virtual void tearDown();
82 //void testStarBasic();
85 CPPUNIT_TEST_SUITE(SwMacrosTest
);
86 #if !defined(MACOSX) && !defined(WNT)
87 //enable this test if you want to play with star basic macros in unit tests
88 //works but does nothing useful yet
89 //CPPUNIT_TEST(testStarBasic);
90 CPPUNIT_TEST(testVba
);
92 CPPUNIT_TEST(testFdo55289
);
94 CPPUNIT_TEST_SUITE_END();
97 uno::Reference
<uno::XInterface
> m_xWriterComponent
;
98 ::rtl::OUString m_aBaseString
;
101 void SwMacrosTest::createFileURL(const rtl::OUString
& aFileBase
, const rtl::OUString
& aFileExtension
, rtl::OUString
& rFilePath
)
103 rtl::OUString
aSep(RTL_CONSTASCII_USTRINGPARAM("/"));
104 rtl::OUStringBuffer
aBuffer( getSrcRootURL() );
105 aBuffer
.append(m_aBaseString
).append(aSep
).append(aFileExtension
);
106 aBuffer
.append(aSep
).append(aFileBase
).append(aFileExtension
);
107 rFilePath
= aBuffer
.makeStringAndClear();
112 void SwMacrosTest::testStarBasic()
114 const rtl::OUString
aFileNameBase(RTL_CONSTASCII_USTRINGPARAM("StarBasic."));
115 rtl::OUString
aFileExtension(aFileFormats
[0].pName
, strlen(aFileFormats
[0].pName
), RTL_TEXTENCODING_UTF8
);
116 rtl::OUString aFileName
;
117 createFileURL(aFileNameBase
, aFileExtension
, aFileName
);
118 uno::Reference
< com::sun::star::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
);
120 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent
.is());
122 rtl::OUString
aURL(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document"));
125 Sequence
< sal_Int16
> aOutParamIndex
;
126 Sequence
< Any
> aOutParam
;
127 Sequence
< uno::Any
> aParams
;
129 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
131 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
133 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
134 pFoundShell
->DoClose();
139 void SwMacrosTest::testVba()
141 TestMacroInfo testInfo
[] = {
143 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("testVba.")),
144 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:Project.NewMacros.Macro1?language=Basic&location=document"))
147 rtl::OUString
aFileExtension( RTL_CONSTASCII_USTRINGPARAM("doc") );
148 for ( sal_uInt32 i
=0; i
<SAL_N_ELEMENTS( testInfo
); ++i
)
150 rtl::OUString aFileName
;
151 createFileURL(testInfo
[i
].sFileBaseName
, aFileExtension
, aFileName
);
152 uno::Reference
< com::sun::star::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
);
153 rtl::OUStringBuffer
sMsg( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Failed to load ")) );
154 sMsg
.append ( aFileName
);
155 CPPUNIT_ASSERT_MESSAGE( rtl::OUStringToOString( sMsg
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
).getStr(), xComponent
.is() );
157 String sUrl
= testInfo
[i
].sMacroUrl
;
159 Sequence
< sal_Int16
> aOutParamIndex
;
160 Sequence
< Any
> aOutParam
;
161 Sequence
< uno::Any
> aParams
;
163 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
165 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
166 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
167 rtl::OUString aStringRes
;
169 std::cout
<< "value of Ret " << rtl::OUStringToOString( aStringRes
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
170 //CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
171 pFoundShell
->DoClose();
175 void SwMacrosTest::testFdo55289()
177 SwDoc
*const pDoc
= new SwDoc
;
178 SwDocShellRef pDocShell
= new SwDocShell(pDoc
, SFX_CREATE_MODE_EMBEDDED
);
179 // this needs to run with no layout to tickle the bugs in the special
180 // cases in SwXShape re-anchoring
181 assert(!pDoc
->GetCurrentLayout());
183 uno::Reference
<frame::XModel
> const xModel(pDocShell
->GetModel());
184 uno::Reference
<drawing::XDrawPageSupplier
> const xDPS(xModel
, UNO_QUERY
);
185 uno::Reference
<drawing::XShapes
> const xShapes(xDPS
->getDrawPage(),
187 uno::Reference
<beans::XPropertySet
> const xShape(
188 uno::Reference
<lang::XMultiServiceFactory
>(xModel
, UNO_QUERY
)->
189 createInstance("com.sun.star.drawing.GraphicObjectShape"),
191 xShape
->setPropertyValue("AnchorType",
192 makeAny(text::TextContentAnchorType_AT_PAGE
));
193 xShapes
->add(uno::Reference
<drawing::XShape
>(xShape
, UNO_QUERY
));
194 xShape
->setPropertyValue("AnchorType",
195 makeAny(text::TextContentAnchorType_AT_CHARACTER
));
196 xShape
->setPropertyValue("AnchorType",
197 makeAny(text::TextContentAnchorType_AS_CHARACTER
));
198 xShape
->setPropertyValue("AnchorType",
199 makeAny(text::TextContentAnchorType_AT_CHARACTER
));
200 xShape
->setPropertyValue("AnchorType",
201 makeAny(text::TextContentAnchorType_AS_CHARACTER
));
202 uno::Reference
<text::XTextRange
> const xEnd
=
203 uno::Reference
<text::XTextDocument
>(xModel
, UNO_QUERY
)->getText()->getEnd();
204 uno::Reference
<text::XTextContent
> const xShapeContent(xShape
, UNO_QUERY
);
205 xShapeContent
->attach(xEnd
);
208 SwMacrosTest::SwMacrosTest()
209 : m_aBaseString(RTL_CONSTASCII_USTRINGPARAM("/sw/qa/core/data"))
213 void SwMacrosTest::setUp()
215 test::BootstrapFixture::setUp();
217 // This is a bit of a fudge, we do this to ensure that SwGlobals::ensure,
218 // which is a private symbol to us, gets called
220 getMultiServiceFactory()->createInstance(rtl::OUString(
221 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.TextDocument")));
222 CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xWriterComponent
.is());
223 mxDesktop
= Reference
<com::sun::star::frame::XDesktop
>( getMultiServiceFactory()->createInstance(
224 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ))), UNO_QUERY
);
225 CPPUNIT_ASSERT_MESSAGE("", mxDesktop
.is());
228 void SwMacrosTest::tearDown()
230 uno::Reference
< lang::XComponent
>( m_xWriterComponent
, UNO_QUERY_THROW
)->dispose();
231 test::BootstrapFixture::tearDown();
234 CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest
);
236 CPPUNIT_PLUGIN_IMPLEMENT();
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */