Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / uibase / fldui / fldui.cxx
blob78b2fcedd42a936ec4a4182d280555a116ef1e90
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/BibliographyDataType.hpp>
13 #include <com/sun/star/text/XTextDocument.hpp>
15 #include <rtl/ustrbuf.hxx>
16 #include <comphelper/propertyvalue.hxx>
18 #include <docsh.hxx>
19 #include <wrtsh.hxx>
20 #include <fldmgr.hxx>
21 #include <authfld.hxx>
22 #include <ndtxt.hxx>
24 using namespace com::sun::star;
26 namespace
28 /// Covers sw/source/uibase/fldui/ fixes.
29 class Test : public SwModelTestBase
33 CPPUNIT_TEST_FIXTURE(Test, testBiblioPageNumberUpdate)
35 // Given a document with 2 biblio fields, same properties, but different page number in the URL:
36 createSwDoc();
37 SwDoc* pDoc = getSwDoc();
38 uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
39 uno::Reference<beans::XPropertySet> xField(
40 xFactory->createInstance("com.sun.star.text.TextField.Bibliography"), uno::UNO_QUERY);
41 uno::Sequence<beans::PropertyValue> aFields = {
42 comphelper::makePropertyValue("BibiliographicType", text::BibliographyDataType::WWW),
43 comphelper::makePropertyValue("Identifier", OUString("AT")),
44 comphelper::makePropertyValue("Author", OUString("Author")),
45 comphelper::makePropertyValue("Title", OUString("Title")),
46 comphelper::makePropertyValue("URL", OUString("http://www.example.com/test.pdf#page=1")),
48 xField->setPropertyValue("Fields", uno::Any(aFields));
49 uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
50 uno::Reference<text::XText> xText = xTextDocument->getText();
51 uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
52 uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
53 xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
54 xField.set(xFactory->createInstance("com.sun.star.text.TextField.Bibliography"),
55 uno::UNO_QUERY);
56 aFields = {
57 comphelper::makePropertyValue("BibiliographicType", text::BibliographyDataType::WWW),
58 comphelper::makePropertyValue("Identifier", OUString("AT")),
59 comphelper::makePropertyValue("Author", OUString("Author")),
60 comphelper::makePropertyValue("Title", OUString("Title")),
61 comphelper::makePropertyValue("URL", OUString("http://www.example.com/test.pdf#page=2")),
63 xField->setPropertyValue("Fields", uno::Any(aFields));
64 xContent.set(xField, uno::UNO_QUERY);
65 xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
67 // When changing the page number in the second field's URL:
68 SwDocShell* pDocShell = pDoc->GetDocShell();
69 SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
70 pWrtShell->SttEndDoc(/*bStt=*/false);
71 pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
72 OUString aCoreFields[AUTH_FIELD_END];
73 aCoreFields[AUTH_FIELD_AUTHORITY_TYPE] = OUString::number(text::BibliographyDataType::WWW);
74 aCoreFields[AUTH_FIELD_IDENTIFIER] = "AT";
75 aCoreFields[AUTH_FIELD_AUTHOR] = "Author";
76 aCoreFields[AUTH_FIELD_TITLE] = "Title";
77 OUString aNewUrl = "http://www.example.com/test.pdf#page=42";
78 aCoreFields[AUTH_FIELD_URL] = aNewUrl;
79 OUStringBuffer aFieldBuffer;
80 for (const auto& rField : aCoreFields)
82 aFieldBuffer.append(rField + OUStringChar(TOX_STYLE_DELIMITER));
84 SwFieldMgr aMgr(pWrtShell);
85 aMgr.UpdateCurField(0, aFieldBuffer.makeStringAndClear(), OUString());
87 // Then make sure that the second field's URL is updated:
88 auto pField = static_cast<SwAuthorityField*>(pWrtShell->GetCurField());
89 const SwAuthEntry* pEntry = pField->GetAuthEntry();
90 // Without the accompanying fix in place, this test would have failed with:
91 // - Expected: http://www.example.com/test.pdf#page=42
92 // - Actual : http://www.example.com/test.pdf#page=2
93 // i.e. the second biblio field's URL was not updated.
94 CPPUNIT_ASSERT_EQUAL(aNewUrl, pEntry->GetAuthorField(AUTH_FIELD_URL));
97 CPPUNIT_TEST_FIXTURE(Test, testInsertRefmark)
99 // Given an empty document:
100 createSwDoc();
101 SwDoc* pDoc = getSwDoc();
103 // When inserting a refmark with text:
104 uno::Sequence<css::beans::PropertyValue> aArgs = {
105 comphelper::makePropertyValue("TypeName", uno::Any(OUString("SetRef"))),
106 comphelper::makePropertyValue(
107 "Name", uno::Any(OUString("ZOTERO_ITEM CSL_CITATION {} RNDpyJknp173F"))),
108 comphelper::makePropertyValue("Content", uno::Any(OUString("aaa<b>bbb</b>ccc"))),
110 dispatchCommand(mxComponent, ".uno:InsertField", aArgs);
112 // Then make sure that we create a refmark that covers that text:
113 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
114 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
115 std::vector<SwTextAttr*> aAttrs = pTextNode->GetTextAttrsAt(0, RES_TXTATR_REFMARK);
116 // Without the accompanying fix in place, this test would have failed with:
117 // - Expected: 1
118 // - Actual : 0
119 // i.e. no refmark was created, only the hard to read Type=12 created a refmark.
120 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aAttrs.size());
121 CPPUNIT_ASSERT_EQUAL(OUString("aaabbbccc"), pTextNode->GetText());
124 CPPUNIT_TEST_FIXTURE(Test, testInsertRefmarkSelection)
126 // Given a document with a single selected word:
127 createSwDoc();
128 SwDoc* pDoc = getSwDoc();
129 SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
130 pWrtShell->Insert2("myword");
131 pWrtShell->SelAll();
133 // When inserting a refmark:
134 SwFieldMgr aMgr(pWrtShell);
135 SwInsertField_Data aData(SwFieldTypesEnum::SetRef, /*nSubType=*/0, "myname", "myword",
136 /*nFormatId=*/0);
137 aMgr.InsertField(aData);
139 // Then make sure the document still just contains that word only once:
140 SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
141 // Without the accompanying fix in place, this test would have failed with:
142 // - Expected: myword
143 // - Actual : mywordmyword
144 // i.e. the content of the selection was duplicated.
145 CPPUNIT_ASSERT_EQUAL(OUString("myword"), pTextNode->GetText());
149 CPPUNIT_PLUGIN_IMPLEMENT();
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */