diff options
Diffstat (limited to 'tests/auto/gui/kernel/qhighdpi')
-rw-r--r-- | tests/auto/gui/kernel/qhighdpi/CMakeLists.txt | 11 | ||||
-rw-r--r-- | tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp | 268 |
2 files changed, 182 insertions, 97 deletions
diff --git a/tests/auto/gui/kernel/qhighdpi/CMakeLists.txt b/tests/auto/gui/kernel/qhighdpi/CMakeLists.txt index bdd9a5e17f..aa61bfbb0b 100644 --- a/tests/auto/gui/kernel/qhighdpi/CMakeLists.txt +++ b/tests/auto/gui/kernel/qhighdpi/CMakeLists.txt @@ -1,13 +1,20 @@ -# Generated from qhighdpi.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qhighdpi Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qhighdpi LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qhighdpi SOURCES tst_qhighdpi.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Gui Qt::GuiPrivate diff --git a/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp b/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp index b732954230..6fe4faec03 100644 --- a/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp +++ b/tests/auto/gui/kernel/qhighdpi/tst_qhighdpi.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 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$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <private/qhighdpiscaling_p.h> #include <qpa/qplatformscreen.h> @@ -35,6 +10,7 @@ #include <QJsonObject> #include <QJsonDocument> #include <QStringView> +#include <QSignalSpy> Q_LOGGING_CATEGORY(lcTests, "qt.gui.tests") @@ -60,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(); @@ -76,6 +55,10 @@ private slots: void mouseEvents(); void mouseVelocity(); void mouseVelocity_data(); + void setCursor(); + void setCursor_data(); + void setGlobalFactorEmits(); + void setScreenFactorEmits(); }; /// Offscreen platform plugin test setup @@ -86,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 { @@ -203,65 +186,6 @@ QJsonObject tst_QHighDpi::offscreenConfiguration() return getConfiguration(platformNativeInterface); } -// JsonValueRef implements support for mutating nested JSON structures, e.g. -// -// JsonValueRef::get(&config)["screens"][0]["logicalDpi"] = 192 -// -class JsonValueRef { -public: - static JsonValueRef get(QJsonValue *value) { - return JsonValueRef(value); - } - - JsonValueRef(QJsonValue *value) - : m_value(value) { } - - JsonValueRef(QJsonValue *value, JsonValueRef *parent, QString key) - : m_value(value), m_parent(parent), m_key(key) { } - - JsonValueRef(QJsonValue *value, JsonValueRef *parent, int index) - : m_value(value), m_parent(parent), m_index(index) { } - - ~JsonValueRef() { - if (m_parent) { - if (!m_key.isNull()) { - QJsonObject parentObject = m_parent->m_value->toObject(); - parentObject[m_key] = *m_value; - *m_parent->m_value = parentObject; - } else if (m_index > -1) { - QJsonArray parentArray = m_parent->m_value->toArray(); - parentArray[m_index] = *m_value; - *m_parent->m_value = parentArray; - } - delete m_value; // owned if we have a parent, see operator[] - } - } - - JsonValueRef operator[](const char *str) { - QString key = QString::fromUtf8(str); - return JsonValueRef(new QJsonValue((*m_value)[key]), this, key); - } - - JsonValueRef operator[](int index) { - return JsonValueRef(new QJsonValue((*m_value)[index]), this, index); - } - - void operator=(int value) { - *m_value = QJsonValue(value); - } - - void operator=(const char *str) { - *m_value = QJsonValue(QString(str)); - } - -private: - Q_DISABLE_COPY(JsonValueRef); - QJsonValue *m_value = nullptr; - JsonValueRef *m_parent = nullptr; - QString m_key; - int m_index = -1; -}; - void tst_QHighDpi::cleanup() { // Some test functions set environment variables. Unset them here, @@ -269,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() @@ -314,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()); } } @@ -327,10 +255,12 @@ void tst_QHighDpi::screenDpiChange() // Set new DPI int newDpi = 192; QJsonValue config = offscreenConfiguration(); - JsonValueRef::get(&config)["screens"][0]["logicalDpi"] = newDpi; - JsonValueRef::get(&config)["screens"][1]["logicalDpi"] = newDpi; - JsonValueRef::get(&config)["screens"][2]["logicalDpi"] = newDpi; - setOffscreenConfiguration(config.toObject()); + // API defect until Qt 7, so go indirectly via CBOR + QCborMap map = QCborMap::fromJsonObject(config.toObject()); + map[QLatin1String("screens")][0][QLatin1String("logicalDpi")] = newDpi; + map[QLatin1String("screens")][1][QLatin1String("logicalDpi")] = newDpi; + map[QLatin1String("screens")][2][QLatin1String("logicalDpi")] = newDpi; + setOffscreenConfiguration(map.toJsonObject()); // TODO check events @@ -338,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)); @@ -390,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()) { @@ -404,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() @@ -431,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 }; @@ -474,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. @@ -483,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 @@ -805,5 +837,51 @@ void tst_QHighDpi::mouseVelocity() } } +void tst_QHighDpi::setCursor_data() +{ + standardScreenDpiTestData(); +} + +void tst_QHighDpi::setCursor() +{ + QFETCH(QList<qreal>, dpiValues); + std::unique_ptr<QGuiApplication> app(createStandardOffscreenApp(dpiValues)); + + for (QScreen *screen : app->screens()) { + QPoint center = screen->geometry().center(); + QCursor::setPos(center.x(), center.y()); + QCOMPARE(QCursor::pos(), center); + } +} + +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); |