diff options
Diffstat (limited to 'tests/auto/qicon')
-rw-r--r-- | tests/auto/qicon/.gitignore | 1 | ||||
-rw-r--r-- | tests/auto/qicon/heart.svg | 55 | ||||
-rw-r--r-- | tests/auto/qicon/heart.svgz | bin | 0 -> 1506 bytes | |||
-rw-r--r-- | tests/auto/qicon/image.png | bin | 0 -> 14743 bytes | |||
-rw-r--r-- | tests/auto/qicon/image.tga | bin | 0 -> 51708 bytes | |||
-rw-r--r-- | tests/auto/qicon/qicon.pro | 18 | ||||
-rw-r--r-- | tests/auto/qicon/rect.png | bin | 0 -> 175 bytes | |||
-rw-r--r-- | tests/auto/qicon/rect.svg | 76 | ||||
-rw-r--r-- | tests/auto/qicon/trash.svg | 58 | ||||
-rw-r--r-- | tests/auto/qicon/tst_qicon.cpp | 614 | ||||
-rw-r--r-- | tests/auto/qicon/tst_qicon.qrc | 6 |
11 files changed, 828 insertions, 0 deletions
diff --git a/tests/auto/qicon/.gitignore b/tests/auto/qicon/.gitignore new file mode 100644 index 0000000..c101ef9 --- /dev/null +++ b/tests/auto/qicon/.gitignore @@ -0,0 +1 @@ +tst_qicon diff --git a/tests/auto/qicon/heart.svg b/tests/auto/qicon/heart.svg new file mode 100644 index 0000000..8c982cd --- /dev/null +++ b/tests/auto/qicon/heart.svg @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --><svg viewBox="100 200 550 500" height="841.88976pt" id="svg1" inkscape:version="0.40+cvs" sodipodi:docbase="C:\Documents and Settings\Jon Phillips\My Documents\projects\clipart-project\submissions" sodipodi:docname="heart-left-highlight.svg" sodipodi:version="0.32" width="595.27559pt" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sodipodi="http://inkscape.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:svg="http://www.w3.org/2000/svg"> +<metadata> +<rdf:RDF xmlns:cc="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> +<cc:Work rdf:about=""> +<dc:title>Heart Left-Highlight</dc:title> +<dc:description>This is a normal valentines day heart.</dc:description> +<dc:subject> +<rdf:Bag> +<rdf:li>holiday</rdf:li> +<rdf:li>valentines</rdf:li> +<rdf:li></rdf:li> +<rdf:li>valentine</rdf:li> +<rdf:li>hash(0x8a091c0)</rdf:li> +<rdf:li>hash(0x8a0916c)</rdf:li> +<rdf:li>signs_and_symbols</rdf:li> +<rdf:li>hash(0x8a091f0)</rdf:li> +<rdf:li>day</rdf:li> +</rdf:Bag> +</dc:subject> +<dc:publisher> +<cc:Agent rdf:about="http://www.openclipart.org"> +<dc:title>Jon Phillips</dc:title> +</cc:Agent> +</dc:publisher> +<dc:creator> +<cc:Agent> +<dc:title>Jon Phillips</dc:title> +</cc:Agent> +</dc:creator> +<dc:rights> +<cc:Agent> +<dc:title>Jon Phillips</dc:title> +</cc:Agent> +</dc:rights> +<dc:date></dc:date> +<dc:format>image/svg+xml</dc:format> +<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/> +<cc:license rdf:resource="http://web.resource.org/cc/PublicDomain"/> +<dc:language>en</dc:language> +</cc:Work> +<cc:License rdf:about="http://web.resource.org/cc/PublicDomain"> +<cc:permits rdf:resource="http://web.resource.org/cc/Reproduction"/> +<cc:permits rdf:resource="http://web.resource.org/cc/Distribution"/> +<cc:permits rdf:resource="http://web.resource.org/cc/DerivativeWorks"/> +</cc:License> +</rdf:RDF> +</metadata> +<defs id="defs3"/> +<sodipodi:namedview bordercolor="#666666" borderopacity="1.0" id="base" inkscape:current-layer="layer1" inkscape:cx="549.40674" inkscape:cy="596.00159" inkscape:document-units="px" inkscape:guide-bbox="true" inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:window-height="615" inkscape:window-width="866" inkscape:window-x="88" inkscape:window-y="116" inkscape:zoom="0.35000000" pagecolor="#ffffff" showguides="true"/> +<g id="layer1" inkscape:groupmode="layer" inkscape:label="Layer 1"> +<path d="M 263.41570,235.14588 C 197.17570,235.14588 143.41575,288.90587 143.41575,355.14588 C 143.41575,489.90139 279.34890,525.23318 371.97820,658.45392 C 459.55244,526.05056 600.54070,485.59932 600.54070,355.14588 C 600.54070,288.90588 546.78080,235.14587 480.54070,235.14588 C 432.49280,235.14588 391.13910,263.51631 371.97820,304.33338 C 352.81740,263.51630 311.46370,235.14587 263.41570,235.14588 z " id="path7" sodipodi:nodetypes="ccccccc" style="fill:#e60000;fill-opacity:1.0000000;stroke:#000000;stroke-width:18.700001;stroke-miterlimit:4.0000000;stroke-opacity:1.0000000"/> +<path d="M 265.00000,253.59375 C 207.04033,253.59375 160.00000,300.63407 160.00000,358.59375 C 160.00000,476.50415 278.91857,507.43251 359.96875,624.00000 C 366.52868,614.08205 220.00000,478.47309 220.00000,378.59375 C 220.00000,320.63407 267.04033,273.59375 325.00000,273.59375 C 325.50453,273.59375 325.99718,273.64912 326.50000,273.65625 C 309.22436,261.07286 288.00557,253.59374 265.00000,253.59375 z " id="path220" sodipodi:nodetypes="ccccccc" style="fill:#e6e6e6;fill-opacity:0.64556962;stroke:none;stroke-width:18.700001;stroke-miterlimit:4.0000000;stroke-opacity:1.0000000"/> +</g> +</svg> diff --git a/tests/auto/qicon/heart.svgz b/tests/auto/qicon/heart.svgz Binary files differnew file mode 100644 index 0000000..0f0913f --- /dev/null +++ b/tests/auto/qicon/heart.svgz diff --git a/tests/auto/qicon/image.png b/tests/auto/qicon/image.png Binary files differnew file mode 100644 index 0000000..8d70364 --- /dev/null +++ b/tests/auto/qicon/image.png diff --git a/tests/auto/qicon/image.tga b/tests/auto/qicon/image.tga Binary files differnew file mode 100644 index 0000000..0cd507d --- /dev/null +++ b/tests/auto/qicon/image.tga diff --git a/tests/auto/qicon/qicon.pro b/tests/auto/qicon/qicon.pro new file mode 100644 index 0000000..c8d77e8 --- /dev/null +++ b/tests/auto/qicon/qicon.pro @@ -0,0 +1,18 @@ +load(qttest_p4) + +SOURCES += tst_qicon.cpp +RESOURCES = tst_qicon.qrc + + +wince*:{ + QT += xml svg + addFiles.sources = *.png *.tga *.svg *.svgz + addFiles.path = . + DEPLOYMENT += addFiles + DEPLOYMENT_PLUGIN += qsvg + DEFINES += SRCDIR=\\\".\\\" +} else { + DEFINES += SRCDIR=\\\"$$PWD\\\" +} + + diff --git a/tests/auto/qicon/rect.png b/tests/auto/qicon/rect.png Binary files differnew file mode 100644 index 0000000..b5d3ecb --- /dev/null +++ b/tests/auto/qicon/rect.png diff --git a/tests/auto/qicon/rect.svg b/tests/auto/qicon/rect.svg new file mode 100644 index 0000000..8eb2472 --- /dev/null +++ b/tests/auto/qicon/rect.svg @@ -0,0 +1,76 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!-- Created with Inkscape (http://www.inkscape.org/) --> +<svg + xmlns:dc="http://purl.org/dc/elements/1.1/" + xmlns:cc="http://creativecommons.org/ns#" + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:svg="http://www.w3.org/2000/svg" + xmlns="http://www.w3.org/2000/svg" + xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" + xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" + version="1.0" + width="40px" + height="5px" + id="svg2" + sodipodi:version="0.32" + inkscape:version="0.46" + sodipodi:docname="test.svg" + inkscape:output_extension="org.inkscape.output.svg.inkscape"> + <metadata + id="metadata7"> + <rdf:RDF> + <cc:Work + rdf:about=""> + <dc:format>image/svg+xml</dc:format> + <dc:type + rdf:resource="http://purl.org/dc/dcmitype/StillImage" /> + </cc:Work> + </rdf:RDF> + </metadata> + <sodipodi:namedview + inkscape:window-height="1005" + inkscape:window-width="1280" + inkscape:pageshadow="2" + inkscape:pageopacity="0.0" + guidetolerance="10.0" + gridtolerance="10.0" + objecttolerance="10.0" + borderopacity="1.0" + bordercolor="#666666" + pagecolor="#ffffff" + id="base" + showgrid="false" + inkscape:zoom="13.6" + inkscape:cx="20" + inkscape:cy="2.5" + inkscape:window-x="-4" + inkscape:window-y="-4" + inkscape:current-layer="svg2" /> + <defs + id="defs4"> + <inkscape:perspective + sodipodi:type="inkscape:persp3d" + inkscape:vp_x="0 : 2.5 : 1" + inkscape:vp_y="0 : 1000 : 0" + inkscape:vp_z="40 : 2.5 : 1" + inkscape:persp3d-origin="20 : 1.6666667 : 1" + id="perspective9" /> + </defs> + <g + id="layer1"> + <rect + width="40px" + height="5px" + x="0px" + y="0px" + id="rect2393" + style="fill:#ff0000" /> + </g> + <rect + style="fill:#000000" + id="rect2382" + width="2" + height="2" + x="19" + y="1.5" /> +</svg> diff --git a/tests/auto/qicon/trash.svg b/tests/auto/qicon/trash.svg new file mode 100644 index 0000000..c44e4c7 --- /dev/null +++ b/tests/auto/qicon/trash.svg @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Generator: Adobe Illustrator 10, SVG Export Plug-In . SVG Version: 3.0.0 Build 76) --><svg enable-background="new 0 0 347 348" height="348" i:pageBounds="0 792 612 0" i:rulerOrigin="0 0" i:viewOrigin="131 567" overflow="visible" space="preserve" viewBox="-20 -20 387 388" width="347" xmlns="http://www.w3.org/2000/svg" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" xmlns:graph="http://ns.adobe.com/Graphs/1.0/" xmlns:i="http://ns.adobe.com/AdobeIllustrator/10.0/" xmlns:x="http://ns.adobe.com/Extensibility/1.0/" xmlns:xlink="http://www.w3.org/1999/xlink"> +<metadata> +<rdf:RDF xmlns:cc="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> +<cc:Work rdf:about=""> +<dc:title>Keep Tidy Inside</dc:title> +<dc:description></dc:description> +<dc:subject> +<rdf:Bag> +<rdf:li></rdf:li> +<rdf:li>symbol</rdf:li> +<rdf:li>bin</rdf:li> +<rdf:li>signs_and_symbols</rdf:li> +<rdf:li>clean</rdf:li> +<rdf:li>rubish</rdf:li> +<rdf:li>trash</rdf:li> +<rdf:li>inside</rdf:li> +<rdf:li>garbage</rdf:li> +<rdf:li>sign</rdf:li> +</rdf:Bag> +</dc:subject> +<dc:publisher> +<cc:Agent rdf:about="http://www.openclipart.org"> +<dc:title>Martin Owens</dc:title> +</cc:Agent> +</dc:publisher> +<dc:creator> +<cc:Agent> +<dc:title>Martin Owens</dc:title> +</cc:Agent> +</dc:creator> +<dc:rights> +<cc:Agent> +<dc:title>Martin Owens</dc:title> +</cc:Agent> +</dc:rights> +<dc:date></dc:date> +<dc:format>image/svg+xml</dc:format> +<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/> +<cc:license rdf:resource="http://web.resource.org/cc/PublicDomain"/> +<dc:language>en</dc:language> +</cc:Work> +<cc:License rdf:about="http://web.resource.org/cc/PublicDomain"> +<cc:permits rdf:resource="http://web.resource.org/cc/Reproduction"/> +<cc:permits rdf:resource="http://web.resource.org/cc/Distribution"/> +<cc:permits rdf:resource="http://web.resource.org/cc/DerivativeWorks"/> +</cc:License> +</rdf:RDF> +</metadata> +<g i:dimmedPercent="50" i:knockout="Off" i:layer="yes" i:rgbTrio="#4F008000FFFF" id="Layer_1"> +<path d="M347,174c0,96.098-77.679,174-173.5,174C77.679,348,0,270.098,0,174 C0,77.902,77.679,0,173.5,0C269.321,0,347,77.902,347,174z" fill="#10A040" i:knockout="Off"/> +<path d="M238,53c0,13.807-11.864,25-26.5,25S185,66.807,185,53s11.864-25,26.5-25 S238,39.193,238,53z" fill="#FFFFFF" i:knockout="Off"/> +<path d="M66,175c1.055,6.355,19.333,126.417,19.333,126.417h68.333 c0,0,14.105-122.524,14.333-126.417c6.224-0.622,6.667-13-2-13c-12.164,0-89.205-0.059-98,0S61.167,174.487,66,175z" fill="#FFFFFF" i:knockout="Off"/> +<path d="M78,141c17.292-5.325,24.179-23.532,27-31c14.513,6.596,40.333,12.265,59,8 c3.683,19.419-28.043,19.31-23,37C132.577,145.705,89.404,167.292,78,141z" fill="#FFFFFF" i:knockout="Off"/> +<path d="M103,82l139-1c-0.6,3.421,33.633,57.497,29,67c-4.089,0.418-67,5-67,5 c6.109-9.379-13-43-13-43L103,82z" fill="#FFFFFF" i:knockout="Off"/> +<path d="M270,156l-66-3c0,0-23.565,143.355-24,145s1.855,2.536,3,1s51-82,51-82 s19.754,80.701,20,82s3.721,1.209,4,0S270,156,270,156z" fill="#FFFFFF" i:knockout="Off"/> +</g> +</svg> diff --git a/tests/auto/qicon/tst_qicon.cpp b/tests/auto/qicon/tst_qicon.cpp new file mode 100644 index 0000000..7e515a5 --- /dev/null +++ b/tests/auto/qicon/tst_qicon.cpp @@ -0,0 +1,614 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <qicon.h> +#include <qiconengine.h> + +Q_DECLARE_METATYPE(QSize) + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QIcon : public QObject +{ + Q_OBJECT +public: + tst_QIcon(); + +private slots: + void init(); + void cleanup(); + + void actualSize_data(); // test with 1 pixmap + void actualSize(); + void actualSize2_data(); // test with 2 pixmaps with different aspect ratio + void actualSize2(); + void svgActualSize(); + void isNull(); + void bestMatch(); + void cacheKey(); + void detach(); + void svg(); + void addFile(); + void availableSizes(); + + void task184901_badCache(); + void task223279_inconsistentAddFile(); + void task239461_custom_iconengine_crash(); + +private: + QString oldCurrentDir; +}; + +void tst_QIcon::init() +{ + QString srcdir(QLatin1String(SRCDIR)); + if (!srcdir.isEmpty()) { + oldCurrentDir = QDir::current().absolutePath(); + QDir::setCurrent(srcdir); + } +} + +void tst_QIcon::cleanup() +{ + if (!oldCurrentDir.isEmpty()) { + QDir::setCurrent(oldCurrentDir); + } +} + +tst_QIcon::tst_QIcon() +{ +} + +void tst_QIcon::actualSize_data() +{ + QTest::addColumn<QString>("source"); + QTest::addColumn<QSize>("argument"); + QTest::addColumn<QSize>("result"); + + // square image + QTest::newRow("resource0") << ":/image.png" << QSize(128, 128) << QSize(128, 128); + QTest::newRow("resource1") << ":/image.png" << QSize( 64, 64) << QSize( 64, 64); + QTest::newRow("resource2") << ":/image.png" << QSize( 32, 64) << QSize( 32, 32); + QTest::newRow("resource3") << ":/image.png" << QSize( 16, 64) << QSize( 16, 16); + QTest::newRow("resource4") << ":/image.png" << QSize( 16, 128) << QSize( 16, 16); + QTest::newRow("resource5") << ":/image.png" << QSize( 128, 16) << QSize( 16, 16); + QTest::newRow("resource6") << ":/image.png" << QSize( 150, 150) << QSize( 128, 128); + // rect image + QTest::newRow("resource7") << ":/rect.png" << QSize( 20, 40) << QSize( 20, 40); + QTest::newRow("resource8") << ":/rect.png" << QSize( 10, 20) << QSize( 10, 20); + QTest::newRow("resource9") << ":/rect.png" << QSize( 15, 50) << QSize( 15, 30); + QTest::newRow("resource10") << ":/rect.png" << QSize( 25, 50) << QSize( 20, 40); + + const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); + + QTest::newRow("external0") << prefix + "image.png" << QSize(128, 128) << QSize(128, 128); + QTest::newRow("external1") << prefix + "image.png" << QSize( 64, 64) << QSize( 64, 64); + QTest::newRow("external2") << prefix + "image.png" << QSize( 32, 64) << QSize( 32, 32); + QTest::newRow("external3") << prefix + "image.png" << QSize( 16, 64) << QSize( 16, 16); + QTest::newRow("external4") << prefix + "image.png" << QSize( 16, 128) << QSize( 16, 16); + QTest::newRow("external5") << prefix + "image.png" << QSize( 128, 16) << QSize( 16, 16); + QTest::newRow("external6") << prefix + "image.png" << QSize( 150, 150) << QSize( 128, 128); + // rect image + QTest::newRow("external7") << ":/rect.png" << QSize( 20, 40) << QSize( 20, 40); + QTest::newRow("external8") << ":/rect.png" << QSize( 10, 20) << QSize( 10, 20); + QTest::newRow("external9") << ":/rect.png" << QSize( 15, 50) << QSize( 15, 30); + QTest::newRow("external10") << ":/rect.png" << QSize( 25, 50) << QSize( 20, 40); +} + +void tst_QIcon::actualSize() +{ + QFETCH(QString, source); + QFETCH(QSize, argument); + QFETCH(QSize, result); + + { + QPixmap pixmap(source); + QIcon icon(pixmap); + QCOMPARE(icon.actualSize(argument), result); + QCOMPARE(icon.pixmap(argument).size(), result); + } + + { + QIcon icon(source); + QCOMPARE(icon.actualSize(argument), result); + QCOMPARE(icon.pixmap(argument).size(), result); + } +} + +void tst_QIcon::actualSize2_data() +{ + QTest::addColumn<QSize>("argument"); + QTest::addColumn<QSize>("result"); + + // two images - 128x128 and 20x40. Let the games begin + QTest::newRow("trivial1") << QSize( 128, 128) << QSize( 128, 128); + QTest::newRow("trivial2") << QSize( 20, 40) << QSize( 20, 40); + + // QIcon chooses the one with the smallest area to choose the pixmap + QTest::newRow("best1") << QSize( 100, 100) << QSize( 100, 100); + QTest::newRow("best2") << QSize( 20, 20) << QSize( 10, 20); + QTest::newRow("best3") << QSize( 15, 30) << QSize( 15, 30); + QTest::newRow("best4") << QSize( 5, 5) << QSize( 2, 5); + QTest::newRow("best5") << QSize( 10, 15) << QSize( 7, 15); +} + +void tst_QIcon::actualSize2() +{ + QIcon icon; + + const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); + + icon.addPixmap(QPixmap(prefix + "image.png")); + icon.addPixmap(QPixmap(prefix + "rect.png")); + + QFETCH(QSize, argument); + QFETCH(QSize, result); + +#if QT_VERSION >= 0x040200 + QCOMPARE(icon.actualSize(argument), result); + QCOMPARE(icon.pixmap(argument).size(), result); +#endif +} + +void tst_QIcon::svgActualSize() +{ + const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); + QIcon icon(prefix + "rect.svg"); + QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 2)); + QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 2)); + + QPixmap p(16, 16); + p.fill(Qt::cyan); + icon.addPixmap(p); + + QCOMPARE(icon.actualSize(QSize(16, 16)), QSize(16, 16)); + QCOMPARE(icon.pixmap(QSize(16, 16)).size(), QSize(16, 16)); + + QCOMPARE(icon.actualSize(QSize(16, 14)), QSize(16, 2)); + QCOMPARE(icon.pixmap(QSize(16, 14)).size(), QSize(16, 2)); +} + +void tst_QIcon::isNull() { + // test default constructor + QIcon defaultConstructor; + QVERIFY(defaultConstructor.isNull()); + + // test copy constructor + QVERIFY(QIcon(defaultConstructor).isNull()); + + // test pixmap constructor + QPixmap nullPixmap; + QVERIFY(QIcon(nullPixmap).isNull()); + + // test string constructor with empty string + QIcon iconEmptyString = QIcon(QString()); + QVERIFY(iconEmptyString.isNull()); + QVERIFY(!iconEmptyString.actualSize(QSize(32, 32)).isValid());; + + // test string constructor with non-existing file + QIcon iconNoFile = QIcon("imagedoesnotexist"); + QVERIFY(!iconNoFile.isNull()); + QVERIFY(!iconNoFile.actualSize(QSize(32, 32)).isValid()); + + // test string constructor with non-existing file with suffix + QIcon iconNoFileSuffix = QIcon("imagedoesnotexist.png"); + QVERIFY(!iconNoFileSuffix.isNull()); + QVERIFY(!iconNoFileSuffix.actualSize(QSize(32, 32)).isValid()); + + const QString prefix = QLatin1String(SRCDIR) + QLatin1String("/"); + + // test string constructor with existing file but unsupported format + QIcon iconUnsupportedFormat = QIcon(prefix + "image.tga"); + QVERIFY(!iconUnsupportedFormat.isNull()); + QVERIFY(!iconUnsupportedFormat.actualSize(QSize(32, 32)).isValid()); + + // test string constructor with existing file and supported format + QIcon iconSupportedFormat = QIcon(prefix + "image.png"); + QVERIFY(!iconSupportedFormat.isNull()); + QVERIFY(iconSupportedFormat.actualSize(QSize(32, 32)).isValid()); +} + +void tst_QIcon::bestMatch() +{ + QPixmap p1(1, 1); + QPixmap p2(2, 2); + QPixmap p3(3, 3); + QPixmap p4(4, 4); + QPixmap p5(5, 5); + QPixmap p6(6, 6); + QPixmap p7(7, 7); + QPixmap p8(8, 8); + + p1.fill(Qt::black); + p2.fill(Qt::black); + p3.fill(Qt::black); + p4.fill(Qt::black); + p5.fill(Qt::black); + p6.fill(Qt::black); + p7.fill(Qt::black); + p8.fill(Qt::black); + + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 2; ++j) { + QIcon::State state = (j == 0) ? QIcon::On : QIcon::Off; + QIcon::State oppositeState = (state == QIcon::On) ? QIcon::Off + : QIcon::On; + QIcon::Mode mode; + QIcon::Mode oppositeMode; + + QIcon icon; + + switch (i) { + case 0: + default: + mode = QIcon::Normal; + oppositeMode = QIcon::Active; + break; + case 1: + mode = QIcon::Active; + oppositeMode = QIcon::Normal; + break; + case 2: + mode = QIcon::Disabled; + oppositeMode = QIcon::Selected; + break; + case 3: + mode = QIcon::Selected; + oppositeMode = QIcon::Disabled; + } + + /* + The test mirrors the code in + QPixmapIconEngine::bestMatch(), to make sure that + nobody breaks QPixmapIconEngine by mistake. Before + you change this test or the code that it tests, + please talk to the maintainer if possible. + */ + if (mode == QIcon::Disabled || mode == QIcon::Selected) { + icon.addPixmap(p1, oppositeMode, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p1.size()); + + icon.addPixmap(p2, oppositeMode, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p2.size()); + + icon.addPixmap(p3, QIcon::Active, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p3.size()); + + icon.addPixmap(p4, QIcon::Normal, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p4.size()); + + icon.addPixmap(p5, mode, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p5.size()); + + icon.addPixmap(p6, QIcon::Active, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p6.size()); + + icon.addPixmap(p7, QIcon::Normal, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p7.size()); + + icon.addPixmap(p8, mode, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p8.size()); + } else { + icon.addPixmap(p1, QIcon::Selected, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p1.size()); + + icon.addPixmap(p2, QIcon::Disabled, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p2.size()); + + icon.addPixmap(p3, QIcon::Selected, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p3.size()); + + icon.addPixmap(p4, QIcon::Disabled, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p4.size()); + + icon.addPixmap(p5, oppositeMode, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p5.size()); + + icon.addPixmap(p6, mode, oppositeState); + QVERIFY(icon.pixmap(100, mode, state).size() == p6.size()); + + icon.addPixmap(p7, oppositeMode, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p7.size()); + + icon.addPixmap(p8, mode, state); + QVERIFY(icon.pixmap(100, mode, state).size() == p8.size()); + } + } + } +} + +void tst_QIcon::cacheKey() +{ + QIcon icon1("image.png"); + qint64 icon1_key = icon1.cacheKey(); + QIcon icon2 = icon1; + + QVERIFY(icon2.cacheKey() == icon1.cacheKey()); + icon2.detach(); + QVERIFY(icon2.cacheKey() != icon1.cacheKey()); + QVERIFY(icon1.cacheKey() == icon1_key); +} + +void tst_QIcon::detach() +{ + QImage img(32, 32, QImage::Format_ARGB32_Premultiplied); + img.fill(0xffff0000); + QIcon icon1(QPixmap::fromImage(img)); + QIcon icon2 = icon1; + icon2.addFile("image.png", QSize(64, 64)); + + QImage img1 = icon1.pixmap(64, 64).toImage(); + QImage img2 = icon2.pixmap(64, 64).toImage(); + QVERIFY(img1 != img2); + + img1 = icon1.pixmap(32, 32).toImage(); + img2 = icon2.pixmap(32, 32).toImage(); + QVERIFY(img1 == img2); +} + +void tst_QIcon::svg() +{ + QIcon icon1("heart.svg"); + + QVERIFY(!icon1.pixmap(32).isNull()); + QImage img1 = icon1.pixmap(32).toImage(); + QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull()); + QImage img2 = icon1.pixmap(32, QIcon::Disabled).toImage(); + + icon1.addFile("trash.svg", QSize(), QIcon::Disabled); + QVERIFY(!icon1.pixmap(32, QIcon::Disabled).isNull()); + QImage img3 = icon1.pixmap(32, QIcon::Disabled).toImage(); + QVERIFY(img3 != img2); + QVERIFY(img3 != img1); + + QPixmap pm("image.png"); + icon1.addPixmap(pm, QIcon::Normal, QIcon::On); + QVERIFY(!icon1.pixmap(128, QIcon::Normal, QIcon::On).isNull()); + QImage img4 = icon1.pixmap(128, QIcon::Normal, QIcon::On).toImage(); + QVERIFY(img4 != img3); + QVERIFY(img4 != img2); + QVERIFY(img4 != img1); + + QIcon icon2; + icon2.addFile("heart.svg"); + QVERIFY(icon2.pixmap(57).toImage() == icon1.pixmap(57).toImage()); + + QIcon icon3("trash.svg"); + icon3.addFile("heart.svg"); + QVERIFY(icon3.pixmap(57).toImage() == icon1.pixmap(57).toImage()); + + QIcon icon4("heart.svg"); + icon4.addFile("image.png", QSize(), QIcon::Active); + QVERIFY(!icon4.pixmap(32).isNull()); + QVERIFY(!icon4.pixmap(32, QIcon::Active).isNull()); + QVERIFY(icon4.pixmap(32).toImage() == img1); + QIcon pmIcon(pm); + QVERIFY(icon4.pixmap(pm.size(), QIcon::Active).toImage() == pmIcon.pixmap(pm.size(), QIcon::Active).toImage()); + +#ifndef QT_NO_COMPRESS + QIcon icon5("heart.svgz"); + QVERIFY(!icon5.pixmap(32).isNull()); +#endif +} + +void tst_QIcon::addFile() +{ + QIcon icon; + icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); + icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-32.png")); + icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-128.png")); + icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png"), QSize(), QIcon::Selected); + icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-32.png"), QSize(), QIcon::Selected); + icon.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-128.png"), QSize(), QIcon::Selected); + +#ifndef Q_OS_WINCE + QVERIFY(icon.pixmap(16, QIcon::Normal).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")).toImage()); + QVERIFY(icon.pixmap(32, QIcon::Normal).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-32.png")).toImage()); + QVERIFY(icon.pixmap(128, QIcon::Normal).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-128.png")).toImage()); + QVERIFY(icon.pixmap(16, QIcon::Selected).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png")).toImage()); + QVERIFY(icon.pixmap(32, QIcon::Selected).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-32.png")).toImage()); + QVERIFY(icon.pixmap(128, QIcon::Selected).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-128.png")).toImage()); +#else + // WinCE only includes the 16x16 images for size reasons + QVERIFY(icon.pixmap(16, QIcon::Normal).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")).toImage()); + QVERIFY(icon.pixmap(16, QIcon::Selected).toImage() == + QPixmap(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-save-16.png")).toImage()); +#endif +} + +static bool sizeLess(const QSize &a, const QSize &b) +{ + return a.width() < b.width(); +} + +void tst_QIcon::availableSizes() +{ + { + QIcon icon; + icon.addFile("image.png", QSize(32,32)); + icon.addFile("image.png", QSize(64,64)); + icon.addFile("image.png", QSize(128,128)); + icon.addFile("image.png", QSize(256,256), QIcon::Disabled); + icon.addFile("image.png", QSize(16,16), QIcon::Normal, QIcon::On); + + QList<QSize> availableSizes = icon.availableSizes(); + QCOMPARE(availableSizes.size(), 3); + qSort(availableSizes.begin(), availableSizes.end(), sizeLess); + QCOMPARE(availableSizes.at(0), QSize(32,32)); + QCOMPARE(availableSizes.at(1), QSize(64,64)); + QCOMPARE(availableSizes.at(2), QSize(128,128)); + + availableSizes = icon.availableSizes(QIcon::Disabled); + QCOMPARE(availableSizes.size(), 1); + QCOMPARE(availableSizes.at(0), QSize(256,256)); + + availableSizes = icon.availableSizes(QIcon::Normal, QIcon::On); + QCOMPARE(availableSizes.size(), 1); + QCOMPARE(availableSizes.at(0), QSize(16,16)); + } + + { + // checks that there are no availableSizes for scalable images. + QIcon icon("heart.svg"); + QList<QSize> availableSizes = icon.availableSizes(); + QVERIFY(availableSizes.isEmpty()); + } + + { + // even if an a scalable image contain added pixmaps, + // availableSizes still should be empty. + QIcon icon("heart.svg"); + icon.addFile("image.png", QSize(32,32)); + QList<QSize> availableSizes = icon.availableSizes(); + QVERIFY(availableSizes.isEmpty()); + } + + { + // we try to load an icon from resources + QIcon icon(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); + QList<QSize> availableSizes = icon.availableSizes(); + QCOMPARE(availableSizes.size(), 1); + QCOMPARE(availableSizes.at(0), QSize(16, 16)); + } + + { + // load an icon from binary data. + QPixmap pix; + QFile file(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); + QVERIFY(file.open(QIODevice::ReadOnly)); + uchar *data = file.map(0, file.size()); + QVERIFY(data != 0); + pix.loadFromData(data, file.size()); + QIcon icon(pix); + + QList<QSize> availableSizes = icon.availableSizes(); + QCOMPARE(availableSizes.size(), 1); + QCOMPARE(availableSizes.at(0), QSize(16,16)); + } + + { + // there shouldn't be available sizes for invalid images! + QVERIFY(QIcon(QLatin1String("")).availableSizes().isEmpty()); + QVERIFY(QIcon(QLatin1String("non-existing.png")).availableSizes().isEmpty()); + } +} + +static inline bool operator<(const QSize &lhs, const QSize &rhs) +{ + if (lhs.width() < rhs.width()) + return true; + else if (lhs.width() == lhs.width()) + return lhs.height() < lhs.height(); + return false; +} + +void tst_QIcon::task184901_badCache() +{ + QPixmap pm("image.png"); + QIcon icon(pm); + + //the disabled icon must have an effect (grayed) + QVERIFY(icon.pixmap(32, QIcon::Normal).toImage() != icon.pixmap(32, QIcon::Disabled).toImage()); + + icon.addPixmap(pm, QIcon::Disabled); + //the disabled icon must now be the same as the normal one. + QVERIFY( icon.pixmap(32, QIcon::Normal).toImage() == icon.pixmap(32, QIcon::Disabled).toImage() ); +} + +void tst_QIcon::task223279_inconsistentAddFile() +{ + QIcon icon1; + icon1.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); + icon1.addFile(QLatin1String("IconThatDoesntExist"), QSize(32, 32)); + QPixmap pm1 = icon1.pixmap(32, 32); + + QIcon icon2; + icon2.addFile(QLatin1String(":/trolltech/styles/commonstyle/images/standardbutton-open-16.png")); + icon2.addFile(QLatin1String("IconThatDoesntExist")); + QPixmap pm2 = icon1.pixmap(32, 32); + + QCOMPARE(pm1.isNull(), false); + QCOMPARE(pm1.size(), QSize(16,16)); + QCOMPARE(pm1.isNull(), pm2.isNull()); + QCOMPARE(pm1.size(), pm2.size()); +} + + +// During detach, v2 engines are cloned, while v1 engines are only +// passed on, so v1 engines need to be referenced counted. This test +// verifies that the engine is destroyed once and only once. + +class IconEngine : public QIconEngine +{ +public: + ~IconEngine() { destructorCalled++; } + virtual void paint(QPainter *, const QRect &, QIcon::Mode, QIcon::State) { } + static int destructorCalled; +}; +int IconEngine::destructorCalled = 0; + +void tst_QIcon::task239461_custom_iconengine_crash() +{ + QIconEngine *engine = new IconEngine(); + { + QIcon icon(engine); + QIcon icon2 = icon; + + QPixmap pixmap(32, 32); + icon.addPixmap(pixmap); + } + QCOMPARE(IconEngine::destructorCalled, 1); +} + + +QTEST_MAIN(tst_QIcon) +#include "tst_qicon.moc" diff --git a/tests/auto/qicon/tst_qicon.qrc b/tests/auto/qicon/tst_qicon.qrc new file mode 100644 index 0000000..1e1a030 --- /dev/null +++ b/tests/auto/qicon/tst_qicon.qrc @@ -0,0 +1,6 @@ +<!DOCTYPE RCC><RCC version="1.0"> +<qresource prefix="/"> +<file>image.png</file> +<file>rect.png</file> +</qresource> +</RCC> |