fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / sw / qa / core / macros-test.cxx
blob3ac4145f4273022dd7022193600dec46fde900ed
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
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>
16 #include <com/sun/star/frame/XDesktop.hpp>
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>
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>
37 #include <basic/sbxdef.hxx>
39 #include <doc.hxx>
40 #include "docsh.hxx"
42 SV_DECL_REF(SwDocShell)
43 SV_IMPL_REF(SwDocShell)
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
48 /* Implementation of Macros test */
50 class SwMacrosTest : public test::BootstrapFixture, public unotest::MacrosTest
52 public:
53 SwMacrosTest();
55 SwDocShellRef load(const OUString &rFilter, const OUString &rURL,
56 const OUString &rUserData, const OUString& rTypeName, sal_uLong nFormatType=0);
58 void createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath);
60 virtual void setUp();
61 virtual void tearDown();
63 //void testStarBasic();
64 void testVba();
65 void testFdo55289();
66 CPPUNIT_TEST_SUITE(SwMacrosTest);
67 #if !defined(MACOSX) && !defined(WNT)
68 //enable this test if you want to play with star basic macros in unit tests
69 //works but does nothing useful yet
70 //CPPUNIT_TEST(testStarBasic);
71 CPPUNIT_TEST(testVba);
72 #endif
73 CPPUNIT_TEST(testFdo55289);
75 CPPUNIT_TEST_SUITE_END();
77 private:
78 uno::Reference<uno::XInterface> m_xWriterComponent;
79 OUString m_aBaseString;
82 void SwMacrosTest::createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath)
84 OUString aSep("/");
85 OUStringBuffer aBuffer( getSrcRootURL() );
86 aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension);
87 aBuffer.append(aSep).append(aFileBase).append(aFileExtension);
88 rFilePath = aBuffer.makeStringAndClear();
91 #if 0
93 void SwMacrosTest::testStarBasic()
95 const OUString aFileNameBase("StarBasic.");
96 OUString aFileExtension(aFileFormats[0].pName, strlen(aFileFormats[0].pName), RTL_TEXTENCODING_UTF8 );
97 OUString aFileName;
98 createFileURL(aFileNameBase, aFileExtension, aFileName);
99 uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
101 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
103 OUString aURL("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document");
104 String sUrl = aURL;
105 Any aRet;
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);
114 pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
115 pFoundShell->DoClose();
118 #endif
120 void SwMacrosTest::testVba()
122 TestMacroInfo testInfo[] = {
124 OUString("testVba."),
125 OUString("vnd.sun.Star.script:Project.NewMacros.Macro1?language=Basic&location=document")
128 OUString aFileExtension( "doc" );
129 for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
131 OUString aFileName;
132 createFileURL(testInfo[i].sFileBaseName, aFileExtension, aFileName);
133 uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
134 OUStringBuffer sMsg( OUString("Failed to load ") );
135 sMsg.append ( aFileName );
136 CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
138 String sUrl = testInfo[i].sMacroUrl;
139 Any aRet;
140 Sequence< sal_Int16 > aOutParamIndex;
141 Sequence< Any > aOutParam;
142 Sequence< uno::Any > aParams;
144 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
146 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
147 pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
148 OUString aStringRes;
149 aRet >>= aStringRes;
150 std::cout << "value of Ret " << OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
151 //CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
152 pFoundShell->DoClose();
156 void SwMacrosTest::testFdo55289()
158 SwDoc *const pDoc = new SwDoc;
159 SwDocShellRef pDocShell = new SwDocShell(pDoc, SFX_CREATE_MODE_EMBEDDED);
160 // this needs to run with no layout to tickle the bugs in the special
161 // cases in SwXShape re-anchoring
162 assert(!pDoc->GetCurrentLayout());
164 uno::Reference<frame::XModel> const xModel(pDocShell->GetModel());
165 uno::Reference<drawing::XDrawPageSupplier> const xDPS(xModel, UNO_QUERY);
166 uno::Reference<drawing::XShapes> const xShapes(xDPS->getDrawPage(),
167 UNO_QUERY);
168 uno::Reference<beans::XPropertySet> const xShape(
169 uno::Reference<lang::XMultiServiceFactory>(xModel, UNO_QUERY)->
170 createInstance("com.sun.star.drawing.GraphicObjectShape"),
171 UNO_QUERY);
172 xShape->setPropertyValue("AnchorType",
173 makeAny(text::TextContentAnchorType_AT_PAGE));
174 xShapes->add(uno::Reference<drawing::XShape>(xShape, UNO_QUERY));
175 xShape->setPropertyValue("AnchorType",
176 makeAny(text::TextContentAnchorType_AT_CHARACTER));
177 xShape->setPropertyValue("AnchorType",
178 makeAny(text::TextContentAnchorType_AS_CHARACTER));
179 xShape->setPropertyValue("AnchorType",
180 makeAny(text::TextContentAnchorType_AT_CHARACTER));
181 xShape->setPropertyValue("AnchorType",
182 makeAny(text::TextContentAnchorType_AS_CHARACTER));
183 uno::Reference<text::XTextRange> const xEnd =
184 uno::Reference<text::XTextDocument>(xModel, UNO_QUERY)->getText()->getEnd();
185 uno::Reference<text::XTextContent> const xShapeContent(xShape, UNO_QUERY);
186 xShapeContent->attach(xEnd);
189 SwMacrosTest::SwMacrosTest()
190 : m_aBaseString("/sw/qa/core/data")
194 void SwMacrosTest::setUp()
196 test::BootstrapFixture::setUp();
198 // This is a bit of a fudge, we do this to ensure that SwGlobals::ensure,
199 // which is a private symbol to us, gets called
200 m_xWriterComponent =
201 getMultiServiceFactory()->createInstance(OUString(
202 "com.sun.star.comp.Writer.TextDocument"));
203 CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xWriterComponent.is());
204 mxDesktop = com::sun::star::frame::Desktop::create( comphelper::getComponentContext(getMultiServiceFactory()) );
207 void SwMacrosTest::tearDown()
209 uno::Reference< lang::XComponent >( m_xWriterComponent, UNO_QUERY_THROW )->dispose();
210 test::BootstrapFixture::tearDown();
213 CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest);
215 CPPUNIT_PLUGIN_IMPLEMENT();
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */