summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/kernel/qscreen/tst_qscreen.cpp
blob: 9d85a31d5a81878a2654df2921f344186706249d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <qpainter.h>
#include <qrasterwindow.h>
#include <qscreen.h>
#include <qpa/qwindowsysteminterface.h>
#include <qpa/qplatformintegration.h>
#include <private/qguiapplication_p.h>
#include <private/qhighdpiscaling_p.h>

#include <QTest>
#include <QSignalSpy>

class tst_QScreen: public QObject
{
    Q_OBJECT

private slots:
    void angleBetween_data();
    void angleBetween();
    void transformBetween_data();
    void transformBetween();
    void orientationChange();

    void grabWindow_data();
    void grabWindow();
};

void tst_QScreen::angleBetween_data()
{
    QTest::addColumn<uint>("oa");
    QTest::addColumn<uint>("ob");
    QTest::addColumn<int>("expected");

    QTest::newRow("Portrait Portrait")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::PortraitOrientation)
        << 0;

    QTest::newRow("Portrait Landscape")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::LandscapeOrientation)
        << 270;

    QTest::newRow("Portrait InvertedPortrait")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::InvertedPortraitOrientation)
        << 180;

    QTest::newRow("Portrait InvertedLandscape")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::InvertedLandscapeOrientation)
        << 90;

    QTest::newRow("InvertedLandscape InvertedPortrait")
        << uint(Qt::InvertedLandscapeOrientation)
        << uint(Qt::InvertedPortraitOrientation)
        << 90;

    QTest::newRow("InvertedLandscape Landscape")
        << uint(Qt::InvertedLandscapeOrientation)
        << uint(Qt::LandscapeOrientation)
        << 180;

    QTest::newRow("Landscape Primary")
        << uint(Qt::LandscapeOrientation)
        << uint(Qt::PrimaryOrientation)
        << QGuiApplication::primaryScreen()->angleBetween(Qt::LandscapeOrientation, QGuiApplication::primaryScreen()->primaryOrientation());
}

void tst_QScreen::angleBetween()
{
    QFETCH( uint, oa );
    QFETCH( uint, ob );
    QFETCH( int, expected );

    Qt::ScreenOrientation a = Qt::ScreenOrientation(oa);
    Qt::ScreenOrientation b = Qt::ScreenOrientation(ob);

    QCOMPARE(QGuiApplication::primaryScreen()->angleBetween(a, b), expected);
    QCOMPARE(QGuiApplication::primaryScreen()->angleBetween(b, a), (360 - expected) % 360);
}

void tst_QScreen::transformBetween_data()
{
    QTest::addColumn<uint>("oa");
    QTest::addColumn<uint>("ob");
    QTest::addColumn<QRect>("rect");
    QTest::addColumn<QTransform>("expected");

    QRect rect(0, 0, 480, 640);

    QTest::newRow("Portrait Portrait")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::PortraitOrientation)
        << rect
        << QTransform();

    QTest::newRow("Portrait Landscape")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::LandscapeOrientation)
        << rect
        << QTransform(0, -1, 1, 0, 0, rect.height());

    QTest::newRow("Portrait InvertedPortrait")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::InvertedPortraitOrientation)
        << rect
        << QTransform(-1, 0, 0, -1, rect.width(), rect.height());

    QTest::newRow("Portrait InvertedLandscape")
        << uint(Qt::PortraitOrientation)
        << uint(Qt::InvertedLandscapeOrientation)
        << rect
        << QTransform(0, 1, -1, 0, rect.width(), 0);

    QTest::newRow("InvertedLandscape InvertedPortrait")
        << uint(Qt::InvertedLandscapeOrientation)
        << uint(Qt::InvertedPortraitOrientation)
        << rect
        << QTransform(0, 1, -1, 0, rect.width(), 0);

    QTest::newRow("InvertedLandscape Landscape")
        << uint(Qt::InvertedLandscapeOrientation)
        << uint(Qt::LandscapeOrientation)
        << rect
        << QTransform(-1, 0, 0, -1, rect.width(), rect.height());

    QTest::newRow("Landscape Primary")
        << uint(Qt::LandscapeOrientation)
        << uint(Qt::PrimaryOrientation)
        << rect
        << QGuiApplication::primaryScreen()->transformBetween(Qt::LandscapeOrientation, QGuiApplication::primaryScreen()->primaryOrientation(), rect);
}

