1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <private/qhighdpiscaling_p.h>
30#include <qpa/qplatformscreen.h>
31
32#include <QtTest/QtTest>
33
34class tst_QHighDpiScaling: public QObject
35{
36 Q_OBJECT
37
38private slots:
39 void factor();
40 void scale();
41};
42
43// Emulate the case of a High DPI secondary screen
44class MyPlatformScreen : public QPlatformScreen
45{
46public:
47 QRect geometry() const override { return QRect(3840, 0, 3840, 1920); }
48 QRect availableGeometry() const override { return geometry(); }
49
50 int depth() const override { return 32; }
51 QImage::Format format() const override { return QImage::Format_ARGB32_Premultiplied; }
52};
53
54void tst_QHighDpiScaling::factor()
55{
56 QHighDpiScaling::setGlobalFactor(2);
57
58 // Verfy that QHighDpiScaling::factor() does not crash on nullptr contexts.
59 QPoint fakeNativePosition = QPoint(5, 5);
60 QPlatformScreen *screenContext = nullptr;
61 QVERIFY(QHighDpiScaling::factor(screenContext) >= 0);
62 QVERIFY(QHighDpiScaling::factor(screenContext, &fakeNativePosition) >= 0);
63 QPlatformScreen *platformScreenContext = nullptr;
64 QVERIFY(QHighDpiScaling::factor(platformScreenContext) >= 0);
65 QVERIFY(QHighDpiScaling::factor(platformScreenContext, &fakeNativePosition) >= 0);
66 QWindow *windowContext = nullptr;
67 QVERIFY(QHighDpiScaling::factor(windowContext) >= 0);
68 QVERIFY(QHighDpiScaling::factor(windowContext, &fakeNativePosition) >= 0);
69}
70
71// QTBUG-77255: Test some scaling overloads
72void tst_QHighDpiScaling::scale()
73{
74 QHighDpiScaling::setGlobalFactor(2);
75 QScopedPointer<QPlatformScreen> screen(new MyPlatformScreen);
76
77 qreal nativeValue = 10;
78 const qreal value = QHighDpi::fromNativePixels(value: nativeValue, context: screen.data());
79 QCOMPARE(value, qreal(5));
80 QCOMPARE(QHighDpi::toNativePixels(value, screen.data()), nativeValue);
81
82 // 10, 10 within screen should translate to 5,5 with origin preserved
83 const QPoint nativePoint = screen->geometry().topLeft() + QPoint(10, 10);
84 const QPoint point = QHighDpi::fromNativePixels(value: nativePoint, context: screen.data());
85 QCOMPARE(point, QPoint(3845, 5));
86 QCOMPARE(QHighDpi::toNativePixels(point, screen.data()), nativePoint);
87
88 const QPointF nativePointF(nativePoint);
89 const QPointF pointF = QHighDpi::fromNativePixels(value: nativePointF, context: screen.data());
90 QCOMPARE(pointF, QPointF(3845, 5));
91 QCOMPARE(QHighDpi::toNativePixels(pointF, screen.data()), nativePointF);
92}
93
94#include "tst_qhighdpiscaling.moc"
95QTEST_MAIN(tst_QHighDpiScaling);
96

source code of qtbase/tests/auto/gui/kernel/qhighdpiscaling/tst_qhighdpiscaling.cpp