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/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>
49 :
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>
55 :
56 : #include <basic/sbxdef.hxx>
57 :
58 : #include <doc.hxx>
59 : #include "docsh.hxx"
60 :
61 2 : SV_DECL_REF(SwDocShell)
62 4 : SV_IMPL_REF(SwDocShell)
63 :
64 : using namespace ::com::sun::star;
65 : using namespace ::com::sun::star::uno;
66 :
67 : /* Implementation of Macros test */
68 :
69 8 : class SwMacrosTest : public test::BootstrapFixture, public unotest::MacrosTest
70 : {
71 : public:
72 : SwMacrosTest();
73 :
74 : SwDocShellRef load(const rtl::OUString &rFilter, const rtl::OUString &rURL,
75 : const rtl::OUString &rUserData, const rtl::OUString& rTypeName, sal_uLong nFormatType=0);
76 :
77 : void createFileURL(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath);
78 :
79 : virtual void setUp();
80 : virtual void tearDown();
81 :
82 : //void testStarBasic();
83 : void testVba();
84 : void testFdo55289();
85 4 : 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 2 : CPPUNIT_TEST(testVba);
91 : #endif
92 2 : CPPUNIT_TEST(testFdo55289);
93 :
94 4 : CPPUNIT_TEST_SUITE_END();
95 :
96 : private:
97 : uno::Reference<uno::XInterface> m_xWriterComponent;
98 : ::rtl::OUString m_aBaseString;
99 : };
100 :
101 2 : void SwMacrosTest::createFileURL(const rtl::OUString& aFileBase, const rtl::OUString& aFileExtension, rtl::OUString& rFilePath)
102 : {
103 2 : rtl::OUString aSep(RTL_CONSTASCII_USTRINGPARAM("/"));
104 2 : rtl::OUStringBuffer aBuffer( getSrcRootURL() );
105 2 : aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension);
106 2 : aBuffer.append(aSep).append(aFileBase).append(aFileExtension);
107 2 : rFilePath = aBuffer.makeStringAndClear();
108 2 : }
109 :
110 : #if 0
111 :
112 : void SwMacrosTest::testStarBasic()
113 : {
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);
119 :
120 : CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
121 :
122 : rtl::OUString aURL(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document"));
123 : String sUrl = aURL;
124 : Any aRet;
125 : Sequence< sal_Int16 > aOutParamIndex;
126 : Sequence< Any > aOutParam;
127 : Sequence< uno::Any > aParams;
128 :
129 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
130 :
131 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
132 :
133 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
134 : pFoundShell->DoClose();
135 : }
136 :
137 : #endif
138 :
139 2 : void SwMacrosTest::testVba()
140 : {
141 : TestMacroInfo testInfo[] = {
142 : {
143 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("testVba.")),
144 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("vnd.sun.Star.script:Project.NewMacros.Macro1?language=Basic&location=document"))
145 : }
146 4 : };
147 2 : rtl::OUString aFileExtension( RTL_CONSTASCII_USTRINGPARAM("doc") );
148 4 : for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
149 : {
150 2 : rtl::OUString aFileName;
151 2 : createFileURL(testInfo[i].sFileBaseName, aFileExtension, aFileName);
152 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName);
153 2 : rtl::OUStringBuffer sMsg( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Failed to load ")) );
154 2 : sMsg.append ( aFileName );
155 2 : CPPUNIT_ASSERT_MESSAGE( rtl::OUStringToOString( sMsg.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
156 :
157 2 : String sUrl = testInfo[i].sMacroUrl;
158 2 : Any aRet;
159 2 : Sequence< sal_Int16 > aOutParamIndex;
160 2 : Sequence< Any > aOutParam;
161 2 : Sequence< uno::Any > aParams;
162 :
163 2 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
164 :
165 2 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
166 2 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
167 2 : rtl::OUString aStringRes;
168 2 : aRet >>= aStringRes;
169 2 : std::cout << "value of Ret " << rtl::OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
170 : //CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
171 2 : pFoundShell->DoClose();
172 6 : }
173 2 : }
174 :
175 2 : void SwMacrosTest::testFdo55289()
176 : {
177 2 : SwDoc *const pDoc = new SwDoc;
178 2 : 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());
182 :
183 2 : uno::Reference<frame::XModel> const xModel(pDocShell->GetModel());
184 2 : uno::Reference<drawing::XDrawPageSupplier> const xDPS(xModel, UNO_QUERY);
185 2 : uno::Reference<drawing::XShapes> const xShapes(xDPS->getDrawPage(),
186 2 : UNO_QUERY);
187 : uno::Reference<beans::XPropertySet> const xShape(
188 4 : uno::Reference<lang::XMultiServiceFactory>(xModel, UNO_QUERY)->
189 2 : createInstance("com.sun.star.drawing.GraphicObjectShape"),
190 2 : UNO_QUERY);
191 2 : xShape->setPropertyValue("AnchorType",
192 2 : makeAny(text::TextContentAnchorType_AT_PAGE));
193 2 : xShapes->add(uno::Reference<drawing::XShape>(xShape, UNO_QUERY));
194 2 : xShape->setPropertyValue("AnchorType",
195 2 : makeAny(text::TextContentAnchorType_AT_CHARACTER));
196 2 : xShape->setPropertyValue("AnchorType",
197 2 : makeAny(text::TextContentAnchorType_AS_CHARACTER));
198 2 : xShape->setPropertyValue("AnchorType",
199 2 : makeAny(text::TextContentAnchorType_AT_CHARACTER));
200 2 : xShape->setPropertyValue("AnchorType",
201 2 : makeAny(text::TextContentAnchorType_AS_CHARACTER));
202 : uno::Reference<text::XTextRange> const xEnd =
203 2 : uno::Reference<text::XTextDocument>(xModel, UNO_QUERY)->getText()->getEnd();
204 2 : uno::Reference<text::XTextContent> const xShapeContent(xShape, UNO_QUERY);
205 2 : xShapeContent->attach(xEnd);
206 2 : }
207 :
208 4 : SwMacrosTest::SwMacrosTest()
209 4 : : m_aBaseString(RTL_CONSTASCII_USTRINGPARAM("/sw/qa/core/data"))
210 : {
211 4 : }
212 :
213 4 : void SwMacrosTest::setUp()
214 : {
215 4 : test::BootstrapFixture::setUp();
216 :
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
219 : m_xWriterComponent =
220 8 : getMultiServiceFactory()->createInstance(rtl::OUString(
221 4 : RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Writer.TextDocument")));
222 4 : CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xWriterComponent.is());
223 8 : mxDesktop = Reference<com::sun::star::frame::XDesktop>( getMultiServiceFactory()->createInstance(
224 4 : rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ))), UNO_QUERY );
225 4 : CPPUNIT_ASSERT_MESSAGE("", mxDesktop.is());
226 4 : }
227 :
228 4 : void SwMacrosTest::tearDown()
229 : {
230 4 : uno::Reference< lang::XComponent >( m_xWriterComponent, UNO_QUERY_THROW )->dispose();
231 4 : test::BootstrapFixture::tearDown();
232 4 : }
233 :
234 2 : CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest);
235 :
236 8 : CPPUNIT_PLUGIN_IMPLEMENT();
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|