Changeset View
Changeset View
Standalone View
Standalone View
autotests/test_virtualkeyboard_dbus.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | KWin - the KDE window manager | ||||
3 | This file is part of the KDE project. | ||||
4 | | ||||
5 | Copyright (C) 2017 Martin Flöser <mgraesslin@kde.org> | ||||
6 | | ||||
7 | This program is free software; you can redistribute it and/or modify | ||||
8 | it under the terms of the GNU General Public License as published by | ||||
9 | the Free Software Foundation; either version 2 of the License, or | ||||
10 | (at your option) any later version. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | #include <QtTest/QTest> | ||||
21 | #include <QDBusConnection> | ||||
22 | #include <QDBusMessage> | ||||
23 | #include <QDBusPendingReply> | ||||
24 | #include <QSignalSpy> | ||||
25 | | ||||
26 | #include "../virtualkeyboard_dbus.h" | ||||
27 | | ||||
28 | using KWin::VirtualKeyboardDBus; | ||||
29 | | ||||
30 | class VirtualKeyboardDBusTest : public QObject | ||||
31 | { | ||||
32 | Q_OBJECT | ||||
33 | private Q_SLOTS: | ||||
34 | void initTestCase(); | ||||
35 | void testEnabled(); | ||||
36 | void testRequestEnabled_data(); | ||||
37 | void testRequestEnabled(); | ||||
38 | }; | ||||
39 | | ||||
40 | class DbusPropertyHelper : public QObject | ||||
41 | { | ||||
42 | Q_OBJECT | ||||
43 | public: | ||||
44 | DbusPropertyHelper() | ||||
45 | : QObject(nullptr) | ||||
46 | { | ||||
47 | QDBusConnection::sessionBus().connect( | ||||
48 | QStringLiteral("org.kde.kwin.testvirtualkeyboard"), | ||||
49 | QStringLiteral("/VirtualKeyboard"), | ||||
50 | QStringLiteral("org.kde.kwin.VirtualKeyboard"), | ||||
51 | QStringLiteral("enabledChanged"), | ||||
52 | this, | ||||
53 | SLOT(slotEnabledChanged())); | ||||
54 | } | ||||
55 | ~DbusPropertyHelper() = default; | ||||
56 | | ||||
57 | Q_SIGNALS: | ||||
58 | void enabledChanged(); | ||||
59 | | ||||
60 | private Q_SLOTS: | ||||
61 | void slotEnabledChanged() { | ||||
62 | emit enabledChanged(); | ||||
63 | } | ||||
64 | }; | ||||
65 | | ||||
66 | void VirtualKeyboardDBusTest::initTestCase() | ||||
67 | { | ||||
68 | QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.kwin.testvirtualkeyboard")); | ||||
69 | } | ||||
70 | | ||||
71 | void VirtualKeyboardDBusTest::testEnabled() | ||||
72 | { | ||||
73 | VirtualKeyboardDBus dbus; | ||||
74 | DbusPropertyHelper helper; | ||||
75 | QSignalSpy helperChangedSpy(&helper, &DbusPropertyHelper::enabledChanged); | ||||
76 | QVERIFY(helperChangedSpy.isValid()); | ||||
77 | | ||||
78 | QCOMPARE(dbus.isEnabled(), false); | ||||
79 | QCOMPARE(dbus.property("enabled").toBool(), false); | ||||
80 | QSignalSpy enabledChangedSpy(&dbus, &VirtualKeyboardDBus::enabledChanged); | ||||
81 | QVERIFY(enabledChangedSpy.isValid()); | ||||
82 | | ||||
83 | auto readProperty = [] (bool enabled) { | ||||
84 | const QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kwin.testvirtualkeyboard"), | ||||
85 | QStringLiteral("/VirtualKeyboard"), | ||||
86 | QStringLiteral("org.kde.kwin.VirtualKeyboard"), | ||||
87 | QStringLiteral("isEnabled")); | ||||
88 | const auto reply = QDBusConnection::sessionBus().call(message); | ||||
89 | QCOMPARE(reply.type(), QDBusMessage::ReplyMessage); | ||||
90 | QCOMPARE(reply.arguments().count(), 1); | ||||
91 | QCOMPARE(reply.arguments().first().toBool(), enabled); | ||||
92 | }; | ||||
93 | readProperty(false); | ||||
94 | | ||||
95 | dbus.setEnabled(true); | ||||
96 | QCOMPARE(enabledChangedSpy.count(), 1); | ||||
97 | QVERIFY(helperChangedSpy.wait()); | ||||
98 | QCOMPARE(helperChangedSpy.count(), 1); | ||||
99 | QCOMPARE(dbus.isEnabled(), true); | ||||
100 | QCOMPARE(dbus.property("enabled").toBool(), true); | ||||
101 | readProperty(true); | ||||
102 | | ||||
103 | // setting again to enabled should not change anything | ||||
104 | dbus.setEnabled(true); | ||||
105 | QCOMPARE(enabledChangedSpy.count(), 1); | ||||
106 | | ||||
107 | // back to false | ||||
108 | dbus.setEnabled(false); | ||||
109 | QCOMPARE(enabledChangedSpy.count(), 2); | ||||
110 | QVERIFY(helperChangedSpy.wait()); | ||||
111 | QCOMPARE(helperChangedSpy.count(), 2); | ||||
112 | QCOMPARE(dbus.isEnabled(), false); | ||||
113 | QCOMPARE(dbus.property("enabled").toBool(), false); | ||||
114 | readProperty(false); | ||||
115 | } | ||||
116 | | ||||
117 | void VirtualKeyboardDBusTest::testRequestEnabled_data() | ||||
118 | { | ||||
119 | QTest::addColumn<QString>("method"); | ||||
120 | QTest::addColumn<bool>("expectedResult"); | ||||
121 | | ||||
122 | QTest::newRow("enable") << QStringLiteral("enable") << true; | ||||
123 | QTest::newRow("disable") << QStringLiteral("disable") << false; | ||||
124 | } | ||||
125 | | ||||
126 | void VirtualKeyboardDBusTest::testRequestEnabled() | ||||
127 | { | ||||
128 | VirtualKeyboardDBus dbus; | ||||
129 | QSignalSpy activateRequestedSpy(&dbus, &VirtualKeyboardDBus::activateRequested); | ||||
130 | QVERIFY(activateRequestedSpy.isValid()); | ||||
131 | QFETCH(QString, method); | ||||
132 | const QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kwin.testvirtualkeyboard"), | ||||
133 | QStringLiteral("/VirtualKeyboard"), | ||||
134 | QStringLiteral("org.kde.kwin.VirtualKeyboard"), | ||||
135 | method); | ||||
136 | QDBusConnection::sessionBus().asyncCall(message); | ||||
137 | QTRY_COMPARE(activateRequestedSpy.count(), 1); | ||||
138 | QTEST(activateRequestedSpy.first().first().toBool(), "expectedResult"); | ||||
139 | } | ||||
140 | | ||||
141 | QTEST_GUILESS_MAIN(VirtualKeyboardDBusTest) | ||||
142 | #include "test_virtualkeyboard_dbus.moc" |