File indexing completed on 2025-01-26 05:09:25

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 "../kdedtestutils.h"
0021 #include "../propertyadaptormock.h"
0022 
0023 #include "common/deviceinformation.h"
0024 #include "common/tabletinfo.h"
0025 #include "common/tabletinformation.h"
0026 
0027 #include "kded/tabletbackend.h"
0028 #include "kded/xinputproperty.h"
0029 #include "kded/xsetwacomproperty.h"
0030 
0031 #include <QtTest>
0032 
0033 using namespace Wacom;
0034 
0035 /**
0036  * @file testtabletbackend.cpp
0037  *
0038  * @test UnitTest for ...
0039  */
0040 class TestTabletBackend : public QObject
0041 {
0042     Q_OBJECT
0043 
0044 private slots:
0045     void initTestCase();
0046     void testGetInformation();
0047     void testSetDeviceProfile();
0048     void testSetProfile();
0049     void testSetProperty();
0050     void cleanupTestCase();
0051 
0052 private:
0053     TabletInformation m_tabletInformation;
0054     TabletBackend *m_tabletBackend;
0055 
0056     PropertyAdaptorMock<XinputProperty> *m_eraserXinputAdaptor;
0057     PropertyAdaptorMock<XsetwacomProperty> *m_eraserXsetwacomAdaptor;
0058 
0059     PropertyAdaptorMock<XinputProperty> *m_padXinputAdaptor;
0060     PropertyAdaptorMock<XsetwacomProperty> *m_padXsetwacomAdaptor;
0061 
0062     PropertyAdaptorMock<XinputProperty> *m_stylusXinputAdaptor;
0063     PropertyAdaptorMock<XsetwacomProperty> *m_stylusXsetwacomAdaptor;
0064 };
0065 
0066 QTEST_MAIN(TestTabletBackend)
0067 
0068 void TestTabletBackend::initTestCase()
0069 {
0070     // init tablet information
0071     m_tabletInformation.set(TabletInfo::CompanyId, TabletInfo::CompanyId.key());
0072     m_tabletInformation.set(TabletInfo::CompanyName, TabletInfo::CompanyName.key());
0073     m_tabletInformation.set(TabletInfo::TabletId, TabletInfo::TabletId.key());
0074     m_tabletInformation.set(TabletInfo::TabletModel, TabletInfo::TabletModel.key());
0075     m_tabletInformation.set(TabletInfo::TabletName, TabletInfo::TabletName.key());
0076     m_tabletInformation.set(TabletInfo::NumPadButtons, QLatin1String("4"));
0077     m_tabletInformation.setAvailable(true);
0078 
0079     DeviceInformation eraserDevInfo(DeviceType::Eraser, QLatin1String("Eraser Device"));
0080     DeviceInformation padDevInfo(DeviceType::Eraser, QLatin1String("Pad Device"));
0081     DeviceInformation stylusDevInfo(DeviceType::Stylus, QLatin1String("Stylus Device"));
0082 
0083     m_tabletInformation.setDevice(eraserDevInfo);
0084     m_tabletInformation.setDevice(padDevInfo);
0085     m_tabletInformation.setDevice(stylusDevInfo);
0086 
0087     // init property adaptors
0088     m_eraserXinputAdaptor = new PropertyAdaptorMock<XinputProperty>();
0089     m_eraserXsetwacomAdaptor = new PropertyAdaptorMock<XsetwacomProperty>();
0090 
0091     m_padXinputAdaptor = new PropertyAdaptorMock<XinputProperty>();
0092     m_padXsetwacomAdaptor = new PropertyAdaptorMock<XsetwacomProperty>();
0093 
0094     m_stylusXinputAdaptor = new PropertyAdaptorMock<XinputProperty>();
0095     m_stylusXsetwacomAdaptor = new PropertyAdaptorMock<XsetwacomProperty>();
0096 
0097     // init tablet backend
0098     m_tabletBackend = new TabletBackend(m_tabletInformation);
0099     m_tabletBackend->addAdaptor(DeviceType::Eraser, m_eraserXsetwacomAdaptor);
0100     m_tabletBackend->addAdaptor(DeviceType::Eraser, m_eraserXinputAdaptor);
0101     m_tabletBackend->addAdaptor(DeviceType::Pad, m_padXinputAdaptor);
0102     m_tabletBackend->addAdaptor(DeviceType::Pad, m_padXsetwacomAdaptor);
0103     m_tabletBackend->addAdaptor(DeviceType::Stylus, m_stylusXsetwacomAdaptor);
0104     m_tabletBackend->addAdaptor(DeviceType::Stylus, m_stylusXinputAdaptor);
0105 }
0106 
0107 void TestTabletBackend::testGetInformation()
0108 {
0109     TabletInformation tabletInformation = m_tabletBackend->getInformation();
0110     KdedTestUtils::assertTabletInformation(m_tabletInformation, tabletInformation);
0111 }
0112 
0113 void TestTabletBackend::testSetDeviceProfile()
0114 {
0115     // cleanup
0116     m_eraserXinputAdaptor->m_properties.clear();
0117     m_eraserXsetwacomAdaptor->m_properties.clear();
0118 
0119     // create simple device profile
0120     DeviceProfile profile;
0121 
0122     profile.setProperty(Property::Button5, Property::Button5.key());
0123     profile.setProperty(Property::CursorAccelAdaptiveDeceleration, Property::CursorAccelAdaptiveDeceleration.key());
0124 
0125     m_tabletBackend->setProfile(DeviceType::Eraser, profile);
0126 
0127     // check values
0128     QVERIFY(m_eraserXsetwacomAdaptor->m_properties.contains(Property::Button5.key()));
0129     QVERIFY(!m_eraserXsetwacomAdaptor->m_properties.contains(Property::CursorAccelAdaptiveDeceleration.key()));
0130     QCOMPARE(Property::Button5.key(), m_eraserXsetwacomAdaptor->m_properties.value(Property::Button5.key()));
0131 
0132     QVERIFY(m_eraserXinputAdaptor->m_properties.contains(Property::CursorAccelAdaptiveDeceleration.key()));
0133     QVERIFY(!m_eraserXinputAdaptor->m_properties.contains(Property::Button5.key()));
0134     QCOMPARE(Property::CursorAccelAdaptiveDeceleration.key(), m_eraserXinputAdaptor->m_properties.value(Property::CursorAccelAdaptiveDeceleration.key()));
0135 }
0136 
0137 void TestTabletBackend::testSetProfile()
0138 {
0139     // cleanup
0140     m_eraserXinputAdaptor->m_properties.clear();
0141     m_eraserXsetwacomAdaptor->m_properties.clear();
0142     m_stylusXinputAdaptor->m_properties.clear();
0143     m_stylusXsetwacomAdaptor->m_properties.clear();
0144 
0145     // create test data
0146     TabletProfile tabletProfile;
0147     DeviceProfile eraserProfile;
0148     DeviceProfile stylusProfile;
0149 
0150     eraserProfile.setDeviceType(DeviceType::Eraser);
0151     eraserProfile.setProperty(Property::Button8, Property::Button8.key());
0152 
0153     stylusProfile.setDeviceType(DeviceType::Stylus);
0154     stylusProfile.setProperty(Property::CursorAccelVelocityScaling, Property::CursorAccelVelocityScaling.key());
0155 
0156     tabletProfile.setName(QLatin1String("MyProfile"));
0157     tabletProfile.setDevice(eraserProfile);
0158     tabletProfile.setDevice(stylusProfile);
0159 
0160     m_tabletBackend->setProfile(tabletProfile);
0161 
0162     // check values
0163     QVERIFY(m_eraserXsetwacomAdaptor->m_properties.contains(Property::Button8.key()));
0164     QVERIFY(!m_eraserXinputAdaptor->m_properties.contains(Property::Button8.key()));
0165     QVERIFY(!m_stylusXinputAdaptor->m_properties.contains(Property::Button8.key()));
0166     QVERIFY(!m_stylusXsetwacomAdaptor->m_properties.contains(Property::Button8.key()));
0167     QCOMPARE(Property::Button8.key(), m_eraserXsetwacomAdaptor->m_properties.value(Property::Button8.key()));
0168 
0169     QVERIFY(m_stylusXinputAdaptor->m_properties.contains(Property::CursorAccelVelocityScaling.key()));
0170     QVERIFY(!m_stylusXsetwacomAdaptor->m_properties.contains(Property::CursorAccelVelocityScaling.key()));
0171     QVERIFY(!m_eraserXinputAdaptor->m_properties.contains(Property::CursorAccelVelocityScaling.key()));
0172     QVERIFY(!m_eraserXsetwacomAdaptor->m_properties.contains(Property::CursorAccelVelocityScaling.key()));
0173     QCOMPARE(Property::CursorAccelVelocityScaling.key(), m_stylusXinputAdaptor->m_properties.value(Property::CursorAccelVelocityScaling.key()));
0174 }
0175 
0176 void TestTabletBackend::testSetProperty()
0177 {
0178     // cleanup
0179     m_eraserXinputAdaptor->m_properties.clear();
0180     m_eraserXsetwacomAdaptor->m_properties.clear();
0181     m_padXinputAdaptor->m_properties.clear();
0182     m_padXsetwacomAdaptor->m_properties.clear();
0183     m_stylusXinputAdaptor->m_properties.clear();
0184     m_stylusXsetwacomAdaptor->m_properties.clear();
0185 
0186     // setting a property on an unsupported device type should fail
0187     QVERIFY(!m_tabletBackend->setProperty(DeviceType::Touch, Property::Gesture, Property::Gesture.key()));
0188 
0189     // these properties should only be set on the XsetwacomAdaptorMock as they are unsupported by Xinput
0190     QVERIFY(m_tabletBackend->setProperty(DeviceType::Eraser, Property::Mode, Property::Mode.key()));
0191     QVERIFY(m_tabletBackend->setProperty(DeviceType::Stylus, Property::Mode, Property::Mode.key()));
0192 
0193     // make sure only the xsetwacom adaptor mocks contain the property which was just set
0194     QVERIFY(!m_eraserXinputAdaptor->m_properties.contains(Property::Mode.key()));
0195     QVERIFY(m_eraserXsetwacomAdaptor->m_properties.contains(Property::Mode.key()));
0196     QCOMPARE(Property::Mode.key(), m_eraserXsetwacomAdaptor->m_properties.value(Property::Mode.key()));
0197 
0198     QVERIFY(!m_stylusXinputAdaptor->m_properties.contains(Property::Mode.key()));
0199     QVERIFY(m_stylusXsetwacomAdaptor->m_properties.contains(Property::Mode.key()));
0200     QCOMPARE(Property::Mode.key(), m_stylusXsetwacomAdaptor->m_properties.value(Property::Mode.key()));
0201 
0202     QVERIFY(!m_padXinputAdaptor->m_properties.contains(Property::Mode.key()));
0203     QVERIFY(!m_padXsetwacomAdaptor->m_properties.contains(Property::Mode.key()));
0204 
0205     // set a value on both adaptors
0206     QVERIFY(m_tabletBackend->setProperty(DeviceType::Pad, Property::Button1, Property::Button1.key()));
0207     QVERIFY(m_tabletBackend->setProperty(DeviceType::Pad, Property::CursorAccelProfile, Property::CursorAccelProfile.key()));
0208 
0209     // make sure each adaptor only contains the property which belongs to him
0210     QVERIFY(!m_padXinputAdaptor->m_properties.contains(Property::Button1.key()));
0211     QVERIFY(!m_padXsetwacomAdaptor->m_properties.contains(Property::CursorAccelProfile.key()));
0212     QVERIFY(m_padXinputAdaptor->m_properties.contains(Property::CursorAccelProfile.key()));
0213     QVERIFY(m_padXsetwacomAdaptor->m_properties.contains(Property::Button1.key()));
0214 
0215     QCOMPARE(Property::CursorAccelProfile.key(), m_padXinputAdaptor->m_properties.value(Property::CursorAccelProfile.key()));
0216     QCOMPARE(Property::Button1.key(), m_padXsetwacomAdaptor->m_properties.value(Property::Button1.key()));
0217 
0218     // make sure the values we just set can be get again
0219     QCOMPARE(Property::Mode.key(), m_tabletBackend->getProperty(DeviceType::Eraser, Property::Mode));
0220     QCOMPARE(Property::Mode.key(), m_tabletBackend->getProperty(DeviceType::Stylus, Property::Mode));
0221     QCOMPARE(Property::Button1.key(), m_tabletBackend->getProperty(DeviceType::Pad, Property::Button1));
0222     QCOMPARE(Property::CursorAccelProfile.key(), m_tabletBackend->getProperty(DeviceType::Pad, Property::CursorAccelProfile));
0223 }
0224 
0225 void TestTabletBackend::cleanupTestCase()
0226 {
0227     delete m_tabletBackend;
0228 }
0229 
0230 #include "testtabletbackend.moc"