| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtFeedback module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | //TESTED_COMPONENT=src/feedback |
| 30 | |
| 31 | #include <QtTest/QtTest> |
| 32 | |
| 33 | #include <qfeedbackeffect.h> |
| 34 | #include <qfeedbackactuator.h> |
| 35 | |
| 36 | QT_USE_NAMESPACE |
| 37 | |
| 38 | class tst_QFeedbackPlugin : public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | public: |
| 42 | tst_QFeedbackPlugin(); |
| 43 | ~tst_QFeedbackPlugin(); |
| 44 | |
| 45 | public slots: |
| 46 | void initTestCase(); |
| 47 | void cleanupTestCase(); |
| 48 | void init(); |
| 49 | void cleanup(); |
| 50 | |
| 51 | private slots: |
| 52 | void testPlugin(); |
| 53 | void testFileEffect(); |
| 54 | void testThemeEffect(); |
| 55 | }; |
| 56 | |
| 57 | tst_QFeedbackPlugin::tst_QFeedbackPlugin() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | tst_QFeedbackPlugin::~tst_QFeedbackPlugin() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | void tst_QFeedbackPlugin::initTestCase() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void tst_QFeedbackPlugin::cleanupTestCase() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | void tst_QFeedbackPlugin::init() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void tst_QFeedbackPlugin::cleanup() |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | void tst_QFeedbackPlugin::testThemeEffect() |
| 82 | { |
| 83 | QVERIFY(QFeedbackEffect::supportsThemeEffect()); |
| 84 | QVERIFY(QFeedbackEffect::playThemeEffect(QFeedbackEffect::Press)); |
| 85 | QVERIFY(!QFeedbackEffect::playThemeEffect(QFeedbackEffect::Release)); |
| 86 | } |
| 87 | |
| 88 | void tst_QFeedbackPlugin::testFileEffect() |
| 89 | { |
| 90 | QFeedbackFileEffect fileEffect; |
| 91 | QVERIFY(QFeedbackFileEffect::supportedMimeTypes().contains("x-test/this is a test")); |
| 92 | |
| 93 | QVERIFY(fileEffect.state() == QFeedbackEffect::Stopped); |
| 94 | |
| 95 | fileEffect.setSource(QUrl("load")); // this should call load |
| 96 | QVERIFY(fileEffect.state() == QFeedbackEffect::Loading); |
| 97 | |
| 98 | fileEffect.setSource(QUrl("ignored")); // not stopped, should fail |
| 99 | QVERIFY(fileEffect.source() == QUrl("load")); |
| 100 | |
| 101 | QVERIFY(fileEffect.isLoaded()); |
| 102 | fileEffect.setLoaded(true); // should do nothing |
| 103 | QVERIFY(fileEffect.isLoaded()); |
| 104 | QCOMPARE(fileEffect.duration(), 5678); // from the plugin |
| 105 | |
| 106 | fileEffect.unload(); // should fail, since we're not STOPPED (HMM!!) |
| 107 | QVERIFY(fileEffect.isLoaded()); |
| 108 | |
| 109 | fileEffect.stop(); |
| 110 | QVERIFY(fileEffect.state() == QFeedbackEffect::Stopped); |
| 111 | // Now we should be able to change things again |
| 112 | |
| 113 | // Make sure setting the source to the same thing is a noop |
| 114 | fileEffect.setSource(fileEffect.source()); |
| 115 | QVERIFY(fileEffect.state() == QFeedbackEffect::Stopped); |
| 116 | |
| 117 | // Now unload |
| 118 | QVERIFY(fileEffect.isLoaded()); |
| 119 | fileEffect.unload(); |
| 120 | QVERIFY(!fileEffect.isLoaded()); |
| 121 | QVERIFY(fileEffect.state() == QFeedbackEffect::Stopped); |
| 122 | QCOMPARE(fileEffect.duration(), 0); // unloaded, shouldn't call? |
| 123 | |
| 124 | // Change the url |
| 125 | fileEffect.setSource(QUrl("failload")); |
| 126 | QVERIFY(!fileEffect.isLoaded()); |
| 127 | // Spinning the event loop is necessary for mmk to fail a load |
| 128 | QTRY_COMPARE(fileEffect.state(), QFeedbackEffect::Stopped); |
| 129 | QCOMPARE(fileEffect.duration(), 0); // unknown |
| 130 | |
| 131 | fileEffect.setSource(QUrl("load")); |
| 132 | QVERIFY(fileEffect.isLoaded()); |
| 133 | QVERIFY(fileEffect.state() == QFeedbackEffect::Loading); |
| 134 | fileEffect.start(); |
| 135 | QVERIFY(fileEffect.state() == QFeedbackEffect::Running); |
| 136 | fileEffect.start(); |
| 137 | QVERIFY(fileEffect.state() == QFeedbackEffect::Running); |
| 138 | fileEffect.stop(); |
| 139 | QVERIFY(fileEffect.state() == QFeedbackEffect::Stopped); |
| 140 | fileEffect.pause(); |
| 141 | QVERIFY(fileEffect.state() == QFeedbackEffect::Paused); // XXX this is a strange transition |
| 142 | } |
| 143 | |
| 144 | void tst_QFeedbackPlugin::testPlugin() |
| 145 | { |
| 146 | QFeedbackHapticsEffect testEffect; |
| 147 | // first get the actuators. we want to use the test plugin actuator. |
| 148 | QFeedbackActuator* testActuator; |
| 149 | QList<QFeedbackActuator*> actuators = QFeedbackActuator::actuators(); |
| 150 | QCOMPARE(actuators.count(), 2); |
| 151 | |
| 152 | QCOMPARE(actuators.at(0)->name(), QString("test plugin")); |
| 153 | QCOMPARE(actuators.at(0)->id(), 0); |
| 154 | QCOMPARE(actuators.at(1)->name(), QString("5555")); |
| 155 | QCOMPARE(actuators.at(1)->id(), 1); |
| 156 | |
| 157 | // make sure we found the test actuator... |
| 158 | testActuator = actuators.at(0); |
| 159 | |
| 160 | QCOMPARE(testActuator->name(), QString("test plugin")); |
| 161 | QCOMPARE(testActuator->id(), 0); // test |
| 162 | QVERIFY(testActuator->isCapabilitySupported(QFeedbackActuator::Period)); |
| 163 | testActuator->setEnabled(true); |
| 164 | QVERIFY(!testActuator->isEnabled()); // the test plugin always returns enabled = false. |
| 165 | testActuator->setEnabled(false); |
| 166 | QVERIFY(!testActuator->isEnabled()); // the test plugin always returns enabled = false. |
| 167 | testActuator->setEnabled(true); |
| 168 | QVERIFY(!testActuator->isEnabled()); // the test plugin always returns enabled = false. |
| 169 | QCOMPARE(testActuator->state(), QFeedbackActuator::Unknown); // and it always returns state = unknown. |
| 170 | // XXX TODO: ensure that a "working" plugin returns real values.. |
| 171 | |
| 172 | // then, ensure that the test effect uses this actuator. |
| 173 | testEffect.setActuator(testActuator); |
| 174 | |
| 175 | // it will do nothing, so stick some values in and play it. |
| 176 | testEffect.setAttackIntensity(0.0); |
| 177 | testEffect.setAttackTime(250); |
| 178 | testEffect.setIntensity(1.0); |
| 179 | testEffect.setDuration(100); |
| 180 | testEffect.setFadeTime(250); |
| 181 | testEffect.setFadeIntensity(0.0); |
| 182 | testEffect.start(); |
| 183 | QVERIFY(testEffect.state() == QFeedbackHapticsEffect::Running); |
| 184 | testEffect.pause(); |
| 185 | QVERIFY(testEffect.state() == QFeedbackHapticsEffect::Paused); |
| 186 | testEffect.start(); |
| 187 | QVERIFY(testEffect.state() == QFeedbackHapticsEffect::Running); |
| 188 | testEffect.stop(); |
| 189 | QVERIFY(testEffect.state() == QFeedbackHapticsEffect::Stopped); |
| 190 | } |
| 191 | |
| 192 | QTEST_MAIN(tst_QFeedbackPlugin) |
| 193 | |
| 194 | #include "tst_qfeedbackplugin.moc" |