File indexing completed on 2025-02-23 05:12:30
0001 /* 0002 * This file is part of the KDE wacomtablet project. For copyright 0003 * information and license terms see the AUTHORS and COPYING files 0004 * in the top-level directory of this distribution. 0005 * 0006 * This program is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * This program is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "../tablethandlermock.h" 0021 0022 #include "kded/dbustabletservice.h" 0023 0024 #include "common/dbustabletinterface.h" 0025 #include "common/tabletinformation.h" 0026 0027 #include <QtTest> 0028 0029 using namespace Wacom; 0030 0031 /** 0032 * @file testdbustabletservice.cpp 0033 * 0034 * @test UnitTest for ... 0035 */ 0036 class TestDBusTabletService : public QObject 0037 { 0038 Q_OBJECT 0039 0040 public: 0041 void assertTabletInformation(const TabletInformation &expectedInformation) const; 0042 0043 public slots: 0044 void onProfileChanged(const QString &TabletId, const QString &profile); 0045 void onTabletAdded(const QString &TabletId); 0046 void onTabletRemoved(const QString &TabletId); 0047 0048 private slots: 0049 //! Run once before all tests. 0050 void initTestCase(); 0051 0052 void testListProfiles(); 0053 void testOnTabletAdded(); 0054 void testOnTabletRemoved(); 0055 void testSetProfile(); 0056 void testSetProperty(); 0057 0058 //! Run once after all tests. 0059 void cleanupTestCase(); 0060 0061 private: 0062 TabletHandlerMock m_tabletHandlerMock; 0063 DBusTabletService *m_tabletService; 0064 0065 QString m_profileWasChangedTo; 0066 bool m_tabletWasAdded; 0067 bool m_tabletWasRemoved; 0068 }; 0069 0070 QTEST_MAIN(TestDBusTabletService) 0071 0072 void TestDBusTabletService::assertTabletInformation(const TabletInformation &expectedInformation) const 0073 { 0074 QDBusReply<QString> actualString; 0075 QDBusReply<bool> actualBool; 0076 0077 // make sure the device list is equal 0078 QStringList expectedDeviceList = expectedInformation.getDeviceList(); 0079 QDBusReply<QStringList> actualDeviceList = DBusTabletInterface::instance().getDeviceList(QLatin1String("TabletId")); 0080 QVERIFY(actualDeviceList.isValid()); 0081 0082 for (int i = 0; i < expectedDeviceList.size(); ++i) { 0083 QCOMPARE(actualDeviceList.value().at(i), expectedDeviceList.at(i)); 0084 } 0085 0086 // make sure the devices are equal 0087 foreach (const DeviceType &type, DeviceType::list()) { 0088 actualString = DBusTabletInterface::instance().getDeviceName(QLatin1String("TabletId"), type.key()); 0089 QVERIFY(actualString.isValid()); 0090 QCOMPARE(actualString.value(), expectedInformation.getDeviceName(type)); 0091 } 0092 0093 // compare tablet information 0094 foreach (const TabletInfo &info, TabletInfo::list()) { 0095 actualString = DBusTabletInterface::instance().getInformation(QLatin1String("TabletId"), info.key()); 0096 QVERIFY(actualString.isValid()); 0097 QCOMPARE(actualString.value(), expectedInformation.get(info)); 0098 } 0099 0100 // check pad buttons 0101 actualBool = DBusTabletInterface::instance().hasPadButtons(QLatin1String("TabletId")); 0102 QVERIFY(actualBool.isValid()); 0103 QVERIFY(expectedInformation.hasButtons() == actualBool.value()); 0104 0105 // check availability 0106 actualBool = DBusTabletInterface::instance().isAvailable(QLatin1String("TabletId")); 0107 QVERIFY(actualBool.isValid()); 0108 QVERIFY(expectedInformation.isAvailable() == actualBool.value()); 0109 } 0110 0111 void TestDBusTabletService::onProfileChanged(const QString &TabletId, const QString &profile) 0112 { 0113 Q_UNUSED(TabletId) 0114 m_profileWasChangedTo = profile; 0115 } 0116 0117 void TestDBusTabletService::onTabletAdded(const QString &TabletId) 0118 { 0119 Q_UNUSED(TabletId) 0120 m_tabletWasAdded = true; 0121 } 0122 0123 void TestDBusTabletService::onTabletRemoved(const QString &TabletId) 0124 { 0125 Q_UNUSED(TabletId) 0126 m_tabletWasRemoved = true; 0127 } 0128 0129 void TestDBusTabletService::initTestCase() 0130 { 0131 m_tabletService = nullptr; 0132 m_profileWasChangedTo.clear(); 0133 m_tabletWasAdded = false; 0134 m_tabletWasRemoved = false; 0135 0136 // make sure the D-Bus service is not already registered 0137 DBusTabletInterface::instance().resetInterface(); 0138 0139 if (DBusTabletInterface::instance().isValid()) { 0140 QSKIP("D-Bus service already running! Please shut it down to run this test!", SkipAll); 0141 return; 0142 } 0143 0144 // register D-Bus service 0145 m_tabletService = new DBusTabletService(m_tabletHandlerMock); 0146 0147 // reset D-Bus client 0148 DBusTabletInterface::instance().resetInterface(); 0149 ; 0150 0151 // connect tablet service to us 0152 connect(m_tabletService, &DBusTabletService::profileChanged, this, &TestDBusTabletService::onProfileChanged); 0153 connect(m_tabletService, &DBusTabletService::tabletAdded, this, &TestDBusTabletService::onTabletAdded); 0154 connect(m_tabletService, &DBusTabletService::tabletRemoved, this, &TestDBusTabletService::onTabletRemoved); 0155 0156 // connect tablet handler to tablet service 0157 connect(&m_tabletHandlerMock, &TabletHandlerMock::profileChanged, m_tabletService, &DBusTabletService::onProfileChanged); 0158 connect(&m_tabletHandlerMock, &TabletHandlerMock::tabletAdded, m_tabletService, &DBusTabletService::onTabletAdded); 0159 connect(&m_tabletHandlerMock, &TabletHandlerMock::tabletRemoved, m_tabletService, &DBusTabletService::onTabletRemoved); 0160 } 0161 0162 void TestDBusTabletService::cleanupTestCase() 0163 { 0164 if (m_tabletService) { 0165 delete m_tabletService; 0166 m_tabletService = nullptr; 0167 } 0168 } 0169 0170 void TestDBusTabletService::testListProfiles() 0171 { 0172 m_tabletHandlerMock.m_profiles.clear(); 0173 0174 QDBusReply<QStringList> profileList = DBusTabletInterface::instance().listProfiles(QLatin1String("TabletId")); 0175 QVERIFY(profileList.isValid()); 0176 QVERIFY(profileList.value().isEmpty()); 0177 0178 m_tabletHandlerMock.m_profiles.append(QLatin1String("Test Profile 1")); 0179 m_tabletHandlerMock.m_profiles.append(QLatin1String("Test Profile 2")); 0180 0181 profileList = DBusTabletInterface::instance().listProfiles(QLatin1String("TabletId")); 0182 0183 QVERIFY(profileList.isValid()); 0184 QCOMPARE(QLatin1String("Test Profile 1"), profileList.value().at(0)); 0185 QCOMPARE(QLatin1String("Test Profile 2"), profileList.value().at(1)); 0186 } 0187 0188 void TestDBusTabletService::testOnTabletAdded() 0189 { 0190 TabletInformation expectedInformation; 0191 0192 foreach (const TabletInfo &tabletInfo, TabletInfo::list()) { 0193 expectedInformation.set(tabletInfo, tabletInfo.key()); 0194 } 0195 0196 expectedInformation.setAvailable(false); // this should be set to true automatically 0197 0198 m_tabletWasAdded = false; 0199 m_tabletHandlerMock.emitTabletAdded(expectedInformation); 0200 0201 QVERIFY(m_tabletWasAdded); 0202 0203 expectedInformation.setAvailable(true); 0204 assertTabletInformation(expectedInformation); 0205 0206 QDBusReply<bool> isAvail = DBusTabletInterface::instance().isAvailable(QLatin1String("TabletId")); 0207 QVERIFY(isAvail.isValid()); 0208 QVERIFY(isAvail.value()); 0209 } 0210 0211 void TestDBusTabletService::testOnTabletRemoved() 0212 { 0213 TabletInformation expectedInformation; 0214 expectedInformation.setAvailable(false); 0215 0216 m_tabletWasRemoved = false; 0217 m_tabletHandlerMock.emitTabletRemoved(QLatin1String("TabletId")); 0218 assertTabletInformation(expectedInformation); 0219 QVERIFY(m_tabletWasRemoved); 0220 } 0221 0222 void TestDBusTabletService::testSetProfile() 0223 { 0224 QString expectedProfile; 0225 QDBusReply<QString> actualProfile; 0226 0227 // set new profile and make sure a signal was emitted by D-Bus 0228 expectedProfile = QLatin1String("Test Profile"); 0229 DBusTabletInterface::instance().setProfile(QLatin1String("TabletId"), expectedProfile); 0230 QCOMPARE(m_tabletHandlerMock.m_profile, expectedProfile); 0231 QCOMPARE(m_profileWasChangedTo, expectedProfile); 0232 0233 // the new profile should also be returned by getProfile 0234 actualProfile = DBusTabletInterface::instance().getProfile(QLatin1String("TabletId")); 0235 QVERIFY(actualProfile.isValid()); 0236 QCOMPARE(actualProfile.value(), expectedProfile); 0237 0238 // when the tablet is removed, the profile should be reset 0239 // however the tablet handler should be unchanged and no signal should be emitted by D-Bus 0240 m_tabletHandlerMock.emitTabletRemoved(QLatin1String("TabletId")); 0241 actualProfile = DBusTabletInterface::instance().getProfile(QLatin1String("TabletId")); 0242 QVERIFY(actualProfile.isValid()); 0243 QVERIFY(actualProfile.value().isEmpty()); 0244 QCOMPARE(m_tabletHandlerMock.m_profile, expectedProfile); 0245 QCOMPARE(m_profileWasChangedTo, expectedProfile); 0246 0247 // set another profile 0248 expectedProfile = QLatin1String("New Profile"); 0249 DBusTabletInterface::instance().setProfile(QLatin1String("TabletId"), expectedProfile); 0250 QCOMPARE(m_tabletHandlerMock.m_profile, expectedProfile); 0251 QCOMPARE(m_profileWasChangedTo, expectedProfile); 0252 0253 // the new profile should also be returned by getProfile 0254 actualProfile = DBusTabletInterface::instance().getProfile(QLatin1String("TabletId")); 0255 QVERIFY(actualProfile.isValid()); 0256 QCOMPARE(actualProfile.value(), expectedProfile); 0257 0258 // clear the profile manually 0259 expectedProfile.clear(); 0260 DBusTabletInterface::instance().setProfile(QLatin1String("TabletId"), expectedProfile); 0261 QCOMPARE(m_tabletHandlerMock.m_profile, expectedProfile); 0262 QCOMPARE(m_profileWasChangedTo, expectedProfile); 0263 0264 // the new profile should also be returned by getProfile 0265 actualProfile = DBusTabletInterface::instance().getProfile(QLatin1String("TabletId")); 0266 QVERIFY(actualProfile.isValid()); 0267 QCOMPARE(actualProfile.value(), expectedProfile); 0268 } 0269 0270 void TestDBusTabletService::testSetProperty() 0271 { 0272 DeviceType expectedDevice = DeviceType::Stylus; 0273 Property expectedProperty = Property::Button1; 0274 QString expectedValue = QLatin1String("My Value"); 0275 0276 // set the property 0277 DBusTabletInterface::instance().setProperty(QLatin1String("TabletId"), expectedDevice.key(), expectedProperty.key(), expectedValue); 0278 0279 QCOMPARE(m_tabletHandlerMock.m_deviceType, expectedDevice.key()); 0280 QCOMPARE(m_tabletHandlerMock.m_property, expectedProperty.key()); 0281 QCOMPARE(m_tabletHandlerMock.m_propertyValue, expectedValue); 0282 0283 // try to get the property 0284 QDBusReply<QString> actualValue = DBusTabletInterface::instance().getProperty(QLatin1String("TabletId"), expectedDevice.key(), expectedProperty.key()); 0285 0286 QVERIFY(actualValue.isValid()); 0287 QCOMPARE(actualValue.value(), expectedValue); 0288 } 0289 0290 #include "testdbustabletservice.moc"