From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- sw/qa/core/fields/fields.cxx | 138 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 sw/qa/core/fields/fields.cxx (limited to 'sw/qa/core/fields/fields.cxx') diff --git a/sw/qa/core/fields/fields.cxx b/sw/qa/core/fields/fields.cxx new file mode 100644 index 000000000..9df1baf12 --- /dev/null +++ b/sw/qa/core/fields/fields.cxx @@ -0,0 +1,138 @@ +/* -*- 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 + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace +{ +/// Covers sw/source/core/fields/ fixes. +class Test : public SwModelTestBase +{ +}; + +constexpr OUStringLiteral DATA_DIRECTORY = u"/sw/qa/core/fields/data/"; + +CPPUNIT_TEST_FIXTURE(Test, testAuthorityTooltip) +{ + // Create a document with a bibliography reference in it. + SwDoc* pDoc = createSwDoc(); + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY); + uno::Sequence aFields = { + comphelper::makePropertyValue("Identifier", OUString("ARJ00")), + comphelper::makePropertyValue("Author", OUString("Ar, J")), + comphelper::makePropertyValue("Title", OUString("mytitle")), + comphelper::makePropertyValue("Year", OUString("2020")), + }; + xField->setPropertyValue("Fields", uno::Any(aFields)); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + uno::Reference xContent(xField, uno::UNO_QUERY); + xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); + + // Get the tooltip of the field. + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 1, /*bBasicCall=*/false); + SwPaM* pCursor = pWrtShell->GetCursor(); + auto pField = dynamic_cast( + SwCursorShell::GetFieldAtCursor(pCursor, /*bIncludeInputFieldAtStart=*/true)); + CPPUNIT_ASSERT(pField); + SwTextNode* pTextNode = pCursor->GetNode().GetTextNode(); + const SwTextAttr* pTextAttr = pTextNode->GetSwpHints().Get(0); + const SwRootFrame* pLayout = pWrtShell->GetLayout(); + OUString aTooltip = pField->GetAuthority(pTextAttr, pLayout); + + // Without the accompanying fix in place, generating this tooltip text was not possible without + // first inserting an empty bibliography table into the document. + CPPUNIT_ASSERT_EQUAL(OUString("ARJ00: Ar, J, mytitle, 2020"), aTooltip); +} + +CPPUNIT_TEST_FIXTURE(Test, testTdf143424) +{ + createSwDoc(DATA_DIRECTORY, "tdf143424.odt"); + + uno::Reference xTextFieldsSupplier(mxComponent, uno::UNO_QUERY); + uno::Reference xFieldsAccess( + xTextFieldsSupplier->getTextFields()); + uno::Reference xFields(xFieldsAccess->createEnumeration()); + + // TODO: I have no idea why fields are enumerated in invalid order, not like in document + + // Field: Chapter Format: Chapter name + uno::Reference xField(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("Another title"), xField->getPresentation(false)); + + // Field: Chapter Format: Chapter number and name + xField.set(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("Chapter 2 - Another title"), xField->getPresentation(false)); + + // Field: Chapter Format: Chapter number + xField.set(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("Chapter 2 -"), xField->getPresentation(false)); + + // Field: Chapter Format: Chapter number without separator + xField.set(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("2"), xField->getPresentation(false)); +} + +CPPUNIT_TEST_FIXTURE(Test, testChapterFieldsFollowedBy) +{ + createSwDoc(DATA_DIRECTORY, "chapter_field_followedby.odt"); + + uno::Reference xTextFieldsSupplier(mxComponent, uno::UNO_QUERY); + uno::Reference xFieldsAccess( + xTextFieldsSupplier->getTextFields()); + uno::Reference xFields(xFieldsAccess->createEnumeration()); + + // TODO: I have no idea why fields are enumerated in invalid order, not like in document + std::vector aFieldValues = { + "Followed by tab", // #1 + "I.I.I.I", // #16 + ">I.I.I.I< Followed by newline", // #15 Linefeed is replaced by space + ">I.I.I.I<", // #14 + "Followed by newline", // #13 + "I.I.I", // #12 + ">I.I.II.I.I<", // #10 + "Followed by nothing", // #9 + "I.I", // #8 + ">I.I< Followed by space", // #7 Space as is + ">I.I<", // #6 + "Followed by space", // #5 + "I", // #4 + ">I< Followed by tab", // #3 Here is a tab, but replaced by space in field + ">I<", // #2 + }; + + for (const auto& sValue : aFieldValues) + { + CPPUNIT_ASSERT(xFields->hasMoreElements()); + uno::Reference xField(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(sValue, xField->getPresentation(false)); + } +} +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3