diff options
author | Morten Johan Sørvig <morten.sorvig@qt.io> | 2020-08-12 15:19:54 +0200 |
---|---|---|
committer | Morten Johan Sørvig <morten.sorvig@qt.io> | 2020-11-08 19:49:53 +0100 |
commit | 46a552583f99ed04945ccf949afbdff58dcdfa1f (patch) | |
tree | f194720cc63b5b282edd8db85523e884dbbc5e28 /tests/auto/gui/image | |
parent | e6cba05b6623d96278ac042b50eaba1c0cd77ddb (diff) |
Teach QPixmapIconEngine how to handle @Nx pixmaps
Bring QPixmapIconEngine on par with QIconLoaderEngine
when it comes to @Nx pixmap handling: Make the scale
factor a test parameter during icon lookup.
This allows storing e.g 16x16@1, 16x16@2, 16x16@3 versions
of a pixmap in the icon, and then having QIcon select
the correct one based on the target devicePixelRatio.
Extend the qiconhighdpi test to also cover QPixmapIconEngine,
via the addPixmap() API.
The corner cases of pixmap lookup can be much complicated.
QIconLoaderEngine and QPixmapIconEngine should ideally
have identical behavior in order to avoid surprises.
Change-Id: I17552cc61755bff9553c4a462e3983ac6759c13b
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'tests/auto/gui/image')
-rw-r--r-- | tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp index 52f38419c7..62878926fa 100644 --- a/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp +++ b/tests/auto/gui/image/qiconhighdpi/tst_qiconhighdpi.cpp @@ -39,6 +39,8 @@ private slots: void initTestCase(); void fromTheme_data(); void fromTheme(); + void addPixmap_data(); + void addPixmap(); void ninePatch(); }; @@ -173,6 +175,49 @@ void tst_QIconHighDpi::fromTheme() 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); +} + void tst_QIconHighDpi::ninePatch() { const QIcon icon(":/icons/misc/button.9.png"); |