author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
child 37 | 758a864f9613 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
42 |
#define Q_TEST_QPIXMAPCACHE |
0 | 43 |
|
44 |
#include <QtTest/QtTest> |
|
45 |
||
46 |
||
47 |
#include <qpixmapcache.h> |
|
48 |
#include "../../../src/gui/image/qpixmapcache_p.h" |
|
49 |
||
50 |
||
51 |
//TESTED_CLASS= |
|
52 |
//TESTED_FILES= |
|
53 |
||
54 |
class tst_QPixmapCache : public QObject |
|
55 |
{ |
|
56 |
Q_OBJECT |
|
57 |
||
58 |
public: |
|
59 |
tst_QPixmapCache(); |
|
60 |
virtual ~tst_QPixmapCache(); |
|
61 |
||
62 |
||
63 |
public slots: |
|
64 |
void init(); |
|
65 |
private slots: |
|
66 |
void cacheLimit(); |
|
67 |
void setCacheLimit(); |
|
68 |
void find(); |
|
69 |
void insert(); |
|
70 |
void replace(); |
|
71 |
void remove(); |
|
72 |
void clear(); |
|
73 |
void pixmapKey(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
74 |
void noLeak(); |
0 | 75 |
}; |
76 |
||
77 |
static QPixmapCache::KeyData* getPrivate(QPixmapCache::Key &key) |
|
78 |
{ |
|
79 |
return (*reinterpret_cast<QPixmapCache::KeyData**>(&key)); |
|
80 |
} |
|
81 |
||
82 |
static QPixmapCache::KeyData** getPrivateRef(QPixmapCache::Key &key) |
|
83 |
{ |
|
84 |
return (reinterpret_cast<QPixmapCache::KeyData**>(&key)); |
|
85 |
} |
|
86 |
||
87 |
static int originalCacheLimit; |
|
88 |
||
89 |
tst_QPixmapCache::tst_QPixmapCache() |
|
90 |
{ |
|
91 |
originalCacheLimit = QPixmapCache::cacheLimit(); |
|
92 |
} |
|
93 |
||
94 |
tst_QPixmapCache::~tst_QPixmapCache() |
|
95 |
{ |
|
96 |
} |
|
97 |
||
98 |
void tst_QPixmapCache::init() |
|
99 |
{ |
|
100 |
QPixmapCache::setCacheLimit(originalCacheLimit); |
|
101 |
QPixmapCache::clear(); |
|
102 |
} |
|
103 |
||
104 |
void tst_QPixmapCache::cacheLimit() |
|
105 |
{ |
|
106 |
// make sure the default is reasonable; |
|
107 |
// it was between 2048 and 10240 last time I looked at it |
|
108 |
QVERIFY(originalCacheLimit >= 1024 && originalCacheLimit <= 20480); |
|
109 |
||
110 |
QPixmapCache::setCacheLimit(100); |
|
111 |
QCOMPARE(QPixmapCache::cacheLimit(), 100); |
|
112 |
||
113 |
QPixmapCache::setCacheLimit(-50); |
|
114 |
QCOMPARE(QPixmapCache::cacheLimit(), -50); |
|
115 |
} |
|
116 |
||
117 |
void tst_QPixmapCache::setCacheLimit() |
|
118 |
{ |
|
119 |
QPixmap *p1 = new QPixmap(2, 3); |
|
120 |
QPixmapCache::insert("P1", *p1); |
|
121 |
QVERIFY(QPixmapCache::find("P1") != 0); |
|
122 |
delete p1; |
|
123 |
||
124 |
QPixmapCache::setCacheLimit(0); |
|
125 |
QVERIFY(QPixmapCache::find("P1") == 0); |
|
126 |
||
127 |
p1 = new QPixmap(2, 3); |
|
128 |
QPixmapCache::setCacheLimit(1000); |
|
129 |
QPixmapCache::insert("P1", *p1); |
|
130 |
QVERIFY(QPixmapCache::find("P1") != 0); |
|
131 |
||
132 |
delete p1; |
|
133 |
||
134 |
//The int part of the API |
|
135 |
p1 = new QPixmap(2, 3); |
|
136 |
QPixmapCache::Key key = QPixmapCache::insert(*p1); |
|
137 |
QVERIFY(QPixmapCache::find(key, p1) != 0); |
|
138 |
delete p1; |
|
139 |
||
140 |
QPixmapCache::setCacheLimit(0); |
|
141 |
QVERIFY(QPixmapCache::find(key, p1) == 0); |
|
142 |
||
143 |
p1 = new QPixmap(2, 3); |
|
144 |
QPixmapCache::setCacheLimit(1000); |
|
145 |
QPixmapCache::replace(key, *p1); |
|
146 |
QVERIFY(QPixmapCache::find(key, p1) == 0); |
|
147 |
||
148 |
delete p1; |
|
149 |
||
150 |
//Let check if keys are released when the pixmap cache is |
|
151 |
//full or has been flushed. |
|
152 |
QPixmapCache::clear(); |
|
153 |
p1 = new QPixmap(2, 3); |
|
154 |
key = QPixmapCache::insert(*p1); |
|
155 |
QVERIFY(QPixmapCache::find(key, p1) != 0); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
156 |
p1->detach(); // dectach so that the cache thinks no-one is using it. |
0 | 157 |
QPixmapCache::setCacheLimit(0); |
158 |
QVERIFY(QPixmapCache::find(key, p1) == 0); |
|
159 |
QPixmapCache::setCacheLimit(1000); |
|
160 |
key = QPixmapCache::insert(*p1); |
|
161 |
QCOMPARE(getPrivate(key)->isValid, true); |
|
162 |
QCOMPARE(getPrivate(key)->key, 1); |
|
163 |
||
164 |
delete p1; |
|
165 |
||
166 |
//Let check if removing old entries doesn't let you get |
|
167 |
// wrong pixmaps |
|
168 |
QPixmapCache::clear(); |
|
169 |
QPixmap p2; |
|
170 |
p1 = new QPixmap(2, 3); |
|
171 |
key = QPixmapCache::insert(*p1); |
|
172 |
QVERIFY(QPixmapCache::find(key, &p2) != 0); |
|
173 |
//we flush the cache |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
174 |
p1->detach(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
175 |
p2.detach(); |
0 | 176 |
QPixmapCache::setCacheLimit(0); |
177 |
QPixmapCache::setCacheLimit(1000); |
|
178 |
QPixmapCache::Key key2 = QPixmapCache::insert(*p1); |
|
179 |
QCOMPARE(getPrivate(key2)->key, 1); |
|
180 |
QVERIFY(QPixmapCache::find(key, &p2) == 0); |
|
181 |
QVERIFY(QPixmapCache::find(key2, &p2) != 0); |
|
182 |
QCOMPARE(p2, *p1); |
|
183 |
||
184 |
delete p1; |
|
185 |
||
186 |
//Here we simulate the flushing when the app is idle |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
187 |
QPixmapCache::clear(); |
0 | 188 |
QPixmapCache::setCacheLimit(originalCacheLimit); |
189 |
p1 = new QPixmap(300, 300); |
|
190 |
key = QPixmapCache::insert(*p1); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
191 |
p1->detach(); |
0 | 192 |
QCOMPARE(getPrivate(key)->key, 1); |
193 |
key2 = QPixmapCache::insert(*p1); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
194 |
p1->detach(); |
0 | 195 |
key2 = QPixmapCache::insert(*p1); |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
196 |
p1->detach(); |
0 | 197 |
QPixmapCache::Key key3 = QPixmapCache::insert(*p1); |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
198 |
p1->detach(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
199 |
QPixmapCache::flushDetachedPixmaps(); |
0 | 200 |
key2 = QPixmapCache::insert(*p1); |
201 |
QCOMPARE(getPrivate(key2)->key, 1); |
|
202 |
//This old key is not valid anymore after the flush |
|
203 |
QCOMPARE(getPrivate(key)->isValid, false); |
|
204 |
QVERIFY(QPixmapCache::find(key, &p2) == 0); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
205 |
delete p1; |
0 | 206 |
} |
207 |
||
208 |
void tst_QPixmapCache::find() |
|
209 |
{ |
|
210 |
QPixmap p1(10, 10); |
|
211 |
p1.fill(Qt::red); |
|
212 |
QVERIFY(QPixmapCache::insert("P1", p1)); |
|
213 |
||
214 |
QPixmap p2; |
|
215 |
QVERIFY(QPixmapCache::find("P1", p2)); |
|
216 |
QCOMPARE(p2.width(), 10); |
|
217 |
QCOMPARE(p2.height(), 10); |
|
218 |
QCOMPARE(p1, p2); |
|
219 |
||
220 |
// obsolete |
|
221 |
QPixmap *p3 = QPixmapCache::find("P1"); |
|
222 |
QVERIFY(p3); |
|
223 |
QCOMPARE(p1, *p3); |
|
224 |
||
225 |
//The int part of the API |
|
226 |
QPixmapCache::Key key = QPixmapCache::insert(p1); |
|
227 |
||
228 |
QVERIFY(QPixmapCache::find(key, &p2)); |
|
229 |
QCOMPARE(p2.width(), 10); |
|
230 |
QCOMPARE(p2.height(), 10); |
|
231 |
QCOMPARE(p1, p2); |
|
232 |
||
233 |
QPixmapCache::clear(); |
|
234 |
QPixmapCache::setCacheLimit(128); |
|
235 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
236 |
QPixmap p4(10,10); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
237 |
key = QPixmapCache::insert(p4); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
238 |
p4.detach(); |
0 | 239 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
240 |
QPixmap p5(10,10); |
0 | 241 |
QList<QPixmapCache::Key> keys; |
242 |
for (int i = 0; i < 4000; ++i) |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
243 |
QPixmapCache::insert(p5); |
0 | 244 |
|
245 |
//at that time the first key has been erase because no more place in the cache |
|
246 |
QVERIFY(QPixmapCache::find(key, &p1) == 0); |
|
247 |
QCOMPARE(getPrivate(key)->isValid, false); |
|
248 |
} |
|
249 |
||
250 |
void tst_QPixmapCache::insert() |
|
251 |
{ |
|
252 |
QPixmap p1(10, 10); |
|
253 |
p1.fill(Qt::red); |
|
254 |
||
255 |
QPixmap p2(10, 10); |
|
256 |
p2.fill(Qt::yellow); |
|
257 |
||
258 |
// Calcuate estimated num of items what fits to cache |
|
259 |
int estimatedNum = (1024 * QPixmapCache::cacheLimit()) |
|
260 |
/ ((p1.width() * p1.height() * p1.depth()) / 8); |
|
261 |
||
262 |
// Mare sure we will put enough items to reach the cache limit |
|
263 |
const int numberOfKeys = estimatedNum + 1000; |
|
264 |
||
265 |
// make sure it doesn't explode |
|
266 |
for (int i = 0; i < numberOfKeys; ++i) |
|
267 |
QPixmapCache::insert("0", p1); |
|
268 |
||
269 |
// ditto |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
270 |
for (int j = 0; j < numberOfKeys; ++j) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
271 |
QPixmap p3(10, 10); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
272 |
QPixmapCache::insert(QString::number(j), p3); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
273 |
} |
0 | 274 |
|
275 |
int num = 0; |
|
276 |
for (int k = 0; k < numberOfKeys; ++k) { |
|
277 |
if (QPixmapCache::find(QString::number(k))) |
|
278 |
++num; |
|
279 |
} |
|
280 |
||
281 |
if (QPixmapCache::find("0")) |
|
282 |
++num; |
|
283 |
||
284 |
QVERIFY(num <= estimatedNum); |
|
285 |
QPixmap p3; |
|
286 |
QPixmapCache::insert("null", p3); |
|
287 |
||
288 |
QPixmap c1(10, 10); |
|
289 |
c1.fill(Qt::yellow); |
|
290 |
QPixmapCache::insert("custom", c1); |
|
291 |
QVERIFY(!c1.isDetached()); |
|
292 |
QPixmap c2(10, 10); |
|
293 |
c2.fill(Qt::red); |
|
294 |
QPixmapCache::insert("custom", c2); |
|
295 |
//We have deleted the old pixmap in the cache for the same key |
|
296 |
QVERIFY(c1.isDetached()); |
|
297 |
||
298 |
//The int part of the API |
|
299 |
// make sure it doesn't explode |
|
300 |
QList<QPixmapCache::Key> keys; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
301 |
for (int i = 0; i < numberOfKeys; ++i) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
302 |
QPixmap p3(10,10); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
303 |
keys.append(QPixmapCache::insert(p3)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
304 |
} |
0 | 305 |
|
306 |
num = 0; |
|
307 |
for (int k = 0; k < numberOfKeys; ++k) { |
|
308 |
if (QPixmapCache::find(keys.at(k), &p2)) |
|
309 |
++num; |
|
310 |
} |
|
311 |
||
312 |
estimatedNum = (1024 * QPixmapCache::cacheLimit()) |
|
313 |
/ ((p1.width() * p1.height() * p1.depth()) / 8); |
|
314 |
QVERIFY(num <= estimatedNum); |
|
315 |
} |
|
316 |
||
317 |
void tst_QPixmapCache::replace() |
|
318 |
{ |
|
319 |
//The int part of the API |
|
320 |
QPixmap p1(10, 10); |
|
321 |
p1.fill(Qt::red); |
|
322 |
||
323 |
QPixmap p2(10, 10); |
|
324 |
p2.fill(Qt::yellow); |
|
325 |
||
326 |
QPixmapCache::Key key = QPixmapCache::insert(p1); |
|
327 |
QCOMPARE(getPrivate(key)->isValid, true); |
|
328 |
||
329 |
QPixmap p3; |
|
330 |
QVERIFY(QPixmapCache::find(key, &p3) == 1); |
|
331 |
||
332 |
QPixmapCache::replace(key, p2); |
|
333 |
||
334 |
QVERIFY(QPixmapCache::find(key, &p3) == 1); |
|
335 |
QCOMPARE(getPrivate(key)->isValid, true); |
|
336 |
QCOMPARE(getPrivate(key)->key, 1); |
|
337 |
||
338 |
QCOMPARE(p3.width(), 10); |
|
339 |
QCOMPARE(p3.height(), 10); |
|
340 |
QCOMPARE(p3, p2); |
|
341 |
||
342 |
//Broken keys |
|
343 |
QCOMPARE(QPixmapCache::replace(QPixmapCache::Key(), p2), false); |
|
344 |
} |
|
345 |
||
346 |
void tst_QPixmapCache::remove() |
|
347 |
{ |
|
348 |
QPixmap p1(10, 10); |
|
349 |
p1.fill(Qt::red); |
|
350 |
||
351 |
QPixmapCache::insert("red", p1); |
|
352 |
p1.fill(Qt::yellow); |
|
353 |
||
354 |
QPixmap p2; |
|
355 |
QVERIFY(QPixmapCache::find("red", p2)); |
|
356 |
QVERIFY(p1.toImage() != p2.toImage()); |
|
357 |
QVERIFY(p1.toImage() == p1.toImage()); // sanity check |
|
358 |
||
359 |
QPixmapCache::remove("red"); |
|
360 |
QVERIFY(QPixmapCache::find("red") == 0); |
|
361 |
QPixmapCache::remove("red"); |
|
362 |
QVERIFY(QPixmapCache::find("red") == 0); |
|
363 |
||
364 |
QPixmapCache::remove("green"); |
|
365 |
QVERIFY(QPixmapCache::find("green") == 0); |
|
366 |
||
367 |
//The int part of the API |
|
368 |
QPixmapCache::clear(); |
|
369 |
p1.fill(Qt::red); |
|
370 |
QPixmapCache::Key key = QPixmapCache::insert(p1); |
|
371 |
p1.fill(Qt::yellow); |
|
372 |
||
373 |
QVERIFY(QPixmapCache::find(key, &p2)); |
|
374 |
QVERIFY(p1.toImage() != p2.toImage()); |
|
375 |
QVERIFY(p1.toImage() == p1.toImage()); // sanity check |
|
376 |
||
377 |
QPixmapCache::remove(key); |
|
378 |
QVERIFY(QPixmapCache::find(key, &p1) == 0); |
|
379 |
||
380 |
//Broken key |
|
381 |
QPixmapCache::remove(QPixmapCache::Key()); |
|
382 |
QVERIFY(QPixmapCache::find(QPixmapCache::Key(), &p1) == 0); |
|
383 |
||
384 |
//Test if keys are release |
|
385 |
QPixmapCache::clear(); |
|
386 |
key = QPixmapCache::insert(p1); |
|
387 |
QCOMPARE(getPrivate(key)->key, 1); |
|
388 |
QPixmapCache::remove(key); |
|
389 |
key = QPixmapCache::insert(p1); |
|
390 |
QCOMPARE(getPrivate(key)->key, 1); |
|
391 |
||
392 |
//Test if pixmaps are correctly deleted |
|
393 |
QPixmapCache::clear(); |
|
394 |
key = QPixmapCache::insert(p1); |
|
395 |
QCOMPARE(getPrivate(key)->key, 1); |
|
396 |
QVERIFY(QPixmapCache::find(key, &p1) != 0); |
|
397 |
QPixmapCache::remove(key); |
|
398 |
QCOMPARE(p1.isDetached(), true); |
|
399 |
||
400 |
//We mix both part of the API |
|
401 |
QPixmapCache::clear(); |
|
402 |
p1.fill(Qt::red); |
|
403 |
QPixmapCache::insert("red", p1); |
|
404 |
key = QPixmapCache::insert(p1); |
|
405 |
QPixmapCache::remove(key); |
|
406 |
QVERIFY(QPixmapCache::find(key, &p1) == 0); |
|
407 |
QVERIFY(QPixmapCache::find("red") != 0); |
|
408 |
} |
|
409 |
||
410 |
void tst_QPixmapCache::clear() |
|
411 |
{ |
|
412 |
QPixmap p1(10, 10); |
|
413 |
p1.fill(Qt::red); |
|
414 |
||
415 |
// Calcuate estimated num of items what fits to cache |
|
416 |
int estimatedNum = (1024 * QPixmapCache::cacheLimit()) |
|
417 |
/ ((p1.width() * p1.height() * p1.depth()) / 8); |
|
418 |
||
419 |
// Mare sure we will put enough items to reach the cache limit |
|
420 |
const int numberOfKeys = estimatedNum + 1000; |
|
421 |
||
422 |
for (int i = 0; i < numberOfKeys; ++i) |
|
423 |
QVERIFY(QPixmapCache::find("x" + QString::number(i)) == 0); |
|
424 |
||
425 |
for (int j = 0; j < numberOfKeys; ++j) |
|
426 |
QPixmapCache::insert(QString::number(j), p1); |
|
427 |
||
428 |
int num = 0; |
|
429 |
for (int k = 0; k < numberOfKeys; ++k) { |
|
430 |
if (QPixmapCache::find(QString::number(k), p1)) |
|
431 |
++num; |
|
432 |
} |
|
433 |
QVERIFY(num > 0); |
|
434 |
||
435 |
QPixmapCache::clear(); |
|
436 |
||
437 |
for (int k = 0; k < numberOfKeys; ++k) |
|
438 |
QVERIFY(QPixmapCache::find(QString::number(k)) == 0); |
|
439 |
||
440 |
//The int part of the API |
|
441 |
QPixmap p2(10, 10); |
|
442 |
p2.fill(Qt::red); |
|
443 |
||
444 |
QList<QPixmapCache::Key> keys; |
|
445 |
for (int k = 0; k < numberOfKeys; ++k) |
|
446 |
keys.append(QPixmapCache::insert(p2)); |
|
447 |
||
448 |
QPixmapCache::clear(); |
|
449 |
||
450 |
for (int k = 0; k < numberOfKeys; ++k) { |
|
451 |
QVERIFY(QPixmapCache::find(keys.at(k), &p1) == 0); |
|
452 |
QCOMPARE(getPrivate(keys[k])->isValid, false); |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
void tst_QPixmapCache::pixmapKey() |
|
457 |
{ |
|
458 |
QPixmapCache::Key key; |
|
459 |
//Default constructed keys have no d pointer unless |
|
460 |
//we use them |
|
461 |
QVERIFY(!getPrivate(key)); |
|
462 |
//Let's put a d pointer |
|
463 |
QPixmapCache::KeyData** keyd = getPrivateRef(key); |
|
464 |
*keyd = new QPixmapCache::KeyData; |
|
465 |
QCOMPARE(getPrivate(key)->ref, 1); |
|
466 |
QPixmapCache::Key key2; |
|
467 |
//Let's put a d pointer |
|
468 |
QPixmapCache::KeyData** key2d = getPrivateRef(key2); |
|
469 |
*key2d = new QPixmapCache::KeyData; |
|
470 |
QCOMPARE(getPrivate(key2)->ref, 1); |
|
471 |
key = key2; |
|
472 |
QCOMPARE(getPrivate(key2)->ref, 2); |
|
473 |
QCOMPARE(getPrivate(key)->ref, 2); |
|
474 |
QPixmapCache::Key key3; |
|
475 |
//Let's put a d pointer |
|
476 |
QPixmapCache::KeyData** key3d = getPrivateRef(key3); |
|
477 |
*key3d = new QPixmapCache::KeyData; |
|
478 |
QPixmapCache::Key key4 = key3; |
|
479 |
QCOMPARE(getPrivate(key3)->ref, 2); |
|
480 |
QCOMPARE(getPrivate(key4)->ref, 2); |
|
481 |
key4 = key; |
|
482 |
QCOMPARE(getPrivate(key4)->ref, 3); |
|
483 |
QCOMPARE(getPrivate(key3)->ref, 1); |
|
484 |
QPixmapCache::Key key5(key3); |
|
485 |
QCOMPARE(getPrivate(key3)->ref, 2); |
|
486 |
QCOMPARE(getPrivate(key5)->ref, 2); |
|
487 |
||
488 |
//let test default constructed keys |
|
489 |
QPixmapCache::Key key6; |
|
490 |
QVERIFY(!getPrivate(key6)); |
|
491 |
QPixmapCache::Key key7; |
|
492 |
QVERIFY(!getPrivate(key7)); |
|
493 |
key6 = key7; |
|
494 |
QVERIFY(!getPrivate(key6)); |
|
495 |
QVERIFY(!getPrivate(key7)); |
|
496 |
QPixmapCache::Key key8(key7); |
|
497 |
QVERIFY(!getPrivate(key8)); |
|
498 |
} |
|
499 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
500 |
//QTP: remove temporarily to get the code compiled. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
501 |
// The following function is implemented in qpixmapcache.cpp in GUI but export for |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
502 |
// auto test only with marco Q_AUTOTEST_EXPORT |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
503 |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
504 |
//QT_BEGIN_NAMESPACE |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
505 |
//extern int q_QPixmapCache_keyHashSize(); |
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
506 |
//QT_END_NAMESPACE |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
void tst_QPixmapCache::noLeak() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
509 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
510 |
QPixmapCache::Key key; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
511 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
int oldSize = 0;//q_QPixmapCache_keyHashSize(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
for (int i = 0; i < 100; ++i) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
QPixmap pm(128, 128); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
515 |
pm.fill(Qt::transparent); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
516 |
key = QPixmapCache::insert(pm); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
517 |
QPixmapCache::remove(key); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
518 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
519 |
int newSize = 0;//q_QPixmapCache_keyHashSize(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
520 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
521 |
QCOMPARE(oldSize, newSize); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
522 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
523 |
|
0 | 524 |
QTEST_MAIN(tst_QPixmapCache) |
525 |
#include "tst_qpixmapcache.moc" |