Debian Codegraph @

sw/qa/uibase/shells/shells.cxx

raw
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 <swmodeltestbase.hxx>
11
12 #include <com/sun/star/frame/Desktop.hpp>
13 #include <com/sun/star/frame/XStorable.hpp>
14 #include <com/sun/star/packages/zip/ZipFileAccess.hpp>
15 #include <com/sun/star/text/BibliographyDataType.hpp>
16 #include <com/sun/star/text/XTextDocument.hpp>
17 #include <com/sun/star/text/XTextEmbeddedObjectsSupplier.hpp>
18 #include <com/sun/star/chart2/XChartDocument.hpp>
19 #include <com/sun/star/chart/XChartDataArray.hpp>
20
21 #include <sfx2/dispatch.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <svx/svdpage.hxx>
24 #include <editeng/eeitem.hxx>
25 #include <editeng/adjustitem.hxx>
26 #include <editeng/outlobj.hxx>
27 #include <editeng/editobj.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <comphelper/propertyvalue.hxx>
30 #include <unotools/ucbstreamhelper.hxx>
31 #include <xmloff/odffields.hxx>
32 #include <comphelper/string.hxx>
33 #include <comphelper/propertysequence.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <osl/thread.hxx>
36
37 #include <IDocumentContentOperations.hxx>
38 #include <IDocumentRedlineAccess.hxx>
39 #include <cmdid.h>
40 #include <fmtanchr.hxx>
41 #include <view.hxx>
42 #include <wrtsh.hxx>
43 #include <IDocumentDrawModelAccess.hxx>
44 #include <drawdoc.hxx>
45 #include <docsh.hxx>
46 #include <ndtxt.hxx>
47 #include <ftnidx.hxx>
48 #include <txtftn.hxx>
49 #include <unotxdoc.hxx>
50 #include <tools/json_writer.hxx>
51
52 #include <boost/property_tree/json_parser.hpp>
53
54 using namespace std::string_literals;
55
56 /// Covers sw/source/uibase/shells/ fixes.
57 class SwUibaseShellsTest : public SwModelTestBase
58 {
59 public:
60 SwUibaseShellsTest()
61 : SwModelTestBase(u"/sw/qa/uibase/shells/data/"_ustr)
62 {
63 }
64 };
65
66 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testTdf130179)
67 {
68 createSwDoc();
69 SwDoc* pDoc = getSwDoc();
70 IDocumentContentOperations& rIDCO = pDoc->getIDocumentContentOperations();
71 SwCursorShell* pShell(pDoc->GetEditShell());
72 CPPUNIT_ASSERT(pShell);
73 SfxItemSet aFrameSet(pDoc->GetAttrPool(), svl::Items<RES_FRMATR_BEGIN, RES_FRMATR_END - 1>);
74 SfxItemSet aGrfSet(pDoc->GetAttrPool(), svl::Items<RES_GRFATR_BEGIN, RES_GRFATR_END - 1>);
75 SwFormatAnchor aAnchor(RndStdIds::FLY_AT_PARA);
76 aFrameSet.Put(aAnchor);
77 Graphic aGrf;
78 CPPUNIT_ASSERT(rIDCO.InsertGraphic(*pShell->GetCursor(), OUString(), OUString(), &aGrf,
79 &aFrameSet, &aGrfSet, nullptr));
80 CPPUNIT_ASSERT_EQUAL(size_t(1), pDoc->GetFlyCount(FLYCNTTYPE_GRF));
81
82 SwView* pView = getSwDocShell()->GetView();
83 selectShape(1);
84
85 std::unique_ptr<SfxPoolItem> pItem;
86 pView->GetViewFrame().GetBindings().QueryState(FN_POSTIT, pItem);
87 // Without the accompanying fix in place, this test would have failed with:
88 // assertion failed
89 // - Expression: !pItem
90 // i.e. comment insertion was enabled for an at-para anchored image.
91 CPPUNIT_ASSERT(!pItem);
92 }
93
94 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testShapeTextAlignment)
95 {
96 // FIXME find out why this fails on macOS/Windows
97 #if !defined(MACOSX) && !defined(_WIN32)
98 // Create a document with a rectangle in it.
99 createSwDoc();
100 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
101 Point aStartPos(1000, 1000);
102 pWrtShell->BeginCreate(SdrObjKind::Rectangle, aStartPos);
103 Point aMovePos(2000, 2000);
104 pWrtShell->MoveCreate(aMovePos);
105 pWrtShell->EndCreate(SdrCreateCmd::ForceEnd);
106
107 // Start shape text edit.
108 SwView* pView = getSwDocShell()->GetView();
109 // Select the shape.
110 selectShape(1);
111 // Start the actual text edit.
112 SdrPage* pPage = pWrtShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
113 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pPage->GetObjCount());
114 SdrObject* pObject = pPage->GetObj(0);
115 pView->EnterShapeDrawTextMode(pObject);
116 pView->AttrChangedNotify(nullptr);
117
118 // Change paragraph adjustment to center.
119 pView->GetViewFrame().GetDispatcher()->Execute(SID_ATTR_PARA_ADJUST_CENTER,
120 SfxCallMode::SYNCHRON);
121
122 // End shape text edit.
123 pWrtShell->EndTextEdit();
124
125 const OutlinerParaObject* pOutliner = pObject->GetOutlinerParaObject();
126 // Without the accompanying fix in place, this test would have failed, because the shape had no
127 // text or text formatting. In other words the paragraph adjustment command was ignored.
128 CPPUNIT_ASSERT(pOutliner);
129 const SfxItemSet& rParaAttribs = pOutliner->GetTextObject().GetParaAttribs(0);
130 SvxAdjust eAdjust = rParaAttribs.GetItem(EE_PARA_JUST)->GetAdjust();
131 CPPUNIT_ASSERT_EQUAL(SvxAdjust::Center, eAdjust);
132 #endif
133 }
134
135 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testOleSavePreviewUpdate)
136 {
137 // Load a document with 2 charts in it. The second is down enough that you have to scroll to
138 // trigger its rendering. Previews are missing for both.
139 createSwDoc("ole-save-preview-update.odt");
140
141 // Explicitly update OLE previews, etc.
142 dispatchCommand(mxComponent, u".uno:UpdateAll"_ustr, {});
143
144 // Save the document and see if we get the previews.
145 uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
146 xStorable->storeToURL(maTempFile.GetURL(), {});
147 uno::Reference<packages::zip::XZipFileAccess2> xNameAccess
148 = packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
149 maTempFile.GetURL());
150
151 // Without the accompanying fix in place, this test would have failed, because the object
152 // replacements were not generated, even after UpdateAll.
153 CPPUNIT_ASSERT(xNameAccess->hasByName(u"ObjectReplacements/Object 1"_ustr));
154 CPPUNIT_ASSERT(xNameAccess->hasByName(u"ObjectReplacements/Object 2"_ustr));
155 }
156
157 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testOlePreviewUpdate)
158 {
159 // Given a document with an embedded Writer object:
160 createSwDoc("ole-preview-update.odt");
161
162 // When updating "all" (including OLE previews):
163 dispatchCommand(mxComponent, u".uno:UpdateAll"_ustr, {});
164
165 // Then make sure the preview is no longer a 0-sized stream:
166 uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
167 xStorable->storeToURL(maTempFile.GetURL(), {});
168 uno::Reference<packages::zip::XZipFileAccess2> xNameAccess
169 = packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
170 maTempFile.GetURL());
171 uno::Reference<io::XInputStream> xInputStream(
172 xNameAccess->getByName(u"ObjectReplacements/Object 1"_ustr), uno::UNO_QUERY);
173 std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
174 // Without the accompanying fix in place, this test would have failed, the stream was still
175 // empty.
176 CPPUNIT_ASSERT_GREATER(static_cast<sal_uInt64>(0), pStream->remainingSize());
177 }
178
179 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testBibliographyUrlContextMenu)
180 {
181 // Given a document with a bibliography field:
182 createSwDoc();
183 uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
184 uno::Reference<beans::XPropertySet> xField(
185 xFactory->createInstance(u"com.sun.star.text.TextField.Bibliography"_ustr), uno::UNO_QUERY);
186 uno::Sequence<beans::PropertyValue> aFields = {
187 comphelper::makePropertyValue(u"BibiliographicType"_ustr, text::BibliographyDataType::WWW),
188 comphelper::makePropertyValue(u"Identifier"_ustr, u"AT"_ustr),
189 comphelper::makePropertyValue(u"Author"_ustr, u"Author"_ustr),
190 comphelper::makePropertyValue(u"Title"_ustr, u"Title"_ustr),
191 comphelper::makePropertyValue(u"URL"_ustr, u"http://www.example.com/test.pdf#page=1"_ustr),
192 };
193 xField->setPropertyValue(u"Fields"_ustr, uno::Any(aFields));
194 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
195 uno::Reference<text::XText> xText = xTextDocument->getText();
196 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
197 uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
198 xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
199
200 // When selecting the field and opening the context menu:
201 SwDocShell* pDocShell = getSwDocShell();
202 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
203 pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
204 SfxDispatcher* pDispatcher = pDocShell->GetViewShell()->GetViewFrame().GetDispatcher();
205 css::uno::Any aState;
206 SfxItemState eStateOpen = pDispatcher->QueryState(SID_OPEN_HYPERLINK, aState);
207 SfxItemState eStateCopy = pDispatcher->QueryState(SID_COPY_HYPERLINK_LOCATION, aState);
208
209 // Then the "open hyperlink" and "copy hyperlink location" menu items should be visible:
210 // Without the accompanying fix in place, this test would have failed with:
211 // - Expected: 32 (SfxItemState::DEFAULT)
212 // - Actual : 1 (SfxItemState::DISABLED)
213 // i.e. the menu item was not visible for biblio entry fields with an URL.
214 CPPUNIT_ASSERT_EQUAL(SfxItemState::DEFAULT, eStateOpen);
215 CPPUNIT_ASSERT_EQUAL(SfxItemState::DEFAULT, eStateCopy);
216 }
217
218 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testProtectedFieldsCopyHyperlinkLocation)
219 {
220 // Given a test document document that contains:
221 // - generic url
222 // - empty line
223 // - bibliography mark
224 // - empty line
225 // - generic url
226 // - empty line
227 // - bibliography table heading
228 // - bibliography entry containing only url
229 // - empty line
230 createSwDoc("protectedLinkCopy.fodt");
231
232 // Copy generic hyperlink
233 dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
234 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
235 dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
236 // Assert generic hyperlink was correctly copied and pasted
237 CPPUNIT_ASSERT_EQUAL(u"http://reset.url/1"_ustr, getParagraph(2)->getString());
238
239 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
240 dispatchCommand(mxComponent, u".uno:GoLeft"_ustr, {});
241 // Copy bibliography mark hyperlink
242 dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
243 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
244 dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
245 // Assert bibliography mark hyperlink was correctly copied and pasted
246 CPPUNIT_ASSERT_EQUAL(u"https://test.url/1"_ustr, getParagraph(4)->getString());
247
248 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
249 dispatchCommand(mxComponent, u".uno:GoLeft"_ustr, {});
250 // Copy generic hyperlink
251 dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
252 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
253 dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
254 // Assert generic hyperlink was correctly copied and pasted
255 CPPUNIT_ASSERT_EQUAL(u"http://reset.url/2"_ustr, getParagraph(6)->getString());
256
257 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
258 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
259 dispatchCommand(mxComponent, u".uno:GoLeft"_ustr, {});
260 // Copy bibliography table hyperlink
261 dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
262 dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
263 dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
264 // Assert bibliography table entry hyperlink was correctly copied and pasted
265 CPPUNIT_ASSERT_EQUAL(u"https://test.url/1"_ustr, getParagraph(9)->getString());
266 }
267
268 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testBibliographyLocalCopyContextMenu)
269 {
270 // Given a document with a bibliography field's local copy:
271 createSwDoc();
272 uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
273 uno::Reference<beans::XPropertySet> xField(
274 xFactory->createInstance(u"com.sun.star.text.TextField.Bibliography"_ustr), uno::UNO_QUERY);
275 uno::Sequence<beans::PropertyValue> aFields = {
276 comphelper::makePropertyValue(u"BibiliographicType"_ustr, text::BibliographyDataType::WWW),
277 comphelper::makePropertyValue(u"Identifier"_ustr, u"AT"_ustr),
278 comphelper::makePropertyValue(u"Author"_ustr, u"Author"_ustr),
279 comphelper::makePropertyValue(u"Title"_ustr, u"Title"_ustr),
280 comphelper::makePropertyValue(u"URL"_ustr, u"http://www.example.com/test.pdf#page=1"_ustr),
281 comphelper::makePropertyValue(u"LocalURL"_ustr, u"file:///home/me/test.pdf"_ustr),
282 };
283 xField->setPropertyValue(u"Fields"_ustr, uno::Any(aFields));
284 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
285 uno::Reference<text::XText> xText = xTextDocument->getText();
286 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
287 uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
288 xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
289
290 // When selecting the field and opening the context menu:
291 SwDocShell* pDocShell = getSwDocShell();
292 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
293 pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
294 SfxDispatcher* pDispatcher = pDocShell->GetViewShell()->GetViewFrame().GetDispatcher();
295 css::uno::Any aState;
296 SfxItemState eState = pDispatcher->QueryState(FN_OPEN_LOCAL_URL, aState);
297
298 // Then the "open local copy" menu item should be visible:
299 // Without the accompanying fix in place, this test would have failed with:
300 // - Expected: 32 (SfxItemState::DEFAULT)
301 // - Actual : 1 (SfxItemState::DISABLED)
302 // i.e. the context menu was disabled all the time, even for biblio fields.
303 CPPUNIT_ASSERT_EQUAL(SfxItemState::DEFAULT, eState);
304 }
305
306 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testContentControlPageBreak)
307 {
308 // Given a document with a content control and a cursor inside the content control:
309 createSwDoc();
310 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
311 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
312 uno::Reference<text::XText> xText = xTextDocument->getText();
313 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
314 xText->insertString(xCursor, u"test"_ustr, /*bAbsorb=*/false);
315 xCursor->gotoStart(/*bExpand=*/false);
316 xCursor->gotoEnd(/*bExpand=*/true);
317 uno::Reference<text::XTextContent> xContentControl(
318 xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
319 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
320 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
321 pWrtShell->SttEndDoc(/*bStt=*/true);
322 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
323
324 // When trying to insert a page break:
325 dispatchCommand(mxComponent, u".uno:InsertPagebreak"_ustr, {});
326
327 // Then make sure that the document still has a single page:
328 // Without the accompanying fix in place, this test would have failed with:
329 // - Expected: 1
330 // - Actual : 2
331 // i.e. inline content control had its start and end in different text nodes, which is not
332 // allowed.
333 CPPUNIT_ASSERT_EQUAL(1, getPages());
334 }
335
336 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormField)
337 {
338 // Given an empty document:
339 createSwDoc();
340 SwDoc* pDoc = getSwDoc();
341
342 // When inserting an ODF_UNHANDLED fieldmark:
343 OUString aExpectedCommand(u"ADDIN ZOTERO_BIBL foo bar"_ustr);
344 uno::Sequence<css::beans::PropertyValue> aArgs = {
345 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
346 comphelper::makePropertyValue(u"FieldCommand"_ustr, uno::Any(aExpectedCommand)),
347 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)),
348 };
349 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
350
351 // Then make sure that it's type/name is correct:
352 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
353 SwCursor* pCursor = pWrtShell->GetCursor();
354 pCursor->SttEndDoc(/*bSttDoc=*/true);
355 sw::mark::Fieldmark* pFieldmark
356 = pDoc->getIDocumentMarkAccess()->getFieldmarkAt(*pCursor->GetPoint());
357 CPPUNIT_ASSERT(pFieldmark);
358 // Without the accompanying fix in place, this test would have failed with:
359 // - Expected: vnd.oasis.opendocument.field.UNHANDLED
360 // - Actual : vnd.oasis.opendocument.field.FORMTEXT
361 // i.e. the custom type parameter was ignored.
362 CPPUNIT_ASSERT_EQUAL(ODF_UNHANDLED, pFieldmark->GetFieldname());
363
364 auto it = pFieldmark->GetParameters()->find(ODF_CODE_PARAM);
365 CPPUNIT_ASSERT(it != pFieldmark->GetParameters()->end());
366 OUString aActualCommand;
367 it->second >>= aActualCommand;
368 CPPUNIT_ASSERT_EQUAL(aExpectedCommand, aActualCommand);
369
370 SwPaM aPam(pFieldmark->GetMarkStart(), pFieldmark->GetMarkEnd());
371 // Ignore the leading field start + sep.
372 aPam.GetMark()->SetContent(aPam.GetMark()->GetContentIndex() + 2);
373 // Ignore the trailing field end.
374 aPam.GetPoint()->SetContent(aPam.GetPoint()->GetContentIndex() - 1);
375 CPPUNIT_ASSERT(aPam.HasMark());
376 OUString aActualResult = aPam.GetText();
377 CPPUNIT_ASSERT_EQUAL(u"aaa\nbbb"_ustr, aActualResult);
378 }
379
380 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateFieldmarks)
381 {
382 // Given a document with 2 fieldmarks:
383 createSwDoc();
384 {
385 uno::Sequence<css::beans::PropertyValue> aArgs = {
386 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
387 comphelper::makePropertyValue(u"FieldCommand"_ustr,
388 uno::Any(u"ADDIN ZOTERO_ITEM old command 1"_ustr)),
389 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"old result 1"_ustr)),
390 };
391 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
392 }
393 {
394 uno::Sequence<css::beans::PropertyValue> aArgs = {
395 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
396 comphelper::makePropertyValue(u"FieldCommand"_ustr,
397 uno::Any(u"ADDIN ZOTERO_ITEM old command 2"_ustr)),
398 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"old result 2"_ustr)),
399 };
400 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
401 }
402
403 // When updating those fieldmarks:
404 uno::Sequence<css::beans::PropertyValue> aField1{
405 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
406 comphelper::makePropertyValue(u"FieldCommand"_ustr,
407 uno::Any(u"ADDIN ZOTERO_ITEM new command 1"_ustr)),
408 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"new result 1"_ustr)),
409 };
410 uno::Sequence<css::beans::PropertyValue> aField2{
411 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
412 comphelper::makePropertyValue(u"FieldCommand"_ustr,
413 uno::Any(u"ADDIN ZOTERO_ITEM new command 2"_ustr)),
414 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"new result 2"_ustr)),
415 };
416 uno::Sequence<uno::Sequence<css::beans::PropertyValue>> aFields = { aField1, aField2 };
417 uno::Sequence<css::beans::PropertyValue> aArgs = {
418 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
419 comphelper::makePropertyValue(u"FieldCommandPrefix"_ustr,
420 uno::Any(u"ADDIN ZOTERO_ITEM"_ustr)),
421 comphelper::makePropertyValue(u"Fields"_ustr, uno::Any(aFields)),
422 };
423 dispatchCommand(mxComponent, u".uno:TextFormFields"_ustr, aArgs);
424
425 // Then make sure that the document text contains the new field results:
426 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
427 pWrtShell->SttEndDoc(/*bStt=*/true);
428 SwCursor* pCursor = pWrtShell->GetCursor();
429 OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText();
430 static sal_Unicode const aForbidden[]
431 = { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 };
432 aActual = comphelper::string::removeAny(aActual, aForbidden);
433 // Without the accompanying fix in place, this test would have failed with:
434 // - Expected: new result 1new result 2
435 // - Actual : old result 1old result 2
436 // i.e. the fieldmarks were not updated.
437 CPPUNIT_ASSERT_EQUAL(u"new result 1new result 2"_ustr, aActual);
438 }
439
440 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertBookmark)
441 {
442 // Given an empty document:
443 createSwDoc();
444 SwDoc* pDoc = getSwDoc();
445
446 // When inserting a bookmark with text:
447 OUString aExpectedBookmarkName(u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr);
448 uno::Sequence<css::beans::PropertyValue> aArgs = {
449 comphelper::makePropertyValue(u"Bookmark"_ustr, uno::Any(aExpectedBookmarkName)),
450 comphelper::makePropertyValue(u"BookmarkText"_ustr, uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)),
451 };
452 dispatchCommand(mxComponent, u".uno:InsertBookmark"_ustr, aArgs);
453
454 // Then make sure that we create a bookmark that covers that text:
455 IDocumentMarkAccess& rIDMA = *pDoc->getIDocumentMarkAccess();
456 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), rIDMA.getBookmarksCount());
457 for (auto it = rIDMA.getBookmarksBegin(); it != rIDMA.getBookmarksEnd(); ++it)
458 {
459 sw::mark::MarkBase* pMark = *it;
460 CPPUNIT_ASSERT_EQUAL(aExpectedBookmarkName, pMark->GetName().toString());
461 SwPaM aPam(pMark->GetMarkStart(), pMark->GetMarkEnd());
462 OUString aActualResult = aPam.GetText();
463 // Without the accompanying fix in place, this test would have failed with:
464 // - Expected: aaa\nbbb
465 // - Actual :
466 // i.e. no text was inserted, the bookmark was collapsed.
467 CPPUNIT_ASSERT_EQUAL(u"aaa\nbbb"_ustr, aActualResult);
468 }
469 }
470
471 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testGotoMark)
472 {
473 // Given a document with 2 paragraphs, a bookmark on the second one:
474 createSwDoc();
475 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
476 pWrtShell->SplitNode();
477 pWrtShell->SttEndDoc(/*bStt=*/false);
478 pWrtShell->SetBookmark(vcl::KeyCode(), SwMarkName(u"mybookmark"_ustr));
479 SwNodeOffset nExpected = pWrtShell->GetCursor()->GetPointNode().GetIndex();
480
481 // When jumping to that mark from the doc start:
482 pWrtShell->SttEndDoc(/*bStt=*/true);
483 uno::Sequence<css::beans::PropertyValue> aArgs = {
484 comphelper::makePropertyValue(u"GotoMark"_ustr, uno::Any(u"mybookmark"_ustr)),
485 };
486 dispatchCommand(mxComponent, u".uno:GotoMark"_ustr, aArgs);
487
488 // Then make sure that the final cursor position is at the bookmark:
489 SwNodeOffset nActual = pWrtShell->GetCursor()->GetPointNode().GetIndex();
490 // Without the accompanying fix in place, this test would have failed with:
491 // - Expected: 10 (bookmark)
492 // - Actual : 9 (doc start)
493 // i.e. the actual jump didn't happen.
494 CPPUNIT_ASSERT_EQUAL(nExpected, nActual);
495 }
496
497 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateBookmarks)
498 {
499 // Given a document with 2 bookmarks, first covering "B" and second covering "D":
500 createSwDoc();
501 SwDoc* pDoc = getSwDoc();
502 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
503 pWrtShell->Insert(u"ABCDE"_ustr);
504 pWrtShell->SttEndDoc(/*bStt=*/true);
505 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
506 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
507 pWrtShell->SetBookmark(vcl::KeyCode(), SwMarkName(u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr));
508 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
509 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
510 pWrtShell->SetBookmark(vcl::KeyCode(), SwMarkName(u"ZOTERO_BREF_PRxDGUb4SWXF"_ustr));
511
512 // When updating the content of bookmarks:
513 pWrtShell->SttEndDoc(/*bStt=*/true);
514 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
515 {
516 "BookmarkNamePrefix": {
517 "type": "string",
518 "value": "ZOTERO_BREF_"
519 },
520 "Bookmarks": {
521 "type": "[][]com.sun.star.beans.PropertyValue",
522 "value": [
523 {
524 "Bookmark": {
525 "type": "string",
526 "value": "ZOTERO_BREF_new1"
527 },
528 "BookmarkText": {
529 "type": "string",
530 "value": "new result 1"
531 }
532 },
533 {
534 "Bookmark": {
535 "type": "string",
536 "value": "ZOTERO_BREF_new2"
537 },
538 "BookmarkText": {
539 "type": "string",
540 "value": "new result 2"
541 }
542 }
543 ]
544 }
545 }
546 )json");
547 uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
548 dispatchCommand(mxComponent, u".uno:UpdateBookmarks"_ustr, aArgs);
549
550 // Then make sure that the only paragraph is updated correctly:
551 SwCursor* pCursor = pWrtShell->GetCursor();
552 OUString aActual = pCursor->GetPointNode().GetTextNode()->GetText();
553 // Without the accompanying fix in place, this test would have failed with:
554 // - Expected: Anew result 1Cnew result 2E
555 // - Actual : ABCDE
556 // i.e. the content was not updated.
557 CPPUNIT_ASSERT_EQUAL(u"Anew result 1Cnew result 2E"_ustr, aActual);
558
559 // Without the accompanying fix in place, this test would have failed, ZOTERO_BREF_GiQ7DAWQYWLy
560 // was not renamed to ZOTERO_BREF_new1.
561 auto it = pDoc->getIDocumentMarkAccess()->findMark(SwMarkName(u"ZOTERO_BREF_new1"_ustr));
562 CPPUNIT_ASSERT(it != pDoc->getIDocumentMarkAccess()->getAllMarksEnd());
563 }
564
565 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertFieldmarkReadonly)
566 {
567 // Given a document with a fieldmark, the cursor inside the fieldmark:
568 createSwDoc();
569 uno::Sequence<css::beans::PropertyValue> aArgs = {
570 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
571 comphelper::makePropertyValue(u"FieldCommand"_ustr, uno::Any(u"my command"_ustr)),
572 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"my result"_ustr)),
573 };
574 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
575 SwDoc* pDoc = getSwDoc();
576 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
577 SwCursor* pCursor = pWrtShell->GetCursor();
578 pCursor->SttEndDoc(/*bSttDoc=*/true);
579 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
580
581 // When trying to insert an inner fieldmark:
582 // Without the accompanying fix in place, this test would have crashed.
583 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
584
585 // Then make sure the read-only content refuses to accept that inner fieldmark, so we still have
586 // just one:
587 IDocumentMarkAccess& rIDMA = *pDoc->getIDocumentMarkAccess();
588 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), rIDMA.getFieldmarksCount());
589 }
590
591 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureTransformChart)
592 {
593 createSwDoc("docStructureChartExampleOriginal.odt");
594 static constexpr OUString aJson = uR"json(
595 {
596 "Transforms": [
597 {"Charts.ByEmbedIndex.0": [
598 {"modifyrow.1": [ 19, 15 ]},
599 {"datayx.3.1": 37},
600 {"deleterow.0": ""},
601 {"deleterow.1": ""},
602 {"insertrow.0": [ 15, 17 ]},
603 {"setrowdesc.0": "Paul"},
604 {"insertrow.2": [ 19, 22 ]},
605 {"setrowdesc.2": "Barbara"},
606 {"insertrow.4": [ 29, 27 ]},
607 {"setrowdesc.4": "Elizabeth"},
608 {"insertrow.5": [ 14, 26 ]},
609 {"setrowdesc.5": "William"},
610 {"insertcolumn.1": [ 1,2,3,4,5,6 ]},
611 {"insertcolumn.0": [ 2,1,2,1,2,1 ]},
612 {"insertcolumn.4": [ 7,7,7,7,7,7 ]},
613 {"setcolumndesc.4": "c4"},
614 {"setcolumndesc.0": "c0"},
615 {"setcolumndesc.2": "c2"}
616 ]},
617 {"Charts.BySubTitle.Subtitle2": {
618 "deletecolumn.3": ""
619 }},
620 {"Charts.ByEmbedName.Object3": [
621 {"resize": [ 12, 3 ]},
622 {"setrowdesc": [
623 "United Kingdom",
624 "United States of America",
625 "Canada",
626 "Brazil",
627 "Germany",
628 "India",
629 "Japan",
630 "Sudan",
631 "Norway",
632 "Italy",
633 "France",
634 "Egypt"
635 ]},
636 {"modifycolumn.0": [ 12, 9, 8, 5, 5, 4, 3, 3, 2, 2, 1, 1]},
637 {"resize": [ 12, 2 ]}
638 ]},
639 {"Charts.ByTitle.Fixed issues": {
640 "data": [ [ 3,1,2 ],
641 [ 2,0,1 ],
642 [ 3,2,0 ],
643 [ 2,1,1 ],
644 [ 4,2,2 ],
645 [ 2,2,1 ],
646 [ 3,2,0,0 ],
647 [ 3,1,2,1 ],
648 [ 3,3,1,0,1 ],
649 [ 2,1,0,1,2 ],
650 [ 4,2,1,2,2,3 ],
651 [ 2,1,0,0,1,4 ],
652 [ 3,2,2,1,3,2 ],
653 [ 4,2,1,1,2,3 ],
654 [ 3,3,2,0,3,5 ],
655 [ 3,3,1,2,2,3 ],
656 [ 5,1,1,1,1,4 ],
657 [ 2,2,2,1,2,3 ] ],
658 "setrowdesc": ["2023.01",".02",".03",".04",".05",".06",".07",".08",".09",".10",".11",".12","2023.01",".02",".03",".04",".05",".06"],
659 "setcolumndesc": ["Jennifer", "Charles", "Thomas", "Maria", "Lisa", "Daniel"]
660 }}
661 ]
662 }
663 )json"_ustr;
664
665 //transform
666 uno::Sequence<css::beans::PropertyValue> aArgs = {
667 comphelper::makePropertyValue(u"DataJson"_ustr, aJson),
668 };
669 dispatchCommand(mxComponent, u".uno:TransformDocumentStructure"_ustr, aArgs);
670
671 // Check transformed values of the 3 chart
672 for (int nShape = 0; nShape < 3; nShape++)
673 {
674 uno::Reference<text::XTextEmbeddedObjectsSupplier> xEOS(mxComponent, uno::UNO_QUERY);
675 CPPUNIT_ASSERT(xEOS.is());
676 uno::Reference<container::XIndexAccess> xEmbeddeds(xEOS->getEmbeddedObjects(),
677 uno::UNO_QUERY);
678 CPPUNIT_ASSERT(xEmbeddeds.is());
679
680 uno::Reference<beans::XPropertySet> xShapeProps(xEmbeddeds->getByIndex(nShape),
681 uno::UNO_QUERY);
682 CPPUNIT_ASSERT(xShapeProps.is());
683
684 uno::Reference<frame::XModel> xDocModel;
685 xShapeProps->getPropertyValue(u"Model"_ustr) >>= xDocModel;
686 CPPUNIT_ASSERT(xDocModel.is());
687
688 uno::Reference<chart2::XChartDocument> xChartDoc(xDocModel, uno::UNO_QUERY);
689 CPPUNIT_ASSERT(xChartDoc.is());
690
691 uno::Reference<chart::XChartDataArray> xDataArray(xChartDoc->getDataProvider(),
692 uno::UNO_QUERY);
693 CPPUNIT_ASSERT(xDataArray.is());
694
695 switch (nShape)
696 {
697 case 0:
698 {
699 std::vector<std::vector<double>> aValues = {
700 { 2, 15, 1, 7 }, { 1, 19, 2, 7 }, { 2, 19, 3, 7 },
701 { 1, 25, 4, 7 }, { 2, 29, 5, 7 }, { 1, 14, 6, 7 },
702 };
703 // RowDescriptions
704 std::vector<OUString> aRowDescsValues
705 = { u"Paul"_ustr, u"Mary"_ustr, u"Barbara"_ustr,
706 u"David"_ustr, u"Elizabeth"_ustr, u"William"_ustr };
707 // ColumnDescriptions
708 std::vector<OUString> aColDescsValues
709 = { u"c0"_ustr, u"2022"_ustr, u"c2"_ustr, u"c4"_ustr };
710
711 uno::Sequence<uno::Sequence<double>> aData = xDataArray->getData();
712 for (size_t nY = 0; nY < aValues.size(); nY++)
713 {
714 for (size_t nX = 0; nX < aValues[nY].size(); nX++)
715 {
716 CPPUNIT_ASSERT_EQUAL(aData[nY][nX], aValues[nY][nX]);
717 }
718 }
719
720 uno::Sequence<OUString> aColDescs = xDataArray->getColumnDescriptions();
721 for (size_t i = 0; i < aColDescsValues.size(); i++)
722 {
723 CPPUNIT_ASSERT_EQUAL(aColDescs[i], aColDescsValues[i]);
724 }
725 uno::Sequence<OUString> aRowDescs = xDataArray->getRowDescriptions();
726 for (size_t i = 0; i < aRowDescsValues.size(); i++)
727 {
728 CPPUNIT_ASSERT_EQUAL(aRowDescs[i], aRowDescsValues[i]);
729 }
730 }
731 break;
732 case 1:
733 {
734 uno::Sequence<uno::Sequence<double>> aData = xDataArray->getData();
735 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(18), aData.getLength());
736 uno::Sequence<double>* pRows = aData.getArray();
737 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), pRows[0].getLength());
738 }
739 break;
740 case 2:
741 {
742 uno::Sequence<uno::Sequence<double>> aData = xDataArray->getData();
743 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(12), aData.getLength());
744 uno::Sequence<double>* pRows = aData.getArray();
745 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), pRows[0].getLength());
746 }
747 break;
748 default:
749 break;
750 }
751 }
752 }
753
754 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureExtractChart)
755 {
756 createSwDoc("docStructureChartExampleOriginal.odt");
757
758 //extract
759 tools::JsonWriter aJsonWriter;
760 std::string_view aCommand(".uno:ExtractDocumentStructure?filter=charts");
761 getSwTextDoc()->getCommandValues(aJsonWriter, aCommand);
762
763 OString aExpectedStr
764 = "{ \"DocStructure\": { \"Charts.ByEmbedIndex.0\": { \"name\": \"Object1\", \"title\": "
765 "\"Paid leave days\", \"subtitle\": \"Subtitle2\", \"RowDescriptions\": [ \"James\", "
766 "\"Mary\", \"Patricia\", \"David\"], \"ColumnDescriptions\": [ \"2022\", \"2023\"], "
767 "\"DataValues\": [ [ \"22\", \"24\"], [ \"18\", \"16\"], [ \"32\", \"32\"], "
768 "[ \"25\", \"23\"]]}, "
769 "\"Charts.ByEmbedIndex.1\": { \"name\": \"Object2\", \"title\": \"Fixed issues\", "
770 "\"subtitle\": \"Subtitle1\", \"RowDescriptions\": [ \"\"], \"ColumnDescriptions\": [ \" "
771 "\"], \"DataValues\": [ [ \"NaN\"]]}, \"Charts.ByEmbedIndex.2\": { \"name\": "
772 "\"Object3\", \"title\": \"Employees from countries\", \"subtitle\": \"Subtitle3\", "
773 "\"RowDescriptions\": [ \"\"], \"ColumnDescriptions\": [ \"Column 1\"], \"DataValues\": "
774 "[ [ \"NaN\"]]}}}"_ostr;
775
776 CPPUNIT_ASSERT_EQUAL(aExpectedStr, aJsonWriter.finishAndGetAsOString());
777 }
778
779 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureDocProperties)
780 {
781 createSwDoc("docStructureChartExampleOriginal.odt");
782 static constexpr OUString aJson = uR"json(
783 {
784 "Transforms": [
785 { "DocumentProperties": {
786 "Author":"Author TxT",
787 "Generator":"Generator TxT",
788 "CreationDate":"2024-01-21T14:45:00",
789 "Title":"Title TxT",
790 "Subject":"Subject TxT",
791 "Description":"Description TxT",
792 "Keywords": [ ],
793 "Language":"en-GB",
794 "ModifiedBy":"ModifiedBy TxT",
795 "ModificationDate":"2024-05-23T10:05:50.159530766",
796 "PrintedBy":"PrintedBy TxT",
797 "PrintDate":"0000-00-00T00:00:00",
798 "TemplateName":"TemplateName TxT",
799 "TemplateURL":"TemplateURL TxT",
800 "TemplateDate":"0000-00-00T00:00:00",
801 "AutoloadURL":"",
802 "AutoloadSecs": 0,
803 "DefaultTarget":"DefaultTarget TxT",
804 "DocumentStatistics": {
805 "PageCount": 300,
806 "TableCount": 60,
807 "ImageCount": 10,
808 "ObjectCount": 0,
809 "ParagraphCount": 2880,
810 "WordCount": 78680,
811 "CharacterCount": 485920,
812 "NonWhitespaceCharacterCount": 411520
813 },
814 "EditingCycles":12,
815 "EditingDuration":12345,
816 "Contributor":["Contributor1 TxT","Contributor2 TXT"],
817 "Coverage":"Coverage TxT",
818 "Identifier":"Identifier TxT",
819 "Publisher":["Publisher TxT","Publisher2 TXT"],
820 "Relation":["Relation TxT","Relation2 TXT"],
821 "Rights":"Rights TxT",
822 "Source":"Source TxT",
823 "Type":"Type TxT",
824 "UserDefinedProperties": [
825 {"Add.NewPropName Str": {
826 "type": "string",
827 "value": "this is a string"
828 }},
829 {"Add.NewPropName Str": {
830 "type": "boolean",
831 "value": false
832 }},
833 {"Add.NewPropName Bool": {
834 "type": "boolean",
835 "value": true
836 }},
837 {"Add.NewPropName Numb": {
838 "type": "long",
839 "value": 1245
840 }},
841 {"Add.NewPropName float": {
842 "type": "float",
843 "value": 12.45
844 }},
845 {"Add.NewPropName Double": {
846 "type": "double",
847 "value": 124.578
848 }},
849 {"Delete": "NewPropName Double"}
850 ]
851 } }
852 ]
853 }
854 )json"_ustr;
855
856 uno::Sequence<css::beans::PropertyValue> aArgs = {
857 comphelper::makePropertyValue(u"DataJson"_ustr, aJson),
858 };
859 dispatchCommand(mxComponent, u".uno:TransformDocumentStructure"_ustr, aArgs);
860
861 tools::JsonWriter aJsonWriter;
862 std::string_view aCommand(".uno:ExtractDocumentStructure?filter=docprops");
863 auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
864 pXTextDocument->getCommandValues(aJsonWriter, aCommand);
865
866 OString aExpectedStr
867 = "{ \"DocStructure\": { \"DocumentProperties\": { \"Author\": \"Author TxT\", "
868 "\"Generator\": \"Generator TxT\", \"CreationDate\": \"2024-01-21T14:45:00\", "
869 "\"Title\": \"Title TxT\", \"Subject\": \"Subject TxT\", \"Description\": "
870 "\"Description TxT\", \"Keywords\": [ ], \"Language\": \"en-GB\", "
871 "\"ModifiedBy\": \"ModifiedBy TxT\", \"ModificationDate\": "
872 "\"2024-05-23T10:05:50.159530766\", \"PrintedBy\": \"PrintedBy TxT\", \"PrintDate\": "
873 "\"0000-00-00T00:00:00\", \"TemplateName\": \"TemplateName TxT\", \"TemplateURL\": "
874 "\"TemplateURL TxT\", \"TemplateDate\": \"0000-00-00T00:00:00\", \"AutoloadURL\": \"\", "
875 "\"AutoloadSecs\": 0, \"DefaultTarget\": \"DefaultTarget TxT\", \"DocumentStatistics\": "
876 "{ \"PageCount\": 300, \"TableCount\": 60, \"ImageCount\": 10, \"ObjectCount\": 0, "
877 "\"ParagraphCount\": 2880, \"WordCount\": 78680, \"CharacterCount\": 485920, "
878 "\"NonWhitespaceCharacterCount\": 411520}, \"EditingCycles\": 12, \"EditingDuration\": "
879 "12345, \"Contributor\": [ \"Contributor1 TxT\", \"Contributor2 TXT\"], \"Coverage\": "
880 "\"Coverage TxT\", \"Identifier\": \"Identifier TxT\", \"Publisher\": [ "
881 "\"Publisher TxT\", \"Publisher2 TXT\"], \"Relation\": [ \"Relation TxT\", "
882 "\"Relation2 TXT\"], \"Rights\": \"Rights TxT\", \"Source\": \"Source TxT\", \"Type\": "
883 "\"Type TxT\", \"UserDefinedProperties\": { \"NewPropName Bool\": { \"type\": "
884 "\"boolean\", \"value\": true}, \"NewPropName Numb\": { \"type\": \"long\", "
885 "\"value\": 1245}, \"NewPropName Str\": { \"type\": \"boolean\", \"value\": false}, "
886 "\"NewPropName float\": { \"type\": \"float\", \"value\": 12.45}}}}}"_ostr;
887
888 CPPUNIT_ASSERT_EQUAL(aExpectedStr, aJsonWriter.finishAndGetAsOString());
889 }
890
891 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureExtractRedlines)
892 {
893 createSwDoc("three-changes.fodt");
894
895 boost::property_tree::ptree tree;
896
897 // 1. No filter arguments
898 {
899 // extract
900 tools::JsonWriter aJsonWriter;
901 std::string_view aCommand(".uno:ExtractDocumentStructure?filter=trackchanges");
902 getSwTextDoc()->getCommandValues(aJsonWriter, aCommand);
903
904 std::stringstream aStream(std::string(aJsonWriter.finishAndGetAsOString()));
905 boost::property_tree::read_json(aStream, tree);
906 }
907
908 CPPUNIT_ASSERT_EQUAL(size_t(1), tree.size());
909 boost::property_tree::ptree docStructure = tree.get_child("DocStructure");
910 CPPUNIT_ASSERT_EQUAL(size_t(3), docStructure.size());
911 auto it = docStructure.begin();
912
913 {
914 // First change
915 CPPUNIT_ASSERT(it != docStructure.end());
916 const auto & [ name, change ] = *it;
917 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.0"s, name);
918 CPPUNIT_ASSERT_EQUAL(size_t(8), change.size());
919 CPPUNIT_ASSERT_EQUAL("Delete"s, change.get<std::string>("type"));
920 CPPUNIT_ASSERT_EQUAL("2025-06-16T14:08:27"s, change.get<std::string>("dateTime"));
921 CPPUNIT_ASSERT_EQUAL("Mike"s, change.get<std::string>("author"));
922 CPPUNIT_ASSERT_EQUAL("Delete “Donec”"s, change.get<std::string>("description"));
923 CPPUNIT_ASSERT_EQUAL(""s, change.get<std::string>("comment"));
924 CPPUNIT_ASSERT(bool(change.find("startPageNumber") == change.not_found()));
925 auto text_before = change.get<std::string>("textBefore");
926 CPPUNIT_ASSERT_EQUAL(size_t(200), text_before.size());
927 CPPUNIT_ASSERT(text_before.ends_with(" egestas. "));
928 auto text_after = change.get<std::string>("textAfter");
929 CPPUNIT_ASSERT_EQUAL(size_t(200), text_after.size());
930 CPPUNIT_ASSERT(text_after.starts_with(" blandit "));
931 CPPUNIT_ASSERT_EQUAL("Donec"s, change.get<std::string>("textChanged"));
932 ++it;
933 }
934
935 {
936 // Second change
937 CPPUNIT_ASSERT(it != docStructure.end());
938 const auto & [ name, change ] = *it;
939 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.1"s, name);
940 CPPUNIT_ASSERT_EQUAL(size_t(8), change.size());
941 CPPUNIT_ASSERT_EQUAL("Format"s, change.get<std::string>("type"));
942 CPPUNIT_ASSERT_EQUAL("2025-06-17T12:41:00"s, change.get<std::string>("dateTime"));
943 CPPUNIT_ASSERT_EQUAL("Mike"s, change.get<std::string>("author"));
944 CPPUNIT_ASSERT_EQUAL("Attributes changed"s, change.get<std::string>("description"));
945 CPPUNIT_ASSERT_EQUAL(""s, change.get<std::string>("comment"));
946 CPPUNIT_ASSERT(bool(change.find("startPageNumber") == change.not_found()));
947 auto text_before = change.get<std::string>("textBefore");
948 CPPUNIT_ASSERT_EQUAL(size_t(200), text_before.size());
949 CPPUNIT_ASSERT(text_before.ends_with(" arcu, nec "));
950 auto text_after = change.get<std::string>("textAfter");
951 CPPUNIT_ASSERT_EQUAL(size_t(200), text_after.size());
952 CPPUNIT_ASSERT(text_after.starts_with(" eros "));
953 CPPUNIT_ASSERT_EQUAL("pellentesque"s, change.get<std::string>("textChanged"));
954 ++it;
955 }
956
957 {
958 // Third change
959 CPPUNIT_ASSERT(it != docStructure.end());
960 const auto & [ name, change ] = *it;
961 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.2"s, name);
962 CPPUNIT_ASSERT_EQUAL(size_t(8), change.size());
963 CPPUNIT_ASSERT_EQUAL("Insert"s, change.get<std::string>("type"));
964 CPPUNIT_ASSERT_EQUAL("2025-06-17T12:41:19"s, change.get<std::string>("dateTime"));
965 CPPUNIT_ASSERT_EQUAL("Mike"s, change.get<std::string>("author"));
966 CPPUNIT_ASSERT_EQUAL("Insert “ Sapienti sat.”"s, change.get<std::string>("description"));
967 CPPUNIT_ASSERT_EQUAL(""s, change.get<std::string>("comment"));
968 CPPUNIT_ASSERT(bool(change.find("startPageNumber") == change.not_found()));
969 auto text_before = change.get<std::string>("textBefore");
970 CPPUNIT_ASSERT_EQUAL(size_t(200), text_before.size());
971 CPPUNIT_ASSERT(text_before.ends_with(" est orci."));
972 auto text_after = change.get<std::string>("textAfter");
973 CPPUNIT_ASSERT(text_after.empty());
974 CPPUNIT_ASSERT_EQUAL(" Sapienti sat."s, change.get<std::string>("textChanged"));
975 ++it;
976 }
977
978 CPPUNIT_ASSERT(bool(it == docStructure.end()));
979
980 // 2. Test contextLen and startPageNumber filter arguments
981 {
982 // extract
983 tools::JsonWriter aJsonWriter;
984 std::string_view aCommand(".uno:ExtractDocumentStructure?filter=trackchanges,foo:bar, "
985 "contextLen: 15, startPageNumber :true ,,");
986 getSwTextDoc()->getCommandValues(aJsonWriter, aCommand);
987
988 std::stringstream aStream(std::string(aJsonWriter.finishAndGetAsOString()));
989 boost::property_tree::read_json(aStream, tree);
990 }
991
992 CPPUNIT_ASSERT_EQUAL(size_t(1), tree.size());
993 docStructure = tree.get_child("DocStructure");
994 CPPUNIT_ASSERT_EQUAL(size_t(3), docStructure.size());
995 it = docStructure.begin();
996
997 {
998 // First change
999 CPPUNIT_ASSERT(it != docStructure.end());
1000 const auto & [ name, change ] = *it;
1001 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.0"s, name);
1002 CPPUNIT_ASSERT_EQUAL("2"s, change.get<std::string>("startPageNumber"));
1003 CPPUNIT_ASSERT_EQUAL("ursus egestas. "s, change.get<std::string>("textBefore"));
1004 CPPUNIT_ASSERT_EQUAL(" blandit auctor"s, change.get<std::string>("textAfter"));
1005 ++it;
1006 }
1007
1008 {
1009 // Second change
1010 CPPUNIT_ASSERT(it != docStructure.end());
1011 const auto & [ name, change ] = *it;
1012 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.1"s, name);
1013 CPPUNIT_ASSERT_EQUAL("2"s, change.get<std::string>("startPageNumber"));
1014 CPPUNIT_ASSERT_EQUAL("ctor arcu, nec "s, change.get<std::string>("textBefore"));
1015 CPPUNIT_ASSERT_EQUAL(" eros molestie "s, change.get<std::string>("textAfter"));
1016 ++it;
1017 }
1018
1019 {
1020 // Third change
1021 CPPUNIT_ASSERT(it != docStructure.end());
1022 const auto & [ name, change ] = *it;
1023 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.2"s, name);
1024 CPPUNIT_ASSERT_EQUAL("2"s, change.get<std::string>("startPageNumber"));
1025 CPPUNIT_ASSERT_EQUAL("esque est orci."s, change.get<std::string>("textBefore"));
1026 CPPUNIT_ASSERT_EQUAL(""s, change.get<std::string>("textAfter"));
1027 ++it;
1028 }
1029
1030 CPPUNIT_ASSERT(bool(it == docStructure.end()));
1031 }
1032
1033 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureExtractRedlines_textBeforeAfter)
1034 {
1035 // The document here has a series of insert/delete/format changes having different times.
1036 // This test checks, that for each extracted data, we produce textBefore / textAfter in proper
1037 // form: all newer changes are pretended to never had happened / as if rejected; other changes
1038 // are shown as if accepted. This produces the correct context for each current change.
1039 createSwDoc("nearby-changes.fodt");
1040
1041 boost::property_tree::ptree tree;
1042
1043 // extract using the context length enough to cover the whole document text
1044 tools::JsonWriter aJsonWriter;
1045 std::string_view aCommand(".uno:ExtractDocumentStructure?filter=trackchanges,contextLen:100");
1046 getSwTextDoc()->getCommandValues(aJsonWriter, aCommand);
1047
1048 std::stringstream aStream(std::string(aJsonWriter.finishAndGetAsOString()));
1049 boost::property_tree::read_json(aStream, tree);
1050
1051 boost::property_tree::ptree docStructure = tree.get_child("DocStructure");
1052 CPPUNIT_ASSERT_EQUAL(size_t(5), docStructure.size());
1053 auto it = docStructure.begin();
1054
1055 // The text before covers older changes. It includes text that was inserted earlier; it
1056 // excludes text that was deleted earlier.
1057 // The text after covers newer changes. It includes text that was deleted later; it
1058 // excludes text that was added later.
1059 // The formatting changes don't affect text.
1060 // The whole text looks like "a del1 b ins2 c fmt3 d del4 e ins5 f"
1061 // where <delN>, <insN>, <fmtN> are successive deletions/insertions/formattings.
1062
1063 {
1064 CPPUNIT_ASSERT(it != docStructure.end());
1065 const auto & [ name, change ] = *it;
1066 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.0"s, name);
1067 CPPUNIT_ASSERT_EQUAL("Delete"s, change.get<std::string>("type"));
1068 CPPUNIT_ASSERT_EQUAL("2025-07-01T12:00:00"s, change.get<std::string>("dateTime"));
1069 CPPUNIT_ASSERT_EQUAL("del1"s, change.get<std::string>("textChanged"));
1070 CPPUNIT_ASSERT_EQUAL("a "s, change.get<std::string>("textBefore"));
1071 CPPUNIT_ASSERT_EQUAL(" b c fmt3 d del4 e f"s, change.get<std::string>("textAfter"));
1072 ++it;
1073 }
1074
1075 {
1076 CPPUNIT_ASSERT(it != docStructure.end());
1077 const auto & [ name, change ] = *it;
1078 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.1"s, name);
1079 CPPUNIT_ASSERT_EQUAL("Insert"s, change.get<std::string>("type"));
1080 CPPUNIT_ASSERT_EQUAL("2025-07-01T12:30:00"s, change.get<std::string>("dateTime"));
1081 CPPUNIT_ASSERT_EQUAL("ins2"s, change.get<std::string>("textChanged"));
1082 CPPUNIT_ASSERT_EQUAL("a b "s, change.get<std::string>("textBefore"));
1083 CPPUNIT_ASSERT_EQUAL(" c fmt3 d del4 e f"s, change.get<std::string>("textAfter"));
1084 ++it;
1085 }
1086
1087 {
1088 CPPUNIT_ASSERT(it != docStructure.end());
1089 const auto & [ name, change ] = *it;
1090 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.2"s, name);
1091 CPPUNIT_ASSERT_EQUAL("Format"s, change.get<std::string>("type"));
1092 CPPUNIT_ASSERT_EQUAL("2025-07-01T13:00:00"s, change.get<std::string>("dateTime"));
1093 CPPUNIT_ASSERT_EQUAL("fmt3"s, change.get<std::string>("textChanged"));
1094 CPPUNIT_ASSERT_EQUAL("a b ins2 c "s, change.get<std::string>("textBefore"));
1095 CPPUNIT_ASSERT_EQUAL(" d del4 e f"s, change.get<std::string>("textAfter"));
1096 ++it;
1097 }
1098
1099 {
1100 CPPUNIT_ASSERT(it != docStructure.end());
1101 const auto & [ name, change ] = *it;
1102 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.3"s, name);
1103 CPPUNIT_ASSERT_EQUAL("Delete"s, change.get<std::string>("type"));
1104 CPPUNIT_ASSERT_EQUAL("2025-07-01T13:30:00"s, change.get<std::string>("dateTime"));
1105 CPPUNIT_ASSERT_EQUAL("del4"s, change.get<std::string>("textChanged"));
1106 CPPUNIT_ASSERT_EQUAL("a b ins2 c fmt3 d "s, change.get<std::string>("textBefore"));
1107 CPPUNIT_ASSERT_EQUAL(" e f"s, change.get<std::string>("textAfter"));
1108 ++it;
1109 }
1110
1111 {
1112 CPPUNIT_ASSERT(it != docStructure.end());
1113 const auto & [ name, change ] = *it;
1114 CPPUNIT_ASSERT_EQUAL("TrackChanges.ByIndex.4"s, name);
1115 CPPUNIT_ASSERT_EQUAL("Insert"s, change.get<std::string>("type"));
1116 CPPUNIT_ASSERT_EQUAL("2025-07-01T14:00:00"s, change.get<std::string>("dateTime"));
1117 CPPUNIT_ASSERT_EQUAL("ins5"s, change.get<std::string>("textChanged"));
1118 CPPUNIT_ASSERT_EQUAL("a b ins2 c fmt3 d e "s, change.get<std::string>("textBefore"));
1119 CPPUNIT_ASSERT_EQUAL(" f"s, change.get<std::string>("textAfter"));
1120 ++it;
1121 }
1122
1123 CPPUNIT_ASSERT(bool(it == docStructure.end()));
1124 }
1125
1126 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureUnoCommand)
1127 {
1128 // 1. Create a document;
1129 // 2. Check that it's not in change tracking mode;
1130 // 3. Perform a "TransformDocumentStructure" with a "UnoCommand" of ".uno:TrackChanges";
1131 // 4. Check that it's in change tracking mode now.
1132
1133 createSwDoc();
1134
1135 // Let comphelper::dispatchCommand (in SfxLokHelper::dispatchUnoCommand) find the frame
1136 auto xDesktop = frame::Desktop::create(comphelper::getProcessComponentContext());
1137 auto pFrame = getSwDocShell()->GetFrame();
1138 CPPUNIT_ASSERT(pFrame);
1139 xDesktop->setActiveFrame(pFrame->GetFrame().GetFrameInterface());
1140
1141 CPPUNIT_ASSERT(!getSwDoc()->getIDocumentRedlineAccess().IsRedlineOn());
1142
1143 static constexpr OUString aJson = uR"json(
1144 {
1145 "UnoCommand": {
1146 "name": ".uno:TrackChanges",
1147 "arguments": {
1148 "TrackChanges": {
1149 "type": "boolean",
1150 "value": "true"
1151 }
1152 }
1153 }
1154 }
1155 )json"_ustr;
1156
1157 dispatchCommand(mxComponent, u".uno:TransformDocumentStructure"_ustr,
1158 { comphelper::makePropertyValue(u"DataJson"_ustr, aJson) });
1159
1160 CPPUNIT_ASSERT(getSwDoc()->getIDocumentRedlineAccess().IsRedlineOn());
1161 }
1162
1163 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateRefmarks)
1164 {
1165 // Given a document with two refmarks, one is not interesting the other is a citation:
1166 createSwDoc();
1167 uno::Sequence<css::beans::PropertyValue> aArgs = {
1168 comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
1169 comphelper::makePropertyValue(u"Name"_ustr, uno::Any(u"some other old refmark"_ustr)),
1170 comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"some other old content"_ustr)),
1171 };
1172 dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
1173 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1174 pWrtShell->SttEndDoc(/*bStt=*/false);
1175 pWrtShell->SplitNode();
1176 pWrtShell->SttEndDoc(/*bStt=*/false);
1177 aArgs = {
1178 comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
1179 comphelper::makePropertyValue(u"Name"_ustr,
1180 uno::Any(u"ZOTERO_ITEM CSL_CITATION {} old refmark"_ustr)),
1181 comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"old content"_ustr)),
1182 };
1183 dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
1184
1185 // When updating that refmark:
1186 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1187 {
1188 "TypeName": {
1189 "type": "string",
1190 "value": "SetRef"
1191 },
1192 "NamePrefix": {
1193 "type": "string",
1194 "value": "ZOTERO_ITEM CSL_CITATION"
1195 },
1196 "Fields": {
1197 "type": "[][]com.sun.star.beans.PropertyValue",
1198 "value": [
1199 {
1200 "Name": {
1201 "type": "string",
1202 "value": "ZOTERO_ITEM CSL_CITATION {} new refmark"
1203 },
1204 "Content": {
1205 "type": "string",
1206 "value": "new content"
1207 }
1208 }
1209 ]
1210 }
1211 }
1212 )json");
1213 aArgs = comphelper::containerToSequence(aArgsVec);
1214 dispatchCommand(mxComponent, u".uno:UpdateFields"_ustr, aArgs);
1215
1216 // Then make sure that the document text features the new content:
1217 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
1218 // Without the accompanying fix in place, this test would have failed with:
1219 // - Expected: new content
1220 // - Actual : old content
1221 // i.e. the doc content was not updated.
1222 CPPUNIT_ASSERT_EQUAL(u"new content"_ustr, pTextNode->GetText());
1223 }
1224
1225 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateFieldmark)
1226 {
1227 // Given a document with a fieldmark:
1228 createSwDoc();
1229 uno::Sequence<css::beans::PropertyValue> aArgs = {
1230 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
1231 comphelper::makePropertyValue(u"FieldCommand"_ustr,
1232 uno::Any(u"ADDIN ZOTERO_ITEM old command 1"_ustr)),
1233 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"old result 1"_ustr)),
1234 };
1235 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
1236
1237 // When updating that fieldmark to have new field command & result:
1238 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1239 pWrtShell->SttEndDoc(/*bStt=*/false);
1240 pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
1241 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1242 {
1243 "FieldType": {
1244 "type": "string",
1245 "value": "vnd.oasis.opendocument.field.UNHANDLED"
1246 },
1247 "FieldCommandPrefix": {
1248 "type": "string",
1249 "value": "ADDIN ZOTERO_ITEM"
1250 },
1251 "Field": {
1252 "type": "[]com.sun.star.beans.PropertyValue",
1253 "value": {
1254 "FieldType": {
1255 "type": "string",
1256 "value": "vnd.oasis.opendocument.field.UNHANDLED"
1257 },
1258 "FieldCommand": {
1259 "type": "string",
1260 "value": "ADDIN ZOTERO_ITEM new command 1"
1261 },
1262 "FieldResult": {
1263 "type": "string",
1264 "value": "new result 1"
1265 }
1266 }
1267 }
1268 }
1269 )json");
1270 aArgs = comphelper::containerToSequence(aArgsVec);
1271 dispatchCommand(mxComponent, u".uno:UpdateTextFormField"_ustr, aArgs);
1272
1273 // Then make sure that the document text is updated accordingly:
1274 SwCursor* pCursor = pWrtShell->GetCursor();
1275 OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText();
1276 static sal_Unicode const aForbidden[]
1277 = { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 };
1278 aActual = comphelper::string::removeAny(aActual, aForbidden);
1279 // Without the accompanying fix in place, this test would have failed with:
1280 // - Expected: new result 1
1281 // - Actual : old result 1
1282 // i.e. the document text was not updated.
1283 CPPUNIT_ASSERT_EQUAL(u"new result 1"_ustr, aActual);
1284 }
1285
1286 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateSections)
1287 {
1288 // Given a document with a section:
1289 createSwDoc();
1290 uno::Sequence<css::beans::PropertyValue> aArgs = {
1291 comphelper::makePropertyValue(u"RegionName"_ustr,
1292 uno::Any(u"ZOTERO_BIBL {} CSL_BIBLIOGRAPHY RNDold"_ustr)),
1293 comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"old content"_ustr)),
1294 };
1295 dispatchCommand(mxComponent, u".uno:InsertSection"_ustr, aArgs);
1296
1297 // When updating that section:
1298 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1299 {
1300 "SectionNamePrefix": {
1301 "type": "string",
1302 "value": "ZOTERO_BIBL"
1303 },
1304 "Sections": {
1305 "type": "[][]com.sun.star.beans.PropertyValue",
1306 "value": [
1307 {
1308 "RegionName": {
1309 "type": "string",
1310 "value": "ZOTERO_BIBL {} CSL_BIBLIOGRAPHY RNDnew"
1311 },
1312 "Content": {
1313 "type": "string",
1314 "value": "new content"
1315 }
1316 }
1317 ]
1318 }
1319 }
1320 )json");
1321 aArgs = comphelper::containerToSequence(aArgsVec);
1322 dispatchCommand(mxComponent, u".uno:UpdateSections"_ustr, aArgs);
1323
1324 // Then make sure that the section is updated:
1325 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1326 pWrtShell->SttEndDoc(/*bStt=*/true);
1327 pWrtShell->EndOfSection(/*bSelect=*/true);
1328 SwCursor* pCursor = pWrtShell->GetCursor();
1329 OUString aActualResult = pCursor->GetText();
1330 // Without the accompanying fix in place, this test would have failed with:
1331 // - Expected: new content
1332 // - Actual : old content
1333 // i.e. the content wasn't updated.
1334 CPPUNIT_ASSERT_EQUAL(u"new content"_ustr, aActualResult);
1335 }
1336
1337 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDeleteFieldmarks)
1338 {
1339 // Given a document with 2 fieldmarks:
1340 createSwDoc();
1341 {
1342 uno::Sequence<css::beans::PropertyValue> aArgs = {
1343 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
1344 comphelper::makePropertyValue(u"FieldCommand"_ustr,
1345 uno::Any(u"ADDIN ZOTERO_ITEM old command 1"_ustr)),
1346 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result 1"_ustr)),
1347 };
1348 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
1349 }
1350 {
1351 uno::Sequence<css::beans::PropertyValue> aArgs = {
1352 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
1353 comphelper::makePropertyValue(u"FieldCommand"_ustr,
1354 uno::Any(u"ADDIN ZOTERO_ITEM old command 2"_ustr)),
1355 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result 2"_ustr)),
1356 };
1357 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
1358 }
1359
1360 // When deleting those fieldmarks:
1361 uno::Sequence<css::beans::PropertyValue> aArgs
1362 = { comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
1363 comphelper::makePropertyValue(u"FieldCommandPrefix"_ustr,
1364 uno::Any(u"ADDIN ZOTERO_ITEM"_ustr)) };
1365 dispatchCommand(mxComponent, u".uno:DeleteTextFormFields"_ustr, aArgs);
1366
1367 // Then make sure that the document doesn't contain fields anymore:
1368 SwDoc* pDoc = getSwDoc();
1369 // Without the accompanying fix in place, this test would have failed with:
1370 // - Expected: 0
1371 // - Actual : 2
1372 // i.e. the fieldmarks were not deleted.
1373 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0),
1374 pDoc->getIDocumentMarkAccess()->getAllMarksCount());
1375 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1376 pWrtShell->SttEndDoc(/*bStt=*/true);
1377 SwCursor* pCursor = pWrtShell->GetCursor();
1378 OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText();
1379 CPPUNIT_ASSERT_EQUAL(u"result 1result 2"_ustr, aActual);
1380 }
1381
1382 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateBookmark)
1383 {
1384 // Given a document with a bookmarks, covering "BC":
1385 createSwDoc();
1386 SwDoc* pDoc = getSwDoc();
1387 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1388 pWrtShell->Insert(u"ABCD"_ustr);
1389 pWrtShell->SttEndDoc(/*bStt=*/true);
1390 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
1391 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 2, /*bBasicCall=*/false);
1392 pWrtShell->SetBookmark(vcl::KeyCode(), SwMarkName(u"ZOTERO_BREF_old"_ustr));
1393
1394 // When updating the content of the bookmark under the cursor:
1395 pWrtShell->SttEndDoc(/*bStt=*/true);
1396 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 2, /*bBasicCall=*/false);
1397 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1398 {
1399 "BookmarkNamePrefix": {
1400 "type": "string",
1401 "value": "ZOTERO_BREF_"
1402 },
1403 "Bookmark": {
1404 "type": "[]com.sun.star.beans.PropertyValue",
1405 "value": {
1406 "Bookmark": {
1407 "type": "string",
1408 "value": "ZOTERO_BREF_new"
1409 },
1410 "BookmarkText": {
1411 "type": "string",
1412 "value": "new result"
1413 }
1414 }
1415 }
1416 }
1417 )json");
1418 uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
1419 dispatchCommand(mxComponent, u".uno:UpdateBookmark"_ustr, aArgs);
1420
1421 // Then make sure that the only paragraph is updated correctly:
1422 SwCursor* pCursor = pWrtShell->GetCursor();
1423 OUString aActual = pCursor->GetPointNode().GetTextNode()->GetText();
1424 // Without the accompanying fix in place, this test would have failed with:
1425 // - Expected: Anew resultD
1426 // - Actual : ABCD
1427 // i.e. it was not possible to update just the bookmark under cursor.
1428 CPPUNIT_ASSERT_EQUAL(u"Anew resultD"_ustr, aActual);
1429 auto it = pDoc->getIDocumentMarkAccess()->findMark(SwMarkName(u"ZOTERO_BREF_new"_ustr));
1430 CPPUNIT_ASSERT(it != pDoc->getIDocumentMarkAccess()->getAllMarksEnd());
1431 }
1432
1433 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateRefmark)
1434 {
1435 // Given a document with a refmark:
1436 createSwDoc();
1437 uno::Sequence<css::beans::PropertyValue> aArgs = {
1438 comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
1439 comphelper::makePropertyValue(u"Name"_ustr,
1440 uno::Any(u"ZOTERO_ITEM CSL_CITATION {} old refmark"_ustr)),
1441 comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"old content"_ustr)),
1442 };
1443 dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
1444
1445 // When updating that refmark:
1446 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1447 {
1448 "TypeName": {
1449 "type": "string",
1450 "value": "SetRef"
1451 },
1452 "NamePrefix": {
1453 "type": "string",
1454 "value": "ZOTERO_ITEM CSL_CITATION"
1455 },
1456 "Field": {
1457 "type": "[]com.sun.star.beans.PropertyValue",
1458 "value": {
1459 "Name": {
1460 "type": "string",
1461 "value": "ZOTERO_ITEM CSL_CITATION {} new refmark"
1462 },
1463 "Content": {
1464 "type": "string",
1465 "value": "new content"
1466 }
1467 }
1468 }
1469 }
1470 )json");
1471 aArgs = comphelper::containerToSequence(aArgsVec);
1472 dispatchCommand(mxComponent, u".uno:UpdateField"_ustr, aArgs);
1473
1474 // Then make sure that the document text features the new content:
1475 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1476 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
1477 // Without the accompanying fix in place, this test would have failed with:
1478 // - Expected: new content
1479 // - Actual : old content
1480 // i.e. the content was not updated.
1481 CPPUNIT_ASSERT_EQUAL(u"new content"_ustr, pTextNode->GetText());
1482 }
1483
1484 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDeleteBookmarks)
1485 {
1486 // Given a document with 2 bookmarks, first covering "B" and second covering "D":
1487 createSwDoc();
1488 SwDoc* pDoc = getSwDoc();
1489 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1490 pWrtShell->Insert(u"ABCDE"_ustr);
1491 pWrtShell->SttEndDoc(/*bStt=*/true);
1492 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
1493 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
1494 pWrtShell->SetBookmark(vcl::KeyCode(), SwMarkName(u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr));
1495 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
1496 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
1497 pWrtShell->SetBookmark(vcl::KeyCode(), SwMarkName(u"other"_ustr));
1498
1499 // When deleting 1 matching bookmark:
1500 pWrtShell->SttEndDoc(/*bStt=*/true);
1501 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1502 {
1503 "BookmarkNamePrefix": {
1504 "type": "string",
1505 "value": "ZOTERO_BREF_"
1506 }
1507 }
1508 )json");
1509 uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
1510 dispatchCommand(mxComponent, u".uno:DeleteBookmarks"_ustr, aArgs);
1511
1512 // Then make sure that only the other bookmark is kept:
1513 auto it
1514 = pDoc->getIDocumentMarkAccess()->findMark(SwMarkName(u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr));
1515 // Without the accompanying fix in place, this test would have failed, the matching bookmark was
1516 // not removed.
1517 CPPUNIT_ASSERT(bool(it == pDoc->getIDocumentMarkAccess()->getAllMarksEnd()));
1518 it = pDoc->getIDocumentMarkAccess()->findMark(SwMarkName(u"other"_ustr));
1519 CPPUNIT_ASSERT(it != pDoc->getIDocumentMarkAccess()->getAllMarksEnd());
1520 }
1521
1522 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDeleteFields)
1523 {
1524 // Given a document with a refmark:
1525 createSwDoc();
1526 uno::Sequence<css::beans::PropertyValue> aArgs = {
1527 comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
1528 comphelper::makePropertyValue(u"Name"_ustr,
1529 uno::Any(u"ZOTERO_ITEM CSL_CITATION {} RNDpyJknp173F"_ustr)),
1530 comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"aaa<b>bbb</b>ccc"_ustr)),
1531 };
1532 dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
1533
1534 // When deleting the refmarks:
1535 std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
1536 {
1537 "TypeName": {
1538 "type": "string",
1539 "value": "SetRef"
1540 },
1541 "NamePrefix": {
1542 "type": "string",
1543 "value": "ZOTERO_ITEM CSL_CITATION"
1544 }
1545 }
1546 )json");
1547 aArgs = comphelper::containerToSequence(aArgsVec);
1548 dispatchCommand(mxComponent, u".uno:DeleteFields"_ustr, aArgs);
1549
1550 // Then make sure that no refmark is kept:
1551 SwDoc* pDoc = getSwDoc();
1552 // Without the accompanying fix in place, this test would have failed with:
1553 // - Expected: 0
1554 // - Actual : 1
1555 // i.e. the refmark was not deleted.
1556 CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(0), pDoc->GetRefMarks());
1557 }
1558
1559 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormFieldFootnote)
1560 {
1561 // Given an empty document:
1562 createSwDoc();
1563 SwDoc* pDoc = getSwDoc();
1564
1565 // When inserting an ODF_UNHANDLED fieldmark inside a footnote:
1566 uno::Sequence<css::beans::PropertyValue> aArgs = {
1567 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
1568 comphelper::makePropertyValue(u"FieldCommand"_ustr,
1569 uno::Any(u"ADDIN ZOTERO_BIBL foo bar"_ustr)),
1570 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result"_ustr)),
1571 comphelper::makePropertyValue(u"Wrapper"_ustr, uno::Any(u"Footnote"_ustr)),
1572 };
1573 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
1574
1575 // Then make sure that the footnote is created:
1576 SwFootnoteIdxs& rFootnotes = pDoc->GetFootnoteIdxs();
1577 // Without the accompanying fix in place, this test would have failed with:
1578 // - Expected: 1
1579 // - Actual : 0
1580 // i.e. no footnote was created.
1581 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rFootnotes.size());
1582 }
1583
1584 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormFieldEndnote)
1585 {
1586 // Given an empty document:
1587 createSwDoc();
1588 SwDoc* pDoc = getSwDoc();
1589
1590 // When inserting an ODF_UNHANDLED fieldmark inside an endnote:
1591 uno::Sequence<css::beans::PropertyValue> aArgs = {
1592 comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
1593 comphelper::makePropertyValue(u"FieldCommand"_ustr,
1594 uno::Any(u"ADDIN ZOTERO_BIBL foo bar"_ustr)),
1595 comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result"_ustr)),
1596 comphelper::makePropertyValue(u"Wrapper"_ustr, uno::Any(u"Endnote"_ustr)),
1597 };
1598 dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
1599
1600 // Then make sure that the endnote is created:
1601 SwFootnoteIdxs& rFootnotes = pDoc->GetFootnoteIdxs();
1602 // Without the accompanying fix in place, this test would have failed with:
1603 // - Expected: 1
1604 // - Actual : 0
1605 // i.e. no endnote was inserted.
1606 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rFootnotes.size());
1607 SwTextFootnote* pEndnote = rFootnotes[0];
1608 const SwFormatFootnote& rFormatEndnote = pEndnote->GetFootnote();
1609 CPPUNIT_ASSERT(rFormatEndnote.IsEndNote());
1610 // Also check that the endnote body contains the fieldmark:
1611 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1612 pWrtShell->SttEndDoc(/*bStt=*/true);
1613 pWrtShell->GotoFootnoteText();
1614 pWrtShell->EndOfSection(/*bSelect=*/true);
1615 SwCursor* pCursor = pWrtShell->GetCursor();
1616 OUString aActual = pCursor->GetText();
1617 static sal_Unicode const aForbidden[]
1618 = { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 };
1619 aActual = comphelper::string::removeAny(aActual, aForbidden);
1620 // Then this was empty: the fieldmark was inserted before the note anchor, not in the note body.
1621 CPPUNIT_ASSERT_EQUAL(u"result"_ustr, aActual);
1622 }
1623
1624 CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateSelectedField)
1625 {
1626 // Given an empty doc:
1627 createSwDoc();
1628 SwDoc* pDoc = getSwDoc();
1629 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
1630 SwCursorShell* pShell(pDoc->GetEditShell());
1631 CPPUNIT_ASSERT(pShell);
1632 SwPaM* pCursor = pShell->GetCursor();
1633
1634 // Insert a time field and select it:
1635 dispatchCommand(mxComponent, u".uno:InsertTimeFieldVar"_ustr, {});
1636
1637 pCursor->SetMark();
1638 pCursor->Move(fnMoveBackward);
1639
1640 OUString aTimeFieldBefore, aTimeFieldAfter;
1641 pWrtShell->GetSelectedText(aTimeFieldBefore);
1642
1643 // Wait for one second:
1644 osl::Thread::wait(std::chrono::seconds(1));
1645
1646 // Update the field at cursor:
1647 dispatchCommand(mxComponent, u".uno:UpdateSelectedField"_ustr, {});
1648 pWrtShell->GetSelectedText(aTimeFieldAfter);
1649
1650 // Check that the selected field has changed:
1651 CPPUNIT_ASSERT(aTimeFieldAfter != aTimeFieldBefore);
1652 }
1653
1654 CPPUNIT_PLUGIN_IMPLEMENT();
1655
1656 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */