tests/auto/qaudiooutput/tst_qaudiooutput.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 
       
    43 
       
    44 #include <QtTest/QtTest>
       
    45 #include <QtCore/qlocale.h>
       
    46 #include <qaudiooutput.h>
       
    47 #include <qaudiodeviceinfo.h>
       
    48 #include <qaudio.h>
       
    49 #include <qaudioformat.h>
       
    50 
       
    51 
       
    52 class tst_QAudioOutput : public QObject
       
    53 {
       
    54     Q_OBJECT
       
    55 public:
       
    56     tst_QAudioOutput(QObject* parent=0) : QObject(parent) {}
       
    57 
       
    58 private slots:
       
    59     void initTestCase();
       
    60     void settings();
       
    61     void buffers();
       
    62     void notifyInterval();
       
    63     void pullFile();
       
    64     void pushFile();
       
    65 
       
    66 private:
       
    67     bool          available;
       
    68     QAudioFormat  format;
       
    69     QAudioOutput* audio;
       
    70 };
       
    71 
       
    72 void tst_QAudioOutput::initTestCase()
       
    73 {
       
    74     format.setFrequency(8000);
       
    75     format.setChannels(1);
       
    76     format.setSampleSize(8);
       
    77     format.setCodec("audio/pcm");
       
    78     format.setByteOrder(QAudioFormat::LittleEndian);
       
    79     format.setSampleType(QAudioFormat::UnSignedInt);
       
    80 
       
    81     // Only perform tests if audio output device exists!
       
    82     QList<QAudioDeviceInfo> devices = QAudioDeviceInfo::deviceList(QAudio::AudioOutput);
       
    83     if(devices.size() > 0)
       
    84         available = true;
       
    85     else {
       
    86         qWarning()<<"NOTE: no audio output device found, no test will be performed";
       
    87         available = false;
       
    88     }
       
    89     audio = new QAudioOutput(format, this);
       
    90 }
       
    91 
       
    92 void tst_QAudioOutput::settings()
       
    93 {
       
    94     if(available) {
       
    95         // Confirm the setting we added in the init function.
       
    96         QAudioFormat f = audio->format();
       
    97 
       
    98         QVERIFY(format.channels() == f.channels());
       
    99         QVERIFY(format.frequency() == f.frequency());
       
   100         QVERIFY(format.sampleSize() == f.sampleSize());
       
   101         QVERIFY(format.codec() == f.codec());
       
   102         QVERIFY(format.byteOrder() == f.byteOrder());
       
   103         QVERIFY(format.sampleType() == f.sampleType());
       
   104     }
       
   105 }
       
   106 
       
   107 void tst_QAudioOutput::buffers()
       
   108 {
       
   109     if(available) {
       
   110         // Should always have a buffer size greater than zero.
       
   111         int store = audio->bufferSize();
       
   112         audio->setBufferSize(4096);
       
   113         QVERIFY(audio->bufferSize() > 0);
       
   114         audio->setBufferSize(store);
       
   115         QVERIFY(audio->bufferSize() == store);
       
   116     }
       
   117 }
       
   118 
       
   119 void tst_QAudioOutput::notifyInterval()
       
   120 {
       
   121     if(available) {
       
   122         QVERIFY(audio->notifyInterval() == 1000);   // Default
       
   123 
       
   124         audio->setNotifyInterval(500);
       
   125         QVERIFY(audio->notifyInterval() == 500);    // Custom
       
   126 
       
   127         audio->setNotifyInterval(1000);             // reset
       
   128     }
       
   129 }
       
   130 
       
   131 void tst_QAudioOutput::pullFile()
       
   132 {
       
   133     if(available) {
       
   134         QFile file(SRCDIR "4.wav");
       
   135         QVERIFY(file.exists());
       
   136         file.open(QIODevice::ReadOnly);
       
   137 
       
   138         QSignalSpy readSignal(audio, SIGNAL(notify()));
       
   139         QSignalSpy stateSignal(audio, SIGNAL(stateChanged(QAudio::State)));
       
   140         audio->setNotifyInterval(100);
       
   141 
       
   142         // Always have default states, before start
       
   143         QVERIFY(audio->state() == QAudio::StopState);
       
   144         QVERIFY(audio->error() == QAudio::NoError);
       
   145         QVERIFY(audio->clock() == 0);
       
   146 
       
   147         audio->start(&file);
       
   148         QTest::qWait(20); // wait 20ms
       
   149         // Check state, bytesFree() and periodSize() are valid non-zero values.
       
   150         QVERIFY(audio->state() == QAudio::ActiveState);
       
   151         QVERIFY(audio->error() == QAudio::NoError);
       
   152         QVERIFY(audio->periodSize() > 0);
       
   153         QVERIFY(audio->clock() > 10000 && audio->clock() < 800000);
       
   154         QVERIFY(stateSignal.count() == 1); // State changed to QAudio::ActiveState
       
   155 
       
   156         // Wait until finished...
       
   157         QTestEventLoop::instance().enterLoop(1);
       
   158         QCOMPARE(audio->totalTime(), qint64(692250));
       
   159 
       
   160 #ifdef Q_OS_WINCE
       
   161         // 4.wav is a little less than 700ms, so notify should fire 4 times on Wince!
       
   162         QVERIFY(readSignal.count() >= 4);
       
   163 #else
       
   164         // 4.wav is a little less than 700ms, so notify should fire 6 times!
       
   165         QVERIFY(readSignal.count() >= 6);
       
   166 #endif
       
   167         audio->stop();
       
   168         QTest::qWait(20); // wait 20ms
       
   169         QVERIFY(audio->state() == QAudio::StopState);
       
   170         QVERIFY(audio->clock() == 0);
       
   171         // Can only check to make sure we got at least 1 more signal, but can be more.
       
   172         QVERIFY(stateSignal.count() > 1);
       
   173 
       
   174         file.close();
       
   175     }
       
   176 }
       
   177 
       
   178 void tst_QAudioOutput::pushFile()
       
   179 {
       
   180     if(available) {
       
   181         QFile file(SRCDIR "4.wav");
       
   182         QVERIFY(file.exists());
       
   183         file.open(QIODevice::ReadOnly);
       
   184 
       
   185         const qint64 fileSize = file.size();
       
   186 
       
   187         QIODevice* feed = audio->start(0);
       
   188 
       
   189         char* buffer = new char[fileSize];
       
   190         file.read(buffer, fileSize);
       
   191 
       
   192         qint64 counter=0;
       
   193         qint64 written=0;
       
   194         while(written < fileSize) {
       
   195             written+=feed->write(buffer+written,fileSize-written);
       
   196             QTest::qWait(20);
       
   197             counter++;
       
   198         }
       
   199         QTestEventLoop::instance().enterLoop(1);
       
   200 
       
   201         QVERIFY(written == fileSize);
       
   202         QVERIFY(audio->totalTime() == 692250);
       
   203 
       
   204         audio->stop();
       
   205         file.close();
       
   206         delete [] buffer;
       
   207         delete audio;
       
   208     }
       
   209 }
       
   210 
       
   211 QTEST_MAIN(tst_QAudioOutput)
       
   212 
       
   213 #include "tst_qaudiooutput.moc"