File indexing completed on 2024-12-29 05:08:32
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 "common/property.h" 0021 #include "common/propertyset.h" 0022 0023 #include <QtTest> 0024 0025 using namespace Wacom; 0026 0027 /** 0028 * @file testpropertyset.cpp 0029 * 0030 * @test UnitTest for the properties 0031 */ 0032 class TestPropertySet : public QObject 0033 { 0034 Q_OBJECT 0035 0036 private slots: 0037 void testCompare(); 0038 void testConstructor(); 0039 void testFind(); 0040 void testId(); 0041 void testIds(); 0042 void testIterator(); 0043 void testKey(); 0044 void testKeys(); 0045 void testList(); 0046 void testMap(); 0047 void testOperator(); 0048 void testSize(); 0049 }; 0050 0051 QTEST_MAIN(TestPropertySet) 0052 0053 // forward declarations & typedefs 0054 class PropertySetTest; 0055 typedef PropertySet<PropertySetTest> PropertySetTestTemplateSpecialization; 0056 0057 /* 0058 * A helper class required for this unit test. 0059 */ 0060 class PropertySetTest : public PropertySetTestTemplateSpecialization 0061 { 0062 private: 0063 explicit PropertySetTest(const Property &id, const QString &key) 0064 : PropertySetTestTemplateSpecialization(this, id, key) 0065 { 0066 } 0067 0068 public: 0069 static const PropertySetTest VAL01_PRIO10; 0070 static const PropertySetTest VAL02_PRIO10; 0071 static const PropertySetTest VAL03_PRIO10; 0072 static const PropertySetTest VAL01_PRIO50; 0073 static const PropertySetTest VAL01_PRIO99; 0074 static const PropertySetTest VAL02_PRIO99; 0075 }; 0076 0077 // template specialization 0078 template<> 0079 PropertySetTestTemplateSpecialization::PropertySetTemplateSpecialization::Container 0080 PropertySetTestTemplateSpecialization::PropertySetTemplateSpecialization::instances = 0081 PropertySetTestTemplateSpecialization::PropertySetTemplateSpecialization::Container(); 0082 0083 // PropertySetTest instances - the order defined here should be kept by the comparator 0084 const PropertySetTest PropertySetTest::VAL01_PRIO50(Property::Touch, QLatin1String("VAL01_PRIO50")); 0085 const PropertySetTest PropertySetTest::VAL03_PRIO10(Property::Gesture, QLatin1String("VAL03_PRIO10")); 0086 const PropertySetTest PropertySetTest::VAL02_PRIO10(Property::AbsWheelUp, QLatin1String("VAL02_PRIO10")); 0087 const PropertySetTest PropertySetTest::VAL01_PRIO10(Property::AbsWheelDown, QLatin1String("VAL01_PRIO10")); 0088 const PropertySetTest PropertySetTest::VAL01_PRIO99(Property::StripLeftDown, QLatin1String("VAL01_PRIO99")); 0089 const PropertySetTest PropertySetTest::VAL02_PRIO99(Property::StripLeftUp, QLatin1String("VAL02_PRIO99")); 0090 0091 /* 0092 * UNIT TESTS 0093 */ 0094 0095 void TestPropertySet::testCompare() 0096 { 0097 // make sure the order is the same as the definition order 0098 PropertySetTest::const_iterator iter = PropertySetTest::begin(); 0099 QCOMPARE(iter++->key(), QLatin1String("VAL01_PRIO50")); 0100 QCOMPARE(iter++->key(), QLatin1String("VAL03_PRIO10")); 0101 QCOMPARE(iter++->key(), QLatin1String("VAL02_PRIO10")); 0102 QCOMPARE(iter++->key(), QLatin1String("VAL01_PRIO10")); 0103 QCOMPARE(iter++->key(), QLatin1String("VAL01_PRIO99")); 0104 QCOMPARE(iter++->key(), QLatin1String("VAL02_PRIO99")); 0105 } 0106 0107 void TestPropertySet::testConstructor() 0108 { 0109 // copy constructor 0110 PropertySetTest test(PropertySetTest::VAL01_PRIO10); 0111 QVERIFY(test == PropertySetTest::VAL01_PRIO10); 0112 } 0113 0114 void TestPropertySet::testFind() 0115 { 0116 // find by key 0117 QVERIFY(PropertySetTest::find(QLatin1String("NON_EXISTANT")) == NULL); 0118 0119 const PropertySetTest *findKey = PropertySetTest::find(QLatin1String("VAL01_PRIO50")); 0120 QVERIFY(findKey != NULL); 0121 QVERIFY(*findKey == PropertySetTest::VAL01_PRIO50); 0122 0123 const PropertySetTest *findKeyIgnoreCase = PropertySetTest::find(QLatin1String("vAl01_PriO50")); 0124 QVERIFY(findKeyIgnoreCase != NULL); 0125 QVERIFY(*findKeyIgnoreCase == PropertySetTest::VAL01_PRIO50); 0126 } 0127 0128 void TestPropertySet::testId() 0129 { 0130 QVERIFY(PropertySetTest::VAL01_PRIO50.id() == Property::Touch); 0131 QVERIFY(PropertySetTest::VAL03_PRIO10.id() == Property::Gesture); 0132 QVERIFY(PropertySetTest::VAL02_PRIO10.id() == Property::AbsWheelUp); 0133 QVERIFY(PropertySetTest::VAL01_PRIO10.id() == Property::AbsWheelDown); 0134 QVERIFY(PropertySetTest::VAL01_PRIO99.id() == Property::StripLeftDown); 0135 QVERIFY(PropertySetTest::VAL02_PRIO99.id() == Property::StripLeftUp); 0136 } 0137 0138 void TestPropertySet::testIds() 0139 { 0140 QList<Property> ids = PropertySetTest::ids(); 0141 0142 QList<Property>::ConstIterator iter = ids.constBegin(); 0143 QVERIFY(*(iter++) == Property::Touch); 0144 QVERIFY(*(iter++) == Property::Gesture); 0145 QVERIFY(*(iter++) == Property::AbsWheelUp); 0146 QVERIFY(*(iter++) == Property::AbsWheelDown); 0147 QVERIFY(*(iter++) == Property::StripLeftDown); 0148 QVERIFY(*(iter++) == Property::StripLeftUp); 0149 } 0150 0151 void TestPropertySet::testIterator() 0152 { 0153 PropertySetTest::const_iterator begin = PropertySetTest::begin(); 0154 PropertySetTest::const_iterator end = PropertySetTest::end(); 0155 0156 PropertySetTest::const_iterator iter, second; 0157 0158 // begin(), operator*, operator-> 0159 QCOMPARE(begin->key(), QLatin1String("VAL01_PRIO50")); 0160 QCOMPARE((*begin).key(), PropertySetTest::VAL01_PRIO50.key()); 0161 QVERIFY(*begin == PropertySetTest::VAL01_PRIO50); 0162 QVERIFY(&(*begin) == &PropertySetTest::VAL01_PRIO50); 0163 0164 // operator== 0165 QVERIFY(begin == PropertySetTest::begin()); 0166 QVERIFY(end == PropertySetTest::end()); 0167 0168 // operator!= 0169 QVERIFY(begin != PropertySetTest::end()); 0170 QVERIFY(end != PropertySetTest::begin()); 0171 0172 // operator= 0173 iter = begin; 0174 QVERIFY(iter == iter); 0175 QVERIFY(iter == begin); 0176 QVERIFY(iter == PropertySetTest::begin()); 0177 0178 // operator++ 0179 second = begin; 0180 ++second; 0181 QCOMPARE(second->key(), QLatin1String("VAL03_PRIO10")); 0182 0183 iter = begin; 0184 QVERIFY(iter++ == begin); 0185 QVERIFY(iter == second); 0186 0187 // operator++(int) 0188 iter = begin; 0189 QVERIFY(++iter == second); 0190 0191 // operator-- 0192 iter = second; 0193 QVERIFY(iter-- == second); 0194 QVERIFY(iter == begin); 0195 0196 // operator--(int) 0197 iter = second; 0198 QVERIFY(--iter == begin); 0199 0200 // end() 0201 iter = end; 0202 --iter; 0203 QCOMPARE(iter->key(), QLatin1String("VAL02_PRIO99")); 0204 } 0205 0206 void TestPropertySet::testKey() 0207 { 0208 QCOMPARE(PropertySetTest::VAL01_PRIO50.key(), QLatin1String("VAL01_PRIO50")); 0209 QCOMPARE(PropertySetTest::VAL03_PRIO10.key(), QLatin1String("VAL03_PRIO10")); 0210 QCOMPARE(PropertySetTest::VAL02_PRIO10.key(), QLatin1String("VAL02_PRIO10")); 0211 QCOMPARE(PropertySetTest::VAL01_PRIO10.key(), QLatin1String("VAL01_PRIO10")); 0212 QCOMPARE(PropertySetTest::VAL01_PRIO99.key(), QLatin1String("VAL01_PRIO99")); 0213 QCOMPARE(PropertySetTest::VAL02_PRIO99.key(), QLatin1String("VAL02_PRIO99")); 0214 } 0215 0216 void TestPropertySet::testKeys() 0217 { 0218 QStringList keys = PropertySetTest::keys(); 0219 0220 QStringList::ConstIterator iter = keys.constBegin(); 0221 QCOMPARE(*(iter++), QLatin1String("VAL01_PRIO50")); 0222 QCOMPARE(*(iter++), QLatin1String("VAL03_PRIO10")); 0223 QCOMPARE(*(iter++), QLatin1String("VAL02_PRIO10")); 0224 QCOMPARE(*(iter++), QLatin1String("VAL01_PRIO10")); 0225 QCOMPARE(*(iter++), QLatin1String("VAL01_PRIO99")); 0226 QCOMPARE(*(iter++), QLatin1String("VAL02_PRIO99")); 0227 } 0228 0229 void TestPropertySet::testList() 0230 { 0231 QList<PropertySetTest> list = PropertySetTest::list(); 0232 QList<PropertySetTest>::ConstIterator iter = list.constBegin(); 0233 0234 QVERIFY(*(iter++) == PropertySetTest::VAL01_PRIO50); 0235 QVERIFY(*(iter++) == PropertySetTest::VAL03_PRIO10); 0236 QVERIFY(*(iter++) == PropertySetTest::VAL02_PRIO10); 0237 QVERIFY(*(iter++) == PropertySetTest::VAL01_PRIO10); 0238 QVERIFY(*(iter++) == PropertySetTest::VAL01_PRIO99); 0239 QVERIFY(*(iter++) == PropertySetTest::VAL02_PRIO99); 0240 } 0241 0242 void TestPropertySet::testMap() 0243 { 0244 QVERIFY(PropertySetTest::map(Property::Button6) == NULL); 0245 const PropertySetTest *findId = PropertySetTest::map(Property::Touch); 0246 QVERIFY(findId != NULL); 0247 QVERIFY(*findId == PropertySetTest::VAL01_PRIO50); 0248 } 0249 0250 void TestPropertySet::testOperator() 0251 { 0252 // operator== 0253 QVERIFY(PropertySetTest::VAL01_PRIO10 == PropertySetTest::VAL01_PRIO10); 0254 QVERIFY(PropertySetTest::VAL02_PRIO10 == PropertySetTest::VAL02_PRIO10); 0255 0256 // operator!= 0257 QVERIFY(PropertySetTest::VAL01_PRIO10 != PropertySetTest::VAL02_PRIO10); 0258 0259 // operator= 0260 PropertySetTest test = PropertySetTest::VAL01_PRIO10; 0261 QVERIFY(test == PropertySetTest::VAL01_PRIO10); 0262 } 0263 0264 void TestPropertySet::testSize() 0265 { 0266 int count = 6; 0267 QCOMPARE((int)PropertySetTest::size(), count); 0268 QCOMPARE(PropertySetTest::keys().size(), count); 0269 QCOMPARE(PropertySetTest::list().size(), count); 0270 } 0271 0272 #include "testpropertyset.moc"