void tst_QScreen::transformBetween()
{
    QFETCH( uint, oa );
    QFETCH( uint, ob );
    QFETCH( QRect, rect );
    QFETCH( QTransform, expected );

    Qt::ScreenOrientation a = Qt::ScreenOrientation(oa);
    Qt::ScreenOrientation b = Qt::ScreenOrientation(ob);

    QCOMPARE(QGuiApplication::primaryScreen()->transformBetween(a, b, rect), expected);
}

void tst_QScreen::orientationChange()
{
    qRegisterMetaType<Qt::ScreenOrientation>("Qt::ScreenOrientation");

    QScreen *screen = QGuiApplication::primaryScreen();
    QSignalSpy spy(screen, SIGNAL(orientationChanged(Qt::ScreenOrientation)));
    int expectedSignalCount = 0;

    QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::LandscapeOrientation);
    QWindowSystemInterface::flushWindowSystemEvents();
    QTRY_COMPARE(screen->orientation(), Qt::LandscapeOrientation);
    QCOMPARE(spy.count(), ++expectedSignalCount);

    QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::PortraitOrientation);
    QWindowSystemInterface::flushWindowSystemEvents();
    QTRY_COMPARE(screen->orientation(), Qt::PortraitOrientation);
    QCOMPARE(spy.count(), ++expectedSignalCount);

    QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::InvertedLandscapeOrientation);
    QWindowSystemInterface::flushWindowSystemEvents();
    QTRY_COMPARE(screen->orientation(), Qt::InvertedLandscapeOrientation);
    QCOMPARE(spy.count(), ++expectedSignalCount);

    QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::InvertedPortraitOrientation);
    QWindowSystemInterface::flushWindowSystemEvents();
    QTRY_COMPARE(screen->orientation(), Qt::InvertedPortraitOrientation);
    QCOMPARE(spy.count(), ++expectedSignalCount);

    QWindowSystemInterface::handleScreenOrientationChange(screen, Qt::LandscapeOrientation);
    QWindowSystemInterface::flushWindowSystemEvents();
    QTRY_COMPARE(screen->orientation(), Qt::LandscapeOrientation);
    QCOMPARE(spy.count(), ++expectedSignalCount);
}

void tst_QScreen::grabWindow_data()
{
    if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(
                QPlatformIntegration::ScreenWindowGrabbing)) {
        QSKIP("This platform does not support grabbing windows on screen.");
    }
    QTest::addColumn<int>("screenIndex");
    QTest::addColumn<QByteArray>("screenName");
    QTest::addColumn<bool>("grabWindow");
    QTest::addColumn<QRect>("windowRect");
    QTest::addColumn<QRect>("grabRect");

    int screenIndex = 0;
    for (const auto screen : QGuiApplication::screens()) {
        const QByteArray screenName = screen->name().toUtf8();
        const QRect availableGeometry = screen->availableGeometry();
        const QPoint topLeft = availableGeometry.topLeft() + QPoint(20, 20);
        QTest::addRow("%s - Window", screenName.data())
            << screenIndex << screenName << true << QRect(topLeft, QSize(200, 200)) << QRect(0, 0, -1, -1);
        QTest::addRow("%s - Window Section", screenName.data())
            << screenIndex << screenName << true << QRect(topLeft, QSize(200, 200)) << QRect(50, 50, 100, 100);
        QTest::addRow("%s - Screen", screenName.data())
            << screenIndex << screenName << false << QRect(topLeft, QSize(200, 200)) << QRect(0, 0, -1, -1);
        QTest::addRow("%s - Screen Section", screenName.data())
            << screenIndex << screenName << false << QRect(topLeft, QSize(200, 200)) << QRect(topLeft, QSize(200, 200));

        ++screenIndex;
    }
}

