diff options
Diffstat (limited to 'sw/qa/core/crsr/crsr.cxx')
-rw-r--r-- | sw/qa/core/crsr/crsr.cxx | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/sw/qa/core/crsr/crsr.cxx b/sw/qa/core/crsr/crsr.cxx new file mode 100644 index 000000000..3dc236c41 --- /dev/null +++ b/sw/qa/core/crsr/crsr.cxx @@ -0,0 +1,193 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <swmodeltestbase.hxx> + +#include <com/sun/star/awt/FontWeight.hpp> +#include <com/sun/star/text/ControlCharacter.hpp> +#include <com/sun/star/view/XLineCursor.hpp> +#include <com/sun/star/text/XTextDocument.hpp> +#include <com/sun/star/text/XTextViewCursorSupplier.hpp> +#include <com/sun/star/text/XTextViewCursor.hpp> + +#include <comphelper/propertysequence.hxx> +#include <svl/srchitem.hxx> +#include <vcl/scheduler.hxx> + +#include <formatcontentcontrol.hxx> +#include <docsh.hxx> +#include <unotxdoc.hxx> +#include <wrtsh.hxx> +#include <ndtxt.hxx> + +constexpr OUStringLiteral DATA_DIRECTORY = u"/sw/qa/core/crsr/data/"; + +/// Covers sw/source/core/crsr/ fixes. +class SwCoreCrsrTest : public SwModelTestBase +{ +}; + +CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testFindReplace) +{ + loadURL("private:factory/swriter", nullptr); + + // Given: a document with two "foo" in it, the second followed by a formatted soft hyphen. + uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XText> xText = xTextDocument->getText(); + // Create a document which has 2 lines: first line has foo in it, second line has the same, but + // followed by a formatted soft hyphen. + xText->insertString(xText->getEnd(), "foo xxx", /*bAbsorb=*/false); + xText->insertControlCharacter(xText->getEnd(), text::ControlCharacter::PARAGRAPH_BREAK, + /*bAbsorb=*/false); + xText->insertString(xText->getEnd(), OUString(u"foo xxx \xad after"), /*bAbsorb=*/false); + uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier( + xModel->getCurrentController(), uno::UNO_QUERY); + uno::Reference<text::XTextViewCursor> xViewCursor = xTextViewCursorSupplier->getViewCursor(); + xViewCursor->gotoEnd(/*bExpand=*/false); + xViewCursor->goLeft(/*nCount=*/6, /*bExpand=*/false); + xViewCursor->goLeft(/*nCount=*/1, /*bExpand=*/true); + uno::Reference<beans::XPropertySet> xViewCursorProps(xViewCursor, uno::UNO_QUERY); + xViewCursorProps->setPropertyValue("CharWeight", uno::Any(awt::FontWeight::BOLD)); + xViewCursor->gotoStart(/*bExpand=*/false); + + // When: doing search & replace 3 times. + uno::Sequence<beans::PropertyValue> aArgs(comphelper::InitPropertySequence({ + { "SearchItem.SearchString", uno::Any(OUString("foo")) }, + { "SearchItem.ReplaceString", uno::Any(OUString("bar")) }, + { "SearchItem.Command", uno::Any(static_cast<sal_Int16>(SvxSearchCmd::REPLACE)) }, + })); + // Find the first foo. + dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs); + // Replace the first foo. + dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs); + // Replace the second foo. + dispatchCommand(mxComponent, ".uno:ExecuteSearch", aArgs); + + // Then: the second "foo" should be replaced as well. + xViewCursor->gotoEnd(/*bExpand=*/false); + uno::Reference<view::XLineCursor> xLineCursor(xViewCursor, uno::UNO_QUERY); + xLineCursor->gotoStartOfLine(/*bExpand=*/true); + OUString aActual = xViewCursor->getString(); + CPPUNIT_ASSERT_GREATEREQUAL(static_cast<sal_Int32>(3), aActual.getLength()); + OUString aActualStart = aActual.copy(0, 3); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: bar + // - Actual : foo + // i.e. the foo on the second line was not replaced. + CPPUNIT_ASSERT_EQUAL(OUString("bar"), aActualStart); +} + +CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testSelAllStartsWithTable) +{ + load(DATA_DIRECTORY, "sel-all-starts-with-table.odt"); + SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get()); + SwDocShell* pDocShell = pTextDoc->GetDocShell(); + SwDoc* pDoc = pDocShell->GetDoc(); + SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); + + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pDoc->GetTableFrameFormatCount(/*bUsed=*/true)); + + pWrtShell->SelAll(); + pWrtShell->SelAll(); + Scheduler::ProcessEventsToIdle(); + pWrtShell->DelLeft(); + + // Without the accompanying fix in place, this test would have failed with: + // - Expected: 0 + // - Actual : 1 + // i.e. the table selection was lost and the table was not deleted. + CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), pDoc->GetTableFrameFormatCount(/*bUsed=*/true)); +} + +CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testContentControlLineBreak) +{ + // Given a document with a (rich text) content control: + SwDoc* pDoc = createSwDoc(); + uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XText> xText = xTextDocument->getText(); + uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor(); + xText->insertString(xCursor, "test", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference<text::XTextContent> xContentControl( + xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true); + + // When pressing "enter" in the middle of that content control: + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->SttEndDoc(/*bStt=*/true); + // Go after "t". + pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/false, 2, /*bBasicCall=*/false); + dispatchCommand(mxComponent, ".uno:InsertPara", {}); + + // Then make sure that we only insert a line break, not a new paragraph: + SwTextNode* pTextNode = pWrtShell->GetCursor()->GetMark()->nNode.GetNode().GetTextNode(); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: t\nest + // - Actual : est + // i.e. a new paragraph was inserted, which is not allowed for inline content controls. + CPPUNIT_ASSERT_EQUAL(OUString("t\nest"), pTextNode->GetExpandText(pWrtShell->GetLayout())); +} + +CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testContentControlReadOnly) +{ + // Given a document with a checkbox content control: + SwDoc* pDoc = createSwDoc(); + uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference<text::XText> xText = xTextDocument->getText(); + uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor(); + xText->insertString(xCursor, u"☐", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference<text::XTextContent> xContentControl( + xMSF->createInstance("com.sun.star.text.ContentControl"), uno::UNO_QUERY); + uno::Reference<beans::XPropertySet> xContentControlProps(xContentControl, uno::UNO_QUERY); + xContentControlProps->setPropertyValue("Checkbox", uno::Any(true)); + xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true); + + // When entering the content control: + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->SttEndDoc(/*bStt=*/true); + pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/false, 1, /*bBasicCall=*/false); + + // Then make sure that the cursor is read-only: + // Without the accompanying fix in place, this test would have failed, it was possible to type + // into the checkbox content control, just to loose the typed content on the next click. + CPPUNIT_ASSERT(pWrtShell->HasReadonlySel()); +} + +CPPUNIT_TEST_FIXTURE(SwCoreCrsrTest, testContentControlProtectedSection) +{ + // Given a document with a date content control in a protected section: + SwDoc* pDoc = createSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->InsertContentControl(SwContentControlType::DATE); + pWrtShell->SelAll(); + OUString aSectionName = pWrtShell->GetUniqueSectionName(); + SwSectionData aSection(SectionType::Content, aSectionName); + aSection.SetProtectFlag(true); + pWrtShell->InsertSection(aSection); + + // When entering the content control: + pWrtShell->SttEndDoc(/*bStt=*/true); + pWrtShell->Right(CRSR_SKIP_CHARS, /*bSelect=*/false, 1, /*bBasicCall=*/false); + + // Then make sure that the cursor is read-only: + // Without the accompanying fix in place, this test would have failed, it was not possible to + // pick a date in a protected section (the new value was inserted, but the placeholder was not + // removed). + CPPUNIT_ASSERT(!pWrtShell->HasReadonlySel()); +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |