| 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 <boost/property_tree/json_parser.hpp> |
| 13 |
|
| 14 |
#include <com/sun/star/frame/XModel2.hpp> |
| 15 |
#include <com/sun/star/text/XTextViewTextRangeSupplier.hpp> |
| 16 |
#include <com/sun/star/util/XCloseable.hpp> |
| 17 |
#include <com/sun/star/text/XTextDocument.hpp> |
| 18 |
#include <com/sun/star/beans/PropertyAttribute.hpp> |
| 19 |
|
| 20 |
#include <vcl/scheduler.hxx> |
| 21 |
#include <tools/json_writer.hxx> |
| 22 |
#include <comphelper/propertyvalue.hxx> |
| 23 |
#include <xmloff/odffields.hxx> |
| 24 |
|
| 25 |
#include <docsh.hxx> |
| 26 |
#include <edtwin.hxx> |
| 27 |
#include <unotextrange.hxx> |
| 28 |
#include <view.hxx> |
| 29 |
#include <wrtsh.hxx> |
| 30 |
#include <rootfrm.hxx> |
| 31 |
#include <sortedobjs.hxx> |
| 32 |
#include <anchoredobject.hxx> |
| 33 |
#include <frameformats.hxx> |
| 34 |
#include <fmtanchr.hxx> |
| 35 |
#include <unotxdoc.hxx> |
| 36 |
|
| 37 |
/// Covers sw/source/uibase/uno/ fixes. |
| 38 |
class SwUibaseUnoTest : public SwModelTestBase |
| 39 |
{ |
| 40 |
public: |
| 41 |
SwUibaseUnoTest() |
| 42 |
: SwModelTestBase(u"/sw/qa/uibase/uno/data/"_ustr) |
| 43 |
{ |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testLockControllers) |
| 48 |
{ |
| 49 |
createSwDoc(); |
| 50 |
{ |
| 51 |
uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY_THROW); |
| 52 |
xModel->lockControllers(); |
| 53 |
} |
| 54 |
{ |
| 55 |
uno::Reference<util::XCloseable> xCloseable(mxComponent, uno::UNO_QUERY_THROW); |
| 56 |
xCloseable->close(false); |
| 57 |
} |
| 58 |
// Without the accompanying fix in place, this test would have crashed. |
| 59 |
mxComponent.clear(); |
| 60 |
} |
| 61 |
|
| 62 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCondFieldCachedValue) |
| 63 |
{ |
| 64 |
createSwDoc("cond-field-cached-value.docx"); |
| 65 |
Scheduler::ProcessEventsToIdle(); |
| 66 |
|
| 67 |
// Without the accompanying fix in place, this test would have failed with: |
| 68 |
// - Expected: 1 |
| 69 |
// - Actual : |
| 70 |
// i.e. the conditional field lost its cached content. |
| 71 |
getParagraph(2, u"1"_ustr); |
| 72 |
} |
| 73 |
|
| 74 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCreateTextRangeByPixelPosition) |
| 75 |
{ |
| 76 |
// Given a document with 2 characters, and the pixel position of the point between them: |
| 77 |
createSwDoc(); |
| 78 |
SwDoc* pDoc = getSwDoc(); |
| 79 |
SwDocShell* pDocShell = getSwDocShell(); |
| 80 |
SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); |
| 81 |
pWrtShell->Insert2(u"AZ"_ustr); |
| 82 |
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); |
| 83 |
Point aLogic = pWrtShell->GetCharRect().Center(); |
| 84 |
SwView* pView = pDocShell->GetView(); |
| 85 |
SwEditWin& rEditWin = pView->GetEditWin(); |
| 86 |
Point aPixel = rEditWin.LogicToPixel(aLogic); |
| 87 |
|
| 88 |
// When converting that pixel position to a document model position (text range): |
| 89 |
uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY); |
| 90 |
uno::Reference<container::XEnumeration> xControllers = xModel->getControllers(); |
| 91 |
uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(), |
| 92 |
uno::UNO_QUERY); |
| 93 |
awt::Point aPoint(aPixel.getX(), aPixel.getY()); |
| 94 |
uno::Reference<text::XTextRange> xTextRange |
| 95 |
= xController->createTextRangeByPixelPosition(aPoint); |
| 96 |
|
| 97 |
// Then make sure that text range points after the first character: |
| 98 |
auto pTextRange = dynamic_cast<SwXTextRange*>(xTextRange.get()); |
| 99 |
SwPaM aPaM(pDoc->GetNodes()); |
| 100 |
pTextRange->GetPositions(aPaM); |
| 101 |
sal_Int32 nActual = aPaM.GetPoint()->GetContentIndex(); |
| 102 |
// Without the needed PixelToLogic() call in place, this test would have failed with: |
| 103 |
// - Expected: 1 |
| 104 |
// - Actual : 0 |
| 105 |
// i.e. the returned text range pointed before the first character, not between the first and |
| 106 |
// the second character. |
| 107 |
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nActual); |
| 108 |
} |
| 109 |
|
| 110 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCreateTextRangeByPixelPositionGraphic) |
| 111 |
{ |
| 112 |
// Given a document with an as-char image and the center of that image in pixels: |
| 113 |
createSwDoc(); |
| 114 |
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY); |
| 115 |
uno::Reference<beans::XPropertySet> xTextGraphic( |
| 116 |
xFactory->createInstance(u"com.sun.star.text.TextGraphicObject"_ustr), uno::UNO_QUERY); |
| 117 |
xTextGraphic->setPropertyValue(u"AnchorType"_ustr, |
| 118 |
uno::Any(text::TextContentAnchorType_AS_CHARACTER)); |
| 119 |
xTextGraphic->setPropertyValue(u"Width"_ustr, uno::Any(static_cast<sal_Int32>(10000))); |
| 120 |
xTextGraphic->setPropertyValue(u"Height"_ustr, uno::Any(static_cast<sal_Int32>(10000))); |
| 121 |
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); |
| 122 |
uno::Reference<text::XText> xBodyText = xTextDocument->getText(); |
| 123 |
uno::Reference<text::XTextCursor> xCursor(xBodyText->createTextCursor()); |
| 124 |
uno::Reference<text::XTextContent> xTextContent(xTextGraphic, uno::UNO_QUERY); |
| 125 |
xBodyText->insertTextContent(xCursor, xTextContent, false); |
| 126 |
SwDoc* pDoc = getSwDoc(); |
| 127 |
SwDocShell* pDocShell = getSwDocShell(); |
| 128 |
SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); |
| 129 |
SwRootFrame* pLayout = pWrtShell->GetLayout(); |
| 130 |
SwFrame* pPage = pLayout->GetLower(); |
| 131 |
SwFrame* pBody = pPage->GetLower(); |
| 132 |
SwFrame* pText = pBody->GetLower(); |
| 133 |
SwSortedObjs& rDrawObjs = *pText->GetDrawObjs(); |
| 134 |
SwAnchoredObject* pAnchored = rDrawObjs[0]; |
| 135 |
Point aLogic = pAnchored->GetObjRect().Center(); |
| 136 |
SwView* pView = pDocShell->GetView(); |
| 137 |
SwEditWin& rEditWin = pView->GetEditWin(); |
| 138 |
Point aPixel = rEditWin.LogicToPixel(aLogic); |
| 139 |
|
| 140 |
// When converting that pixel position to a document model position (text range): |
| 141 |
uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY); |
| 142 |
uno::Reference<container::XEnumeration> xControllers = xModel->getControllers(); |
| 143 |
uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(), |
| 144 |
uno::UNO_QUERY); |
| 145 |
awt::Point aPoint(aPixel.getX(), aPixel.getY()); |
| 146 |
// Without the accompanying fix in place, this test would have crashed, because an XTextRange |
| 147 |
// can't point to a graphic node. |
| 148 |
uno::Reference<text::XTextRange> xTextRange |
| 149 |
= xController->createTextRangeByPixelPosition(aPoint); |
| 150 |
|
| 151 |
// Then make sure that the anchor of the image is returned: |
| 152 |
const auto& rFormats = *pDoc->GetSpzFrameFormats(); |
| 153 |
const auto pFormat = rFormats[0]; |
| 154 |
SwPosition aAnchorPos(*pFormat->GetAnchor().GetContentAnchor()); |
| 155 |
auto pTextRange = dynamic_cast<SwXTextRange*>(xTextRange.get()); |
| 156 |
SwPaM aPaM(pDoc->GetNodes()); |
| 157 |
pTextRange->GetPositions(aPaM); |
| 158 |
CPPUNIT_ASSERT_EQUAL(aAnchorPos, *aPaM.GetPoint()); |
| 159 |
} |
| 160 |
|
| 161 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCreateTextRangeByPixelPositionAtPageGraphic) |
| 162 |
{ |
| 163 |
// Given a document with an at-page anchored image: |
| 164 |
createSwDoc(); |
| 165 |
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY); |
| 166 |
uno::Reference<beans::XPropertySet> xTextGraphic( |
| 167 |
xFactory->createInstance(u"com.sun.star.text.TextGraphicObject"_ustr), uno::UNO_QUERY); |
| 168 |
xTextGraphic->setPropertyValue(u"AnchorType"_ustr, |
| 169 |
uno::Any(text::TextContentAnchorType_AT_PAGE)); |
| 170 |
xTextGraphic->setPropertyValue(u"AnchorPageNo"_ustr, uno::Any(static_cast<sal_Int16>(1))); |
| 171 |
xTextGraphic->setPropertyValue(u"Width"_ustr, uno::Any(static_cast<sal_Int32>(10000))); |
| 172 |
xTextGraphic->setPropertyValue(u"Height"_ustr, uno::Any(static_cast<sal_Int32>(10000))); |
| 173 |
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY); |
| 174 |
uno::Reference<text::XText> xBodyText = xTextDocument->getText(); |
| 175 |
uno::Reference<text::XTextCursor> xCursor(xBodyText->createTextCursor()); |
| 176 |
uno::Reference<text::XTextContent> xTextContent(xTextGraphic, uno::UNO_QUERY); |
| 177 |
xBodyText->insertTextContent(xCursor, xTextContent, false); |
| 178 |
SwDocShell* pDocShell = getSwDocShell(); |
| 179 |
SwWrtShell* pWrtShell = pDocShell->GetWrtShell(); |
| 180 |
SwRootFrame* pLayout = pWrtShell->GetLayout(); |
| 181 |
SwFrame* pPage = pLayout->GetLower(); |
| 182 |
SwSortedObjs& rDrawObjs = *pPage->GetDrawObjs(); |
| 183 |
SwAnchoredObject* pAnchored = rDrawObjs[0]; |
| 184 |
Point aLogic = pAnchored->GetObjRect().Center(); |
| 185 |
SwView* pView = pDocShell->GetView(); |
| 186 |
SwEditWin& rEditWin = pView->GetEditWin(); |
| 187 |
Point aPixel = rEditWin.LogicToPixel(aLogic); |
| 188 |
|
| 189 |
// When asking for the doc model pos of the image's anchor by pixel position: |
| 190 |
uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY); |
| 191 |
uno::Reference<container::XEnumeration> xControllers = xModel->getControllers(); |
| 192 |
uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(), |
| 193 |
uno::UNO_QUERY); |
| 194 |
awt::Point aPoint(aPixel.getX(), aPixel.getY()); |
| 195 |
// Without the accompanying fix in place, this test would have crashed. |
| 196 |
uno::Reference<text::XTextRange> xTextRange |
| 197 |
= xController->createTextRangeByPixelPosition(aPoint); |
| 198 |
|
| 199 |
// Then make sure that the result is empty, since the image is at-page anchored: |
| 200 |
CPPUNIT_ASSERT(!xTextRange.is()); |
| 201 |
} |
| 202 |
|
| 203 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetTextFormFields) |
| 204 |
{ |
| 205 |
// Given a document with 3 fieldmarks: 2 zotero items and a zotero |
| 206 |
// bibliography: |
| 207 |
createSwDoc(); |
| 208 |
for (int i = 0; i < 2; ++i) |
| 209 |
{ |
| 210 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 211 |
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)), |
| 212 |
comphelper::makePropertyValue(u"FieldCommand"_ustr, |
| 213 |
uno::Any(u"ADDIN ZOTERO_ITEM foo bar"_ustr)), |
| 214 |
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result"_ustr)), |
| 215 |
}; |
| 216 |
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs); |
| 217 |
} |
| 218 |
{ |
| 219 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 220 |
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)), |
| 221 |
comphelper::makePropertyValue(u"FieldCommand"_ustr, |
| 222 |
uno::Any(u"ADDIN ZOTERO_BIBL foo bar"_ustr)), |
| 223 |
comphelper::makePropertyValue(u"FieldResult"_ustr, |
| 224 |
uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)), |
| 225 |
}; |
| 226 |
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs); |
| 227 |
} |
| 228 |
|
| 229 |
// When getting the zotero items: |
| 230 |
tools::JsonWriter aJsonWriter; |
| 231 |
std::string_view aCommand(".uno:TextFormFields?type=vnd.oasis.opendocument.field.UNHANDLED&" |
| 232 |
"commandPrefix=ADDIN%20ZOTERO_ITEM"); |
| 233 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 234 |
|
| 235 |
// Then make sure we find the 2 items and ignore the bibliography: |
| 236 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 237 |
std::stringstream aStream((std::string(pJSON))); |
| 238 |
boost::property_tree::ptree aTree; |
| 239 |
boost::property_tree::read_json(aStream, aTree); |
| 240 |
// Without the accompanying fix in place, this test would have failed with: |
| 241 |
// - No such node (fields) |
| 242 |
// i.e. the returned JSON was just empty. |
| 243 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aTree.get_child("fields").count("")); |
| 244 |
} |
| 245 |
|
| 246 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetDocumentProperties) |
| 247 |
{ |
| 248 |
// Given a document with 3 custom properties: 2 Zotero ones and one other: |
| 249 |
createSwDoc(); |
| 250 |
SwDocShell* pDocShell = getSwDocShell(); |
| 251 |
uno::Reference<document::XDocumentPropertiesSupplier> xDPS(pDocShell->GetModel(), |
| 252 |
uno::UNO_QUERY); |
| 253 |
uno::Reference<document::XDocumentProperties> xDP = xDPS->getDocumentProperties(); |
| 254 |
uno::Reference<beans::XPropertyContainer> xUDP = xDP->getUserDefinedProperties(); |
| 255 |
xUDP->addProperty(u"ZOTERO_PREF_1"_ustr, beans::PropertyAttribute::REMOVABLE, |
| 256 |
uno::Any(u"foo"_ustr)); |
| 257 |
xUDP->addProperty(u"ZOTERO_PREF_2"_ustr, beans::PropertyAttribute::REMOVABLE, |
| 258 |
uno::Any(u"bar"_ustr)); |
| 259 |
xUDP->addProperty(u"OTHER"_ustr, beans::PropertyAttribute::REMOVABLE, uno::Any(u"baz"_ustr)); |
| 260 |
|
| 261 |
// When getting the zotero properties: |
| 262 |
tools::JsonWriter aJsonWriter; |
| 263 |
std::string_view aCommand(".uno:SetDocumentProperties?namePrefix=ZOTERO_PREF_"); |
| 264 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 265 |
|
| 266 |
// Then make sure we find the 2 properties and ignore the other one: |
| 267 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 268 |
std::stringstream aStream((std::string(pJSON))); |
| 269 |
boost::property_tree::ptree aTree; |
| 270 |
boost::property_tree::read_json(aStream, aTree); |
| 271 |
// Without the accompanying fix in place, this test would have failed with: |
| 272 |
// - No such node (userDefinedProperties) |
| 273 |
// i.e. the returned JSON was just empty. |
| 274 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), |
| 275 |
aTree.get_child("userDefinedProperties").count("")); |
| 276 |
} |
| 277 |
|
| 278 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetBookmarks) |
| 279 |
{ |
| 280 |
// Given a document with 3 bookmarks: 2 zotero references and a zotero bibliography: |
| 281 |
createSwDoc(); |
| 282 |
{ |
| 283 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 284 |
comphelper::makePropertyValue(u"Bookmark"_ustr, uno::Any(u"ZOTERO_BREF_1"_ustr)), |
| 285 |
}; |
| 286 |
dispatchCommand(mxComponent, u".uno:InsertBookmark"_ustr, aArgs); |
| 287 |
} |
| 288 |
{ |
| 289 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 290 |
comphelper::makePropertyValue(u"Bookmark"_ustr, uno::Any(u"ZOTERO_BREF_2"_ustr)), |
| 291 |
}; |
| 292 |
dispatchCommand(mxComponent, u".uno:InsertBookmark"_ustr, aArgs); |
| 293 |
} |
| 294 |
{ |
| 295 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 296 |
comphelper::makePropertyValue(u"Bookmark"_ustr, uno::Any(u"ZOTERO_BIBL"_ustr)), |
| 297 |
}; |
| 298 |
dispatchCommand(mxComponent, u".uno:InsertBookmark"_ustr, aArgs); |
| 299 |
} |
| 300 |
|
| 301 |
// When getting the reference bookmarks: |
| 302 |
tools::JsonWriter aJsonWriter; |
| 303 |
std::string_view aCommand(".uno:Bookmarks?namePrefix=ZOTERO_BREF_"); |
| 304 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 305 |
|
| 306 |
// Then make sure we get the 2 references but not the bibliography: |
| 307 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 308 |
std::stringstream aStream((std::string(pJSON))); |
| 309 |
boost::property_tree::ptree aTree; |
| 310 |
boost::property_tree::read_json(aStream, aTree); |
| 311 |
// Without the accompanying fix in place, this test would have failed with: |
| 312 |
// - No such node (bookmarks) |
| 313 |
// i.e. the returned JSON was just empty. |
| 314 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aTree.get_child("bookmarks").count("")); |
| 315 |
} |
| 316 |
|
| 317 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetFields) |
| 318 |
{ |
| 319 |
// Given a document with a refmark: |
| 320 |
createSwDoc(); |
| 321 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 322 |
OUString aName(u"ZOTERO_ITEM CSL_CITATION {} "_ustr); |
| 323 |
for (int i = 0; i < 5; ++i) |
| 324 |
{ |
| 325 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 326 |
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)), |
| 327 |
comphelper::makePropertyValue(u"Name"_ustr, uno::Any(aName + OUString::number(i + 1))), |
| 328 |
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"mycontent"_ustr)), |
| 329 |
}; |
| 330 |
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs); |
| 331 |
pWrtShell->SttEndDoc(/*bStt=*/false); |
| 332 |
pWrtShell->SplitNode(); |
| 333 |
pWrtShell->SttEndDoc(/*bStt=*/false); |
| 334 |
} |
| 335 |
|
| 336 |
// When getting the refmarks: |
| 337 |
tools::JsonWriter aJsonWriter; |
| 338 |
std::string_view aCommand(".uno:Fields?typeName=SetRef&namePrefix=ZOTERO_ITEM%20CSL_CITATION"); |
| 339 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 340 |
|
| 341 |
// Then make sure we get the 1 refmark: |
| 342 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 343 |
std::stringstream aStream((std::string(pJSON))); |
| 344 |
boost::property_tree::ptree aTree; |
| 345 |
boost::property_tree::read_json(aStream, aTree); |
| 346 |
// Without the accompanying fix in place, this test would have failed with: |
| 347 |
// - No such node (setRefs) |
| 348 |
// i.e. the returned JSON was just empty. |
| 349 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(5), aTree.get_child("setRefs").count("")); |
| 350 |
auto it = aTree.get_child("setRefs").begin(); |
| 351 |
boost::property_tree::ptree aRef = (it++)->second; |
| 352 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} 1"), |
| 353 |
aRef.get<std::string>("name")); |
| 354 |
aRef = (it++)->second; |
| 355 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} 2"), |
| 356 |
aRef.get<std::string>("name")); |
| 357 |
aRef = (it++)->second; |
| 358 |
// Without the accompanying fix in place, this test would have failed with: |
| 359 |
// - Expected: ZOTERO_ITEM CSL_CITATION {} 3 |
| 360 |
// - Actual : ZOTERO_ITEM CSL_CITATION {} 4 |
| 361 |
// i.e. the output was unsorted. |
| 362 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} 3"), |
| 363 |
aRef.get<std::string>("name")); |
| 364 |
aRef = (it++)->second; |
| 365 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} 4"), |
| 366 |
aRef.get<std::string>("name")); |
| 367 |
aRef = (it++)->second; |
| 368 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} 5"), |
| 369 |
aRef.get<std::string>("name")); |
| 370 |
} |
| 371 |
|
| 372 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetLayout) |
| 373 |
{ |
| 374 |
// Given a document with 2 pages: |
| 375 |
createSwDoc(); |
| 376 |
SwDoc* pDoc = getSwDoc(); |
| 377 |
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); |
| 378 |
pWrtShell->InsertPageBreak(); |
| 379 |
|
| 380 |
// When getting info about the layout: |
| 381 |
tools::JsonWriter aJsonWriter; |
| 382 |
std::string_view aCommand(".uno:Layout"); |
| 383 |
auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get()); |
| 384 |
pXTextDocument->getCommandValues(aJsonWriter, aCommand); |
| 385 |
|
| 386 |
// Then make sure we get the 2 pages: |
| 387 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 388 |
std::stringstream aStream((std::string(pJSON))); |
| 389 |
boost::property_tree::ptree aTree; |
| 390 |
boost::property_tree::read_json(aStream, aTree); |
| 391 |
auto aPages = aTree.get_child("commandValues").get_child("pages"); |
| 392 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aPages.count("")); |
| 393 |
for (const auto& rPage : aPages) |
| 394 |
{ |
| 395 |
CPPUNIT_ASSERT(!rPage.second.get<bool>("isInvalidContent")); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetTextFormField) |
| 400 |
{ |
| 401 |
// Given a document with a fieldmark: |
| 402 |
createSwDoc(); |
| 403 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 404 |
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)), |
| 405 |
comphelper::makePropertyValue(u"FieldCommand"_ustr, |
| 406 |
uno::Any(u"ADDIN ZOTERO_ITEM foo bar"_ustr)), |
| 407 |
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result"_ustr)), |
| 408 |
}; |
| 409 |
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs); |
| 410 |
|
| 411 |
// When stepping into the fieldmark with the cursor and getting the command value for |
| 412 |
// uno:TextFormField: |
| 413 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 414 |
pWrtShell->SttEndDoc(/*bStt=*/false); |
| 415 |
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); |
| 416 |
tools::JsonWriter aJsonWriter; |
| 417 |
std::string_view aCommand(".uno:TextFormField?type=vnd.oasis.opendocument.field.UNHANDLED&" |
| 418 |
"commandPrefix=ADDIN%20ZOTERO_ITEM"); |
| 419 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 420 |
|
| 421 |
// Then make sure we find the inserted fieldmark: |
| 422 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 423 |
std::stringstream aStream((std::string(pJSON))); |
| 424 |
boost::property_tree::ptree aTree; |
| 425 |
boost::property_tree::read_json(aStream, aTree); |
| 426 |
// Without the accompanying fix in place, this test would have failed with: |
| 427 |
// - No such node (type) |
| 428 |
// i.e. the returned JSON was just an empty object. |
| 429 |
auto field = aTree.get_child("field"); |
| 430 |
CPPUNIT_ASSERT_EQUAL(std::string("vnd.oasis.opendocument.field.UNHANDLED"), |
| 431 |
field.get<std::string>("type")); |
| 432 |
CPPUNIT_ASSERT_EQUAL(std::string("ADDIN ZOTERO_ITEM foo bar"), |
| 433 |
field.get<std::string>("command")); |
| 434 |
} |
| 435 |
|
| 436 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetSections) |
| 437 |
{ |
| 438 |
// Given a document with a section: |
| 439 |
createSwDoc(); |
| 440 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 441 |
comphelper::makePropertyValue( |
| 442 |
u"RegionName"_ustr, uno::Any(u"ZOTERO_BIBL {} CSL_BIBLIOGRAPHY RNDRfiit6mXBc"_ustr)), |
| 443 |
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)), |
| 444 |
}; |
| 445 |
dispatchCommand(mxComponent, u".uno:InsertSection"_ustr, aArgs); |
| 446 |
|
| 447 |
// When asking for a list of section names: |
| 448 |
tools::JsonWriter aJsonWriter; |
| 449 |
std::string_view aCommand(".uno:Sections?namePrefix=ZOTERO_BIBL"); |
| 450 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 451 |
|
| 452 |
// Make sure we find our just inserted section: |
| 453 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 454 |
std::stringstream aStream((std::string(pJSON))); |
| 455 |
boost::property_tree::ptree aTree; |
| 456 |
boost::property_tree::read_json(aStream, aTree); |
| 457 |
// Without the accompanying fix in place, this test would have failed with: |
| 458 |
// - No such node (sections) |
| 459 |
// i.e. the returned JSON was an empty object. |
| 460 |
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), aTree.get_child("sections").count("")); |
| 461 |
} |
| 462 |
|
| 463 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetBookmark) |
| 464 |
{ |
| 465 |
// Given a document with a bookmark: |
| 466 |
createSwDoc(); |
| 467 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 468 |
comphelper::makePropertyValue(u"Bookmark"_ustr, uno::Any(u"ZOTERO_BREF_1"_ustr)), |
| 469 |
comphelper::makePropertyValue(u"BookmarkText"_ustr, uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)), |
| 470 |
}; |
| 471 |
dispatchCommand(mxComponent, u".uno:InsertBookmark"_ustr, aArgs); |
| 472 |
|
| 473 |
// When stepping into the bookmark with the cursor and getting the command value for |
| 474 |
// .uno:Bookmark: |
| 475 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 476 |
pWrtShell->SttEndDoc(/*bStt=*/false); |
| 477 |
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); |
| 478 |
tools::JsonWriter aJsonWriter; |
| 479 |
std::string_view aCommand(".uno:Bookmark?namePrefix=ZOTERO_BREF_"); |
| 480 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 481 |
|
| 482 |
// Then make sure we find the inserted bookmark: |
| 483 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 484 |
std::stringstream aStream((std::string(pJSON))); |
| 485 |
boost::property_tree::ptree aTree; |
| 486 |
boost::property_tree::read_json(aStream, aTree); |
| 487 |
boost::property_tree::ptree aBookmark = aTree.get_child("bookmark"); |
| 488 |
// Without the accompanying fix in place, this test would have failed with: |
| 489 |
// - No such node (bookmark) |
| 490 |
// i.e. the returned JSON was an empty object. |
| 491 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_BREF_1"), aBookmark.get<std::string>("name")); |
| 492 |
} |
| 493 |
|
| 494 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetField) |
| 495 |
{ |
| 496 |
// Given a document with a refmark: |
| 497 |
createSwDoc(); |
| 498 |
uno::Sequence<css::beans::PropertyValue> aArgs = { |
| 499 |
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)), |
| 500 |
comphelper::makePropertyValue(u"Name"_ustr, |
| 501 |
uno::Any(u"ZOTERO_ITEM CSL_CITATION {} refmark"_ustr)), |
| 502 |
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"content"_ustr)), |
| 503 |
}; |
| 504 |
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs); |
| 505 |
|
| 506 |
// When in the refmark with the cursor and getting the command value for .uno:Field: |
| 507 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 508 |
pWrtShell->SttEndDoc(/*bStt=*/false); |
| 509 |
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false); |
| 510 |
tools::JsonWriter aJsonWriter; |
| 511 |
std::string_view aCommand(".uno:Field?typeName=SetRef&namePrefix=ZOTERO_ITEM%20CSL_CITATION"); |
| 512 |
getSwTextDoc()->getCommandValues(aJsonWriter, aCommand); |
| 513 |
|
| 514 |
// Then make sure we find the inserted refmark: |
| 515 |
OString pJSON(aJsonWriter.finishAndGetAsOString()); |
| 516 |
std::stringstream aStream((std::string(pJSON))); |
| 517 |
boost::property_tree::ptree aTree; |
| 518 |
boost::property_tree::read_json(aStream, aTree); |
| 519 |
boost::property_tree::ptree aBookmark = aTree.get_child("setRef"); |
| 520 |
// Without the accompanying fix in place, this test would have failed with: |
| 521 |
// - No such node (setRef) |
| 522 |
// i.e. the returned JSON was an empty object. |
| 523 |
CPPUNIT_ASSERT_EQUAL(std::string("ZOTERO_ITEM CSL_CITATION {} refmark"), |
| 524 |
aBookmark.get<std::string>("name")); |
| 525 |
} |
| 526 |
|
| 527 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testDoNotBreakWrappedTables) |
| 528 |
{ |
| 529 |
// Given an empty document: |
| 530 |
createSwDoc(); |
| 531 |
|
| 532 |
// When checking the state of the DoNotBreakWrappedTables compat flag: |
| 533 |
uno::Reference<lang::XMultiServiceFactory> xDocument(mxComponent, uno::UNO_QUERY); |
| 534 |
uno::Reference<beans::XPropertySet> xSettings( |
| 535 |
xDocument->createInstance(u"com.sun.star.document.Settings"_ustr), uno::UNO_QUERY); |
| 536 |
bool bDoNotBreakWrappedTables{}; |
| 537 |
// Without the accompanying fix in place, this test would have failed with: |
| 538 |
// An uncaught exception of type com.sun.star.beans.UnknownPropertyException |
| 539 |
// i.e. the compat flag was not recognized. |
| 540 |
xSettings->getPropertyValue(u"DoNotBreakWrappedTables"_ustr) >>= bDoNotBreakWrappedTables; |
| 541 |
// Then make sure it's false by default: |
| 542 |
CPPUNIT_ASSERT(!bDoNotBreakWrappedTables); |
| 543 |
|
| 544 |
// And when setting DoNotBreakWrappedTables=true: |
| 545 |
xSettings->setPropertyValue(u"DoNotBreakWrappedTables"_ustr, uno::Any(true)); |
| 546 |
// Then make sure it gets enabled: |
| 547 |
xSettings->getPropertyValue(u"DoNotBreakWrappedTables"_ustr) >>= bDoNotBreakWrappedTables; |
| 548 |
CPPUNIT_ASSERT(bDoNotBreakWrappedTables); |
| 549 |
} |
| 550 |
|
| 551 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testAllowTextAfterFloatingTableBreak) |
| 552 |
{ |
| 553 |
// Given an empty document: |
| 554 |
createSwDoc(); |
| 555 |
|
| 556 |
// When checking the state of the AllowTextAfterFloatingTableBreak compat flag: |
| 557 |
uno::Reference<lang::XMultiServiceFactory> xDocument(mxComponent, uno::UNO_QUERY); |
| 558 |
uno::Reference<beans::XPropertySet> xSettings( |
| 559 |
xDocument->createInstance(u"com.sun.star.document.Settings"_ustr), uno::UNO_QUERY); |
| 560 |
bool bAllowTextAfterFloatingTableBreak{}; |
| 561 |
// Without the accompanying fix in place, this test would have failed with: |
| 562 |
// An uncaught exception of type com.sun.star.beans.UnknownPropertyException |
| 563 |
// i.e. the compat flag was not recognized. |
| 564 |
xSettings->getPropertyValue(u"AllowTextAfterFloatingTableBreak"_ustr) |
| 565 |
>>= bAllowTextAfterFloatingTableBreak; |
| 566 |
// Then make sure it's false by default: |
| 567 |
CPPUNIT_ASSERT(!bAllowTextAfterFloatingTableBreak); |
| 568 |
|
| 569 |
// And when setting AllowTextAfterFloatingTableBreak=true: |
| 570 |
xSettings->setPropertyValue(u"AllowTextAfterFloatingTableBreak"_ustr, uno::Any(true)); |
| 571 |
// Then make sure it gets enabled: |
| 572 |
xSettings->getPropertyValue(u"AllowTextAfterFloatingTableBreak"_ustr) |
| 573 |
>>= bAllowTextAfterFloatingTableBreak; |
| 574 |
CPPUNIT_ASSERT(bAllowTextAfterFloatingTableBreak); |
| 575 |
} |
| 576 |
|
| 577 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testDoNotMirrorRtlDrawObjs) |
| 578 |
{ |
| 579 |
// Given an empty document: |
| 580 |
createSwDoc(); |
| 581 |
|
| 582 |
// When checking the state of the DoNotMirrorRtlDrawObjs compat flag: |
| 583 |
uno::Reference<lang::XMultiServiceFactory> xDocument(mxComponent, uno::UNO_QUERY); |
| 584 |
uno::Reference<beans::XPropertySet> xSettings( |
| 585 |
xDocument->createInstance(u"com.sun.star.document.Settings"_ustr), uno::UNO_QUERY); |
| 586 |
bool bDoNotMirrorRtlDrawObjs{}; |
| 587 |
// Without the accompanying fix in place, this test would have failed with: |
| 588 |
// An uncaught exception of type com.sun.star.beans.UnknownPropertyException |
| 589 |
// i.e. the compat flag was not recognized. |
| 590 |
xSettings->getPropertyValue(u"DoNotMirrorRtlDrawObjs"_ustr) >>= bDoNotMirrorRtlDrawObjs; |
| 591 |
// Then make sure it's false by default: |
| 592 |
CPPUNIT_ASSERT(!bDoNotMirrorRtlDrawObjs); |
| 593 |
|
| 594 |
// And when setting DoNotMirrorRtlDrawObjs=true: |
| 595 |
xSettings->setPropertyValue(u"DoNotMirrorRtlDrawObjs"_ustr, uno::Any(true)); |
| 596 |
// Then make sure it gets enabled: |
| 597 |
xSettings->getPropertyValue(u"DoNotMirrorRtlDrawObjs"_ustr) >>= bDoNotMirrorRtlDrawObjs; |
| 598 |
CPPUNIT_ASSERT(bDoNotMirrorRtlDrawObjs); |
| 599 |
} |
| 600 |
|
| 601 |
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testRedlineRenderModePartInfo) |
| 602 |
{ |
| 603 |
// Given a document with redline render mode set to "omit deletes": |
| 604 |
createSwDoc(); |
| 605 |
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell(); |
| 606 |
SwViewOption aOpt(*pWrtShell->GetViewOptions()); |
| 607 |
aOpt.SetRedlineRenderMode(SwRedlineRenderMode::OmitDeletes); |
| 608 |
pWrtShell->ApplyViewOptions(aOpt); |
| 609 |
|
| 610 |
// When getting the LOK part info: |
| 611 |
OUString aPartInfo = getSwTextDoc()->getPartInfo(0); |
| 612 |
|
| 613 |
// Then make sure we get the correct value: |
| 614 |
std::stringstream aStream((std::string(aPartInfo.toUtf8()))); |
| 615 |
boost::property_tree::ptree aTree; |
| 616 |
boost::property_tree::read_json(aStream, aTree); |
| 617 |
// Without the accompanying fix in place, this test would have failed with: |
| 618 |
// - <unspecified file>(1): expected value |
| 619 |
// i.e. the json "mode" key was missing. |
| 620 |
CPPUNIT_ASSERT_EQUAL(std::string("2"), aTree.get<std::string>("mode")); |
| 621 |
} |
| 622 |
|
| 623 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 624 |
|
| 625 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |