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