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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
|
/* -*- 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 <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/packages/zip/ZipFileAccess.hpp>
#include <com/sun/star/text/BibliographyDataType.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/text/XTextEmbeddedObjectsSupplier.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>
#include <com/sun/star/chart/XChartDataArray.hpp>
#include <sfx2/dispatch.hxx>
#include <sfx2/viewfrm.hxx>
#include <svx/svdpage.hxx>
#include <editeng/eeitem.hxx>
#include <editeng/adjustitem.hxx>
#include <editeng/outlobj.hxx>
#include <editeng/editobj.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertyvalue.hxx>
#include <unotools/ucbstreamhelper.hxx>
#include <xmloff/odffields.hxx>
#include <comphelper/string.hxx>
#include <comphelper/propertysequence.hxx>
#include <comphelper/sequence.hxx>
#include <osl/thread.hxx>
#include <IDocumentContentOperations.hxx>
#include <cmdid.h>
#include <fmtanchr.hxx>
#include <view.hxx>
#include <wrtsh.hxx>
#include <IDocumentDrawModelAccess.hxx>
#include <drawdoc.hxx>
#include <docsh.hxx>
#include <ndtxt.hxx>
#include <ftnidx.hxx>
#include <txtftn.hxx>
#include <unotxdoc.hxx>
#include <tools/json_writer.hxx>
/// Covers sw/source/uibase/shells/ fixes.
class SwUibaseShellsTest : public SwModelTestBase
{
public:
SwUibaseShellsTest()
: SwModelTestBase(u"/sw/qa/uibase/shells/data/"_ustr)
{
}
};
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testTdf130179)
{
createSwDoc();
SwDoc* pDoc = getSwDoc();
IDocumentContentOperations& rIDCO = pDoc->getIDocumentContentOperations();
SwCursorShell* pShell(pDoc->GetEditShell());
CPPUNIT_ASSERT(pShell);
SfxItemSet aFrameSet(pDoc->GetAttrPool(), svl::Items<RES_FRMATR_BEGIN, RES_FRMATR_END - 1>);
SfxItemSet aGrfSet(pDoc->GetAttrPool(), svl::Items<RES_GRFATR_BEGIN, RES_GRFATR_END - 1>);
SwFormatAnchor aAnchor(RndStdIds::FLY_AT_PARA);
aFrameSet.Put(aAnchor);
Graphic aGrf;
CPPUNIT_ASSERT(rIDCO.InsertGraphic(*pShell->GetCursor(), OUString(), OUString(), &aGrf,
&aFrameSet, &aGrfSet, nullptr));
CPPUNIT_ASSERT_EQUAL(size_t(1), pDoc->GetFlyCount(FLYCNTTYPE_GRF));
SwView* pView = pDoc->GetDocShell()->GetView();
selectShape(1);
std::unique_ptr<SfxPoolItem> pItem;
pView->GetViewFrame().GetBindings().QueryState(FN_POSTIT, pItem);
// Without the accompanying fix in place, this test would have failed with:
// assertion failed
// - Expression: !pItem
// i.e. comment insertion was enabled for an at-para anchored image.
CPPUNIT_ASSERT(!pItem);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testShapeTextAlignment)
{
// FIXME find out why this fails on macOS/Windows
#if !defined(MACOSX) && !defined(_WIN32)
// Create a document with a rectangle in it.
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
Point aStartPos(1000, 1000);
pWrtShell->BeginCreate(SdrObjKind::Rectangle, aStartPos);
Point aMovePos(2000, 2000);
pWrtShell->MoveCreate(aMovePos);
pWrtShell->EndCreate(SdrCreateCmd::ForceEnd);
// Start shape text edit.
SwView* pView = pDoc->GetDocShell()->GetView();
// Select the shape.
selectShape(1);
// Start the actual text edit.
SdrPage* pPage = pWrtShell->GetDoc()->getIDocumentDrawModelAccess().GetDrawModel()->GetPage(0);
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), pPage->GetObjCount());
SdrObject* pObject = pPage->GetObj(0);
pView->EnterShapeDrawTextMode(pObject);
pView->AttrChangedNotify(nullptr);
// Change paragraph adjustment to center.
pView->GetViewFrame().GetDispatcher()->Execute(SID_ATTR_PARA_ADJUST_CENTER,
SfxCallMode::SYNCHRON);
// End shape text edit.
pWrtShell->EndTextEdit();
const OutlinerParaObject* pOutliner = pObject->GetOutlinerParaObject();
// Without the accompanying fix in place, this test would have failed, because the shape had no
// text or text formatting. In other words the paragraph adjustment command was ignored.
CPPUNIT_ASSERT(pOutliner);
const SfxItemSet& rParaAttribs = pOutliner->GetTextObject().GetParaAttribs(0);
SvxAdjust eAdjust = rParaAttribs.GetItem(EE_PARA_JUST)->GetAdjust();
CPPUNIT_ASSERT_EQUAL(SvxAdjust::Center, eAdjust);
#endif
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testOleSavePreviewUpdate)
{
// Load a document with 2 charts in it. The second is down enough that you have to scroll to
// trigger its rendering. Previews are missing for both.
createSwDoc("ole-save-preview-update.odt");
// Explicitly update OLE previews, etc.
dispatchCommand(mxComponent, u".uno:UpdateAll"_ustr, {});
// Save the document and see if we get the previews.
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
xStorable->storeToURL(maTempFile.GetURL(), {});
uno::Reference<packages::zip::XZipFileAccess2> xNameAccess
= packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
maTempFile.GetURL());
// Without the accompanying fix in place, this test would have failed, because the object
// replacements were not generated, even after UpdateAll.
CPPUNIT_ASSERT(xNameAccess->hasByName(u"ObjectReplacements/Object 1"_ustr));
CPPUNIT_ASSERT(xNameAccess->hasByName(u"ObjectReplacements/Object 2"_ustr));
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testOlePreviewUpdate)
{
// Given a document with an embedded Writer object:
createSwDoc("ole-preview-update.odt");
// When updating "all" (including OLE previews):
dispatchCommand(mxComponent, u".uno:UpdateAll"_ustr, {});
// Then make sure the preview is no longer a 0-sized stream:
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
xStorable->storeToURL(maTempFile.GetURL(), {});
uno::Reference<packages::zip::XZipFileAccess2> xNameAccess
= packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
maTempFile.GetURL());
uno::Reference<io::XInputStream> xInputStream(
xNameAccess->getByName(u"ObjectReplacements/Object 1"_ustr), uno::UNO_QUERY);
std::unique_ptr<SvStream> pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true));
// Without the accompanying fix in place, this test would have failed, the stream was still
// empty.
CPPUNIT_ASSERT_GREATER(static_cast<sal_uInt64>(0), pStream->remainingSize());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testBibliographyUrlContextMenu)
{
// Given a document with a bibliography field:
createSwDoc();
SwDoc* pDoc = getSwDoc();
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
uno::Reference<beans::XPropertySet> xField(
xFactory->createInstance(u"com.sun.star.text.TextField.Bibliography"_ustr), uno::UNO_QUERY);
uno::Sequence<beans::PropertyValue> aFields = {
comphelper::makePropertyValue(u"BibiliographicType"_ustr, text::BibliographyDataType::WWW),
comphelper::makePropertyValue(u"Identifier"_ustr, u"AT"_ustr),
comphelper::makePropertyValue(u"Author"_ustr, u"Author"_ustr),
comphelper::makePropertyValue(u"Title"_ustr, u"Title"_ustr),
comphelper::makePropertyValue(u"URL"_ustr, u"http://www.example.com/test.pdf#page=1"_ustr),
};
xField->setPropertyValue(u"Fields"_ustr, uno::Any(aFields));
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
uno::Reference<text::XText> xText = xTextDocument->getText();
uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
// When selecting the field and opening the context menu:
SwDocShell* pDocShell = pDoc->GetDocShell();
SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
SfxDispatcher* pDispatcher = pDocShell->GetViewShell()->GetViewFrame().GetDispatcher();
css::uno::Any aState;
SfxItemState eStateOpen = pDispatcher->QueryState(SID_OPEN_HYPERLINK, aState);
SfxItemState eStateCopy = pDispatcher->QueryState(SID_COPY_HYPERLINK_LOCATION, aState);
// Then the "open hyperlink" and "copy hyperlink location" menu items should be visible:
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 32 (SfxItemState::DEFAULT)
// - Actual : 1 (SfxItemState::DISABLED)
// i.e. the menu item was not visible for biblio entry fields with an URL.
CPPUNIT_ASSERT_EQUAL(SfxItemState::DEFAULT, eStateOpen);
CPPUNIT_ASSERT_EQUAL(SfxItemState::DEFAULT, eStateCopy);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testProtectedFieldsCopyHyperlinkLocation)
{
// Given a test document document that contains:
// - generic url
// - empty line
// - bibliography mark
// - empty line
// - generic url
// - empty line
// - bibliography table heading
// - bibliography entry containing only url
// - empty line
createSwDoc("protectedLinkCopy.fodt");
// Copy generic hyperlink
dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
// Assert generic hyperlink was correctly copied and pasted
CPPUNIT_ASSERT_EQUAL(u"http://reset.url/1"_ustr, getParagraph(2)->getString());
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoLeft"_ustr, {});
// Copy bibliography mark hyperlink
dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
// Assert bibliography mark hyperlink was correctly copied and pasted
CPPUNIT_ASSERT_EQUAL(u"https://test.url/1"_ustr, getParagraph(4)->getString());
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoLeft"_ustr, {});
// Copy generic hyperlink
dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
// Assert generic hyperlink was correctly copied and pasted
CPPUNIT_ASSERT_EQUAL(u"http://reset.url/2"_ustr, getParagraph(6)->getString());
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoLeft"_ustr, {});
// Copy bibliography table hyperlink
dispatchCommand(mxComponent, u".uno:CopyHyperlinkLocation"_ustr, {});
dispatchCommand(mxComponent, u".uno:GoDown"_ustr, {});
dispatchCommand(mxComponent, u".uno:Paste"_ustr, {});
// Assert bibliography table entry hyperlink was correctly copied and pasted
CPPUNIT_ASSERT_EQUAL(u"https://test.url/1"_ustr, getParagraph(9)->getString());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testBibliographyLocalCopyContextMenu)
{
// Given a document with a bibliography field's local copy:
createSwDoc();
SwDoc* pDoc = getSwDoc();
uno::Reference<lang::XMultiServiceFactory> xFactory(mxComponent, uno::UNO_QUERY);
uno::Reference<beans::XPropertySet> xField(
xFactory->createInstance(u"com.sun.star.text.TextField.Bibliography"_ustr), uno::UNO_QUERY);
uno::Sequence<beans::PropertyValue> aFields = {
comphelper::makePropertyValue(u"BibiliographicType"_ustr, text::BibliographyDataType::WWW),
comphelper::makePropertyValue(u"Identifier"_ustr, u"AT"_ustr),
comphelper::makePropertyValue(u"Author"_ustr, u"Author"_ustr),
comphelper::makePropertyValue(u"Title"_ustr, u"Title"_ustr),
comphelper::makePropertyValue(u"URL"_ustr, u"http://www.example.com/test.pdf#page=1"_ustr),
comphelper::makePropertyValue(u"LocalURL"_ustr, u"file:///home/me/test.pdf"_ustr),
};
xField->setPropertyValue(u"Fields"_ustr, uno::Any(aFields));
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
uno::Reference<text::XText> xText = xTextDocument->getText();
uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
uno::Reference<text::XTextContent> xContent(xField, uno::UNO_QUERY);
xText->insertTextContent(xCursor, xContent, /*bAbsorb=*/false);
// When selecting the field and opening the context menu:
SwDocShell* pDocShell = pDoc->GetDocShell();
SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
SfxDispatcher* pDispatcher = pDocShell->GetViewShell()->GetViewFrame().GetDispatcher();
css::uno::Any aState;
SfxItemState eState = pDispatcher->QueryState(FN_OPEN_LOCAL_URL, aState);
// Then the "open local copy" menu item should be visible:
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 32 (SfxItemState::DEFAULT)
// - Actual : 1 (SfxItemState::DISABLED)
// i.e. the context menu was disabled all the time, even for biblio fields.
CPPUNIT_ASSERT_EQUAL(SfxItemState::DEFAULT, eState);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testContentControlPageBreak)
{
// Given a document with a content control and a cursor inside the content control:
createSwDoc();
SwDoc* pDoc = getSwDoc();
uno::Reference<lang::XMultiServiceFactory> xMSF(mxComponent, uno::UNO_QUERY);
uno::Reference<text::XTextDocument> xTextDocument(mxComponent, uno::UNO_QUERY);
uno::Reference<text::XText> xText = xTextDocument->getText();
uno::Reference<text::XTextCursor> xCursor = xText->createTextCursor();
xText->insertString(xCursor, u"test"_ustr, /*bAbsorb=*/false);
xCursor->gotoStart(/*bExpand=*/false);
xCursor->gotoEnd(/*bExpand=*/true);
uno::Reference<text::XTextContent> xContentControl(
xMSF->createInstance(u"com.sun.star.text.ContentControl"_ustr), uno::UNO_QUERY);
xText->insertTextContent(xCursor, xContentControl, /*bAbsorb=*/true);
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
// When trying to insert a page break:
dispatchCommand(mxComponent, u".uno:InsertPagebreak"_ustr, {});
// Then make sure that the document still has a single page:
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 1
// - Actual : 2
// i.e. inline content control had its start and end in different text nodes, which is not
// allowed.
CPPUNIT_ASSERT_EQUAL(1, getPages());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormField)
{
// Given an empty document:
createSwDoc();
SwDoc* pDoc = getSwDoc();
// When inserting an ODF_UNHANDLED fieldmark:
OUString aExpectedCommand(u"ADDIN ZOTERO_BIBL foo bar"_ustr);
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr, uno::Any(aExpectedCommand)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
// Then make sure that it's type/name is correct:
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
SwCursor* pCursor = pWrtShell->GetCursor();
pCursor->SttEndDoc(/*bSttDoc=*/true);
sw::mark::Fieldmark* pFieldmark
= pDoc->getIDocumentMarkAccess()->getFieldmarkAt(*pCursor->GetPoint());
CPPUNIT_ASSERT(pFieldmark);
// Without the accompanying fix in place, this test would have failed with:
// - Expected: vnd.oasis.opendocument.field.UNHANDLED
// - Actual : vnd.oasis.opendocument.field.FORMTEXT
// i.e. the custom type parameter was ignored.
CPPUNIT_ASSERT_EQUAL(ODF_UNHANDLED, pFieldmark->GetFieldname());
auto it = pFieldmark->GetParameters()->find(ODF_CODE_PARAM);
CPPUNIT_ASSERT(it != pFieldmark->GetParameters()->end());
OUString aActualCommand;
it->second >>= aActualCommand;
CPPUNIT_ASSERT_EQUAL(aExpectedCommand, aActualCommand);
SwPaM aPam(pFieldmark->GetMarkStart(), pFieldmark->GetMarkEnd());
// Ignore the leading field start + sep.
aPam.GetMark()->SetContent(aPam.GetMark()->GetContentIndex() + 2);
// Ignore the trailing field end.
aPam.GetPoint()->SetContent(aPam.GetPoint()->GetContentIndex() - 1);
CPPUNIT_ASSERT(aPam.HasMark());
OUString aActualResult = aPam.GetText();
CPPUNIT_ASSERT_EQUAL(u"aaa\nbbb"_ustr, aActualResult);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateFieldmarks)
{
// Given a document with 2 fieldmarks:
createSwDoc();
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM old command 1"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"old result 1"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
}
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM old command 2"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"old result 2"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
}
// When updating those fieldmarks:
uno::Sequence<css::beans::PropertyValue> aField1{
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM new command 1"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"new result 1"_ustr)),
};
uno::Sequence<css::beans::PropertyValue> aField2{
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM new command 2"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"new result 2"_ustr)),
};
uno::Sequence<uno::Sequence<css::beans::PropertyValue>> aFields = { aField1, aField2 };
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommandPrefix"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM"_ustr)),
comphelper::makePropertyValue(u"Fields"_ustr, uno::Any(aFields)),
};
dispatchCommand(mxComponent, u".uno:TextFormFields"_ustr, aArgs);
// Then make sure that the document text contains the new field results:
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/true);
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText();
static sal_Unicode const aForbidden[]
= { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 };
aActual = comphelper::string::removeAny(aActual, aForbidden);
// Without the accompanying fix in place, this test would have failed with:
// - Expected: new result 1new result 2
// - Actual : old result 1old result 2
// i.e. the fieldmarks were not updated.
CPPUNIT_ASSERT_EQUAL(u"new result 1new result 2"_ustr, aActual);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertBookmark)
{
// Given an empty document:
createSwDoc();
SwDoc* pDoc = getSwDoc();
// When inserting a bookmark with text:
OUString aExpectedBookmarkName(u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr);
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"Bookmark"_ustr, uno::Any(aExpectedBookmarkName)),
comphelper::makePropertyValue(u"BookmarkText"_ustr, uno::Any(u"<p>aaa</p><p>bbb</p>"_ustr)),
};
dispatchCommand(mxComponent, u".uno:InsertBookmark"_ustr, aArgs);
// Then make sure that we create a bookmark that covers that text:
IDocumentMarkAccess& rIDMA = *pDoc->getIDocumentMarkAccess();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), rIDMA.getBookmarksCount());
for (auto it = rIDMA.getBookmarksBegin(); it != rIDMA.getBookmarksEnd(); ++it)
{
sw::mark::MarkBase* pMark = *it;
CPPUNIT_ASSERT_EQUAL(aExpectedBookmarkName, pMark->GetName());
SwPaM aPam(pMark->GetMarkStart(), pMark->GetMarkEnd());
OUString aActualResult = aPam.GetText();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: aaa\nbbb
// - Actual :
// i.e. no text was inserted, the bookmark was collapsed.
CPPUNIT_ASSERT_EQUAL(u"aaa\nbbb"_ustr, aActualResult);
}
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testGotoMark)
{
// Given a document with 2 paragraphs, a bookmark on the second one:
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SplitNode();
pWrtShell->SttEndDoc(/*bStt=*/false);
pWrtShell->SetBookmark(vcl::KeyCode(), u"mybookmark"_ustr);
SwNodeOffset nExpected = pWrtShell->GetCursor()->GetPointNode().GetIndex();
// When jumping to that mark from the doc start:
pWrtShell->SttEndDoc(/*bStt=*/true);
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"GotoMark"_ustr, uno::Any(u"mybookmark"_ustr)),
};
dispatchCommand(mxComponent, u".uno:GotoMark"_ustr, aArgs);
// Then make sure that the final cursor position is at the bookmark:
SwNodeOffset nActual = pWrtShell->GetCursor()->GetPointNode().GetIndex();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 10 (bookmark)
// - Actual : 9 (doc start)
// i.e. the actual jump didn't happen.
CPPUNIT_ASSERT_EQUAL(nExpected, nActual);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateBookmarks)
{
// Given a document with 2 bookmarks, first covering "B" and second covering "D":
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->Insert(u"ABCDE"_ustr);
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
pWrtShell->SetBookmark(vcl::KeyCode(), u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
pWrtShell->SetBookmark(vcl::KeyCode(), u"ZOTERO_BREF_PRxDGUb4SWXF"_ustr);
// When updating the content of bookmarks:
pWrtShell->SttEndDoc(/*bStt=*/true);
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"BookmarkNamePrefix": {
"type": "string",
"value": "ZOTERO_BREF_"
},
"Bookmarks": {
"type": "[][]com.sun.star.beans.PropertyValue",
"value": [
{
"Bookmark": {
"type": "string",
"value": "ZOTERO_BREF_new1"
},
"BookmarkText": {
"type": "string",
"value": "new result 1"
}
},
{
"Bookmark": {
"type": "string",
"value": "ZOTERO_BREF_new2"
},
"BookmarkText": {
"type": "string",
"value": "new result 2"
}
}
]
}
}
)json"_ostr);
uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:UpdateBookmarks"_ustr, aArgs);
// Then make sure that the only paragraph is updated correctly:
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActual = pCursor->GetPointNode().GetTextNode()->GetText();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: Anew result 1Cnew result 2E
// - Actual : ABCDE
// i.e. the content was not updated.
CPPUNIT_ASSERT_EQUAL(u"Anew result 1Cnew result 2E"_ustr, aActual);
// Without the accompanying fix in place, this test would have failed, ZOTERO_BREF_GiQ7DAWQYWLy
// was not renamed to ZOTERO_BREF_new1.
auto it = pDoc->getIDocumentMarkAccess()->findMark(u"ZOTERO_BREF_new1"_ustr);
CPPUNIT_ASSERT(it != pDoc->getIDocumentMarkAccess()->getAllMarksEnd());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertFieldmarkReadonly)
{
// Given a document with a fieldmark, the cursor inside the fieldmark:
createSwDoc();
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr, uno::Any(u"my command"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"my result"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
SwCursor* pCursor = pWrtShell->GetCursor();
pCursor->SttEndDoc(/*bSttDoc=*/true);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
// When trying to insert an inner fieldmark:
// Without the accompanying fix in place, this test would have crashed.
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
// Then make sure the read-only content refuses to accept that inner fieldmark, so we still have
// just one:
IDocumentMarkAccess& rIDMA = *pDoc->getIDocumentMarkAccess();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), rIDMA.getFieldmarksCount());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureTransformChart)
{
createSwDoc("docStructureChartExampleOriginal.odt");
OString aJson = R"json(
{
"Transforms": {
"Charts.ByEmbedIndex.0": {
"modifyrow.1": [ 19, 15 ],
"datayx.3.1": 37,
"deleterow.0": "",
"deleterow.1": "",
"insertrow.0": [ 15, 17 ],
"setrowdesc.0": "Paul",
"insertrow.2": [ 19, 22 ],
"setrowdesc.2": "Barbara",
"insertrow.4": [ 29, 27 ],
"setrowdesc.4": "Elizabeth",
"insertrow.5": [ 14, 26 ],
"setrowdesc.5": "William",
"insertcolumn.1": [ 1,2,3,4,5,6 ],
"insertcolumn.0": [ 2,1,2,1,2,1 ],
"insertcolumn.4": [ 7,7,7,7,7,7 ],
"setcolumndesc.4": "c4",
"setcolumndesc.0": "c0",
"setcolumndesc.2": "c2"
},
"Charts.BySubTitle.Subtitle2": {
"deletecolumn.3": ""
},
"Charts.ByEmbedName.Object3": {
"resize": [ 12, 3 ],
"setrowdesc":
[
"United Kingdom",
"United States of America",
"Canada",
"Brazil",
"Germany",
"India",
"Japan",
"Sudan",
"Norway",
"Italy",
"France",
"Egypt"
],
"modifycolumn.0": [ 12, 9, 8, 5, 5, 4, 3, 3, 2, 2, 1, 1],
"resize": [ 12, 2 ]
},
"Charts.ByTitle.Fixed issues": {
"data": [ [ 3,1,2 ],
[ 2,0,1 ],
[ 3,2,0 ],
[ 2,1,1 ],
[ 4,2,2 ],
[ 2,2,1 ],
[ 3,2,0,0 ],
[ 3,1,2,1 ],
[ 3,3,1,0,1 ],
[ 2,1,0,1,2 ],
[ 4,2,1,2,2,3 ],
[ 2,1,0,0,1,4 ],
[ 3,2,2,1,3,2 ],
[ 4,2,1,1,2,3 ],
[ 3,3,2,0,3,5 ],
[ 3,3,1,2,2,3 ],
[ 5,1,1,1,1,4 ],
[ 2,2,2,1,2,3 ] ],
"setrowdesc": ["2023.01",".02",".03",".04",".05",".06",".07",".08",".09",".10",".11",".12","2023.01",".02",".03",".04",".05",".06"],
"setcolumndesc": ["Jennifer", "Charles", "Thomas", "Maria", "Lisa", "Daniel"]
}
}
}
)json"_ostr;
//transform
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"DataJson"_ustr,
uno::Any(OStringToOUString(aJson, RTL_TEXTENCODING_UTF8))),
};
dispatchCommand(mxComponent, u".uno:TransformDocumentStructure"_ustr, aArgs);
// Check transformed values of the 3 chart
for (int nShape = 0; nShape < 3; nShape++)
{
uno::Reference<text::XTextEmbeddedObjectsSupplier> xEOS(mxComponent, uno::UNO_QUERY);
CPPUNIT_ASSERT(xEOS.is());
uno::Reference<container::XIndexAccess> xEmbeddeds(xEOS->getEmbeddedObjects(),
uno::UNO_QUERY);
CPPUNIT_ASSERT(xEmbeddeds.is());
uno::Reference<beans::XPropertySet> xShapeProps(xEmbeddeds->getByIndex(nShape),
uno::UNO_QUERY);
CPPUNIT_ASSERT(xShapeProps.is());
uno::Reference<frame::XModel> xDocModel;
xShapeProps->getPropertyValue(u"Model"_ustr) >>= xDocModel;
CPPUNIT_ASSERT(xDocModel.is());
uno::Reference<chart2::XChartDocument> xChartDoc(xDocModel, uno::UNO_QUERY);
CPPUNIT_ASSERT(xChartDoc.is());
uno::Reference<chart::XChartDataArray> xDataArray(xChartDoc->getDataProvider(),
uno::UNO_QUERY);
CPPUNIT_ASSERT(xDataArray.is());
switch (nShape)
{
case 0:
{
std::vector<std::vector<double>> aValues = {
{ 2, 15, 1, 7 }, { 1, 19, 2, 7 }, { 2, 19, 3, 7 },
{ 1, 25, 4, 7 }, { 2, 29, 5, 7 }, { 1, 14, 6, 7 },
};
// RowDescriptions
std::vector<OUString> aRowDescsValues
= { u"Paul"_ustr, u"Mary"_ustr, u"Barbara"_ustr,
u"David"_ustr, u"Elizabeth"_ustr, u"William"_ustr };
// ColumnDescriptions
std::vector<OUString> aColDescsValues
= { u"c0"_ustr, u"2022"_ustr, u"c2"_ustr, u"c4"_ustr };
uno::Sequence<uno::Sequence<double>> aData = xDataArray->getData();
for (size_t nY = 0; nY < aValues.size(); nY++)
{
for (size_t nX = 0; nX < aValues[nY].size(); nX++)
{
CPPUNIT_ASSERT_EQUAL(aData[nY][nX], aValues[nY][nX]);
}
}
uno::Sequence<OUString> aColDescs = xDataArray->getColumnDescriptions();
for (size_t i = 0; i < aColDescsValues.size(); i++)
{
CPPUNIT_ASSERT_EQUAL(aColDescs[i], aColDescsValues[i]);
}
uno::Sequence<OUString> aRowDescs = xDataArray->getRowDescriptions();
for (size_t i = 0; i < aRowDescsValues.size(); i++)
{
CPPUNIT_ASSERT_EQUAL(aRowDescs[i], aRowDescsValues[i]);
}
}
break;
case 1:
{
uno::Sequence<uno::Sequence<double>> aData = xDataArray->getData();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(18), aData.getLength());
uno::Sequence<double>* pRows = aData.getArray();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(6), pRows[0].getLength());
}
break;
case 2:
{
uno::Sequence<uno::Sequence<double>> aData = xDataArray->getData();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(12), aData.getLength());
uno::Sequence<double>* pRows = aData.getArray();
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(2), pRows[0].getLength());
}
break;
default:
break;
}
}
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDocumentStructureExtractChart)
{
createSwDoc("docStructureChartExampleOriginal.odt");
//extract
tools::JsonWriter aJsonWriter;
std::string_view aCommand(".uno:ExtractDocumentStructure");
auto pXTextDocument = dynamic_cast<SwXTextDocument*>(mxComponent.get());
pXTextDocument->getCommandValues(aJsonWriter, aCommand);
OString aExpectedStr
= "{ \"DocStructure\": { \"Charts.ByEmbedIndex.0\": { \"name\": \"Object1\", \"title\": "
"\"Paid leave days\", \"subtitle\": \"Subtitle2\", \"RowDescriptions\": [ \"James\", "
"\"Mary\", \"Patricia\", \"David\"], \"ColumnDescriptions\": [ \"2022\", \"2023\"], "
"\"DataValues\": [ \"Row.0\": [ \"22\", \"24\"], \"Row.1\": [ \"18\", \"16\"], "
"\"Row.2\": [ \"32\", \"32\"], \"Row.3\": [ \"25\", \"23\"]]}, "
"\"Charts.ByEmbedIndex.1\": { \"name\": \"Object2\", \"title\": \"Fixed issues\", "
"\"subtitle\": \"Subtitle1\", \"RowDescriptions\": [ \"\"], \"ColumnDescriptions\": [ \" "
"\"], \"DataValues\": [ \"Row.0\": [ \"NaN\"]]}, \"Charts.ByEmbedIndex.2\": { \"name\": "
"\"Object3\", \"title\": \"Employees from countries\", \"subtitle\": \"Subtitle3\", "
"\"RowDescriptions\": [ \"\"], \"ColumnDescriptions\": [ \"Column 1\"], \"DataValues\": "
"[ \"Row.0\": [ \"NaN\"]]}}}"_ostr;
CPPUNIT_ASSERT_EQUAL(aExpectedStr, aJsonWriter.finishAndGetAsOString());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateRefmarks)
{
// Given a document with two refmarks, one is not interesting the other is a citation:
createSwDoc();
SwDoc* pDoc = getSwDoc();
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
comphelper::makePropertyValue(u"Name"_ustr, uno::Any(u"some other old refmark"_ustr)),
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"some other old content"_ustr)),
};
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/false);
pWrtShell->SplitNode();
pWrtShell->SttEndDoc(/*bStt=*/false);
aArgs = {
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
comphelper::makePropertyValue(u"Name"_ustr,
uno::Any(u"ZOTERO_ITEM CSL_CITATION {} old refmark"_ustr)),
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"old content"_ustr)),
};
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
// When updating that refmark:
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"TypeName": {
"type": "string",
"value": "SetRef"
},
"NamePrefix": {
"type": "string",
"value": "ZOTERO_ITEM CSL_CITATION"
},
"Fields": {
"type": "[][]com.sun.star.beans.PropertyValue",
"value": [
{
"Name": {
"type": "string",
"value": "ZOTERO_ITEM CSL_CITATION {} new refmark"
},
"Content": {
"type": "string",
"value": "new content"
}
}
]
}
}
)json"_ostr);
aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:UpdateFields"_ustr, aArgs);
// Then make sure that the document text features the new content:
SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: new content
// - Actual : old content
// i.e. the doc content was not updated.
CPPUNIT_ASSERT_EQUAL(u"new content"_ustr, pTextNode->GetText());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateFieldmark)
{
// Given a document with a fieldmark:
createSwDoc();
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM old command 1"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"old result 1"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
// When updating that fieldmark to have new field command & result:
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/false);
pWrtShell->Left(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"FieldType": {
"type": "string",
"value": "vnd.oasis.opendocument.field.UNHANDLED"
},
"FieldCommandPrefix": {
"type": "string",
"value": "ADDIN ZOTERO_ITEM"
},
"Field": {
"type": "[]com.sun.star.beans.PropertyValue",
"value": {
"FieldType": {
"type": "string",
"value": "vnd.oasis.opendocument.field.UNHANDLED"
},
"FieldCommand": {
"type": "string",
"value": "ADDIN ZOTERO_ITEM new command 1"
},
"FieldResult": {
"type": "string",
"value": "new result 1"
}
}
}
}
)json"_ostr);
aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:UpdateTextFormField"_ustr, aArgs);
// Then make sure that the document text is updated accordingly:
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText();
static sal_Unicode const aForbidden[]
= { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 };
aActual = comphelper::string::removeAny(aActual, aForbidden);
// Without the accompanying fix in place, this test would have failed with:
// - Expected: new result 1
// - Actual : old result 1
// i.e. the document text was not updated.
CPPUNIT_ASSERT_EQUAL(u"new result 1"_ustr, aActual);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateSections)
{
// Given a document with a section:
createSwDoc();
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"RegionName"_ustr,
uno::Any(u"ZOTERO_BIBL {} CSL_BIBLIOGRAPHY RNDold"_ustr)),
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"old content"_ustr)),
};
dispatchCommand(mxComponent, u".uno:InsertSection"_ustr, aArgs);
// When updating that section:
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"SectionNamePrefix": {
"type": "string",
"value": "ZOTERO_BIBL"
},
"Sections": {
"type": "[][]com.sun.star.beans.PropertyValue",
"value": [
{
"RegionName": {
"type": "string",
"value": "ZOTERO_BIBL {} CSL_BIBLIOGRAPHY RNDnew"
},
"Content": {
"type": "string",
"value": "new content"
}
}
]
}
}
)json"_ostr);
aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:UpdateSections"_ustr, aArgs);
// Then make sure that the section is updated:
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->EndOfSection(/*bSelect=*/true);
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActualResult = pCursor->GetText();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: new content
// - Actual : old content
// i.e. the content wasn't updated.
CPPUNIT_ASSERT_EQUAL(u"new content"_ustr, aActualResult);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDeleteFieldmarks)
{
// Given a document with 2 fieldmarks:
createSwDoc();
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM old command 1"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result 1"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
}
{
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM old command 2"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result 2"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
}
// When deleting those fieldmarks:
uno::Sequence<css::beans::PropertyValue> aArgs
= { comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommandPrefix"_ustr,
uno::Any(u"ADDIN ZOTERO_ITEM"_ustr)) };
dispatchCommand(mxComponent, u".uno:DeleteTextFormFields"_ustr, aArgs);
// Then make sure that the document doesn't contain fields anymore:
SwDoc* pDoc = getSwDoc();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 0
// - Actual : 2
// i.e. the fieldmarks were not deleted.
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0),
pDoc->getIDocumentMarkAccess()->getAllMarksCount());
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/true);
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActual = pCursor->Start()->GetNode().GetTextNode()->GetText();
CPPUNIT_ASSERT_EQUAL(u"result 1result 2"_ustr, aActual);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateBookmark)
{
// Given a document with a bookmarks, covering "BC":
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->Insert(u"ABCD"_ustr);
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 2, /*bBasicCall=*/false);
pWrtShell->SetBookmark(vcl::KeyCode(), u"ZOTERO_BREF_old"_ustr);
// When updating the content of the bookmark under the cursor:
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 2, /*bBasicCall=*/false);
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"BookmarkNamePrefix": {
"type": "string",
"value": "ZOTERO_BREF_"
},
"Bookmark": {
"type": "[]com.sun.star.beans.PropertyValue",
"value": {
"Bookmark": {
"type": "string",
"value": "ZOTERO_BREF_new"
},
"BookmarkText": {
"type": "string",
"value": "new result"
}
}
}
}
)json"_ostr);
uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:UpdateBookmark"_ustr, aArgs);
// Then make sure that the only paragraph is updated correctly:
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActual = pCursor->GetPointNode().GetTextNode()->GetText();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: Anew resultD
// - Actual : ABCD
// i.e. it was not possible to update just the bookmark under cursor.
CPPUNIT_ASSERT_EQUAL(u"Anew resultD"_ustr, aActual);
auto it = pDoc->getIDocumentMarkAccess()->findMark(u"ZOTERO_BREF_new"_ustr);
CPPUNIT_ASSERT(it != pDoc->getIDocumentMarkAccess()->getAllMarksEnd());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateRefmark)
{
// Given a document with a refmark:
createSwDoc();
SwDoc* pDoc = getSwDoc();
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
comphelper::makePropertyValue(u"Name"_ustr,
uno::Any(u"ZOTERO_ITEM CSL_CITATION {} old refmark"_ustr)),
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"old content"_ustr)),
};
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
// When updating that refmark:
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"TypeName": {
"type": "string",
"value": "SetRef"
},
"NamePrefix": {
"type": "string",
"value": "ZOTERO_ITEM CSL_CITATION"
},
"Field": {
"type": "[]com.sun.star.beans.PropertyValue",
"value": {
"Name": {
"type": "string",
"value": "ZOTERO_ITEM CSL_CITATION {} new refmark"
},
"Content": {
"type": "string",
"value": "new content"
}
}
}
}
)json"_ostr);
aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:UpdateField"_ustr, aArgs);
// Then make sure that the document text features the new content:
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
SwTextNode* pTextNode = pWrtShell->GetCursor()->GetPointNode().GetTextNode();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: new content
// - Actual : old content
// i.e. the content was not updated.
CPPUNIT_ASSERT_EQUAL(u"new content"_ustr, pTextNode->GetText());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDeleteBookmarks)
{
// Given a document with 2 bookmarks, first covering "B" and second covering "D":
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
pWrtShell->Insert(u"ABCDE"_ustr);
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
pWrtShell->SetBookmark(vcl::KeyCode(), u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/false, 1, /*bBasicCall=*/false);
pWrtShell->Right(SwCursorSkipMode::Chars, /*bSelect=*/true, 1, /*bBasicCall=*/false);
pWrtShell->SetBookmark(vcl::KeyCode(), u"other"_ustr);
// When deleting 1 matching bookmark:
pWrtShell->SttEndDoc(/*bStt=*/true);
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"BookmarkNamePrefix": {
"type": "string",
"value": "ZOTERO_BREF_"
}
}
)json"_ostr);
uno::Sequence<beans::PropertyValue> aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:DeleteBookmarks"_ustr, aArgs);
// Then make sure that only the other bookmark is kept:
auto it = pDoc->getIDocumentMarkAccess()->findMark(u"ZOTERO_BREF_GiQ7DAWQYWLy"_ustr);
// Without the accompanying fix in place, this test would have failed, the matching bookmark was
// not removed.
CPPUNIT_ASSERT(bool(it == pDoc->getIDocumentMarkAccess()->getAllMarksEnd()));
it = pDoc->getIDocumentMarkAccess()->findMark(u"other"_ustr);
CPPUNIT_ASSERT(it != pDoc->getIDocumentMarkAccess()->getAllMarksEnd());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testDeleteFields)
{
// Given a document with a refmark:
createSwDoc();
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"TypeName"_ustr, uno::Any(u"SetRef"_ustr)),
comphelper::makePropertyValue(u"Name"_ustr,
uno::Any(u"ZOTERO_ITEM CSL_CITATION {} RNDpyJknp173F"_ustr)),
comphelper::makePropertyValue(u"Content"_ustr, uno::Any(u"aaa<b>bbb</b>ccc"_ustr)),
};
dispatchCommand(mxComponent, u".uno:InsertField"_ustr, aArgs);
// When deleting the refmarks:
std::vector<beans::PropertyValue> aArgsVec = comphelper::JsonToPropertyValues(R"json(
{
"TypeName": {
"type": "string",
"value": "SetRef"
},
"NamePrefix": {
"type": "string",
"value": "ZOTERO_ITEM CSL_CITATION"
}
}
)json"_ostr);
aArgs = comphelper::containerToSequence(aArgsVec);
dispatchCommand(mxComponent, u".uno:DeleteFields"_ustr, aArgs);
// Then make sure that no refmark is kept:
SwDoc* pDoc = getSwDoc();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 0
// - Actual : 1
// i.e. the refmark was not deleted.
CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(0), pDoc->GetRefMarks());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormFieldFootnote)
{
// Given an empty document:
createSwDoc();
SwDoc* pDoc = getSwDoc();
// When inserting an ODF_UNHANDLED fieldmark inside a footnote:
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_BIBL foo bar"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result"_ustr)),
comphelper::makePropertyValue(u"Wrapper"_ustr, uno::Any(u"Footnote"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
// Then make sure that the footnote is created:
SwFootnoteIdxs& rFootnotes = pDoc->GetFootnoteIdxs();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 1
// - Actual : 0
// i.e. no footnote was created.
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rFootnotes.size());
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testInsertTextFormFieldEndnote)
{
// Given an empty document:
createSwDoc();
SwDoc* pDoc = getSwDoc();
// When inserting an ODF_UNHANDLED fieldmark inside an endnote:
uno::Sequence<css::beans::PropertyValue> aArgs = {
comphelper::makePropertyValue(u"FieldType"_ustr, uno::Any(ODF_UNHANDLED)),
comphelper::makePropertyValue(u"FieldCommand"_ustr,
uno::Any(u"ADDIN ZOTERO_BIBL foo bar"_ustr)),
comphelper::makePropertyValue(u"FieldResult"_ustr, uno::Any(u"result"_ustr)),
comphelper::makePropertyValue(u"Wrapper"_ustr, uno::Any(u"Endnote"_ustr)),
};
dispatchCommand(mxComponent, u".uno:TextFormField"_ustr, aArgs);
// Then make sure that the endnote is created:
SwFootnoteIdxs& rFootnotes = pDoc->GetFootnoteIdxs();
// Without the accompanying fix in place, this test would have failed with:
// - Expected: 1
// - Actual : 0
// i.e. no endnote was inserted.
CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), rFootnotes.size());
SwTextFootnote* pEndnote = rFootnotes[0];
const SwFormatFootnote& rFormatEndnote = pEndnote->GetFootnote();
CPPUNIT_ASSERT(rFormatEndnote.IsEndNote());
// Also check that the endnote body contains the fieldmark:
SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
pWrtShell->SttEndDoc(/*bStt=*/true);
pWrtShell->GotoFootnoteText();
pWrtShell->EndOfSection(/*bSelect=*/true);
SwCursor* pCursor = pWrtShell->GetCursor();
OUString aActual = pCursor->GetText();
static sal_Unicode const aForbidden[]
= { CH_TXT_ATR_FIELDSTART, CH_TXT_ATR_FIELDSEP, CH_TXT_ATR_FIELDEND, 0 };
aActual = comphelper::string::removeAny(aActual, aForbidden);
// Then this was empty: the fieldmark was inserted before the note anchor, not in the note body.
CPPUNIT_ASSERT_EQUAL(u"result"_ustr, aActual);
}
CPPUNIT_TEST_FIXTURE(SwUibaseShellsTest, testUpdateSelectedField)
{
// Given an empty doc:
createSwDoc();
SwDoc* pDoc = getSwDoc();
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
SwCursorShell* pShell(pDoc->GetEditShell());
CPPUNIT_ASSERT(pShell);
SwPaM* pCursor = pShell->GetCursor();
// Insert a time field and select it:
dispatchCommand(mxComponent, u".uno:InsertTimeFieldVar"_ustr, {});
pCursor->SetMark();
pCursor->Move(fnMoveBackward);
OUString aTimeFieldBefore, aTimeFieldAfter;
pWrtShell->GetSelectedText(aTimeFieldBefore);
// Wait for one second:
osl::Thread::wait(std::chrono::seconds(1));
// Update the field at cursor:
dispatchCommand(mxComponent, u".uno:UpdateSelectedField"_ustr, {});
pWrtShell->GetSelectedText(aTimeFieldAfter);
// Check that the selected field has changed:
CPPUNIT_ASSERT(aTimeFieldAfter != aTimeFieldBefore);
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|