tests/auto/qaudioinput/tst_qaudioinput.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    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 
       
    50 class tst_QAudioInput : public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 public:
       
    54     tst_QAudioInput(QObject* parent=0) : QObject(parent) {}
       
    55 
       
    56 private slots:
       
    57     void initTestCase();
       
    58     void settings();
       
    59     void buffers();
       
    60     void notifyInterval();
       
    61     void pullFile();
       
    62 
       
    63 private:
       
    64     bool           available;
       
    65     QAudioFormat   format;
       
    66     QAudioInput*   audio;
       
    67 };
       
    68 
       
    69 void tst_QAudioInput::initTestCase()
       
    70 {
       
    71     format.setFrequency(8000);
       
    72     format.setChannels(1);
       
    73     format.setSampleSize(8);
       
    74     format.setCodec("audio/pcm");
       
    75     format.setByteOrder(QAudioFormat::LittleEndian);
       
    76     format.setSampleType(QAudioFormat::UnSignedInt);
       
    77 
       
    78     // Only perform tests if audio input device exists!
       
    79     QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::deviceList(QAudio::AudioInput);
       
    80     if(devices.size() > 0)
       
    81         available = true;
       
    82     else {
       
    83         qWarning()<<"NOTE: no audio input device found, no test will be performed";
       
    84         available = false;
       
    85     }
       
    86 
       
    87     if(available)
       
    88         audio = new QAudioInput(format, this);
       
    89 }
       
    90 
       
    91 void tst_QAudioInput::settings()
       
    92 {
       
    93     if(available) {
       
    94         // Confirm the setting we added in the init function.
       
    95         QAudioFormat f = audio->format();
       
    96 
       
    97         QVERIFY(format.channels() == f.channels());
       
    98         QVERIFY(format.frequency() == f.frequency());
       
    99         QVERIFY(format.sampleSize() == f.sampleSize());
       
   100         QVERIFY(format.codec() == f.codec());
       
   101         QVERIFY(format.byteOrder() == f.byteOrder());
       
   102         QVERIFY(format.sampleType() == f.sampleType());
       
   103     }
       
   104 }
       
   105 
       
   106 void tst_QAudioInput::buffers()
       
   107 {
       
   108     if(available) {
       
   109         // Should always have a buffer size greater than zero.
       
   110         int store = audio->bufferSize();
       
   111         audio->setBufferSize(4096);
       
   112         QVERIFY(audio->bufferSize() > 0);
       
   113         audio->setBufferSize(store);
       
   114         QVERIFY(audio->bufferSize() == store);
       
   115     }
       
   116 }
       
   117 
       
   118 void tst_QAudioInput::notifyInterval()
       
   119 {
       
   120     if(available) {
       
   121         QVERIFY(audio->notifyInterval() == 1000);   // Default
       
   122 
       
   123         audio->setNotifyInterval(500);
       
   124         QVERIFY(audio->notifyInterval() == 500);    // Custom
       
   125 
       
   126         audio->setNotifyInterval(1000);             // reset
       
   127     }
       
   128 }
       
   129 
       
   130 void tst_QAudioInput::pullFile()
       
   131 {
       
   132     if(available) {
       
   133         QFile filename(SRCDIR "test.raw");
       
   134         filename.open( QIODevice::WriteOnly | QIODevice::Truncate );
       
   135 
       
   136         QSignalSpy readSignal(audio, SIGNAL(notify()));
       
   137         QSignalSpy stateSignal(audio, SIGNAL(stateChanged(QAudio::State)));
       
   138 
       
   139         // Always have default states, before start
       
   140         QVERIFY(audio->state() == QAudio::StopState);
       
   141         QVERIFY(audio->error() == QAudio::NoError);
       
   142         QVERIFY(audio->clock() == 0);
       
   143 
       
   144         audio->start(&filename);
       
   145         QTest::qWait(20);
       
   146         // Check state and periodSize() are valid non-zero values.
       
   147         QVERIFY(audio->state() == QAudio::ActiveState);
       
   148         QVERIFY(audio->error() == QAudio::NoError);
       
   149         QVERIFY(audio->clock() > 10000 && audio->clock() < 800000);
       
   150         QVERIFY(audio->periodSize() > 0);
       
   151         QVERIFY(stateSignal.count() == 1); // State changed to QAudio::ActiveState
       
   152 
       
   153         // Wait until finished...
       
   154         QTest::qWait(5000);
       
   155 
       
   156         QVERIFY(readSignal.count() > 0);
       
   157         QVERIFY(audio->totalTime() > 0);
       
   158 
       
   159         audio->stop();
       
   160         QTest::qWait(20);
       
   161         QVERIFY(audio->state() == QAudio::StopState);
       
   162         QVERIFY(audio->clock() == 0);
       
   163         // Can only check to make sure we got at least 1 more signal, but can be more.
       
   164         QVERIFY(stateSignal.count() > 1);
       
   165 
       
   166         filename.close();
       
   167     }
       
   168 }
       
   169 
       
   170 QTEST_MAIN(tst_QAudioInput)
       
   171 
       
   172 #include "tst_qaudioinput.moc"