tests/auto/qicon/tst_qicon.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     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 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qicon.h>
       
    46 
       
    47 #if defined(Q_OS_SYMBIAN)
       
    48 #define SRCDIR "."
       
    49 #endif
       
    50 #include <qiconengine.h>
       
    51 
       
    52 Q_DECLARE_METATYPE(QSize)
       
    53 
       
    54 //TESTED_CLASS=
       
    55 //TESTED_FILES=
       
    56 
       
    57 class tst_QIcon : public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60 public:
       
    61     tst_QIcon();
       
    62 
       
    63 private slots:
       
    64     void init();
       
    65     void cleanup();
       
    66 
       
    67     void actualSize_data(); // test with 1 pixmap
       
    68     void actualSize();
       
    69     void actualSize2_data(); // test with 2 pixmaps with different aspect ratio
       
    70     void actualSize2();
       
    71     void svgActualSize();
       
    72     void isNull();
       
    73     void bestMatch();
       
    74     void cacheKey();
       
    75     void detach();
       
    76     void svg();
       
    77     void addFile();
       
    78     void availableSizes();
       
    79     void streamAvailableSizes_data();
       
    80     void streamAvailableSizes();
       
    81     void fromTheme();
       
    82 
       
    83     void task184901_badCache();
       
    84     void task223279_inconsistentAddFile();
       
    85     void task239461_custom_iconengine_crash();
       
    86 
       
    87 private:
       
    88     QString oldCurrentDir;
       
    89 };
       
    90 
       
    91 void tst_QIcon::init()
       
    92 {
       
    93     QString srcdir(QLatin1String(SRCDIR));
       
    94     if (!srcdir.isEmpty()) {
       
    95         oldCurrentDir = QDir::current().absolutePath();
       
    96         QDir::setCurrent(srcdir);
       
    97     }
       
    98 }
       
    99 
       
   100 void tst_QIcon::cleanup()
       
   101 {
       
   102     if (!oldCurrentDir.isEmpty()) {
       
   103         QDir::setCurrent(oldCurrentDir);
       
   104     }
       
   105 }
       
   106 
       
   107 tst_QIcon::tst_QIcon()
       
   108 {
       
   109 }
       
   110 
       
   111 void tst_QIcon::actualSize_data()
       
   112 {
       
   113     QTest::addColumn<QString>("source");
       
   114     QTest::addColumn<QSize>("argument");
       
   115     QTest::addColumn<QSize>("result");
       
   116 
       
   117     // square image
       
   118     QTest::newRow("resource0") << ":/image.png" << QSize(128, 128) << QSize(128, 128);
       
   119     QTest::newRow("resource1") << ":/image.png" << QSize( 64,  64) << QSize( 64,  64);
       
   120     QTest::newRow("resource2") << ":/image.png" << QSize( 32,  64) << QSize( 32,  32);
       
   121     QTest::newRow("resource3") << ":/image.png" << QSize( 16,  64) << QSize( 16,  16);
       
   122     QTest::newRow("resource4") << ":/image.png" << QSize( 16,  128) << QSize( 16,  16);
       
   123     QTest::newRow("resource5") << ":/image.png" << QSize( 128,  16) << QSize( 16,  16);
       
   124     QTest::newRow("resource6") << ":/image.png" << QSize( 150,  150) << QSize( 128,  128);
       
   125     // rect image
       
   126     QTest::newRow("resource7") << ":/rect.png" << QSize( 20,  40) << QSize( 20,  40);
       
   127     QTest::newRow("resource8") << ":/rect.png" << QSize( 10,  20) << QSize( 10,  20);
       
   128     QTest::newRow("resource9") << ":/rect.png" << QSize( 15,  50) << QSize( 15,  30);
       
   129     QTest::newRow("resource10") << ":/rect.png" << QSize( 25,  50) << QSize( 20,  40);
       
   130 
       
   131     const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/");
       
   132     QTest::newRow("external0") << prefix + "image.png" << QSize(128, 128) << QSize(128, 128);
       
   133     QTest::newRow("external1") << prefix + "image.png" << QSize( 64,  64) << QSize( 64,  64);
       
   134     QTest::newRow("external2") << prefix + "image.png" << QSize( 32,  64) << QSize( 32,  32);
       
   135     QTest::newRow("external3") << prefix + "image.png" << QSize( 16,  64) << QSize( 16,  16);
       
   136     QTest::newRow("external4") << prefix + "image.png" << QSize( 16,  128) << QSize( 16,  16);
       
   137     QTest::newRow("external5") << prefix + "image.png" << QSize( 128,  16) << QSize( 16,  16);
       
   138     QTest::newRow("external6") << prefix + "image.png" << QSize( 150,  150) << QSize( 128,  128);
       
   139     // rect image
       
   140     QTest::newRow("external7") << ":/rect.png" << QSize( 20,  40) << QSize( 20,  40);
       
   141     QTest::newRow("external8") << ":/rect.png" << QSize( 10,  20) << QSize( 10,  20);
       
   142     QTest::newRow("external9") << ":/rect.png" << QSize( 15,  50) << QSize( 15,  30);
       
   143     QTest::newRow("external10") << ":/rect.png" << QSize( 25,  50) << QSize( 20,  40);
       
   144 }
       
   145 
       
   146 void tst_QIcon::actualSize()
       
   147 {
       
   148     QFETCH(QString, source);
       
   149     QFETCH(QSize, argument);
       
   150     QFETCH(QSize, result);
       
   151 
       
   152     {
       
   153         QPixmap pixmap(source);
       
   154         QIcon icon(pixmap);
       
   155         QCOMPARE(icon.actualSize(argument), result);
       
   156         QCOMPARE(icon.pixmap(argument).size(), result);
       
   157     }
       
   158 
       
   159     {
       
   160         QIcon icon(source);
       
   161         QCOMPARE(icon.actualSize(argument), result);
       
   162         QCOMPARE(icon.pixmap(argument).size(), result);
       
   163     }
       
   164 }
       
   165 
       
   166 void tst_QIcon::actualSize2_data()
       
   167 {
       
   168     QTest::addColumn<QSize>("argument");
       
   169     QTest::addColumn<QSize>("result");
       
   170 
       
   171     // two images - 128x128 and 20x40. Let the games begin
       
   172     QTest::newRow("trivial1") << QSize( 128,  128) << QSize( 128,  128);
       
   173     QTest::newRow("trivial2") << QSize( 20,  40) << QSize( 20,  40);
       
   174 
       
   175     // QIcon chooses the one with the smallest area to choose the pixmap
       
   176     QTest::newRow("best1") << QSize( 100,  100) << QSize( 100,  100);
       
   177     QTest::newRow("best2") << QSize( 20,  20) << QSize( 10,  20);
       
   178     QTest::newRow("best3") << QSize( 15,  30) << QSize( 15,  30);
       
   179     QTest::newRow("best4") << QSize( 5,  5) << QSize( 2,  5);
       
   180     QTest::newRow("best5") << QSize( 10,  15) << QSize( 7,  15);
       
   181 }
       
   182 
       
   183 void tst_QIcon::actualSize2()
       
   184 {
       
   185     QIcon icon;
       
   186     const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/");
       
   187 
       
   188     icon.addPixmap(QPixmap(prefix + "image.png"));
       
   189     icon.addPixmap(QPixmap(prefix + "rect.png"));
       
   190 
       
   191     QFETCH(QSize, argument);
       
   192     QFETCH(QSize, result);
       
   193 
       
   194     QCOMPARE(icon.actualSize(argument), result);
       
   195     QCOMPARE(icon.pixmap(argument).size(), result);
       
   196 }
       
   197 
       
   198 void tst_QIcon::svgActualSize()
       
   199 {
       
   200     const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/");
       
   201     QIcon icon(prefix + "rect.svg");
       
   202     QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 2));
       
   203     QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 2));
       
   204 
       
   205     QPixmap p(16, 16);
       
   206     p.fill(Qt::cyan);
       
   207     icon.addPixmap(p);
       
   208 
       
   209     QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 16));
       
   210     QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 16));
       
   211 
       
   212     QCOMPARE(icon.actualSize(QSize(16, 14)), QSize(16, 2));
       
   213     QCOMPARE(icon.pixmap(QSize(16, 14)).size(), QSize(16, 2));
       
   214 }
       
   215 
       
   216 void tst_QIcon::isNull() {
       
   217     // test default constructor
       
   218     QIcon defaultConstructor;
       
   219     QVERIFY(defaultConstructor.isNull());
       
   220 
       
   221     // test copy constructor
       
   222     QVERIFY(QIcon(defaultConstructor).isNull());
       
   223 
       
   224     // test pixmap constructor
       
   225     QPixmap nullPixmap;
       
   226     QVERIFY(QIcon(nullPixmap).isNull());
       
   227 
       
   228     // test string constructor with empty string
       
   229     QIcon iconEmptyString = QIcon(QString());
       
   230     QVERIFY(iconEmptyString.isNull());
       
   231     QVERIFY(!iconEmptyString.actualSize(QSize(32, 32)).isValid());;
       
   232 
       
   233     // test string constructor with non-existing file
       
   234     QIcon iconNoFile = QIcon("imagedoesnotexist");
       
   235     QVERIFY(!iconNoFile.isNull());
       
   236     QVERIFY(!iconNoFile.actualSize(QSize(32, 32)).isValid());
       
   237 
       
   238     // test string constructor with non-existing file with suffix
       
   239     QIcon iconNoFileSuffix = QIcon("imagedoesnotexist.png");
       
   240     QVERIFY(!iconNoFileSuffix.isNull());
       
   241     QVERIFY(!iconNoFileSuffix.actualSize(QSize(32, 32)).isValid());
       
   242 
       
   243     const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/");
       
   244     // test string constructor with existing file but unsupported format
       
   245     QIcon iconUnsupportedFormat = QIcon(prefix + "tst_qicon.cpp");
       
   246     QVERIFY(!iconUnsupportedFormat.isNull());
       
   247     QVERIFY(!iconUnsupportedFormat.actualSize(QSize(32, 32)).isValid());
       
   248 
       
   249     // test string constructor with existing file and supported format
       
   250     QIcon iconSupportedFormat = QIcon(prefix + "image.png");
       
   251     QVERIFY(!iconSupportedFormat.isNull());
       
   252     QVERIFY(iconSupportedFormat.actualSize(QSize(32, 32)).isValid());
       
   253 }
       
   254 
       
   255 void tst_QIcon::bestMatch()
       
   256 {
       
   257     QPixmap p1(1, 1);
       
   258     QPixmap p2(2, 2);
       
   259     QPixmap p3(3, 3);
       
   260     QPixmap p4(4, 4);
       
   261     QPixmap p5(5, 5);
       
   262     QPixmap p6(6, 6);
       
   263     QPixmap p7(7, 7);
       
   264     QPixmap p8(8, 8);
       
   265 
       
   266     p1.fill(Qt::black);
       
   267     p2.fill(Qt::black);
       
   268     p3.fill(Qt::black);
       
   269     p4.fill(Qt::black);
       
   270     p5.fill(Qt::black);
       
   271     p6.fill(Qt::black);
       
   272     p7.fill(Qt::black);
       
   273     p8.fill(Qt::black);
       
   274 
       
   275     for (int i = 0; i < 4; ++i) {
       
   276         for (int j = 0; j < 2; ++j) {
       
   277             QIcon::State state = (j == 0) ? QIcon::On : QIcon::Off;
       
   278             QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off
       
   279                                                               : QIcon::On;
       
   280             QIcon::Mode mode;
       
   281             QIcon::Mode oppositeMode;
       
   282 
       
   283             QIcon icon;
       
   284 
       
   285             switch (i) {
       
   286             case 0:
       
   287             default:
       
   288                 mode = QIcon::Normal;
       
   289                 oppositeMode = QIcon::Active;
       
   290                 break;
       
   291             case 1:
       
   292                 mode = QIcon::Active;
       
   293                 oppositeMode = QIcon::Normal;
       
   294                 break;
       
   295             case 2:
       
   296                 mode = QIcon::Disabled;
       
   297                 oppositeMode = QIcon::Selected;
       
   298                 break;
       
   299             case 3:
       
   300                 mode = QIcon::Selected;
       
   301                 oppositeMode = QIcon::Disabled;
       
   302             }
       
   303 
       
   304             /*
       
   305                 The test mirrors the code in
       
   306                 QPixmapIconEngine::bestMatch(), to make sure that
       
   307                 nobody breaks QPixmapIconEngine by mistake. Before
       
   308                 you change this test or the code that it tests,
       
   309                 please talk to the maintainer if possible.
       
   310             */
       
   311             if (mode == QIcon::Disabled || mode == QIcon::Selected) {
       
   312                 icon.addPixmap(p1, oppositeMode, oppositeState);
       
   313                 QVERIFY(icon.pixmap(100, mode, state).size() == p1.size());
       
   314 
       
   315                 icon.addPixmap(p2, oppositeMode, state);
       
   316                 QVERIFY(icon.pixmap(100, mode, state).size() == p2.size());
       
   317 
       
   318                 icon.addPixmap(p3, QIcon::Active, oppositeState);
       
   319                 QVERIFY(icon.pixmap(100, mode, state).size() == p3.size());
       
   320 
       
   321                 icon.addPixmap(p4, QIcon::Normal, oppositeState);
       
   322                 QVERIFY(icon.pixmap(100, mode, state).size() == p4.size());
       
   323 
       
   324                 icon.addPixmap(p5, mode, oppositeState);
       
   325                 QVERIFY(icon.pixmap(100, mode, state).size() == p5.size());
       
   326 
       
   327                 icon.addPixmap(p6, QIcon::Active, state);
       
   328                 QVERIFY(icon.pixmap(100, mode, state).size() == p6.size());
       
   329 
       
   330                 icon.addPixmap(p7, QIcon::Normal, state);
       
   331                 QVERIFY(icon.pixmap(100, mode, state).size() == p7.size());
       
   332 
       
   333                 icon.addPixmap(p8, mode, state);
       
   334                 QVERIFY(icon.pixmap(100, mode, state).size() == p8.size());
       
   335             } else {
       
   336                 icon.addPixmap(p1, QIcon::Selected, oppositeState);
       
   337                 QVERIFY(icon.pixmap(100, mode, state).size() == p1.size());
       
   338 
       
   339                 icon.addPixmap(p2, QIcon::Disabled, oppositeState);
       
   340                 QVERIFY(icon.pixmap(100, mode, state).size() == p2.size());
       
   341 
       
   342                 icon.addPixmap(p3, QIcon::Selected, state);
       
   343                 QVERIFY(icon.pixmap(100, mode, state).size() == p3.size());
       
   344 
       
   345                 icon.addPixmap(p4, QIcon::Disabled, state);
       
   346                 QVERIFY(icon.pixmap(100, mode, state).size() == p4.size());
       
   347 
       
   348                 icon.addPixmap(p5, oppositeMode, oppositeState);
       
   349                 QVERIFY(icon.pixmap(100, mode, state).size() == p5.size());
       
   350 
       
   351                 icon.addPixmap(p6, mode, oppositeState);
       
   352                 QVERIFY(icon.pixmap(100, mode, state).size() == p6.size());
       
   353 
       
   354                 icon.addPixmap(p7, oppositeMode, state);
       
   355                 QVERIFY(icon.pixmap(100, mode, state).size() == p7.size());
       
   356 
       
   357                 icon.addPixmap(p8, mode, state);
       
   358                 QVERIFY(icon.pixmap(100, mode, state).size() == p8.size());
       
   359             }
       
   360         }
       
   361     }
       
   362 }
       
   363 
       
   364 void tst_QIcon::cacheKey()
       
   365 {
       
   366     QIcon icon1("image.png");
       
   367     qint64 icon1_key = icon1.cacheKey();
       
   368     QIcon icon2 = icon1;
       
   369 
       
   370     QVERIFY(icon2.cacheKey() == icon1.cacheKey());
       
   371     icon2.detach();
       
   372     QVERIFY(icon2.cacheKey() != icon1.cacheKey());
       
   373     QVERIFY(icon1.cacheKey() == icon1_key);
       
   374 }
       
   375 
       
   376 void tst_QIcon::detach()
       
   377 {
       
   378     QImage img(32, 32, QImage::Format_ARGB32_Premultiplied);
       
   379     img.fill(0xffff0000);
       
   380     QIcon icon1(QPixmap::fromImage(img));
       
   381     QIcon icon2 = icon1;
       
   382     icon2.addFile("image.png", QSize(64, 64));
       
   383 
       
   384     QImage img1 = icon1.pixmap(64, 64).toImage();
       
   385     QImage img2 = icon2.pixmap(64, 64).toImage();
       
   386     QVERIFY(img1 != img2);
       
   387 
       
   388     img1 = icon1.pixmap(32, 32).toImage();
       
   389     img2 = icon2.pixmap(32, 32).toImage();
       
   390     QVERIFY(img1 == img2);
       
   391 }
       
   392 
       
   393 void tst_QIcon::svg()
       
   394 {
       
   395     QIcon icon1("heart.svg");
       
   396 
       
   397     QVERIFY(!icon1.pixmap(32).isNull());
       
   398     QImage img1 = icon1.pixmap(32).toImage();
       
   399     QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull());
       
   400     QImage img2 = icon1.pixmap(32, QIcon::Disabled).toImage();
       
   401 
       
   402     icon1.addFile("trash.svg", QSize(), QIcon::Disabled);
       
   403     QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull());
       
   404     QImage img3 = icon1.pixmap(32, QIcon::Disabled).toImage();
       
   405     QVERIFY(img3 != img2);
       
   406     QVERIFY(img3 != img1);
       
   407 
       
   408     QPixmap pm("image.png");
       
   409     icon1.addPixmap(pm, QIcon::Normal, QIcon::On);
       
   410     QVERIFY(!icon1.pixmap(128, QIcon::Normal, QIcon::On).isNull());
       
   411     QImage img4 = icon1.pixmap(128, QIcon::Normal, QIcon::On).toImage();
       
   412     QVERIFY(img4 != img3);
       
   413     QVERIFY(img4 != img2);
       
   414     QVERIFY(img4 != img1);
       
   415 
       
   416     QIcon icon2;
       
   417     icon2.addFile("heart.svg");
       
   418     QVERIFY(icon2.pixmap(57).toImage() == icon1.pixmap(57).toImage());
       
   419 
       
   420     QIcon icon3("trash.svg");
       
   421     icon3.addFile("heart.svg");
       
   422     QVERIFY(icon3.pixmap(57).toImage() == icon1.pixmap(57).toImage());
       
   423 
       
   424     QIcon icon4("heart.svg");
       
   425     icon4.addFile("image.png", QSize(), QIcon::Active);
       
   426     QVERIFY(!icon4.pixmap(32).isNull());
       
   427     QVERIFY(!icon4.pixmap(32, QIcon::Active).isNull());
       
   428     QVERIFY(icon4.pixmap(32).toImage() == img1);
       
   429     QIcon pmIcon(pm);
       
   430     QVERIFY(icon4.pixmap(pm.size(), QIcon::Active).toImage() == pmIcon.pixmap(pm.size(), QIcon::Active).toImage());
       
   431 
       
   432 #ifndef QT_NO_COMPRESS
       
   433     QIcon icon5("heart.svgz");
       
   434     QVERIFY(!icon5.pixmap(32).isNull());
       
   435 #endif
       
   436 }
       
   437 
       
   438 void tst_QIcon::addFile()
       
   439 {
       
   440     QIcon icon;
       
   441     icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png"));
       
   442     icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-32.png"));
       
   443     icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-128.png"));
       
   444     icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png"), QSize(), QIcon::Selected);
       
   445     icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-32.png"), QSize(), QIcon::Selected);
       
   446     icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-128.png"), QSize(), QIcon::Selected);
       
   447 
       
   448 #ifndef Q_OS_WINCE
       
   449     QVERIFY(icon.pixmap(16, QIcon::Normal).toImage() ==
       
   450             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")).toImage());
       
   451     QVERIFY(icon.pixmap(32, QIcon::Normal).toImage() ==
       
   452             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-32.png")).toImage());
       
   453     QVERIFY(icon.pixmap(128, QIcon::Normal).toImage() ==
       
   454             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-128.png")).toImage());
       
   455     QVERIFY(icon.pixmap(16, QIcon::Selected).toImage() ==
       
   456             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png")).toImage());
       
   457     QVERIFY(icon.pixmap(32, QIcon::Selected).toImage() ==
       
   458             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-32.png")).toImage());
       
   459     QVERIFY(icon.pixmap(128, QIcon::Selected).toImage() ==
       
   460             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-128.png")).toImage());
       
   461 #else
       
   462     // WinCE only includes the 16x16 images for size reasons
       
   463     QVERIFY(icon.pixmap(16, QIcon::Normal).toImage() ==
       
   464             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")).toImage());
       
   465     QVERIFY(icon.pixmap(16, QIcon::Selected).toImage() ==
       
   466             QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png")).toImage());
       
   467 #endif
       
   468 }
       
   469 
       
   470 static bool sizeLess(const QSize &a, const QSize &b)
       
   471 {
       
   472     return a.width() < b.width();
       
   473 }
       
   474 
       
   475 void tst_QIcon::availableSizes()
       
   476 {
       
   477     {
       
   478         QIcon icon;
       
   479         icon.addFile("image.png", QSize(32,32));
       
   480         icon.addFile("image.png", QSize(64,64));
       
   481         icon.addFile("image.png", QSize(128,128));
       
   482         icon.addFile("image.png", QSize(256,256), QIcon::Disabled);
       
   483         icon.addFile("image.png", QSize(16,16), QIcon::Normal, QIcon::On);
       
   484 
       
   485         QList<QSize> availableSizes = icon.availableSizes();
       
   486         QCOMPARE(availableSizes.size(), 3);
       
   487         qSort(availableSizes.begin(), availableSizes.end(), sizeLess);
       
   488         QCOMPARE(availableSizes.at(0), QSize(32,32));
       
   489         QCOMPARE(availableSizes.at(1), QSize(64,64));
       
   490         QCOMPARE(availableSizes.at(2), QSize(128,128));
       
   491 
       
   492         availableSizes = icon.availableSizes(QIcon::Disabled);
       
   493         QCOMPARE(availableSizes.size(), 1);
       
   494         QCOMPARE(availableSizes.at(0), QSize(256,256));
       
   495 
       
   496         availableSizes = icon.availableSizes(QIcon::Normal, QIcon::On);
       
   497         QCOMPARE(availableSizes.size(), 1);
       
   498         QCOMPARE(availableSizes.at(0), QSize(16,16));
       
   499     }
       
   500 
       
   501     {
       
   502         // checks that there are no availableSizes for scalable images.
       
   503         QIcon icon("heart.svg");
       
   504         QList<QSize> availableSizes = icon.availableSizes();
       
   505         QVERIFY(availableSizes.isEmpty());
       
   506     }
       
   507 
       
   508     {
       
   509         // even if an a scalable image contain added pixmaps,
       
   510         // availableSizes still should be empty.
       
   511         QIcon icon("heart.svg");
       
   512         icon.addFile("image.png", QSize(32,32));
       
   513         QList<QSize> availableSizes = icon.availableSizes();
       
   514         QVERIFY(availableSizes.isEmpty());
       
   515     }
       
   516 
       
   517     {
       
   518         // we try to load an icon from resources
       
   519         QIcon icon(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png"));
       
   520         QList<QSize> availableSizes = icon.availableSizes();
       
   521         QCOMPARE(availableSizes.size(), 1);
       
   522         QCOMPARE(availableSizes.at(0), QSize(16, 16));
       
   523     }
       
   524 
       
   525     {
       
   526         // load an icon from binary data.
       
   527         QPixmap pix;
       
   528         QFile file(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png"));
       
   529         QVERIFY(file.open(QIODevice::ReadOnly));
       
   530         uchar *data = file.map(0, file.size());
       
   531         QVERIFY(data != 0);
       
   532         pix.loadFromData(data, file.size());
       
   533         QIcon icon(pix);
       
   534 
       
   535         QList<QSize> availableSizes = icon.availableSizes();
       
   536         QCOMPARE(availableSizes.size(), 1);
       
   537         QCOMPARE(availableSizes.at(0), QSize(16,16));
       
   538     }
       
   539 
       
   540     {
       
   541         // there shouldn't be available sizes for invalid images!
       
   542         QVERIFY(QIcon(QLatin1String("")).availableSizes().isEmpty());
       
   543         QVERIFY(QIcon(QLatin1String("non-existing.png")).availableSizes().isEmpty());
       
   544     }
       
   545 }
       
   546 
       
   547 void tst_QIcon::streamAvailableSizes_data()
       
   548 {
       
   549     QTest::addColumn<QIcon>("icon");
       
   550 
       
   551     QIcon icon;
       
   552     icon.addFile(":/image.png", QSize(32,32));
       
   553     QTest::newRow( "32x32" ) << icon;
       
   554     icon.addFile(":/image.png", QSize(64,64));
       
   555     QTest::newRow( "64x64" ) << icon;
       
   556     icon.addFile(":/image.png", QSize(128,128));
       
   557     QTest::newRow( "128x128" ) << icon;
       
   558     icon.addFile(":/image.png", QSize(256,256));
       
   559     QTest::newRow( "256x256" ) << icon;
       
   560 }
       
   561 
       
   562 void tst_QIcon::streamAvailableSizes()
       
   563 {
       
   564     QFETCH(QIcon, icon);
       
   565 
       
   566     QByteArray ba;
       
   567     // write to QByteArray
       
   568     {
       
   569         QBuffer buffer(&ba);
       
   570         buffer.open(QIODevice::WriteOnly);
       
   571         QDataStream stream(&buffer);
       
   572         stream << icon;
       
   573     }
       
   574 
       
   575     // read from QByteArray
       
   576     {
       
   577         QBuffer buffer(&ba);
       
   578         buffer.open(QIODevice::ReadOnly);
       
   579         QDataStream stream(&buffer);
       
   580         QIcon i;
       
   581         stream >> i;
       
   582         QCOMPARE(i.isNull(), icon.isNull());
       
   583         QCOMPARE(i.availableSizes(), icon.availableSizes());
       
   584     }
       
   585 }
       
   586 
       
   587 
       
   588 static inline bool operator<(const QSize &lhs, const QSize &rhs)
       
   589 {
       
   590     if (lhs.width() < rhs.width())
       
   591         return true;
       
   592     else if (lhs.width() == lhs.width())
       
   593         return lhs.height() < lhs.height();
       
   594     return false;
       
   595 }
       
   596 
       
   597 void tst_QIcon::task184901_badCache()
       
   598 {
       
   599     QPixmap pm("image.png");
       
   600     QIcon icon(pm);
       
   601 
       
   602     //the disabled icon must have an effect (grayed)
       
   603     QVERIFY(icon.pixmap(32, QIcon::Normal).toImage() != icon.pixmap(32, QIcon::Disabled).toImage());
       
   604 
       
   605     icon.addPixmap(pm, QIcon::Disabled);
       
   606     //the disabled icon must now be the same as the normal one.
       
   607     QVERIFY( icon.pixmap(32, QIcon::Normal).toImage() == icon.pixmap(32, QIcon::Disabled).toImage() );
       
   608 }
       
   609 
       
   610 void tst_QIcon::fromTheme()
       
   611 {
       
   612     QString searchPath = QLatin1String(":/icons");
       
   613     QIcon::setThemeSearchPaths(QStringList() << searchPath);
       
   614     QVERIFY(QIcon::themeSearchPaths().size() == 1);
       
   615     QCOMPARE(searchPath, QIcon::themeSearchPaths()[0]);
       
   616 
       
   617     QString themeName("testtheme");
       
   618     QIcon::setThemeName(themeName);
       
   619     QCOMPARE(QIcon::themeName(), themeName);
       
   620 
       
   621     // Test normal icon
       
   622     QIcon appointmentIcon = QIcon::fromTheme("appointment-new");
       
   623     QVERIFY(!appointmentIcon.isNull());
       
   624     QVERIFY(!appointmentIcon.availableSizes(QIcon::Normal, QIcon::Off).isEmpty());
       
   625     QVERIFY(appointmentIcon.availableSizes().contains(QSize(16, 16)));
       
   626     QVERIFY(appointmentIcon.availableSizes().contains(QSize(32, 32)));
       
   627     QVERIFY(appointmentIcon.availableSizes().contains(QSize(22, 22)));
       
   628 
       
   629     // Test icon from parent theme
       
   630     QIcon abIcon = QIcon::fromTheme("address-book-new");
       
   631     QVERIFY(!abIcon.isNull());
       
   632     QVERIFY(QIcon::hasThemeIcon("address-book-new"));
       
   633     QVERIFY(!abIcon.availableSizes().isEmpty());
       
   634 
       
   635     // Test non existing icon
       
   636     QIcon noIcon = QIcon::fromTheme("broken-icon");
       
   637     QVERIFY(noIcon.isNull());
       
   638     QVERIFY(!QIcon::hasThemeIcon("broken-icon"));
       
   639 
       
   640     // Test non existing icon with fallback
       
   641     noIcon = QIcon::fromTheme("broken-icon", abIcon);
       
   642     QVERIFY(noIcon.cacheKey() == abIcon.cacheKey());
       
   643 
       
   644     // Test svg-only icon
       
   645     noIcon = QIcon::fromTheme("svg-icon", abIcon);
       
   646     QVERIFY(!noIcon.availableSizes().isEmpty());
       
   647 
       
   648     QByteArray ba;
       
   649     // write to QByteArray
       
   650     {
       
   651         QBuffer buffer(&ba);
       
   652         buffer.open(QIODevice::WriteOnly);
       
   653         QDataStream stream(&buffer);
       
   654         stream << abIcon;
       
   655     }
       
   656 
       
   657     // read from QByteArray
       
   658     {
       
   659         QBuffer buffer(&ba);
       
   660         buffer.open(QIODevice::ReadOnly);
       
   661         QDataStream stream(&buffer);
       
   662         QIcon i;
       
   663         stream >> i;
       
   664         QCOMPARE(i.isNull(), abIcon.isNull());
       
   665         QCOMPARE(i.availableSizes(), abIcon.availableSizes());
       
   666     }
       
   667 
       
   668     // Make sure setting the theme name clears the state
       
   669     QIcon::setThemeName("");
       
   670     abIcon = QIcon::fromTheme("address-book-new");
       
   671     QVERIFY(abIcon.isNull());
       
   672 }
       
   673 
       
   674 
       
   675 void tst_QIcon::task223279_inconsistentAddFile()
       
   676 {
       
   677     QIcon icon1;
       
   678     icon1.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png"));
       
   679     icon1.addFile(QLatin1String("IconThatDoesntExist"), QSize(32, 32));
       
   680     QPixmap pm1 = icon1.pixmap(32, 32);
       
   681 
       
   682     QIcon icon2;
       
   683     icon2.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png"));
       
   684     icon2.addFile(QLatin1String("IconThatDoesntExist"));
       
   685     QPixmap pm2 = icon1.pixmap(32, 32);
       
   686 
       
   687     QCOMPARE(pm1.isNull(), false);
       
   688     QCOMPARE(pm1.size(), QSize(16,16));
       
   689     QCOMPARE(pm1.isNull(), pm2.isNull());
       
   690     QCOMPARE(pm1.size(), pm2.size());
       
   691 }
       
   692 
       
   693 
       
   694 // During detach, v2 engines are cloned, while v1 engines are only
       
   695 // passed on, so v1 engines need to be referenced counted. This test
       
   696 // verifies that the engine is destroyed once and only once.
       
   697 
       
   698 class IconEngine : public QIconEngine
       
   699 {
       
   700 public:
       
   701     ~IconEngine() { destructorCalled++; }
       
   702     virtual void paint(QPainter *, const QRect &, QIcon::Mode, QIcon::State) { }
       
   703     static int destructorCalled;
       
   704 };
       
   705 int IconEngine::destructorCalled = 0;
       
   706 
       
   707 void tst_QIcon::task239461_custom_iconengine_crash()
       
   708 {
       
   709     QIconEngine *engine = new IconEngine();
       
   710     {
       
   711         QIcon icon(engine);
       
   712         QIcon icon2 = icon;
       
   713 
       
   714         QPixmap pixmap(32, 32);
       
   715         icon.addPixmap(pixmap);
       
   716     }
       
   717     QCOMPARE(IconEngine::destructorCalled, 1);
       
   718 }
       
   719 
       
   720 
       
   721 QTEST_MAIN(tst_QIcon)
       
   722 #include "tst_qicon.moc"