Mixxx

/home/maxime/Projets/Mixxx/1.10/mixxx/src/test/enginemastertest.cpp

Go to the documentation of this file.
00001 #include <gtest/gtest.h>
00002 #include <gmock/gmock.h>
00003 
00004 #include <QtDebug>
00005 
00006 #include "defs.h"
00007 #include "configobject.h"
00008 #include "engine/enginemaster.h"
00009 #include "engine/enginechannel.h"
00010 
00011 using ::testing::Return;
00012 using ::testing::_;
00013 
00014 namespace {
00015 
00016 class EngineChannelMock : public EngineChannel {
00017   public:
00018     EngineChannelMock(const char* group, ChannelOrientation defaultOrientation)
00019             : EngineChannel(group, defaultOrientation) {
00020     }
00021 
00022     void applyVolume(CSAMPLE* pBuff, const int iBufferSize) {
00023     }
00024 
00025     MOCK_METHOD0(isActive, bool());
00026     MOCK_METHOD0(isPFL, bool());
00027     MOCK_METHOD3(process, void(const CSAMPLE* pIn, const CSAMPLE* pOut, const int iBufferSize));
00028 };
00029 
00030 class EngineMasterTest : public testing::Test {
00031   protected:
00032     virtual void SetUp() {
00033         m_pConfig = new ConfigObject<ConfigValue>("");
00034         m_pMaster = new EngineMaster(m_pConfig, "[Master]");
00035     }
00036 
00037     virtual void TearDown() {
00038         delete m_pMaster;
00039         delete m_pConfig;
00040     }
00041 
00042     void ClearBuffer(CSAMPLE* pBuffer, int length) {
00043         memset(pBuffer, 0, sizeof(pBuffer[0])*length);
00044     }
00045 
00046     void FillBuffer(CSAMPLE* pBuffer, CSAMPLE value, int length) {
00047         for (int i = 0; i < length; ++i) {
00048             pBuffer[i] = value;
00049         }
00050     }
00051 
00052     void AssertWholeBufferEquals(const CSAMPLE* pBuffer, CSAMPLE value, int iBufferLen) {
00053         for (int i = 0; i < iBufferLen; ++i) {
00054             EXPECT_FLOAT_EQ(value, pBuffer[i]);
00055         }
00056     }
00057 
00058     ConfigObject<ConfigValue>* m_pConfig;
00059     EngineMaster* m_pMaster;
00060 };
00061 
00062 TEST_F(EngineMasterTest, SingleChannelOutputWorks) {
00063     EngineChannelMock* pChannel = new EngineChannelMock("[Test1]", EngineChannel::CENTER);
00064     m_pMaster->addChannel(pChannel);
00065 
00066     // Pretend that the channel processed the buffer by stuffing it with 1.0's
00067     CSAMPLE* pChannelBuffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test1]"));
00068     // We assume it uses MAX_BUFFER_LEN. This should probably be fixed.
00069     FillBuffer(pChannelBuffer, 1.0f, MAX_BUFFER_LEN);
00070 
00071     // Instruct the mock to claim it is active and not PFL.
00072     EXPECT_CALL(*pChannel, isActive())
00073             .Times(1)
00074             .WillOnce(Return(true));
00075     EXPECT_CALL(*pChannel, isPFL())
00076             .Times(1)
00077             .WillOnce(Return(false));
00078 
00079     // Instruct the mock to just return when process() gets called.
00080     EXPECT_CALL(*pChannel, process(_, _, MAX_BUFFER_LEN))
00081             .Times(1)
00082             .WillOnce(Return());
00083 
00084     m_pMaster->process(NULL, NULL, MAX_BUFFER_LEN);
00085 
00086     // Check that the master output contains the channel data.
00087     const CSAMPLE* pMasterBuffer = m_pMaster->getMasterBuffer();
00088     AssertWholeBufferEquals(pMasterBuffer, 1.0f, MAX_BUFFER_LEN);
00089 
00090     // Check that the headphone output does not contain the channel data.
00091     const CSAMPLE* pHeadphoneBuffer = m_pMaster->getHeadphoneBuffer();
00092     AssertWholeBufferEquals(pHeadphoneBuffer, 0.0f, MAX_BUFFER_LEN);
00093 }
00094 
00095 TEST_F(EngineMasterTest, TwoChannelOutputWorks) {
00096     EngineChannelMock* pChannel1 = new EngineChannelMock("[Test1]", EngineChannel::CENTER);
00097     m_pMaster->addChannel(pChannel1);
00098     EngineChannelMock* pChannel2 = new EngineChannelMock("[Test2]", EngineChannel::CENTER);
00099     m_pMaster->addChannel(pChannel2);
00100 
00101     // Pretend that the channel processed the buffer by stuffing it with 1.0's
00102     CSAMPLE* pChannel1Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test1]"));
00103     CSAMPLE* pChannel2Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test2]"));
00104 
00105     // We assume it uses MAX_BUFFER_LEN. This should probably be fixed.
00106     FillBuffer(pChannel1Buffer, 1.0f, MAX_BUFFER_LEN);
00107     FillBuffer(pChannel2Buffer, 2.0f, MAX_BUFFER_LEN);
00108 
00109     // Instruct channel 1 to claim it is active and not PFL.
00110     EXPECT_CALL(*pChannel1, isActive())
00111             .Times(1)
00112             .WillOnce(Return(true));
00113     EXPECT_CALL(*pChannel1, isPFL())
00114             .Times(1)
00115             .WillOnce(Return(false));
00116 
00117     // Instruct channel 2 to claim it is active and not PFL.
00118     EXPECT_CALL(*pChannel2, isActive())
00119             .Times(1)
00120             .WillOnce(Return(true));
00121     EXPECT_CALL(*pChannel2, isPFL())
00122             .Times(1)
00123             .WillOnce(Return(false));
00124 
00125     // Instruct the mock to just return when process() gets called.
00126     EXPECT_CALL(*pChannel1, process(_, _, MAX_BUFFER_LEN))
00127             .Times(1)
00128             .WillOnce(Return());
00129     EXPECT_CALL(*pChannel2, process(_, _, MAX_BUFFER_LEN))
00130             .Times(1)
00131             .WillOnce(Return());
00132 
00133     m_pMaster->process(NULL, NULL, MAX_BUFFER_LEN);
00134 
00135     // Check that the master output contains the sum of the channel data.
00136     const CSAMPLE* pMasterBuffer = m_pMaster->getMasterBuffer();
00137     AssertWholeBufferEquals(pMasterBuffer, 3.0f, MAX_BUFFER_LEN);
00138 
00139     // Check that the headphone output does not contain any channel data.
00140     const CSAMPLE* pHeadphoneBuffer = m_pMaster->getHeadphoneBuffer();
00141     AssertWholeBufferEquals(pHeadphoneBuffer, 0.0f, MAX_BUFFER_LEN);
00142 }
00143 
00144 TEST_F(EngineMasterTest, TwoChannelPFLOutputWorks) {
00145     EngineChannelMock* pChannel1 = new EngineChannelMock("[Test1]", EngineChannel::CENTER);
00146     m_pMaster->addChannel(pChannel1);
00147     EngineChannelMock* pChannel2 = new EngineChannelMock("[Test2]", EngineChannel::CENTER);
00148     m_pMaster->addChannel(pChannel2);
00149 
00150     // Pretend that the channel processed the buffer by stuffing it with 1.0's
00151     CSAMPLE* pChannel1Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test1]"));
00152     CSAMPLE* pChannel2Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test2]"));
00153 
00154     // We assume it uses MAX_BUFFER_LEN. This should probably be fixed.
00155     FillBuffer(pChannel1Buffer, 1.0f, MAX_BUFFER_LEN);
00156     FillBuffer(pChannel2Buffer, 2.0f, MAX_BUFFER_LEN);
00157 
00158     // Instruct channel 1 to claim it is active and not PFL.
00159     EXPECT_CALL(*pChannel1, isActive())
00160             .Times(1)
00161             .WillOnce(Return(true));
00162     EXPECT_CALL(*pChannel1, isPFL())
00163             .Times(1)
00164             .WillOnce(Return(true));
00165 
00166     // Instruct channel 2 to claim it is active and not PFL.
00167     EXPECT_CALL(*pChannel2, isActive())
00168             .Times(1)
00169             .WillOnce(Return(true));
00170     EXPECT_CALL(*pChannel2, isPFL())
00171             .Times(1)
00172             .WillOnce(Return(true));
00173 
00174     // Instruct the mock to just return when process() gets called.
00175     EXPECT_CALL(*pChannel1, process(_, _, MAX_BUFFER_LEN))
00176             .Times(1)
00177             .WillOnce(Return());
00178     EXPECT_CALL(*pChannel2, process(_, _, MAX_BUFFER_LEN))
00179             .Times(1)
00180             .WillOnce(Return());
00181 
00182     m_pMaster->process(NULL, NULL, MAX_BUFFER_LEN);
00183 
00184     // Check that the master output contains the sum of the channel data.
00185     const CSAMPLE* pMasterBuffer = m_pMaster->getMasterBuffer();
00186     AssertWholeBufferEquals(pMasterBuffer, 3.0f, MAX_BUFFER_LEN);
00187 
00188     // Check that the headphone output does not contain any channel data.
00189     const CSAMPLE* pHeadphoneBuffer = m_pMaster->getHeadphoneBuffer();
00190     AssertWholeBufferEquals(pHeadphoneBuffer, 3.0f, MAX_BUFFER_LEN);
00191 }
00192 
00193 TEST_F(EngineMasterTest, ThreeChannelOutputWorks) {
00194     EngineChannelMock* pChannel1 = new EngineChannelMock("[Test1]", EngineChannel::CENTER);
00195     m_pMaster->addChannel(pChannel1);
00196     EngineChannelMock* pChannel2 = new EngineChannelMock("[Test2]", EngineChannel::CENTER);
00197     m_pMaster->addChannel(pChannel2);
00198     EngineChannelMock* pChannel3 = new EngineChannelMock("[Test3]", EngineChannel::CENTER);
00199     m_pMaster->addChannel(pChannel3);
00200 
00201     // Pretend that the channel processed the buffer by stuffing it with 1.0's
00202     CSAMPLE* pChannel1Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test1]"));
00203     CSAMPLE* pChannel2Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test2]"));
00204     CSAMPLE* pChannel3Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test3]"));
00205 
00206     // We assume it uses MAX_BUFFER_LEN. This should probably be fixed.
00207     FillBuffer(pChannel1Buffer, 1.0f, MAX_BUFFER_LEN);
00208     FillBuffer(pChannel2Buffer, 2.0f, MAX_BUFFER_LEN);
00209     FillBuffer(pChannel3Buffer, 3.0f, MAX_BUFFER_LEN);
00210 
00211     // Instruct channel 1 to claim it is active and not PFL.
00212     EXPECT_CALL(*pChannel1, isActive())
00213             .Times(1)
00214             .WillOnce(Return(true));
00215     EXPECT_CALL(*pChannel1, isPFL())
00216             .Times(1)
00217             .WillOnce(Return(false));
00218 
00219     // Instruct channel 2 to claim it is active and not PFL.
00220     EXPECT_CALL(*pChannel2, isActive())
00221             .Times(1)
00222             .WillOnce(Return(true));
00223     EXPECT_CALL(*pChannel2, isPFL())
00224             .Times(1)
00225             .WillOnce(Return(false));
00226 
00227     // Instruct channel 3 to claim it is active and not PFL.
00228     EXPECT_CALL(*pChannel3, isActive())
00229             .Times(1)
00230             .WillOnce(Return(true));
00231     EXPECT_CALL(*pChannel3, isPFL())
00232             .Times(1)
00233             .WillOnce(Return(false));
00234 
00235     // Instruct the mock to just return when process() gets called.
00236     EXPECT_CALL(*pChannel1, process(_, _, MAX_BUFFER_LEN))
00237             .Times(1)
00238             .WillOnce(Return());
00239     EXPECT_CALL(*pChannel2, process(_, _, MAX_BUFFER_LEN))
00240             .Times(1)
00241             .WillOnce(Return());
00242     EXPECT_CALL(*pChannel3, process(_, _, MAX_BUFFER_LEN))
00243             .Times(1)
00244             .WillOnce(Return());
00245 
00246     m_pMaster->process(NULL, NULL, MAX_BUFFER_LEN);
00247 
00248     // Check that the master output contains the sum of the channel data.
00249     const CSAMPLE* pMasterBuffer = m_pMaster->getMasterBuffer();
00250     AssertWholeBufferEquals(pMasterBuffer, 6.0f, MAX_BUFFER_LEN);
00251 
00252     // Check that the headphone output does not contain any channel data.
00253     const CSAMPLE* pHeadphoneBuffer = m_pMaster->getHeadphoneBuffer();
00254     AssertWholeBufferEquals(pHeadphoneBuffer, 0.0f, MAX_BUFFER_LEN);
00255 }
00256 
00257 TEST_F(EngineMasterTest, ThreeChannelPFLOutputWorks) {
00258     EngineChannelMock* pChannel1 = new EngineChannelMock("[Test1]", EngineChannel::CENTER);
00259     m_pMaster->addChannel(pChannel1);
00260     EngineChannelMock* pChannel2 = new EngineChannelMock("[Test2]", EngineChannel::CENTER);
00261     m_pMaster->addChannel(pChannel2);
00262     EngineChannelMock* pChannel3 = new EngineChannelMock("[Test3]", EngineChannel::CENTER);
00263     m_pMaster->addChannel(pChannel3);
00264 
00265     // Pretend that the channel processed the buffer by stuffing it with 1.0's
00266     CSAMPLE* pChannel1Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test1]"));
00267     CSAMPLE* pChannel2Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test2]"));
00268     CSAMPLE* pChannel3Buffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test3]"));
00269 
00270     // We assume it uses MAX_BUFFER_LEN. This should probably be fixed.
00271     FillBuffer(pChannel1Buffer, 1.0f, MAX_BUFFER_LEN);
00272     FillBuffer(pChannel2Buffer, 2.0f, MAX_BUFFER_LEN);
00273     FillBuffer(pChannel3Buffer, 3.0f, MAX_BUFFER_LEN);
00274 
00275     // Instruct channel 1 to claim it is active and not PFL.
00276     EXPECT_CALL(*pChannel1, isActive())
00277             .Times(1)
00278             .WillOnce(Return(true));
00279     EXPECT_CALL(*pChannel1, isPFL())
00280             .Times(1)
00281             .WillOnce(Return(true));
00282 
00283     // Instruct channel 2 to claim it is active and not PFL.
00284     EXPECT_CALL(*pChannel2, isActive())
00285             .Times(1)
00286             .WillOnce(Return(true));
00287     EXPECT_CALL(*pChannel2, isPFL())
00288             .Times(1)
00289             .WillOnce(Return(true));
00290 
00291     // Instruct channel 3 to claim it is active and not PFL.
00292     EXPECT_CALL(*pChannel3, isActive())
00293             .Times(1)
00294             .WillOnce(Return(true));
00295     EXPECT_CALL(*pChannel3, isPFL())
00296             .Times(1)
00297             .WillOnce(Return(true));
00298 
00299     // Instruct the mock to just return when process() gets called.
00300     EXPECT_CALL(*pChannel1, process(_, _, MAX_BUFFER_LEN))
00301             .Times(1)
00302             .WillOnce(Return());
00303     EXPECT_CALL(*pChannel2, process(_, _, MAX_BUFFER_LEN))
00304             .Times(1)
00305             .WillOnce(Return());
00306     EXPECT_CALL(*pChannel3, process(_, _, MAX_BUFFER_LEN))
00307             .Times(1)
00308             .WillOnce(Return());
00309 
00310     m_pMaster->process(NULL, NULL, MAX_BUFFER_LEN);
00311 
00312     // Check that the master output contains the sum of the channel data.
00313     const CSAMPLE* pMasterBuffer = m_pMaster->getMasterBuffer();
00314     AssertWholeBufferEquals(pMasterBuffer, 6.0f, MAX_BUFFER_LEN);
00315 
00316     // Check that the headphone output does not contain any channel data.
00317     const CSAMPLE* pHeadphoneBuffer = m_pMaster->getHeadphoneBuffer();
00318     AssertWholeBufferEquals(pHeadphoneBuffer, 6.0f, MAX_BUFFER_LEN);
00319 }
00320 
00321 TEST_F(EngineMasterTest, SingleChannelPFLOutputWorks) {
00322     EngineChannelMock* pChannel = new EngineChannelMock("[Test1]", EngineChannel::CENTER);
00323     m_pMaster->addChannel(pChannel);
00324 
00325     // Pretend that the channel processed the buffer by stuffing it with 1.0's
00326     CSAMPLE* pChannelBuffer = const_cast<CSAMPLE*>(m_pMaster->getChannelBuffer("[Test1]"));
00327     // We assume it uses MAX_BUFFER_LEN. This should probably be fixed.
00328     FillBuffer(pChannelBuffer, 1.0f, MAX_BUFFER_LEN);
00329 
00330     // Instruct the mock to claim it is active and PFL
00331     EXPECT_CALL(*pChannel, isActive())
00332             .Times(1)
00333             .WillOnce(Return(true));
00334     EXPECT_CALL(*pChannel, isPFL())
00335             .Times(1)
00336             .WillOnce(Return(true));
00337 
00338     // Instruct the mock to just return when process() gets called.
00339     EXPECT_CALL(*pChannel, process(_, _, _))
00340             .Times(1)
00341             .WillOnce(Return());
00342 
00343     m_pMaster->process(NULL, NULL, MAX_BUFFER_LEN);
00344 
00345     // Check that the master output contains the channel data.
00346     const CSAMPLE* pMasterBuffer = m_pMaster->getMasterBuffer();
00347     AssertWholeBufferEquals(pMasterBuffer, 1.0f, MAX_BUFFER_LEN);
00348 
00349     // Check that the headphone output contains the channel data.
00350     const CSAMPLE* pHeadphoneBuffer = m_pMaster->getHeadphoneBuffer();
00351     AssertWholeBufferEquals(pHeadphoneBuffer, 1.0f, MAX_BUFFER_LEN);
00352 }
00353 
00354 }  // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines