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>
12 #include <string_view>
14 #include <test/unoapi_test.hxx>
16 #include <com/sun/star/beans/XPropertySet.hpp>
17 #include <com/sun/star/container/XIndexContainer.hpp>
18 #include <com/sun/star/container/XNameContainer.hpp>
19 #include <com/sun/star/document/XEmbeddedScripts.hpp>
20 #include <com/sun/star/drawing/XControlShape.hpp>
21 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
22 #include <com/sun/star/drawing/XShapeGrouper.hpp>
23 #include <com/sun/star/drawing/XShape.hpp>
24 #include <com/sun/star/drawing/XShapes.hpp>
25 #include <com/sun/star/form/XForm.hpp>
26 #include <com/sun/star/form/XFormsSupplier.hpp>
27 #include <com/sun/star/frame/Desktop.hpp>
28 #include <com/sun/star/frame/XStorable.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/script/XLibraryContainer.hpp>
31 #include <com/sun/star/script/XLibraryContainerPassword.hpp>
32 #include <com/sun/star/text/TextContentAnchorType.hpp>
33 #include <com/sun/star/text/XTextDocument.hpp>
34 #include <com/sun/star/util/SearchAlgorithms2.hpp>
36 #include <i18nutil/searchopt.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <comphelper/propertysequence.hxx>
39 #include <comphelper/scopeguard.hxx>
41 #include <unotools/tempfile.hxx>
43 #include <unocrsr.hxx>
46 #include <IDocumentLayoutAccess.hxx>
47 #include <IDocumentMarkAccess.hxx>
48 #include <IDocumentUndoRedo.hxx>
49 #include <IDocumentContentOperations.hxx>
51 #include <unotxdoc.hxx>
53 typedef tools::SvRef
<SwDocShell
> SwDocShellRef
;
55 using namespace ::com::sun::star
;
56 using namespace ::com::sun::star::uno
;
58 /* Implementation of Macros test */
60 class SwMacrosTest
: public UnoApiTest
66 void testModernVBADelete();
67 void testBookmarkDeleteAndJoin();
68 void testBookmarkDeleteTdf90816();
69 void testControlShapeGrouping();
74 void testFindReplace();
76 CPPUNIT_TEST_SUITE(SwMacrosTest
);
77 CPPUNIT_TEST(testVba
);
78 CPPUNIT_TEST(testModernVBADelete
);
79 CPPUNIT_TEST(testBookmarkDeleteAndJoin
);
80 CPPUNIT_TEST(testBookmarkDeleteTdf90816
);
81 CPPUNIT_TEST(testControlShapeGrouping
);
82 CPPUNIT_TEST(testTdf151846
);
83 CPPUNIT_TEST(testFdo55289
);
84 CPPUNIT_TEST(testFdo68983
);
85 CPPUNIT_TEST(testFdo87530
);
86 CPPUNIT_TEST(testFindReplace
);
87 CPPUNIT_TEST_SUITE_END();
90 void SwMacrosTest::testVba()
92 TestMacroInfo testInfo
[] = {
94 OUString("testVBA.docm"),
95 OUString("vnd.sun.Star.script:Project.ThisDocument.testAll?language=Basic&location=document")
98 OUString("testModernVBA.docm"),
99 OUString("vnd.sun.Star.script:Project.ThisDocument.testAll?language=Basic&location=document")
102 OUString("testFind.docm"),
103 OUString("vnd.sun.Star.script:Project.Module1.testAll?language=Basic&location=document")
106 OUString("testDocumentRange.docm"),
107 OUString("vnd.sun.Star.script:Project.Module1.testAll?language=Basic&location=document")
110 OUString("testSelectionFind.docm"),
111 OUString("vnd.sun.Star.script:Project.Module1.testAll?language=Basic&location=document")
114 //current working tests here!
116 OUString("testFontColor.docm"),
117 OUString("vnd.sun.Star.script:Project.ThisDocument.testAll?language=Basic&location=document")
119 // TODO - make these pass in Writer
121 OUString("testSentences.docm"),
122 OUString("vnd.sun.Star.script:Project.ThisDocument.TestAll?language=Basic&location=document")
125 OUString("testWords.docm"),
126 OUString("vnd.sun.Star.script:Project.ThisDocument.TestAll?language=Basic&location=document")
129 OUString("testParagraphFormat.docm"),
130 OUString("vnd.sun.Star.script:Project.ThisDocument.TestAll?language=Basic&location=document")
133 OUString("testTables.docm"),
134 OUString("vnd.sun.Star.script:Project.ThisDocument.TestAll?language=Basic&location=document")
138 for (auto const & [ sFileBaseName
, sMacroUrl
] : testInfo
)
140 OUString
sFileName("docm/" + sFileBaseName
);
141 loadFromURL(sFileName
);
143 uno::Any aRet
= executeMacro(sMacroUrl
);
145 CPPUNIT_ASSERT_MESSAGE(sFileName
.toUtf8().getStr(), aRet
>>= aStringRes
);
146 CPPUNIT_ASSERT_EQUAL(OUString("OK"), aStringRes
);
150 void SwMacrosTest::testModernVBADelete()
152 TestMacroInfo testInfo
=
154 OUString("testModernVBADelete.docm"),
155 OUString("vnd.sun.Star.script:Project.ThisDocument.testAll?language=Basic&location=document")
158 OUString
sFileName("docm/" + testInfo
.sFileBaseName
);
159 loadFromURL(sFileName
);
161 SwXTextDocument
*const pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
162 SwDoc
*const pDoc
= pTextDoc
->GetDocShell()->GetDoc();
163 pDoc
->GetIDocumentUndoRedo().DoUndo(true);
164 CPPUNIT_ASSERT(!pDoc
->GetIDocumentUndoRedo().GetUndoActionCount());
166 uno::Any aRet
= executeMacro(testInfo
.sMacroUrl
);
167 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc
->GetIDocumentUndoRedo().GetUndoActionCount());
170 CPPUNIT_ASSERT(aRet
>>= aStringRes
);
171 CPPUNIT_ASSERT_EQUAL(OUString("OK"), aStringRes
);
174 void SwMacrosTest::testBookmarkDeleteAndJoin()
176 rtl::Reference
<SwDoc
> const pDoc(new SwDoc
);
177 pDoc
->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
178 SwNodeIndex
aIdx(pDoc
->GetNodes().GetEndOfContent(), -1);
181 IDocumentContentOperations
& rIDCO(pDoc
->getIDocumentContentOperations());
182 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
183 rIDCO
.InsertString(aPaM
, "A");
184 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
185 rIDCO
.InsertString(aPaM
, "A");
186 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
187 aPaM
.Move(fnMoveBackward
, GoInNode
);
188 aPaM
.Move(fnMoveBackward
, GoInNode
);
189 aPaM
.Move(fnMoveBackward
, GoInContent
);
191 aPaM
.Move(fnMoveForward
, GoInDoc
);
192 IDocumentMarkAccess
& rIDMA
= *pDoc
->getIDocumentMarkAccess();
193 sw::mark::IMark
*pMark
=
194 rIDMA
.makeMark(aPaM
, "test", IDocumentMarkAccess::MarkType::BOOKMARK
,
195 ::sw::mark::InsertMode::New
);
196 CPPUNIT_ASSERT(pMark
);
197 // select so pMark start position is on a node that is fully deleted
198 aPaM
.Move(fnMoveBackward
, GoInNode
);
199 // must leave un-selected content in last node to get the bJoinPrev flag!
200 aPaM
.Move(fnMoveBackward
, GoInContent
);
202 aPaM
.Move(fnMoveBackward
, GoInDoc
);
204 rIDCO
.DeleteAndJoin(aPaM
);
206 for (IDocumentMarkAccess::const_iterator_t i
= rIDMA
.getAllMarksBegin(); i
!= rIDMA
.getAllMarksEnd(); ++i
)
208 // problem was that the nContent was pointing at deleted node
209 CPPUNIT_ASSERT_EQUAL((*i
)->GetMarkStart().GetContentNode(),
210 static_cast<const SwContentNode
*>((*i
)->GetMarkStart().GetNode().GetContentNode()));
214 void SwMacrosTest::testBookmarkDeleteTdf90816()
216 rtl::Reference
<SwDoc
> const pDoc(new SwDoc
);
217 pDoc
->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
218 SwNodeIndex
aIdx(pDoc
->GetNodes().GetEndOfContent(), -1);
221 IDocumentContentOperations
& rIDCO(pDoc
->getIDocumentContentOperations());
222 rIDCO
.AppendTextNode(*aPaM
.GetPoint());
223 rIDCO
.InsertString(aPaM
, "ABC");
224 aPaM
.Move(fnMoveBackward
, GoInContent
);
226 aPaM
.Move(fnMoveBackward
, GoInContent
);
227 IDocumentMarkAccess
& rIDMA
= *pDoc
->getIDocumentMarkAccess();
228 sw::mark::IMark
*pMark
=
229 rIDMA
.makeMark(aPaM
, "test", IDocumentMarkAccess::MarkType::BOOKMARK
,
230 ::sw::mark::InsertMode::New
);
231 CPPUNIT_ASSERT(pMark
);
233 // delete the same selection as the bookmark
234 rIDCO
.DeleteAndJoin(aPaM
);
236 // bookmark still there?
237 auto iter
= rIDMA
.getAllMarksBegin();
238 CPPUNIT_ASSERT_MESSAGE("the bookmark was deleted",
239 iter
!= rIDMA
.getAllMarksEnd());
240 CPPUNIT_ASSERT_EQUAL((*iter
)->GetMarkPos(), *aPaM
.Start());
241 CPPUNIT_ASSERT_EQUAL((*iter
)->GetOtherMarkPos(), *aPaM
.End());
244 void SwMacrosTest::testControlShapeGrouping()
246 loadFromURL(u
"odt/testControlShapeGrouping.odt");
248 uno::Reference
<frame::XModel
> const xModel(mxComponent
, UNO_QUERY
);
249 CPPUNIT_ASSERT(xModel
.is());
250 uno::Reference
<lang::XMultiServiceFactory
> xFactory(xModel
, UNO_QUERY
);
251 uno::Reference
<drawing::XDrawPageSupplier
> const xDPS(xModel
, UNO_QUERY
);
252 uno::Reference
<drawing::XDrawPage
> const xDP
= xDPS
->getDrawPage();
253 CPPUNIT_ASSERT(xDP
.is());
254 uno::Reference
<drawing::XShapes
> const xDPShapes
= xDP
;
255 CPPUNIT_ASSERT(xDPShapes
.is());
256 uno::Reference
<drawing::XShapes
> const xShapes(getMultiServiceFactory()->createInstance("com.sun.star.drawing.ShapeCollection"),
258 CPPUNIT_ASSERT(xShapes
.is());
259 uno::Reference
<container::XIndexAccess
> xShapesIC
= xShapes
;
260 CPPUNIT_ASSERT(xShapesIC
.is());
262 // uno::Reference<beans::XPropertySet> xFormProps(xForm, UNO_QUERY);
263 // xFormProps->setPropertyValue("Name", makeAny("aForm"));
264 uno::Reference
<form::XFormsSupplier
> const xFS(xDP
, UNO_QUERY
);
265 CPPUNIT_ASSERT(xFS
.is());
266 uno::Reference
<container::XIndexContainer
> const xForms(xFS
->getForms(), UNO_QUERY
);
267 CPPUNIT_ASSERT(xForms
.is());
268 uno::Reference
<form::XForm
> xForm(xForms
->getByIndex(0), UNO_QUERY
);
269 CPPUNIT_ASSERT(xForm
.is());
270 uno::Reference
<container::XNameContainer
> xFormNC(xForm
, UNO_QUERY
);
272 uno::Reference
<drawing::XControlShape
> const xDateShape(
273 xFactory
->createInstance("com.sun.star.drawing.ControlShape"),
275 uno::Reference
<awt::XControlModel
> const xDateControlModel(
276 xFactory
->createInstance("com.sun.star.form.component.DateField"),
278 xDateShape
->setControl(xDateControlModel
);
279 uno::Reference
<beans::XPropertySet
> xDateShapeProps(xDateShape
, UNO_QUERY
);
280 xDateShapeProps
->setPropertyValue("AnchorType", Any(text::TextContentAnchorType_AT_PARAGRAPH
));
282 uno::Reference
<drawing::XControlShape
> const xTimeShape(
283 xFactory
->createInstance("com.sun.star.drawing.ControlShape"),
285 uno::Reference
<awt::XControlModel
> const xTimeControlModel(
286 xFactory
->createInstance("com.sun.star.form.component.TimeField"),
288 xTimeShape
->setControl(xTimeControlModel
);
289 uno::Reference
<beans::XPropertySet
> xTimeShapeProps(xTimeShape
, UNO_QUERY
);
290 xTimeShapeProps
->setPropertyValue("AnchorType", Any(text::TextContentAnchorType_AT_PARAGRAPH
));
292 xFormNC
->insertByName("aDateCntrl", Any(xDateControlModel
));
293 xDPShapes
->add(xDateShape
);
294 xFormNC
->insertByName("aTimeCntrl", Any(xTimeControlModel
));
295 xDPShapes
->add(xTimeShape
);
297 xShapes
->add(xDateShape
);
298 xShapes
->add(xTimeShape
);
299 uno::Reference
<drawing::XShapeGrouper
> const xDPGrouper(xDP
, UNO_QUERY
);
300 CPPUNIT_ASSERT(xDPGrouper
.is());
301 uno::Reference
<drawing::XShapeGroup
> xGroup(xDPGrouper
->group(xShapes
));
302 CPPUNIT_ASSERT(xGroup
.is());
305 uno::Reference
<container::XIndexAccess
> xGroupIC(xGroup
, UNO_QUERY
);
306 CPPUNIT_ASSERT(xDateShape
->getControl().is());
307 CPPUNIT_ASSERT_EQUAL(xDateShape
->getControl(), xDateControlModel
);
308 CPPUNIT_ASSERT(xTimeShape
->getControl().is());
309 CPPUNIT_ASSERT_EQUAL(xTimeShape
->getControl(), xTimeControlModel
);
312 uno::Reference
< uno::XInterface
> xDI
;
313 xGroupIC
->getByIndex(0) >>= xDI
;
314 CPPUNIT_ASSERT(xDI
.is());
315 uno::Reference
< drawing::XControlShape
> xDS(xDI
, UNO_QUERY
);
316 CPPUNIT_ASSERT(xDS
.is());
317 CPPUNIT_ASSERT_EQUAL(xDS
->getControl(), xDateControlModel
);
319 uno::Reference
< uno::XInterface
> xTI
;
320 xGroupIC
->getByIndex(1) >>= xTI
;
321 CPPUNIT_ASSERT(xTI
.is());
322 uno::Reference
< drawing::XControlShape
> xTS(xTI
, UNO_QUERY
);
323 CPPUNIT_ASSERT(xTS
.is());
324 CPPUNIT_ASSERT_EQUAL(xTS
->getControl(), xTimeControlModel
);
327 uno::Reference
< uno::XInterface
> xDI
;
328 xShapesIC
->getByIndex(0) >>= xDI
;
329 CPPUNIT_ASSERT(xDI
.is());
330 uno::Reference
< drawing::XControlShape
> xDS(xDI
, UNO_QUERY
);
331 CPPUNIT_ASSERT(xDS
.is());
332 CPPUNIT_ASSERT_EQUAL(xDS
->getControl(), xDateControlModel
);
334 uno::Reference
< uno::XInterface
> xTI
;
335 xShapesIC
->getByIndex(1) >>= xTI
;
336 CPPUNIT_ASSERT(xTI
.is());
337 uno::Reference
< drawing::XControlShape
> xTS(xTI
, UNO_QUERY
);
338 CPPUNIT_ASSERT(xTS
.is());
339 CPPUNIT_ASSERT_EQUAL(xTS
->getControl(), xTimeControlModel
);
344 void SwMacrosTest::testTdf151846()
346 loadFromURL(u
"odt/tdf151846.odt");
348 // Without the fix in place, this test would have failed with
349 // Property or method not found: createDiagramByDataSource.
350 executeMacro("vnd.sun.Star.script:Standard.Module1.Main?language=Basic&location=document");
352 uno::Reference
<text::XTextEmbeddedObjectsSupplier
> xTEOSupplier(mxComponent
, uno::UNO_QUERY
);
353 uno::Reference
<container::XNameAccess
> xAccess(xTEOSupplier
->getEmbeddedObjects());
354 uno::Sequence
<OUString
> aSeq(xAccess
->getElementNames());
356 // Check number of embedded objects.
357 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), aSeq
.getLength());
360 void SwMacrosTest::testFdo55289()
362 SwDoc
* const pDoc
= new SwDoc
;
363 SwDocShellRef pDocShell
= new SwDocShell(*pDoc
, SfxObjectCreateMode::EMBEDDED
);
364 // this needs to run with no layout to tickle the bugs in the special
365 // cases in SwXShape re-anchoring
366 assert(!pDoc
->getIDocumentLayoutAccess().GetCurrentLayout());
368 uno::Reference
<frame::XModel
> const xModel(pDocShell
->GetModel());
369 uno::Reference
<drawing::XDrawPageSupplier
> const xDPS(xModel
, UNO_QUERY
);
370 uno::Reference
<drawing::XShapes
> const xShapes
= xDPS
->getDrawPage();
371 uno::Reference
<beans::XPropertySet
> const xShape(
372 uno::Reference
<lang::XMultiServiceFactory
>(xModel
, UNO_QUERY_THROW
)->
373 createInstance("com.sun.star.drawing.GraphicObjectShape"),
375 xShape
->setPropertyValue("AnchorType",
376 Any(text::TextContentAnchorType_AT_PAGE
));
377 xShapes
->add(uno::Reference
<drawing::XShape
>(xShape
, UNO_QUERY
));
378 xShape
->setPropertyValue("AnchorType",
379 Any(text::TextContentAnchorType_AT_CHARACTER
));
380 xShape
->setPropertyValue("AnchorType",
381 Any(text::TextContentAnchorType_AS_CHARACTER
));
382 xShape
->setPropertyValue("AnchorType",
383 Any(text::TextContentAnchorType_AT_CHARACTER
));
384 xShape
->setPropertyValue("AnchorType",
385 Any(text::TextContentAnchorType_AS_CHARACTER
));
386 uno::Reference
<text::XTextRange
> const xEnd
=
387 uno::Reference
<text::XTextDocument
>(xModel
, UNO_QUERY_THROW
)->getText()->getEnd();
388 uno::Reference
<text::XTextContent
> const xShapeContent(xShape
, UNO_QUERY
);
389 xShapeContent
->attach(xEnd
);
390 pDocShell
->DoClose();
393 void SwMacrosTest::testFdo68983()
395 loadFromURL(u
"odt/fdo68983.odt");
396 Reference
< frame::XStorable
> xDocStorable(mxComponent
, UNO_QUERY_THROW
);
398 saveAndReload("writer8");
400 // check that password-protected library survived store and re-load
401 Reference
<document::XEmbeddedScripts
> xDocScr(mxComponent
, UNO_QUERY_THROW
);
402 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
403 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
404 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
405 CPPUNIT_ASSERT(xBasLibPwd
->isLibraryPasswordProtected("Library1"));
406 CPPUNIT_ASSERT(xBasLibPwd
->verifyLibraryPassword("Library1", "foo"));
407 xBasLib
->loadLibrary("Library1");
408 CPPUNIT_ASSERT(xBasLib
->isLibraryLoaded("Library1"));
411 void SwMacrosTest::testFdo87530()
413 mxComponent
= loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
416 // insert initial password protected library
417 Reference
<document::XEmbeddedScripts
> xDocScr(mxComponent
, UNO_QUERY_THROW
);
418 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
419 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
420 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
421 Reference
<container::XNameContainer
> xLibrary(xBasLib
->createLibrary("BarLibrary"));
422 xLibrary
->insertByName("BarModule",
423 uno::Any(OUString("Sub Main\nEnd Sub\n")));
424 xBasLibPwd
->changeLibraryPassword("BarLibrary", "", "foo");
427 saveAndReload("writer8");
430 // check that password-protected library survived store and re-load
431 Reference
<document::XEmbeddedScripts
> xDocScr(mxComponent
, UNO_QUERY_THROW
);
432 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
433 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
434 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
435 CPPUNIT_ASSERT(xBasLibPwd
->isLibraryPasswordProtected("BarLibrary"));
436 CPPUNIT_ASSERT(xBasLibPwd
->verifyLibraryPassword("BarLibrary", "foo"));
437 xBasLib
->loadLibrary("BarLibrary");
438 CPPUNIT_ASSERT(xBasLib
->isLibraryLoaded("BarLibrary"));
439 Reference
<container::XNameContainer
> xLibrary(xBasLib
->getByName("BarLibrary"), UNO_QUERY
);
440 Any
module(xLibrary
->getByName("BarModule"));
441 CPPUNIT_ASSERT_EQUAL(OUString("Sub Main\nEnd Sub\n"), module
.get
<OUString
>());
443 // add a second module now - tdf#87530 happened here
444 Reference
<container::XNameContainer
> xFooLib(xBasLib
->createLibrary("FooLibrary"));
445 xFooLib
->insertByName("FooModule",
446 uno::Any(OUString("Sub Main\nEnd Sub\n")));
447 xBasLibPwd
->changeLibraryPassword("FooLibrary", "", "foo");
450 saveAndReload("writer8");
452 // check that password-protected library survived store and re-load
453 Reference
<document::XEmbeddedScripts
> xDocScr(mxComponent
, UNO_QUERY_THROW
);
454 Reference
<script::XStorageBasedLibraryContainer
> xStorBasLib(xDocScr
->getBasicLibraries());
455 Reference
<script::XLibraryContainer
> xBasLib(xStorBasLib
, UNO_QUERY_THROW
);
456 Reference
<script::XLibraryContainerPassword
> xBasLibPwd(xStorBasLib
, UNO_QUERY_THROW
);
457 CPPUNIT_ASSERT(xBasLibPwd
->isLibraryPasswordProtected("FooLibrary"));
458 CPPUNIT_ASSERT(xBasLibPwd
->verifyLibraryPassword("FooLibrary", "foo"));
459 xBasLib
->loadLibrary("FooLibrary");
460 CPPUNIT_ASSERT(xBasLib
->isLibraryLoaded("FooLibrary"));
461 Reference
<container::XNameContainer
> xLibrary(xBasLib
->getByName("FooLibrary"), UNO_QUERY
);
462 Any
module(xLibrary
->getByName("FooModule"));
463 CPPUNIT_ASSERT_EQUAL(OUString("Sub Main\nEnd Sub\n"), module
.get
<OUString
>());
467 void SwMacrosTest::testFindReplace()
469 // we need a full document with view and layout etc. because ::GetNode()
471 loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
473 SwXTextDocument
*const pTextDoc
= dynamic_cast<SwXTextDocument
*>(mxComponent
.get());
474 CPPUNIT_ASSERT(pTextDoc
);
475 SwDoc
*const pDoc
= pTextDoc
->GetDocShell()->GetDoc();
476 SwNodeIndex
aIdx(pDoc
->GetNodes().GetEndOfContent(), -1);
477 // use a UnoCursor so it will be corrected when deleting nodes
478 auto pPaM(pDoc
->CreateUnoCursor(SwPosition(aIdx
)));
480 IDocumentContentOperations
& rIDCO(pDoc
->getIDocumentContentOperations());
481 rIDCO
.InsertString(*pPaM
, "foo");
482 rIDCO
.AppendTextNode(*pPaM
->GetPoint());
483 rIDCO
.InsertString(*pPaM
, "bar");
484 rIDCO
.AppendTextNode(*pPaM
->GetPoint());
485 rIDCO
.InsertString(*pPaM
, "baz");
486 pPaM
->Move(fnMoveBackward
, GoInDoc
);
489 i18nutil::SearchOptions2
opts(
490 util::SearchAlgorithms_REGEXP
,
494 lang::Locale("en", "US", ""),
498 TransliterationFlags::IGNORE_CASE
| TransliterationFlags::IGNORE_WIDTH
|
499 TransliterationFlags::IGNORE_KASHIDA_CTL
| TransliterationFlags::IGNORE_DIACRITICS_CTL
,
500 util::SearchAlgorithms2::REGEXP
,
503 // find newline on 1st paragraph
504 bool bFound
= pPaM
->Find_Text(
505 opts
, false, SwDocPositions::Curr
, SwDocPositions::End
, bCancel
, FindRanges::InBody
);
506 CPPUNIT_ASSERT(bFound
);
507 CPPUNIT_ASSERT(pPaM
->HasMark());
508 CPPUNIT_ASSERT(pPaM
->GetPoint()->GetNode() != pPaM
->GetMark()->GetNode());
509 CPPUNIT_ASSERT_EQUAL(OUString(OUStringChar(CH_TXTATR_NEWLINE
)), pPaM
->GetText());
511 // now do another Find, inside the selection from the first Find
512 // opts.searchFlags = 71680;
513 bFound
= pPaM
->Find_Text(
514 opts
, false, SwDocPositions::Curr
, SwDocPositions::End
, bCancel
, FindRanges::InSel
);
515 CPPUNIT_ASSERT(bFound
);
516 CPPUNIT_ASSERT(pPaM
->HasMark());
517 CPPUNIT_ASSERT(pPaM
->GetPoint()->GetNode() != pPaM
->GetMark()->GetNode());
518 CPPUNIT_ASSERT_EQUAL(OUString(OUStringChar(CH_TXTATR_NEWLINE
)), pPaM
->GetText());
520 rIDCO
.ReplaceRange(*pPaM
, " ", true);
523 pPaM
->Move(fnMoveBackward
, GoInDoc
);
525 // problem was that after the 2nd Find, the wrong newline was selected
526 CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
527 pPaM
->Start()->GetNode().GetTextNode()->GetText());
528 pPaM
->Move(fnMoveForward
, GoInNode
);
529 CPPUNIT_ASSERT_EQUAL(OUString("baz"),
530 pPaM
->End()->GetNode().GetTextNode()->GetText());
533 SwMacrosTest::SwMacrosTest()
534 : UnoApiTest("/sw/qa/core/data/")
538 CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest
);
540 CPPUNIT_PLUGIN_IMPLEMENT();
542 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */