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