File indexing completed on 2024-11-10 04:55:55

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kwin_wayland_test.h"
0011 
0012 #include "effect/effecthandler.h"
0013 #include "effect/effectloader.h"
0014 #include "virtualdesktops.h"
0015 #include "wayland_server.h"
0016 #include "window.h"
0017 #include "workspace.h"
0018 
0019 #include <KWayland/Client/surface.h>
0020 
0021 using namespace KWin;
0022 
0023 static const QString s_socketName = QStringLiteral("wayland_test_effects_desktop_switching_animation-0");
0024 
0025 class DesktopSwitchingAnimationTest : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 private Q_SLOTS:
0030     void initTestCase();
0031     void init();
0032     void cleanup();
0033 
0034     void testSwitchDesktops_data();
0035     void testSwitchDesktops();
0036 };
0037 
0038 void DesktopSwitchingAnimationTest::initTestCase()
0039 {
0040     if (!Test::renderNodeAvailable()) {
0041         QSKIP("no render node available");
0042         return;
0043     }
0044     qputenv("XDG_DATA_DIRS", QCoreApplication::applicationDirPath().toUtf8());
0045 
0046     qRegisterMetaType<KWin::Window *>();
0047     QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
0048     QVERIFY(waylandServer()->init(s_socketName));
0049     Test::setOutputConfig({
0050         QRect(0, 0, 1280, 1024),
0051         QRect(1280, 0, 1280, 1024),
0052     });
0053 
0054     auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig);
0055     KConfigGroup plugins(config, QStringLiteral("Plugins"));
0056     const auto builtinNames = EffectLoader().listOfKnownEffects();
0057     for (const QString &name : builtinNames) {
0058         plugins.writeEntry(name + QStringLiteral("Enabled"), false);
0059     }
0060     config->sync();
0061     kwinApp()->setConfig(config);
0062 
0063     qputenv("KWIN_COMPOSE", QByteArrayLiteral("O2"));
0064     qputenv("KWIN_EFFECTS_FORCE_ANIMATIONS", QByteArrayLiteral("1"));
0065 
0066     kwinApp()->start();
0067     QVERIFY(applicationStartedSpy.wait());
0068 }
0069 
0070 void DesktopSwitchingAnimationTest::init()
0071 {
0072     QVERIFY(Test::setupWaylandConnection());
0073 }
0074 
0075 void DesktopSwitchingAnimationTest::cleanup()
0076 {
0077     QVERIFY(effects);
0078     effects->unloadAllEffects();
0079     QVERIFY(effects->loadedEffects().isEmpty());
0080 
0081     VirtualDesktopManager::self()->setCount(1);
0082 
0083     Test::destroyWaylandConnection();
0084 }
0085 
0086 void DesktopSwitchingAnimationTest::testSwitchDesktops_data()
0087 {
0088     QTest::addColumn<QString>("effectName");
0089 
0090     QTest::newRow("Fade Desktop") << QStringLiteral("fadedesktop");
0091     QTest::newRow("Slide") << QStringLiteral("slide");
0092 }
0093 
0094 void DesktopSwitchingAnimationTest::testSwitchDesktops()
0095 {
0096     // This test verifies that virtual desktop switching animation effects actually
0097     // try to animate switching between desktops.
0098 
0099     // We need at least 2 virtual desktops for the test.
0100     VirtualDesktopManager::self()->setCount(2);
0101     QCOMPARE(VirtualDesktopManager::self()->current(), 1u);
0102     QCOMPARE(VirtualDesktopManager::self()->count(), 2u);
0103 
0104     // The Fade Desktop effect will do nothing if there are no windows to fade,
0105     // so we have to create a dummy test window.
0106     std::unique_ptr<KWayland::Client::Surface> surface(Test::createSurface());
0107     QVERIFY(surface != nullptr);
0108     std::unique_ptr<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.get()));
0109     QVERIFY(shellSurface != nullptr);
0110     Window *window = Test::renderAndWaitForShown(surface.get(), QSize(100, 50), Qt::blue);
0111     QVERIFY(window);
0112     QCOMPARE(window->desktops().count(), 1);
0113     QCOMPARE(window->desktops().first(), VirtualDesktopManager::self()->desktops().first());
0114 
0115     // Load effect that will be tested.
0116     QFETCH(QString, effectName);
0117     QVERIFY(effects);
0118     QVERIFY(effects->loadEffect(effectName));
0119     QCOMPARE(effects->loadedEffects().count(), 1);
0120     QCOMPARE(effects->loadedEffects().first(), effectName);
0121     Effect *effect = effects->findEffect(effectName);
0122     QVERIFY(effect);
0123     QVERIFY(!effect->isActive());
0124 
0125     // Switch to the second virtual desktop.
0126     VirtualDesktopManager::self()->setCurrent(2u);
0127     QCOMPARE(VirtualDesktopManager::self()->current(), 2u);
0128     QVERIFY(effect->isActive());
0129     QCOMPARE(effects->activeFullScreenEffect(), effect);
0130 
0131     // Eventually, the animation will be complete.
0132     QTRY_VERIFY(!effect->isActive());
0133     QTRY_COMPARE(effects->activeFullScreenEffect(), nullptr);
0134 
0135     // Destroy the test window.
0136     surface.reset();
0137     QVERIFY(Test::waitForWindowClosed(window));
0138 }
0139 
0140 WAYLANDTEST_MAIN(DesktopSwitchingAnimationTest)
0141 #include "desktop_switching_animation_test.moc"