Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / core / crsr / crsr.cxx
blobbf2a504b3304f3c0ee62d95c2972aa49ee2882b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
23 #include <docsh.hxx>
24 #include <unotxdoc.hxx>
25 #include <wrtsh.hxx>
26 #include <ndtxt.hxx>
27 #include <formatcontentcontrol.hxx>
29 /// Covers sw/source/core/crsr/ fixes.
30 class SwCoreCrsrTest : public SwModelTestBase
32 public:
33 SwCoreCrsrTest()
34 : SwModelTestBase("/sw/qa/core/crsr/data/")
39 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testFindReplace)
41 createSwDoc();
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(), "foo xxx", /*bAbsorb=*/false);
49 xText->insertControlCharacter(xText->getEnd(), text::ControlCharacter::PARAGRAPH_BREAK,
50 /*bAbsorb=*/false);
51 xText->insertString(xText->getEnd(), OUString(u"foo xxx \xad after"), /*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("CharWeight", 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(OUString("foo")) },
66 { "SearchItem.ReplaceString", uno::Any(OUString("bar")) },
67 { "SearchItem.Command", uno::Any(static_cast<sal_Int16>(SvxSearchCmd::REPLACE)) },
68 }));
69 // Find the first foo.
70 dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs);
71 // Replace the first foo.
72 dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs);
73 // Replace the second foo.
74 dispatchCommand(mxComponent, ".uno:ExecuteSearch", 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:
84 // - Expected: bar
85 // - Actual : foo
86 // i.e. the foo on the second line was not replaced.
87 CPPUNIT_ASSERT_EQUAL(OUString("bar"), aActualStart);
90 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testSelAllStartsWithTable)
92 createSwDoc("sel-all-starts-with-table.odt");
93 SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
94 SwDocShell* pDocShell = pTextDoc->GetDocShell();
95 SwDoc* pDoc = pDocShell->GetDoc();
96 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
98 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetTableFrameFormatCount(/*bUsed=*/true));
100 pWrtShell->SelAll();
101 pWrtShell->SelAll();
102 Scheduler::ProcessEventsToIdle();
103 pWrtShell->DelLeft();
105 // Without the accompanying fix in place, this test would have failed with:
106 // - Expected: 0
107 // - Actual : 1
108 // i.e. the table selection was lost and the table was not deleted.
109 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pDoc->GetTableFrameFormatCount(/*bUsed=*/true));
112 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testContentControlLineBreak)
114 // Given a document with a (rich text) content control:
115 createSwDoc();
116 SwDoc* pDoc = getSwDoc();
117 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
118 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
119 uno::Reference<text::XText> xText = xTextDocument->getText();
120 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
121 xText->insertString(xCursor, "test", /*bAbsorb=*/false);
122 xCursor->gotoStart(/*bExpand=*/false);
123 xCursor->gotoEnd(/*bExpand=*/true);
124 uno::Reference<text::XTextContent> xContentControl(
125 xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY);
126 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
128 // When pressing "enter" in the middle of that content control:
129 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
130 pWrtShell->SttEndDoc(/*bStt=*/true);
131 // Go after "t".
132 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 2, /*bBasicCall=*/false);
133 dispatchCommand(mxComponent, ".uno:InsertPara", {});
135 // Then make sure that we only insert a line break, not a new paragraph:
136 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetMark()->GetNode().GetTextNode();
137 // Without the accompanying fix in place, this test would have failed with:
138 // - Expected: t\nest
139 // - Actual : est
140 // i.e. a new paragraph was inserted, which is not allowed for inline content controls.
141 CPPUNIT_ASSERT_EQUAL(OUString("t\nest"), pTextNode->GetExpandText(pWrtShell->GetLayout()));
144 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testContentControlReadOnly)
146 // Given a document with a checkbox content control:
147 createSwDoc();
148 SwDoc* pDoc = getSwDoc();
149 uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
150 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
151 uno::Reference<text::XText> xText = xTextDocument->getText();
152 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
153 xText->insertString(xCursor, u"☐", /*bAbsorb=*/false);
154 xCursor->gotoStart(/*bExpand=*/false);
155 xCursor->gotoEnd(/*bExpand=*/true);
156 uno::Reference<text::XTextContent> xContentControl(
157 xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY);
158 uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY);
159 xContentControlProps->setPropertyValue("Checkbox", uno::Any(true));
160 xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
162 // When entering the content control:
163 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
164 pWrtShell->SttEndDoc(/*bStt=*/true);
165 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
167 // Then make sure that the cursor is read-only:
168 // Without the accompanying fix in place, this test would have failed, it was possible to type
169 // into the checkbox content control, just to loose the typed content on the next click.
170 CPPUNIT_ASSERT(pWrtShell->HasReadonlySel());
173 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testTdf135451)
175 createSwDoc();
176 SwDoc* pDoc = getSwDoc();
177 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
179 // Insert narrow no-break space and move the cursor right before it
180 pWrtShell->Insert(u"a" + OUStringChar(CHAR_NNBSP) + "b");
181 pWrtShell->EndPara(/*bSelect=*/false);
182 pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
183 pWrtShell->GoPrevWord();
184 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
186 // Without the accompanying fix in place, this test would have failed with:
187 // - Expected: a
188 // - Actual : CHAR_NNBSP
189 // i.e., the cursor did not move over the narrow no-break space (CHAR_NNBSP)
190 CPPUNIT_ASSERT_EQUAL(OUString("a"), pWrtShell->GetSelText());
193 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testDropdownContentControl)
195 // Given a document with a dropdown content control:
196 createSwDoc();
197 SwDoc* pDoc = getSwDoc();
198 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
199 pWrtShell->InsertContentControl(SwContentControlType::DROP_DOWN_LIST);
201 // When entering the content control:
202 pWrtShell->SttEndDoc(/*bStt=*/true);
203 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
205 // Then make sure that the cursor is read-only:
206 // Without the accompanying fix in place, this test would have failed, it was possible to type
207 // into the drop-down content control, providing content that is not one of the list items.
208 CPPUNIT_ASSERT(pWrtShell->HasReadonlySel());
211 CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testContentControlProtectedSection)
213 // Given a document with a date content control in a protected section:
214 createSwDoc();
215 SwDoc* pDoc = getSwDoc();
216 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
217 pWrtShell->InsertContentControl(SwContentControlType::DATE);
218 pWrtShell->SelAll();
219 OUString aSectionName = pWrtShell->GetUniqueSectionName();
220 SwSectionData aSection(SectionType::Content, aSectionName);
221 aSection.SetProtectFlag(true);
222 pWrtShell->InsertSection(aSection);
224 // When entering the content control:
225 pWrtShell->SttEndDoc(/*bStt=*/true);
226 pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
228 // Then make sure that the cursor is read-only:
229 // Without the accompanying fix in place, this test would have failed, it was not possible to
230 // pick a date in a protected section (the new value was inserted, but the placeholder was not
231 // removed).
232 CPPUNIT_ASSERT(!pWrtShell->HasReadonlySel());
235 CPPUNIT_PLUGIN_IMPLEMENT();
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */