File: x11lockertest.cpp

package info (click to toggle)
kscreenlocker 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,172 kB
  • sloc: cpp: 5,698; xml: 88; sh: 32; makefile: 5
file content (261 lines) | stat: -rw-r--r-- 8,035 bytes parent folder | download | duplicates (2)
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
/*
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
SPDX-FileCopyrightText: 2015 Bhushan Shah <bhush94@gmail.com>

SPDX-License-Identifier: GPL-2.0-or-later
*/
// own
#include "../x11locker.h"
// KDE Frameworks
#include <KWindowSystem>
// Qt
#include <QTest>
#include <QWindow>
#include <private/qtx11extras_p.h>
// xcb
#include <xcb/xcb.h>

template<typename T>
using ScopedCPointer = QScopedPointer<T, QScopedPointerPodDeleter>;

class LockWindowTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void testBlankScreen();
    void testEmergencyShow();
};

xcb_screen_t *defaultScreen()
{
    int screen = QX11Info::appScreen();
    for (xcb_screen_iterator_t it = xcb_setup_roots_iterator(xcb_get_setup(QX11Info::connection())); it.rem; --screen, xcb_screen_next(&it)) {
        if (screen == 0) {
            return it.data;
        }
    }
    return nullptr;
}

bool isColored(const QColor color, const int x, const int y, const int width, const int height)
{
    xcb_connection_t *c = QX11Info::connection();
    const auto cookie = xcb_get_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, QX11Info::appRootWindow(), x, y, width, height, ~0);
    ScopedCPointer<xcb_get_image_reply_t> xImage(xcb_get_image_reply(c, cookie, nullptr));
    if (xImage.isNull()) {
        return false;
    }

    // this operates on the assumption that X server default depth matches Qt's image format
    QImage image(xcb_get_image_data(xImage.data()), width, height, xcb_get_image_data_length(xImage.data()) / height, QImage::Format_ARGB32_Premultiplied);

    for (int i = 0; i < image.width(); i++) {
        for (int j = 0; j < image.height(); j++) {
            if (QColor(image.pixel(i, j)) != color) {
                return false;
            }
        }
    }
    return true;
}

bool isBlack()
{
    xcb_screen_t *screen = defaultScreen();
    const int width = screen->width_in_pixels;
    const int height = screen->height_in_pixels;

    return isColored(Qt::black, 0, 0, width, height);
}

xcb_atom_t screenLockerAtom()
{
    const QByteArray atomName = QByteArrayLiteral("_KDE_SCREEN_LOCKER");
    xcb_connection_t *c = QX11Info::connection();
    const auto cookie = xcb_intern_atom(c, false, atomName.length(), atomName.constData());
    ScopedCPointer<xcb_intern_atom_reply_t> atom(xcb_intern_atom_reply(c, cookie, nullptr));
    if (atom.isNull()) {
        return XCB_ATOM_NONE;
    }
    return atom->atom;
}

void LockWindowTest::initTestCase()
{
    QCoreApplication::setAttribute(Qt::AA_ForceRasterWidgets);
}

void LockWindowTest::testBlankScreen()
{
    if (!KWindowSystem::isPlatformX11()) {
        QSKIP("test requires X11");
    }

    // create and show a dummy window to ensure the background doesn't start as black
    QWidget dummy;
    dummy.setWindowFlags(Qt::X11BypassWindowManagerHint);
    QPalette p;
    p.setColor(QPalette::Window, Qt::red);
    dummy.setAutoFillBackground(true);
    dummy.setPalette(p);
    dummy.setGeometry(0, 0, 100, 100);
    dummy.show();
    xcb_flush(QX11Info::connection());

    // Lets wait till it gets shown
    QTest::qWait(1000);

    // Verify that red window is shown
    QVERIFY(isColored(Qt::red, 0, 0, 100, 100));

    ScreenLocker::X11Locker lockWindow;
    lockWindow.showLockWindow();

    // the screen used to be blanked once the first lock window gets mapped, so let's create one
    QWindow fakeWindow;
    fakeWindow.setFlags(Qt::X11BypassWindowManagerHint);
    // it's on purpose outside the visual area
    fakeWindow.setGeometry(-1, -1, 1, 1);
    fakeWindow.create();
    xcb_atom_t atom = screenLockerAtom();
    QVERIFY(atom != XCB_ATOM_NONE);
    xcb_connection_t *c = QX11Info::connection();
    xcb_change_property(c, XCB_PROP_MODE_REPLACE, fakeWindow.winId(), atom, atom, 32, 0, nullptr);
    xcb_flush(c);
    fakeWindow.show();

    // give it time to be shown
    QTest::qWait(1000);

    // now lets try to get a screen grab and verify it's not yet black
    QVERIFY(!isBlack());

    // nowadays we need to pass the window id to the lock window
    lockWindow.addAllowedWindow(fakeWindow.winId());

    // give it time to be shown
    QTest::qWait(1000);

    // now lets try to get a screen grab and verify it's black
    QVERIFY(isBlack());
    dummy.hide();

    // destroying the fakeWindow should not remove the blanked screen
    fakeWindow.destroy();
    QTest::qWait(1000);
    QVERIFY(isBlack());

    // let's create another window and try to raise it above the lockWindow
    // using a QWidget to get proper content which won't be black
    QWidget widgetWindow;
    widgetWindow.setGeometry(10, 10, 100, 100);
    QPalette p1;
    p1.setColor(QPalette::Window, Qt::blue);
    widgetWindow.setAutoFillBackground(true);
    widgetWindow.setPalette(p1);
    widgetWindow.show();
    const uint32_t values[] = {XCB_STACK_MODE_ABOVE};
    xcb_configure_window(c, widgetWindow.winId(), XCB_CONFIG_WINDOW_STACK_MODE, values);
    xcb_flush(c);
    QTest::qWait(1000);
    QVERIFY(isBlack());

    lockWindow.hideLockWindow();
}

void LockWindowTest::testEmergencyShow()
{
    if (!KWindowSystem::isPlatformX11()) {
        QSKIP("test requires X11");
    }

    QWidget dummy;
    dummy.setWindowFlags(Qt::X11BypassWindowManagerHint);
    QPalette p;
    p.setColor(QPalette::Window, Qt::red);
    dummy.setAutoFillBackground(true);
    dummy.setPalette(p);
    dummy.setGeometry(0, 0, 100, 100);
    dummy.show();
    xcb_flush(QX11Info::connection());

    // Lets wait till it gets shown
    QTest::qWait(1000);

    // Verify that red window is shown
    QVERIFY(isColored(Qt::red, 0, 0, 100, 100));

    qputenv("KSLD_TESTMODE", QByteArrayLiteral("true"));

    ScreenLocker::X11Locker lockWindow;
    lockWindow.showLockWindow();

    QTest::qWait(1000);

    // the screen used to be blanked once the first lock window gets mapped, so let's create one
    QWindow fakeWindow;
    fakeWindow.setFlags(Qt::X11BypassWindowManagerHint);
    // it's on purpose outside the visual area
    fakeWindow.setGeometry(-1, -1, 1, 1);
    fakeWindow.create();
    xcb_atom_t atom = screenLockerAtom();
    QVERIFY(atom != XCB_ATOM_NONE);
    xcb_connection_t *c = QX11Info::connection();
    xcb_change_property(c, XCB_PROP_MODE_REPLACE, fakeWindow.winId(), atom, atom, 32, 0, nullptr);
    xcb_flush(c);
    fakeWindow.show();

    // nowadays we need to pass the window id to the lock window
    lockWindow.addAllowedWindow(fakeWindow.winId());

    QTest::qWait(1000);

    lockWindow.emergencyShow();

    QTest::qWait(1000);
    xcb_flush(c);

    xcb_screen_t *screen = defaultScreen();
    const int width = screen->width_in_pixels;
    const int height = screen->height_in_pixels;

    const auto cookie = xcb_get_image(c, XCB_IMAGE_FORMAT_Z_PIXMAP, QX11Info::appRootWindow(), 0, 0, width, height, ~0);
    ScopedCPointer<xcb_get_image_reply_t> xImage(xcb_get_image_reply(c, cookie, nullptr));

    QVERIFY(!xImage.isNull());

    // this operates on the assumption that X server default depth matches Qt's image format
    QImage image(xcb_get_image_data(xImage.data()), width, height, xcb_get_image_data_length(xImage.data()) / height, QImage::Format_ARGB32_Premultiplied);

    bool isColored = false;
    int blackPixelCount = 0;
    int whitePixelCount = 0;

    // This verifies that,
    // - there is at least one white and one black pixel
    // - there is no other colored pixel
    QColor color;
    for (int i = 0; i < image.width(); i++) {
        for (int j = 0; j < image.height(); j++) {
            color = QColor(image.pixel(i, j));
            if (color == Qt::black) {
                blackPixelCount++;
            } else if (color == Qt::white) {
                whitePixelCount++;
            } else {
                isColored = true;
                break;
            }
        }
    }

    QVERIFY(!isColored);
    QVERIFY(blackPixelCount > 0);
    QVERIFY(whitePixelCount > 0);

    lockWindow.hideLockWindow();
}

QTEST_MAIN(LockWindowTest)
#include "x11lockertest.moc"