File indexing completed on 2024-11-10 04:55:56
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 "core/output.h" 0013 #include "scripting/scripting.h" 0014 #include "wayland_server.h" 0015 #include "window.h" 0016 #include "workspace.h" 0017 0018 #include <KPackage/PackageLoader> 0019 #include <KWayland/Client/surface.h> 0020 0021 #include <linux/input.h> 0022 0023 namespace KWin 0024 { 0025 0026 static const QString s_socketName = QStringLiteral("wayland_test_minimizeall-0"); 0027 static const QString s_scriptName = QStringLiteral("minimizeall"); 0028 0029 class MinimizeAllScriptTest : public QObject 0030 { 0031 Q_OBJECT 0032 0033 private Q_SLOTS: 0034 void initTestCase(); 0035 void init(); 0036 void cleanup(); 0037 0038 void testMinimizeUnminimize(); 0039 }; 0040 0041 void MinimizeAllScriptTest::initTestCase() 0042 { 0043 qputenv("XDG_DATA_DIRS", QCoreApplication::applicationDirPath().toUtf8()); 0044 0045 qRegisterMetaType<Window *>(); 0046 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 kwinApp()->start(); 0055 QVERIFY(applicationStartedSpy.wait()); 0056 const auto outputs = workspace()->outputs(); 0057 QCOMPARE(outputs.count(), 2); 0058 QCOMPARE(outputs[0]->geometry(), QRect(0, 0, 1280, 1024)); 0059 QCOMPARE(outputs[1]->geometry(), QRect(1280, 0, 1280, 1024)); 0060 } 0061 0062 static QString locateMainScript(const QString &pluginName) 0063 { 0064 const QList<KPluginMetaData> offers = KPackage::PackageLoader::self()->findPackages( 0065 QStringLiteral("KWin/Script"), 0066 QStringLiteral("kwin/scripts"), 0067 [&](const KPluginMetaData &metaData) { 0068 return metaData.pluginId() == pluginName; 0069 }); 0070 if (offers.isEmpty()) { 0071 return QString(); 0072 } 0073 const QFileInfo metaDataFileInfo(offers.first().fileName()); 0074 return metaDataFileInfo.path() + QLatin1String("/contents/code/main.js"); 0075 } 0076 0077 void MinimizeAllScriptTest::init() 0078 { 0079 QVERIFY(Test::setupWaylandConnection()); 0080 0081 Scripting::self()->loadScript(locateMainScript(s_scriptName), s_scriptName); 0082 QTRY_VERIFY(Scripting::self()->isScriptLoaded(s_scriptName)); 0083 0084 AbstractScript *script = Scripting::self()->findScript(s_scriptName); 0085 QVERIFY(script); 0086 QSignalSpy runningChangedSpy(script, &AbstractScript::runningChanged); 0087 script->run(); 0088 QTRY_COMPARE(runningChangedSpy.count(), 1); 0089 } 0090 0091 void MinimizeAllScriptTest::cleanup() 0092 { 0093 Test::destroyWaylandConnection(); 0094 0095 Scripting::self()->unloadScript(s_scriptName); 0096 QTRY_VERIFY(!Scripting::self()->isScriptLoaded(s_scriptName)); 0097 } 0098 0099 void MinimizeAllScriptTest::testMinimizeUnminimize() 0100 { 0101 #if !KWIN_BUILD_GLOBALSHORTCUTS 0102 QSKIP("Can't test shortcuts without shortcuts"); 0103 return; 0104 #endif 0105 0106 // This test verifies that all windows are minimized when Meta+Shift+D 0107 // is pressed, and unminimized when the shortcut is pressed once again. 0108 0109 // Create a couple of test windows. 0110 std::unique_ptr<KWayland::Client::Surface> surface1(Test::createSurface()); 0111 std::unique_ptr<Test::XdgToplevel> shellSurface1(Test::createXdgToplevelSurface(surface1.get())); 0112 Window *window1 = Test::renderAndWaitForShown(surface1.get(), QSize(100, 50), Qt::blue); 0113 QVERIFY(window1); 0114 QVERIFY(window1->isActive()); 0115 QVERIFY(window1->isMinimizable()); 0116 0117 std::unique_ptr<KWayland::Client::Surface> surface2(Test::createSurface()); 0118 std::unique_ptr<Test::XdgToplevel> shellSurface2(Test::createXdgToplevelSurface(surface2.get())); 0119 Window *window2 = Test::renderAndWaitForShown(surface2.get(), QSize(100, 50), Qt::red); 0120 QVERIFY(window2); 0121 QVERIFY(window2->isActive()); 0122 QVERIFY(window2->isMinimizable()); 0123 0124 // Minimize the windows. 0125 quint32 timestamp = 1; 0126 Test::keyboardKeyPressed(KEY_LEFTMETA, timestamp++); 0127 Test::keyboardKeyPressed(KEY_LEFTSHIFT, timestamp++); 0128 Test::keyboardKeyPressed(KEY_D, timestamp++); 0129 Test::keyboardKeyReleased(KEY_D, timestamp++); 0130 Test::keyboardKeyReleased(KEY_LEFTSHIFT, timestamp++); 0131 Test::keyboardKeyReleased(KEY_LEFTMETA, timestamp++); 0132 0133 QTRY_VERIFY(window1->isMinimized()); 0134 QTRY_VERIFY(window2->isMinimized()); 0135 0136 // Unminimize the windows. 0137 Test::keyboardKeyPressed(KEY_LEFTMETA, timestamp++); 0138 Test::keyboardKeyPressed(KEY_LEFTSHIFT, timestamp++); 0139 Test::keyboardKeyPressed(KEY_D, timestamp++); 0140 Test::keyboardKeyReleased(KEY_D, timestamp++); 0141 Test::keyboardKeyReleased(KEY_LEFTSHIFT, timestamp++); 0142 Test::keyboardKeyReleased(KEY_LEFTMETA, timestamp++); 0143 0144 QTRY_VERIFY(!window1->isMinimized()); 0145 QTRY_VERIFY(!window2->isMinimized()); 0146 0147 // Destroy test windows. 0148 shellSurface2.reset(); 0149 QVERIFY(Test::waitForWindowClosed(window2)); 0150 shellSurface1.reset(); 0151 QVERIFY(Test::waitForWindowClosed(window1)); 0152 } 0153 0154 } 0155 0156 WAYLANDTEST_MAIN(KWin::MinimizeAllScriptTest) 0157 #include "minimizeall_test.moc"