Commit a738ecce authored by Vlad Zahorodnii's avatar Vlad Zahorodnii
Browse files

[scripting] Provide conversion functions for AbstractClient

Summary:
If no conversion functions are provided for a QObject-subclass, then
QScriptEngine will use QScriptEngine::newQObject() method without any
special options to convert an instance of that QObject-subclass to a
QScriptValue. However, it's very important that every client object is
wrapped with PreferExistingWrapperObject option. We need that option
because a script may set a property on a client object and that property
must remain until it's deleted by the script.

BUG: 413044
FIXED-IN: 5.17.2

Test Plan: New test.

Reviewers: #kwin, davidedmundson

Reviewed By: #kwin, davidedmundson

Subscribers: davidedmundson, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D24944
parent 8e176c8b
integrationTest(NAME testScriptingScreenEdge SRCS screenedge_test.cpp)
integrationTest(WAYLAND_ONLY NAME testMinimizeAllScript SRCS minimizeall_test.cpp)
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 Vlad Zahorodnii <vladzzag@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "kwin_wayland_test.h"
#include "platform.h"
#include "screens.h"
#include "scripting/scripting.h"
#include "shell_client.h"
#include "wayland_server.h"
#include "workspace.h"
#include <KPackage/PackageLoader>
#include <KWayland/Client/surface.h>
#include <linux/input.h>
namespace KWin
{
static const QString s_socketName = QStringLiteral("wayland_test_minimizeall-0");
static const QString s_scriptName = QStringLiteral("minimizeall");
class MinimizeAllScriptTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void init();
void cleanup();
void testMinimizeUnminimize();
};
void MinimizeAllScriptTest::initTestCase()
{
qputenv("XDG_DATA_DIRS", QCoreApplication::applicationDirPath().toUtf8());
qRegisterMetaType<AbstractClient *>();
qRegisterMetaType<ShellClient *>();
QSignalSpy workspaceCreatedSpy(kwinApp(), &Application::workspaceCreated);
QVERIFY(workspaceCreatedSpy.isValid());
kwinApp()->platform()->setInitialWindowSize(QSize(1280, 1024));
QVERIFY(waylandServer()->init(s_socketName.toLocal8Bit()));
QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::DirectConnection, Q_ARG(int, 2));
kwinApp()->start();
QVERIFY(workspaceCreatedSpy.wait());
QCOMPARE(screens()->count(), 2);
QCOMPARE(screens()->geometry(0), QRect(0, 0, 1280, 1024));
QCOMPARE(screens()->geometry(1), QRect(1280, 0, 1280, 1024));
waylandServer()->initWorkspace();
}
static QString locateMainScript(const QString &pluginName)
{
const QList<KPluginMetaData> offers = KPackage::PackageLoader::self()->findPackages(
QStringLiteral("KWin/Script"),
QStringLiteral("kwin/scripts"),
[&](const KPluginMetaData &metaData) {
return metaData.pluginId() == pluginName;
}
);
if (offers.isEmpty()) {
return QString();
}
const KPluginMetaData &metaData = offers.first();
const QString mainScriptFileName = metaData.value(QStringLiteral("X-Plasma-MainScript"));
const QFileInfo metaDataFileInfo(metaData.fileName());
return metaDataFileInfo.path() + QLatin1String("/contents/") + mainScriptFileName;
}
void MinimizeAllScriptTest::init()
{
QVERIFY(Test::setupWaylandConnection());
Scripting::self()->loadScript(locateMainScript(s_scriptName), s_scriptName);
QTRY_VERIFY(Scripting::self()->isScriptLoaded(s_scriptName));
AbstractScript *script = Scripting::self()->findScript(s_scriptName);
QVERIFY(script);
QSignalSpy runningChangedSpy(script, &AbstractScript::runningChanged);
QVERIFY(runningChangedSpy.isValid());
script->run();
QTRY_COMPARE(runningChangedSpy.count(), 1);
}
void MinimizeAllScriptTest::cleanup()
{
Test::destroyWaylandConnection();
Scripting::self()->unloadScript(s_scriptName);
QTRY_VERIFY(!Scripting::self()->isScriptLoaded(s_scriptName));
}
void MinimizeAllScriptTest::testMinimizeUnminimize()
{
// This test verifies that all windows are minimized when Meta+Shift+D
// is pressed, and unminimized when the shortcut is pressed once again.
using namespace KWayland::Client;
// Create a couple of test clients.
QScopedPointer<Surface> surface1(Test::createSurface());
QScopedPointer<XdgShellSurface> shellSurface1(Test::createXdgShellStableSurface(surface1.data()));
ShellClient *client1 = Test::renderAndWaitForShown(surface1.data(), QSize(100, 50), Qt::blue);
QVERIFY(client1);
QVERIFY(client1->isActive());
QVERIFY(client1->isMinimizable());
QScopedPointer<Surface> surface2(Test::createSurface());
QScopedPointer<XdgShellSurface> shellSurface2(Test::createXdgShellStableSurface(surface2.data()));
ShellClient *client2 = Test::renderAndWaitForShown(surface2.data(), QSize(100, 50), Qt::red);
QVERIFY(client2);
QVERIFY(client2->isActive());
QVERIFY(client2->isMinimizable());
// Minimize the windows.
quint32 timestamp = 1;
kwinApp()->platform()->keyboardKeyPressed(KEY_LEFTMETA, timestamp++);
kwinApp()->platform()->keyboardKeyPressed(KEY_LEFTSHIFT, timestamp++);
kwinApp()->platform()->keyboardKeyPressed(KEY_D, timestamp++);
kwinApp()->platform()->keyboardKeyReleased(KEY_D, timestamp++);
kwinApp()->platform()->keyboardKeyReleased(KEY_LEFTSHIFT, timestamp++);
kwinApp()->platform()->keyboardKeyReleased(KEY_LEFTMETA, timestamp++);
QTRY_VERIFY(client1->isMinimized());
QTRY_VERIFY(client2->isMinimized());
// Unminimize the windows.
kwinApp()->platform()->keyboardKeyPressed(KEY_LEFTMETA, timestamp++);
kwinApp()->platform()->keyboardKeyPressed(KEY_LEFTSHIFT, timestamp++);
kwinApp()->platform()->keyboardKeyPressed(KEY_D, timestamp++);
kwinApp()->platform()->keyboardKeyReleased(KEY_D, timestamp++);
kwinApp()->platform()->keyboardKeyReleased(KEY_LEFTSHIFT, timestamp++);
kwinApp()->platform()->keyboardKeyReleased(KEY_LEFTMETA, timestamp++);
QTRY_VERIFY(!client1->isMinimized());
QTRY_VERIFY(!client2->isMinimized());
// Destroy test clients.
shellSurface2.reset();
QVERIFY(Test::waitForWindowDestroyed(client2));
shellSurface1.reset();
QVERIFY(Test::waitForWindowDestroyed(client1));
}
}
WAYLANDTEST_MAIN(KWin::MinimizeAllScriptTest)
#include "minimizeall_test.moc"
......@@ -96,6 +96,20 @@ void Rect::fromScriptValue(const QScriptValue& obj, QRect &rect)
}
// End of meta for QRect object
QScriptValue AbstractClient::toScriptValue(QScriptEngine *engine, const KAbstractClientRef &client)
{
return engine->newQObject(client, QScriptEngine::QtOwnership,
QScriptEngine::ExcludeChildObjects |
QScriptEngine::ExcludeDeleteLater |
QScriptEngine::PreferExistingWrapperObject |
QScriptEngine::AutoCreateDynamicProperties);
}
void AbstractClient::fromScriptValue(const QScriptValue &value, KWin::AbstractClient *&client)
{
client = qobject_cast<KWin::AbstractClient *>(value.toQObject());
}
QScriptValue Client::toScriptValue(QScriptEngine *eng, const KClientRef &client)
{
return eng->newQObject(client, QScriptEngine::QtOwnership,
......@@ -130,6 +144,7 @@ void KWin::MetaScripting::registration(QScriptEngine* eng)
qScriptRegisterMetaType<QPoint>(eng, Point::toScriptValue, Point::fromScriptValue);
qScriptRegisterMetaType<QSize>(eng, Size::toScriptValue, Size::fromScriptValue);
qScriptRegisterMetaType<QRect>(eng, Rect::toScriptValue, Rect::fromScriptValue);
qScriptRegisterMetaType<KAbstractClientRef>(eng, AbstractClient::toScriptValue, AbstractClient::fromScriptValue);
qScriptRegisterMetaType<KClientRef>(eng, Client::toScriptValue, Client::fromScriptValue);
qScriptRegisterMetaType<KToplevelRef>(eng, Toplevel::toScriptValue, Toplevel::fromScriptValue);
......
......@@ -30,10 +30,12 @@ class QScriptContext;
class QSize;
namespace KWin {
class AbstractClient;
class Client;
class Toplevel;
}
typedef KWin::AbstractClient *KAbstractClientRef;
typedef KWin::Client* KClientRef;
typedef KWin::Toplevel* KToplevelRef;
......@@ -75,6 +77,12 @@ QScriptValue toScriptValue(QScriptEngine*, const QRect&);
void fromScriptValue(const QScriptValue&, QRect&);
}
namespace AbstractClient
{
QScriptValue toScriptValue(QScriptEngine *engine, const KAbstractClientRef &client);
void fromScriptValue(const QScriptValue &value, KAbstractClientRef &client);
}
namespace Client
{
QScriptValue toScriptValue(QScriptEngine *eng, const KClientRef &client);
......
kpackage_install_package(videowall videowall scripts kwin)
kpackage_install_package(synchronizeskipswitcher synchronizeskipswitcher scripts kwin)
kpackage_install_package(desktopchangeosd desktopchangeosd scripts kwin)
kpackage_install_package(minimizeall minimizeall scripts kwin)
function(add_kwin_script name)
kpackage_install_package(${name} ${name} scripts kwin)
# Copy the script to the build directory so one can run tests without prior
# make install. FIXME: use add_custom_command.
file(COPY ${name} DESTINATION ${CMAKE_BINARY_DIR}/bin/kwin/scripts/)
endfunction()
add_kwin_script(videowall)
add_kwin_script(synchronizeskipswitcher)
add_kwin_script(desktopchangeosd)
add_kwin_script(minimizeall)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment