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
|
/* -*- 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/awt/XBitmap.hpp>
#include <com/sun/star/graphic/XGraphic.hpp>
#include <officecfg/Office/Common.hxx>
#include <comphelper/processfactory.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>
#include <doc.hxx>
#include <ndgrf.hxx>
#include <drawdoc.hxx>
#include <unotools/fltrcfg.hxx>
class Test : public SwModelTestBase
{
public:
Test() : SwModelTestBase() {}
void testSwappedOutImageExport();
void testLinkedGraphicRT();
void testImageWithSpecialID();
void testGraphicShape();
void testCharHighlight();
void testCharHighlightODF();
void testCharHighlightBody();
void testMSCharBackgroundEditing();
void testCharBackgroundToHighlighting();
#if !defined(_WIN32)
void testSkipImages();
#endif
CPPUNIT_TEST_SUITE(Test);
CPPUNIT_TEST(testSwappedOutImageExport);
CPPUNIT_TEST(testLinkedGraphicRT);
CPPUNIT_TEST(testImageWithSpecialID);
CPPUNIT_TEST(testGraphicShape);
CPPUNIT_TEST(testCharHighlight);
CPPUNIT_TEST(testCharHighlightODF);
CPPUNIT_TEST(testMSCharBackgroundEditing);
CPPUNIT_TEST(testCharBackgroundToHighlighting);
#if !defined(_WIN32)
CPPUNIT_TEST(testSkipImages);
#endif
CPPUNIT_TEST_SUITE_END();
};
void Test::testSwappedOutImageExport()
{
const char* aFilterNames[] = {
"writer8",
"Rich Text Format",
"MS Word 97",
"Office Open XML Text",
};
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
// Check whether the export code swaps in the image which was swapped out before by auto mechanism
// Set cache size to a very small value to make sure one of the images is swapped out
std::shared_ptr< comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
officecfg::Office::Common::Cache::GraphicManager::TotalCacheSize::set(static_cast<sal_Int32>(1), batch);
batch->commit();
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/document_with_two_images.odt"), "com.sun.star.text.TextDocument");
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString::createFromAscii(aFilterNames[nFilter]);
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
// Check whether graphic exported well after it was swapped out
uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XIndexAccess> xDraws(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY);
const OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter];
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(2), xDraws->getCount());
// First image
uno::Reference<drawing::XShape> xImage(xDraws->getByIndex(0), uno::UNO_QUERY);
uno::Reference< beans::XPropertySet > XPropSet( xImage, uno::UNO_QUERY_THROW );
// Check URL
{
OUString sURL;
XPropSet->getPropertyValue("GraphicURL") >>= sURL;
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), sURL != "vnd.sun.star.GraphicObject:00000000000000000000000000000000");
}
// Check size
{
uno::Reference<graphic::XGraphic> xGraphic;
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), xBitmap.is());
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(610), xBitmap->getSize().Width );
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(381), xBitmap->getSize().Height );
}
// Second Image
xImage.set(xDraws->getByIndex(1), uno::UNO_QUERY);
XPropSet.set( xImage, uno::UNO_QUERY_THROW );
// Check URL
{
OUString sURL;
XPropSet->getPropertyValue("GraphicURL") >>= sURL;
CPPUNIT_ASSERT_MESSAGE(
sFailedMessage.getStr(), sURL != "vnd.sun.star.GraphicObject:00000000000000000000000000000000");
}
// Check size
{
uno::Reference<graphic::XGraphic> xGraphic;
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), xBitmap.is());
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(900), xBitmap->getSize().Width );
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(600), xBitmap->getSize().Height );
}
}
}
void Test::testLinkedGraphicRT()
{
const std::vector<OUString> aFilterNames = {
"writer8",
// "Rich Text Format", Note: picture is there, but SwGrfNode is not found?
"MS Word 97",
"Office Open XML Text",
};
for( size_t nFilter = 0; nFilter < aFilterNames.size(); ++nFilter )
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/document_with_linked_graphic.odt"), "com.sun.star.text.TextDocument");
const OString sFailedMessage = OString("Failed on filter: ")
+ OUStringToOString(aFilterNames[nFilter], RTL_TEXTENCODING_ASCII_US);
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= aFilterNames[nFilter];
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument *>(mxComponent.get());
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), pTextDoc);
SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), pDoc);
SwNodes& aNodes = pDoc->GetNodes();
// Find the image
bool bImageFound = false;
for( sal_uLong nIndex = 0; nIndex < aNodes.Count(); ++nIndex)
{
if( aNodes[nIndex]->IsGrfNode() )
{
SwGrfNode* pGrfNode = aNodes[nIndex]->GetGrfNode();
CPPUNIT_ASSERT(pGrfNode);
// RT via DOCX makes linked graphic embedded?!
if( aFilterNames[nFilter] != "Office Open XML Text" )
{
CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pGrfNode->IsGrfLink());
}
const GraphicObject& rGraphicObj = pGrfNode->GetGrfObj(true);
CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), !rGraphicObj.IsSwappedOut());
CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetType());
CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), static_cast<sal_uLong>(864900), rGraphicObj.GetSizeBytes());
bImageFound = true;
}
}
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), bImageFound);
}
}
void Test::testImageWithSpecialID()
{
// Check how LO handles when the imported graphic's ID is different from that one
// which is generated by LO.
const char* aFilterNames[] = {
"writer8",
"Rich Text Format",
"MS Word 97",
"Office Open XML Text",
};
// Trigger swap out mechanism to test swapped state factor too.
std::shared_ptr< comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
officecfg::Office::Common::Cache::GraphicManager::TotalCacheSize::set(static_cast<sal_Int32>(1), batch);
batch->commit();
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/images_with_special_IDs.odt"), "com.sun.star.text.TextDocument");
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString::createFromAscii(aFilterNames[nFilter]);
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
// Check whether graphic exported well
uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XIndexAccess> xDraws(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY);
const OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter];
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(2), xDraws->getCount());
uno::Reference<drawing::XShape> xImage = getShape(1);
uno::Reference< beans::XPropertySet > XPropSet( xImage, uno::UNO_QUERY_THROW );
// Check URL
{
OUString sURL;
XPropSet->getPropertyValue("GraphicURL") >>= sURL;
CPPUNIT_ASSERT(sURL != "vnd.sun.star.GraphicObject:00000000000000000000000000000000");
}
// Check size
{
uno::Reference<graphic::XGraphic> xGraphic;
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
CPPUNIT_ASSERT(xBitmap.is());
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(610), xBitmap->getSize().Width );
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(381), xBitmap->getSize().Height );
}
// Second Image
xImage = getShape(2);
XPropSet.set( xImage, uno::UNO_QUERY_THROW );
// Check URL
{
OUString sURL;
XPropSet->getPropertyValue("GraphicURL") >>= sURL;
CPPUNIT_ASSERT(sURL != "vnd.sun.star.GraphicObject:00000000000000000000000000000000");
}
// Check size
{
uno::Reference<graphic::XGraphic> xGraphic;
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
CPPUNIT_ASSERT(xBitmap.is());
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(900), xBitmap->getSize().Width );
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(600), xBitmap->getSize().Height );
}
}
}
void Test::testGraphicShape()
{
// There are two kind of images in Writer: 1) Writer specific handled by SwGrfNode and
// 2) graphic shape handled by SdrGrafObj (e.g. after copy&paste from Impress).
const char* aFilterNames[] = {
"writer8",
"Rich Text Format",
"MS Word 97",
"Office Open XML Text",
};
// Trigger swap out mechanism to test swapped state factor too.
std::shared_ptr< comphelper::ConfigurationChanges > batch(comphelper::ConfigurationChanges::create());
officecfg::Office::Common::Cache::GraphicManager::TotalCacheSize::set(static_cast<sal_Int32>(1), batch);
batch->commit();
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/graphic_shape.odt"), "com.sun.star.text.TextDocument");
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString::createFromAscii(aFilterNames[nFilter]);
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
// Check whether graphic exported well
uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XIndexAccess> xDraws(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY);
const OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter];
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(2), xDraws->getCount());
uno::Reference<drawing::XShape> xImage = getShape(1);
uno::Reference< beans::XPropertySet > XPropSet( xImage, uno::UNO_QUERY_THROW );
// First image is embedded
// Check URL
{
OUString sURL;
XPropSet->getPropertyValue("GraphicURL") >>= sURL;
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), sURL != "vnd.sun.star.GraphicObject:00000000000000000000000000000000");
}
// Check size
{
uno::Reference<graphic::XGraphic> xGraphic;
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), xBitmap.is());
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(610), xBitmap->getSize().Width );
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(381), xBitmap->getSize().Height );
}
// MS filters make this kind of linked images broken !?
if( OUString::createFromAscii(aFilterNames[nFilter]) != "writer8" )
return;
// Second image is a linked one
xImage = getShape(2);
XPropSet.set( xImage, uno::UNO_QUERY_THROW );
// Check URL
{
OUString sURL;
XPropSet->getPropertyValue("GraphicURL") >>= sURL;
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), sURL.endsWith("linked_graphic.jpg"));
}
// Check size
{
uno::Reference<graphic::XGraphic> xGraphic;
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
uno::Reference<awt::XBitmap> xBitmap(xGraphic, uno::UNO_QUERY);
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), xBitmap.is());
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(620), xBitmap->getSize().Width );
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(465), xBitmap->getSize().Height );
}
}
}
void Test::testCharHighlightBody()
{
// MS Word has two kind of character backgrounds called character shading and highlighting
// MS filters handle these attributes separately, but ODF export merges them into one background attribute
const char* aFilterNames[] = {
"writer8",
"Rich Text Format",
"MS Word 97",
"Office Open XML Text",
};
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/char_highlight.docx"),
"com.sun.star.text.TextDocument");
const OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter];
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString::createFromAscii(aFilterNames[nFilter]);
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
const uno::Reference< text::XTextRange > xPara = getParagraph(1);
// Both highlight and background
const sal_Int32 nBackColor(0x4F81BD);
for( int nRun = 1; nRun <= 16; ++nRun )
{
const uno::Reference<beans::XPropertySet> xRun(getRun(xPara,nRun), uno::UNO_QUERY);
sal_Int32 nHighlightColor = 0;
switch( nRun )
{
case 1: nHighlightColor = 0x000000; break; //black
case 2: nHighlightColor = 0x0000ff; break; //blue
case 3: nHighlightColor = 0x00ffff; break; //cyan
case 4: nHighlightColor = 0x00ff00; break; //green
case 5: nHighlightColor = 0xff00ff; break; //magenta
case 6: nHighlightColor = 0xff0000; break; //red
case 7: nHighlightColor = 0xffff00; break; //yellow
case 8: nHighlightColor = 0xffffff; break; //white
case 9: nHighlightColor = 0x000080; break;//dark blue
case 10: nHighlightColor = 0x008080; break; //dark cyan
case 11: nHighlightColor = 0x008000; break; //dark green
case 12: nHighlightColor = 0x800080; break; //dark magenta
case 13: nHighlightColor = 0x800000; break; //dark red
case 14: nHighlightColor = 0x808000; break; //dark yellow
case 15: nHighlightColor = 0x808080; break; //dark gray
case 16: nHighlightColor = 0xC0C0C0; break; //light gray
}
if( strcmp(aFilterNames[nFilter], "writer8") == 0 )
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), nHighlightColor, getProperty<sal_Int32>(xRun,"CharBackColor"));
}
else // MS filters
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), nHighlightColor, getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), nBackColor, getProperty<sal_Int32>(xRun,"CharBackColor"));
}
}
// Only highlight
{
const uno::Reference<beans::XPropertySet> xRun(getRun(xPara,18), uno::UNO_QUERY);
if( strcmp(aFilterNames[nFilter], "writer8") == 0 )
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0xff0000), getProperty<sal_Int32>(xRun,"CharBackColor"));
}
else
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0xff0000), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharBackColor"));
}
}
// Only background
{
const uno::Reference<beans::XPropertySet> xRun(getRun(xPara,19), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0x0000ff), getProperty<sal_Int32>(xRun,"CharBackColor"));
}
}
}
void Test::testCharHighlight()
{
SvtFilterOptions& rOpt = SvtFilterOptions::Get();
rOpt.SetCharBackground2Shading();
testCharHighlightBody();
rOpt.SetCharBackground2Highlighting();
testCharHighlightBody();
}
void Test::testCharHighlightODF()
{
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/char_background_editing.docx"),
"com.sun.star.text.TextDocument");
// don't check import, testMSCharBackgroundEditing already does that
uno::Reference<text::XTextRange> xPara = getParagraph(1);
for (int i = 1; i <= 4; ++i)
{
uno::Reference<beans::XPropertySet> xRun(getRun(xPara,i), uno::UNO_QUERY);
switch (i)
{
case 1: // non-transparent highlight
xRun->setPropertyValue("CharBackColor", uno::makeAny(static_cast<sal_Int32>(128)));
xRun->setPropertyValue("CharBackTransparent", uno::makeAny(true));
xRun->setPropertyValue("CharHighlight", uno::makeAny(static_cast<sal_Int32>(64)));
break;
case 2: // transparent backcolor
xRun->setPropertyValue("CharBackColor", uno::makeAny(static_cast<sal_Int32>(128)));
xRun->setPropertyValue("CharBackTransparent", uno::makeAny(true));
xRun->setPropertyValue("CharHighlight", uno::makeAny(static_cast<sal_Int32>(COL_TRANSPARENT)));
break;
case 3: // non-transparent backcolor
xRun->setPropertyValue("CharBackColor", uno::makeAny(static_cast<sal_Int32>(128)));
xRun->setPropertyValue("CharBackTransparent", uno::makeAny(false));
xRun->setPropertyValue("CharHighlight", uno::makeAny(static_cast<sal_Int32>(COL_TRANSPARENT)));
break;
case 4: // non-transparent highlight again
xRun->setPropertyValue("CharBackColor", uno::makeAny(static_cast<sal_Int32>(128)));
xRun->setPropertyValue("CharBackTransparent", uno::makeAny(false));
xRun->setPropertyValue("CharHighlight", uno::makeAny(static_cast<sal_Int32>(64)));
break;
}
}
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString("writer8");
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
xPara.set(getParagraph(1));
for (int i = 1; i <= 4; ++i)
{
uno::Reference<beans::XPropertySet> xRun(getRun(xPara,i), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun, "CharHighlight"));
switch (i)
{
case 1: // non-transparent highlight
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(64), getProperty<sal_Int32>(xRun, "CharBackColor"));
CPPUNIT_ASSERT_EQUAL(sal_False, getProperty<sal_Bool>(xRun, "CharBackTransparent"));
break;
case 2: // transparent backcolor
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun, "CharBackColor"));
CPPUNIT_ASSERT_EQUAL(sal_True, getProperty<sal_Bool>(xRun, "CharBackTransparent"));
break;
case 3: // non-transparent backcolor
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(128), getProperty<sal_Int32>(xRun, "CharBackColor"));
CPPUNIT_ASSERT_EQUAL(sal_False, getProperty<sal_Bool>(xRun, "CharBackTransparent"));
break;
case 4: // non-transparent highlight again
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(64), getProperty<sal_Int32>(xRun, "CharBackColor"));
CPPUNIT_ASSERT_EQUAL(sal_False, getProperty<sal_Bool>(xRun, "CharBackTransparent"));
break;
}
}
}
void Test::testMSCharBackgroundEditing()
{
// Simulate the editing process of imported MSO character background attributes
// and check how export behaves.
const char* aFilterNames[] = {
"writer8",
"Rich Text Format",
"MS Word 97",
"Office Open XML Text",
};
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/char_background_editing.docx"),
"com.sun.star.text.TextDocument");
const OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter];
// Check whether import was done on the right way
uno::Reference< text::XTextRange > xPara = getParagraph(1);
{
uno::Reference<beans::XPropertySet> xRun(getRun(xPara,1), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0xff0000), getProperty<sal_Int32>(xRun,"CharBackColor"));
xRun.set(getRun(xPara,2), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0x0000ff), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharBackColor"));
xRun.set(getRun(xPara,3), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0x0000ff), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(0xff0000), getProperty<sal_Int32>(xRun,"CharBackColor"));
xRun.set(getRun(xPara,4), uno::UNO_QUERY);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharBackColor"));
}
// Simulate editing
for( int i = 1; i <= 4; ++i )
{
uno::Reference<beans::XPropertySet> xRun(getRun(xPara,i), uno::UNO_QUERY);
// Change background
sal_Int32 nBackColor = 0;
switch( i )
{
case 1: nBackColor = 0x000000; break; //black
case 2: nBackColor = 0x00ffff; break; //cyan
case 3: nBackColor = 0x00ff00; break; //green
case 4: nBackColor = 0xff00ff; break; //magenta
}
xRun->setPropertyValue("CharBackColor", uno::makeAny(static_cast<sal_Int32>(nBackColor)));
// Remove highlighting
xRun->setPropertyValue("CharHighlight", uno::makeAny(static_cast<sal_Int32>(COL_TRANSPARENT)));
// Remove shading marker
uno::Sequence<beans::PropertyValue> aGrabBag = getProperty<uno::Sequence<beans::PropertyValue> >(xRun,"CharInteropGrabBag");
for (int j = 0; j < aGrabBag.getLength(); ++j)
{
beans::PropertyValue& rProp = aGrabBag[j];
if (rProp.Name == "CharShadingMarker")
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), true, rProp.Value.get<bool>());
rProp.Value = uno::makeAny(false);
}
}
xRun->setPropertyValue("CharInteropGrabBag", uno::makeAny(aGrabBag));
}
SvtFilterOptions& rOpt = SvtFilterOptions::Get();
rOpt.SetCharBackground2Highlighting();
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString::createFromAscii(aFilterNames[nFilter]);
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
// Check whether background was exported as highlighting
xPara.set(getParagraph(1));
for( int i = 1; i <= 4; ++i )
{
sal_Int32 nBackColor = 0;
switch( i )
{
case 1: nBackColor = 0x000000; break; //black
case 2: nBackColor = 0x00ffff; break; //cyan
case 3: nBackColor = 0x00ff00; break; //green
case 4: nBackColor = 0xff00ff; break; //magenta
}
const uno::Reference<beans::XPropertySet> xRun(getRun(xPara,i), uno::UNO_QUERY);
if( strcmp(aFilterNames[nFilter], "writer8") == 0 )
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(nBackColor), getProperty<sal_Int32>(xRun,"CharBackColor"));
}
else
{
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(nBackColor), getProperty<sal_Int32>(xRun,"CharHighlight"));
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(COL_TRANSPARENT), getProperty<sal_Int32>(xRun,"CharBackColor"));
}
}
}
}
void Test::testCharBackgroundToHighlighting()
{
// MSO highlighting has less kind of values so let's see how LO character background is converted
// to these values
const char* aFilterNames[] = {
"Rich Text Format",
"MS Word 97",
"Office Open XML Text",
};
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
if (mxComponent.is())
mxComponent->dispose();
mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/char_background.odt"),
"com.sun.star.text.TextDocument");
OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter];
SvtFilterOptions& rOpt = SvtFilterOptions::Get();
rOpt.SetCharBackground2Highlighting();
// Export the document and import again for a check
uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
utl::MediaDescriptor aMediaDescriptor;
aMediaDescriptor["FilterName"] <<= OUString::createFromAscii(aFilterNames[nFilter]);
utl::TempFile aTempFile;
aTempFile.EnableKillingFile();
xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
uno::Reference< lang::XComponent > xComponent(xStorable, uno::UNO_QUERY);
xComponent->dispose();
mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
// Check highlight color
const uno::Reference< text::XTextRange > xPara = getParagraph(1);
for( int nRun = 1; nRun <= 19; ++nRun )
{
const uno::Reference<beans::XPropertySet> xRun(getRun(xPara,nRun), uno::UNO_QUERY);
sal_Int32 nHighlightColor = 0;
switch( nRun )
{
case 1: nHighlightColor = 0x000000; break; //black
case 2: nHighlightColor = 0xffff00; break; //yellow
case 3: nHighlightColor = 0xff00ff; break; //magenta
case 4: nHighlightColor = 0x00ffff; break; //cyan
case 5: nHighlightColor = 0xffff00; break; //yellow
case 6: nHighlightColor = 0xff0000; break; //red
case 7: nHighlightColor = 0x0000ff; break; //blue
case 8: nHighlightColor = 0x00ff00; break; //green
case 9: nHighlightColor = 0x008000; break; //dark green
case 10: nHighlightColor = 0x800080; break; //dark magenta
case 11: nHighlightColor = 0x000080; break; //dark blue
case 12: nHighlightColor = 0x808000; break; //dark yellow
case 13: nHighlightColor = 0x808080; break; //dark gray
case 14: nHighlightColor = 0x000000; break; //black
case 15: nHighlightColor = 0xff0000; break; //red
case 16: nHighlightColor = 0xC0C0C0; break; //light gray
case 17: nHighlightColor = 0x800000; break; //dark red
case 18: nHighlightColor = 0x808080; break; //dark gray
case 19: nHighlightColor = 0xffff00; break; //yellow
}
const OString sMessage = sFailedMessage +". Index of run with unmatched color: " + OString::number(nRun);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sMessage.getStr(), nHighlightColor, getProperty<sal_Int32>(xRun,"CharHighlight"));
}
}
}
#if !defined(_WIN32)
void Test::testSkipImages()
{
// Check how LO skips image loading (but not texts of textboxes and custom shapes)
// during DOC and DOCX import, using the "SkipImages" FilterOptions.
const char* aFilterNames[][2] = {
{ "/sw/qa/extras/globalfilter/data/skipimages.doc", nullptr },
{ "/sw/qa/extras/globalfilter/data/skipimages.doc", "SkipImages" },
{ "/sw/qa/extras/globalfilter/data/skipimages.docx", nullptr },
{ "/sw/qa/extras/globalfilter/data/skipimages.docx", "SkipImages" }
};
for( size_t nFilter = 0; nFilter < SAL_N_ELEMENTS(aFilterNames); ++nFilter )
{
bool bSkipImages = aFilterNames[nFilter][1] != nullptr;
OString sFailedMessage = OString("Failed on filter: ") + aFilterNames[nFilter][0];
if (mxComponent.is())
mxComponent->dispose();
if (bSkipImages)
{
// FilterOptions parameter
uno::Sequence<beans::PropertyValue> args(1);
args[0].Name = "FilterOptions";
args[0].Handle = -1;
args[0].Value <<= OUString::createFromAscii(aFilterNames[nFilter][1]);
args[0].State = beans::PropertyState_DIRECT_VALUE;
mxComponent = loadFromDesktop(m_directories.getURLFromSrc(aFilterNames[nFilter][0]), "com.sun.star.text.TextDocument", args);
sFailedMessage = sFailedMessage + " - " + aFilterNames[nFilter][1];
} else
mxComponent = loadFromDesktop(m_directories.getURLFromSrc(aFilterNames[nFilter][0]), "com.sun.star.text.TextDocument");
// Check shapes (images, textboxes, custom shapes)
uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XIndexAccess> xDraws(xDrawPageSupplier->getDrawPage(), uno::UNO_QUERY);
uno::Reference<drawing::XShape> xShape;
uno::Reference<graphic::XGraphic> xGraphic;
uno::Reference< beans::XPropertySet > XPropSet;
uno::Reference<awt::XBitmap> xBitmap;
bool bHasTextboxText = false;
bool bHasCustomShapeText = false;
sal_Int32 nImageCount = 0;
for (int i = 1; i<= xDraws->getCount(); i++)
{
xShape = getShape(i);
XPropSet.set( xShape, uno::UNO_QUERY_THROW );
try
{
XPropSet->getPropertyValue("Graphic") >>= xGraphic;
xBitmap.set(xGraphic, uno::UNO_QUERY);
if (xBitmap.is())
nImageCount++;
}
catch (beans::UnknownPropertyException &)
{ /* ignore */ }
uno::Reference<text::XTextRange> xText(xShape, uno::UNO_QUERY);
if (xText.is())
{
OUString shapeText = xText->getString();
if (shapeText.startsWith("Lorem ipsum"))
bHasTextboxText = true;
else if (shapeText.startsWith("Nam pretium"))
bHasCustomShapeText = true;
}
}
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), bHasTextboxText);
CPPUNIT_ASSERT_MESSAGE(sFailedMessage.getStr(), bHasCustomShapeText);
CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), static_cast<sal_Int32>(bSkipImages ? 0 : 3), nImageCount );
}
}
#endif
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|