From 267c6f2ac71f92999e969232431ba04678e7437e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 07:54:39 +0200 Subject: Adding upstream version 4:24.2.0. Signed-off-by: Daniel Baumann --- .../core/fields/data/chapter_field_followedby.odt | Bin 0 -> 11183 bytes sw/qa/core/fields/data/styleref-flags.docx | Bin 0 -> 23218 bytes sw/qa/core/fields/data/styleref.odt | Bin 0 -> 40280 bytes sw/qa/core/fields/data/suppress-non-numerical.docx | Bin 0 -> 17046 bytes sw/qa/core/fields/data/tdf143424.odt | Bin 0 -> 10600 bytes sw/qa/core/fields/data/tdf86790.docx | Bin 0 -> 15679 bytes sw/qa/core/fields/fields.cxx | 484 +++++++++++++++++++++ 7 files changed, 484 insertions(+) create mode 100644 sw/qa/core/fields/data/chapter_field_followedby.odt create mode 100644 sw/qa/core/fields/data/styleref-flags.docx create mode 100644 sw/qa/core/fields/data/styleref.odt create mode 100644 sw/qa/core/fields/data/suppress-non-numerical.docx create mode 100644 sw/qa/core/fields/data/tdf143424.odt create mode 100644 sw/qa/core/fields/data/tdf86790.docx create mode 100644 sw/qa/core/fields/fields.cxx (limited to 'sw/qa/core/fields') diff --git a/sw/qa/core/fields/data/chapter_field_followedby.odt b/sw/qa/core/fields/data/chapter_field_followedby.odt new file mode 100644 index 0000000000..b2086f2591 Binary files /dev/null and b/sw/qa/core/fields/data/chapter_field_followedby.odt differ diff --git a/sw/qa/core/fields/data/styleref-flags.docx b/sw/qa/core/fields/data/styleref-flags.docx new file mode 100644 index 0000000000..443624a575 Binary files /dev/null and b/sw/qa/core/fields/data/styleref-flags.docx differ diff --git a/sw/qa/core/fields/data/styleref.odt b/sw/qa/core/fields/data/styleref.odt new file mode 100644 index 0000000000..27af5aae1f Binary files /dev/null and b/sw/qa/core/fields/data/styleref.odt differ diff --git a/sw/qa/core/fields/data/suppress-non-numerical.docx b/sw/qa/core/fields/data/suppress-non-numerical.docx new file mode 100644 index 0000000000..439e048b9f Binary files /dev/null and b/sw/qa/core/fields/data/suppress-non-numerical.docx differ diff --git a/sw/qa/core/fields/data/tdf143424.odt b/sw/qa/core/fields/data/tdf143424.odt new file mode 100644 index 0000000000..d485267f12 Binary files /dev/null and b/sw/qa/core/fields/data/tdf143424.odt differ diff --git a/sw/qa/core/fields/data/tdf86790.docx b/sw/qa/core/fields/data/tdf86790.docx new file mode 100644 index 0000000000..a45c03f6b8 Binary files /dev/null and b/sw/qa/core/fields/data/tdf86790.docx differ diff --git a/sw/qa/core/fields/fields.cxx b/sw/qa/core/fields/fields.cxx new file mode 100644 index 0000000000..b783dc0cf3 --- /dev/null +++ b/sw/qa/core/fields/fields.cxx @@ -0,0 +1,484 @@ +/* -*- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace +{ +/// Covers sw/source/core/fields/ fixes. +class Test : public SwModelTestBase +{ +public: + Test() + : SwModelTestBase("/sw/qa/core/fields/data/") + { + } +}; + +CPPUNIT_TEST_FIXTURE(Test, testAuthorityTooltip) +{ + // Create a document with a bibliography reference in it. + createSwDoc(); + SwDoc* pDoc = getSwDoc(); + 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(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); + SwPaM* pCursor = pWrtShell->GetCursor(); + auto pField = dynamic_cast( + SwCursorShell::GetFieldAtCursor(pCursor, /*bIncludeInputFieldAtStart=*/true)); + CPPUNIT_ASSERT(pField); + const SwRootFrame* pLayout = pWrtShell->GetLayout(); + OUString aTooltip = pField->GetAuthority(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("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("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_TEST_FIXTURE(Test, testTdf86790) +{ + loadFromFile(u"tdf86790.docx"); + + uno::Reference xTextFieldsSupplier(mxComponent, uno::UNO_QUERY); + uno::Reference xFieldsAccess( + xTextFieldsSupplier->getTextFields()); + uno::Reference xFields(xFieldsAccess->createEnumeration()); + + // Similarly to testChapterFieldsFollowedBy, the fields are enumerated with #1 first and everything else in reverse order + std::vector> aFieldValues = { + { " Heading 1", "1" }, // #1 + { " foobar", "22.2" }, // #5 + { " foobar", "4" }, // #4 + { " Heading 2", "1.1" }, // #3 + { " Heading 2", "1.1" }, // #2 + }; + + for (const auto& sValue : aFieldValues) + { + CPPUNIT_ASSERT(xFields->hasMoreElements()); + uno::Reference xField(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(sValue.first, xField->getPresentation(true)); + CPPUNIT_ASSERT_EQUAL(sValue.second, xField->getPresentation(false)); + } + CPPUNIT_ASSERT(!xFields->hasMoreElements()); +} + +void InsertParagraphBreak(const uno::Reference& xCursor) +{ + uno::Reference xText = xCursor->getText(); + xText->insertControlCharacter(xCursor, text::ControlCharacter::PARAGRAPH_BREAK, false); +} +void InsertHeading(const uno::Reference& xCursor, const OUString& content) +{ + uno::Reference xCursorPropertySet(xCursor, uno::UNO_QUERY); + uno::Reference xText = xCursor->getText(); + + xCursorPropertySet->setPropertyValue("ParaStyleName", uno::Any(OUString("Heading 1"))); + xText->insertString(xCursor, content, false); + InsertParagraphBreak(xCursor); +} + +/// If there is referenced text both above and below, STYLEREF searches up +CPPUNIT_TEST_FIXTURE(Test, testStyleRefSearchUp) +{ + // Arrange + // Create a document with headings both above and below a cursor + createSwDoc(); + + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + + uno::Reference xCursor = xText->createTextCursor(); + + InsertHeading(xCursor, "Heading far above field"); + InsertHeading(xCursor, "Heading above field"); + InsertParagraphBreak(xCursor); + InsertHeading(xCursor, "Heading below field"); + InsertHeading(xCursor, "Heading far below field"); + + uno::Reference xParagraphCursor(xCursor, uno::UNO_QUERY); + xParagraphCursor->gotoPreviousParagraph(false); // Heading far below... + xParagraphCursor->gotoPreviousParagraph(false); // Heading below... + xParagraphCursor->gotoPreviousParagraph(false); // Blank space + + // Act + // Insert a STYLEREF field which looks for "Heading 1"s + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.GetReference"), uno::UNO_QUERY); + + uno::Reference xFieldPropertySet(xField, uno::UNO_QUERY); + xFieldPropertySet->setPropertyValue("ReferenceFieldSource", + uno::Any(sal_Int16(text::ReferenceFieldSource::STYLE))); + xFieldPropertySet->setPropertyValue("ReferenceFieldPart", + uno::Any(sal_Int16(text::ReferenceFieldPart::TEXT))); + xFieldPropertySet->setPropertyValue("SourceName", uno::Any(OUString("Heading 1"))); + + xField->attach(xCursor); + + // Assert + // Make sure the field has the right text + CPPUNIT_ASSERT_EQUAL(OUString("Heading above field"), xField->getPresentation(false)); +} + +/// If there is referenced text only below, STYLEREF searches down +CPPUNIT_TEST_FIXTURE(Test, testStyleRefSearchDown) +{ + // Arrange + // Create a document with headings below a cursor + createSwDoc(); + + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + + uno::Reference xCursor = xText->createTextCursor(); + + InsertParagraphBreak(xCursor); + InsertHeading(xCursor, "Heading below field"); + InsertHeading(xCursor, "Heading far below field"); + + uno::Reference xParagraphCursor(xCursor, uno::UNO_QUERY); + xParagraphCursor->gotoPreviousParagraph(false); // Heading far below... + xParagraphCursor->gotoPreviousParagraph(false); // Heading below... + xParagraphCursor->gotoPreviousParagraph(false); // Blank space + + // Act + // Insert a STYLEREF field which looks for "Heading 1"s + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.GetReference"), uno::UNO_QUERY); + + uno::Reference xFieldPropertySet(xField, uno::UNO_QUERY); + xFieldPropertySet->setPropertyValue("ReferenceFieldSource", + uno::Any(sal_Int16(text::ReferenceFieldSource::STYLE))); + xFieldPropertySet->setPropertyValue("ReferenceFieldPart", + uno::Any(sal_Int16(text::ReferenceFieldPart::TEXT))); + xFieldPropertySet->setPropertyValue("SourceName", uno::Any(OUString("Heading 1"))); + + xField->attach(xCursor); + + // Assert + // Make sure the field has the right text + CPPUNIT_ASSERT_EQUAL(OUString("Heading below field"), xField->getPresentation(false)); +} + +/// STYLEREFs in marginals (headers or footers) should search in the page they are on first, regardless if there is anything above them +CPPUNIT_TEST_FIXTURE(Test, testMarginalStyleRef) +{ + // Arrange + // Create a document with 2 headings + createSwDoc(); + + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + + uno::Reference xCursor = xText->createTextCursor(); + + InsertHeading(xCursor, "Top heading on page"); + InsertHeading(xCursor, "Bottom heading on page"); + + // Act + // Insert a STYLEREF field which looks for "Heading 1"s into the footer + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.GetReference"), uno::UNO_QUERY); + + uno::Reference xStyleFamiliesSupplier(mxComponent, + uno::UNO_QUERY); + uno::Reference xParagraphStylesContainer( + xStyleFamiliesSupplier->getStyleFamilies()->getByName("PageStyles"), uno::UNO_QUERY); + + uno::Reference xPagePropertySet( + xParagraphStylesContainer->getByName("Standard"), uno::UNO_QUERY); + + xPagePropertySet->setPropertyValue("FooterIsOn", uno::Any(true)); + uno::Reference xFooterText(xPagePropertySet->getPropertyValue("FooterText"), + uno::UNO_QUERY); + + uno::Reference xFieldPropertySet(xField, uno::UNO_QUERY); + xFieldPropertySet->setPropertyValue("ReferenceFieldSource", + uno::Any(sal_Int16(text::ReferenceFieldSource::STYLE))); + xFieldPropertySet->setPropertyValue("ReferenceFieldPart", + uno::Any(sal_Int16(text::ReferenceFieldPart::TEXT))); + xFieldPropertySet->setPropertyValue("SourceName", uno::Any(OUString("Heading 1"))); + + uno::Reference xFooterCursor = xFooterText->createTextCursor(); + xField->attach(xFooterCursor); + + // Assert + // Make sure the field has the right text + CPPUNIT_ASSERT_EQUAL(OUString("Top heading on page"), xField->getPresentation(false)); +} + +/// STYLEREFs in footnotes should search from the point of the reference mark +CPPUNIT_TEST_FIXTURE(Test, testFootnoteStyleRef) +{ + // Arrange + // Create a document with headings both above and below a cursor + createSwDoc(); + + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + + uno::Reference xCursor = xText->createTextCursor(); + + InsertHeading(xCursor, "Heading far above reference mark"); + InsertHeading(xCursor, "Heading above reference mark"); + InsertParagraphBreak(xCursor); + InsertHeading(xCursor, "Heading below reference mark"); + InsertHeading(xCursor, "Heading far below reference mark"); + + uno::Reference xParagraphCursor(xCursor, uno::UNO_QUERY); + xParagraphCursor->gotoPreviousParagraph(false); // Heading far below... + xParagraphCursor->gotoPreviousParagraph(false); // Heading below... + xParagraphCursor->gotoPreviousParagraph(false); // Blank space + + // Act + // Insert a STYLEREF field which looks for "Heading 1"s into a footnote + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + + uno::Reference xFootnote( + xFactory->createInstance("com.sun.star.text.Footnote"), uno::UNO_QUERY); + xFootnote->setLabel("Style reference mark"); + xText->insertTextContent(xCursor, xFootnote, false); + + uno::Reference xField( + xFactory->createInstance("com.sun.star.text.TextField.GetReference"), uno::UNO_QUERY); + + uno::Reference xFieldPropertySet(xField, uno::UNO_QUERY); + xFieldPropertySet->setPropertyValue("ReferenceFieldSource", + uno::Any(sal_Int16(text::ReferenceFieldSource::STYLE))); + xFieldPropertySet->setPropertyValue("ReferenceFieldPart", + uno::Any(sal_Int16(text::ReferenceFieldPart::TEXT))); + xFieldPropertySet->setPropertyValue("SourceName", uno::Any(OUString("Heading 1"))); + + uno::Reference xFootnoteText(xFootnote, uno::UNO_QUERY); + uno::Reference xFootnoteCursor = xFootnoteText->createTextCursor(); + xField->attach(xFootnoteCursor); + + // Assert + // Make sure the field has the right text + CPPUNIT_ASSERT_EQUAL(OUString("Heading above reference mark"), xField->getPresentation(false)); +} + +/// STYLEREFs with the REFFLDFLAG_HIDE_NON_NUMERICAL flag should hide all characters that are not numerical or delimiters +CPPUNIT_TEST_FIXTURE(Test, testNumericalStyleRef) +{ + loadFromFile(u"suppress-non-numerical.docx"); + + uno::Reference xTextFieldsSupplier(mxComponent, uno::UNO_QUERY); + uno::Reference xFieldsAccess( + xTextFieldsSupplier->getTextFields()); + uno::Reference xFields(xFieldsAccess->createEnumeration()); + + // Similarly to testChapterFieldsFollowedBy, the fields are enumerated with #1 first and everything else in reverse order + std::vector> aFieldValues = { + { " Heading 1", "3" }, // #1 + { " Heading 2", ".2\\@123^&~|a....." }, // #4 + { " Heading 2", ".2\\|a....." }, // #3 + { " Heading 1", "Chapter 3.!" }, // #2 + }; + + for (const auto& sValue : aFieldValues) + { + CPPUNIT_ASSERT(xFields->hasMoreElements()); + uno::Reference xField(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(sValue.first, xField->getPresentation(true)); + CPPUNIT_ASSERT_EQUAL(sValue.second, xField->getPresentation(false)); + } + CPPUNIT_ASSERT(!xFields->hasMoreElements()); +} + +/// We should be able to import these flags from OOXML documents +/// (Note: in Word, STYLEREF 'search bottom up' flag does nothing unless it's in a marginal, we place no such limitation but +/// this docx only has such stylerefs to avoid confusion) +CPPUNIT_TEST_FIXTURE(Test, testOOXMLStyleRefFlags) +{ + loadFromFile(u"styleref-flags.docx"); + + uno::Reference xTextFieldsSupplier(mxComponent, uno::UNO_QUERY); + uno::Reference xFieldsAccess( + xTextFieldsSupplier->getTextFields()); + uno::Reference xFields(xFieldsAccess->createEnumeration()); + + // Similarly to testChapterFieldsFollowedBy, the fields are enumerated with #1 first and everything else in reverse order + std::vector> aFieldValues = { + { " Heading 2", "2.1" }, // #1 + { " Heading 1", "3" }, // #5 + { " Heading 1", "A top level heading at the top of the page" }, // #4 + { " Heading 1", "The last top level heading" }, // #3 + { " Heading 2", "Heading 2.1" }, // #2 + }; + + for (const auto& sValue : aFieldValues) + { + CPPUNIT_ASSERT(xFields->hasMoreElements()); + uno::Reference xField(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(sValue.first, xField->getPresentation(true)); + CPPUNIT_ASSERT_EQUAL(sValue.second, xField->getPresentation(false)); + } + CPPUNIT_ASSERT(!xFields->hasMoreElements()); +} + +/// We should be able to import these flags from OOXML documents +/// (Note: in Word, STYLEREF 'search bottom up' flag does nothing unless it's in a marginal, we place no such limitation but +/// this docx only has such stylerefs to avoid confusion) +CPPUNIT_TEST_FIXTURE(Test, testODFStyleRef) +{ + loadFromFile(u"styleref.odt"); + + uno::Reference xTextFieldsSupplier(mxComponent, uno::UNO_QUERY); + uno::Reference xFieldsAccess( + xTextFieldsSupplier->getTextFields()); + uno::Reference xFields(xFieldsAccess->createEnumeration()); + + // Similarly to testChapterFieldsFollowedBy, the fields are enumerated with #1 first and everything else in reverse order + std::vector> aFieldValues = { + { " Heading 1", "1:" }, // #1 + { " Heading 2", "In footnotes" }, // #10 + { " Heading 2", "In document text" }, // #9 + { " Heading 1", "STYLEREF in different locations" }, // #8 + { " Heading 1", "2:" }, // #7 + { " Heading 1", "With STYLEREF, many things are possible" }, // #6 + { " Heading 1", "1:" }, // #5 + { " Heading 1", "STYLEREF in different locations" }, // #4 + { " Heading 1", "2:" }, // #3 + { " Heading 1", "With STYLEREF, many things are possible" }, // #2 + }; + + for (const auto& sValue : aFieldValues) + { + CPPUNIT_ASSERT(xFields->hasMoreElements()); + uno::Reference xField(xFields->nextElement(), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(sValue.first, xField->getPresentation(true)); + CPPUNIT_ASSERT_EQUAL(sValue.second, xField->getPresentation(false)); + } + CPPUNIT_ASSERT(!xFields->hasMoreElements()); +} +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3