KWin
Loading...
Searching...
No Matches
screencasting_test.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2023 Aleix Pol Gonzalez <aleixpol@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
9#include "compositor.h"
10#include "core/output.h"
12#include "opengl/glplatform.h"
13#include "pointer_input.h"
15#include "wayland_server.h"
16#include "window.h"
17#include "workspace.h"
18
19#include <KWayland/Client/output.h>
20#include <KWayland/Client/subsurface.h>
21#include <KWayland/Client/surface.h>
22#include <PipeWireSourceStream>
23#include <QPainter>
24#include <QScreen>
25
26#define QCOMPAREIMG(actual, expected, id) \
27 { \
28 if ((actual) != (expected)) { \
29 const auto actualFile = QStringLiteral("appium_artifact_actual_%1.png").arg(id); \
30 const auto expectedFile = QStringLiteral("appium_artifact_expected_%1.png").arg(id); \
31 (actual).save(actualFile); \
32 (expected).save(expectedFile); \
33 qDebug() << "Generated failed file" << actualFile << expectedFile; \
34 } \
35 QCOMPARE(actual, expected); \
36 }
37
38namespace KWin
39{
40
41static const QString s_socketName = QStringLiteral("wayland_test_buffer_size_change-0");
42
44{
45 Q_OBJECT
46public:
48 : GenericSceneOpenGLTest(QByteArrayLiteral("O2"))
49 {
50 auto wrap = [this](const QString &process, const QStringList &arguments = {}) {
51 // Make sure PipeWire is running. If it's already running it will just exit
52 QProcess *p = new QProcess(this);
53 p->setProcessChannelMode(QProcess::MergedChannels);
54 p->setArguments(arguments);
55 connect(this, &QObject::destroyed, p, [p] {
56 p->terminate();
57 p->waitForFinished();
58 p->kill();
59 });
60 connect(p, &QProcess::errorOccurred, p, [p](auto status) {
61 qDebug() << "error" << status << p->program();
62 });
63 connect(p, &QProcess::finished, p, [p](int code, auto status) {
64 if (code != 0) {
65 qDebug() << p->readAll();
66 }
67 qDebug() << "finished" << code << status << p->program();
68 });
69 p->setProgram(process);
70 p->start();
71 };
72
73 // If I run this outside the CI, it breaks the system's pipewire
74 if (qgetenv("KDECI_BUILD") == "TRUE") {
75 wrap("pipewire");
76 wrap("dbus-launch", {"wireplumber"});
77 }
78 }
79private Q_SLOTS:
80 void init();
81 void testWindowCasting();
82 void testOutputCasting();
83
84private:
85 std::optional<QImage> oneFrameAndClose(Test::ScreencastingStreamV1 *stream);
86};
87
88void ScreencastingTest::init()
89{
93}
94
95std::optional<QImage> ScreencastingTest::oneFrameAndClose(Test::ScreencastingStreamV1 *stream)
96{
97 Q_ASSERT(stream);
98 PipeWireSourceStream pwStream;
99 qDebug() << "start" << stream;
100 connect(stream, &Test::ScreencastingStreamV1::failed, qGuiApp, [](const QString &error) {
101 qDebug() << "stream failed with error" << error;
102 Q_ASSERT(false);
103 });
104 connect(stream, &Test::ScreencastingStreamV1::closed, qGuiApp, [&pwStream] {
105 pwStream.setActive(false);
106 });
107 connect(stream, &Test::ScreencastingStreamV1::created, qGuiApp, [&pwStream](quint32 nodeId) {
108 pwStream.createStream(nodeId, 0);
109 });
110
111 std::optional<QImage> img;
112 connect(&pwStream, &PipeWireSourceStream::frameReceived, qGuiApp, [&img](const PipeWireFrame &frame) {
113 if (frame.dataFrame) {
114 img = frame.dataFrame->toImage();
115 }
116 });
117
118 QSignalSpy spy(&pwStream, &PipeWireSourceStream::frameReceived);
119 if (!spy.wait()) {
120 qDebug() << "Did not receive any frames";
121 }
122 pwStream.stopStreaming();
123 return img;
124}
125
126void ScreencastingTest::testWindowCasting()
127{
128 std::unique_ptr<KWayland::Client::Surface> surface(Test::createSurface());
129 QVERIFY(surface != nullptr);
130
131 std::unique_ptr<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.get()));
132 QVERIFY(shellSurface != nullptr);
133
134 QImage sourceImage(QSize(30, 10), QImage::Format_RGBA8888_Premultiplied);
135 sourceImage.fill(Qt::red);
136
137 Window *window = Test::renderAndWaitForShown(surface.get(), sourceImage);
138 QVERIFY(window);
139
140 auto stream = KWin::Test::screencasting()->createWindowStream(window->internalId().toString(), QtWayland::zkde_screencast_unstable_v1::pointer_hidden);
141
142 std::optional<QImage> img = oneFrameAndClose(stream);
143 QVERIFY(img);
144 img->convertTo(sourceImage.format());
145 QCOMPAREIMG(*img, sourceImage, QLatin1String("window_cast"));
146}
147
148void ScreencastingTest::testOutputCasting()
149{
150 auto theOutput = KWin::Test::waylandOutputs().constFirst();
151
152 std::unique_ptr<KWayland::Client::Surface> surface(Test::createSurface());
153 QVERIFY(surface != nullptr);
154
155 std::unique_ptr<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.get()));
156 QVERIFY(shellSurface != nullptr);
157
158 QImage sourceImage(theOutput->pixelSize(), QImage::Format_RGBA8888_Premultiplied);
159 sourceImage.fill(Qt::green);
160 {
161 QPainter p(&sourceImage);
162 p.drawRect(100, 100, 100, 100);
163 }
164
165 Window *window = Test::renderAndWaitForShown(surface.get(), sourceImage);
166 QVERIFY(window);
167 QCOMPARE(window->frameGeometry(), window->output()->geometry());
168
169 auto stream = KWin::Test::screencasting()->createOutputStream(theOutput->output(), QtWayland::zkde_screencast_unstable_v1::pointer_hidden);
170
171 std::optional<QImage> img = oneFrameAndClose(stream);
172 QVERIFY(img);
173 img->convertTo(sourceImage.format());
174 QCOMPAREIMG(*img, sourceImage, QLatin1String("output_cast"));
175}
176
177}
178
180#include "screencasting_test.moc"
void hideCursor()
Definition cursor.cpp:69
static Cursors * self()
Definition cursor.cpp:35
void failed(const QString &error)
void created(quint32 nodeid)
ScreencastingStreamV1 * createOutputStream(wl_output *output, pointer mode)
ScreencastingStreamV1 * createWindowStream(const QString &uuid, pointer mode)
#define WAYLANDTEST_MAIN(TestObject)
Window * renderAndWaitForShown(KWayland::Client::Surface *surface, const QSize &size, const QColor &color, const QImage::Format &format=QImage::Format_ARGB32, int timeout=5000)
bool setupWaylandConnection(AdditionalWaylandInterfaces flags=AdditionalWaylandInterfaces())
ScreencastingV1 * screencasting()
QList< KWayland::Client::Output * > waylandOutputs()
std::unique_ptr< KWayland::Client::Surface > createSurface()
XdgToplevel * createXdgToplevelSurface(KWayland::Client::Surface *surface, QObject *parent=nullptr)
#define QCOMPAREIMG(actual, expected, id)