1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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/lang/XComponent.hpp>
17 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
18 #include <com/sun/star/beans/XPropertySet.hpp>
19 #include <com/sun/star/util/SearchOptions2.hpp>
20 #include <com/sun/star/util/SearchAlgorithms2.hpp>
21 #include <com/sun/star/frame/Desktop.hpp>
22 #include <com/sun/star/frame/XStorable.hpp>
23 #include <com/sun/star/document/MacroExecMode.hpp>
24 #include <com/sun/star/document/XEmbeddedScripts.hpp>
25 #include <com/sun/star/script/XLibraryContainer.hpp>
26 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
27 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
28 #include <com/sun/star/drawing/XShapes.hpp>
29 #include <com/sun/star/drawing/XShape.hpp>
30 #include <com/sun/star/drawing/XShapeGrouper.hpp>
31 #include <com/sun/star/drawing/XControlShape.hpp>
32 #include <com/sun/star/text/XTextDocument.hpp>
33 #include <com/sun/star/text/TextContentAnchorType.hpp>
34 #include <com/sun/star/form/XForm.hpp>
35 #include <com/sun/star/form/XFormsSupplier.hpp>
36 #include <com/sun/star/awt/XControlModel.hpp>
37 #include <com/sun/star/container/XIndexContainer.hpp>
39 #include <i18nutil/searchopt.hxx>
40 #include <sfx2/app.hxx>
41 #include <sfx2/docfilt.hxx>
42 #include <sfx2/docfile.hxx>
43 #include <sfx2/sfxmodelfactory.hxx>
44 #include <svl/intitem.hxx>
45 #include <comphelper/processfactory.hxx>
46 #include <comphelper/propertysequence.hxx>
47 #include <comphelper/scopeguard.hxx>
49 #include <basic/sbxdef.hxx>
50 #include <unotools/tempfile.hxx>
52 #include <unocrsr.hxx>
55 #include <IDocumentLayoutAccess.hxx>
56 #include <IDocumentMarkAccess.hxx>
57 #include <IDocumentUndoRedo.hxx>
58 #include <IDocumentContentOperations.hxx>
60 #include <unotxdoc.hxx>
62 typedef tools::SvRef
<SwDocShell
> SwDocShellRef
;
64 using namespace ::com::sun::star
;
65 using namespace ::com::sun::star::uno
;
67 /* Implementation of Macros test */
69 class SwMacrosTest
: public test::BootstrapFixture
, public unotest::MacrosTest
74 void createFileURL(const OUString
& aFileBase
, const OUString
& aFileExtension
, OUString
& rFilePath
);
76 virtual void setUp() override
;
77 virtual void tearDown() override
;
79 //void testStarBasic();
80 #if !defined(MACOSX) && !defined(_WIN32)
83 void testBookmarkDeleteAndJoin();
84 void testBookmarkDeleteTdf90816();
86 void testControlShapeGrouping();
91 void testFindReplace();
92 CPPUNIT_TEST_SUITE(SwMacrosTest
);
93 #if !defined(MACOSX) && !defined(_WIN32)
94 //enable this test if you want to play with star basic macros in unit tests
95 //works but does nothing useful yet
96 //CPPUNIT_TEST(testStarBasic);
97 CPPUNIT_TEST(testVba
);
99 CPPUNIT_TEST(testBookmarkDeleteAndJoin
);
100 CPPUNIT_TEST(testBookmarkDeleteTdf90816
);
102 CPPUNIT_TEST(testControlShapeGrouping
);
104 CPPUNIT_TEST(testFdo55289
);
105 CPPUNIT_TEST(testFdo68983
);
106 CPPUNIT_TEST(testFdo87530
);
107 CPPUNIT_TEST(testFindReplace
);
109 CPPUNIT_TEST_SUITE_END();
112 uno::Reference
<uno::XInterface
> m_xWriterComponent
;
113 OUString m_aBaseString
;
116 void SwMacrosTest::createFileURL(const OUString
& aFileBase
, const OUString
& aFileExtension
, OUString
& rFilePath
)
119 OUStringBuffer
aBuffer( m_directories
.getSrcRootURL() );
120 aBuffer
.append(m_aBaseString
).append(aSep
).append(aFileExtension
);
121 aBuffer
.append(aSep
).append(aFileBase
).append(aFileExtension
);
122 rFilePath
= aBuffer
.makeStringAndClear();
127 void SwMacrosTest::testStarBasic()
129 const OUString
aFileNameBase("StarBasic.");
130 OUString
aFileExtension(aFileFormats
[0].pName
, strlen(aFileFormats
[0].pName
), RTL_TEXTENCODING_UTF8
);
132 createFileURL(aFileNameBase
, aFileExtension
, aFileName
);
133 uno::Reference
< css::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
, "com.sun.star.text.TextDocument");
135 CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent
.is());
137 OUString
aURL("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document");
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
);
148 pFoundShell
->CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
149 pFoundShell
->DoClose();
154 #if !defined(MACOSX) && !defined(_WIN32)
155 void SwMacrosTest::testVba()
157 TestMacroInfo testInfo
[] = {
159 OUString("testVba."),
160 OUString("vnd.sun.Star.script:Project.NewMacros.Macro1?language=Basic&location=document")
163 for ( sal_uInt32 i
=0; i
<SAL_N_ELEMENTS( testInfo
); ++i
)
166 createFileURL(testInfo
[i
].sFileBaseName
, "doc", aFileName
);
167 uno::Reference
< css::lang::XComponent
> xComponent
= loadFromDesktop(aFileName
, "com.sun.star.text.TextDocument");
168 OUStringBuffer
sMsg( "Failed to load " );
169 sMsg
.append ( aFileName
);
170 CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
).getStr(), xComponent
.is() );
172 OUString sUrl
= testInfo
[i
].sMacroUrl
;
174 Sequence
< sal_Int16
> aOutParamIndex
;
175 Sequence
< Any
> aOutParam
;
176 Sequence
< uno::Any
> aParams
;
178 SfxObjectShell
* pFoundShell
= SfxObjectShell::GetShellFromComponent(xComponent
);
180 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell
);
181 SfxObjectShell::CallXScript(xComponent
, sUrl
, aParams
, aRet
, aOutParamIndex
,aOutParam
);
184 std::cout
<< "value of Ret " << aStringRes
<< std::endl
;
185 //CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
186 pFoundShell
->DoClose();
191 void SwMacrosTest::testBookmarkDeleteAndJoin()
193 SwDoc
*const pDoc
= new SwDoc
;
194 pDoc
->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
195 SwNodeIndex
aIdx(pDoc
->GetNodes().GetEndOfContent(), -1);
198 IDocumentContentOperations
& rIDCO(pDoc
->getIDocumentContentOperations());
199 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
200 rIDCO
.InsertString(aPaM
, "A");
201 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
202 rIDCO
.InsertString(aPaM
, "A");
203 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
204 aPaM
.Move(fnMoveBackward
, GoInNode
);
205 aPaM
.Move(fnMoveBackward
, GoInNode
);
206 aPaM
.Move(fnMoveBackward
, GoInContent
);
208 aPaM
.Move(fnMoveForward
, GoInDoc
);
209 IDocumentMarkAccess
& rIDMA
= *pDoc
->getIDocumentMarkAccess();
210 sw::mark::IMark
*pMark
=
211 rIDMA
.makeMark(aPaM
, "test", IDocumentMarkAccess::MarkType::BOOKMARK
,
212 ::sw::mark::InsertMode::New
);
213 CPPUNIT_ASSERT(pMark
);
214 // select so pMark start position is on a node that is fully deleted
215 aPaM
.Move(fnMoveBackward
, GoInNode
);
216 // must leave un-selected content in last node to get the bJoinPrev flag!
217 aPaM
.Move(fnMoveBackward
, GoInContent
);
219 aPaM
.Move(fnMoveBackward
, GoInDoc
);
221 rIDCO
.DeleteAndJoin(aPaM
);
223 for (IDocumentMarkAccess::const_iterator_t i
= rIDMA
.getAllMarksBegin(); i
!= rIDMA
.getAllMarksEnd(); ++i
)
225 // problem was that the nContent was pointing at deleted node
226 CPPUNIT_ASSERT_EQUAL(static_cast<const SwContentNode
*>((*i
)->GetMarkStart().nContent
.GetIdxReg()),
227 static_cast<const SwContentNode
*>((*i
)->GetMarkStart().nNode
.GetNode().GetContentNode()));
231 void SwMacrosTest::testBookmarkDeleteTdf90816()
233 SwDoc
*const pDoc
= new SwDoc
;
234 pDoc
->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
235 SwNodeIndex
aIdx(pDoc
->GetNodes().GetEndOfContent(), -1);
238 IDocumentContentOperations
& rIDCO(pDoc
->getIDocumentContentOperations());
239 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
240 rIDCO
.InsertString(aPaM
, "ABC");
241 aPaM
.Move(fnMoveBackward
, GoInContent
);
243 aPaM
.Move(fnMoveBackward
, GoInContent
);
244 IDocumentMarkAccess
& rIDMA
= *pDoc
->getIDocumentMarkAccess();
245 sw::mark::IMark
*pMark
=
246 rIDMA
.makeMark(aPaM
, "test", IDocumentMarkAccess::MarkType::BOOKMARK
,
247 ::sw::mark::InsertMode::New
);
248 CPPUNIT_ASSERT(pMark
);
250 // delete the same selection as the bookmark
251 rIDCO
.DeleteAndJoin(aPaM
);
253 // bookmark still there?
254 auto iter
= rIDMA
.getAllMarksBegin();
255 CPPUNIT_ASSERT_MESSAGE("the bookmark was deleted",
256 iter
!= rIDMA
.getAllMarksEnd());
257 CPPUNIT_ASSERT_EQUAL((*iter
)->GetMarkPos(), *aPaM
.Start());
258 CPPUNIT_ASSERT_EQUAL((*iter
)->GetOtherMarkPos(), *aPaM
.End());
262 void SwMacrosTest::testControlShapeGrouping()
265 createFileURL("testControlShapeGrouping.", "odt", aFileName
);
266 Reference
< css::lang::XComponent
> xComponent(
267 loadFromDesktop(aFileName
, "com.sun.star.text.TextDocument"));
268 CPPUNIT_ASSERT(xComponent
.is());
270 uno::Reference
<frame::XModel
> const xModel(xComponent
, UNO_QUERY
);
271 CPPUNIT_ASSERT(xModel
.is());
272 uno::Reference
<lang::XMultiServiceFactory
> xFactory(xModel
, UNO_QUERY
);
273 uno::Reference
<drawing::XDrawPageSupplier
> const xDPS(xModel
, UNO_QUERY
);
274 uno::Reference
<drawing::XDrawPage
> const xDP(xDPS
->getDrawPage(), UNO_QUERY
);
275 CPPUNIT_ASSERT(xDP
.is());
276 uno::Reference
<drawing::XShapes
> const xDPShapes(xDP
, UNO_QUERY
);
277 CPPUNIT_ASSERT(xDPShapes
.is());
278 uno::Reference
<drawing::XShapes
> const xShapes(getMultiServiceFactory()->createInstance("com.sun.star.drawing.ShapeCollection"),
280 CPPUNIT_ASSERT(xShapes
.is());
281 uno::Reference
<container::XIndexAccess
> xShapesIC(xShapes
, UNO_QUERY
);
282 CPPUNIT_ASSERT(xShapesIC
.is());
284 // uno::Reference<beans::XPropertySet> xFormProps(xForm, UNO_QUERY);
285 // xFormProps->setPropertyValue("Name", makeAny("aForm"));
286 uno::Reference
<form::XFormsSupplier
> const xFS(xDP
, UNO_QUERY
);
287 CPPUNIT_ASSERT(xFS
.is());
288 uno::Reference
<container::XIndexContainer
> const xForms(xFS
->getForms(), UNO_QUERY
);
289 CPPUNIT_ASSERT(xForms
.is());
290 uno::Reference
<form::XForm
> xForm(xForms
->getByIndex(0), UNO_QUERY
);
291 CPPUNIT_ASSERT(xForm
.is());
292 uno::Reference
<container::XNameContainer
> xFormNC(xForm
, UNO_QUERY
);
294 uno::Reference
<drawing::XControlShape
> const xDateShape(
295 xFactory
->createInstance("com.sun.star.drawing.ControlShape"),
297 uno::Reference
<awt::XControlModel
> const xDateControlModel(
298 xFactory
->createInstance("com.sun.star.form.component.DateField"),
300 xDateShape
->setControl(xDateControlModel
);
301 uno::Reference
<beans::XPropertySet
> xDateShapeProps(xDateShape
, UNO_QUERY
);
302 xDateShapeProps
->setPropertyValue("AnchorType", makeAny(text::TextContentAnchorType_AT_PARAGRAPH
));
304 uno::Reference
<drawing::XControlShape
> const xTimeShape(
305 xFactory
->createInstance("com.sun.star.drawing.ControlShape"),
307 uno::Reference
<awt::XControlModel
> const xTimeControlModel(
308 xFactory
->createInstance("com.sun.star.form.component.TimeField"),
310 xTimeShape
->setControl(xTimeControlModel
);
311 uno::Reference
<beans::XPropertySet
> xTimeShapeProps(xTimeShape
, UNO_QUERY
);
312 xTimeShapeProps
->setPropertyValue("AnchorType", makeAny(text::TextContentAnchorType_AT_PARAGRAPH
));
314 xFormNC
->insertByName("aDateCntrl", makeAny(xDateControlModel
));
315 xDPShapes
->add(xDateShape
);
316 xFormNC
->insertByName("aTimeCntrl", makeAny(xTimeControlModel
));
317 xDPShapes
->add(xTimeShape
);
319 xShapes
->add(uno::Reference
<drawing::XShape
>(xDateShape
, UNO_QUERY
));
320 xShapes
->add(uno::Reference
<drawing::XShape
>(xTimeShape
, UNO_QUERY
));
321 uno::Reference
<drawing::XShapeGrouper
> const xDPGrouper(xDP
, UNO_QUERY
);
322 CPPUNIT_ASSERT(xDPGrouper
.is());
323 uno::Reference
<drawing::XShapeGroup
> xGroup(xDPGrouper
->group(xShapes
));
324 CPPUNIT_ASSERT(xGroup
.is());
325 uno::Reference
<container::XIndexAccess
> xGroupIC(xGroup
, UNO_QUERY
);
326 CPPUNIT_ASSERT(xGroup
.is());
328 CPPUNIT_ASSERT(xDateShape
->getControl().is());
329 CPPUNIT_ASSERT(xDateShape
->getControl() == xDateControlModel
);
330 CPPUNIT_ASSERT(xTimeShape
->getControl().is());
331 CPPUNIT_ASSERT(xTimeShape
->getControl() == xTimeControlModel
);
334 uno::Reference
< uno::XInterface
> xDI
;
335 xGroupIC
->getByIndex(0) >>= xDI
;
336 CPPUNIT_ASSERT(xDI
.is());
337 uno::Reference
< drawing::XControlShape
> xDS(xDI
, UNO_QUERY
);
338 CPPUNIT_ASSERT(xDS
.is());
339 CPPUNIT_ASSERT(xDS
->getControl() == xDateControlModel
);
341 uno::Reference
< uno::XInterface
> xTI
;
342 xGroupIC
->getByIndex(1) >>= xTI
;
343 CPPUNIT_ASSERT(xTI
.is());
344 uno::Reference
< drawing::XControlShape
> xTS(xTI
, UNO_QUERY
);
345 CPPUNIT_ASSERT(xTS
.is());
346 CPPUNIT_ASSERT(xTS
->getControl() == xTimeControlModel
);
349 uno::Reference
< uno::XInterface
> xDI
;
350 xShapesIC
->getByIndex(0) >>= xDI
;
351 CPPUNIT_ASSERT(xDI
.is());
352 uno::Reference
< drawing::XControlShape
> xDS(xDI
, UNO_QUERY
);
353 CPPUNIT_ASSERT(xDS
.is());
354 CPPUNIT_ASSERT(xDS
->getControl() == xDateControlModel
);
356 uno::Reference
< uno::XInterface
> xTI
;
357 xShapesIC
->getByIndex(1) >>= xTI
;
358 CPPUNIT_ASSERT(xTI
.is());
359 uno::Reference
< drawing::XControlShape
> xTS(xTI
, UNO_QUERY
);
360 CPPUNIT_ASSERT(xTS
.is());
361 CPPUNIT_ASSERT(xTS
->getControl() == xTimeControlModel
);
366 void SwMacrosTest::testFdo55289()
368 SwDoc
*const pDoc
= new SwDoc
;
369 SwDocShellRef pDocShell
= new SwDocShell(pDoc
, SfxObjectCreateMode::EMBEDDED
);
370 // this needs to run with no layout to tickle the bugs in the special
371 // cases in SwXShape re-anchoring
372 assert(!pDoc
->getIDocumentLayoutAccess().GetCurrentLayout());
374 uno::Reference
<frame::XModel
> const xModel(pDocShell
->GetModel());
375 uno::Reference
<drawing::XDrawPageSupplier
> const xDPS(xModel
, UNO_QUERY
);
376 uno::Reference
<drawing::XShapes
> const xShapes(xDPS
->getDrawPage(),
378 uno::Reference
<beans::XPropertySet
> const xShape(
379 uno::Reference
<lang::XMultiServiceFactory
>(xModel
, UNO_QUERY
)->
380 createInstance("com.sun.star.drawing.GraphicObjectShape"),
382 xShape
->setPropertyValue("AnchorType",
383 makeAny(text::TextContentAnchorType_AT_PAGE
));
384 xShapes
->add(uno::Reference
<drawing::XShape
>(xShape
, UNO_QUERY
));
385 xShape
->setPropertyValue("AnchorType",
386 makeAny(text::TextContentAnchorType_AT_CHARACTER
));
387 xShape
->setPropertyValue("AnchorType",
388 makeAny(text::TextContentAnchorType_AS_CHARACTER
));
389 xShape
->setPropertyValue("AnchorType",
390 makeAny(text::TextContentAnchorType_AT_CHARACTER
));
391 xShape
->setPropertyValue("AnchorType",
392 makeAny(text::TextContentAnchorType_AS_CHARACTER
));
393 uno::Reference
<text::XTextRange
> const xEnd
=
394 uno::Reference
<text::XTextDocument
>(xModel
, UNO_QUERY
)->getText()->getEnd();
395 uno::Reference
<text::XTextContent
> const xShapeContent(xShape
, UNO_QUERY
);
396 xShapeContent
->attach(xEnd
);
399 void SwMacrosTest::testFdo68983()
402 createFileURL("fdo68983.", "odt", aFileName
);
403 Reference
< css::lang::XComponent
> xComponent
=
404 loadFromDesktop(aFileName
, "com.sun.star.text.TextDocument");
406 CPPUNIT_ASSERT_MESSAGE("Failed to load fdo68983.odt", xComponent
.is());
408 Reference
< frame::XStorable
> xDocStorable(xComponent
, UNO_QUERY_THROW
);
409 CPPUNIT_ASSERT(xDocStorable
.is());
411 utl::TempFile aTempFile
;
412 aTempFile
.EnableKillingFile();
413 Sequence
<beans::PropertyValue
> desc( comphelper::InitPropertySequence({
414 { "FilterName", Any(OUString("writer8")) }
416 xDocStorable
->storeAsURL(aTempFile
.GetURL(), desc
);
418 Reference
<util::XCloseable
>(xComponent
, UNO_QUERY_THROW
)->close(false);
421 xComponent
= loadFromDesktop(aTempFile
.GetURL(), "com.sun.star.text.TextDocument");
423 // check that password-protected library survived store and re-load
424 Reference
<document::XEmbeddedScripts
> xDocScr(xComponent
, UNO_QUERY_THROW
);
425 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
426 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
427 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
428 CPPUNIT_ASSERT(xBasLibPwd
->isLibraryPasswordProtected("Library1"));
429 CPPUNIT_ASSERT(xBasLibPwd
->verifyLibraryPassword("Library1", "foo"));
430 xBasLib
->loadLibrary("Library1");
431 CPPUNIT_ASSERT(xBasLib
->isLibraryLoaded("Library1"));
434 Reference
<util::XCloseable
> xDocCloseable(xComponent
, UNO_QUERY_THROW
);
435 xDocCloseable
->close(false);
438 void SwMacrosTest::testFdo87530()
440 Reference
<css::lang::XComponent
> xComponent
=
441 loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
443 utl::TempFile aTempFile
;
444 aTempFile
.EnableKillingFile();
446 Sequence
<beans::PropertyValue
> desc( comphelper::InitPropertySequence({
447 { "FilterName", Any(OUString("writer8")) }
451 // insert initial password protected library
452 Reference
<document::XEmbeddedScripts
> xDocScr(xComponent
, UNO_QUERY_THROW
);
453 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
454 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
455 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
456 Reference
<container::XNameContainer
> xLibrary(xBasLib
->createLibrary("BarLibrary"));
457 xLibrary
->insertByName("BarModule",
458 uno::makeAny(OUString("Sub Main\nEnd Sub\n")));
459 xBasLibPwd
->changeLibraryPassword("BarLibrary", "", "foo");
461 Reference
<frame::XStorable
> xDocStorable(xComponent
, UNO_QUERY_THROW
);
462 CPPUNIT_ASSERT(xDocStorable
.is());
464 xDocStorable
->storeAsURL(aTempFile
.GetURL(), desc
);
467 Reference
<util::XCloseable
>(xComponent
, UNO_QUERY_THROW
)->close(false);
470 xComponent
= loadFromDesktop(aTempFile
.GetURL(), "com.sun.star.text.TextDocument");
473 // check that password-protected library survived store and re-load
474 Reference
<document::XEmbeddedScripts
> xDocScr(xComponent
, UNO_QUERY_THROW
);
475 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
476 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
477 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
478 CPPUNIT_ASSERT(xBasLibPwd
->isLibraryPasswordProtected("BarLibrary"));
479 CPPUNIT_ASSERT(xBasLibPwd
->verifyLibraryPassword("BarLibrary", "foo"));
480 xBasLib
->loadLibrary("BarLibrary");
481 CPPUNIT_ASSERT(xBasLib
->isLibraryLoaded("BarLibrary"));
482 Reference
<container::XNameContainer
> xLibrary(xBasLib
->getByName("BarLibrary"), UNO_QUERY
);
483 Any
module(xLibrary
->getByName("BarModule"));
484 CPPUNIT_ASSERT_EQUAL(module
.get
<OUString
>(), OUString("Sub Main\nEnd Sub\n"));
486 // add a second module now - tdf#87530 happened here
487 Reference
<container::XNameContainer
> xFooLib(xBasLib
->createLibrary("FooLibrary"));
488 xFooLib
->insertByName("FooModule",
489 uno::makeAny(OUString("Sub Main\nEnd Sub\n")));
490 xBasLibPwd
->changeLibraryPassword("FooLibrary", "", "foo");
493 Reference
<frame::XStorable
> xDocStorable(xComponent
, UNO_QUERY_THROW
);
494 CPPUNIT_ASSERT(xDocStorable
.is());
496 xDocStorable
->store();
499 Reference
<util::XCloseable
>(xComponent
, UNO_QUERY_THROW
)->close(false);
502 xComponent
= loadFromDesktop(aTempFile
.GetURL(), "com.sun.star.text.TextDocument");
504 // check that password-protected library survived store and re-load
505 Reference
<document::XEmbeddedScripts
> xDocScr(xComponent
, UNO_QUERY_THROW
);
506 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
507 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
508 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
509 CPPUNIT_ASSERT(xBasLibPwd
->isLibraryPasswordProtected("FooLibrary"));
510 CPPUNIT_ASSERT(xBasLibPwd
->verifyLibraryPassword("FooLibrary", "foo"));
511 xBasLib
->loadLibrary("FooLibrary");
512 CPPUNIT_ASSERT(xBasLib
->isLibraryLoaded("FooLibrary"));
513 Reference
<container::XNameContainer
> xLibrary(xBasLib
->getByName("FooLibrary"), UNO_QUERY
);
514 Any
module(xLibrary
->getByName("FooModule"));
515 CPPUNIT_ASSERT_EQUAL(module
.get
<OUString
>(), OUString("Sub Main\nEnd Sub\n"));
518 Reference
<util::XCloseable
>(xComponent
, UNO_QUERY_THROW
)->close(false);
522 void SwMacrosTest::testFindReplace()
524 // we need a full document with view and layout etc. because ::GetNode()
525 Reference
<lang::XComponent
> const xComponent
=
526 loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
528 const ::comphelper::ScopeGuard
aComponentScopeGuard(
529 [&xComponent
]() { xComponent
->dispose(); } );
531 SwXTextDocument
*const pTextDoc
= dynamic_cast<SwXTextDocument
*>(xComponent
.get());
532 CPPUNIT_ASSERT(pTextDoc
);
533 SwDoc
*const pDoc
= pTextDoc
->GetDocShell()->GetDoc();
534 SwNodeIndex
aIdx(pDoc
->GetNodes().GetEndOfContent(), -1);
535 // use a UnoCursor so it will be corrected when deleting nodes
536 auto pPaM(pDoc
->CreateUnoCursor(SwPosition(aIdx
)));
538 IDocumentContentOperations
& rIDCO(pDoc
->getIDocumentContentOperations());
539 rIDCO
.InsertString(*pPaM
, "foo");
540 rIDCO
.AppendTextNode(*pPaM
->GetPoint());
541 rIDCO
.InsertString(*pPaM
, "bar");
542 rIDCO
.AppendTextNode(*pPaM
->GetPoint());
543 rIDCO
.InsertString(*pPaM
, "baz");
544 pPaM
->Move(fnMoveBackward
, GoInDoc
);
547 i18nutil::SearchOptions2
opts(
548 util::SearchAlgorithms_REGEXP
,
552 lang::Locale("en", "US", ""),
556 TransliterationFlags::IGNORE_CASE
| TransliterationFlags::IGNORE_WIDTH
|
557 TransliterationFlags::IGNORE_KASHIDA_CTL
| TransliterationFlags::IGNORE_DIACRITICS_CTL
,
558 util::SearchAlgorithms2::REGEXP
,
561 // find newline on 1st paragraph
562 bool bFound
= pPaM
->Find(
563 opts
, false, SwDocPositions::Curr
, SwDocPositions::End
, bCancel
, FindRanges::InBody
);
564 CPPUNIT_ASSERT(bFound
);
565 CPPUNIT_ASSERT(pPaM
->HasMark());
566 CPPUNIT_ASSERT_EQUAL(OUString(), pPaM
->GetText());
568 // now do another Find, inside the selection from the first Find
569 // opts.searchFlags = 71680;
571 opts
, false, SwDocPositions::Curr
, SwDocPositions::End
, bCancel
, FindRanges::InSel
);
572 CPPUNIT_ASSERT(bFound
);
573 CPPUNIT_ASSERT(pPaM
->HasMark());
574 CPPUNIT_ASSERT_EQUAL(OUString(), pPaM
->GetText());
576 rIDCO
.ReplaceRange(*pPaM
, " ", true);
579 pPaM
->Move(fnMoveBackward
, GoInDoc
);
581 // problem was that after the 2nd Find, the wrong newline was selected
582 CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
583 pPaM
->Start()->nNode
.GetNode().GetTextNode()->GetText());
584 pPaM
->Move(fnMoveForward
, GoInNode
);
585 CPPUNIT_ASSERT_EQUAL(OUString("baz"),
586 pPaM
->End()->nNode
.GetNode().GetTextNode()->GetText());
589 SwMacrosTest::SwMacrosTest()
590 : m_aBaseString("/sw/qa/core/data")
594 void SwMacrosTest::setUp()
596 test::BootstrapFixture::setUp();
598 // This is a bit of a fudge, we do this to ensure that SwGlobals::ensure,
599 // which is a private symbol to us, gets called
601 getMultiServiceFactory()->createInstance("com.sun.star.comp.Writer.TextDocument");
602 CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xWriterComponent
.is());
603 mxDesktop
= css::frame::Desktop::create( comphelper::getComponentContext(getMultiServiceFactory()) );
606 void SwMacrosTest::tearDown()
608 uno::Reference
< lang::XComponent
>( m_xWriterComponent
, UNO_QUERY_THROW
)->dispose();
609 test::BootstrapFixture::tearDown();
612 CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest
);
614 CPPUNIT_PLUGIN_IMPLEMENT();
616 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */