| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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 |
*/ |
| 9 |
|
| 10 |
#include <swmodeltestbase.hxx> |
| 11 |
|
| 12 |
#include <com/sun/star/text/BibliographyDataType.hpp> |
| 13 |
#include <com/sun/star/text/XTextDocument.hpp> |
| 14 |
|
| 15 |
#include <rtl/ustrbuf.hxx> |
| 16 |
#include <comphelper/propertyvalue.hxx> |
| 17 |
|
| 18 |
#include <docsh.hxx> |
| 19 |
#include <wrtsh.hxx> |
| 20 |
#include <fldmgr.hxx> |
| 21 |
#include <authfld.hxx> |
| 22 |
#include <ndtxt.hxx> |
| 23 |
|
| 24 |
using namespace com::sun::star; |
| 25 |
|
| 26 |
namespace |
| 27 |
{ |
| 28 |
/// Covers sw/source/uibase/fldui/ fixes. |
| 29 |
class Test : public SwModelTestBase |
| 30 |
{ |
| 31 |
}; |
| 32 |
|
| 33 |
CPPUNIT_TEST_FIXTURE(Test, testBiblioPageNumberUpdate) |
| 34 |
{ |
| 35 |
// Given a document with 2 biblio fields, same properties, but different page number in the URL: |
| 36 |
createSwDoc(); |
| 37 |
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY); |
| 38 |
uno::Reference<beans::XPropertySet> xField( |
| 39 |
xFactory->createInstance(u"com.sun.star.text.TextField.Bibliography"_ustr), uno::UNO_QUERY); |
| 40 |
uno::Sequence<beans::PropertyValue> aFields = { |
| 41 |
comphelper::makePropertyValue(u"BibiliographicType"_ustr, text::BibliographyDataType::WWW), |
| 42 |
comphelper::makePropertyValue(u"Identifier"_ustr, u"AT"_ustr), |
| 43 |
comphelper::makePropertyValue(u"Author"_ustr, u"Author"_ustr), |
| 44 |
comphelper::makePropertyValue(u"Title"_ustr, u"Title"_ustr), |
| 45 |
comphelper::makePropertyValue(u"URL"_ustr, u"http://www.example.com/test.pdf#page=1"_ustr), |
| 46 |
}; |
| 47 |
xField->setPropertyValue(u"Fields"_ustr, uno::Any(aFields)); |
| 48 |
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); |
| 49 |
uno::Reference<text::XText> xText = xTextDocument->getText(); |
| 50 |
uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor(); |
| 51 |
uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY); |
| 52 |
xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); |
| 53 |
xField.set(xFactory->createInstance(u"com.sun.star.text.TextField.Bibliography"_ustr), |
| 54 |
uno::UNO_QUERY); |
| 55 |
aFields = { |
| 56 |
comphelper::makePropertyValue(u"BibiliographicType"_ustr, text::BibliographyDataType::WWW), |
| 57 |
comphelper::makePropertyValue(u"Identifier"_ustr, u"AT"_ustr), |
| 58 |
comphelper::makePropertyValue(u"Author"_ustr, u"Author"_ustr), |
| 59 |
comphelper::makePropertyValue(u"Title"_ustr, u"Title"_ustr), |
| 60 |
comphelper::makePropertyValue(u"URL"_ustr, u"http://www.example.com/test.pdf#page=2"_ustr), |
| 61 |
}; |
| 62 |
xField->setPropertyValue(u"Fields"_ustr, uno::Any(aFields)); |
| 63 |
xContent.set(xField, uno::UNO_QUERY); |
| 64 |
xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false); |
| 65 |
|
| 66 |
// When changing the page number in the second field's URL: |
| 67 |
SwDocShell* pDocShell = getSwDocShell(); |
| 68 |
SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); |
| 69 |
pWrtShell->SttEndDoc(/*bStt=*/false); |
| 70 |
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); |
| 71 |
OUString aCoreFields[AUTH_FIELD_END]; |
| 72 |
aCoreFields[AUTH_FIELD_AUTHORITY_TYPE] = OUString::number(text::BibliographyDataType::WWW); |
| 73 |
aCoreFields[AUTH_FIELD_IDENTIFIER] = "AT"; |
| 74 |
aCoreFields[AUTH_FIELD_AUTHOR] = "Author"; |
| 75 |
aCoreFields[AUTH_FIELD_TITLE] = "Title"; |
| 76 |
OUString aNewUrl = u"http://www.example.com/test.pdf#page=42"_ustr; |
| 77 |
aCoreFields[AUTH_FIELD_URL] = aNewUrl; |
| 78 |
OUStringBuffer aFieldBuffer; |
| 79 |
for (const auto& rField : aCoreFields) |
| 80 |
{ |
| 81 |
aFieldBuffer.append(rField + OUStringChar(TOX_STYLE_DELIMITER)); |
| 82 |
} |
| 83 |
SwFieldMgr aMgr(pWrtShell); |
| 84 |
aMgr.UpdateCurField(0, aFieldBuffer.makeStringAndClear(), OUString()); |
| 85 |
|
| 86 |
// Then make sure that the second field's URL is updated: |
| 87 |
auto pField = static_cast<SwAuthorityField*>(pWrtShell->GetCurField()); |
| 88 |
const SwAuthEntry* pEntry = pField->GetAuthEntry(); |
| 89 |
// Without the accompanying fix in place, this test would have failed with: |
| 90 |
// - Expected: http://www.example.com/test.pdf#page=42 |
| 91 |
// - Actual : http://www.example.com/test.pdf#page=2 |
| 92 |
// i.e. the second biblio field's URL was not updated. |
| 93 |
CPPUNIT_ASSERT_EQUAL(aNewUrl, pEntry->GetAuthorField(AUTH_FIELD_URL)); |
| 94 |
} |
| 95 |
|
| 96 |
CPPUNIT_TEST_FIXTURE(Test, testInsertRefmark) |
| 97 |
{ |
| 98 |
// Given an empty document: |
| 99 |
createSwDoc(); |
| 100 |
|
| 101 |
// When inserting a refmark with text: |
| 102 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 103 |
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)), |
| 104 |
comphelper::makePropertyValue(u"Name"_ustr, |
| 105 |
uno::Any(u"ZOTERO_ITEM CSL_CITATION {} RNDpyJknp173F"_ustr)), |
| 106 |
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"aaa<b>bbb</b>ccc"_ustr)), |
| 107 |
}; |
| 108 |
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs); |
| 109 |
|
| 110 |
// Then make sure that we create a refmark that covers that text: |
| 111 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 112 |
SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode(); |
| 113 |
std::vector<SwTextAttr*> aAttrs = pTextNode->GetTextAttrsAt(0, RES_TXTATR_REFMARK); |
| 114 |
// Without the accompanying fix in place, this test would have failed with: |
| 115 |
// - Expected: 1 |
| 116 |
// - Actual : 0 |
| 117 |
// i.e. no refmark was created, only the hard to read Type=12 created a refmark. |
| 118 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aAttrs.size()); |
| 119 |
CPPUNIT_ASSERT_EQUAL(u"aaabbbccc"_ustr, pTextNode->GetText()); |
| 120 |
} |
| 121 |
|
| 122 |
CPPUNIT_TEST_FIXTURE(Test, testTdf68364InsertConditionalFieldWithTwoDots) |
| 123 |
{ |
| 124 |
// Create an empty document |
| 125 |
createSwDoc(); |
| 126 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 127 |
|
| 128 |
// Insert a conditional field containing exactly two dots for its condition |
| 129 |
SwFieldMgr aFieldMgr(pWrtShell); |
| 130 |
SwInsertField_Data aFieldData(SwFieldTypesEnum::ConditionalText, 0, u"true"_ustr, |
| 131 |
u"19.12.2023"_ustr, 0); |
| 132 |
CPPUNIT_ASSERT(aFieldMgr.InsertField(aFieldData)); |
| 133 |
pWrtShell->SttEndDoc(true); |
| 134 |
|
| 135 |
// Without the accompanying fix in place, this test would have failed with: |
| 136 |
// - Expected: 19.12.2023 |
| 137 |
// - Actual : |
| 138 |
CPPUNIT_ASSERT_EQUAL(u"19.12.2023"_ustr, pWrtShell->GetCurField()->ExpandField(true, nullptr)); |
| 139 |
} |
| 140 |
|
| 141 |
CPPUNIT_TEST_FIXTURE(Test, testTdf170392FilenameFormatConstantsIdl) |
| 142 |
{ |
| 143 |
SwFileNameFormat aFormats[] = { SwFileNameFormat::Name, SwFileNameFormat::Path }; |
| 144 |
for (SwFileNameFormat eFormat : aFormats) |
| 145 |
{ |
| 146 |
// Create an empty document |
| 147 |
createSwDoc(); |
| 148 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 149 |
|
| 150 |
// Insert a filename field containing a specific format |
| 151 |
SwFieldMgr aFieldMgr(pWrtShell); |
| 152 |
SwInsertField_Data aFieldData(SwFieldTypesEnum::Filename, 0, u""_ustr, u""_ustr, |
| 153 |
static_cast<sal_uInt32>(eFormat)); |
| 154 |
CPPUNIT_ASSERT(aFieldMgr.InsertField(aFieldData)); |
| 155 |
|
| 156 |
// First reload in order to get a value for the filename field |
| 157 |
saveAndReload(TestFilter::ODT); |
| 158 |
pWrtShell = getSwDocShell()->GetWrtShell(); |
| 159 |
pWrtShell->SttEndDoc(true); |
| 160 |
const auto aExpandedField = pWrtShell->GetCurField()->ExpandField(true, nullptr); |
| 161 |
|
| 162 |
// Second reload in order to check consistency for the filename field |
| 163 |
saveAndReload(TestFilter::ODT); |
| 164 |
pWrtShell = getSwDocShell()->GetWrtShell(); |
| 165 |
pWrtShell->SttEndDoc(true); |
| 166 |
|
| 167 |
// Without the accompanying fix in place, this test would have failed with: |
| 168 |
// - Expected: filename field is the same |
| 169 |
// - Actual : filename field differ since the filename format constants differ |
| 170 |
CPPUNIT_ASSERT_EQUAL(aExpandedField, pWrtShell->GetCurField()->ExpandField(true, nullptr)); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
CPPUNIT_TEST_FIXTURE(Test, testInsertRefmarkSelection) |
| 175 |
{ |
| 176 |
// Given a document with a single selected word: |
| 177 |
createSwDoc(); |
| 178 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 179 |
pWrtShell->Insert2(u"myword"_ustr); |
| 180 |
pWrtShell->SelAll(); |
| 181 |
|
| 182 |
// When inserting a refmark: |
| 183 |
SwFieldMgr aMgr(pWrtShell); |
| 184 |
SwInsertField_Data aData(SwFieldTypesEnum::SetRef, /*nSubType=*/0, u"myname"_ustr, |
| 185 |
u"myword"_ustr, |
| 186 |
/*nFormatId=*/0); |
| 187 |
aMgr.InsertField(aData); |
| 188 |
|
| 189 |
// Then make sure the document still just contains that word only once: |
| 190 |
SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode(); |
| 191 |
// Without the accompanying fix in place, this test would have failed with: |
| 192 |
// - Expected: myword |
| 193 |
// - Actual : mywordmyword |
| 194 |
// i.e. the content of the selection was duplicated. |
| 195 |
CPPUNIT_ASSERT_EQUAL(u"myword"_ustr, pTextNode->GetText()); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 200 |
|
| 201 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |