| 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/qlocale.h> |
| 32 | #include <qaudiodeviceinfo.h> |
| 33 | |
| 34 | #include <QStringList> |
| 35 | #include <QList> |
| 36 | |
| 37 | //TESTED_COMPONENT=src/multimedia |
| 38 | |
| 39 | class tst_QAudioDeviceInfo : public QObject |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | public: |
| 43 | tst_QAudioDeviceInfo(QObject* parent=0) : QObject(parent) {} |
| 44 | |
| 45 | private slots: |
| 46 | void initTestCase(); |
| 47 | void checkAvailableDefaultInput(); |
| 48 | void checkAvailableDefaultOutput(); |
| 49 | void codecs(); |
| 50 | void channels(); |
| 51 | void sampleSizes(); |
| 52 | void byteOrders(); |
| 53 | void sampleTypes(); |
| 54 | void sampleRates(); |
| 55 | void isFormatSupported(); |
| 56 | void preferred(); |
| 57 | void nearest(); |
| 58 | void supportedChannelCounts(); |
| 59 | void supportedSampleRates(); |
| 60 | void assignOperator(); |
| 61 | void deviceName(); |
| 62 | void defaultConstructor(); |
| 63 | void equalityOperator(); |
| 64 | |
| 65 | private: |
| 66 | QAudioDeviceInfo* device; |
| 67 | }; |
| 68 | |
| 69 | void tst_QAudioDeviceInfo::initTestCase() |
| 70 | { |
| 71 | // Only perform tests if audio output device exists! |
| 72 | QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(mode: QAudio::AudioOutput); |
| 73 | if (devices.size() == 0) { |
| 74 | QSKIP("NOTE: no audio output device found, no tests will be performed"); |
| 75 | } else { |
| 76 | device = new QAudioDeviceInfo(devices.at(i: 0)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void tst_QAudioDeviceInfo::checkAvailableDefaultInput() |
| 81 | { |
| 82 | // Only perform tests if audio input device exists! |
| 83 | QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(mode: QAudio::AudioInput); |
| 84 | if (devices.size() > 0) { |
| 85 | QVERIFY(!QAudioDeviceInfo::defaultInputDevice().isNull()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void tst_QAudioDeviceInfo::checkAvailableDefaultOutput() |
| 90 | { |
| 91 | QVERIFY(!QAudioDeviceInfo::defaultOutputDevice().isNull()); |
| 92 | } |
| 93 | |
| 94 | void tst_QAudioDeviceInfo::codecs() |
| 95 | { |
| 96 | QStringList avail = device->supportedCodecs(); |
| 97 | QVERIFY(avail.size() > 0); |
| 98 | } |
| 99 | |
| 100 | void tst_QAudioDeviceInfo::channels() |
| 101 | { |
| 102 | QList<int> avail = device->supportedChannelCounts(); |
| 103 | QVERIFY(avail.size() > 0); |
| 104 | } |
| 105 | |
| 106 | void tst_QAudioDeviceInfo::sampleSizes() |
| 107 | { |
| 108 | QList<int> avail = device->supportedSampleSizes(); |
| 109 | QVERIFY(avail.size() > 0); |
| 110 | } |
| 111 | |
| 112 | void tst_QAudioDeviceInfo::byteOrders() |
| 113 | { |
| 114 | QList<QAudioFormat::Endian> avail = device->supportedByteOrders(); |
| 115 | QVERIFY(avail.size() > 0); |
| 116 | } |
| 117 | |
| 118 | void tst_QAudioDeviceInfo::sampleTypes() |
| 119 | { |
| 120 | QList<QAudioFormat::SampleType> avail = device->supportedSampleTypes(); |
| 121 | QVERIFY(avail.size() > 0); |
| 122 | } |
| 123 | |
| 124 | void tst_QAudioDeviceInfo::sampleRates() |
| 125 | { |
| 126 | QList<int> avail = device->supportedSampleRates(); |
| 127 | QVERIFY(avail.size() > 0); |
| 128 | } |
| 129 | |
| 130 | void tst_QAudioDeviceInfo::isFormatSupported() |
| 131 | { |
| 132 | QAudioFormat format; |
| 133 | format.setSampleRate(44100); |
| 134 | format.setChannelCount(2); |
| 135 | format.setSampleType(QAudioFormat::SignedInt); |
| 136 | format.setByteOrder(QAudioFormat::LittleEndian); |
| 137 | format.setSampleSize(16); |
| 138 | format.setCodec("audio/pcm"); |
| 139 | |
| 140 | // Should always be true for these format |
| 141 | QVERIFY(device->isFormatSupported(format)); |
| 142 | } |
| 143 | |
| 144 | void tst_QAudioDeviceInfo::preferred() |
| 145 | { |
| 146 | QAudioFormat format = device->preferredFormat(); |
| 147 | QVERIFY(format.isValid()); |
| 148 | QVERIFY(device->isFormatSupported(format)); |
| 149 | QVERIFY(device->nearestFormat(format) == format); |
| 150 | } |
| 151 | |
| 152 | // Returns closest QAudioFormat to settings that system audio supports. |
| 153 | void tst_QAudioDeviceInfo::nearest() |
| 154 | { |
| 155 | /* |
| 156 | QAudioFormat format1, format2; |
| 157 | format1.setSampleRate(8000); |
| 158 | format2 = device->nearestFormat(format1); |
| 159 | QVERIFY(format2.sampleRate() == 44100); |
| 160 | */ |
| 161 | QAudioFormat format; |
| 162 | format.setSampleRate(44100); |
| 163 | format.setChannelCount(2); |
| 164 | format.setSampleType(QAudioFormat::SignedInt); |
| 165 | format.setByteOrder(QAudioFormat::LittleEndian); |
| 166 | format.setSampleSize(16); |
| 167 | format.setCodec("audio/pcm"); |
| 168 | |
| 169 | QAudioFormat format2 = device->nearestFormat(format); |
| 170 | |
| 171 | // This is definitely dependent on platform support (but isFormatSupported tests that above) |
| 172 | QVERIFY(format2.sampleRate() == 44100); |
| 173 | } |
| 174 | |
| 175 | // Returns a list of supported channel counts. |
| 176 | void tst_QAudioDeviceInfo::supportedChannelCounts() |
| 177 | { |
| 178 | QList<int> avail = device->supportedChannelCounts(); |
| 179 | QVERIFY(avail.size() > 0); |
| 180 | } |
| 181 | |
| 182 | // Returns a list of supported sample rates. |
| 183 | void tst_QAudioDeviceInfo::supportedSampleRates() |
| 184 | { |
| 185 | QList<int> avail = device->supportedSampleRates(); |
| 186 | QVERIFY(avail.size() > 0); |
| 187 | } |
| 188 | |
| 189 | // QAudioDeviceInfo's assignOperator method |
| 190 | void tst_QAudioDeviceInfo::assignOperator() |
| 191 | { |
| 192 | QAudioDeviceInfo dev; |
| 193 | QVERIFY(dev.deviceName().isNull()); |
| 194 | QVERIFY(dev.isNull() == true); |
| 195 | |
| 196 | QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::availableDevices(mode: QAudio::AudioOutput); |
| 197 | QVERIFY(devices.size() > 0); |
| 198 | QAudioDeviceInfo dev1(devices.at(i: 0)); |
| 199 | dev = dev1; |
| 200 | QVERIFY(dev.isNull() == false); |
| 201 | QVERIFY(dev.deviceName() == dev1.deviceName()); |
| 202 | } |
| 203 | |
| 204 | // Returns human readable name of audio device |
| 205 | void tst_QAudioDeviceInfo::deviceName() |
| 206 | { |
| 207 | QVERIFY(!device->deviceName().isNull()); |
| 208 | QVERIFY(device->deviceName() == QAudioDeviceInfo::availableDevices(QAudio::AudioOutput).at(0).deviceName()); |
| 209 | } |
| 210 | |
| 211 | // QAudioDeviceInfo's defaultConstructor method |
| 212 | void tst_QAudioDeviceInfo::defaultConstructor() |
| 213 | { |
| 214 | QAudioDeviceInfo dev; |
| 215 | QVERIFY(dev.isNull() == true); |
| 216 | QVERIFY(dev.deviceName().isNull()); |
| 217 | } |
| 218 | |
| 219 | void tst_QAudioDeviceInfo::equalityOperator() |
| 220 | { |
| 221 | // Get some default device infos |
| 222 | QAudioDeviceInfo dev1; |
| 223 | QAudioDeviceInfo dev2; |
| 224 | |
| 225 | QVERIFY(dev1 == dev2); |
| 226 | QVERIFY(!(dev1 != dev2)); |
| 227 | |
| 228 | // Make sure each available device is not equal to null |
| 229 | const auto infos = QAudioDeviceInfo::availableDevices(mode: QAudio::AudioOutput); |
| 230 | for (const QAudioDeviceInfo info : infos) { |
| 231 | QVERIFY(dev1 != info); |
| 232 | QVERIFY(!(dev1 == info)); |
| 233 | |
| 234 | dev2 = info; |
| 235 | |
| 236 | QVERIFY(dev2 == info); |
| 237 | QVERIFY(!(dev2 != info)); |
| 238 | |
| 239 | QVERIFY(dev1 != dev2); |
| 240 | QVERIFY(!(dev1 == dev2)); |
| 241 | } |
| 242 | |
| 243 | // XXX Perhaps each available device should not be equal to any other |
| 244 | } |
| 245 | |
| 246 | QTEST_MAIN(tst_QAudioDeviceInfo) |
| 247 | |
| 248 | #include "tst_qaudiodeviceinfo.moc" |
| 249 |
