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
|
/* -*- 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 <cppunit/extensions/HelperMacros.h>
#include "cppunit/plugin/TestPlugIn.h"
#include <svtools/grfmgr.hxx>
#include <test/bootstrapfixture.hxx>
#include <tools/stream.hxx>
#include <vcl/image.hxx>
#include <com/sun/star/frame/Desktop.hpp>
#include <officecfg/Office/Common.hxx>
#include <unotest/macros_test.hxx>
#include <comphelper/processfactory.hxx>
#include <unotxdoc.hxx>
#include <docsh.hxx>
#include <doc.hxx>
#include <ndgrf.hxx>
#include <boost/shared_ptr.hpp>
using namespace css;
namespace
{
class GraphicObjectTest: public test::BootstrapFixture, public unotest::MacrosTest
{
public:
void testSwap();
void testSizeBasedAutoSwap();
void testTdf88836();
void testTdf88935();
virtual void setUp() SAL_OVERRIDE
{
test::BootstrapFixture::setUp();
mxDesktop.set(css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory())));
}
private:
DECL_LINK(getLinkStream, GraphicObject*);
private:
CPPUNIT_TEST_SUITE(GraphicObjectTest);
CPPUNIT_TEST(testSwap);
CPPUNIT_TEST(testSizeBasedAutoSwap);
CPPUNIT_TEST(testTdf88836);
CPPUNIT_TEST(testTdf88935);
CPPUNIT_TEST_SUITE_END();
};
static const char aGraphicFile[] = "/svtools/qa/unit/data/graphic.png";
static const sal_uLong nGraphicSizeBytes = 4800;
const Graphic lcl_loadGraphic(const rtl::OUString &rUrl)
{
const Image aImage(rUrl);
return Graphic(aImage.GetBitmapEx());
}
IMPL_LINK(GraphicObjectTest, getLinkStream, GraphicObject*, /*pGraphObj*/)
{
return reinterpret_cast<sal_IntPtr>(GRFMGR_AUTOSWAPSTREAM_LINK);
}
void GraphicObjectTest::testSwap()
{
// simple non-linked case
{
GraphicObject aGraphObj(lcl_loadGraphic(getURLFromSrc(aGraphicFile)));
CPPUNIT_ASSERT(!aGraphObj.HasSwapStreamHdl());
CPPUNIT_ASSERT(!aGraphObj.IsSwappedOut());
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj.GetGraphic().GetSizeBytes());
// swap out
CPPUNIT_ASSERT(aGraphObj.SwapOut());
CPPUNIT_ASSERT(aGraphObj.IsSwappedOut());
// swap in
CPPUNIT_ASSERT(aGraphObj.SwapIn());
CPPUNIT_ASSERT(!aGraphObj.IsSwappedOut());
// the data are still there
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj.GetGraphic().GetSizeBytes());
}
// linked case
{
GraphicObject aGraphObj(lcl_loadGraphic(getURLFromSrc(aGraphicFile)));
aGraphObj.SetSwapStreamHdl(LINK(this, GraphicObjectTest, getLinkStream));
CPPUNIT_ASSERT(aGraphObj.HasSwapStreamHdl());
CPPUNIT_ASSERT(!aGraphObj.IsSwappedOut());
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj.GetGraphic().GetSizeBytes());
// swap out
CPPUNIT_ASSERT(aGraphObj.SwapOut());
CPPUNIT_ASSERT(aGraphObj.IsSwappedOut());
// swap in
CPPUNIT_ASSERT(aGraphObj.SwapIn());
CPPUNIT_ASSERT(!aGraphObj.IsSwappedOut());
// the data are still there
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj.GetGraphic().GetSizeBytes());
}
// combination of two GraphicObjects
{
GraphicObject aGraphObj(lcl_loadGraphic(getURLFromSrc(aGraphicFile)));
GraphicObject aGraphObj2(aGraphObj);
aGraphObj2.SetSwapStreamHdl(LINK(this, GraphicObjectTest, getLinkStream));
CPPUNIT_ASSERT(!aGraphObj.IsSwappedOut());
CPPUNIT_ASSERT(!aGraphObj2.IsSwappedOut());
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj.GetGraphic().GetSizeBytes());
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj2.GetGraphic().GetSizeBytes());
// GraphicObjects never share the same Graphic. A new one is created as one step during
// registration of the GraphicObject at GraphicManager.
// swap out
CPPUNIT_ASSERT(aGraphObj.SwapOut());
CPPUNIT_ASSERT(aGraphObj.IsSwappedOut());
CPPUNIT_ASSERT(!aGraphObj2.IsSwappedOut());
CPPUNIT_ASSERT(aGraphObj2.SwapOut());
CPPUNIT_ASSERT(aGraphObj2.IsSwappedOut());
// swap in
CPPUNIT_ASSERT(aGraphObj2.SwapIn());
CPPUNIT_ASSERT(!aGraphObj2.IsSwappedOut());
CPPUNIT_ASSERT(aGraphObj.IsSwappedOut());
CPPUNIT_ASSERT(aGraphObj.SwapIn());
CPPUNIT_ASSERT(!aGraphObj.IsSwappedOut());
// the data are still there
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj.GetGraphic().GetSizeBytes());
CPPUNIT_ASSERT_EQUAL(nGraphicSizeBytes, aGraphObj2.GetGraphic().GetSizeBytes());
}
}
void GraphicObjectTest::testSizeBasedAutoSwap()
{
// Set cache size to a very small value to check what happens
{
std::shared_ptr< comphelper::ConfigurationChanges > aBatch(comphelper::ConfigurationChanges::create());
officecfg::Office::Common::Cache::GraphicManager::TotalCacheSize::set(sal_Int32(1), aBatch);
aBatch->commit();
}
uno::Reference< lang::XComponent > xComponent =
loadFromDesktop(getURLFromSrc("svtools/qa/unit/data/document_with_two_images.odt"), "com.sun.star.text.TextDocument");
SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(xComponent.get());
CPPUNIT_ASSERT(pTxtDoc);
SwDoc* pDoc = pTxtDoc->GetDocShell()->GetDoc();
CPPUNIT_ASSERT(pDoc);
SwNodes& aNodes = pDoc->GetNodes();
// Find images
const GraphicObject* pGrafObj1 = 0;
const GraphicObject* pGrafObj2 = 0;
for( sal_uLong nIndex = 0; nIndex < aNodes.Count(); ++nIndex)
{
if( aNodes[nIndex]->IsGrfNode() )
{
SwGrfNode* pGrfNode = aNodes[nIndex]->GetGrfNode();
CPPUNIT_ASSERT(pGrfNode);
if( !pGrafObj1 )
{
pGrafObj1 = &pGrfNode->GetGrfObj();
}
else
{
pGrafObj2 = &pGrfNode->GetGrfObj();
}
}
}
CPPUNIT_ASSERT_MESSAGE("Missing image", pGrafObj1 != 0 && pGrafObj2 != 0);
{
// First image should be swapped out
CPPUNIT_ASSERT(pGrafObj1->IsSwappedOut());
CPPUNIT_ASSERT_EQUAL(sal_uLong(697230), pGrafObj1->GetSizeBytes());
// Still swapped out: size is cached
CPPUNIT_ASSERT(pGrafObj1->IsSwappedOut());
}
{
// Second image should be in the memory
// Size based swap out is triggered by swap in, so the last swapped in image should be
// in the memory despite of size limit is reached.
CPPUNIT_ASSERT(!pGrafObj2->IsSwappedOut());
CPPUNIT_ASSERT_EQUAL(sal_uLong(1620000), pGrafObj2->GetSizeBytes());
}
// Swap in first image -> second image will be swapped out
{
pGrafObj1->GetGraphic(); // GetGraphic calls swap in on a const object
CPPUNIT_ASSERT(!pGrafObj1->IsSwappedOut());
CPPUNIT_ASSERT(pGrafObj2->IsSwappedOut());
}
// Swap in second image -> first image will be swapped out
{
pGrafObj2->GetGraphic(); // GetGraphic calls swap in on a const object
CPPUNIT_ASSERT(!pGrafObj2->IsSwappedOut());
CPPUNIT_ASSERT(pGrafObj1->IsSwappedOut());
}
// Use bigger cache
{
GraphicManager& rGrfMgr = pGrafObj1->GetGraphicManager();
rGrfMgr.SetMaxCacheSize((pGrafObj1->GetSizeBytes()+pGrafObj2->GetSizeBytes())*10);
}
// Swap in both images -> both should be swapped in
{
pGrafObj1->GetGraphic();
pGrafObj2->GetGraphic();
CPPUNIT_ASSERT(!pGrafObj1->IsSwappedOut());
CPPUNIT_ASSERT(!pGrafObj2->IsSwappedOut());
}
xComponent->dispose();
}
void GraphicObjectTest::testTdf88836()
{
// Construction with empty bitmap -> type should be GRAPHIC_NONE
Graphic aGraphic = Bitmap();
CPPUNIT_ASSERT_EQUAL(GRAPHIC_NONE, aGraphic.GetType());
aGraphic = Graphic(BitmapEx());
CPPUNIT_ASSERT_EQUAL(GRAPHIC_NONE, aGraphic.GetType());
}
void GraphicObjectTest::testTdf88935()
{
// Cache size was not updated by deletion of graphic objects
// Load a file with two images
uno::Reference< lang::XComponent > xComponent =
loadFromDesktop(getURLFromSrc("svtools/qa/unit/data/document_with_two_images.odt"), "com.sun.star.text.TextDocument");
SwXTextDocument* pTxtDoc = dynamic_cast<SwXTextDocument *>(xComponent.get());
CPPUNIT_ASSERT(pTxtDoc);
SwDoc* pDoc = pTxtDoc->GetDocShell()->GetDoc();
CPPUNIT_ASSERT(pDoc);
SwNodes& aNodes = pDoc->GetNodes();
// Find images
const GraphicObject* pGraphObj1 = 0;
const GraphicObject* pGraphObj2 = 0;
for( sal_uLong nIndex = 0; nIndex < aNodes.Count(); ++nIndex)
{
if( aNodes[nIndex]->IsGrfNode() )
{
SwGrfNode* pGrfNode = aNodes[nIndex]->GetGrfNode();
if( !pGraphObj1 )
{
pGraphObj1 = &pGrfNode->GetGrfObj();
}
else
{
pGraphObj2 = &pGrfNode->GetGrfObj();
}
}
}
CPPUNIT_ASSERT_MESSAGE("Missing image", pGraphObj1 != 0 && pGraphObj2 != 0);
// Set cache size
{
GraphicManager& rGrfMgr = pGraphObj1->GetGraphicManager();
rGrfMgr.SetMaxCacheSize((pGraphObj1->GetSizeBytes()+pGraphObj2->GetSizeBytes())*10);
}
// Both images fit into the cache
{
pGraphObj1->GetGraphic();
pGraphObj2->GetGraphic();
CPPUNIT_ASSERT(!pGraphObj1->IsSwappedOut());
CPPUNIT_ASSERT(!pGraphObj2->IsSwappedOut());
}
// Create and remove some copy of the first image
for( int i = 0; i < 50; ++i )
{
GraphicObject aGraphObj3(*pGraphObj1, &pGraphObj1->GetGraphicManager());
CPPUNIT_ASSERT(aGraphObj3.SwapOut());
CPPUNIT_ASSERT(aGraphObj3.SwapIn());
}
// Both images fit into the cache
{
pGraphObj1->GetGraphic();
pGraphObj2->GetGraphic();
CPPUNIT_ASSERT(!pGraphObj1->IsSwappedOut());
CPPUNIT_ASSERT(!pGraphObj2->IsSwappedOut());
}
xComponent->dispose();
}
CPPUNIT_TEST_SUITE_REGISTRATION(GraphicObjectTest);
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|