| 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 test suite 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 | |
| 30 | #include <QtTest/QtTest> |
| 31 | #include <QtCore/QString> |
| 32 | #include <QSound> |
| 33 | #include <QSoundEffect> |
| 34 | |
| 35 | class tst_QSound : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | tst_QSound( QObject* parent=0) : QObject(parent) {} |
| 41 | |
| 42 | private slots: |
| 43 | void initTestCase(); |
| 44 | void cleanupTestCase(); |
| 45 | void testLooping(); |
| 46 | void testPlay(); |
| 47 | void testStop(); |
| 48 | |
| 49 | void testPlayResource_data(); |
| 50 | void testPlayResource(); |
| 51 | |
| 52 | void testStaticPlay(); |
| 53 | |
| 54 | private: |
| 55 | QSound* sound; |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | void tst_QSound::initTestCase() |
| 60 | { |
| 61 | sound = 0; |
| 62 | // Only perform tests if audio device exists |
| 63 | QStringList mimeTypes = QSoundEffect::supportedMimeTypes(); |
| 64 | if (mimeTypes.empty()) |
| 65 | QSKIP("No audio devices available" ); |
| 66 | |
| 67 | const QString testFileName = QStringLiteral("test.wav" ); |
| 68 | const QString fullPath = QFINDTESTDATA(testFileName); |
| 69 | QVERIFY2(!fullPath.isEmpty(), qPrintable(QStringLiteral("Unable to locate " ) + testFileName)); |
| 70 | sound = new QSound(fullPath, this); |
| 71 | |
| 72 | QVERIFY(!sound->fileName().isEmpty()); |
| 73 | QCOMPARE(sound->loops(),1); |
| 74 | } |
| 75 | |
| 76 | void tst_QSound::cleanupTestCase() |
| 77 | { |
| 78 | if (sound) |
| 79 | { |
| 80 | delete sound; |
| 81 | sound = NULL; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void tst_QSound::testLooping() |
| 86 | { |
| 87 | sound->setLoops(5); |
| 88 | QCOMPARE(sound->loops(),5); |
| 89 | |
| 90 | sound->play(); |
| 91 | QVERIFY(!sound->isFinished()); |
| 92 | |
| 93 | // test.wav is about 200ms, wait until it has finished playing 5 times |
| 94 | QTRY_VERIFY(sound->isFinished()); |
| 95 | QCOMPARE(sound->loopsRemaining(),0); |
| 96 | } |
| 97 | |
| 98 | void tst_QSound::testPlay() |
| 99 | { |
| 100 | sound->setLoops(1); |
| 101 | sound->play(); |
| 102 | QVERIFY(!sound->isFinished()); |
| 103 | QTRY_VERIFY(sound->isFinished()); |
| 104 | } |
| 105 | |
| 106 | void tst_QSound::testStop() |
| 107 | { |
| 108 | sound->setLoops(10); |
| 109 | sound->play(); |
| 110 | QVERIFY(!sound->isFinished()); |
| 111 | QTest::qWait(ms: 1000); |
| 112 | sound->stop(); |
| 113 | QTRY_VERIFY(sound->isFinished()); |
| 114 | } |
| 115 | |
| 116 | void tst_QSound::testPlayResource_data() |
| 117 | { |
| 118 | QTest::addColumn<QString>(name: "filePath" ); |
| 119 | |
| 120 | QTest::newRow(dataTag: "prefix :/" ) << ":/test.wav" ; |
| 121 | QTest::newRow(dataTag: "prefix qrc:" ) << "qrc:test.wav" ; |
| 122 | QTest::newRow(dataTag: "prefix qrc:///" ) << "qrc:///test.wav" ; |
| 123 | } |
| 124 | |
| 125 | void tst_QSound::testPlayResource() |
| 126 | { |
| 127 | QFETCH(QString, filePath); |
| 128 | |
| 129 | QSound snd(filePath); |
| 130 | snd.play(); |
| 131 | QVERIFY(!snd.isFinished()); |
| 132 | QTRY_VERIFY(snd.isFinished()); |
| 133 | } |
| 134 | |
| 135 | void tst_QSound::testStaticPlay() |
| 136 | { |
| 137 | // Check that you hear sound with static play also. |
| 138 | const QString testFileName = QStringLiteral("test.wav" ); |
| 139 | const QString fullPath = QFINDTESTDATA(testFileName); |
| 140 | QVERIFY2(!fullPath.isEmpty(), qPrintable(QStringLiteral("Unable to locate " ) + testFileName)); |
| 141 | |
| 142 | QSound::play(filename: fullPath); |
| 143 | |
| 144 | QTest::qWait(ms: 1000); |
| 145 | } |
| 146 | |
| 147 | QTEST_MAIN(tst_QSound); |
| 148 | #include "tst_qsound.moc" |
| 149 | |