Debian Codegraph @

sw/qa/uibase/wrtsh/wrtsh.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 <wrtsh.hxx>
11
12 #include <optional>
13
14 #include <com/sun/star/text/XTextContent.hpp>
15 #include <com/sun/star/text/XTextRange.hpp>
16 #include <com/sun/star/text/XTextDocument.hpp>
17
18 #include <rtl/ustring.hxx>
19 #include <sal/types.h>
20 #include <comphelper/propertyvalue.hxx>
21 #include <editeng/fontitem.hxx>
22 #include <editeng/lrspitem.hxx>
23
24 #include <swmodeltestbase.hxx>
25 #include <doc.hxx>
26 #include <docsh.hxx>
27 #include <formatlinebreak.hxx>
28 #include <ndtxt.hxx>
29 #include <textcontentcontrol.hxx>
30 #include <fmtanchr.hxx>
31 #include <view.hxx>
32 #include <itabenum.hxx>
33 #include <frmmgr.hxx>
34 #include <formatflysplit.hxx>
35 #include <frmatr.hxx>
36
37 #include <svx/fontworkbar.hxx>
38 #include <svx/svdpage.hxx>
39 #include <svx/svdobj.hxx>
40 #include <svx/svdview.hxx>
41 #include <drawdoc.hxx>
42 #include <IDocumentDrawModelAccess.hxx>
43 #include <swdtflvr.hxx>
44
45 namespace
46 {
47 /// Covers sw/source/uibase/wrtsh/ fixes.
48 class Test : public SwModelTestBase
49 {
50 public:
51 Test()
52 : SwModelTestBase(u"/sw/qa/uibase/wrtsh/data/"_ustr)
53 {
54 }
55 };
56
57 CPPUNIT_TEST_FIXTURE(Test, testInsertLineBreak)
58 {
59 // Given an empty document:
60 createSwDoc();
61
62 // When inserting a clearing break:
63 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
64 std::optional<SwLineBreakClear> oClear = SwLineBreakClear::ALL;
65 pWrtShell->InsertLineBreak(oClear);
66
67 // Then make sure it's not just a plain linebreak:
68 uno::Reference<css::text::XTextRange> xTextPortion = getRun(getParagraph(1), 1);
69 auto aPortionType = getProperty<OUString>(xTextPortion, u"TextPortionType"_ustr);
70 // Without the accompanying fix in place, this test would have failed with:
71 // - Expected: LineBreak
72 // - Actual : Text
73 // i.e. the line break lost its "clear" property.
74 CPPUNIT_ASSERT_EQUAL(u"LineBreak"_ustr, aPortionType);
75 auto xLineBreak
76 = getProperty<uno::Reference<text::XTextContent>>(xTextPortion, u"LineBreak"_ustr);
77 auto eClear = getProperty<sal_Int16>(xLineBreak, u"Clear"_ustr);
78 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int16>(SwLineBreakClear::ALL), eClear);
79 }
80
81 CPPUNIT_TEST_FIXTURE(Test, testGotoContentControl)
82 {
83 // Given a document with a content control:
84 createSwDoc();
85 SwDoc* pDoc = getSwDoc();
86 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
87 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
88 uno::Reference<text::XText> xText = xTextDocument->getText();
89 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
90 xText->insertString(xCursor, u"test"_ustr, /*bAbsorb=*/false);
91 xCursor->gotoStart(/*bExpand=*/false);
92 xCursor->gotoEnd(/*bExpand=*/true);
93 uno::Reference<text::XTextContent> xContentControl(
94 xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
95 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
96 xContentControlProps->setPropertyValue(u"ShowingPlaceHolder"_ustr, uno::Any(true));
97 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
98
99 // When going to that content control in placeholder mode:
100 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
101 SwNodeOffset nIndex = pWrtShell->GetCursor()->GetPointNode().GetIndex();
102 SwTextNode* pTextNode = pDoc->GetNodes()[nIndex]->GetTextNode();
103 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
104 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
105 auto& rFormatContentControl
106 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
107 pWrtShell->GotoContentControl(rFormatContentControl);
108
109 // Then make sure that the content control is selected (without the dummy character):
110 // Without the accompanying fix in place, this test would have failed, the user had to manually
111 // select the placeholder text.
112 sal_Int32 nStart = pWrtShell->GetCursor()->Start()->GetContentIndex();
113 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nStart);
114 sal_Int32 nEnd = pWrtShell->GetCursor()->End()->GetContentIndex();
115 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(5), nEnd);
116 }
117
118 CPPUNIT_TEST_FIXTURE(Test, testTickCheckboxContentControl)
119 {
120 // Given a document with a checkbox (checked) content control:
121 createSwDoc();
122 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
123
124 // The default Liberation Serif doesn't have a checkmark glyph, avoid font fallback.
125 SwView& rView = pWrtShell->GetView();
126 SfxItemSet aSet(
127 SfxItemSet::makeFixedSfxItemSet<RES_CHRATR_BEGIN, RES_CHRATR_END>(rView.GetPool()));
128 SvxFontItem aFont(FAMILY_DONTKNOW, u"DejaVu Sans"_ustr, OUString(), PITCH_DONTKNOW,
129 RTL_TEXTENCODING_DONTKNOW, RES_CHRATR_FONT);
130 aSet.Put(aFont);
131 pWrtShell->SetAttrSet(aSet);
132
133 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
134 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
135 uno::Reference<text::XText> xText = xTextDocument->getText();
136 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
137 xText->insertString(xCursor, u"☒"_ustr, /*bAbsorb=*/false);
138 xCursor->gotoStart(/*bExpand=*/false);
139 xCursor->gotoEnd(/*bExpand=*/true);
140 uno::Reference<text::XTextContent> xContentControl(
141 xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
142 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
143 xContentControlProps->setPropertyValue(u"Checkbox"_ustr, uno::Any(true));
144 xContentControlProps->setPropertyValue(u"Checked"_ustr, uno::Any(true));
145 xContentControlProps->setPropertyValue(u"CheckedState"_ustr, uno::Any(u"☒"_ustr));
146 xContentControlProps->setPropertyValue(u"UncheckedState"_ustr, uno::Any(u"☐"_ustr));
147 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
148
149 // When clicking on that content control:
150 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
151 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
152 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
153 auto& rFormatContentControl
154 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
155 pWrtShell->GotoContentControl(rFormatContentControl);
156
157 // Then make sure that the checkbox is no longer checked:
158 // Without the accompanying fix in place, this test would have failed:
159 // - Expected: ☐
160 // - Actual : ☒
161 // i.e. the text node's text was "Ballot Box with X", not just "Ballot Box".
162 CPPUNIT_ASSERT_EQUAL(u"☐"_ustr, pTextNode->GetExpandText(pWrtShell->GetLayout()));
163 }
164
165 CPPUNIT_TEST_FIXTURE(Test, testInsertContentControl)
166 {
167 // Given an empty document:
168 createSwDoc();
169
170 // When inserting a content control:
171 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
172 pWrtShell->InsertContentControl(SwContentControlType::RICH_TEXT);
173
174 // Then make sure that the matching text attribute is added to the document model:
175 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
176 // Without the accompanying fix in place, this test would have failed, nothing happened on
177 // InsertContentControl().
178 CPPUNIT_ASSERT(pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL));
179 }
180
181 CPPUNIT_TEST_FIXTURE(Test, testInsertCheckboxContentControl)
182 {
183 // Given an empty document:
184 createSwDoc();
185
186 // When inserting a content control:
187 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
188
189 // The default Liberation Serif doesn't have a checkmark glyph, avoid font fallback.
190 SwView& rView = pWrtShell->GetView();
191 SfxItemSet aSet(
192 SfxItemSet::makeFixedSfxItemSet<RES_CHRATR_BEGIN, RES_CHRATR_END>(rView.GetPool()));
193 SvxFontItem aFont(FAMILY_DONTKNOW, u"DejaVu Sans"_ustr, OUString(), PITCH_DONTKNOW,
194 RTL_TEXTENCODING_DONTKNOW, RES_CHRATR_FONT);
195 aSet.Put(aFont);
196 pWrtShell->SetAttrSet(aSet);
197
198 pWrtShell->InsertContentControl(SwContentControlType::CHECKBOX);
199
200 // Then make sure that the matching text attribute is added to the document model:
201 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
202 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
203 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
204 auto& rFormatContentControl
205 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
206 std::shared_ptr<SwContentControl> pContentControl = rFormatContentControl.GetContentControl();
207 // Without the accompanying fix in place, this test would have failed, the inserted content
208 // control wasn't a checkbox one.
209 CPPUNIT_ASSERT(pContentControl->GetCheckbox());
210 }
211
212 CPPUNIT_TEST_FIXTURE(Test, testSelectDropdownContentControl)
213 {
214 // Given a document with a dropdown content control:
215 createSwDoc();
216 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
217 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
218 uno::Reference<text::XText> xText = xTextDocument->getText();
219 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
220 xText->insertString(xCursor, u"choose an item"_ustr, /*bAbsorb=*/false);
221 xCursor->gotoStart(/*bExpand=*/false);
222 xCursor->gotoEnd(/*bExpand=*/true);
223 uno::Reference<text::XTextContent> xContentControl(
224 xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
225 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
226 {
227 uno::Sequence<beans::PropertyValues> aListItems = {
228 {
229 comphelper::makePropertyValue(u"DisplayText"_ustr, uno::Any(u"red"_ustr)),
230 comphelper::makePropertyValue(u"Value"_ustr, uno::Any(u"R"_ustr)),
231 },
232 {
233 comphelper::makePropertyValue(u"DisplayText"_ustr, uno::Any(u"green"_ustr)),
234 comphelper::makePropertyValue(u"Value"_ustr, uno::Any(u"G"_ustr)),
235 },
236 {
237 comphelper::makePropertyValue(u"DisplayText"_ustr, uno::Any(u"blue"_ustr)),
238 comphelper::makePropertyValue(u"Value"_ustr, uno::Any(u"B"_ustr)),
239 },
240 };
241 xContentControlProps->setPropertyValue(u"ListItems"_ustr, uno::Any(aListItems));
242 }
243 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
244
245 // When clicking on that content control:
246 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
247 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
248 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
249 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
250 auto& rFormatContentControl
251 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
252 rFormatContentControl.GetContentControl()->SetSelectedListItem(0);
253 pWrtShell->GotoContentControl(rFormatContentControl);
254
255 // Then make sure that the document text is updated:
256 // Without the accompanying fix in place, this test would have failed:
257 // - Expected: red
258 // - Actual : choose an item
259 // i.e. the document text was unchanged instead of display text of the first list item.
260 CPPUNIT_ASSERT_EQUAL(u"red"_ustr, pTextNode->GetExpandText(pWrtShell->GetLayout()));
261 }
262
263 CPPUNIT_TEST_FIXTURE(Test, testInsertDropdownContentControl)
264 {
265 // Given an empty document:
266 createSwDoc();
267
268 // When inserting a content control:
269 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
270 pWrtShell->InsertContentControl(SwContentControlType::DROP_DOWN_LIST);
271
272 // Then make sure that the matching text attribute is added to the document model:
273 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
274 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
275 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
276 auto& rFormatContentControl
277 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
278 std::shared_ptr<SwContentControl> pContentControl = rFormatContentControl.GetContentControl();
279 // Without the accompanying fix in place, this test would have failed:
280 // - Expected: 1
281 // - Actual : 0
282 // i.e. the inserted content control was a default (rich text) one, not a dropdown.
283 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pContentControl->GetListItems().size());
284 }
285
286 CPPUNIT_TEST_FIXTURE(Test, testReplacePictureContentControl)
287 {
288 // Given a document with a picture content control:
289 createSwDoc();
290 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
291 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
292 uno::Reference<text::XText> xText = xTextDocument->getText();
293 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
294 uno::Reference<beans::XPropertySet> xTextGraphic(
295 xMSF->createInstance(u"com.sun.star.text.TextGraphicObject"_ustr), uno::UNO_QUERY);
296 xTextGraphic->setPropertyValue(u"AnchorType"_ustr,
297 uno::Any(text::TextContentAnchorType_AS_CHARACTER));
298 uno::Reference<text::XTextContent> xTextContent(xTextGraphic, uno::UNO_QUERY);
299 xText->insertTextContent(xCursor, xTextContent, false);
300 xCursor->gotoStart(/*bExpand=*/false);
301 xCursor->gotoEnd(/*bExpand=*/true);
302 uno::Reference<text::XTextContent> xContentControl(
303 xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
304 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
305 xContentControlProps->setPropertyValue(u"ShowingPlaceHolder"_ustr, uno::Any(true));
306 xContentControlProps->setPropertyValue(u"Picture"_ustr, uno::Any(true));
307 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
308
309 // When clicking on that content control:
310 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
311 pWrtShell->GotoObj(/*bNext=*/true, GotoObjFlags::Any);
312 pWrtShell->EnterSelFrameMode();
313 const SwFrameFormat* pFlyFormat = pWrtShell->GetFlyFrameFormat();
314 const SwFormatAnchor& rFormatAnchor = pFlyFormat->GetAnchor();
315 SwNode* pAnchorNode = rFormatAnchor.GetAnchorNode();
316 SwTextNode* pTextNode = pAnchorNode->GetTextNode();
317 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
318 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
319 auto& rFormatContentControl
320 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
321 pWrtShell->GotoContentControl(rFormatContentControl);
322
323 // Then make sure that the picture is replaced:
324 CPPUNIT_ASSERT(!rFormatContentControl.GetContentControl()->GetShowingPlaceHolder());
325 // Without the accompanying fix in place, this test would have failed, there was no special
326 // handling for picture content control (how to interact with them), and the default handler
327 // killed the image selection.
328 CPPUNIT_ASSERT(pWrtShell->IsFrameSelected());
329 }
330
331 CPPUNIT_TEST_FIXTURE(Test, testInsertPictureContentControl)
332 {
333 // Given an empty document:
334 createSwDoc();
335
336 // When inserting a content control:
337 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
338 pWrtShell->InsertContentControl(SwContentControlType::PICTURE);
339
340 // Then make sure that the matching text attribute is added to the document model:
341 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
342 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
343 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
344 auto& rFormatContentControl
345 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
346 std::shared_ptr<SwContentControl> pContentControl = rFormatContentControl.GetContentControl();
347 // Without the accompanying fix in place, this test would have failed, there was no special
348 // handling for picture content control, no placeholder fly content was inserted.
349 CPPUNIT_ASSERT(pContentControl->GetPicture());
350 CPPUNIT_ASSERT(pTextNode->GetTextAttrForCharAt(1, RES_TXTATR_FLYCNT));
351 }
352
353 CPPUNIT_TEST_FIXTURE(Test, testSelectDateContentControl)
354 {
355 // Given a document with a date content control:
356 createSwDoc();
357 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
358 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
359 uno::Reference<text::XText> xText = xTextDocument->getText();
360 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
361 xText->insertString(xCursor, u"test"_ustr, /*bAbsorb=*/false);
362 xCursor->gotoStart(/*bExpand=*/false);
363 xCursor->gotoEnd(/*bExpand=*/true);
364 uno::Reference<text::XTextContent> xContentControl(
365 xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
366 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
367 xContentControlProps->setPropertyValue(u"Date"_ustr, uno::Any(true));
368 xContentControlProps->setPropertyValue(u"DateFormat"_ustr, uno::Any(u"YYYY-MM-DD"_ustr));
369 xContentControlProps->setPropertyValue(u"DateLanguage"_ustr, uno::Any(u"en-US"_ustr));
370 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
371
372 // When clicking on that content control:
373 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
374 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
375 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
376 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
377 auto& rFormatContentControl
378 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
379 rFormatContentControl.GetContentControl()->SetSelectedDate(44705);
380 pWrtShell->GotoContentControl(rFormatContentControl);
381
382 // Then make sure that the document text is updated:
383 // Without the accompanying fix in place, this test would have failed with:
384 // - Expected: 2022-05-24
385 // - Actual : test
386 // i.e. the content control was not updated.
387 CPPUNIT_ASSERT_EQUAL(u"2022-05-24"_ustr, pTextNode->GetExpandText(pWrtShell->GetLayout()));
388 CPPUNIT_ASSERT_EQUAL(u"2022-05-24T00:00:00Z"_ustr,
389 rFormatContentControl.GetContentControl()->GetCurrentDate());
390 }
391
392 CPPUNIT_TEST_FIXTURE(Test, testInsertDateContentControl)
393 {
394 // Given an empty document:
395 createSwDoc();
396
397 // When inserting a date content control:
398 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
399 pWrtShell->InsertContentControl(SwContentControlType::DATE);
400
401 // Then make sure that the matching text attribute is added to the document model:
402 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
403 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
404 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
405 auto& rFormatContentControl
406 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
407 std::shared_ptr<SwContentControl> pContentControl = rFormatContentControl.GetContentControl();
408 // Without the accompanying fix in place, this test would have failed, there was no special
409 // handling for date content control.
410 CPPUNIT_ASSERT(pContentControl->GetDate());
411 }
412
413 CPPUNIT_TEST_FIXTURE(Test, testInsertPlainTextContentControl)
414 {
415 // Given an empty document:
416 createSwDoc();
417
418 // When inserting a plain text content control:
419 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
420 pWrtShell->InsertContentControl(SwContentControlType::PLAIN_TEXT);
421
422 // Then make sure that the matching text attribute is added to the document model:
423 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
424 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
425 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
426 auto& rFormatContentControl
427 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
428 std::shared_ptr<SwContentControl> pContentControl = rFormatContentControl.GetContentControl();
429 // Without the accompanying fix in place, this test would have failed, there was no special
430 // handling for plain text content controls.
431 CPPUNIT_ASSERT(pContentControl->GetPlainText());
432
433 CPPUNIT_ASSERT(pContentControl->GetShowingPlaceHolder());
434 pWrtShell->GotoContentControl(rFormatContentControl);
435 CPPUNIT_ASSERT(pContentControl->GetShowingPlaceHolder());
436 pWrtShell->Insert(u"Foo"_ustr);
437 // No longer showing placeholder text, as it has been changed
438 CPPUNIT_ASSERT(!pContentControl->GetShowingPlaceHolder());
439 }
440
441 CPPUNIT_TEST_FIXTURE(Test, testInsertComboBoxContentControl)
442 {
443 // Given an empty document:
444 createSwDoc();
445
446 // When inserting a combo box content control:
447 dispatchCommand(mxComponent, u".uno:InsertComboBoxContentControl"_ustr, {});
448
449 // Then make sure that the matching text attribute is added to the document model:
450 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
451 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
452 // Without the accompanying fix in place, this test would have failed, no content control was
453 // inserted.
454 SwTextAttr* pAttr = pTextNode->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL);
455 CPPUNIT_ASSERT(pAttr);
456 auto pTextContentControl = static_txtattr_cast<SwTextContentControl*>(pAttr);
457 auto& rFormatContentControl
458 = static_cast<SwFormatContentControl&>(pTextContentControl->GetAttr());
459 std::shared_ptr<SwContentControl> pContentControl = rFormatContentControl.GetContentControl();
460 CPPUNIT_ASSERT(pContentControl->GetComboBox());
461 }
462
463 void InsertSplitFly(SwWrtShell* pWrtShell)
464 {
465 SwPosition aInsertPos = *pWrtShell->GetCursor()->GetPoint();
466 // Insert a table:
467 SwInsertTableOptions aTableOptions(SwInsertTableFlags::DefaultBorder, 0);
468 pWrtShell->InsertTable(aTableOptions, /*nRows=*/2, /*nCols=*/1);
469 pWrtShell->MoveTable(GotoPrevTable, fnTableStart);
470 pWrtShell->GoPrevCell();
471 pWrtShell->Insert(u"A1"_ustr);
472 // Select cell:
473 pWrtShell->SelAll();
474 // Select table:
475 pWrtShell->SelAll();
476 // Wrap the table in a text frame:
477 SwFlyFrameAttrMgr aMgr(true, pWrtShell, Frmmgr_Type::TEXT, nullptr);
478 pWrtShell->StartAllAction();
479 aMgr.InsertFlyFrame(RndStdIds::FLY_AT_PARA, aMgr.GetPos(), aMgr.GetSize());
480 pWrtShell->EndAllAction();
481 // Set fly properties:
482 pWrtShell->StartAllAction();
483 SwFrameFormat* pFly = pWrtShell->GetFlyFrameFormat();
484 SwAttrSet aSet(pFly->GetAttrSet());
485 aSet.Put(SwFormatFlySplit(true));
486 SwFormatAnchor aAnchor(RndStdIds::FLY_AT_PARA);
487 aAnchor.SetAnchor(&aInsertPos);
488 aSet.Put(aAnchor);
489 SwDoc* pDoc = pWrtShell->GetDoc();
490 pDoc->SetAttr(aSet, *pFly);
491 pWrtShell->EndAllAction();
492 pWrtShell->EnterStdMode();
493 }
494
495 CPPUNIT_TEST_FIXTURE(Test, testSplitFlysAnchorJoin)
496 {
497 // Given a document with two paragraphs, each serving as an anchor of a split fly:
498 createSwDoc();
499 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
500 pWrtShell->Insert(u"first para"_ustr);
501 pWrtShell->SplitNode();
502 pWrtShell->Insert(u"second para"_ustr);
503 pWrtShell->SttEndDoc(/*bStt=*/true);
504 InsertSplitFly(pWrtShell);
505 pWrtShell->SttEndDoc(/*bStt=*/false);
506 pWrtShell->SttPara();
507 InsertSplitFly(pWrtShell);
508
509 // When trying to delete at the end of the first para:
510 pWrtShell->SttEndDoc(/*bStt=*/true);
511 pWrtShell->EndPara();
512 pWrtShell->DelRight();
513
514 // Then make sure the join doesn't happen till a text node can only be an anchor for one split
515 // fly:
516 pWrtShell->SttEndDoc(/*bStt=*/true);
517 SwCursor* pCursor = pWrtShell->GetCursor();
518 // Without the accompanying fix in place, this test would have failed with:
519 // - Expected: first para
520 // - Actual : first parasecond para
521 // i.e. we did join the 2 anchors and for complex enough documents the layout never finished.
522 CPPUNIT_ASSERT_EQUAL(u"first para"_ustr, pCursor->GetPointNode().GetTextNode()->GetText());
523 pWrtShell->SttEndDoc(/*bStt=*/false);
524 CPPUNIT_ASSERT_EQUAL(u"second para"_ustr, pCursor->GetPointNode().GetTextNode()->GetText());
525 }
526
527 CPPUNIT_TEST_FIXTURE(Test, testBulletCharChangeOnIndent)
528 {
529 // Given an empty document:
530 createSwDoc();
531
532 // When adding 2 bullets, A is level 1, B is level 2:
533 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
534 pWrtShell->BulletOn();
535 pWrtShell->Insert(u"A"_ustr);
536 pWrtShell->SplitNode();
537 // Increase indent: downgrade to level 2.
538 pWrtShell->NumUpDown(/*bDown=*/true);
539 pWrtShell->Insert(u"B"_ustr);
540
541 // Then make sure the bullet characters are different:
542 pWrtShell->Up(/*bSelect=*/false);
543 SwCursor* pCursor = pWrtShell->GetCursor();
544 sal_UCS4 nBullet1 = 0;
545 {
546 SwTextNode* pTextNode = pCursor->GetPointNode().GetTextNode();
547 SwNumRule* pNumRule = pTextNode->GetNumRule();
548 const SwNumFormat& rNumFormat = pNumRule->Get(pTextNode->GetActualListLevel());
549 nBullet1 = rNumFormat.GetBulletChar();
550 }
551 pWrtShell->Down(/*bSelect=*/false);
552 sal_UCS4 nBullet2 = 0;
553 {
554 SwTextNode* pTextNode = pCursor->GetPointNode().GetTextNode();
555 SwNumRule* pNumRule = pTextNode->GetNumRule();
556 const SwNumFormat& rNumFormat = pNumRule->Get(pTextNode->GetActualListLevel());
557 nBullet2 = rNumFormat.GetBulletChar();
558 }
559 // Without the accompanying fix in place, this test would have failed, while nBullet1 should be
560 // • and nBullet2 should be ◦.
561 CPPUNIT_ASSERT(nBullet1 != nBullet2);
562 }
563
564 CPPUNIT_TEST_FIXTURE(Test, testRemoveIndent)
565 {
566 // Given a document with an empty, bulleted paragraph at the document end:
567 createSwDoc("remove-indent.docx");
568 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
569 pWrtShell->SttEndDoc(/*bStt=*/false);
570 // Press backspace once to make it not numbered:
571 bool bOnlyBackspaceKey = true;
572 pWrtShell->NumOrNoNum(!bOnlyBackspaceKey);
573
574 // When pressing backspace again to try to decrease its indent to change from left margin to
575 // first line margin:
576 pWrtShell->TryRemoveIndent();
577
578 // Then make sure we actually decrease the indent:
579 SwPaM* pCursor = pWrtShell->GetCursor();
580 SwTextNode* pTextNode = pCursor->GetPointNode().GetTextNode();
581 SwTwips nLeftMargin = pTextNode->GetSwAttrSet().GetTextLeftMargin().GetTextLeft().m_dValue;
582 // Without the accompanying fix in place, this test would have failed with:
583 // - Expected: 1135
584 // - Actual : 1418
585 // i.e. there was no decrease of the left text margin on pressing backspace.
586 CPPUNIT_ASSERT_EQUAL(static_cast<SwTwips>(1135), nLeftMargin);
587 }
588
589 CPPUNIT_TEST_FIXTURE(Test, testCutFontworkObject)
590 {
591 // Given a document with a fontwork object named "fontwork1":
592 createSwDoc("tdf58511.odt");
593 SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
594 SwDoc* pDoc = getSwDoc();
595
596 // First, verify that the fontwork object exists
597 SwDrawModel* pDrawModel = pDoc->getIDocumentDrawModelAccess().GetDrawModel();
598 SdrPage* pPage = pDrawModel->GetPage(0);
599 SdrObject* pFontworkObj = nullptr;
600
601 // Search for the fontwork object by name
602 for (size_t i = 0; i < pPage->GetObjCount(); ++i)
603 {
604 SdrObject* pObj = pPage->GetObj(i);
605 if (pObj && pObj->GetName() == u"fontwork1")
606 {
607 // Check if it's actually a fontwork object using the helper function
608 if (svx::checkForFontWork(pObj))
609 {
610 pFontworkObj = pObj;
611 break;
612 }
613 }
614 }
615
616 // Make sure the fontwork object was found initially
617 CPPUNIT_ASSERT(pFontworkObj != nullptr);
618
619 // Select the fontwork object
620 SdrView* pSdrView = pWrtShell->GetDrawView();
621 CPPUNIT_ASSERT(pSdrView != nullptr);
622
623 // Clear any existing selection
624 pSdrView->UnmarkAll();
625
626 // Mark the fontwork object
627 pSdrView->MarkObj(pFontworkObj, pSdrView->GetSdrPageView());
628
629 // Verify the object is selected
630 const SdrMarkList& rMarkList = pSdrView->GetMarkedObjectList();
631 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rMarkList.GetMarkCount());
632 CPPUNIT_ASSERT_EQUAL(pFontworkObj, rMarkList.GetMark(0)->GetMarkedSdrObj());
633
634 // When cutting the fontwork object - use SwTransferable::Copy with bIsCut = true:
635 rtl::Reference<SwTransferable> xTransfer(new SwTransferable(*pWrtShell));
636 xTransfer->Cut();
637
638 // Then make sure the fontwork object is deleted from the document:
639 pFontworkObj = nullptr;
640 for (size_t i = 0; i < pPage->GetObjCount(); ++i)
641 {
642 SdrObject* pObj = pPage->GetObj(i);
643 if (pObj && pObj->GetName() == u"fontwork1")
644 {
645 if (svx::checkForFontWork(pObj))
646 {
647 pFontworkObj = pObj;
648 break;
649 }
650 }
651 }
652
653 // Without the accompanying fix in place, this test would have failed because
654 // the fontwork object was not properly deleted when cut.
655 CPPUNIT_ASSERT(pFontworkObj == nullptr);
656 }
657 }
658
659 CPPUNIT_PLUGIN_IMPLEMENT();
660
661 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */