File indexing completed on 2024-11-10 04:56:08

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2017 Martin Flöser <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "kwin_wayland_test.h"
0011 
0012 #include "main.h"
0013 #include "virtualkeyboard_dbus.h"
0014 #include "wayland_server.h"
0015 
0016 #include <QDBusConnection>
0017 #include <QDBusMessage>
0018 #include <QDBusPendingReply>
0019 #include <QSignalSpy>
0020 #include <QTest>
0021 
0022 #include <virtualkeyboardinterface.h>
0023 
0024 using KWin::VirtualKeyboardDBus;
0025 using namespace KWin;
0026 using namespace KWin::Test;
0027 
0028 static const QString s_socketName = QStringLiteral("wayland_test_kwin_virtualkeyboarddbus-0");
0029 
0030 class VirtualKeyboardDBusTest : public QObject
0031 {
0032     Q_OBJECT
0033 private Q_SLOTS:
0034     void initTestCase();
0035     void testEnabled();
0036     void testRequestEnabled_data();
0037     void testRequestEnabled();
0038     void init();
0039     void cleanup();
0040 };
0041 
0042 void VirtualKeyboardDBusTest::initTestCase()
0043 {
0044     QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kwin.testvirtualkeyboard"));
0045     QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
0046     QVERIFY(waylandServer()->init(s_socketName));
0047     Test::setOutputConfig({
0048         QRect(0, 0, 1280, 1024),
0049         QRect(1280, 0, 1280, 1024),
0050     });
0051 
0052     static_cast<WaylandTestApplication *>(kwinApp())->setInputMethodServerToStart("internal");
0053     kwinApp()->start();
0054     QVERIFY(applicationStartedSpy.wait());
0055 
0056     QVERIFY(setupWaylandConnection(AdditionalWaylandInterface::Seat | AdditionalWaylandInterface::InputMethodV1 | AdditionalWaylandInterface::TextInputManagerV2 | AdditionalWaylandInterface::TextInputManagerV3));
0057 }
0058 
0059 void VirtualKeyboardDBusTest::init()
0060 {
0061     kwinApp()->inputMethod()->setEnabled(false);
0062 }
0063 
0064 void VirtualKeyboardDBusTest::cleanup()
0065 {
0066     Test::destroyWaylandConnection();
0067 }
0068 
0069 void VirtualKeyboardDBusTest::testEnabled()
0070 {
0071     VirtualKeyboardDBus dbus(KWin::kwinApp()->inputMethod());
0072     OrgKdeKwinVirtualKeyboardInterface iface(QStringLiteral("org.kde.kwin.testvirtualkeyboard"), QStringLiteral("/VirtualKeyboard"), QDBusConnection::sessionBus());
0073     QSignalSpy helperChangedSpy(&iface, &OrgKdeKwinVirtualKeyboardInterface::enabledChanged);
0074 
0075     QCOMPARE(dbus.isEnabled(), false);
0076     QCOMPARE(dbus.property("enabled").toBool(), false);
0077     QSignalSpy enabledChangedSpy(&dbus, &VirtualKeyboardDBus::enabledChanged);
0078 
0079     QVERIFY(iface.isValid());
0080     QCOMPARE(iface.enabled(), false);
0081 
0082     dbus.setEnabled(true);
0083     QCOMPARE(enabledChangedSpy.count(), 1);
0084     QVERIFY(helperChangedSpy.wait());
0085     QCOMPARE(helperChangedSpy.count(), 1);
0086     QCOMPARE(dbus.isEnabled(), true);
0087     QCOMPARE(dbus.property("enabled").toBool(), true);
0088     QCOMPARE(iface.enabled(), true);
0089 
0090     // setting again to enabled should not change anything
0091     dbus.setEnabled(true);
0092     QCOMPARE(enabledChangedSpy.count(), 1);
0093 
0094     // back to false
0095     dbus.setEnabled(false);
0096     QCOMPARE(enabledChangedSpy.count(), 2);
0097     QVERIFY(helperChangedSpy.wait());
0098     QCOMPARE(helperChangedSpy.count(), 2);
0099     QCOMPARE(dbus.isEnabled(), false);
0100     QCOMPARE(dbus.property("enabled").toBool(), false);
0101     QCOMPARE(iface.enabled(), false);
0102 }
0103 
0104 void VirtualKeyboardDBusTest::testRequestEnabled_data()
0105 {
0106     QTest::addColumn<QString>("method");
0107     QTest::addColumn<bool>("expectedResult");
0108 
0109     QTest::newRow("enable") << QStringLiteral("setEnabled") << true;
0110     QTest::newRow("disable") << QStringLiteral("setEnabled") << false;
0111 }
0112 
0113 void VirtualKeyboardDBusTest::testRequestEnabled()
0114 {
0115     QFETCH(QString, method);
0116     QFETCH(bool, expectedResult);
0117 
0118     VirtualKeyboardDBus dbus(KWin::kwinApp()->inputMethod());
0119     OrgKdeKwinVirtualKeyboardInterface iface(QStringLiteral("org.kde.kwin.testvirtualkeyboard"), QStringLiteral("/VirtualKeyboard"), QDBusConnection::sessionBus());
0120 
0121     iface.setEnabled(expectedResult);
0122     QTRY_COMPARE(iface.enabled(), expectedResult);
0123 }
0124 
0125 WAYLANDTEST_MAIN(VirtualKeyboardDBusTest)
0126 #include "test_virtualkeyboard_dbus.moc"