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/util/SearchOptions.hpp>
22 : #include <com/sun/star/frame/Desktop.hpp>
23 : #include <com/sun/star/frame/XComponentLoader.hpp>
24 : #include <com/sun/star/frame/XStorable.hpp>
25 : #include <com/sun/star/document/MacroExecMode.hpp>
26 : #include <com/sun/star/document/XEmbeddedScripts.hpp>
27 : #include <com/sun/star/script/XLibraryContainer.hpp>
28 : #include <com/sun/star/script/XLibraryContainerPassword.hpp>
29 : #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
30 : #include <com/sun/star/drawing/XShapes.hpp>
31 : #include <com/sun/star/drawing/XShape.hpp>
32 : #include <com/sun/star/drawing/XShapeGrouper.hpp>
33 : #include <com/sun/star/drawing/XControlShape.hpp>
34 : #include <com/sun/star/text/XTextDocument.hpp>
35 : #include <com/sun/star/text/TextContentAnchorType.hpp>
36 : #include <com/sun/star/form/XForm.hpp>
37 : #include <com/sun/star/form/XFormsSupplier.hpp>
38 : #include <com/sun/star/awt/XControlModel.hpp>
39 : #include <com/sun/star/container/XIndexContainer.hpp>
40 :
41 : #include <sfx2/app.hxx>
42 : #include <sfx2/docfilt.hxx>
43 : #include <sfx2/docfile.hxx>
44 : #include <sfx2/sfxmodelfactory.hxx>
45 : #include <svl/intitem.hxx>
46 : #include <comphelper/processfactory.hxx>
47 :
48 : #include <basic/sbxdef.hxx>
49 : #include <unotools/tempfile.hxx>
50 :
51 : #include <unocrsr.hxx>
52 : #include <ndtxt.hxx>
53 : #include <doc.hxx>
54 : #include <IDocumentLayoutAccess.hxx>
55 : #include <IDocumentUndoRedo.hxx>
56 : #include <IDocumentContentOperations.hxx>
57 : #include "docsh.hxx"
58 : #include <unotxdoc.hxx>
59 :
60 : typedef tools::SvRef<SwDocShell> SwDocShellRef;
61 :
62 : using namespace ::com::sun::star;
63 : using namespace ::com::sun::star::uno;
64 :
65 : /* Implementation of Macros test */
66 :
67 12 : class SwMacrosTest : public test::BootstrapFixture, public unotest::MacrosTest
68 : {
69 : public:
70 : SwMacrosTest();
71 :
72 : void createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath);
73 :
74 : virtual void setUp() SAL_OVERRIDE;
75 : virtual void tearDown() SAL_OVERRIDE;
76 :
77 : //void testStarBasic();
78 : #if !defined MACOSX && !defined WNT
79 : void testVba();
80 : #endif
81 : void testBookmarkDeleteAndJoin();
82 : void testBookmarkDeleteTdf90816();
83 : #if 0
84 : void testControlShapeGrouping();
85 : #endif
86 : void testFdo55289();
87 : void testFdo68983();
88 : void testFindReplace();
89 2 : CPPUNIT_TEST_SUITE(SwMacrosTest);
90 : #if !defined(MACOSX) && !defined(WNT)
91 : //enable this test if you want to play with star basic macros in unit tests
92 : //works but does nothing useful yet
93 : //CPPUNIT_TEST(testStarBasic);
94 1 : CPPUNIT_TEST(testVba);
95 : #endif
96 1 : CPPUNIT_TEST(testBookmarkDeleteAndJoin);
97 1 : CPPUNIT_TEST(testBookmarkDeleteTdf90816);
98 : #if 0
99 : CPPUNIT_TEST(testControlShapeGrouping);
100 : #endif
101 1 : CPPUNIT_TEST(testFdo55289);
102 1 : CPPUNIT_TEST(testFdo68983);
103 1 : CPPUNIT_TEST(testFindReplace);
104 :
105 5 : CPPUNIT_TEST_SUITE_END();
106 :
107 : private:
108 : uno::Reference<uno::XInterface> m_xWriterComponent;
109 : OUString m_aBaseString;
110 : };
111 :
112 2 : void SwMacrosTest::createFileURL(const OUString& aFileBase, const OUString& aFileExtension, OUString& rFilePath)
113 : {
114 2 : OUString aSep("/");
115 4 : OUStringBuffer aBuffer( getSrcRootURL() );
116 2 : aBuffer.append(m_aBaseString).append(aSep).append(aFileExtension);
117 2 : aBuffer.append(aSep).append(aFileBase).append(aFileExtension);
118 4 : rFilePath = aBuffer.makeStringAndClear();
119 2 : }
120 :
121 : #if 0
122 :
123 : void SwMacrosTest::testStarBasic()
124 : {
125 : const OUString aFileNameBase("StarBasic.");
126 : OUString aFileExtension(aFileFormats[0].pName, strlen(aFileFormats[0].pName), RTL_TEXTENCODING_UTF8 );
127 : OUString aFileName;
128 : createFileURL(aFileNameBase, aFileExtension, aFileName);
129 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
130 :
131 : CPPUNIT_ASSERT_MESSAGE("Failed to load StarBasic.ods", xComponent.is());
132 :
133 : OUString aURL("vnd.sun.Star.script:Standard.Module1.Macro1?language=Basic&location=document");
134 : String sUrl = aURL;
135 : Any aRet;
136 : Sequence< sal_Int16 > aOutParamIndex;
137 : Sequence< Any > aOutParam;
138 : Sequence< uno::Any > aParams;
139 :
140 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
141 :
142 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
143 :
144 : pFoundShell->CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
145 : pFoundShell->DoClose();
146 : }
147 :
148 : #endif
149 :
150 : #if !defined MACOSX && !defined WNT
151 1 : void SwMacrosTest::testVba()
152 : {
153 : TestMacroInfo testInfo[] = {
154 : {
155 : OUString("testVba."),
156 : OUString("vnd.sun.Star.script:Project.NewMacros.Macro1?language=Basic&location=document")
157 : }
158 2 : };
159 2 : OUString aFileExtension( "doc" );
160 2 : for ( sal_uInt32 i=0; i<SAL_N_ELEMENTS( testInfo ); ++i )
161 : {
162 1 : OUString aFileName;
163 1 : createFileURL(testInfo[i].sFileBaseName, aFileExtension, aFileName);
164 2 : uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
165 2 : OUStringBuffer sMsg( "Failed to load " );
166 1 : sMsg.append ( aFileName );
167 1 : CPPUNIT_ASSERT_MESSAGE( OUStringToOString( sMsg.makeStringAndClear(), RTL_TEXTENCODING_UTF8 ).getStr(), xComponent.is() );
168 :
169 2 : OUString sUrl = testInfo[i].sMacroUrl;
170 2 : Any aRet;
171 2 : Sequence< sal_Int16 > aOutParamIndex;
172 2 : Sequence< Any > aOutParam;
173 2 : Sequence< uno::Any > aParams;
174 :
175 1 : SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
176 :
177 1 : CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
178 1 : SfxObjectShell::CallXScript(xComponent, sUrl, aParams, aRet, aOutParamIndex,aOutParam);
179 2 : OUString aStringRes;
180 1 : aRet >>= aStringRes;
181 1 : std::cout << "value of Ret " << OUStringToOString( aStringRes, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
182 : //CPPUNIT_ASSERT_MESSAGE( "script reported failure",aStringRes == "OK" );
183 1 : pFoundShell->DoClose();
184 3 : }
185 1 : }
186 : #endif
187 :
188 1 : void SwMacrosTest::testBookmarkDeleteAndJoin()
189 : {
190 1 : SwDoc *const pDoc = new SwDoc;
191 1 : pDoc->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
192 1 : SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
193 2 : SwPaM aPaM(aIdx);
194 :
195 1 : IDocumentContentOperations & rIDCO(pDoc->getIDocumentContentOperations());
196 1 : rIDCO.AppendTextNode(*aPaM.GetPoint());
197 1 : rIDCO.InsertString(aPaM, OUString("A"));
198 1 : rIDCO.AppendTextNode(*aPaM.GetPoint());
199 1 : rIDCO.InsertString(aPaM, OUString("A"));
200 1 : rIDCO.AppendTextNode(*aPaM.GetPoint());
201 1 : aPaM.Move(fnMoveBackward, fnGoNode);
202 1 : aPaM.Move(fnMoveBackward, fnGoNode);
203 1 : aPaM.Move(fnMoveBackward, fnGoContent);
204 1 : aPaM.SetMark();
205 1 : aPaM.Move(fnMoveForward, fnGoDoc);
206 1 : IDocumentMarkAccess & rIDMA = *pDoc->getIDocumentMarkAccess();
207 : sw::mark::IMark *pMark =
208 1 : rIDMA.makeMark(aPaM, "test", IDocumentMarkAccess::MarkType::BOOKMARK);
209 1 : CPPUNIT_ASSERT(pMark);
210 : // select so pMark start position is on a node that is fully deleted
211 1 : aPaM.Move(fnMoveBackward, fnGoNode);
212 : // must leave un-selected content in last node to get the bJoinPrev flag!
213 1 : aPaM.Move(fnMoveBackward, fnGoContent);
214 1 : aPaM.Exchange();
215 1 : aPaM.Move(fnMoveBackward, fnGoDoc);
216 : // delete
217 1 : rIDCO.DeleteAndJoin(aPaM, false);
218 :
219 2 : for (IDocumentMarkAccess::const_iterator_t i = rIDMA.getAllMarksBegin(); i != rIDMA.getAllMarksEnd(); ++i)
220 : {
221 : // problem was that the nContent was pointing at deleted node
222 2 : CPPUNIT_ASSERT((*i)->GetMarkStart().nNode.GetNode().GetContentNode() ==
223 1 : static_cast<const SwContentNode*>((*i)->GetMarkStart().nContent.GetIdxReg()));
224 1 : }
225 1 : }
226 :
227 1 : void SwMacrosTest::testBookmarkDeleteTdf90816()
228 : {
229 1 : SwDoc *const pDoc = new SwDoc;
230 1 : pDoc->GetIDocumentUndoRedo().DoUndo(true); // bug is in SwUndoDelete
231 1 : SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
232 2 : SwPaM aPaM(aIdx);
233 :
234 1 : IDocumentContentOperations & rIDCO(pDoc->getIDocumentContentOperations());
235 1 : rIDCO.AppendTextNode(*aPaM.GetPoint());
236 1 : rIDCO.InsertString(aPaM, OUString("ABC"));
237 1 : aPaM.Move(fnMoveBackward, fnGoContent);
238 1 : aPaM.SetMark();
239 1 : aPaM.Move(fnMoveBackward, fnGoContent);
240 1 : IDocumentMarkAccess & rIDMA = *pDoc->getIDocumentMarkAccess();
241 : sw::mark::IMark *pMark =
242 1 : rIDMA.makeMark(aPaM, "test", IDocumentMarkAccess::MarkType::BOOKMARK);
243 1 : CPPUNIT_ASSERT(pMark);
244 :
245 : // delete the same selection as the bookmark
246 1 : rIDCO.DeleteAndJoin(aPaM, false);
247 :
248 : // bookmark still there?
249 1 : auto iter = rIDMA.getAllMarksBegin();
250 2 : CPPUNIT_ASSERT_MESSAGE("the bookmark was deleted",
251 1 : iter != rIDMA.getAllMarksEnd());
252 1 : CPPUNIT_ASSERT(*aPaM.Start() == (*iter)->GetMarkPos());
253 2 : CPPUNIT_ASSERT(*aPaM.End() == (*iter)->GetOtherMarkPos());
254 1 : }
255 :
256 : #if 0
257 : void SwMacrosTest::testControlShapeGrouping()
258 : {
259 : OUString aFileName;
260 : createFileURL("testControlShapeGrouping.", "odt", aFileName);
261 : Reference< com::sun::star::lang::XComponent > xComponent(
262 : loadFromDesktop(aFileName, "com.sun.star.text.TextDocument"));
263 : CPPUNIT_ASSERT(xComponent.is());
264 :
265 : uno::Reference<frame::XModel> const xModel(xComponent, UNO_QUERY);
266 : CPPUNIT_ASSERT(xModel.is());
267 : uno::Reference<lang::XMultiServiceFactory> xFactory(xModel, UNO_QUERY);
268 : uno::Reference<drawing::XDrawPageSupplier> const xDPS(xModel, UNO_QUERY);
269 : uno::Reference<drawing::XDrawPage> const xDP(xDPS->getDrawPage(), UNO_QUERY);
270 : CPPUNIT_ASSERT(xDP.is());
271 : uno::Reference<drawing::XShapes> const xDPShapes(xDP, UNO_QUERY);
272 : CPPUNIT_ASSERT(xDPShapes.is());
273 : uno::Reference<drawing::XShapes> const xShapes(getMultiServiceFactory()->createInstance("com.sun.star.drawing.ShapeCollection"),
274 : UNO_QUERY);
275 : CPPUNIT_ASSERT(xShapes.is());
276 : uno::Reference<container::XIndexAccess> xShapesIC(xShapes, UNO_QUERY);
277 : CPPUNIT_ASSERT(xShapesIC.is());
278 :
279 : // uno::Reference<beans::XPropertySet> xFormProps(xForm, UNO_QUERY);
280 : // xFormProps->setPropertyValue("Name", makeAny("aForm"));
281 : uno::Reference<form::XFormsSupplier> const xFS(xDP, UNO_QUERY);
282 : CPPUNIT_ASSERT(xFS.is());
283 : uno::Reference<container::XIndexContainer> const xForms(xFS->getForms(), UNO_QUERY);
284 : CPPUNIT_ASSERT(xForms.is());
285 : uno::Reference<form::XForm> xForm(xForms->getByIndex(0), UNO_QUERY);
286 : CPPUNIT_ASSERT(xForm.is());
287 : uno::Reference<container::XNameContainer> xFormNC(xForm, UNO_QUERY);
288 :
289 : uno::Reference<drawing::XControlShape> const xDateShape(
290 : xFactory->createInstance("com.sun.star.drawing.ControlShape"),
291 : UNO_QUERY);
292 : uno::Reference<awt::XControlModel> const xDateControlModel(
293 : xFactory->createInstance("com.sun.star.form.component.DateField"),
294 : UNO_QUERY);
295 : xDateShape->setControl(xDateControlModel);
296 : uno::Reference<beans::XPropertySet> xDateShapeProps(xDateShape, UNO_QUERY);
297 : xDateShapeProps->setPropertyValue("AnchorType", makeAny(text::TextContentAnchorType_AT_PARAGRAPH));
298 :
299 : uno::Reference<drawing::XControlShape> const xTimeShape(
300 : xFactory->createInstance("com.sun.star.drawing.ControlShape"),
301 : UNO_QUERY);
302 : uno::Reference<awt::XControlModel> const xTimeControlModel(
303 : xFactory->createInstance("com.sun.star.form.component.TimeField"),
304 : UNO_QUERY);
305 : xTimeShape->setControl(xTimeControlModel);
306 : uno::Reference<beans::XPropertySet> xTimeShapeProps(xTimeShape, UNO_QUERY);
307 : xTimeShapeProps->setPropertyValue("AnchorType", makeAny(text::TextContentAnchorType_AT_PARAGRAPH));
308 :
309 : xFormNC->insertByName("aDateCntrl", makeAny(xDateControlModel));
310 : xDPShapes->add(xDateShape);
311 : xFormNC->insertByName("aTimeCntrl", makeAny(xTimeControlModel));
312 : xDPShapes->add(xTimeShape);
313 :
314 : xShapes->add(uno::Reference<drawing::XShape>(xDateShape, UNO_QUERY));
315 : xShapes->add(uno::Reference<drawing::XShape>(xTimeShape, UNO_QUERY));
316 : uno::Reference<drawing::XShapeGrouper> const xDPGrouper(xDP, UNO_QUERY);
317 : CPPUNIT_ASSERT(xDPGrouper.is());
318 : uno::Reference<drawing::XShapeGroup> xGroup(xDPGrouper->group(xShapes));
319 : CPPUNIT_ASSERT(xGroup.is());
320 : uno::Reference<container::XIndexAccess> xGroupIC(xGroup, UNO_QUERY);
321 : CPPUNIT_ASSERT(xGroup.is());
322 :
323 : CPPUNIT_ASSERT(xDateShape->getControl().is());
324 : CPPUNIT_ASSERT(xDateShape->getControl() == xDateControlModel);
325 : CPPUNIT_ASSERT(xTimeShape->getControl().is());
326 : CPPUNIT_ASSERT(xTimeShape->getControl() == xTimeControlModel);
327 :
328 : {
329 : uno::Reference< uno::XInterface > xDI;
330 : xGroupIC->getByIndex(0) >>= xDI;
331 : CPPUNIT_ASSERT(xDI.is());
332 : uno::Reference< drawing::XControlShape > xDS(xDI, UNO_QUERY);
333 : CPPUNIT_ASSERT(xDS.is());
334 : CPPUNIT_ASSERT(xDS->getControl() == xDateControlModel);
335 :
336 : uno::Reference< uno::XInterface > xTI;
337 : xGroupIC->getByIndex(1) >>= xTI;
338 : CPPUNIT_ASSERT(xTI.is());
339 : uno::Reference< drawing::XControlShape > xTS(xTI, UNO_QUERY);
340 : CPPUNIT_ASSERT(xTS.is());
341 : CPPUNIT_ASSERT(xTS->getControl() == xTimeControlModel);
342 : }
343 : {
344 : uno::Reference< uno::XInterface > xDI;
345 : xShapesIC->getByIndex(0) >>= xDI;
346 : CPPUNIT_ASSERT(xDI.is());
347 : uno::Reference< drawing::XControlShape > xDS(xDI, UNO_QUERY);
348 : CPPUNIT_ASSERT(xDS.is());
349 : CPPUNIT_ASSERT(xDS->getControl() == xDateControlModel);
350 :
351 : uno::Reference< uno::XInterface > xTI;
352 : xShapesIC->getByIndex(1) >>= xTI;
353 : CPPUNIT_ASSERT(xTI.is());
354 : uno::Reference< drawing::XControlShape > xTS(xTI, UNO_QUERY);
355 : CPPUNIT_ASSERT(xTS.is());
356 : CPPUNIT_ASSERT(xTS->getControl() == xTimeControlModel);
357 : }
358 : }
359 : #endif
360 :
361 1 : void SwMacrosTest::testFdo55289()
362 : {
363 1 : SwDoc *const pDoc = new SwDoc;
364 1 : SwDocShellRef pDocShell = new SwDocShell(pDoc, SfxObjectCreateMode::EMBEDDED);
365 : // this needs to run with no layout to tickle the bugs in the special
366 : // cases in SwXShape re-anchoring
367 : assert(!pDoc->getIDocumentLayoutAccess().GetCurrentLayout());
368 :
369 2 : uno::Reference<frame::XModel> const xModel(pDocShell->GetModel());
370 2 : uno::Reference<drawing::XDrawPageSupplier> const xDPS(xModel, UNO_QUERY);
371 1 : uno::Reference<drawing::XShapes> const xShapes(xDPS->getDrawPage(),
372 2 : UNO_QUERY);
373 : uno::Reference<beans::XPropertySet> const xShape(
374 2 : uno::Reference<lang::XMultiServiceFactory>(xModel, UNO_QUERY)->
375 1 : createInstance("com.sun.star.drawing.GraphicObjectShape"),
376 2 : UNO_QUERY);
377 1 : xShape->setPropertyValue("AnchorType",
378 1 : makeAny(text::TextContentAnchorType_AT_PAGE));
379 1 : xShapes->add(uno::Reference<drawing::XShape>(xShape, UNO_QUERY));
380 1 : xShape->setPropertyValue("AnchorType",
381 1 : makeAny(text::TextContentAnchorType_AT_CHARACTER));
382 1 : xShape->setPropertyValue("AnchorType",
383 1 : makeAny(text::TextContentAnchorType_AS_CHARACTER));
384 1 : xShape->setPropertyValue("AnchorType",
385 1 : makeAny(text::TextContentAnchorType_AT_CHARACTER));
386 1 : xShape->setPropertyValue("AnchorType",
387 1 : makeAny(text::TextContentAnchorType_AS_CHARACTER));
388 : uno::Reference<text::XTextRange> const xEnd =
389 2 : uno::Reference<text::XTextDocument>(xModel, UNO_QUERY)->getText()->getEnd();
390 2 : uno::Reference<text::XTextContent> const xShapeContent(xShape, UNO_QUERY);
391 2 : xShapeContent->attach(xEnd);
392 1 : }
393 :
394 1 : void SwMacrosTest::testFdo68983()
395 : {
396 1 : OUString aFileName;
397 1 : createFileURL("fdo68983.", "odt", aFileName);
398 : Reference< com::sun::star::lang::XComponent > xComponent =
399 2 : loadFromDesktop(aFileName, "com.sun.star.text.TextDocument");
400 :
401 1 : CPPUNIT_ASSERT_MESSAGE("Failed to load fdo68983.odt", xComponent.is());
402 :
403 2 : Reference< frame::XStorable > xDocStorable(xComponent, UNO_QUERY_THROW);
404 1 : CPPUNIT_ASSERT(xDocStorable.is());
405 :
406 2 : utl::TempFile aTempFile;
407 1 : aTempFile.EnableKillingFile();
408 2 : Sequence<beans::PropertyValue> desc(1);
409 1 : desc[0].Name = "FilterName";
410 1 : desc[0].Value <<= OUString("writer8");
411 1 : xDocStorable->storeAsURL(aTempFile.GetURL(), desc);
412 :
413 1 : Reference<util::XCloseable>(xComponent, UNO_QUERY_THROW)->close(false);
414 :
415 : // re-load
416 1 : xComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
417 :
418 : // check that password-protected library survived store and re-load
419 2 : Reference<document::XEmbeddedScripts> xDocScr(xComponent, UNO_QUERY_THROW);
420 2 : Reference<script::XStorageBasedLibraryContainer> xStorBasLib(xDocScr->getBasicLibraries());
421 2 : Reference<script::XLibraryContainer> xBasLib(xStorBasLib, UNO_QUERY_THROW);
422 2 : Reference<script::XLibraryContainerPassword> xBasLibPwd(xStorBasLib, UNO_QUERY_THROW);
423 1 : CPPUNIT_ASSERT(xBasLibPwd->isLibraryPasswordProtected("Library1"));
424 1 : CPPUNIT_ASSERT(xBasLibPwd->verifyLibraryPassword("Library1", "foo"));
425 1 : xBasLib->loadLibrary("Library1");
426 1 : CPPUNIT_ASSERT(xBasLib->isLibraryLoaded("Library1"));
427 :
428 : // close
429 2 : Reference<util::XCloseable> xDocCloseable(xComponent, UNO_QUERY_THROW);
430 2 : xDocCloseable->close(false);
431 1 : }
432 :
433 1 : void SwMacrosTest::testFindReplace()
434 : {
435 : // we need a full document with view and layout etc. because ::GetNode()
436 : Reference<lang::XComponent> const xComponent =
437 1 : loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
438 1 : SwXTextDocument *const pTextDoc = dynamic_cast<SwXTextDocument *>(xComponent.get());
439 1 : CPPUNIT_ASSERT(pTextDoc);
440 1 : SwDoc *const pDoc = pTextDoc->GetDocShell()->GetDoc();
441 2 : SwNodeIndex aIdx(pDoc->GetNodes().GetEndOfContent(), -1);
442 : // use a UnoCrsr so it will be corrected when deleting nodes
443 2 : auto pPaM(pDoc->CreateUnoCrsr(SwPosition(aIdx), false));
444 :
445 1 : IDocumentContentOperations & rIDCO(pDoc->getIDocumentContentOperations());
446 1 : rIDCO.InsertString(*pPaM, OUString("foo"));
447 1 : rIDCO.AppendTextNode(*pPaM->GetPoint());
448 1 : rIDCO.InsertString(*pPaM, OUString("bar"));
449 1 : rIDCO.AppendTextNode(*pPaM->GetPoint());
450 1 : rIDCO.InsertString(*pPaM, OUString("baz"));
451 1 : pPaM->Move(fnMoveBackward, fnGoDoc);
452 :
453 1 : bool bCancel(false);
454 : util::SearchOptions opts(
455 : util::SearchAlgorithms_REGEXP,
456 : 65536,
457 : "$",
458 : "",
459 : lang::Locale("en", "US", ""),
460 : 2,
461 : 2,
462 : 2,
463 2 : 1073745152);
464 :
465 : // find newline on 1st paragraph
466 1 : bool bFound = pPaM->Find(
467 1 : opts, false, DOCPOS_CURR, DOCPOS_END, bCancel, FND_IN_BODY, false);
468 1 : CPPUNIT_ASSERT(bFound);
469 1 : CPPUNIT_ASSERT(pPaM->HasMark());
470 1 : CPPUNIT_ASSERT_EQUAL(OUString(""), pPaM->GetText());
471 :
472 : // now do another Find, inside the selection from the first Find
473 : // opts.searchFlags = 71680;
474 1 : bFound = pPaM->Find(
475 1 : opts, false, DOCPOS_CURR, DOCPOS_END, bCancel, FND_IN_SEL, false);
476 1 : CPPUNIT_ASSERT(bFound);
477 1 : CPPUNIT_ASSERT(pPaM->HasMark());
478 1 : CPPUNIT_ASSERT_EQUAL(OUString(""), pPaM->GetText());
479 :
480 1 : rIDCO.ReplaceRange(*pPaM, " ", true);
481 :
482 1 : pPaM->DeleteMark();
483 1 : pPaM->Move(fnMoveBackward, fnGoDoc);
484 :
485 : // problem was that after the 2nd Find, the wrong newline was selected
486 2 : CPPUNIT_ASSERT_EQUAL(OUString("foo bar"),
487 1 : pPaM->Start()->nNode.GetNode().GetTextNode()->GetText());
488 1 : pPaM->Move(fnMoveForward, fnGoNode);
489 2 : CPPUNIT_ASSERT_EQUAL(OUString("baz"),
490 2 : pPaM->End()->nNode.GetNode().GetTextNode()->GetText());
491 1 : }
492 :
493 6 : SwMacrosTest::SwMacrosTest()
494 6 : : m_aBaseString("/sw/qa/core/data")
495 : {
496 6 : }
497 :
498 6 : void SwMacrosTest::setUp()
499 : {
500 6 : test::BootstrapFixture::setUp();
501 :
502 : // This is a bit of a fudge, we do this to ensure that SwGlobals::ensure,
503 : // which is a private symbol to us, gets called
504 12 : m_xWriterComponent =
505 12 : getMultiServiceFactory()->createInstance(OUString(
506 12 : "com.sun.star.comp.Writer.TextDocument"));
507 6 : CPPUNIT_ASSERT_MESSAGE("no calc component!", m_xWriterComponent.is());
508 6 : mxDesktop = com::sun::star::frame::Desktop::create( comphelper::getComponentContext(getMultiServiceFactory()) );
509 6 : }
510 :
511 6 : void SwMacrosTest::tearDown()
512 : {
513 6 : uno::Reference< lang::XComponent >( m_xWriterComponent, UNO_QUERY_THROW )->dispose();
514 6 : test::BootstrapFixture::tearDown();
515 6 : }
516 :
517 1 : CPPUNIT_TEST_SUITE_REGISTRATION(SwMacrosTest);
518 :
519 4 : CPPUNIT_PLUGIN_IMPLEMENT();
520 :
521 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|