| 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 | //TESTED_COMPONENT=src/feedback |
| 29 | |
| 30 | #include <QtTest/QtTest> |
| 31 | |
| 32 | #include <qfeedbackeffect.h> |
| 33 | #include <qfeedbackactuator.h> |
| 34 | |
| 35 | QT_USE_NAMESPACE |
| 36 | |
| 37 | class tst_QFeedbackHapticsEffect : public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | public: |
| 41 | tst_QFeedbackHapticsEffect(); |
| 42 | ~tst_QFeedbackHapticsEffect(); |
| 43 | |
| 44 | public slots: |
| 45 | void initTestCase(); |
| 46 | void cleanupTestCase(); |
| 47 | void init(); |
| 48 | void cleanup(); |
| 49 | |
| 50 | private slots: |
| 51 | void initialization(); |
| 52 | void envelope_data(); |
| 53 | void envelope(); |
| 54 | void startStop_data(); |
| 55 | void startStop(); |
| 56 | void themeSupport(); |
| 57 | |
| 58 | }; |
| 59 | |
| 60 | tst_QFeedbackHapticsEffect::tst_QFeedbackHapticsEffect() |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | tst_QFeedbackHapticsEffect::~tst_QFeedbackHapticsEffect() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void tst_QFeedbackHapticsEffect::initTestCase() |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void tst_QFeedbackHapticsEffect::cleanupTestCase() |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void tst_QFeedbackHapticsEffect::init() |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | void tst_QFeedbackHapticsEffect::cleanup() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | |
| 85 | void tst_QFeedbackHapticsEffect::initialization() |
| 86 | { |
| 87 | QFeedbackHapticsEffect effect; |
| 88 | if (QFeedbackActuator::actuators().isEmpty()) { |
| 89 | QVERIFY(!effect.actuator()->isValid()); |
| 90 | } else { |
| 91 | QVERIFY(effect.actuator()->isValid()); |
| 92 | QCOMPARE(effect.actuator(), QFeedbackActuator::actuators().first()); |
| 93 | } |
| 94 | |
| 95 | // actuators from other plugins need to be verified. |
| 96 | //test default values |
| 97 | QCOMPARE(effect.duration(), 250); //250ms is the default length for effects |
| 98 | QCOMPARE(effect.intensity(), qreal(1)); |
| 99 | QCOMPARE(effect.attackTime(), 0); |
| 100 | QCOMPARE(effect.attackIntensity(), qreal(0)); |
| 101 | QCOMPARE(effect.fadeTime(), 0); |
| 102 | QCOMPARE(effect.fadeIntensity(), qreal(0)); |
| 103 | QCOMPARE(effect.period(), -1); |
| 104 | QCOMPARE(effect.state(), QFeedbackEffect::Stopped); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | void tst_QFeedbackHapticsEffect::envelope_data() |
| 109 | { |
| 110 | QTest::addColumn<int>(name: "duration" ); |
| 111 | QTest::addColumn<qreal>(name: "intensity" ); |
| 112 | QTest::addColumn<int>(name: "attackTime" ); |
| 113 | QTest::addColumn<qreal>(name: "attackIntensity" ); |
| 114 | QTest::addColumn<int>(name: "fadeTime" ); |
| 115 | QTest::addColumn<qreal>(name: "fadeIntensity" ); |
| 116 | QTest::addColumn<int>(name: "period" ); |
| 117 | |
| 118 | QTest::newRow(dataTag: "TEST1" ) << 300 << qreal(.2) << 1 << qreal(.2) << 2 << qreal(1) << -1; |
| 119 | QTest::newRow(dataTag: "TEST2" ) << 500 << qreal(1) << 100 << qreal(.5) << 0 << qreal(0) << -1; |
| 120 | QTest::newRow(dataTag: "TEST3" ) << 250 << qreal(.8) << 150 << qreal(1) << 150 << qreal(1) << -1; |
| 121 | QTest::newRow(dataTag: "TEST period" ) << 1000 << qreal(1) << 150 << qreal(1) << 150 << qreal(1) << 150; |
| 122 | } |
| 123 | |
| 124 | void tst_QFeedbackHapticsEffect::envelope() |
| 125 | { |
| 126 | QFETCH(int, duration); |
| 127 | QFETCH(qreal, intensity); |
| 128 | QFETCH(int, attackTime); |
| 129 | QFETCH(qreal, attackIntensity); |
| 130 | QFETCH(int, fadeTime); |
| 131 | QFETCH(qreal, fadeIntensity); |
| 132 | QFETCH(int, period); |
| 133 | |
| 134 | QFeedbackHapticsEffect effect; |
| 135 | |
| 136 | // actuators from other plugins need to be verified. |
| 137 | effect.setDuration(duration); |
| 138 | QCOMPARE(effect.duration(), duration); |
| 139 | |
| 140 | effect.setIntensity(intensity); |
| 141 | QCOMPARE(effect.intensity(), intensity); |
| 142 | |
| 143 | effect.setAttackTime(attackTime); |
| 144 | QCOMPARE(effect.attackTime(), attackTime); |
| 145 | |
| 146 | effect.setAttackIntensity(attackIntensity); |
| 147 | QCOMPARE(effect.attackIntensity(), attackIntensity); |
| 148 | |
| 149 | effect.setFadeTime(fadeTime); |
| 150 | QCOMPARE(effect.fadeTime(), fadeTime); |
| 151 | |
| 152 | effect.setFadeIntensity(fadeIntensity); |
| 153 | QCOMPARE(effect.fadeIntensity(), fadeIntensity); |
| 154 | |
| 155 | effect.setPeriod(period); |
| 156 | QCOMPARE(effect.period(), period); |
| 157 | |
| 158 | //test all the values at once |
| 159 | QCOMPARE(effect.duration(), duration); |
| 160 | QCOMPARE(effect.intensity(), intensity); |
| 161 | QCOMPARE(effect.attackTime(), attackTime); |
| 162 | QCOMPARE(effect.attackIntensity(), attackIntensity); |
| 163 | QCOMPARE(effect.fadeTime(), fadeTime); |
| 164 | QCOMPARE(effect.fadeIntensity(), fadeIntensity); |
| 165 | QCOMPARE(effect.period(), period); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void tst_QFeedbackHapticsEffect::startStop_data() |
| 170 | { |
| 171 | envelope_data(); |
| 172 | } |
| 173 | |
| 174 | void tst_QFeedbackHapticsEffect::startStop() |
| 175 | { |
| 176 | qRegisterMetaType<QFeedbackEffect::ErrorType>(typeName: "QFeedbackEffect::ErrorType" ); |
| 177 | if (QFeedbackActuator::actuators().isEmpty()) |
| 178 | QSKIP("this test requires to have actuators" ); |
| 179 | |
| 180 | QList<QFeedbackActuator*> actuators = QFeedbackActuator::actuators(); |
| 181 | |
| 182 | QFETCH(int, duration); |
| 183 | QFETCH(qreal, intensity); |
| 184 | QFETCH(int, attackTime); |
| 185 | QFETCH(qreal, attackIntensity); |
| 186 | QFETCH(int, fadeTime); |
| 187 | QFETCH(qreal, fadeIntensity); |
| 188 | QFETCH(int, period); |
| 189 | |
| 190 | QFeedbackHapticsEffect effect; |
| 191 | QSignalSpy errorspy(&effect, SIGNAL(error(QFeedbackEffect::ErrorType))); |
| 192 | QSignalSpy stateSpy(&effect, SIGNAL(stateChanged())); |
| 193 | |
| 194 | effect.setDuration(duration); |
| 195 | effect.setIntensity(intensity); |
| 196 | effect.setAttackTime(attackTime); |
| 197 | effect.setAttackIntensity(attackIntensity); |
| 198 | effect.setFadeTime(fadeTime); |
| 199 | effect.setFadeIntensity(fadeIntensity); |
| 200 | effect.setPeriod(period); |
| 201 | |
| 202 | QCOMPARE(effect.state(), QFeedbackHapticsEffect::Stopped); |
| 203 | QCOMPARE(effect.actuator(), actuators.at(0)); |
| 204 | |
| 205 | // Double set a few properties (shouldn't call the backend) |
| 206 | effect.setAttackTime(attackTime); |
| 207 | QVERIFY(effect.attackTime() == attackTime); |
| 208 | |
| 209 | effect.setFadeTime(fadeTime); |
| 210 | QVERIFY(effect.fadeTime() == fadeTime); |
| 211 | |
| 212 | // Test setting to a non default actuator and then back again |
| 213 | if (actuators.count() > 1) { |
| 214 | effect.setActuator(actuators.at(i: 1)); |
| 215 | QCOMPARE(effect.actuator(), actuators.at(1)); |
| 216 | |
| 217 | effect.setActuator(0); |
| 218 | QCOMPARE(effect.actuator(), actuators.at(0)); |
| 219 | |
| 220 | // noop |
| 221 | effect.setActuator(actuators.at(i: 0)); |
| 222 | QCOMPARE(effect.actuator(), actuators.at(0)); |
| 223 | } |
| 224 | |
| 225 | // Now start |
| 226 | QVERIFY(stateSpy.isEmpty()); |
| 227 | effect.start(); |
| 228 | QVERIFY(errorspy.isEmpty()); |
| 229 | |
| 230 | // This seems a little risky |
| 231 | QTRY_COMPARE(effect.state(), QFeedbackHapticsEffect::Running); |
| 232 | QVERIFY(errorspy.isEmpty()); |
| 233 | QCOMPARE(stateSpy.count(), 1); // stopped to start |
| 234 | |
| 235 | effect.pause(); |
| 236 | QCOMPARE(effect.state(), QFeedbackHapticsEffect::Paused); |
| 237 | QCOMPARE(stateSpy.count(), 2); |
| 238 | |
| 239 | // Now try to change a few properties (should be refused because of !Stopped) |
| 240 | effect.setPeriod(period + 100); |
| 241 | QVERIFY(effect.period() == period); |
| 242 | |
| 243 | QFeedbackActuator* actuator = effect.actuator(); |
| 244 | effect.setActuator(0); |
| 245 | QVERIFY(effect.actuator() == actuator); |
| 246 | |
| 247 | effect.start(); |
| 248 | QCOMPARE(effect.state(), QFeedbackHapticsEffect::Running); |
| 249 | QCOMPARE(stateSpy.count(), 3); |
| 250 | |
| 251 | QTRY_COMPARE(effect.state(), QFeedbackHapticsEffect::Stopped); |
| 252 | QVERIFY(errorspy.isEmpty()); |
| 253 | QCOMPARE(stateSpy.count(), 4); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | void tst_QFeedbackHapticsEffect::themeSupport() |
| 258 | { |
| 259 | const bool supportsTheme = QFeedbackEffect::supportsThemeEffect(); |
| 260 | |
| 261 | if (!supportsTheme) |
| 262 | QSKIP("No theme support" ); |
| 263 | int numberOfSupportedEffects = 0; |
| 264 | for (int i = 0; i < QFeedbackEffect::NumberOfEffects; ++i) { |
| 265 | if (QFeedbackEffect::playThemeEffect(effect: QFeedbackEffect::Effect(i))) |
| 266 | numberOfSupportedEffects++; |
| 267 | QTest::qWait(ms: 250); //let's make sure the device is ready again |
| 268 | } |
| 269 | |
| 270 | QVERIFY(numberOfSupportedEffects > 0); |
| 271 | } |
| 272 | |
| 273 | QTEST_MAIN(tst_QFeedbackHapticsEffect) |
| 274 | |
| 275 | #include "tst_qfeedbackhapticseffect.moc" |
| 276 | |