| 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 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 | #include <QtTest/QtTest> |
| 30 | #include <QDebug> |
| 31 | |
| 32 | #include <qaudioformat.h> |
| 33 | |
| 34 | #include <qaudiorecorder.h> |
| 35 | #include <qaudioencodersettingscontrol.h> |
| 36 | #include <qmediarecordercontrol.h> |
| 37 | #include <qaudioinputselectorcontrol.h> |
| 38 | #include <qaudiodeviceinfo.h> |
| 39 | #include <qaudioinput.h> |
| 40 | #include <qmediaobject.h> |
| 41 | |
| 42 | //TESTED_COMPONENT=src/multimedia |
| 43 | |
| 44 | #include "mockmediaserviceprovider.h" |
| 45 | #include "mockmediarecorderservice.h" |
| 46 | |
| 47 | QT_USE_NAMESPACE |
| 48 | |
| 49 | class tst_QAudioRecorder: public QObject |
| 50 | { |
| 51 | Q_OBJECT |
| 52 | |
| 53 | public slots: |
| 54 | void init(); |
| 55 | void cleanup(); |
| 56 | |
| 57 | private slots: |
| 58 | void testNullService(); |
| 59 | void testNullControl(); |
| 60 | void testAudioSource(); |
| 61 | void testDevices(); |
| 62 | void testAvailability(); |
| 63 | void testAvailableAudioInputChangedSignal(); |
| 64 | |
| 65 | private: |
| 66 | QAudioRecorder *audiosource; |
| 67 | MockMediaRecorderService *mockMediaRecorderService; |
| 68 | MockMediaServiceProvider *mockProvider; |
| 69 | }; |
| 70 | |
| 71 | void tst_QAudioRecorder::init() |
| 72 | { |
| 73 | mockMediaRecorderService = new MockMediaRecorderService(this, new MockMediaRecorderControl(this)); |
| 74 | mockProvider = new MockMediaServiceProvider(mockMediaRecorderService); |
| 75 | audiosource = 0; |
| 76 | |
| 77 | QMediaServiceProvider::setDefaultServiceProvider(mockProvider); |
| 78 | } |
| 79 | |
| 80 | void tst_QAudioRecorder::cleanup() |
| 81 | { |
| 82 | delete mockMediaRecorderService; |
| 83 | delete mockProvider; |
| 84 | delete audiosource; |
| 85 | mockMediaRecorderService = 0; |
| 86 | mockProvider = 0; |
| 87 | audiosource = 0; |
| 88 | } |
| 89 | |
| 90 | void tst_QAudioRecorder::testNullService() |
| 91 | { |
| 92 | mockProvider->service = 0; |
| 93 | QAudioRecorder source; |
| 94 | |
| 95 | QVERIFY(!source.isAvailable()); |
| 96 | QCOMPARE(source.availability(), QMultimedia::ServiceMissing); |
| 97 | |
| 98 | QCOMPARE(source.audioInputs().size(), 0); |
| 99 | QCOMPARE(source.defaultAudioInput(), QString()); |
| 100 | QCOMPARE(source.audioInput(), QString()); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | void tst_QAudioRecorder::testNullControl() |
| 105 | { |
| 106 | mockMediaRecorderService->hasControls = false; |
| 107 | QAudioRecorder source; |
| 108 | |
| 109 | QVERIFY(!source.isAvailable()); |
| 110 | QCOMPARE(source.availability(), QMultimedia::ServiceMissing); |
| 111 | |
| 112 | QCOMPARE(source.audioInputs().size(), 0); |
| 113 | QCOMPARE(source.defaultAudioInput(), QString()); |
| 114 | QCOMPARE(source.audioInput(), QString()); |
| 115 | |
| 116 | QCOMPARE(source.audioInputDescription("blah" ), QString()); |
| 117 | |
| 118 | QSignalSpy deviceNameSpy(&source, SIGNAL(audioInputChanged(QString))); |
| 119 | |
| 120 | source.setAudioInput("blah" ); |
| 121 | QCOMPARE(deviceNameSpy.count(), 0); |
| 122 | } |
| 123 | |
| 124 | void tst_QAudioRecorder::testAudioSource() |
| 125 | { |
| 126 | audiosource = new QAudioRecorder; |
| 127 | |
| 128 | QCOMPARE(audiosource->mediaObject()->service(),(QMediaService *) mockMediaRecorderService); |
| 129 | } |
| 130 | |
| 131 | void tst_QAudioRecorder::testDevices() |
| 132 | { |
| 133 | audiosource = new QAudioRecorder; |
| 134 | QList<QString> devices = audiosource->audioInputs(); |
| 135 | QVERIFY(devices.size() > 0); |
| 136 | QVERIFY(devices.at(0).compare("device1" ) == 0); |
| 137 | QVERIFY(audiosource->audioInputDescription("device1" ).compare("dev1 comment" ) == 0); |
| 138 | QVERIFY(audiosource->defaultAudioInput() == "device1" ); |
| 139 | QVERIFY(audiosource->isAvailable() == true); |
| 140 | |
| 141 | QSignalSpy checkSignal(audiosource, SIGNAL(audioInputChanged(QString))); |
| 142 | audiosource->setAudioInput("device2" ); |
| 143 | QVERIFY(audiosource->audioInput().compare("device2" ) == 0); |
| 144 | QVERIFY(checkSignal.count() == 1); |
| 145 | QVERIFY(audiosource->isAvailable() == true); |
| 146 | } |
| 147 | |
| 148 | void tst_QAudioRecorder::testAvailability() |
| 149 | { |
| 150 | QAudioRecorder source; |
| 151 | |
| 152 | QVERIFY(source.isAvailable()); |
| 153 | QCOMPARE(source.availability(), QMultimedia::Available); |
| 154 | } |
| 155 | |
| 156 | void tst_QAudioRecorder::testAvailableAudioInputChangedSignal() |
| 157 | { |
| 158 | // The availabilityChangedSignal is implemented in QAudioRecorder. SO using it to test the signal. |
| 159 | audiosource = new QAudioRecorder; |
| 160 | |
| 161 | /* Spy the signal availableInputsChanged and audioInputchanged */ |
| 162 | QSignalSpy changed(mockMediaRecorderService->mockAudioInputSelector, SIGNAL(availableInputsChanged())); |
| 163 | QSignalSpy audioInputchange(audiosource, SIGNAL(availableAudioInputsChanged())); |
| 164 | |
| 165 | /* Add the end points and verify if the available end point changed signal is emitted. */ |
| 166 | QMetaObject::invokeMethod(obj: mockMediaRecorderService->mockAudioInputSelector, member: "addInputs" ); |
| 167 | QVERIFY(changed.count() == 1); |
| 168 | QVERIFY(audioInputchange.count() == 1); |
| 169 | |
| 170 | /* Now try removes */ |
| 171 | changed.clear(); |
| 172 | audioInputchange.clear(); |
| 173 | QMetaObject::invokeMethod(obj: mockMediaRecorderService->mockAudioInputSelector, member: "removeInputs" ); |
| 174 | QVERIFY(changed.count() == 1); |
| 175 | QVERIFY(audioInputchange.count() == 1); |
| 176 | } |
| 177 | |
| 178 | QTEST_GUILESS_MAIN(tst_QAudioRecorder) |
| 179 | |
| 180 | #include "tst_qaudiorecorder.moc" |
| 181 | |