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 <unotest/bootstrapfixturebase.hxx>
12 #include <cppunit/TestAssert.h>
13 #include <cppunit/TestFixture.h>
14 #include <cppunit/extensions/HelperMacros.h>
16 #include <com/sun/star/uno/Sequence.hxx>
17 #include <com/sun/star/beans/PropertyValue.hpp>
19 #include <vcl/graphicfilter.hxx>
20 #include <tools/stream.hxx>
26 class GraphicDescriptorTest
: public test::BootstrapFixtureBase
28 OUString
getFullUrl(std::u16string_view sFileName
)
30 return m_directories
.getURLFromSrc(u
"/vcl/qa/cppunit/data/") + sFileName
;
37 void testDetectWEBP();
41 CPPUNIT_TEST_SUITE(GraphicDescriptorTest
);
42 CPPUNIT_TEST(testDetectPNG
);
43 CPPUNIT_TEST(testDetectJPG
);
44 CPPUNIT_TEST(testDetectGIF
);
45 CPPUNIT_TEST(testDetectTIF
);
46 CPPUNIT_TEST(testDetectBMP
);
47 CPPUNIT_TEST(testDetectWEBP
);
48 CPPUNIT_TEST(testDetectEMF
);
49 CPPUNIT_TEST(testDetectWMF
);
50 CPPUNIT_TEST_SUITE_END();
53 BitmapEx
createBitmap()
55 Bitmap
aBitmap(Size(100, 100), vcl::PixelFormat::N24_BPP
);
56 aBitmap
.Erase(COL_LIGHTRED
);
58 return BitmapEx(aBitmap
);
61 void createBitmapAndExportForType(SvStream
& rStream
, std::u16string_view sType
)
63 BitmapEx aBitmapEx
= createBitmap();
65 uno::Sequence
<beans::PropertyValue
> aFilterData
;
66 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
67 sal_uInt16 nFilterFormat
= rGraphicFilter
.GetExportFormatNumberForShortName(sType
);
68 rGraphicFilter
.ExportGraphic(aBitmapEx
, u
"none", rStream
, nFilterFormat
, &aFilterData
);
70 rStream
.Seek(STREAM_SEEK_TO_BEGIN
);
73 void GraphicDescriptorTest::testDetectPNG()
75 SvMemoryStream aStream
;
76 createBitmapAndExportForType(aStream
, u
"png");
78 GraphicDescriptor
aDescriptor(aStream
, nullptr);
79 aDescriptor
.Detect(true);
81 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::PNG
, aDescriptor
.GetFileFormat());
83 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Width());
84 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Height());
87 void GraphicDescriptorTest::testDetectJPG()
89 SvMemoryStream aStream
;
90 createBitmapAndExportForType(aStream
, u
"jpg");
92 GraphicDescriptor
aDescriptor(aStream
, nullptr);
93 aDescriptor
.Detect(true);
95 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::JPG
, aDescriptor
.GetFileFormat());
97 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Width());
98 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Height());
101 void GraphicDescriptorTest::testDetectGIF()
103 SvMemoryStream aStream
;
104 createBitmapAndExportForType(aStream
, u
"gif");
106 GraphicDescriptor
aDescriptor(aStream
, nullptr);
107 aDescriptor
.Detect(true);
109 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::GIF
, aDescriptor
.GetFileFormat());
111 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Width());
112 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Height());
115 void GraphicDescriptorTest::testDetectTIF()
117 SvMemoryStream aStream
;
118 createBitmapAndExportForType(aStream
, u
"tif");
120 GraphicDescriptor
aDescriptor(aStream
, nullptr);
121 aDescriptor
.Detect(true);
123 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::TIF
, aDescriptor
.GetFileFormat());
125 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Width());
126 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Height());
129 void GraphicDescriptorTest::testDetectBMP()
131 GraphicFilter
& rGraphicFilter
= GraphicFilter::GetGraphicFilter();
132 SvFileStream
aFileStream(getFullUrl(u
"graphic-descriptor-mapmode.bmp"), StreamMode::READ
);
134 Graphic aGraphic
= rGraphicFilter
.ImportUnloadedGraphic(aFileStream
);
136 CPPUNIT_ASSERT(!aGraphic
.isAvailable());
137 // Without the accompanying fix in place, this test would have failed with:
138 // - Expected: 2 (MapUnit::MapMM)
139 // - Actual : 0 (MapUnit::Map100thMM)
140 // i.e. lazy load and load created different map modes, breaking the contour polygon code in
142 CPPUNIT_ASSERT_EQUAL(MapUnit::MapMM
, aGraphic
.GetPrefMapMode().GetMapUnit());
143 aGraphic
.makeAvailable();
144 CPPUNIT_ASSERT_EQUAL(MapUnit::MapMM
, aGraphic
.GetPrefMapMode().GetMapUnit());
147 void GraphicDescriptorTest::testDetectWEBP()
149 SvMemoryStream aStream
;
150 createBitmapAndExportForType(aStream
, u
"webp");
152 GraphicDescriptor
aDescriptor(aStream
, nullptr);
153 aDescriptor
.Detect(true);
155 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::WEBP
, aDescriptor
.GetFileFormat());
157 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Width());
158 CPPUNIT_ASSERT_EQUAL(tools::Long(100), aDescriptor
.GetSizePixel().Height());
161 void GraphicDescriptorTest::testDetectEMF()
163 SvFileStream
aFileStream(getFullUrl(u
"TypeDetectionExample.emf"), StreamMode::READ
);
164 GraphicDescriptor
aDescriptor(aFileStream
, nullptr);
165 aDescriptor
.Detect(true);
166 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::EMF
, aDescriptor
.GetFileFormat());
167 // Test that Bounds/Frame values are fetched from header
168 CPPUNIT_ASSERT_EQUAL(tools::Long(142), aDescriptor
.GetSizePixel().Width());
169 CPPUNIT_ASSERT_EQUAL(tools::Long(142), aDescriptor
.GetSizePixel().Height());
170 CPPUNIT_ASSERT_EQUAL(tools::Long(300), aDescriptor
.GetSize_100TH_MM().Width());
171 CPPUNIT_ASSERT_EQUAL(tools::Long(300), aDescriptor
.GetSize_100TH_MM().Height());
174 void GraphicDescriptorTest::testDetectWMF()
176 // Test placeable wmf
178 SvFileStream
aFileStream(m_directories
.getURLFromSrc(u
"/emfio/qa/cppunit/wmf/data/")
179 + "tdf88163-wrong-font-size.wmf",
181 GraphicDescriptor
aDescriptor(aFileStream
, nullptr);
182 aDescriptor
.Detect(true);
183 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::WMF
, aDescriptor
.GetFileFormat());
185 // Test non-placeable wmf
187 SvFileStream
aFileStream(m_directories
.getURLFromSrc(u
"/emfio/qa/cppunit/wmf/data/")
188 + "tdf88163-non-placeable.wmf",
190 GraphicDescriptor
aDescriptor(aFileStream
, nullptr);
191 aDescriptor
.Detect(true);
192 CPPUNIT_ASSERT_EQUAL(GraphicFileFormat::WMF
, aDescriptor
.GetFileFormat());
198 CPPUNIT_TEST_SUITE_REGISTRATION(GraphicDescriptorTest
);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */