1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
12 #include <string_view>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <unotest/bootstrapfixturebase.hxx>
18 #include <graphic/GraphicFormatDetector.hxx>
19 #include <graphic/DetectorTools.hxx>
21 #include <tools/stream.hxx>
22 #include <o3tl/string_view.hxx>
28 class GraphicFormatDetectorTest
: public test::BootstrapFixtureBase
30 OUString
getFullUrl(std::u16string_view sFileName
)
32 return m_directories
.getURLFromSrc(u
"/vcl/qa/cppunit/data/") + sFileName
;
49 void testDetectSVGZ();
52 void testDetectWEBP();
55 void testMatchArray();
56 void testCheckArrayForMatchingStrings();
58 CPPUNIT_TEST_SUITE(GraphicFormatDetectorTest
);
59 CPPUNIT_TEST(testDetectMET
);
60 CPPUNIT_TEST(testDetectBMP
);
61 CPPUNIT_TEST(testDetectWMF
);
62 CPPUNIT_TEST(testDetectWMZ
);
63 CPPUNIT_TEST(testDetectPCX
);
64 CPPUNIT_TEST(testDetectJPG
);
65 CPPUNIT_TEST(testDetectPNG
);
66 CPPUNIT_TEST(testDetectGIF
);
67 CPPUNIT_TEST(testDetectPSD
);
68 CPPUNIT_TEST(testDetectTGA
);
69 CPPUNIT_TEST(testDetectTIF
);
70 CPPUNIT_TEST(testDetectXBM
);
71 CPPUNIT_TEST(testDetectXPM
);
72 CPPUNIT_TEST(testDetectSVG
);
73 CPPUNIT_TEST(testDetectSVGZ
);
74 CPPUNIT_TEST(testDetectPDF
);
75 CPPUNIT_TEST(testDetectEPS
);
76 CPPUNIT_TEST(testDetectWEBP
);
77 CPPUNIT_TEST(testDetectEMF
);
78 CPPUNIT_TEST(testDetectEMZ
);
79 CPPUNIT_TEST(testMatchArray
);
80 CPPUNIT_TEST(testCheckArrayForMatchingStrings
);
81 CPPUNIT_TEST_SUITE_END();
84 void GraphicFormatDetectorTest::testDetectMET()
86 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.met"), StreamMode::READ
);
87 vcl::GraphicFormatDetector
aDetector(aFileStream
, "MET");
89 CPPUNIT_ASSERT(aDetector
.detect());
90 CPPUNIT_ASSERT(aDetector
.checkMET());
92 aFileStream
.Seek(aDetector
.mnStreamPosition
);
94 OUString rFormatExtension
;
95 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
96 CPPUNIT_ASSERT_EQUAL(OUString("MET"), rFormatExtension
);
99 void GraphicFormatDetectorTest::testDetectBMP()
101 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.bmp"), StreamMode::READ
);
102 vcl::GraphicFormatDetector
aDetector(aFileStream
, "BMP");
104 CPPUNIT_ASSERT(aDetector
.detect());
105 CPPUNIT_ASSERT(aDetector
.checkBMP());
107 aFileStream
.Seek(aDetector
.mnStreamPosition
);
109 OUString rFormatExtension
;
110 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
111 CPPUNIT_ASSERT_EQUAL(OUString("BMP"), rFormatExtension
);
114 void GraphicFormatDetectorTest::testDetectWMF()
116 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.wmf"), StreamMode::READ
);
117 vcl::GraphicFormatDetector
aDetector(aFileStream
, "WMF");
119 CPPUNIT_ASSERT(aDetector
.detect());
120 CPPUNIT_ASSERT(aDetector
.checkWMF());
122 aFileStream
.Seek(aDetector
.mnStreamPosition
);
124 OUString rFormatExtension
;
125 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
126 CPPUNIT_ASSERT_EQUAL(OUString("WMF"), rFormatExtension
);
129 void GraphicFormatDetectorTest::testDetectWMZ()
131 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.wmz"), StreamMode::READ
);
132 vcl::GraphicFormatDetector
aDetector(aFileStream
, "WMZ");
134 CPPUNIT_ASSERT(aDetector
.detect());
135 CPPUNIT_ASSERT(aDetector
.checkWMF());
137 aFileStream
.Seek(aDetector
.mnStreamPosition
);
139 OUString rFormatExtension
;
140 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
141 CPPUNIT_ASSERT_EQUAL(OUString("WMZ"), rFormatExtension
);
144 void GraphicFormatDetectorTest::testDetectPCX()
146 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.pcx"), StreamMode::READ
);
147 vcl::GraphicFormatDetector
aDetector(aFileStream
, "PCX");
149 CPPUNIT_ASSERT(aDetector
.detect());
150 CPPUNIT_ASSERT(aDetector
.checkPCX());
152 aFileStream
.Seek(aDetector
.mnStreamPosition
);
154 OUString rFormatExtension
;
155 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
156 CPPUNIT_ASSERT_EQUAL(OUString("PCX"), rFormatExtension
);
159 void GraphicFormatDetectorTest::testDetectJPG()
161 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.jpg"), StreamMode::READ
);
162 vcl::GraphicFormatDetector
aDetector(aFileStream
, "JPG");
164 CPPUNIT_ASSERT(aDetector
.detect());
165 CPPUNIT_ASSERT(aDetector
.checkJPG());
167 aFileStream
.Seek(aDetector
.mnStreamPosition
);
169 OUString rFormatExtension
;
170 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
171 CPPUNIT_ASSERT_EQUAL(OUString("JPG"), rFormatExtension
);
174 void GraphicFormatDetectorTest::testDetectPNG()
176 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.png"), StreamMode::READ
);
177 vcl::GraphicFormatDetector
aDetector(aFileStream
, "PNG");
179 CPPUNIT_ASSERT(aDetector
.detect());
180 CPPUNIT_ASSERT(aDetector
.checkPNG());
182 aFileStream
.Seek(aDetector
.mnStreamPosition
);
184 OUString rFormatExtension
;
185 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
186 CPPUNIT_ASSERT_EQUAL(OUString("PNG"), rFormatExtension
);
189 void GraphicFormatDetectorTest::testDetectGIF()
191 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.gif"), StreamMode::READ
);
192 vcl::GraphicFormatDetector
aDetector(aFileStream
, "GIF");
194 CPPUNIT_ASSERT(aDetector
.detect());
195 CPPUNIT_ASSERT(aDetector
.checkGIF());
197 aFileStream
.Seek(aDetector
.mnStreamPosition
);
199 OUString rFormatExtension
;
200 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
201 CPPUNIT_ASSERT_EQUAL(OUString("GIF"), rFormatExtension
);
204 void GraphicFormatDetectorTest::testDetectPSD()
206 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.psd"), StreamMode::READ
);
207 vcl::GraphicFormatDetector
aDetector(aFileStream
, "PSD");
209 CPPUNIT_ASSERT(aDetector
.detect());
210 CPPUNIT_ASSERT(aDetector
.checkPSD());
212 aFileStream
.Seek(aDetector
.mnStreamPosition
);
214 OUString rFormatExtension
;
215 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
216 CPPUNIT_ASSERT_EQUAL(OUString("PSD"), rFormatExtension
);
219 void GraphicFormatDetectorTest::testDetectTGA()
221 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.tga"), StreamMode::READ
);
222 vcl::GraphicFormatDetector
aDetector(aFileStream
, "TGA");
224 CPPUNIT_ASSERT(aDetector
.detect());
225 CPPUNIT_ASSERT(aDetector
.checkTGA());
227 aFileStream
.Seek(aDetector
.mnStreamPosition
);
229 OUString
rFormatExtension("TGA"); // detection is based on extension only
230 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
231 CPPUNIT_ASSERT_EQUAL(OUString("TGA"), rFormatExtension
);
234 void GraphicFormatDetectorTest::testDetectTIF()
236 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.tif"), StreamMode::READ
);
237 vcl::GraphicFormatDetector
aDetector(aFileStream
, "TIF");
239 CPPUNIT_ASSERT(aDetector
.detect());
240 CPPUNIT_ASSERT(aDetector
.checkTIF());
242 aFileStream
.Seek(aDetector
.mnStreamPosition
);
244 OUString rFormatExtension
;
245 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
246 CPPUNIT_ASSERT_EQUAL(OUString("TIF"), rFormatExtension
);
249 void GraphicFormatDetectorTest::testDetectXBM()
251 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.xbm"), StreamMode::READ
);
252 vcl::GraphicFormatDetector
aDetector(aFileStream
, "XBM");
254 CPPUNIT_ASSERT(aDetector
.detect());
255 CPPUNIT_ASSERT(aDetector
.checkXBM());
257 aFileStream
.Seek(aDetector
.mnStreamPosition
);
259 OUString rFormatExtension
;
260 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
261 CPPUNIT_ASSERT_EQUAL(OUString("XBM"), rFormatExtension
);
264 void GraphicFormatDetectorTest::testDetectXPM()
266 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.xpm"), StreamMode::READ
);
267 vcl::GraphicFormatDetector
aDetector(aFileStream
, "XPM");
269 CPPUNIT_ASSERT(aDetector
.detect());
270 CPPUNIT_ASSERT(aDetector
.checkXPM());
272 aFileStream
.Seek(aDetector
.mnStreamPosition
);
274 OUString rFormatExtension
;
275 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
276 CPPUNIT_ASSERT_EQUAL(OUString("XPM"), rFormatExtension
);
279 void GraphicFormatDetectorTest::testDetectSVG()
281 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.svg"), StreamMode::READ
);
282 vcl::GraphicFormatDetector
aDetector(aFileStream
, "SVG");
284 CPPUNIT_ASSERT(aDetector
.detect());
285 CPPUNIT_ASSERT(aDetector
.checkSVG());
287 aFileStream
.Seek(aDetector
.mnStreamPosition
);
289 OUString rFormatExtension
;
290 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
291 CPPUNIT_ASSERT_EQUAL(OUString("SVG"), rFormatExtension
);
294 void GraphicFormatDetectorTest::testDetectSVGZ()
296 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.svgz"), StreamMode::READ
);
297 vcl::GraphicFormatDetector
aDetector(aFileStream
, "SVGZ");
299 CPPUNIT_ASSERT(aDetector
.detect());
300 CPPUNIT_ASSERT(aDetector
.checkSVG());
302 aFileStream
.Seek(aDetector
.mnStreamPosition
);
304 OUString rFormatExtension
;
305 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
306 CPPUNIT_ASSERT_EQUAL(OUString("SVGZ"), rFormatExtension
);
309 void GraphicFormatDetectorTest::testDetectPDF()
311 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.pdf"), StreamMode::READ
);
312 vcl::GraphicFormatDetector
aDetector(aFileStream
, "PDF");
314 CPPUNIT_ASSERT(aDetector
.detect());
315 CPPUNIT_ASSERT(aDetector
.checkPDF());
317 aFileStream
.Seek(aDetector
.mnStreamPosition
);
319 OUString rFormatExtension
;
320 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
321 CPPUNIT_ASSERT_EQUAL(OUString("PDF"), rFormatExtension
);
324 void GraphicFormatDetectorTest::testDetectEPS()
326 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.eps"), StreamMode::READ
);
327 vcl::GraphicFormatDetector
aDetector(aFileStream
, "EPS");
329 CPPUNIT_ASSERT(aDetector
.detect());
330 CPPUNIT_ASSERT(aDetector
.checkEPS());
332 aFileStream
.Seek(aDetector
.mnStreamPosition
);
334 OUString rFormatExtension
;
335 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
336 CPPUNIT_ASSERT_EQUAL(OUString("EPS"), rFormatExtension
);
339 void GraphicFormatDetectorTest::testDetectWEBP()
341 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.webp"), StreamMode::READ
);
342 vcl::GraphicFormatDetector
aDetector(aFileStream
, "WEBP");
344 CPPUNIT_ASSERT(aDetector
.detect());
345 CPPUNIT_ASSERT(aDetector
.checkWEBP());
347 aFileStream
.Seek(aDetector
.mnStreamPosition
);
349 OUString rFormatExtension
;
350 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
351 CPPUNIT_ASSERT_EQUAL(OUString("WEBP"), rFormatExtension
);
354 void GraphicFormatDetectorTest::testDetectEMF()
356 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.emf"), StreamMode::READ
);
357 vcl::GraphicFormatDetector
aDetector(aFileStream
, "EMF");
359 CPPUNIT_ASSERT(aDetector
.detect());
360 CPPUNIT_ASSERT(aDetector
.checkEMF());
362 aFileStream
.Seek(aDetector
.mnStreamPosition
);
364 OUString rFormatExtension
;
365 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
366 CPPUNIT_ASSERT_EQUAL(OUString("EMF"), rFormatExtension
);
369 void GraphicFormatDetectorTest::testDetectEMZ()
371 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.emz"), StreamMode::READ
);
372 vcl::GraphicFormatDetector
aDetector(aFileStream
, "EMZ");
374 CPPUNIT_ASSERT(aDetector
.detect());
375 CPPUNIT_ASSERT(aDetector
.checkEMF());
377 aFileStream
.Seek(aDetector
.mnStreamPosition
);
379 OUString rFormatExtension
;
380 CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream
, rFormatExtension
, false));
381 CPPUNIT_ASSERT_EQUAL(OUString("EMZ"), rFormatExtension
);
384 void GraphicFormatDetectorTest::testMatchArray()
386 std::string
aString("<?xml version=\"1.0\" standalone=\"no\"?>\n"
387 "<svg width=\"5cm\" height=\"4cm\" version=\"1.1\"\n"
388 "xmlns=\"http://www.w3.org/2000/svg\">\n"
391 const char* pCompleteStringPointer
= aString
.c_str();
392 const char* pMatchPointer
;
393 int nCheckSize
= aString
.size();
395 // Check beginning of the input string
396 pMatchPointer
= vcl::matchArrayWithString(pCompleteStringPointer
, nCheckSize
, "<?xml");
397 CPPUNIT_ASSERT(pMatchPointer
!= nullptr);
398 CPPUNIT_ASSERT_EQUAL(0, int(pMatchPointer
- pCompleteStringPointer
));
399 CPPUNIT_ASSERT_EQUAL(true, o3tl::starts_with(pMatchPointer
, "<?xml"));
401 // Check middle of the input string
402 pMatchPointer
= vcl::matchArrayWithString(aString
.c_str(), nCheckSize
, "version");
403 CPPUNIT_ASSERT(pMatchPointer
!= nullptr);
404 CPPUNIT_ASSERT_EQUAL(6, int(pMatchPointer
- pCompleteStringPointer
));
405 CPPUNIT_ASSERT_EQUAL(true, o3tl::starts_with(pMatchPointer
, "version"));
407 pMatchPointer
= vcl::matchArrayWithString(aString
.c_str(), nCheckSize
, "<svg");
408 CPPUNIT_ASSERT(pMatchPointer
!= nullptr);
409 CPPUNIT_ASSERT_EQUAL(38, int(pMatchPointer
- pCompleteStringPointer
));
410 CPPUNIT_ASSERT_EQUAL(true, o3tl::starts_with(pMatchPointer
, "<svg"));
412 // Check end of the input string
413 pMatchPointer
= vcl::matchArrayWithString(aString
.c_str(), nCheckSize
, "/svg>");
414 CPPUNIT_ASSERT(pMatchPointer
!= nullptr);
415 CPPUNIT_ASSERT_EQUAL(119, int(pMatchPointer
- pCompleteStringPointer
));
416 CPPUNIT_ASSERT_EQUAL(true, o3tl::starts_with(pMatchPointer
, "/svg>"));
418 // Check that non-existing search string
419 pMatchPointer
= vcl::matchArrayWithString(aString
.c_str(), nCheckSize
, "none");
420 CPPUNIT_ASSERT(pMatchPointer
== nullptr);
423 void GraphicFormatDetectorTest::testCheckArrayForMatchingStrings()
425 std::string
aString("<?xml version=\"1.0\" standalone=\"no\"?>\n"
426 "<svg width=\"5cm\" height=\"4cm\" version=\"1.1\"\n"
427 "xmlns=\"http://www.w3.org/2000/svg\">\n"
429 const char* pCompleteStringPointer
= aString
.c_str();
430 int nCheckSize
= aString
.size();
433 // check beginning string
434 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
, { "<?xml" });
435 CPPUNIT_ASSERT_EQUAL(true, bResult
);
437 // check ending string
438 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
, { "/svg>" });
439 CPPUNIT_ASSERT_EQUAL(true, bResult
);
441 // check middle string
442 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
, { "version" });
443 CPPUNIT_ASSERT_EQUAL(true, bResult
);
445 // check beginning and then ending string
446 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
447 { "<?xml", "/svg>" });
448 CPPUNIT_ASSERT_EQUAL(true, bResult
);
450 // check ending and then beginning string
451 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
452 { "/svg>", "<?xml" });
453 CPPUNIT_ASSERT_EQUAL(false, bResult
);
455 // check middle strings
456 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
457 { "version", "<svg" });
458 CPPUNIT_ASSERT_EQUAL(true, bResult
);
460 // check beginning, middle and ending strings
461 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
462 { "<?xml", "version", "<svg", "/svg>" });
463 CPPUNIT_ASSERT_EQUAL(true, bResult
);
465 // check non-existing
466 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
, { "none" });
467 CPPUNIT_ASSERT_EQUAL(false, bResult
);
469 // check non-existing on the beginning
470 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
471 { "none", "version", "<svg", "/svg>" });
472 CPPUNIT_ASSERT_EQUAL(false, bResult
);
474 // check non-existing on the end
475 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
476 { "<?xml", "version", "<svg", "none" });
477 CPPUNIT_ASSERT_EQUAL(false, bResult
);
479 // check non-existing after the end
480 bResult
= vcl::checkArrayForMatchingStrings(pCompleteStringPointer
, nCheckSize
,
481 { "<?xml", "/svg>", "none" });
482 CPPUNIT_ASSERT_EQUAL(false, bResult
);
487 CPPUNIT_TEST_SUITE_REGISTRATION(GraphicFormatDetectorTest
);
489 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */