File: ksldtest.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 (286 lines) | stat: -rw-r--r-- 9,422 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
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
/*
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>

SPDX-License-Identifier: GPL-2.0-or-later
*/
// own
#include "../ksldapp.h"
#include "kscreensaversettings.h"
// KDE Frameworks
#include <KIdleTime>
#include <KWindowSystem>
// Qt
#include <QProcess>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>
#include <private/qtx11extras_p.h>
// xcb
#include <xcb/xcb.h>
#include <xcb/xtest.h>
// POSIX
#include <sys/socket.h> // socketpair

class KSldTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void cleanup();
    void testEstablishGrab();
    void testActivateOnTimeout();
    void testGraceTimeUnlocking();
    void testLockAfterInhibit();
    void testRestartIdlePeriodAfterInhibit();
};

void KSldTest::initTestCase()
{
    QCoreApplication::setAttribute(Qt::AA_ForceRasterWidgets);
    // change to the build bin dir
    QDir::setCurrent(QCoreApplication::applicationDirPath());
    QStandardPaths::setTestModeEnabled(true);
}

void KSldTest::cleanup()
{
    KScreenSaverSettings::self()->setDefaults();
    KScreenSaverSettings::self()->save();
}

void KSldTest::testEstablishGrab()
{
    if (!KWindowSystem::isPlatformX11()) {
        QSKIP("keyboard and pointer grabbing by other processes is only possible on X11");
    }

    ScreenLocker::KSldApp ksld;
    ksld.initialize();
    QVERIFY(ksld.establishGrab());
    // grab is established, trying again should succeed as well
    QVERIFY(ksld.establishGrab());

    // let's ungrab
    ksld.doUnlock();

    // to get the grab to fail we need another X client
    // we start another process to perform our grab
    QProcess keyboardGrabber;
    keyboardGrabber.start(QStringLiteral("./keyboardGrabber"), QStringList());
    QVERIFY(keyboardGrabber.waitForStarted());

    // let's add some delay to be sure that keyboardGrabber has it's X stuff done
    QTest::qWait(std::chrono::milliseconds{100});

    // now grabbing should fail
    QVERIFY(!ksld.establishGrab());

    // let's terminate again
    keyboardGrabber.terminate();
    QVERIFY(keyboardGrabber.waitForFinished());

    // now grabbing should succeed again
    QVERIFY(ksld.establishGrab());

    ksld.doUnlock();

    // now the same with pointer
    QProcess pointerGrabber;
    pointerGrabber.start(QStringLiteral("./pointerGrabber"), QStringList());
    QVERIFY(pointerGrabber.waitForStarted());

    // let's add some delay to be sure that pointerGrabber has it's X stuff done
    QTest::qWait(std::chrono::milliseconds{100});

    // now grabbing should fail
    QVERIFY(!ksld.establishGrab());

    // let's terminate again
    pointerGrabber.terminate();
    QVERIFY(pointerGrabber.waitForFinished());

    // now grabbing should succeed again
    QVERIFY(ksld.establishGrab());
}

void KSldTest::testActivateOnTimeout()
{
    // this time verifies that the screen gets locked on idle timeout, requires the system to be idle
    ScreenLocker::KSldApp ksld;
    ksld.initialize();

    if (KWindowSystem::isPlatformWayland()) {
        // without having a wayland fd, the app will follow the X11 code path
        int sx[2];
        QCOMPARE(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx), 0);
        ksld.setWaylandFd(sx[1]);
    }

    // we need to modify the idle timeout of KSLD, it's in minutes we cannot wait that long
    if (ksld.idleId() != 0) {
        // remove old Idle id
        KIdleTime::instance()->removeIdleTimeout(ksld.idleId());
    }
    ksld.setIdleId(KIdleTime::instance()->addIdleTimeout(std::chrono::seconds{5}));

    QSignalSpy lockStateChangedSpy(&ksld, &ScreenLocker::KSldApp::lockStateChanged);
    QVERIFY(lockStateChangedSpy.isValid());

    // let's wait the double of the idle timeout
    QVERIFY2(lockStateChangedSpy.wait(std::chrono::seconds{10}),
             "If running the test locally, make sure you're not moving the mouse or otherwise interrupting this test's idle check.");
    QCOMPARE(ksld.lockState(), ScreenLocker::KSldApp::AcquiringLock);

    // let's simulate unlock to get rid of started greeter process
    const auto children = ksld.children();
    for (auto it = children.begin(); it != children.end(); ++it) {
        if (qstrcmp((*it)->metaObject()->className(), "LogindIntegration") != 0) {
            continue;
        }
        QMetaObject::invokeMethod(*it, "requestUnlock");
        break;
    }
    QVERIFY(lockStateChangedSpy.wait());

    KIdleTime::instance()->removeIdleTimeout(ksld.idleId());
}

void KSldTest::testGraceTimeUnlocking()
{
    if (!KWindowSystem::isPlatformX11()) {
        QSKIP("XCB fake input won't work on Wayland");
    }

    // this time verifies that the screen gets unlocked during grace time by simulated user activity
    ScreenLocker::KSldApp ksld(this);
    ksld.initialize();

    // we need to modify the idle timeout of KSLD, it's in minutes we cannot wait that long
    if (ksld.idleId() != 0) {
        // remove old Idle id
        KIdleTime::instance()->removeIdleTimeout(ksld.idleId());
    }
    ksld.setIdleId(KIdleTime::instance()->addIdleTimeout(std::chrono::seconds{5}));
    // infinite
    ksld.setGraceTime(-1);

    QSignalSpy lockedSpy(&ksld, &ScreenLocker::KSldApp::locked);
    QVERIFY(lockedSpy.isValid());
    QSignalSpy unlockedSpy(&ksld, &ScreenLocker::KSldApp::unlocked);
    QVERIFY(unlockedSpy.isValid());

    // let's wait quite some time to give the greeter a chance to come up
    QVERIFY(lockedSpy.wait(std::chrono::seconds{30}));
    QCOMPARE(ksld.lockState(), ScreenLocker::KSldApp::Locked);

    // let's simulate unlock by faking input
    const QPoint cursorPos = QCursor::pos();
    xcb_test_fake_input(QX11Info::connection(), XCB_MOTION_NOTIFY, 0, XCB_TIME_CURRENT_TIME, XCB_WINDOW_NONE, cursorPos.x() + 1, cursorPos.y() + 1, 0);
    xcb_flush(QX11Info::connection());
    QVERIFY(unlockedSpy.wait());

    // now let's test again without grace time
    ksld.setGraceTime(0);
    QVERIFY(lockedSpy.wait(std::chrono::seconds{30}));
    QCOMPARE(ksld.lockState(), ScreenLocker::KSldApp::Locked);

    xcb_test_fake_input(QX11Info::connection(), XCB_MOTION_NOTIFY, 0, XCB_TIME_CURRENT_TIME, XCB_WINDOW_NONE, cursorPos.x(), cursorPos.y(), 0);
    xcb_flush(QX11Info::connection());
    QVERIFY(!unlockedSpy.wait());

    // and unlock
    const auto children = ksld.children();
    for (auto it = children.begin(); it != children.end(); ++it) {
        if (qstrcmp((*it)->metaObject()->className(), "LogindIntegration") != 0) {
            continue;
        }
        QMetaObject::invokeMethod(*it, "requestUnlock");
        break;
    }
    QVERIFY(unlockedSpy.wait());
}

void KSldTest::testLockAfterInhibit()
{
    KScreenSaverSettings::setTimeout(1 / 60.0); // 1 second
    KScreenSaverSettings::self()->save();

    ScreenLocker::KSldApp ksld;
    if (KWindowSystem::isPlatformWayland()) {
        // without having a wayland fd, the app will follow the X11 code path
        int sx[2];
        QCOMPARE(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx), 0);
        ksld.setWaylandFd(sx[1]);
    }

    QSignalSpy lockStateChangedSpy(&ksld, &ScreenLocker::KSldApp::lockStateChanged);
    QVERIFY(lockStateChangedSpy.isValid());

    QSignalSpy idleSpy(KIdleTime::instance(), &KIdleTime::timeoutReached);
    QVERIFY(idleSpy.isValid());

    ksld.inhibit();
    ksld.initialize();

    QCOMPARE(ksld.lockState(), ScreenLocker::KSldApp::Unlocked);
    QCOMPARE(lockStateChangedSpy.count(), 0);
    QVERIFY(!lockStateChangedSpy.wait(std::chrono::seconds{2}));

    ksld.uninhibit();

    QVERIFY(idleSpy.wait());
    QCOMPARE_NE(ksld.lockState(), ScreenLocker::KSldApp::Unlocked);

    const auto children = ksld.children();
    for (auto it = children.begin(); it != children.end(); ++it) {
        if (qstrcmp((*it)->metaObject()->className(), "LogindIntegration") != 0) {
            continue;
        }
        QMetaObject::invokeMethod(*it, "requestUnlock");
        break;
    }
    QTRY_COMPARE(ksld.lockState(), ScreenLocker::KSldApp::Unlocked);
}

void KSldTest::testRestartIdlePeriodAfterInhibit()
{
    KScreenSaverSettings::setTimeout(2 / 60.0); // 2 seconds
    KScreenSaverSettings::self()->save();

    ScreenLocker::KSldApp ksld;
    if (KWindowSystem::isPlatformWayland()) {
        // without having a wayland fd, the app will follow the X11 code path
        int sx[2];
        QCOMPARE(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx), 0);
        ksld.setWaylandFd(sx[1]);
    }

    QSignalSpy lockStateChangedSpy(&ksld, &ScreenLocker::KSldApp::lockStateChanged);
    QVERIFY(lockStateChangedSpy.isValid());

    ksld.inhibit();
    ksld.initialize();

    QVERIFY(!lockStateChangedSpy.wait(std::chrono::milliseconds(1800)));

    ksld.uninhibit();

    QVERIFY(!lockStateChangedSpy.wait(std::chrono::milliseconds(500)));
    QCOMPARE(ksld.lockState(), ScreenLocker::KSldApp::Unlocked);

    QVERIFY(lockStateChangedSpy.wait(std::chrono::seconds(2)));
    QCOMPARE_NE(ksld.lockState(), ScreenLocker::KSldApp::Unlocked);

    const auto children = ksld.children();
    for (auto it = children.begin(); it != children.end(); ++it) {
        if (qstrcmp((*it)->metaObject()->className(), "LogindIntegration") != 0) {
            continue;
        }
        QMetaObject::invokeMethod(*it, "requestUnlock");
        break;
    }
    QTRY_COMPARE(ksld.lockState(), ScreenLocker::KSldApp::Unlocked);
}

QTEST_MAIN(KSldTest)
#include "ksldtest.moc"