1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/* -*- 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 <swmodeltestbase.hxx>
#include <boost/property_tree/json_parser.hpp>
#include <com/sun/star/frame/XModel2.hpp>
#include <com/sun/star/text/XTextViewTextRangeSupplier.hpp>
#include <com/sun/star/util/XCloseable.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <vcl/scheduler.hxx>
#include <tools/json_writer.hxx>
#include <comphelper/propertyvalue.hxx>
#include <xmloff/odffields.hxx>
#include <docsh.hxx>
#include <edtwin.hxx>
#include <unotextrange.hxx>
#include <view.hxx>
#include <wrtsh.hxx>
#include <rootfrm.hxx>
#include <sortedobjs.hxx>
#include <anchoredobject.hxx>
#include <frameformats.hxx>
#include <fmtanchr.hxx>
#include <unotxdoc.hxx>
/// Covers sw/source/uibase/uno/ fixes.
class SwUibaseUnoTest : public SwModelTestBase
{
public:
SwUibaseUnoTest()
: SwModelTestBase("/sw/qa/uibase/uno/data/")
{
}
};
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testLockControllers)
{
createSwDoc();
{
uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY_THROW);
xModel->lockControllers();
}
{
uno::Reference<util::XCloseable> xCloseable(mxComponent, uno::UNO_QUERY_THROW);
xCloseable->close(false);
}
// Without the accompanying fix in place, this test would have crashed.
mxComponent.clear();
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCondFieldCachedValue)
{
createSwDoc("cond-field-cached-value.docx");
Scheduler::ProcessEventsToIdle();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 1
// - Actual :
// i.e. the conditional field lost its cached content.
getParagraph(2, "1");
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCreateTextRangeByPixelPosition)
{
// Given a document with 2 characters, and the pixel position of the point between them:
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwDocShell* pDocShell = pDoc->GetDocShell();
SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
pWrtShell->Insert2("AZ");
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
Point aLogic = pWrtShell->GetCharRect().Center();
SwView* pView = pDocShell->GetView();
SwEditWin& rEditWin = pView->GetEditWin();
Point aPixel = rEditWin.LogicToPixel(aLogic);
// When converting that pixel position to a document model position (text range):
uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XEnumeration> xControllers = xModel->getControllers();
uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(),
uno::UNO_QUERY);
awt::Point aPoint(aPixel.getX(), aPixel.getY());
uno::Reference<text::XTextRange> xTextRange
= xController->createTextRangeByPixelPosition(aPoint);
// Then make sure that text range points after the first character:
auto pTextRange = dynamic_cast<SwXTextRange*>(xTextRange.get());
SwPaM aPaM(pDoc->GetNodes());
pTextRange->GetPositions(aPaM);
sal_Int32 nActual = aPaM.GetPoint()->GetContentIndex();
// Without the needed PixelToLogic() call in place, this test would have failed with:
// - Expected: 1
// - Actual : 0
// i.e. the returned text range pointed before the first character, not between the first and
// the second character.
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nActual);
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCreateTextRangeByPixelPositionGraphic)
{
// Given a document with an as-char image and the center of that image in pixels:
createSwDoc();
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
uno::Reference<beans::XPropertySet> xTextGraphic(
xFactory->createInstance("com.sun.star.text.TextGraphicObject"), uno::UNO_QUERY);
xTextGraphic->setPropertyValue("AnchorType",
uno::Any(text::TextContentAnchorType_AS_CHARACTER));
xTextGraphic->setPropertyValue("Width", uno::Any(static_cast<sal_Int32>(10000)));
xTextGraphic->setPropertyValue("Height", uno::Any(static_cast<sal_Int32>(10000)));
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
uno::Reference<text::XText> xBodyText = xTextDocument->getText();
uno::Reference<text::XTextCursor> xCursor(xBodyText->createTextCursor());
uno::Reference<text::XTextContent> xTextContent(xTextGraphic, uno::UNO_QUERY);
xBodyText->insertTextContent(xCursor, xTextContent, false);
SwDoc* pDoc = getSwDoc();
SwDocShell* pDocShell = pDoc->GetDocShell();
SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
SwRootFrame* pLayout = pWrtShell->GetLayout();
SwFrame* pPage = pLayout->GetLower();
SwFrame* pBody = pPage->GetLower();
SwFrame* pText = pBody->GetLower();
SwSortedObjs& rDrawObjs = *pText->GetDrawObjs();
SwAnchoredObject* pAnchored = rDrawObjs[0];
Point aLogic = pAnchored->GetObjRect().Center();
SwView* pView = pDocShell->GetView();
SwEditWin& rEditWin = pView->GetEditWin();
Point aPixel = rEditWin.LogicToPixel(aLogic);
// When converting that pixel position to a document model position (text range):
uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XEnumeration> xControllers = xModel->getControllers();
uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(),
uno::UNO_QUERY);
awt::Point aPoint(aPixel.getX(), aPixel.getY());
// Without the accompanying fix in place, this test would have crashed, because an XTextRange
// can't point to a graphic node.
uno::Reference<text::XTextRange> xTextRange
= xController->createTextRangeByPixelPosition(aPoint);
// Then make sure that the anchor of the image is returned:
const SwFrameFormats& rFormats = *pDoc->GetSpzFrameFormats();
const SwFrameFormat* pFormat = rFormats[0];
SwPosition aAnchorPos(*pFormat->GetAnchor().GetContentAnchor());
auto pTextRange = dynamic_cast<SwXTextRange*>(xTextRange.get());
SwPaM aPaM(pDoc->GetNodes());
pTextRange->GetPositions(aPaM);
CPPUNIT_ASSERT_EQUAL(aAnchorPos, *aPaM.GetPoint());
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testCreateTextRangeByPixelPositionAtPageGraphic)
{
// Given a document with an at-page anchored image:
createSwDoc();
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
uno::Reference<beans::XPropertySet> xTextGraphic(
xFactory->createInstance("com.sun.star.text.TextGraphicObject"), uno::UNO_QUERY);
xTextGraphic->setPropertyValue("AnchorType", uno::Any(text::TextContentAnchorType_AT_PAGE));
xTextGraphic->setPropertyValue("AnchorPageNo", uno::Any(static_cast<sal_Int16>(1)));
xTextGraphic->setPropertyValue("Width", uno::Any(static_cast<sal_Int32>(10000)));
xTextGraphic->setPropertyValue("Height", uno::Any(static_cast<sal_Int32>(10000)));
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
uno::Reference<text::XText> xBodyText = xTextDocument->getText();
uno::Reference<text::XTextCursor> xCursor(xBodyText->createTextCursor());
uno::Reference<text::XTextContent> xTextContent(xTextGraphic, uno::UNO_QUERY);
xBodyText->insertTextContent(xCursor, xTextContent, false);
SwDoc* pDoc = getSwDoc();
SwDocShell* pDocShell = pDoc->GetDocShell();
SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
SwRootFrame* pLayout = pWrtShell->GetLayout();
SwFrame* pPage = pLayout->GetLower();
SwSortedObjs& rDrawObjs = *pPage->GetDrawObjs();
SwAnchoredObject* pAnchored = rDrawObjs[0];
Point aLogic = pAnchored->GetObjRect().Center();
SwView* pView = pDocShell->GetView();
SwEditWin& rEditWin = pView->GetEditWin();
Point aPixel = rEditWin.LogicToPixel(aLogic);
// When asking for the doc model pos of the image's anchor by pixel position:
uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XEnumeration> xControllers = xModel->getControllers();
uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(),
uno::UNO_QUERY);
awt::Point aPoint(aPixel.getX(), aPixel.getY());
// Without the accompanying fix in place, this test would have crashed.
uno::Reference<text::XTextRange> xTextRange
= xController->createTextRangeByPixelPosition(aPoint);
// Then make sure that the result is empty, since the image is at-page anchored:
CPPUNIT_ASSERT(!xTextRange.is());
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetTextFormFields)
{
// Given a document with 3 fieldmarks: 2 zotero items and a zotero
// bibliography:
createSwDoc();
for (int i = 0; i < 2; ++i)
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))),
comphelper::makePropertyValue("FieldCommand",
uno::Any(OUString("ADDIN ZOTERO_ITEM foo bar"))),
comphelper::makePropertyValue("FieldResult", uno::Any(OUString("result"))),
};
dispatchCommand(mxComponent, ".uno:TextFormField", aArgs);
}
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue("FieldType", uno::Any(OUString(ODF_UNHANDLED))),
comphelper::makePropertyValue("FieldCommand",
uno::Any(OUString("ADDIN ZOTERO_BIBL foo bar"))),
comphelper::makePropertyValue("FieldResult",
uno::Any(OUString("<p>aaa</p><p>bbb</p>"))),
};
dispatchCommand(mxComponent, ".uno:TextFormField", aArgs);
}
// When getting the zotero items:
tools::JsonWriter aJsonWriter;
std::string_view aCommand(".uno:TextFormFields?type=vnd.oasis.opendocument.field.UNHANDLED&"
"commandPrefix=ADDIN%20ZOTERO_ITEM");
auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
pXTextDocument->getCommandValues(aJsonWriter, aCommand);
// Then make sure we find the 2 items and ignore the bibliography:
std::unique_ptr<char[], o3tl::free_delete> pJSON(aJsonWriter.extractData());
std::stringstream aStream(pJSON.get());
boost::property_tree::ptree aTree;
boost::property_tree::read_json(aStream, aTree);
// Without the accompanying fix in place, this test would have failed with:
// - No such node (fields)
// i.e. the returned JSON was just empty.
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aTree.get_child("fields").count(""));
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetDocumentProperties)
{
// Given a document with 3 custom properties: 2 zotero ones and an other one:
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwDocShell* pDocShell = pDoc->GetDocShell();
uno::Reference<document::XDocumentPropertiesSupplier> xDPS(pDocShell->GetModel(),
uno::UNO_QUERY);
uno::Reference<document::XDocumentProperties> xDP = xDPS->getDocumentProperties();
uno::Reference<beans::XPropertyContainer> xUDP = xDP->getUserDefinedProperties();
xUDP->addProperty("ZOTERO_PREF_1", beans::PropertyAttribute::REMOVABLE,
uno::Any(OUString("foo")));
xUDP->addProperty("ZOTERO_PREF_2", beans::PropertyAttribute::REMOVABLE,
uno::Any(OUString("bar")));
xUDP->addProperty("OTHER", beans::PropertyAttribute::REMOVABLE, uno::Any(OUString("baz")));
// When getting the zotero properties:
tools::JsonWriter aJsonWriter;
std::string_view aCommand(".uno:SetDocumentProperties?namePrefix=ZOTERO_PREF_");
auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
pXTextDocument->getCommandValues(aJsonWriter, aCommand);
// Then make sure we find the 2 properties and ignore the other one:
std::unique_ptr<char[], o3tl::free_delete> pJSON(aJsonWriter.extractData());
std::stringstream aStream(pJSON.get());
boost::property_tree::ptree aTree;
boost::property_tree::read_json(aStream, aTree);
// Without the accompanying fix in place, this test would have failed with:
// - No such node (userDefinedProperties)
// i.e. the returned JSON was just empty.
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2),
aTree.get_child("userDefinedProperties").count(""));
}
CPPUNIT_TEST_FIXTURE(SwUibaseUnoTest, testGetBookmarks)
{
// Given a document with 3 bookmarks: 2 zotero references and a zotero bibliography:
createSwDoc();
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue("Bookmark", uno::Any(OUString("ZOTERO_BREF_1"))),
};
dispatchCommand(mxComponent, ".uno:InsertBookmark", aArgs);
}
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue("Bookmark", uno::Any(OUString("ZOTERO_BREF_2"))),
};
dispatchCommand(mxComponent, ".uno:InsertBookmark", aArgs);
}
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue("Bookmark", uno::Any(OUString("ZOTERO_BIBL"))),
};
dispatchCommand(mxComponent, ".uno:InsertBookmark", aArgs);
}
// When getting the reference bookmarks:
tools::JsonWriter aJsonWriter;
std::string_view aCommand(".uno:Bookmarks?namePrefix=ZOTERO_BREF_");
auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
pXTextDocument->getCommandValues(aJsonWriter, aCommand);
// Then make sure we get the 2 references but not the bibliography:
std::unique_ptr<char[], o3tl::free_delete> pJSON(aJsonWriter.extractData());
std::stringstream aStream(pJSON.get());
boost::property_tree::ptree aTree;
boost::property_tree::read_json(aStream, aTree);
// Without the accompanying fix in place, this test would have failed with:
// - No such node (bookmarks)
// i.e. the returned JSON was just empty.
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), aTree.get_child("bookmarks").count(""));
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|