1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <swmodeltestbase.hxx>
12 #include <comphelper/propertyvalue.hxx>
13 #include <comphelper/propertysequence.hxx>
14 #include <comphelper/sequence.hxx>
15 #include <sfx2/viewfrm.hxx>
16 #include <sfx2/bindings.hxx>
17 #include <editeng/fontitem.hxx>
23 #include <formatflysplit.hxx>
26 #include <formatcontentcontrol.hxx>
28 #include <textcontentcontrol.hxx>
29 #include <IDocumentStylePoolAccess.hxx>
30 #include <poolfmt.hxx>
34 /// Covers sw/source/uibase/shells/textsh1.cxx fixes.
35 class Test
: public SwModelTestBase
39 : SwModelTestBase("/sw/qa/uibase/shells/data/")
44 CPPUNIT_TEST_FIXTURE(Test
, testDeleteRichTextContentControl
)
46 // Given an empty document
48 SwDoc
* pDoc
= getSwDoc();
49 SwWrtShell
* pWrtShell
= pDoc
->GetDocShell()->GetWrtShell();
51 // When inserting a content control:
52 pWrtShell
->InsertContentControl(SwContentControlType::RICH_TEXT
);
54 // Then verify that the content control has been inserted
55 SwTextNode
* pTextNode
= pWrtShell
->GetCursor()->GetPointNode().GetTextNode();
56 CPPUNIT_ASSERT(pTextNode
->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL
));
58 // And verify that there is text in the document
59 size_t nTextNodeLength
= pTextNode
->GetText().getLength();
60 CPPUNIT_ASSERT(nTextNodeLength
> 0);
62 // And the content control manager has one content control
63 SwContentControlManager
& rManager
= pDoc
->GetContentControlManager();
64 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rManager
.GetCount());
66 // When deleting the content control
67 dispatchCommand(mxComponent
, ".uno:DeleteContentControl", {});
69 // Then verify that there are no content controls
70 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), rManager
.GetCount());
71 CPPUNIT_ASSERT(!pTextNode
->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL
));
72 CPPUNIT_ASSERT_EQUAL(nTextNodeLength
, static_cast<size_t>(pTextNode
->GetText().getLength()));
75 CPPUNIT_TEST_FIXTURE(Test
, testDeleteCheckboxContentControl
)
77 // Given an empty document
79 SwDoc
* pDoc
= getSwDoc();
80 SwWrtShell
* pWrtShell
= pDoc
->GetDocShell()->GetWrtShell();
81 // The default Liberation Serif doesn't have a checkmark glyph, avoid font fallback.
82 SwView
& rView
= pWrtShell
->GetView();
83 SfxItemSetFixed
<RES_CHRATR_BEGIN
, RES_CHRATR_END
> aSet(rView
.GetPool());
84 SvxFontItem
aFont(FAMILY_DONTKNOW
, u
"DejaVu Sans"_ustr
, OUString(), PITCH_DONTKNOW
,
85 RTL_TEXTENCODING_DONTKNOW
, RES_CHRATR_FONT
);
87 SwTextFormatColl
* pStyle
88 = pDoc
->getIDocumentStylePoolAccess().GetTextCollFromPool(RES_POOLCOLL_STANDARD
);
89 pStyle
->SetFormatAttr(aSet
);
91 // When inserting a content control:
92 pWrtShell
->InsertContentControl(SwContentControlType::CHECKBOX
);
94 // Then verify that a checkbox content control exists in the document
95 SwTextNode
* pTextNode
= pWrtShell
->GetCursor()->GetPointNode().GetTextNode();
96 SwTextAttr
* pAttr
= pTextNode
->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL
);
97 auto pTextContentControl
= static_txtattr_cast
<SwTextContentControl
*>(pAttr
);
98 auto& rFormatContentControl
99 = static_cast<SwFormatContentControl
&>(pTextContentControl
->GetAttr());
100 std::shared_ptr
<SwContentControl
> pContentControl
= rFormatContentControl
.GetContentControl();
101 CPPUNIT_ASSERT(pContentControl
->GetCheckbox());
103 // And the content control manager has one content control
104 SwContentControlManager
& rManager
= pDoc
->GetContentControlManager();
105 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rManager
.GetCount());
107 // When deleting the content control
108 dispatchCommand(mxComponent
, ".uno:DeleteContentControl", {});
110 // Then verify that there are no content controls
111 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), rManager
.GetCount());
112 CPPUNIT_ASSERT(!pTextNode
->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL
));
114 // And verify that text remains in the document
115 size_t nTextNodeLength
= pTextNode
->GetText().getLength();
116 CPPUNIT_ASSERT(nTextNodeLength
> 0);
119 CPPUNIT_TEST_FIXTURE(Test
, testDeleteDropdownContentControl
)
121 // Create a document with a dropdown content control
123 SwDoc
* pDoc
= getSwDoc();
124 SwWrtShell
* pWrtShell
= pDoc
->GetDocShell()->GetWrtShell();
125 pWrtShell
->InsertContentControl(SwContentControlType::DROP_DOWN_LIST
);
127 // Then verify that the content control manager has one content control
128 SwContentControlManager
& rManager
= pDoc
->GetContentControlManager();
129 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rManager
.GetCount());
131 // When deleting the content control
132 dispatchCommand(mxComponent
, ".uno:DeleteContentControl", {});
134 // Then verify that there are no content controls
135 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), rManager
.GetCount());
136 SwTextNode
* pTextNode
= pWrtShell
->GetCursor()->GetPointNode().GetTextNode();
137 CPPUNIT_ASSERT(!pTextNode
->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL
));
139 // And verify that text remains in the document
140 size_t nTextNodeLength
= pTextNode
->GetText().getLength();
141 CPPUNIT_ASSERT(nTextNodeLength
> 0);
144 CPPUNIT_TEST_FIXTURE(Test
, testDeleteDateContentControl
)
146 // Create a document with a date content control
148 SwDoc
* pDoc
= getSwDoc();
149 SwWrtShell
* pWrtShell
= pDoc
->GetDocShell()->GetWrtShell();
150 pWrtShell
->InsertContentControl(SwContentControlType::DATE
);
152 // Then verify that the content control manager has one content control
153 SwContentControlManager
& rManager
= pDoc
->GetContentControlManager();
154 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rManager
.GetCount());
156 // When deleting the content control
157 dispatchCommand(mxComponent
, ".uno:DeleteContentControl", {});
159 // Then verify that there are no content controls
160 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), rManager
.GetCount());
161 SwTextNode
* pTextNode
= pWrtShell
->GetCursor()->GetPointNode().GetTextNode();
162 CPPUNIT_ASSERT(!pTextNode
->GetTextAttrForCharAt(0, RES_TXTATR_CONTENTCONTROL
));
164 // And verify that text remains in the document
165 size_t nTextNodeLength
= pTextNode
->GetText().getLength();
166 CPPUNIT_ASSERT(nTextNodeLength
> 0);
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */