tests/auto/qpixmapcache/tst_qpixmapcache.cpp
changeset 30 5dc02b23752f
parent 18 2f34d5167611
child 37 758a864f9613
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
       
    42 #define Q_TEST_QPIXMAPCACHE
    42 
    43 
    43 #include <QtTest/QtTest>
    44 #include <QtTest/QtTest>
    44 
    45 
    45 
    46 
    46 #include <qpixmapcache.h>
    47 #include <qpixmapcache.h>
   150     //full or has been flushed.
   151     //full or has been flushed.
   151     QPixmapCache::clear();
   152     QPixmapCache::clear();
   152     p1 = new QPixmap(2, 3);
   153     p1 = new QPixmap(2, 3);
   153     key = QPixmapCache::insert(*p1);
   154     key = QPixmapCache::insert(*p1);
   154     QVERIFY(QPixmapCache::find(key, p1) != 0);
   155     QVERIFY(QPixmapCache::find(key, p1) != 0);
       
   156     p1->detach(); // dectach so that the cache thinks no-one is using it.
   155     QPixmapCache::setCacheLimit(0);
   157     QPixmapCache::setCacheLimit(0);
   156     QVERIFY(QPixmapCache::find(key, p1) == 0);
   158     QVERIFY(QPixmapCache::find(key, p1) == 0);
   157     QPixmapCache::setCacheLimit(1000);
   159     QPixmapCache::setCacheLimit(1000);
   158     key = QPixmapCache::insert(*p1);
   160     key = QPixmapCache::insert(*p1);
   159     QCOMPARE(getPrivate(key)->isValid, true);
   161     QCOMPARE(getPrivate(key)->isValid, true);
   167     QPixmap p2;
   169     QPixmap p2;
   168     p1 = new QPixmap(2, 3);
   170     p1 = new QPixmap(2, 3);
   169     key = QPixmapCache::insert(*p1);
   171     key = QPixmapCache::insert(*p1);
   170     QVERIFY(QPixmapCache::find(key, &p2) != 0);
   172     QVERIFY(QPixmapCache::find(key, &p2) != 0);
   171     //we flush the cache
   173     //we flush the cache
       
   174     p1->detach();
       
   175     p2.detach();
   172     QPixmapCache::setCacheLimit(0);
   176     QPixmapCache::setCacheLimit(0);
   173     QPixmapCache::setCacheLimit(1000);
   177     QPixmapCache::setCacheLimit(1000);
   174     QPixmapCache::Key key2 = QPixmapCache::insert(*p1);
   178     QPixmapCache::Key key2 = QPixmapCache::insert(*p1);
   175     QCOMPARE(getPrivate(key2)->key, 1);
   179     QCOMPARE(getPrivate(key2)->key, 1);
   176     QVERIFY(QPixmapCache::find(key, &p2) == 0);
   180     QVERIFY(QPixmapCache::find(key, &p2) == 0);
   178     QCOMPARE(p2, *p1);
   182     QCOMPARE(p2, *p1);
   179 
   183 
   180     delete p1;
   184     delete p1;
   181 
   185 
   182     //Here we simulate the flushing when the app is idle
   186     //Here we simulate the flushing when the app is idle
   183     /*QPixmapCache::clear();
   187     QPixmapCache::clear();
   184     QPixmapCache::setCacheLimit(originalCacheLimit);
   188     QPixmapCache::setCacheLimit(originalCacheLimit);
   185     p1 = new QPixmap(300, 300);
   189     p1 = new QPixmap(300, 300);
   186     key = QPixmapCache::insert(*p1);
   190     key = QPixmapCache::insert(*p1);
       
   191     p1->detach();
   187     QCOMPARE(getPrivate(key)->key, 1);
   192     QCOMPARE(getPrivate(key)->key, 1);
   188     key2 = QPixmapCache::insert(*p1);
   193     key2 = QPixmapCache::insert(*p1);
       
   194     p1->detach();
   189     key2 = QPixmapCache::insert(*p1);
   195     key2 = QPixmapCache::insert(*p1);
       
   196     p1->detach();
   190     QPixmapCache::Key key3 = QPixmapCache::insert(*p1);
   197     QPixmapCache::Key key3 = QPixmapCache::insert(*p1);
   191     QTest::qWait(32000);
   198     p1->detach();
       
   199     QPixmapCache::flushDetachedPixmaps();
   192     key2 = QPixmapCache::insert(*p1);
   200     key2 = QPixmapCache::insert(*p1);
   193     QCOMPARE(getPrivate(key2)->key, 1);
   201     QCOMPARE(getPrivate(key2)->key, 1);
   194     //This old key is not valid anymore after the flush
   202     //This old key is not valid anymore after the flush
   195     QCOMPARE(getPrivate(key)->isValid, false);
   203     QCOMPARE(getPrivate(key)->isValid, false);
   196     QVERIFY(QPixmapCache::find(key, &p2) == 0);
   204     QVERIFY(QPixmapCache::find(key, &p2) == 0);
   197     delete p1;*/
   205     delete p1;
   198 }
   206 }
   199 
   207 
   200 void tst_QPixmapCache::find()
   208 void tst_QPixmapCache::find()
   201 {
   209 {
   202     QPixmap p1(10, 10);
   210     QPixmap p1(10, 10);
   223     QCOMPARE(p1, p2);
   231     QCOMPARE(p1, p2);
   224 
   232 
   225     QPixmapCache::clear();
   233     QPixmapCache::clear();
   226     QPixmapCache::setCacheLimit(128);
   234     QPixmapCache::setCacheLimit(128);
   227 
   235 
   228     key = QPixmapCache::insert(p1);
   236     QPixmap p4(10,10);
   229 
   237     key = QPixmapCache::insert(p4);
   230     //The int part of the API
   238     p4.detach();
       
   239 
       
   240     QPixmap p5(10,10);
   231     QList<QPixmapCache::Key> keys;
   241     QList<QPixmapCache::Key> keys;
   232     for (int i = 0; i < 4000; ++i)
   242     for (int i = 0; i < 4000; ++i)
   233         QPixmapCache::insert(p1);
   243         QPixmapCache::insert(p5);
   234 
   244 
   235     //at that time the first key has been erase because no more place in the cache
   245     //at that time the first key has been erase because no more place in the cache
   236     QVERIFY(QPixmapCache::find(key, &p1) == 0);
   246     QVERIFY(QPixmapCache::find(key, &p1) == 0);
   237     QCOMPARE(getPrivate(key)->isValid, false);
   247     QCOMPARE(getPrivate(key)->isValid, false);
   238 }
   248 }
   255     // make sure it doesn't explode
   265     // make sure it doesn't explode
   256     for (int i = 0; i < numberOfKeys; ++i)
   266     for (int i = 0; i < numberOfKeys; ++i)
   257         QPixmapCache::insert("0", p1);
   267         QPixmapCache::insert("0", p1);
   258 
   268 
   259     // ditto
   269     // ditto
   260     for (int j = 0; j < numberOfKeys; ++j)
   270     for (int j = 0; j < numberOfKeys; ++j) {
   261         QPixmapCache::insert(QString::number(j), p1);
   271         QPixmap p3(10, 10);
       
   272         QPixmapCache::insert(QString::number(j), p3);
       
   273     }
   262 
   274 
   263     int num = 0;
   275     int num = 0;
   264     for (int k = 0; k < numberOfKeys; ++k) {
   276     for (int k = 0; k < numberOfKeys; ++k) {
   265         if (QPixmapCache::find(QString::number(k)))
   277         if (QPixmapCache::find(QString::number(k)))
   266             ++num;
   278             ++num;
   284     QVERIFY(c1.isDetached());
   296     QVERIFY(c1.isDetached());
   285 
   297 
   286     //The int part of the API
   298     //The int part of the API
   287     // make sure it doesn't explode
   299     // make sure it doesn't explode
   288     QList<QPixmapCache::Key> keys;
   300     QList<QPixmapCache::Key> keys;
   289     for (int i = 0; i < numberOfKeys; ++i)
   301     for (int i = 0; i < numberOfKeys; ++i) {
   290         keys.append(QPixmapCache::insert(p1));
   302         QPixmap p3(10,10);
       
   303         keys.append(QPixmapCache::insert(p3));
       
   304     }
   291 
   305 
   292     num = 0;
   306     num = 0;
   293     for (int k = 0; k < numberOfKeys; ++k) {
   307     for (int k = 0; k < numberOfKeys; ++k) {
   294         if (QPixmapCache::find(keys.at(k), &p2))
   308         if (QPixmapCache::find(keys.at(k), &p2))
   295             ++num;
   309             ++num;