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/.
11 #include <cppunit/extensions/HelperMacros.h>
12 #include "cppunit/plugin/TestPlugIn.h"
14 #include <svtools/grfmgr.hxx>
16 #include <test/bootstrapfixture.hxx>
18 #include <tools/stream.hxx>
20 #include <vcl/image.hxx>
22 #include <com/sun/star/frame/Desktop.hpp>
23 #include <officecfg/Office/Common.hxx>
24 #include <unotest/macros_test.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <unotxdoc.hxx>
30 #include <boost/shared_ptr.hpp>
37 class GraphicObjectTest
: public test::BootstrapFixture
, public unotest::MacrosTest
42 void testSizeBasedAutoSwap();
47 virtual void setUp() SAL_OVERRIDE
49 test::BootstrapFixture::setUp();
51 mxDesktop
.set(css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory())));
55 DECL_LINK(getLinkStream
, GraphicObject
*);
58 CPPUNIT_TEST_SUITE(GraphicObjectTest
);
59 CPPUNIT_TEST(testSwap
);
60 CPPUNIT_TEST(testSizeBasedAutoSwap
);
61 CPPUNIT_TEST(testTdf88836
);
62 CPPUNIT_TEST(testTdf88935
);
63 CPPUNIT_TEST_SUITE_END();
66 static const char aGraphicFile
[] = "/svtools/qa/unit/data/graphic.png";
67 static const sal_uLong nGraphicSizeBytes
= 4800;
69 const Graphic
lcl_loadGraphic(const rtl::OUString
&rUrl
)
71 const Image
aImage(rUrl
);
72 return Graphic(aImage
.GetBitmapEx());
75 IMPL_LINK(GraphicObjectTest
, getLinkStream
, GraphicObject
*, /*pGraphObj*/)
77 return reinterpret_cast<sal_IntPtr
>(GRFMGR_AUTOSWAPSTREAM_LINK
);
80 void GraphicObjectTest::testSwap()
82 // simple non-linked case
84 GraphicObject
aGraphObj(lcl_loadGraphic(getURLFromSrc(aGraphicFile
)));
85 CPPUNIT_ASSERT(!aGraphObj
.HasSwapStreamHdl());
86 CPPUNIT_ASSERT(!aGraphObj
.IsSwappedOut());
87 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj
.GetGraphic().GetSizeBytes());
89 CPPUNIT_ASSERT(aGraphObj
.SwapOut());
90 CPPUNIT_ASSERT(aGraphObj
.IsSwappedOut());
92 CPPUNIT_ASSERT(aGraphObj
.SwapIn());
93 CPPUNIT_ASSERT(!aGraphObj
.IsSwappedOut());
94 // the data are still there
95 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj
.GetGraphic().GetSizeBytes());
100 GraphicObject
aGraphObj(lcl_loadGraphic(getURLFromSrc(aGraphicFile
)));
101 aGraphObj
.SetSwapStreamHdl(LINK(this, GraphicObjectTest
, getLinkStream
));
103 CPPUNIT_ASSERT(aGraphObj
.HasSwapStreamHdl());
104 CPPUNIT_ASSERT(!aGraphObj
.IsSwappedOut());
105 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj
.GetGraphic().GetSizeBytes());
107 CPPUNIT_ASSERT(aGraphObj
.SwapOut());
108 CPPUNIT_ASSERT(aGraphObj
.IsSwappedOut());
110 CPPUNIT_ASSERT(aGraphObj
.SwapIn());
111 CPPUNIT_ASSERT(!aGraphObj
.IsSwappedOut());
112 // the data are still there
113 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj
.GetGraphic().GetSizeBytes());
116 // combination of two GraphicObjects
118 GraphicObject
aGraphObj(lcl_loadGraphic(getURLFromSrc(aGraphicFile
)));
120 GraphicObject
aGraphObj2(aGraphObj
);
121 aGraphObj2
.SetSwapStreamHdl(LINK(this, GraphicObjectTest
, getLinkStream
));
123 CPPUNIT_ASSERT(!aGraphObj
.IsSwappedOut());
124 CPPUNIT_ASSERT(!aGraphObj2
.IsSwappedOut());
125 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj
.GetGraphic().GetSizeBytes());
126 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj2
.GetGraphic().GetSizeBytes());
128 // GraphicObjects never share the same Graphic. A new one is created as one step during
129 // registration of the GraphicObject at GraphicManager.
132 CPPUNIT_ASSERT(aGraphObj
.SwapOut());
133 CPPUNIT_ASSERT(aGraphObj
.IsSwappedOut());
134 CPPUNIT_ASSERT(!aGraphObj2
.IsSwappedOut());
135 CPPUNIT_ASSERT(aGraphObj2
.SwapOut());
136 CPPUNIT_ASSERT(aGraphObj2
.IsSwappedOut());
138 CPPUNIT_ASSERT(aGraphObj2
.SwapIn());
139 CPPUNIT_ASSERT(!aGraphObj2
.IsSwappedOut());
140 CPPUNIT_ASSERT(aGraphObj
.IsSwappedOut());
141 CPPUNIT_ASSERT(aGraphObj
.SwapIn());
142 CPPUNIT_ASSERT(!aGraphObj
.IsSwappedOut());
143 // the data are still there
144 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj
.GetGraphic().GetSizeBytes());
145 CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes
, aGraphObj2
.GetGraphic().GetSizeBytes());
149 void GraphicObjectTest::testSizeBasedAutoSwap()
151 // Set cache size to a very small value to check what happens
153 std::shared_ptr
< comphelper::ConfigurationChanges
> aBatch(comphelper::ConfigurationChanges::create());
154 officecfg::Office::Common::Cache::GraphicManager::TotalCacheSize::set(sal_Int32(1), aBatch
);
158 uno::Reference
< lang::XComponent
> xComponent
=
159 loadFromDesktop(getURLFromSrc("svtools/qa/unit/data/document_with_two_images.odt"), "com.sun.star.text.TextDocument");
161 SwXTextDocument
* pTxtDoc
= dynamic_cast<SwXTextDocument
*>(xComponent
.get());
162 CPPUNIT_ASSERT(pTxtDoc
);
163 SwDoc
* pDoc
= pTxtDoc
->GetDocShell()->GetDoc();
164 CPPUNIT_ASSERT(pDoc
);
165 SwNodes
& aNodes
= pDoc
->GetNodes();
168 const GraphicObject
* pGrafObj1
= 0;
169 const GraphicObject
* pGrafObj2
= 0;
170 for( sal_uLong nIndex
= 0; nIndex
< aNodes
.Count(); ++nIndex
)
172 if( aNodes
[nIndex
]->IsGrfNode() )
174 SwGrfNode
* pGrfNode
= aNodes
[nIndex
]->GetGrfNode();
175 CPPUNIT_ASSERT(pGrfNode
);
178 pGrafObj1
= &pGrfNode
->GetGrfObj();
182 pGrafObj2
= &pGrfNode
->GetGrfObj();
186 CPPUNIT_ASSERT_MESSAGE("Missing image", pGrafObj1
!= 0 && pGrafObj2
!= 0);
189 // First image should be swapped out
190 CPPUNIT_ASSERT(pGrafObj1
->IsSwappedOut());
191 CPPUNIT_ASSERT_EQUAL(sal_uLong(697230), pGrafObj1
->GetSizeBytes());
193 // Still swapped out: size is cached
194 CPPUNIT_ASSERT(pGrafObj1
->IsSwappedOut());
198 // Second image should be in the memory
199 // Size based swap out is triggered by swap in, so the last swapped in image should be
200 // in the memory despite of size limit is reached.
201 CPPUNIT_ASSERT(!pGrafObj2
->IsSwappedOut());
202 CPPUNIT_ASSERT_EQUAL(sal_uLong(1620000), pGrafObj2
->GetSizeBytes());
205 // Swap in first image -> second image will be swapped out
207 pGrafObj1
->GetGraphic(); // GetGraphic calls swap in on a const object
208 CPPUNIT_ASSERT(!pGrafObj1
->IsSwappedOut());
209 CPPUNIT_ASSERT(pGrafObj2
->IsSwappedOut());
212 // Swap in second image -> first image will be swapped out
214 pGrafObj2
->GetGraphic(); // GetGraphic calls swap in on a const object
215 CPPUNIT_ASSERT(!pGrafObj2
->IsSwappedOut());
216 CPPUNIT_ASSERT(pGrafObj1
->IsSwappedOut());
221 GraphicManager
& rGrfMgr
= pGrafObj1
->GetGraphicManager();
222 rGrfMgr
.SetMaxCacheSize((pGrafObj1
->GetSizeBytes()+pGrafObj2
->GetSizeBytes())*10);
224 // Swap in both images -> both should be swapped in
226 pGrafObj1
->GetGraphic();
227 pGrafObj2
->GetGraphic();
228 CPPUNIT_ASSERT(!pGrafObj1
->IsSwappedOut());
229 CPPUNIT_ASSERT(!pGrafObj2
->IsSwappedOut());
232 xComponent
->dispose();
235 void GraphicObjectTest::testTdf88836()
237 // Construction with empty bitmap -> type should be GRAPHIC_NONE
238 Graphic aGraphic
= Bitmap();
239 CPPUNIT_ASSERT_EQUAL(GRAPHIC_NONE
, aGraphic
.GetType());
240 aGraphic
= Graphic(BitmapEx());
241 CPPUNIT_ASSERT_EQUAL(GRAPHIC_NONE
, aGraphic
.GetType());
244 void GraphicObjectTest::testTdf88935()
246 // Cache size was not updated by deletion of graphic objects
248 // Load a file with two images
249 uno::Reference
< lang::XComponent
> xComponent
=
250 loadFromDesktop(getURLFromSrc("svtools/qa/unit/data/document_with_two_images.odt"), "com.sun.star.text.TextDocument");
251 SwXTextDocument
* pTxtDoc
= dynamic_cast<SwXTextDocument
*>(xComponent
.get());
252 CPPUNIT_ASSERT(pTxtDoc
);
253 SwDoc
* pDoc
= pTxtDoc
->GetDocShell()->GetDoc();
254 CPPUNIT_ASSERT(pDoc
);
255 SwNodes
& aNodes
= pDoc
->GetNodes();
258 const GraphicObject
* pGraphObj1
= 0;
259 const GraphicObject
* pGraphObj2
= 0;
260 for( sal_uLong nIndex
= 0; nIndex
< aNodes
.Count(); ++nIndex
)
262 if( aNodes
[nIndex
]->IsGrfNode() )
264 SwGrfNode
* pGrfNode
= aNodes
[nIndex
]->GetGrfNode();
265 CPPUNIT_ASSERT(pGrfNode
);
268 pGraphObj1
= &pGrfNode
->GetGrfObj();
272 pGraphObj2
= &pGrfNode
->GetGrfObj();
276 CPPUNIT_ASSERT_MESSAGE("Missing image", pGraphObj1
!= 0 && pGraphObj2
!= 0);
280 GraphicManager
& rGrfMgr
= pGraphObj1
->GetGraphicManager();
281 rGrfMgr
.SetMaxCacheSize((pGraphObj1
->GetSizeBytes()+pGraphObj2
->GetSizeBytes())*10);
284 // Both images fit into the cache
286 pGraphObj1
->GetGraphic();
287 pGraphObj2
->GetGraphic();
288 CPPUNIT_ASSERT(!pGraphObj1
->IsSwappedOut());
289 CPPUNIT_ASSERT(!pGraphObj2
->IsSwappedOut());
292 // Create and remove some copy of the first image
293 for( int i
= 0; i
< 50; ++i
)
295 GraphicObject
aGraphObj3(*pGraphObj1
, &pGraphObj1
->GetGraphicManager());
296 CPPUNIT_ASSERT(aGraphObj3
.SwapOut());
297 CPPUNIT_ASSERT(aGraphObj3
.SwapIn());
300 // Both images fit into the cache
302 pGraphObj1
->GetGraphic();
303 pGraphObj2
->GetGraphic();
304 CPPUNIT_ASSERT(!pGraphObj1
->IsSwappedOut());
305 CPPUNIT_ASSERT(!pGraphObj2
->IsSwappedOut());
308 xComponent
->dispose();
311 CPPUNIT_TEST_SUITE_REGISTRATION(GraphicObjectTest
);
315 CPPUNIT_PLUGIN_IMPLEMENT();
317 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */