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 <com/sun/star/awt/FontWeight.hpp>
13 #include <com/sun/star/text/ControlCharacter.hpp>
14 #include <com/sun/star/view/XLineCursor.hpp>
15 #include <com/sun/star/text/XTextDocument.hpp>
16 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
17 #include <com/sun/star/text/XTextViewCursor.hpp>
19 #include <comphelper/propertysequence.hxx>
20 #include <svl/srchitem.hxx>
21 #include <vcl/scheduler.hxx>
24 #include <unotxdoc.hxx>
27 #include <formatcontentcontrol.hxx>
29 /// Covers sw/source/core/crsr/ fixes.
30 class SwCoreCrsrTest
: public SwModelTestBase
34 : SwModelTestBase(u
"/sw/qa/core/crsr/data/"_ustr
)
39 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testFindReplace
)
43 // Given: a document with two "foo" in it, the second followed by a formatted soft hyphen.
44 uno::Reference
<text::XTextDocument
> xTextDocument(mxComponent
, uno::UNO_QUERY
);
45 uno::Reference
<text::XText
> xText
= xTextDocument
->getText();
46 // Create a document which has 2 lines: first line has foo in it, second line has the same, but
47 // followed by a formatted soft hyphen.
48 xText
->insertString(xText
->getEnd(), u
"foo xxx"_ustr
, /*bAbsorb=*/false);
49 xText
->insertControlCharacter(xText
->getEnd(), text::ControlCharacter::PARAGRAPH_BREAK
,
51 xText
->insertString(xText
->getEnd(), u
"foo xxx \xad after"_ustr
, /*bAbsorb=*/false);
52 uno::Reference
<frame::XModel
> xModel(mxComponent
, uno::UNO_QUERY
);
53 uno::Reference
<text::XTextViewCursorSupplier
> xTextViewCursorSupplier(
54 xModel
->getCurrentController(), uno::UNO_QUERY
);
55 uno::Reference
<text::XTextViewCursor
> xViewCursor
= xTextViewCursorSupplier
->getViewCursor();
56 xViewCursor
->gotoEnd(/*bExpand=*/false);
57 xViewCursor
->goLeft(/*nCount=*/6, /*bExpand=*/false);
58 xViewCursor
->goLeft(/*nCount=*/1, /*bExpand=*/true);
59 uno::Reference
<beans::XPropertySet
> xViewCursorProps(xViewCursor
, uno::UNO_QUERY
);
60 xViewCursorProps
->setPropertyValue(u
"CharWeight"_ustr
, uno::Any(awt::FontWeight::BOLD
));
61 xViewCursor
->gotoStart(/*bExpand=*/false);
63 // When: doing search & replace 3 times.
64 uno::Sequence
<beans::PropertyValue
> aArgs(comphelper::InitPropertySequence({
65 { "SearchItem.SearchString", uno::Any(u
"foo"_ustr
) },
66 { "SearchItem.ReplaceString", uno::Any(u
"bar"_ustr
) },
67 { "SearchItem.Command", uno::Any(static_cast<sal_Int16
>(SvxSearchCmd::REPLACE
)) },
69 // Find the first foo.
70 dispatchCommand(mxComponent
, u
".uno:ExecuteSearch"_ustr
, aArgs
);
71 // Replace the first foo.
72 dispatchCommand(mxComponent
, u
".uno:ExecuteSearch"_ustr
, aArgs
);
73 // Replace the second foo.
74 dispatchCommand(mxComponent
, u
".uno:ExecuteSearch"_ustr
, aArgs
);
76 // Then: the second "foo" should be replaced as well.
77 xViewCursor
->gotoEnd(/*bExpand=*/false);
78 uno::Reference
<view::XLineCursor
> xLineCursor(xViewCursor
, uno::UNO_QUERY
);
79 xLineCursor
->gotoStartOfLine(/*bExpand=*/true);
80 OUString aActual
= xViewCursor
->getString();
81 CPPUNIT_ASSERT_GREATEREQUAL(static_cast<sal_Int32
>(3), aActual
.getLength());
82 OUString aActualStart
= aActual
.copy(0, 3);
83 // Without the accompanying fix in place, this test would have failed with:
86 // i.e. the foo on the second line was not replaced.
87 CPPUNIT_ASSERT_EQUAL(u
"bar"_ustr
, aActualStart
);
90 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testSelAllStartsWithTable
)
92 createSwDoc("sel-all-starts-with-table.odt");
93 SwDocShell
* pDocShell
= getSwDocShell();
94 SwDoc
* pDoc
= pDocShell
->GetDoc();
95 SwWrtShell
* pWrtShell
= pDocShell
->GetWrtShell();
97 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc
->GetTableFrameFormatCount(/*bUsed=*/true));
101 Scheduler::ProcessEventsToIdle();
102 pWrtShell
->DelLeft();
104 // Without the accompanying fix in place, this test would have failed with:
107 // i.e. the table selection was lost and the table was not deleted.
108 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pDoc
->GetTableFrameFormatCount(/*bUsed=*/true));
111 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testContentControlLineBreak
)
113 // Given a document with a (rich text) content control:
115 uno::Reference
<lang::XMultiServiceFactory
> xMSF(mxComponent
, uno::UNO_QUERY
);
116 uno::Reference
<text::XTextDocument
> xTextDocument(mxComponent
, uno::UNO_QUERY
);
117 uno::Reference
<text::XText
> xText
= xTextDocument
->getText();
118 uno::Reference
<text::XTextCursor
> xCursor
= xText
->createTextCursor();
119 xText
->insertString(xCursor
, u
"test"_ustr
, /*bAbsorb=*/false);
120 xCursor
->gotoStart(/*bExpand=*/false);
121 xCursor
->gotoEnd(/*bExpand=*/true);
122 uno::Reference
<text::XTextContent
> xContentControl(
123 xMSF
->createInstance(u
"com.sun.star.text.ContentControl"_ustr
), uno::UNO_QUERY
);
124 xText
->insertTextContent(xCursor
, xContentControl
, /*bAbsorb=*/true);
126 // When pressing "enter" in the middle of that content control:
127 SwWrtShell
* pWrtShell
= getSwDocShell()->GetWrtShell();
128 pWrtShell
->SttEndDoc(/*bStt=*/true);
130 pWrtShell
->Right(SwCursorSkipMode::Chars
, /*bSelect=*/false, 2, /*bBasicCall=*/false);
131 dispatchCommand(mxComponent
, u
".uno:InsertPara"_ustr
, {});
133 // Then make sure that we only insert a line break, not a new paragraph:
134 SwTextNode
* pTextNode
= pWrtShell
->GetCursor()->GetMark()->GetNode().GetTextNode();
135 // Without the accompanying fix in place, this test would have failed with:
136 // - Expected: t\nest
138 // i.e. a new paragraph was inserted, which is not allowed for inline content controls.
139 CPPUNIT_ASSERT_EQUAL(u
"t\nest"_ustr
, pTextNode
->GetExpandText(pWrtShell
->GetLayout()));
142 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testContentControlReadOnly
)
144 // Given a document with a checkbox content control:
146 uno::Reference
<lang::XMultiServiceFactory
> xMSF(mxComponent
, uno::UNO_QUERY
);
147 uno::Reference
<text::XTextDocument
> xTextDocument(mxComponent
, uno::UNO_QUERY
);
148 uno::Reference
<text::XText
> xText
= xTextDocument
->getText();
149 uno::Reference
<text::XTextCursor
> xCursor
= xText
->createTextCursor();
150 xText
->insertString(xCursor
, u
"☐"_ustr
, /*bAbsorb=*/false);
151 xCursor
->gotoStart(/*bExpand=*/false);
152 xCursor
->gotoEnd(/*bExpand=*/true);
153 uno::Reference
<text::XTextContent
> xContentControl(
154 xMSF
->createInstance(u
"com.sun.star.text.ContentControl"_ustr
), uno::UNO_QUERY
);
155 uno::Reference
<beans::XPropertySet
> xContentControlProps(xContentControl
, uno::UNO_QUERY
);
156 xContentControlProps
->setPropertyValue(u
"Checkbox"_ustr
, uno::Any(true));
157 xText
->insertTextContent(xCursor
, xContentControl
, /*bAbsorb=*/true);
159 // When entering the content control:
160 SwWrtShell
* pWrtShell
= getSwDocShell()->GetWrtShell();
161 pWrtShell
->SttEndDoc(/*bStt=*/true);
162 pWrtShell
->Right(SwCursorSkipMode::Chars
, /*bSelect=*/false, 1, /*bBasicCall=*/false);
164 // Then make sure that the cursor is read-only:
165 // Without the accompanying fix in place, this test would have failed, it was possible to type
166 // into the checkbox content control, just to loose the typed content on the next click.
167 CPPUNIT_ASSERT(pWrtShell
->HasReadonlySel());
170 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testTdf135451
)
173 SwWrtShell
* pWrtShell
= getSwDocShell()->GetWrtShell();
175 // Insert narrow no-break space and move the cursor right before it
176 pWrtShell
->Insert(u
"a" + OUStringChar(CHAR_NNBSP
) + "b");
177 pWrtShell
->EndPara(/*bSelect=*/false);
178 pWrtShell
->Left(SwCursorSkipMode::Chars
, /*bSelect=*/false, 1, /*bBasicCall=*/false);
179 pWrtShell
->GoPrevWord();
180 pWrtShell
->Right(SwCursorSkipMode::Chars
, /*bSelect=*/true, 1, /*bBasicCall=*/false);
182 // Without the accompanying fix in place, this test would have failed with:
184 // - Actual : CHAR_NNBSP
185 // i.e., the cursor did not move over the narrow no-break space (CHAR_NNBSP)
186 CPPUNIT_ASSERT_EQUAL(u
"a"_ustr
, pWrtShell
->GetSelText());
189 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testDropdownContentControl
)
191 // Given a document with a dropdown content control:
193 SwWrtShell
* pWrtShell
= getSwDocShell()->GetWrtShell();
194 pWrtShell
->InsertContentControl(SwContentControlType::DROP_DOWN_LIST
);
196 // When entering the content control:
197 pWrtShell
->SttEndDoc(/*bStt=*/true);
198 pWrtShell
->Right(SwCursorSkipMode::Chars
, /*bSelect=*/false, 1, /*bBasicCall=*/false);
200 // Then make sure that the cursor is read-only:
201 // Without the accompanying fix in place, this test would have failed, it was possible to type
202 // into the drop-down content control, providing content that is not one of the list items.
203 CPPUNIT_ASSERT(pWrtShell
->HasReadonlySel());
206 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest
, testContentControlProtectedSection
)
208 // Given a document with a date content control in a protected section:
210 SwWrtShell
* pWrtShell
= getSwDocShell()->GetWrtShell();
211 pWrtShell
->InsertContentControl(SwContentControlType::DATE
);
213 OUString aSectionName
= pWrtShell
->GetUniqueSectionName();
214 SwSectionData
aSection(SectionType::Content
, aSectionName
);
215 aSection
.SetProtectFlag(true);
216 pWrtShell
->InsertSection(aSection
);
218 // When entering the content control:
219 pWrtShell
->SttEndDoc(/*bStt=*/true);
220 pWrtShell
->Right(SwCursorSkipMode::Chars
, /*bSelect=*/false, 1, /*bBasicCall=*/false);
222 // Then make sure that the cursor is read-only:
223 // Without the accompanying fix in place, this test would have failed, it was not possible to
224 // pick a date in a protected section (the new value was inserted, but the placeholder was not
226 CPPUNIT_ASSERT(!pWrtShell
->HasReadonlySel());
229 CPPUNIT_PLUGIN_IMPLEMENT();
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */