summaryrefslogtreecommitdiffstats
path: root/tests/manual/qscreen_xrandr/tst_qscreen_xrandr.cpp
blob: 166b11e2f6bd1285454d45d96662def66dfcf5a7 (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
/****************************************************************************
**
** 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$
**
****************************************************************************/

#include <qpainter.h>
#include <qrasterwindow.h>
#include <qscreen.h>
#include <qpa/qwindowsysteminterface.h>
#include <QProcess>

#include <QtTest/QtTest>

class tst_QScreen_Xrandr: public QObject
{
    Q_OBJECT

private slots:
    void xrandr_15_merge_and_unmerge();
    void xrandr_15_scale();
    void xrandr_15_off_and_on();

private:
    void xrandr_process(const QStringList &arguments = {});
};

// this test requires an X11 desktop with at least two screens
void tst_QScreen_Xrandr::xrandr_15_merge_and_unmerge()
{
    QStringList originalScreenNames;
    QStringList mergedScreenNames;
    {
        QList<QScreen *> screens = QGuiApplication::screens();
        if (screens.size() < 2)
            QSKIP("This test requires two or more screens.");
        qDebug() << "initial set of screens:" << screens.size();
        for (QScreen *s : screens) {
            qDebug() << "screen: " << s->name();
            originalScreenNames << s->name();
            if (mergedScreenNames.size() < 2)
                mergedScreenNames << s->name();
        }
    }

    QSignalSpy addedSpy(qApp, &QGuiApplication::screenAdded);
    QSignalSpy removedSpy(qApp, &QGuiApplication::screenRemoved);

    // combine the first two screens into one monitor
    // e.g. "xrandr --setmonitor Qt-merged auto eDP,HDMI-0"
    QStringList args;
    args << "--setmonitor" << "Qt-merged" << "auto" << mergedScreenNames.join(',');
    xrandr_process(args);

    QTRY_COMPARE(removedSpy.count(), 2);
    QVERIFY(QGuiApplication::screens().size() != originalScreenNames.size());
    auto combinedScreens = QGuiApplication::screens();
    qDebug() << "added" << addedSpy.count() << "removed" << removedSpy.count() << "combined screen(s):" << combinedScreens.size();
    QScreen *merged = nullptr;
    for (QScreen *s : combinedScreens) {
        qDebug() << "screen: " << s->name();
        if (s->name() == QLatin1String("Qt-merged"))
            merged = s;
    }
    // the screen that we created must be in the list now
    QVERIFY(merged);
    QCOMPARE(QGuiApplication::screens().size(), originalScreenNames.size() - 1);
    addedSpy.clear();
    removedSpy.clear();

    // "xrandr --delmonitor Qt-merged"
    args.clear();
    args << "--delmonitor" << "Qt-merged";
    xrandr_process(args);

    QTRY_COMPARE(removedSpy.count(), 1);
    QVERIFY(QGuiApplication::screens().size() != combinedScreens.size());
    auto separatedScreens = QGuiApplication::screens();
    qDebug() << "added" << addedSpy.count() << "removed" << removedSpy.count() << "separated screen(s):" << separatedScreens.size();
    for (QScreen *s : separatedScreens)
        qDebug() << "screen: " << s->name();

    QCOMPARE(QGuiApplication::screens().size(), originalScreenNames.size());
}

// try to scale the first screen to 1.5x1.5 and scale back to 1x1
void tst_QScreen_Xrandr::xrandr_15_scale()
{
    QList<QScreen *> screens = QGuiApplication::screens();
    if (screens.size() < 1)
        QSKIP("This test requires at least one screen.");

    QScreen *screen1 = screens.at(0);
    QString name1 = screen1->name();
    int height1 = screen1->size().height();
    int width1 = screen1->size().width();
    qDebug() << "screen " << name1 << ": height=" << height1 << ", width=" << width1;

    int expectedHeight = height1 * 1.5;
    int expectedWidth = width1 * 1.5;

    QSignalSpy geometryChangedSpy1(screen1, &QScreen::geometryChanged);

    // "xrandr --output name1 --scale 1.5x1.5"
    QStringList args;
    args << "--output" << name1 << "--scale" << "1.5x1.5";
    xrandr_process(args);
    QTRY_COMPARE(geometryChangedSpy1.count(), 1);

    QList<QScreen *> screens2 = QGuiApplication::screens();
    QVERIFY(screens2.size() >= 1);
    QScreen *screen2 = nullptr;
    for (QScreen *s : screens2) {
        if (s->name() == name1)
            screen2 = s;
    }
    int height2 = screen2->size().height();
    int width2 = screen2->size().width();
    qDebug() << "screen " << name1 << ": height=" << height2 << ", width=" << width2;
    QVERIFY(height2 == expectedHeight);
    QVERIFY(width2 == expectedWidth);

    QSignalSpy geometryChangedSpy2(screen1, &QScreen::geometryChanged);

    // "xrandr --output name1 --scale 1x1"
    args.clear();
    args << "--output" << name1 << "--scale" << "1x1";
    xrandr_process(args);
    QTRY_COMPARE(geometryChangedSpy2.count(), 1);

    QList<QScreen *> screens3 = QGuiApplication::screens();
    QVERIFY(screens3.size() >= 1);
    QScreen *screen3 = nullptr;
    for (QScreen *s : screens3) {
        if (s->name() == name1)
            screen3 = s;
    }
    int height3 = screen3->size().height();
    int width3 = screen3->size().width();
    qDebug() << "screen " << name1 << ": height=" << height3 << ", width=" << width3;
    QVERIFY(height3 == height1);
    QVERIFY(width3 == width1);
}

void tst_QScreen_Xrandr::xrandr_15_off_and_on()
{
    QList<QScreen *> screens = QGuiApplication::screens();
    int ss = screens.size();
    if (ss < 1)
        QSKIP("This test requires at least one screen.");

    QStringList names;
    for (QScreen *s : screens)
        names << s->name();

    QStringList args;
    for (int i = ss; i > 0; i--) {
        QString name = names.at(i-1);
        qDebug() << "i=" << i << ", name:" << name;
        args.clear();
        args << "--output" << name << "--off";
        xrandr_process(args);
        QTest::qWait(500);

        QVERIFY(QGuiApplication::screens().size() == (i != 1 ? i - 1 : i));
    }

    for (int i = ss; i > 0; i--) {
        QString name = names.at(i-1);
        qDebug() << "i=" << i << ", name:" << name;
        args.clear();
        args << "--output" << name << "--auto";
        xrandr_process(args);
        QTest::qWait(500);

        QVERIFY(QGuiApplication::screens().size() == (i == ss ? 1 : ss - i + 1));
    }
}

void tst_QScreen_Xrandr::xrandr_process(const QStringList &args)
{
    QString prog = "xrandr";
    QProcess *process = new QProcess(this);
    qDebug() << Q_FUNC_INFO << prog << args;
    process->start(prog, args);
    QVERIFY(process->waitForFinished());
}

#include <tst_qscreen_xrandr.moc>
QTEST_MAIN(tst_QScreen_Xrandr);