diff options
Diffstat (limited to 'tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp')
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp | 147 |
1 files changed, 87 insertions, 60 deletions
diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp index 51892cca04..34f0132865 100644 --- a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp @@ -1,32 +1,7 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QTest> #include <qicon.h> class tst_QIconHighDpi : public QObject @@ -39,7 +14,10 @@ private slots: void initTestCase(); void fromTheme_data(); void fromTheme(); + void addPixmap_data(); + void addPixmap(); void ninePatch(); + void preferUpscale(); }; tst_QIconHighDpi::tst_QIconHighDpi() @@ -53,19 +31,18 @@ void tst_QIconHighDpi::initTestCase() void tst_QIconHighDpi::fromTheme_data() { QTest::addColumn<int>("requestedSize"); + QTest::addColumn<qreal>("requestedDpr"); QTest::addColumn<int>("expectedSize"); QTest::addColumn<qreal>("expectedDpr"); // The pixmaps that we have available can be found in tst_qiconhighdpi.qrc. - // Currently we only have @1 and @2 icons available. - const int dpr = qCeil(qApp->devicePixelRatio()); + // Currently we only have @1 and @2 icons available, which limits the expected + // devicePixelRatio when testing at e.g. devicePixelRatio 3. // We have an @2 high DPI version of the 22x22 size of this icon. - switch (dpr) { - case 1: QTest::newRow("22x22,dpr=1") << 22 << 22 << 1.0; break; - case 2: QTest::newRow("22x22,dpr=2") << 22 << 44 << 2.0; break; - case 3: QTest::newRow("22x22,dpr=3") << 22 << 44 << 2.0; break; - } + QTest::newRow("22x22,dpr=1") << 22 << 1.0 << 22 << 1.0; + QTest::newRow("22x22,dpr=2") << 22 << 2.0 << 44 << 2.0; + QTest::newRow("22x22,dpr=3") << 22 << 3.0 << 44 << 2.0; // We don't have a high DPI version of the 16x16 size of this icon, // so directoryMatchesSize() will return false for all directories. @@ -86,13 +63,11 @@ void tst_QIconHighDpi::fromTheme_data() // Both of these functions are implementations of the freedesktop icon theme spec, // which dictates that if there is no matching scale, directorySizeDistance() determines // the winner, regardless of whether or not the scale is too low for the requested scale. - switch (dpr) { - case 1: QTest::newRow("16x16,dpr=1") << 16 << 16 << 1.0; break; + QTest::newRow("16x16,dpr=1") << 16 << 1.0 << 16 << 1.0; // PixmapEntry::pixmap() will only downscale the pixmap if actualSize.width() > size.width(). // In this case, 22 > 32 is false, so a 22x22 pixmap is returned. - case 2: QTest::newRow("16x16,dpr=2") << 16 << 22 << 1.375; break; - case 3: QTest::newRow("16x16,dpr=3") << 16 << 44 << 2.75; break; - } + QTest::newRow("16x16,dpr=2") << 16 << 2.0 << 22 << 1.375; + QTest::newRow("16x16,dpr=3") << 16 << 3.0 << 44 << 2.75; // We don't have an 8x8 size of this icon, so: // Available size | Available scale | Requested size | Requested scale | Distance @@ -107,11 +82,9 @@ void tst_QIconHighDpi::fromTheme_data() // 22 * 2 - 8 * 3 = 20 // 22 * 1 - 8 * 3 = 2 < (22x22) // 16 * 1 - 8 * 3 = 8 - switch (dpr) { - case 1: QTest::newRow("8x8,dpr=1") << 8 << 8 << 1.0; break; - case 2: QTest::newRow("8x8,dpr=2") << 8 << 16 << 2.0; break; - case 3: QTest::newRow("8x8,dpr=3") << 8 << 22 << 2.75; break; - } + QTest::newRow("8x8,dpr=1") << 8 << 1.0 << 8 << 1.0; + QTest::newRow("8x8,dpr=2") << 8 << 2.0 << 16 << 2.0; + QTest::newRow("8x8,dpr=3") << 8 << 3.0 << 22 << 2.75; // We don't have a 44x44 size of this icon, so: // Available size | Available scale | Requested size | Requested scale | Distance @@ -126,11 +99,9 @@ void tst_QIconHighDpi::fromTheme_data() // 22 * 2 - 44 * 3 = 88 < (22x22@2) // 22 * 1 - 44 * 3 = 110 // 16 * 1 - 44 * 3 = 116 - switch (dpr) { - case 1: QTest::newRow("44x44,dpr=1") << 44 << 44 << 1.0; break; - case 2: QTest::newRow("44x44,dpr=2") << 44 << 44 << 1.0; break; - case 3: QTest::newRow("44x44,dpr=3") << 44 << 44 << 1.0; break; - } + QTest::newRow("44x44,dpr=1") << 44 << 1.0 << 44 << 1.0; + QTest::newRow("44x44,dpr=2") << 44 << 2.0 << 44 << 1.0; + QTest::newRow("44x44,dpr=3") << 44 << 3.0 << 44 << 1.0; // We don't have a 20x20 size of this icon, so: // Available size | Available scale | Requested size | Requested scale | Distance @@ -145,18 +116,17 @@ void tst_QIconHighDpi::fromTheme_data() // 22 * 2 - 20 * 3 = 16 < (22x22@2) // 22 * 1 - 20 * 3 = 38 // 16 * 1 - 20 * 3 = 44 - switch (dpr) { - case 1: QTest::newRow("20x20,dpr=1") << 20 << 20 << 1.0; break; + QTest::newRow("20x20,dpr=1") << 20 << 1.0 << 20 << 1.0; // PixmapEntry::pixmap() will only downscale the pixmap if actualSize.width() > size.width(). // In this case, 44 > 40 is true, so the 44x44 pixmap is downscaled to 40x40. - case 2: QTest::newRow("20x20,dpr=2") << 20 << 40 << 2.0; break; - case 3: QTest::newRow("20x20,dpr=3") << 20 << 44 << 2.2; break; - } + QTest::newRow("20x20,dpr=2") << 20 << 2.0 << 40 << 2.0; + QTest::newRow("20x20,dpr=3") << 20 << 3.0 << 44 << 2.2; } void tst_QIconHighDpi::fromTheme() { QFETCH(int, requestedSize); + QFETCH(qreal, requestedDpr); QFETCH(int, expectedSize); QFETCH(qreal, expectedDpr); @@ -175,11 +145,52 @@ void tst_QIconHighDpi::fromTheme() QVERIFY(appointmentIcon.availableSizes().contains(QSize(16, 16))); QVERIFY(appointmentIcon.availableSizes().contains(QSize(22, 22))); - const QPixmap pixmap = appointmentIcon.pixmap(requestedSize); + const QPixmap pixmap = appointmentIcon.pixmap(QSize(requestedSize, requestedSize), requestedDpr); QCOMPARE(pixmap.size(), QSize(expectedSize, expectedSize)); // We should get the high DPI version of an image if it exists in the correct directory. - // Note that we didn't pass the QWindow to QIcon::pixmap(), - // because QGuiApplication::devicePixelRatio() will be used if no window was given. + QCOMPARE(pixmap.devicePixelRatio(), expectedDpr); +} + +void tst_QIconHighDpi::addPixmap_data() +{ + // fromTheme() and addPixmap() test the QIconLoaderEngine and QPixmapIconEngine + // QIcon backend, repsectively. We want these to have identical behavior and + // re-use the same test data here. + fromTheme_data(); +} + +void tst_QIconHighDpi::addPixmap() +{ + QFETCH(int, requestedSize); + QFETCH(qreal, requestedDpr); + QFETCH(int, expectedSize); + QFETCH(qreal, expectedDpr); + + QIcon icon; + + // manual pixmap adder for full control of devicePixelRatio + auto addPixmapWithDpr = [&icon](const QString &path, qreal dpr) { + QImage image(path); + image.setDevicePixelRatio(dpr); + icon.addPixmap(QPixmap::fromImage(image)); + }; + + addPixmapWithDpr(":icons/testtheme/16x16/actions/appointment-new.png", 1); + addPixmapWithDpr(":icons/testtheme/22x22/actions/appointment-new.png", 1); + addPixmapWithDpr(":icons/testtheme/22x22@2/actions/appointment-new.png", 2); + + QVERIFY(icon.availableSizes().contains(QSize(16, 16))); + QVERIFY(icon.availableSizes().contains(QSize(22, 22))); + + if (qstrcmp(QTest::currentDataTag(), "16x16,dpr=2") == 0) + QSKIP("broken corner case"); // expect 22x22, get 32x32 + if (qstrcmp(QTest::currentDataTag(), "44x44,dpr=1") == 0) + QSKIP("broken corner case"); // expect 44x44, get 22x22 + if (qstrcmp(QTest::currentDataTag(), "8x8,dpr=3") == 0) + QSKIP("broken corner case");// expect 22x22, get 24x24 + + const QPixmap pixmap = icon.pixmap(QSize(requestedSize, requestedSize), requestedDpr); + QCOMPARE(pixmap.size(), QSize(expectedSize, expectedSize)); QCOMPARE(pixmap.devicePixelRatio(), expectedDpr); } @@ -201,9 +212,25 @@ void tst_QIconHighDpi::ninePatch() } } +void tst_QIconHighDpi::preferUpscale() +{ + QIcon icon; + + // manual pixmap adder for full control of devicePixelRatio + auto addPixmapWithDpr = [&icon](const QString &path, qreal dpr) { + QImage image(path); + image.setDevicePixelRatio(dpr); + icon.addPixmap(QPixmap::fromImage(image)); + }; + + addPixmapWithDpr(":/icons/testtheme/22x22/actions/appointment-new.png", 1); + addPixmapWithDpr(":/icons/testtheme/22x22@2/actions/appointment-new.png", 2); + + QCOMPARE(icon.pixmap(QSize(22, 22), 1.25f).devicePixelRatio(), 1.25f); +} + int main(int argc, char *argv[]) { - QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); QGuiApplication app(argc, argv); Q_UNUSED(app); tst_QIconHighDpi test; |