void tst_QScreen::grabWindow()
{
    QFETCH(int, screenIndex);
    QFETCH(QByteArray, screenName);
    QFETCH(bool, grabWindow);
    QFETCH(QRect, windowRect);
    QFETCH(QRect, grabRect);

    class Window : public QRasterWindow
    {
    public:
        Window(QScreen *scr)
        : image(scr->size(), QImage::Format_ARGB32_Premultiplied)
        {
            setFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
            setScreen(scr);
            image.setDevicePixelRatio(scr->devicePixelRatio());
        }
        QImage image;

    protected:
        void resizeEvent(QResizeEvent *e) override
        {
            const QSize sz = e->size();
            image = image.scaled(sz * image.devicePixelRatio());
            QPainter painter(&image);
            painter.fillRect(0, 0, sz.width(), sz.height(), Qt::black);
            painter.setPen(QPen(Qt::red, 2));
            painter.drawLine(0, 0, sz.width(), sz.height());
            painter.drawLine(0, sz.height(), sz.width(), 0);
            painter.drawRect(0, 0, sz.width(), sz.height());
        }
        void paintEvent(QPaintEvent *) override
        {
            QPainter painter(this);
            painter.drawImage(0, 0, image);
        }
    };
    const auto screens = QGuiApplication::screens();
    double highestDpr = 0;
    for (auto screen : screens)
        highestDpr = qMax(highestDpr, screen->devicePixelRatio());

    QScreen *screen = screens.at(screenIndex);
    QCOMPARE(screen->name().toUtf8(), screenName);
    const double screenDpr = screen->devicePixelRatio();

    if (QHighDpiScaling::isActive()) {
        const float rawFactor = QHighDpiScaling::factor(screen);
        const float roundedFactor = qRound(rawFactor);
        if (!qFuzzyCompare(roundedFactor, rawFactor))
            QSKIP("HighDPI enabled with non-integer factor. Skip due to possible rounding errors.");
    }

    Window window(screen);
    window.setGeometry(windowRect);
#ifndef Q_OS_ANDROID
    window.show();
#else
    window.showNormal();
#endif

    if (!QTest::qWaitForWindowExposed(&window))
        QSKIP("Failed to expose window - aborting");

    // this is necessary because of scrolling effects combined with potential slowness of VMs
    QTest::qWait(1500);

    QSize expectedGrabSize = grabRect.isValid()
                           ? grabRect.size()
                           : (grabWindow ?  windowRect.size() : screen->size());
    // we ask for pixel coordinates, but will get a pixmap with device-specific DPR
    expectedGrabSize *= screen->devicePixelRatio();

    // the painted image will always be in the screen's DPR
    QImage paintedImage = window.image;
    QCOMPARE(paintedImage.devicePixelRatio(), screenDpr);

    const QPixmap pixmap = screen->grabWindow(grabWindow
                         ? window.winId()
                         : 0, grabRect.x(), grabRect.y(), grabRect.width(), grabRect.height());

    QImage grabbedImage = pixmap.toImage();
    const QSize grabbedSize = grabbedImage.size();
    QCOMPARE(grabbedSize, expectedGrabSize);

    QPoint pixelOffset = QPoint(0, 0);
    if (!grabRect.isValid()) {
        if (grabWindow) {
            // if we grab the entire window, then the grabbed image should be as large as the window
            QCOMPARE(grabbedImage.size(), paintedImage.size());
        } else {
            // if we grab the entire screen, then the grabbed image should be as large as the screen
            QCOMPARE(grabbedImage.size(), screen->size() * screenDpr);
            pixelOffset = window.geometry().topLeft() - screen->geometry().topLeft();
            grabbedImage = grabbedImage.copy(QRect(pixelOffset * screenDpr, window.geometry().size() * screenDpr));
        }
    } else if (grabWindow) {
        // if we grab the section, compare with the corresponding section from the painted image
        const QRect sectionRect = QRect(grabRect.topLeft() * screenDpr,
                                        grabRect.size() * screenDpr);
        paintedImage = paintedImage.copy(sectionRect);
    }
    QCOMPARE(grabbedImage.size(), paintedImage.size());

    // the two images might differ in format, or DPR, so instead of comparing them, sample a few pixels
    for (auto point : {
                       QPoint(0, 0),
                       QPoint(5, 15),
                       QPoint(paintedImage.width() - 1, paintedImage.height() - 1),
                       QPoint(paintedImage.width() - 5, paintedImage.height() - 10)
                      }) {
        QCOMPARE(grabbedImage.pixelColor(point), paintedImage.pixelColor(point));
    }
}

#include <tst_qscreen.moc>
QTEST_MAIN(tst_QScreen);