| 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 | #include <QSignalSpy> |
| 36 | #include <QFeedbackFileEffect> |
| 37 | |
| 38 | class tst_QFeedbackMMK : public QObject |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | public: |
| 42 | tst_QFeedbackMMK(); |
| 43 | ~tst_QFeedbackMMK(); |
| 44 | |
| 45 | public slots: |
| 46 | void initTestCase(); |
| 47 | void cleanupTestCase(); |
| 48 | void init(); |
| 49 | void cleanup(); |
| 50 | |
| 51 | private slots: |
| 52 | void goodFile(); |
| 53 | void badFile(); |
| 54 | |
| 55 | private: |
| 56 | QUrl url; |
| 57 | }; |
| 58 | |
| 59 | tst_QFeedbackMMK::tst_QFeedbackMMK() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | tst_QFeedbackMMK::~tst_QFeedbackMMK() |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | void tst_QFeedbackMMK::initTestCase() |
| 68 | { |
| 69 | // Only perform tests if audio device exists |
| 70 | // have to check specific formats supported as test plugin maybe installed |
| 71 | QStringList mimeTypes = QFeedbackFileEffect::supportedMimeTypes(); |
| 72 | if (!(mimeTypes.contains(str: "audio/x-wav" ) || mimeTypes.contains(str: "audio/wav" ) |
| 73 | || mimeTypes.contains(str: "audio/wave" ) || mimeTypes.contains(str: "audio/x-pn-wav" ))) |
| 74 | QSKIP("No audio devices available" ); |
| 75 | |
| 76 | // Some of this comes from the qsoundeffect testcase . .. ... |
| 77 | #ifdef QT_QFEEDBACKMMK_USEAPPLICATIONPATH |
| 78 | url = QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + QString("/test.wav" )); |
| 79 | #else |
| 80 | url = QUrl::fromLocalFile(localfile: QString(SRCDIR "test.wav" )); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | void tst_QFeedbackMMK::cleanupTestCase() |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | void tst_QFeedbackMMK::init() |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | void tst_QFeedbackMMK::cleanup() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | Q_DECLARE_METATYPE(QFeedbackEffect::ErrorType); |
| 97 | |
| 98 | void tst_QFeedbackMMK::goodFile() |
| 99 | { |
| 100 | QFeedbackFileEffect fe; |
| 101 | qRegisterMetaType<QFeedbackEffect::ErrorType>(); |
| 102 | QSignalSpy errorSpy(&fe, SIGNAL(error(QFeedbackEffect::ErrorType))); |
| 103 | QSignalSpy stateSpy(&fe, SIGNAL(stateChanged())); |
| 104 | |
| 105 | QFileInfo fi(url.toLocalFile()); |
| 106 | qDebug() << "URL for test data:" << url << url.toLocalFile() << fi.exists(); |
| 107 | |
| 108 | fe.setSource(url); |
| 109 | |
| 110 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Loading); |
| 111 | QCOMPARE(errorSpy.count(), 0); |
| 112 | QCOMPARE(stateSpy.count(), 1); // Stopped to Loading |
| 113 | |
| 114 | // Wait for it to be loaded |
| 115 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Stopped); |
| 116 | QCOMPARE(errorSpy.count(), 0); |
| 117 | QCOMPARE(stateSpy.count(), 2); // Stopped to Loading to Stopped |
| 118 | |
| 119 | // Now play! |
| 120 | fe.start(); |
| 121 | |
| 122 | // Now wait for it to be playing |
| 123 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Running); |
| 124 | QCOMPARE(errorSpy.count(), 0); |
| 125 | QCOMPARE(stateSpy.count(), 3); // Stopped to Loading to Stopped to Running |
| 126 | QVERIFY(fe.isLoaded()); |
| 127 | QVERIFY(fe.duration() == 0); // unsupported :/ |
| 128 | |
| 129 | // Try pausing - not supported |
| 130 | fe.pause(); // XXX this emits stateChanged even when it fails |
| 131 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Running); |
| 132 | QCOMPARE(errorSpy.count(), 1); |
| 133 | QCOMPARE(stateSpy.count(), 4); // Stopped to Loading to Stopped to Running |
| 134 | |
| 135 | // It should run out, eventually |
| 136 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Stopped); |
| 137 | QCOMPARE(errorSpy.count(), 1); |
| 138 | QCOMPARE(stateSpy.count(), 5); // Stopped to Loading to Stopped to Running to Stopped |
| 139 | |
| 140 | // Play it again.. |
| 141 | fe.start(); |
| 142 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Running); |
| 143 | QCOMPARE(errorSpy.count(), 1); |
| 144 | QCOMPARE(stateSpy.count(), 6); // Stopped to Loading to Stopped to Running to Stopped to Running |
| 145 | |
| 146 | fe.stop(); |
| 147 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Stopped); |
| 148 | QCOMPARE(errorSpy.count(), 1); |
| 149 | QCOMPARE(stateSpy.count(), 7); // Stopped to Loading to Stopped to Running to Stopped to Running to Stopped |
| 150 | |
| 151 | fe.unload(); |
| 152 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Stopped); |
| 153 | QCOMPARE(stateSpy.count(), 7); // no change |
| 154 | QCOMPARE(fe.isLoaded(), false); |
| 155 | QCOMPARE(fe.duration(), 0); |
| 156 | |
| 157 | // now load again |
| 158 | fe.load(); |
| 159 | #ifdef Q_OS_MAC |
| 160 | QEXPECT_FAIL("" , "QTBUG-25448" , Abort); |
| 161 | #endif |
| 162 | QTRY_COMPARE((int)fe.state(), (int)QFeedbackFileEffect::Loading); |
| 163 | QCOMPARE(errorSpy.count(), 1); |
| 164 | QCOMPARE(stateSpy.count(), 8); // Stopped to Loading |
| 165 | |
| 166 | // Now wait for it to be loaded and playing |
| 167 | QTRY_COMPARE((int)fe.state(), (int) QFeedbackFileEffect::Stopped); |
| 168 | QCOMPARE(errorSpy.count(), 1); |
| 169 | QCOMPARE(stateSpy.count(), 9); // Stopped to Loading to Stopped |
| 170 | QVERIFY(fe.isLoaded()); |
| 171 | |
| 172 | // destroy it while playing |
| 173 | } |
| 174 | |
| 175 | void tst_QFeedbackMMK::badFile() |
| 176 | { |
| 177 | QFeedbackFileEffect fe; |
| 178 | qRegisterMetaType<QFeedbackEffect::ErrorType>(); |
| 179 | QSignalSpy errorSpy(&fe, SIGNAL(error(QFeedbackEffect::ErrorType))); |
| 180 | QSignalSpy stateSpy(&fe, SIGNAL(stateChanged())); |
| 181 | |
| 182 | fe.setSource(QUrl("file:///does/not/exist/ever.wav" )); |
| 183 | |
| 184 | // Depending on event loops we might miss the Loading state. |
| 185 | QTRY_VERIFY(stateSpy.count() > 0); // Loading & Stopped |
| 186 | QTRY_COMPARE(fe.state(), QFeedbackEffect::Stopped); |
| 187 | |
| 188 | QVERIFY(errorSpy.count() > 0); |
| 189 | QVERIFY(fe.isLoaded() == false); |
| 190 | stateSpy.clear(); |
| 191 | errorSpy.clear(); |
| 192 | |
| 193 | fe.start(); // this actually causes a load, so it goes into LOADING, then fails, should go to STOPPED |
| 194 | QTRY_VERIFY(stateSpy.count() > 0); // Loading & Stopped |
| 195 | QTRY_COMPARE(fe.state(), QFeedbackEffect::Stopped); |
| 196 | QVERIFY(errorSpy.count() > 0); |
| 197 | QVERIFY(fe.isLoaded() == false); |
| 198 | } |
| 199 | |
| 200 | QTEST_MAIN(tst_QFeedbackMMK) |
| 201 | |
| 202 | #include "tst_qfeedbackmmk.moc" |
| 203 | |