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