| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 Canonical, Ltd. and/or its subsidiary(-ies). |
| 4 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 5 | ** Contact: http://www.qt-project.org/legal |
| 6 | ** |
| 7 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include <QtTest/qtest.h> |
| 31 | #include <QSignalSpy> |
| 32 | |
| 33 | #include "qinputinfo.h" |
| 34 | |
| 35 | QT_USE_NAMESPACE |
| 36 | |
| 37 | class tst_QInputDeviceInfo : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | |
| 41 | private slots: |
| 42 | void initTestCase(); |
| 43 | void tst_deviceMap(); |
| 44 | void tst_deviceFilter(); |
| 45 | void tst_signals(); |
| 46 | }; |
| 47 | |
| 48 | void tst_QInputDeviceInfo::initTestCase() |
| 49 | { |
| 50 | qRegisterMetaType<QInputDevice::InputType>(); |
| 51 | qRegisterMetaType<QInputDevice::InputTypeFlags>(); |
| 52 | } |
| 53 | |
| 54 | void tst_QInputDeviceInfo::tst_deviceMap() |
| 55 | { |
| 56 | QInputInfoManager manager; |
| 57 | QSignalSpy spy(&manager, SIGNAL(ready())); |
| 58 | QTRY_COMPARE(spy.count(), 1); |
| 59 | |
| 60 | QMap <QString, QInputDevice *> map = manager.deviceMap(); |
| 61 | QVERIFY(map.count() > 0); |
| 62 | QMapIterator<QString, QInputDevice *> i(map); |
| 63 | while (i.hasNext()) { |
| 64 | i.next(); |
| 65 | QVERIFY(!i.value()->name().isEmpty()); |
| 66 | QVERIFY(!i.value()->identifier().isEmpty()); |
| 67 | QVERIFY(!i.value()->properties().value("name").toString().isEmpty()); |
| 68 | QVERIFY(!i.value()->properties().value("identifier").toString().isEmpty()); |
| 69 | |
| 70 | QVERIFY(i.value()->name() == i.value()->properties().value("name")); |
| 71 | QVERIFY(i.value()->identifier() == i.value()->properties().value("identifier")); |
| 72 | QVERIFY(!i.value()->types().testFlag(QInputDevice::UnknownType)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void tst_QInputDeviceInfo::tst_deviceFilter() |
| 77 | { |
| 78 | QInputInfoManager manager; |
| 79 | QVERIFY(manager.filter().testFlag(QInputDevice::Keyboard) |
| 80 | && manager.filter().testFlag(QInputDevice::Button) |
| 81 | && manager.filter().testFlag(QInputDevice::Mouse) |
| 82 | && manager.filter().testFlag(QInputDevice::TouchPad) |
| 83 | && manager.filter().testFlag(QInputDevice::TouchScreen) |
| 84 | && manager.filter().testFlag(QInputDevice::Switch)); |
| 85 | int mouseCount = manager.count(QInputDevice::Mouse); |
| 86 | int keyboardCount = manager.count(QInputDevice::Keyboard); |
| 87 | |
| 88 | manager.setFilter(QInputDevice::Keyboard); |
| 89 | QVERIFY(manager.filter().testFlag(QInputDevice::Keyboard)); |
| 90 | |
| 91 | QVERIFY(!manager.filter().testFlag(QInputDevice::Button)); |
| 92 | QVERIFY(!manager.filter().testFlag(QInputDevice::Mouse)); |
| 93 | QVERIFY(!manager.filter().testFlag(QInputDevice::TouchPad)); |
| 94 | QVERIFY(!manager.filter().testFlag(QInputDevice::TouchScreen)); |
| 95 | QVERIFY(!manager.filter().testFlag(QInputDevice::Switch)); |
| 96 | |
| 97 | QSignalSpy spy(&manager, SIGNAL(filterChanged(QInputDevice::InputTypeFlags))); |
| 98 | QVERIFY(spy.isValid()); |
| 99 | manager.setFilter(QInputDevice::Mouse); |
| 100 | QTRY_COMPARE(spy.count(), 1); |
| 101 | QCOMPARE(manager.filter(),QInputDevice::Mouse); |
| 102 | QList<QVariant> arguments = spy.takeFirst(); |
| 103 | |
| 104 | manager.setFilter(QInputDevice::Mouse | QInputDevice::Keyboard); |
| 105 | QTRY_COMPARE(spy.count(), 1); |
| 106 | |
| 107 | QVERIFY(manager.count() == mouseCount + keyboardCount); |
| 108 | QVERIFY(manager.count(QInputDevice::Mouse) == mouseCount); |
| 109 | |
| 110 | manager.setFilter(QInputDevice::TouchScreen); |
| 111 | QCOMPARE(manager.count(QInputDevice::Mouse), 1); |
| 112 | |
| 113 | |
| 114 | } |
| 115 | |
| 116 | void tst_QInputDeviceInfo::tst_signals() |
| 117 | { |
| 118 | QInputInfoManager manager; |
| 119 | |
| 120 | QSignalSpy spy(&manager, SIGNAL(filterChanged(QInputDevice::InputTypeFlags))); |
| 121 | QVERIFY(spy.isValid()); |
| 122 | |
| 123 | QSignalSpy readyspy(&manager, SIGNAL(ready())); |
| 124 | QVERIFY(readyspy.isValid()); |
| 125 | |
| 126 | QSignalSpy deviceAddedSpy(&manager, SIGNAL(deviceAdded(QInputDevice *))); |
| 127 | QVERIFY(deviceAddedSpy.isValid()); |
| 128 | |
| 129 | QSignalSpy deviceRemovedSpy(&manager, SIGNAL(deviceRemoved(const QString &))); |
| 130 | QVERIFY(deviceRemovedSpy.isValid()); |
| 131 | |
| 132 | QSignalSpy countChangedSpy(&manager, SIGNAL(countChanged(int))); |
| 133 | QVERIFY(countChangedSpy.isValid()); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | QTEST_MAIN(tst_QInputDeviceInfo) |
| 138 | #include "tst_qinputdeviceinfo.moc" |