Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / core / fields / fields.cxx
blobf2fdd808734fd8263f99de835aef7ca3f85cdb36
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/text/XTextDocument.hpp>
13 #include <com/sun/star/text/XTextField.hpp>
14 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
16 #include <comphelper/propertyvalue.hxx>
18 #include <authfld.hxx>
19 #include <docsh.hxx>
20 #include <rootfrm.hxx>
21 #include <wrtsh.hxx>
23 namespace
25 /// Covers sw/source/core/fields/ fixes.
26 class Test : public SwModelTestBase
28 public:
29 Test()
30 : SwModelTestBase("/sw/qa/core/fields/data/")
35 CPPUNIT_TEST_FIXTURE(Test, testAuthorityTooltip)
37 // Create a document with a bibliography reference in it.
38 createSwDoc();
39 SwDoc* pDoc = getSwDoc();
40 uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
41 uno::Reference<beans::XPropertySet> xField(
42 xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY);
43 uno::Sequence<beans::PropertyValue> aFields = {
44 comphelper::makePropertyValue("Identifier", OUString("ARJ00")),
45 comphelper::makePropertyValue("Author", OUString("Ar, J")),
46 comphelper::makePropertyValue("Title", OUString("mytitle")),
47 comphelper::makePropertyValue("Year", OUString("2020")),
49 xField->setPropertyValue("Fields", uno::Any(aFields));
50 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
51 uno::Reference<text::XText> xText = xTextDocument->getText();
52 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
53 uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
54 xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
56 // Get the tooltip of the field.
57 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
58 pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
59 SwPaM* pCursor = pWrtShell->GetCursor();
60 auto pField = dynamic_cast<SwAuthorityField*>(
61 SwCursorShell::GetFieldAtCursor(pCursor, /*bIncludeInputFieldAtStart=*/true));
62 CPPUNIT_ASSERT(pField);
63 const SwRootFrame* pLayout = pWrtShell->GetLayout();
64 OUString aTooltip = pField->GetAuthority(pLayout);
66 // Without the accompanying fix in place, generating this tooltip text was not possible without
67 // first inserting an empty bibliography table into the document.
68 CPPUNIT_ASSERT_EQUAL(OUString("ARJ00: Ar, J, mytitle, 2020"), aTooltip);
71 CPPUNIT_TEST_FIXTURE(Test, testTdf143424)
73 createSwDoc("tdf143424.odt");
75 uno::Reference<text::XTextFieldsSupplier> xTextFieldsSupplier(mxComponent, uno::UNO_QUERY);
76 uno::Reference<container::XEnumerationAccess> xFieldsAccess(
77 xTextFieldsSupplier->getTextFields());
78 uno::Reference<container::XEnumeration> xFields(xFieldsAccess->createEnumeration());
80 // TODO: I have no idea why fields are enumerated in invalid order, not like in document
82 // Field: Chapter Format: Chapter name
83 uno::Reference<text::XTextField> xField(xFields->nextElement(), uno::UNO_QUERY);
84 CPPUNIT_ASSERT_EQUAL(OUString("Another title"), xField->getPresentation(false));
86 // Field: Chapter Format: Chapter number and name
87 xField.set(xFields->nextElement(), uno::UNO_QUERY);
88 CPPUNIT_ASSERT_EQUAL(OUString("Chapter 2 - Another title"), xField->getPresentation(false));
90 // Field: Chapter Format: Chapter number
91 xField.set(xFields->nextElement(), uno::UNO_QUERY);
92 CPPUNIT_ASSERT_EQUAL(OUString("Chapter 2 -"), xField->getPresentation(false));
94 // Field: Chapter Format: Chapter number without separator
95 xField.set(xFields->nextElement(), uno::UNO_QUERY);
96 CPPUNIT_ASSERT_EQUAL(OUString("2"), xField->getPresentation(false));
99 CPPUNIT_TEST_FIXTURE(Test, testChapterFieldsFollowedBy)
101 createSwDoc("chapter_field_followedby.odt");
103 uno::Reference<text::XTextFieldsSupplier> xTextFieldsSupplier(mxComponent, uno::UNO_QUERY);
104 uno::Reference<container::XEnumerationAccess> xFieldsAccess(
105 xTextFieldsSupplier->getTextFields());
106 uno::Reference<container::XEnumeration> xFields(xFieldsAccess->createEnumeration());
108 // TODO: I have no idea why fields are enumerated in invalid order, not like in document
109 std::vector<OUString> aFieldValues = {
110 "Followed by tab", // #1
111 "I.I.I.I", // #16
112 ">I.I.I.I< Followed by newline", // #15 Linefeed is replaced by space
113 ">I.I.I.I<", // #14
114 "Followed by newline", // #13
115 "I.I.I", // #12
116 ">I.I.I<Followed by nothing", // #11 Nothing between text & outline
117 ">I.I.I<", // #10
118 "Followed by nothing", // #9
119 "I.I", // #8
120 ">I.I< Followed by space", // #7 Space as is
121 ">I.I<", // #6
122 "Followed by space", // #5
123 "I", // #4
124 ">I< Followed by tab", // #3 Here is a tab, but replaced by space in field
125 ">I<", // #2
128 for (const auto& sValue : aFieldValues)
130 CPPUNIT_ASSERT(xFields->hasMoreElements());
131 uno::Reference<text::XTextField> xField(xFields->nextElement(), uno::UNO_QUERY);
132 CPPUNIT_ASSERT_EQUAL(sValue, xField->getPresentation(false));
137 CPPUNIT_PLUGIN_IMPLEMENT();
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */