kwin/autotests/integration/idle_inhibition_test.cpp

358 lines
13 KiB
C++
Raw Normal View History

2020-08-02 22:22:19 +00:00
/*
KWin - the KDE window manager
This file is part of the KDE project.
2020-08-02 22:22:19 +00:00
SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
2020-08-02 22:22:19 +00:00
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kwin_wayland_test.h"
#include "platform.h"
#include "virtualdesktops.h"
#include "wayland/display.h"
#include "wayland/idle_interface.h"
#include "wayland_server.h"
2022-04-22 17:39:12 +00:00
#include "window.h"
#include "workspace.h"
#include <KWayland/Client/surface.h>
using namespace KWin;
using namespace KWayland::Client;
using KWaylandServer::IdleInterface;
static const QString s_socketName = QStringLiteral("wayland_test_kwin_idle_inhbition_test-0");
class TestIdleInhibition : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void init();
void cleanup();
void testInhibit();
void testDontInhibitWhenNotOnCurrentDesktop();
void testDontInhibitWhenMinimized();
void testDontInhibitWhenUnmapped();
void testDontInhibitWhenLeftCurrentDesktop();
};
void TestIdleInhibition::initTestCase()
{
2022-04-22 17:39:12 +00:00
qRegisterMetaType<KWin::Window *>();
QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
QVERIFY(applicationStartedSpy.isValid());
kwinApp()->platform()->setInitialWindowSize(QSize(1280, 1024));
QVERIFY(waylandServer()->init(s_socketName));
QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::DirectConnection, Q_ARG(int, 2));
kwinApp()->start();
QVERIFY(applicationStartedSpy.wait());
Test::initWaylandWorkspace();
}
void TestIdleInhibition::init()
{
QVERIFY(Test::setupWaylandConnection(Test::AdditionalWaylandInterface::IdleInhibitV1));
}
void TestIdleInhibition::cleanup()
{
Test::destroyWaylandConnection();
VirtualDesktopManager::self()->setCount(1);
QCOMPARE(VirtualDesktopManager::self()->count(), 1u);
}
void TestIdleInhibition::testInhibit()
{
auto idle = waylandServer()->display()->findChild<IdleInterface *>();
QVERIFY(idle);
QVERIFY(!idle->isInhibited());
QSignalSpy inhibitedSpy(idle, &IdleInterface::inhibitedChanged);
QVERIFY(inhibitedSpy.isValid());
// now create window
QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
// now create inhibition on window
QScopedPointer<Test::IdleInhibitorV1> inhibitor(Test::createIdleInhibitorV1(surface.data()));
QVERIFY(inhibitor);
2022-04-23 19:51:16 +00:00
// render the window
auto window = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
QVERIFY(window);
// this should inhibit our server object
QVERIFY(idle->isInhibited());
// deleting the object should uninhibit again
inhibitor.reset();
QVERIFY(inhibitedSpy.wait());
QVERIFY(!idle->isInhibited());
// inhibit again and destroy window
QScopedPointer<Test::IdleInhibitorV1> inhibitor2(Test::createIdleInhibitorV1(surface.data()));
QVERIFY(inhibitedSpy.wait());
QVERIFY(idle->isInhibited());
shellSurface.reset();
2022-04-23 19:51:16 +00:00
QVERIFY(Test::waitForWindowDestroyed(window));
QTRY_VERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 4);
}
void TestIdleInhibition::testDontInhibitWhenNotOnCurrentDesktop()
{
// This test verifies that the idle inhibitor object is not honored when
// the associated surface is not on the current virtual desktop.
VirtualDesktopManager::self()->setCount(2);
QCOMPARE(VirtualDesktopManager::self()->count(), 2u);
// Get reference to the idle interface.
auto idle = waylandServer()->display()->findChild<IdleInterface *>();
QVERIFY(idle);
QVERIFY(!idle->isInhibited());
QSignalSpy inhibitedSpy(idle, &IdleInterface::inhibitedChanged);
QVERIFY(inhibitedSpy.isValid());
2022-04-23 19:51:16 +00:00
// Create the test window.
QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
QVERIFY(!surface.isNull());
QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
QVERIFY(!shellSurface.isNull());
// Create the inhibitor object.
QScopedPointer<Test::IdleInhibitorV1> inhibitor(Test::createIdleInhibitorV1(surface.data()));
QVERIFY(inhibitor);
2022-04-23 19:51:16 +00:00
// Render the window.
auto window = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
QVERIFY(window);
2022-04-23 19:51:16 +00:00
// The test window should be only on the first virtual desktop.
QCOMPARE(window->desktops().count(), 1);
QCOMPARE(window->desktops().first(), VirtualDesktopManager::self()->desktops().first());
// This should inhibit our server object.
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 1);
// Switch to the second virtual desktop.
VirtualDesktopManager::self()->setCurrent(2);
// The surface is no longer visible, so the compositor don't have to honor the
// idle inhibitor object.
QVERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 2);
// Switch back to the first virtual desktop.
VirtualDesktopManager::self()->setCurrent(1);
2022-04-23 19:51:16 +00:00
// The test window became visible again, so the compositor has to honor the idle
// inhibitor object back again.
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 3);
2022-04-23 19:51:16 +00:00
// Destroy the test window.
shellSurface.reset();
2022-04-23 19:51:16 +00:00
QVERIFY(Test::waitForWindowDestroyed(window));
QTRY_VERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 4);
}
void TestIdleInhibition::testDontInhibitWhenMinimized()
{
// This test verifies that the idle inhibitor object is not honored when the
// associated surface is minimized.
// Get reference to the idle interface.
auto idle = waylandServer()->display()->findChild<IdleInterface *>();
QVERIFY(idle);
QVERIFY(!idle->isInhibited());
QSignalSpy inhibitedSpy(idle, &IdleInterface::inhibitedChanged);
QVERIFY(inhibitedSpy.isValid());
2022-04-23 19:51:16 +00:00
// Create the test window.
QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
QVERIFY(!surface.isNull());
QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
QVERIFY(!shellSurface.isNull());
// Create the inhibitor object.
QScopedPointer<Test::IdleInhibitorV1> inhibitor(Test::createIdleInhibitorV1(surface.data()));
QVERIFY(inhibitor);
2022-04-23 19:51:16 +00:00
// Render the window.
auto window = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
QVERIFY(window);
// This should inhibit our server object.
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 1);
2022-04-23 19:51:16 +00:00
// Minimize the window, the idle inhibitor object should not be honored.
window->minimize();
QVERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 2);
2022-04-23 19:51:16 +00:00
// Unminimize the window, the idle inhibitor object should be honored back again.
window->unminimize();
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 3);
2022-04-23 19:51:16 +00:00
// Destroy the test window.
shellSurface.reset();
2022-04-23 19:51:16 +00:00
QVERIFY(Test::waitForWindowDestroyed(window));
QTRY_VERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 4);
}
void TestIdleInhibition::testDontInhibitWhenUnmapped()
{
// This test verifies that the idle inhibitor object is not honored by KWin
2022-04-23 19:51:16 +00:00
// when the associated window is unmapped.
// Get reference to the idle interface.
auto idle = waylandServer()->display()->findChild<IdleInterface *>();
QVERIFY(idle);
QVERIFY(!idle->isInhibited());
QSignalSpy inhibitedSpy(idle, &IdleInterface::inhibitedChanged);
QVERIFY(inhibitedSpy.isValid());
2022-04-23 19:51:16 +00:00
// Create the test window.
QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
QVERIFY(!surface.isNull());
QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
QVERIFY(!shellSurface.isNull());
QSignalSpy surfaceConfigureRequestedSpy(shellSurface->xdgSurface(), &Test::XdgSurface::configureRequested);
QVERIFY(surfaceConfigureRequestedSpy.isValid());
// Create the inhibitor object.
QScopedPointer<Test::IdleInhibitorV1> inhibitor(Test::createIdleInhibitorV1(surface.data()));
QVERIFY(inhibitor);
2022-04-23 19:51:16 +00:00
// Map the window.
QSignalSpy windowAddedSpy(workspace(), &Workspace::windowAdded);
QVERIFY(windowAddedSpy.isValid());
Test::render(surface.data(), QSize(100, 50), Qt::blue);
QVERIFY(windowAddedSpy.isEmpty());
QVERIFY(windowAddedSpy.wait());
QCOMPARE(windowAddedSpy.count(), 1);
2022-04-23 19:51:16 +00:00
Window *window = windowAddedSpy.last().first().value<Window *>();
QVERIFY(window);
QCOMPARE(window->readyForPainting(), true);
// The compositor will respond with a configure event when the surface becomes active.
QVERIFY(surfaceConfigureRequestedSpy.wait());
QCOMPARE(surfaceConfigureRequestedSpy.count(), 1);
// This should inhibit our server object.
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 1);
2022-04-23 19:51:16 +00:00
// Unmap the window.
surface->attachBuffer(Buffer::Ptr());
surface->commit(KWayland::Client::Surface::CommitFlag::None);
2022-04-23 19:51:16 +00:00
QVERIFY(Test::waitForWindowDestroyed(window));
// The surface is no longer visible, so the compositor doesn't have to honor the
// idle inhibitor object.
QVERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 2);
// Tell the compositor that we want to map the surface.
surface->commit(KWayland::Client::Surface::CommitFlag::None);
// The compositor will respond with a configure event.
QVERIFY(surfaceConfigureRequestedSpy.wait());
QCOMPARE(surfaceConfigureRequestedSpy.count(), 2);
2022-04-23 19:51:16 +00:00
// Map the window.
Test::render(surface.data(), QSize(100, 50), Qt::blue);
QVERIFY(windowAddedSpy.wait());
QCOMPARE(windowAddedSpy.count(), 2);
2022-04-23 19:51:16 +00:00
window = windowAddedSpy.last().first().value<Window *>();
QVERIFY(window);
QCOMPARE(window->readyForPainting(), true);
2022-04-23 19:51:16 +00:00
// The test window became visible again, so the compositor has to honor the idle
// inhibitor object back again.
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 3);
2022-04-23 19:51:16 +00:00
// Destroy the test window.
shellSurface.reset();
2022-04-23 19:51:16 +00:00
QVERIFY(Test::waitForWindowDestroyed(window));
QTRY_VERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 4);
}
void TestIdleInhibition::testDontInhibitWhenLeftCurrentDesktop()
{
// This test verifies that the idle inhibitor object is not honored by KWin
// when the associated surface leaves the current virtual desktop.
VirtualDesktopManager::self()->setCount(2);
QCOMPARE(VirtualDesktopManager::self()->count(), 2u);
// Get reference to the idle interface.
auto idle = waylandServer()->display()->findChild<IdleInterface *>();
QVERIFY(idle);
QVERIFY(!idle->isInhibited());
QSignalSpy inhibitedSpy(idle, &IdleInterface::inhibitedChanged);
QVERIFY(inhibitedSpy.isValid());
2022-04-23 19:51:16 +00:00
// Create the test window.
QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
QVERIFY(!surface.isNull());
QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
QVERIFY(!shellSurface.isNull());
// Create the inhibitor object.
QScopedPointer<Test::IdleInhibitorV1> inhibitor(Test::createIdleInhibitorV1(surface.data()));
QVERIFY(inhibitor);
2022-04-23 19:51:16 +00:00
// Render the window.
auto window = Test::renderAndWaitForShown(surface.data(), QSize(100, 50), Qt::blue);
QVERIFY(window);
2022-04-23 19:51:16 +00:00
// The test window should be only on the first virtual desktop.
QCOMPARE(window->desktops().count(), 1);
QCOMPARE(window->desktops().first(), VirtualDesktopManager::self()->desktops().first());
// This should inhibit our server object.
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 1);
2022-04-23 19:51:16 +00:00
// Let the window enter the second virtual desktop.
window->enterDesktop(VirtualDesktopManager::self()->desktops().at(1));
QCOMPARE(inhibitedSpy.count(), 1);
2022-04-23 19:51:16 +00:00
// If the window leaves the first virtual desktop, then the associated idle
// inhibitor object should not be honored.
2022-04-23 19:51:16 +00:00
window->leaveDesktop(VirtualDesktopManager::self()->desktops().at(0));
QVERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 2);
2022-04-23 19:51:16 +00:00
// If the window enters the first desktop, then the associated idle inhibitor
// object should be honored back again.
2022-04-23 19:51:16 +00:00
window->enterDesktop(VirtualDesktopManager::self()->desktops().at(0));
QVERIFY(idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 3);
2022-04-23 19:51:16 +00:00
// Destroy the test window.
shellSurface.reset();
2022-04-23 19:51:16 +00:00
QVERIFY(Test::waitForWindowDestroyed(window));
QTRY_VERIFY(!idle->isInhibited());
QCOMPARE(inhibitedSpy.count(), 4);
}
WAYLANDTEST_MAIN(TestIdleInhibition)
#include "idle_inhibition_test.moc"