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 <test/bootstrapfixture.hxx>
16 #include <com/sun/star/graphic/XGraphic.hpp>
17 #include <com/sun/star/graphic/GraphicType.hpp>
18 #include <com/sun/star/graphic/GraphicProvider.hpp>
19 #include <com/sun/star/graphic/XGraphicProvider.hpp>
20 #include <com/sun/star/awt/Size.hpp>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/propertysequence.hxx>
29 constexpr OUStringLiteral gaDataUrl
= u
"/vcl/qa/api/data/";
31 class XGraphicTest
: public test::BootstrapFixture
35 : BootstrapFixture(true, false)
39 OUString
getFullUrl(std::u16string_view sFileName
)
41 return m_directories
.getURLFromSrc(gaDataUrl
) + sFileName
;
45 void testGraphicDescriptor();
46 void testGraphicProvider();
48 CPPUNIT_TEST_SUITE(XGraphicTest
);
49 CPPUNIT_TEST(testGraphic
);
50 CPPUNIT_TEST(testGraphicDescriptor
);
51 CPPUNIT_TEST(testGraphicProvider
);
52 CPPUNIT_TEST_SUITE_END();
55 BitmapEx
createBitmap()
57 Bitmap
aBitmap(Size(100, 50), vcl::PixelFormat::N24_BPP
);
58 aBitmap
.Erase(COL_LIGHTRED
);
60 return BitmapEx(aBitmap
);
63 void XGraphicTest::testGraphic()
66 uno::Reference
<graphic::XGraphic
> xGraphic
= aGraphic
.GetXGraphic();
69 void XGraphicTest::testGraphicDescriptor()
71 Graphic
aGraphic(createBitmap());
72 uno::Reference
<graphic::XGraphic
> xGraphic
= aGraphic
.GetXGraphic();
73 uno::Reference
<beans::XPropertySet
> xGraphicDescriptor(xGraphic
, uno::UNO_QUERY_THROW
);
75 //[property] byte GraphicType;
77 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"GraphicType"_ustr
) >>= nType
);
78 CPPUNIT_ASSERT_EQUAL(graphic::GraphicType::PIXEL
, nType
);
80 //[property] string MimeType;
82 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"MimeType"_ustr
) >>= sMimeType
);
83 CPPUNIT_ASSERT_EQUAL(u
"image/x-vclgraphic"_ustr
, sMimeType
);
85 //[optional, property] ::com::sun::star::awt::Size SizePixel;
87 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"SizePixel"_ustr
) >>= aSizePixel
);
88 CPPUNIT_ASSERT_EQUAL(sal_Int32(100), aSizePixel
.Width
);
89 CPPUNIT_ASSERT_EQUAL(sal_Int32(50), aSizePixel
.Height
);
91 //[optional, property] ::com::sun::star::awt::Size Size100thMM;
92 awt::Size aSize100thMM
;
93 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Size100thMM"_ustr
) >>= aSize100thMM
);
94 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aSize100thMM
.Width
);
95 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), aSize100thMM
.Height
);
97 //[optional, property] byte BitsPerPixel;
98 sal_Int8 nBitsPerPixel
;
99 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"BitsPerPixel"_ustr
) >>= nBitsPerPixel
);
100 CPPUNIT_ASSERT_EQUAL(sal_Int8(24), nBitsPerPixel
);
102 //[optional, property] boolean Transparent;
104 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Transparent"_ustr
) >>= bTransparent
);
105 CPPUNIT_ASSERT_EQUAL(false, bTransparent
);
107 //[optional, property] boolean Alpha;
109 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Alpha"_ustr
) >>= bAlpha
);
110 CPPUNIT_ASSERT_EQUAL(false, bAlpha
);
112 //[optional, property] boolean Animated;
114 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Animated"_ustr
) >>= bAnimated
);
115 CPPUNIT_ASSERT_EQUAL(false, bAnimated
);
118 void XGraphicTest::testGraphicProvider()
120 OUString aGraphicURL
= getFullUrl(u
"TestGraphic.png");
123 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
124 uno::Reference
<graphic::XGraphicProvider
> xGraphicProvider
;
125 xGraphicProvider
.set(graphic::GraphicProvider::create(xContext
), uno::UNO_SET_THROW
);
127 auto aMediaProperties(comphelper::InitPropertySequence({
128 { "URL", uno::Any(aGraphicURL
) },
129 { "LazyRead", uno::Any(true) },
130 { "LoadAsLink", uno::Any(false) },
133 uno::Reference
<graphic::XGraphic
> xGraphic(
134 xGraphicProvider
->queryGraphic(aMediaProperties
));
135 CPPUNIT_ASSERT(xGraphic
.is());
136 Graphic
aGraphic(xGraphic
);
137 CPPUNIT_ASSERT_EQUAL(false, aGraphic
.isAvailable());
139 uno::Reference
<beans::XPropertySet
> xGraphicDescriptor(xGraphic
, uno::UNO_QUERY_THROW
);
142 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"GraphicType"_ustr
) >>= nType
);
143 CPPUNIT_ASSERT_EQUAL(graphic::GraphicType::PIXEL
, nType
);
145 awt::Size aSizePixel
;
146 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"SizePixel"_ustr
) >>= aSizePixel
);
147 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aSizePixel
.Width
);
148 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aSizePixel
.Height
);
151 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Linked"_ustr
) >>= bLinked
);
152 CPPUNIT_ASSERT_EQUAL(false, bLinked
);
155 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"OriginURL"_ustr
) >>= sOriginURL
);
156 CPPUNIT_ASSERT_EQUAL(OUString(), sOriginURL
);
158 CPPUNIT_ASSERT_EQUAL(false, aGraphic
.isAvailable());
162 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
163 uno::Reference
<graphic::XGraphicProvider
> xGraphicProvider
;
164 xGraphicProvider
.set(graphic::GraphicProvider::create(xContext
), uno::UNO_SET_THROW
);
166 auto aMediaProperties(comphelper::InitPropertySequence({
167 { "URL", uno::Any(aGraphicURL
) },
168 { "LazyRead", uno::Any(false) },
169 { "LoadAsLink", uno::Any(true) },
172 uno::Reference
<graphic::XGraphic
> xGraphic(
173 xGraphicProvider
->queryGraphic(aMediaProperties
));
174 CPPUNIT_ASSERT(xGraphic
.is());
175 Graphic
aGraphic(xGraphic
);
176 CPPUNIT_ASSERT_EQUAL(true, aGraphic
.isAvailable());
178 uno::Reference
<beans::XPropertySet
> xGraphicDescriptor(xGraphic
, uno::UNO_QUERY_THROW
);
181 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"GraphicType"_ustr
) >>= nType
);
182 CPPUNIT_ASSERT_EQUAL(graphic::GraphicType::PIXEL
, nType
);
184 awt::Size aSizePixel
;
185 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"SizePixel"_ustr
) >>= aSizePixel
);
186 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aSizePixel
.Width
);
187 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aSizePixel
.Height
);
190 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Linked"_ustr
) >>= bLinked
);
191 CPPUNIT_ASSERT_EQUAL(true, bLinked
);
194 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"OriginURL"_ustr
) >>= sOriginURL
);
195 CPPUNIT_ASSERT_EQUAL(aGraphicURL
, sOriginURL
);
198 { // Load lazy and as link
199 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
200 uno::Reference
<graphic::XGraphicProvider
> xGraphicProvider
;
201 xGraphicProvider
.set(graphic::GraphicProvider::create(xContext
), uno::UNO_SET_THROW
);
203 auto aMediaProperties(comphelper::InitPropertySequence({
204 { "URL", uno::Any(aGraphicURL
) },
205 { "LazyRead", uno::Any(true) },
206 { "LoadAsLink", uno::Any(true) },
209 uno::Reference
<graphic::XGraphic
> xGraphic(
210 xGraphicProvider
->queryGraphic(aMediaProperties
));
211 CPPUNIT_ASSERT(xGraphic
.is());
212 Graphic
aGraphic(xGraphic
);
214 CPPUNIT_ASSERT_EQUAL(false, aGraphic
.isAvailable());
216 uno::Reference
<beans::XPropertySet
> xGraphicDescriptor(xGraphic
, uno::UNO_QUERY_THROW
);
219 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"GraphicType"_ustr
) >>= nType
);
220 CPPUNIT_ASSERT_EQUAL(graphic::GraphicType::PIXEL
, nType
);
222 awt::Size aSizePixel
;
223 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"SizePixel"_ustr
) >>= aSizePixel
);
224 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aSizePixel
.Width
);
225 CPPUNIT_ASSERT_EQUAL(sal_Int32(8), aSizePixel
.Height
);
228 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"Linked"_ustr
) >>= bLinked
);
229 CPPUNIT_ASSERT_EQUAL(true, bLinked
);
232 CPPUNIT_ASSERT(xGraphicDescriptor
->getPropertyValue(u
"OriginURL"_ustr
) >>= sOriginURL
);
233 CPPUNIT_ASSERT_EQUAL(aGraphicURL
, sOriginURL
);
235 CPPUNIT_ASSERT_EQUAL(false, aGraphic
.isAvailable());
241 CPPUNIT_TEST_SUITE_REGISTRATION(XGraphicTest
);
243 CPPUNIT_PLUGIN_IMPLEMENT();
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */