diff options
Diffstat (limited to 'tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp')
-rw-r--r-- | tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp | 153 |
1 files changed, 147 insertions, 6 deletions
diff --git a/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp b/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp index 5f0d226124..6fe4faec03 100644 --- a/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp +++ b/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2020 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <private/qhighdpiscaling_p.h> #include <qpa/qplatformscreen.h> @@ -10,6 +10,7 @@ #include <QJsonObject> #include <QJsonDocument> #include <QStringView> +#include <QSignalSpy> Q_LOGGING_CATEGORY(lcTests, "qt.gui.tests") @@ -35,10 +36,13 @@ private slots: void screenDpiAndDpr_data(); void screenDpiAndDpr(); void screenDpiChange(); + void screenDpiChangeWithWindow(); void environment_QT_SCALE_FACTOR(); void environment_QT_SCREEN_SCALE_FACTORS_data(); void environment_QT_SCREEN_SCALE_FACTORS(); void environment_QT_USE_PHYSICAL_DPI(); + void environment_QT_SCALE_FACTOR_ROUNDING_POLICY(); + void application_setScaleFactorRoundingPolicy(); void screenAt_data(); void screenAt(); void screenGeometry_data(); @@ -53,6 +57,8 @@ private slots: void mouseVelocity_data(); void setCursor(); void setCursor_data(); + void setGlobalFactorEmits(); + void setScreenFactorEmits(); }; /// Offscreen platform plugin test setup @@ -63,7 +69,7 @@ const int standardScreenCount = 3; QJsonArray tst_QHighDpi::createStandardScreens(const QList<qreal> &dpiValues) { - Q_ASSERT(dpiValues.count() == standardScreenCount); + Q_ASSERT(dpiValues.size() == standardScreenCount); // Create row of three screens: screen#0 screen#1 screen#2 return QJsonArray { @@ -187,6 +193,7 @@ void tst_QHighDpi::cleanup() qunsetenv("QT_SCALE_FACTOR"); qunsetenv("QT_SCREEN_SCALE_FACTORS"); qunsetenv("QT_USE_PHYSICAL_DPI"); + qunsetenv("QT_SCALE_FACTOR_ROUNDING_POLICY"); } void tst_QHighDpi::qhighdpiscaling_data() @@ -232,6 +239,9 @@ void tst_QHighDpi::screenDpiAndDpr() QWindow window(screen); QCOMPARE(window.devicePixelRatio(), screen->devicePixelRatio()); + window.setGeometry(QRect(screen->geometry().center(), QSize(10, 10))); + window.create(); + QCOMPARE(window.devicePixelRatio(), screen->devicePixelRatio()); } } @@ -258,16 +268,52 @@ void tst_QHighDpi::screenDpiChange() for (QScreen *screen : app->screens()) { QCOMPARE(screen->devicePixelRatio(), newDpi / standardBaseDpi); QCOMPARE(screen->logicalDotsPerInch(), newDpi / screen->devicePixelRatio()); + QWindow window(screen); QCOMPARE(window.devicePixelRatio(), screen->devicePixelRatio()); + window.create(); + QCOMPARE(window.devicePixelRatio(), screen->devicePixelRatio()); } QCOMPARE(app->devicePixelRatio(), newDpi / standardBaseDpi); } +void tst_QHighDpi::screenDpiChangeWithWindow() +{ + QList<qreal> dpiValues = { 96, 192, 288 }; + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + + // Create windows for screens + QList<QScreen *> screens = app->screens(); + QList<QWindow *> windows; + for (int i = 0; i < screens.count(); ++i) { + QScreen *screen = screens[i]; + QWindow *window = new QWindow(); + windows.append(window); + window->setGeometry(QRect(screen->geometry().center(), QSize(10, 10))); + window->create(); + QCOMPARE(window->devicePixelRatio(), dpiValues[i] / standardBaseDpi); + } + + // Change screen DPI + QList<qreal> newDpiValues = { 288, 192, 96 }; + QJsonValue config = offscreenConfiguration(); + QCborMap map = QCborMap::fromJsonObject(config.toObject()); + for (int i = 0; i < screens.count(); ++i) { + map[QLatin1String("screens")][i][QLatin1String("logicalDpi")] = newDpiValues[i]; + } + setOffscreenConfiguration(map.toJsonObject()); + + // Verify that window DPR changes on Screen DPI change. + for (int i = 0; i < screens.count(); ++i) { + QWindow *window = windows[i]; + QCOMPARE(window->devicePixelRatio(), newDpiValues[i] / standardBaseDpi); + } +} + void tst_QHighDpi::environment_QT_SCALE_FACTOR() { qreal factor = 3.1415; - qputenv("QT_SCALE_FACTOR", QByteArray::number(factor)); + qputenv("QT_SCALE_FACTOR", std::to_string(factor)); QList<qreal> dpiValues { 96, 144, 192 }; std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); @@ -310,9 +356,10 @@ void tst_QHighDpi::environment_QT_SCREEN_SCALE_FACTORS() QFETCH(QByteArray, environment); QFETCH(QList<qreal>, expectedDprValues); + qputenv("QT_SCREEN_SCALE_FACTORS", environment); + // Verify that setting QT_SCREEN_SCALE_FACTORS overrides the from-platform-screen-DPI DPR. { - qputenv("QT_SCREEN_SCALE_FACTORS", environment); std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(platformScreenDpi)); int i = 0; for (QScreen *screen : app->screens()) { @@ -324,6 +371,18 @@ void tst_QHighDpi::environment_QT_SCREEN_SCALE_FACTORS() QCOMPARE(window.devicePixelRatio(), expextedDpr); } } + + // Verify that setHighDpiScaleFactorRoundingPolicy applies to QT_SCREEN_SCALE_FACTORS as well + QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(platformScreenDpi)); + int i = 0; + for (QScreen *screen : app->screens()) { + qreal expectedRounderDpr = qRound(expectedDprValues[i++]); + qreal windowDpr = QWindow(screen).devicePixelRatio(); + QCOMPARE(windowDpr, expectedRounderDpr); + } + } } void tst_QHighDpi::environment_QT_USE_PHYSICAL_DPI() @@ -351,6 +410,59 @@ void tst_QHighDpi::environment_QT_USE_PHYSICAL_DPI() } } +void tst_QHighDpi::environment_QT_SCALE_FACTOR_ROUNDING_POLICY() +{ + QList<qreal> dpiValues { 96, 144, 192 }; + + qputenv("QT_SCALE_FACTOR_ROUNDING_POLICY", "PassThrough"); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + for (int i = 0; i < dpiValues.size(); ++i) + QCOMPARE(app->screens()[i]->devicePixelRatio(), dpiValues[i] / qreal(96)); + } + + qputenv("QT_SCALE_FACTOR_ROUNDING_POLICY", "Round"); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + for (int i = 0; i < dpiValues.size(); ++i) + QCOMPARE(app->screens()[i]->devicePixelRatio(), qRound(dpiValues[i] / qreal(96))); + } + + qunsetenv("QT_SCALE_FACTOR_ROUNDING_POLICY"); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + for (int i = 0; i < dpiValues.size(); ++i) + QCOMPARE(app->screens()[i]->devicePixelRatio(), dpiValues[i] / qreal(96)); + } +} + +void tst_QHighDpi::application_setScaleFactorRoundingPolicy() +{ + QList<qreal> dpiValues { 96, 144, 192 }; + QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + for (int i = 0; i < dpiValues.size(); ++i) + QCOMPARE(app->screens()[i]->devicePixelRatio(), qRound(dpiValues[i] / qreal(96))); + } + + QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + for (int i = 0; i < dpiValues.size(); ++i) + QCOMPARE(app->screens()[i]->devicePixelRatio(), dpiValues[i] / qreal(96)); + } + + // Verify that environment overrides app setting + QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round); + qputenv("QT_SCALE_FACTOR_ROUNDING_POLICY", "PassThrough"); + { + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + for (int i = 0; i < dpiValues.size(); ++i) + QCOMPARE(app->screens()[i]->devicePixelRatio(), dpiValues[i] / qreal(96)); + } +} + void tst_QHighDpi::minimumDpr() { QList<qreal> dpiValues { 40, 60, 95 }; @@ -394,7 +506,7 @@ void tst_QHighDpi::screenAt() QFETCH(QList<qreal>, dpiValues); std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); - QCOMPARE(app->screens().count(), standardScreenCount); // standard setup + QCOMPARE(app->screens().size(), standardScreenCount); // standard setup // Verify that screenAt() returns the correct or no screen for various points, // for all screens. @@ -403,7 +515,7 @@ void tst_QHighDpi::screenAt() qreal dpi = dpiValues[i++]; // veryfy virtualSiblings and that AA_EnableHighDpiScaling is active - QCOMPARE(screen->virtualSiblings().count(), standardScreenCount); + QCOMPARE(screen->virtualSiblings().size(), standardScreenCount); QCOMPARE(screen->geometry().size(), QSize(standardScreenWidth, standardScreenHeight) * (96.0 / dpi)); // test points on screen @@ -742,5 +854,34 @@ void tst_QHighDpi::setCursor() } } +void tst_QHighDpi::setGlobalFactorEmits() +{ + QList<qreal> dpiValues { 96, 96, 96 }; + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + + std::vector<std::unique_ptr<QSignalSpy>> spies; + for (QScreen *screen : app->screens()) + spies.push_back(std::make_unique<QSignalSpy>(screen, &QScreen::geometryChanged)); + + QHighDpiScaling::setGlobalFactor(2); + + for (const auto &spy : spies) + QCOMPARE(spy->count(), 1); + + QHighDpiScaling::setGlobalFactor(1); +} + +void tst_QHighDpi::setScreenFactorEmits() +{ + QList<qreal> dpiValues { 96, 96, 96 }; + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + + for (QScreen *screen : app->screens()) { + QSignalSpy spy(screen, &QScreen::geometryChanged); + QHighDpiScaling::setScreenFactor(screen, 2); + QCOMPARE(spy.count(), 1); + } +} + #include "tst_qhighdpi.moc" QTEST_APPLESS_MAIN(tst_QHighDpi); |