1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QtCore/qlocale.h>
44 #include <qaudioinput.h>
45 #include <qaudiodeviceinfo.h>
46 #include <qaudio.h>
47 #include <qaudioformat.h>
48
49 #if defined(Q_OS_SYMBIAN)
50 #define SRCDIR ""
51 #endif
52
53 Q_DECLARE_METATYPE(QAudioFormat)
54
55 class tst_QAudioInput : public QObject
56 {
57 Q_OBJECT
58 public:
tst_QAudioInput(QObject * parent=0)59 tst_QAudioInput(QObject* parent=0) : QObject(parent) {}
60
61 private slots:
62 void initTestCase();
63 void invalidFormat_data();
64 void invalidFormat();
65 void settings();
66 void buffers();
67 void notifyInterval();
68 void pullFile();
69
70 private:
71 bool available;
72 QAudioFormat format;
73 QAudioInput* audio;
74 };
75
initTestCase()76 void tst_QAudioInput::initTestCase()
77 {
78 qRegisterMetaType<QAudioFormat>();
79
80 format.setFrequency(8000);
81 format.setChannels(1);
82 format.setSampleSize(8);
83 format.setCodec("audio/pcm");
84 format.setByteOrder(QAudioFormat::LittleEndian);
85 format.setSampleType(QAudioFormat::UnSignedInt);
86
87 // Only perform tests if audio input device exists!
88 QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
89 if (devices.size() > 0)
90 available = true;
91 else {
92 qWarning()<<"NOTE: no audio input device found, no test will be performed";
93 available = false;
94 }
95
96 if (available)
97 audio = new QAudioInput(format, this);
98 }
99
invalidFormat_data()100 void tst_QAudioInput::invalidFormat_data()
101 {
102 QTest::addColumn<QAudioFormat>("invalidFormat");
103
104 QAudioFormat audioFormat;
105
106 QTest::newRow("Null Format")
107 << audioFormat;
108
109 audioFormat = format;
110 audioFormat.setChannels(0);
111 QTest::newRow("Channel count 0")
112 << audioFormat;
113
114 audioFormat = format;
115 audioFormat.setFrequency(0);
116 QTest::newRow("Sample rate 0")
117 << audioFormat;
118
119 audioFormat = format;
120 audioFormat.setSampleSize(0);
121 QTest::newRow("Sample size 0")
122 << audioFormat;
123 }
124
invalidFormat()125 void tst_QAudioInput::invalidFormat()
126 {
127 if (!available)
128 QSKIP("No audio input device found, no test will be performed", SkipAll);
129
130 QFETCH(QAudioFormat, invalidFormat);
131
132 QAudioInput audioInput(invalidFormat, this);
133
134 // Check that we are in the default state before calling start
135 QVERIFY2((audioInput.state() == QAudio::StoppedState), "state() was not set to StoppedState before start()");
136 QVERIFY2((audioInput.error() == QAudio::NoError), "error() was not set to QAudio::NoError before start()");
137
138 audioInput.start();
139
140 // Check that error is raised
141 QVERIFY2((audioInput.error() == QAudio::OpenError),"error() was not set to QAudio::OpenError after start()");
142 }
143
settings()144 void tst_QAudioInput::settings()
145 {
146 if (!available)
147 QSKIP("No audio input device found, no test will be performed", SkipAll);
148
149 // Confirm the setting we added in the init function.
150 QAudioFormat f = audio->format();
151
152 QVERIFY(format.channels() == f.channels());
153 QVERIFY(format.frequency() == f.frequency());
154 QVERIFY(format.sampleSize() == f.sampleSize());
155 QVERIFY(format.codec() == f.codec());
156 QVERIFY(format.byteOrder() == f.byteOrder());
157 QVERIFY(format.sampleType() == f.sampleType());
158 }
159
buffers()160 void tst_QAudioInput::buffers()
161 {
162 if (!available)
163 QSKIP("No audio input device found, no test will be performed", SkipAll);
164
165 // Should always have a buffer size greater than zero.
166 int store = audio->bufferSize();
167 audio->setBufferSize(4096);
168 QVERIFY(audio->bufferSize() > 0);
169 audio->setBufferSize(store);
170 QVERIFY(audio->bufferSize() == store);
171 }
172
notifyInterval()173 void tst_QAudioInput::notifyInterval()
174 {
175 if (!available)
176 QSKIP("No audio input device found, no test will be performed", SkipAll);
177
178 QVERIFY(audio->notifyInterval() == 1000); // Default
179
180 audio->setNotifyInterval(500);
181 QVERIFY(audio->notifyInterval() == 500); // Custom
182
183 audio->setNotifyInterval(1000); // reset
184 }
185
pullFile()186 void tst_QAudioInput::pullFile()
187 {
188 if (!available)
189 QSKIP("No audio input device found, no test will be performed", SkipAll);
190
191 QFile filename(SRCDIR"test.raw");
192 filename.open( QIODevice::WriteOnly | QIODevice::Truncate );
193
194 QSignalSpy readSignal(audio, SIGNAL(notify()));
195 QSignalSpy stateSignal(audio, SIGNAL(stateChanged(QAudio::State)));
196
197 // Always have default states, before start
198 QVERIFY(audio->state() == QAudio::StoppedState);
199 QVERIFY(audio->error() == QAudio::NoError);
200 QVERIFY(audio->elapsedUSecs() == 0);
201
202 audio->start(&filename);
203 QTest::qWait(20);
204 // Check state and periodSize() are valid non-zero values.
205 QVERIFY(audio->state() == QAudio::ActiveState);
206 QVERIFY(audio->error() == QAudio::NoError);
207 QVERIFY(audio->elapsedUSecs() > 10000 && audio->elapsedUSecs() < 800000);
208 QVERIFY(audio->periodSize() > 0);
209 QVERIFY(stateSignal.count() == 1); // State changed to QAudio::ActiveState
210
211 // Wait until finished...
212 QTest::qWait(5000);
213
214 QVERIFY(readSignal.count() > 0);
215 QVERIFY(audio->processedUSecs() > 0);
216
217 audio->stop();
218 QTest::qWait(20);
219 QVERIFY(audio->state() == QAudio::StoppedState);
220 QVERIFY(audio->elapsedUSecs() == 0);
221 // Can only check to make sure we got at least 1 more signal, but can be more.
222 QVERIFY(stateSignal.count() > 1);
223
224 filename.close();
225 }
226
227 QTEST_MAIN(tst_QAudioInput)
228
229 #include "tst_qaudioinput.moc"
230