/*
 * vdr-podcatcher - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2006 Tobias Grimm <vdr@e-tobi.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: synchedpespacketbuffer_test.cc 3255 2006-04-23 14:48:45Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "synchedpespacketbuffer.h"
#include "pesaudiopacket.h"
#include "circularpespacketbuffer.h"
#include "stringmessagemock.h"
#include "sleepermock.h"
#include "sleeper.h"

using namespace std;

class SynchedPesPacketBufferTest: public CxxTest::TestSuite
{
    public:
        void TestPutGetForwardedToWrappedBuffer()
        {
            SynchedPesPacketBuffer buffer(
              ref_ptr<IPesPacketBuffer>(new CircularPesPacketBuffer(10)),
              ref_ptr<ISleeper>(new SleeperMock()),
              ref_ptr<ISleeper>(new SleeperMock()));

            ref_ptr<PesAudioPacket> packet1(new PesAudioPacket(20));
            ref_ptr<PesAudioPacket> packet2(new PesAudioPacket(20));

            buffer.PutPesPacket(packet1);
            buffer.PutPesPacket(packet2);

            TS_ASSERT_EQUALS(packet1.get(), buffer.GetPesPacket().get());
            TS_ASSERT_EQUALS(packet2.get(), buffer.GetPesPacket().get());
        }

        void TestEofForwardedToWrapper()
        {
            CircularPesPacketBuffer* wrappedBuffer =
              new CircularPesPacketBuffer(10);
            SynchedPesPacketBuffer buffer(
              ref_ptr<IPesPacketBuffer>(wrappedBuffer),
              ref_ptr<ISleeper>(new SleeperMock()),
              ref_ptr<ISleeper>(new SleeperMock()));

            wrappedBuffer->SetEof(true);
            TS_ASSERT(buffer.Eof());

            wrappedBuffer->SetEof(false);
            TS_ASSERT(!buffer.Eof());

            buffer.SetEof(false);
            TS_ASSERT(!wrappedBuffer->Eof());

            buffer.SetEof(true);
            TS_ASSERT(wrappedBuffer->Eof());
        }

        void TestIsEmptyForwardedToWrapper()
        {
            CircularPesPacketBuffer* wrappedBuffer =
              new CircularPesPacketBuffer(10);
            SynchedPesPacketBuffer buffer(
              ref_ptr<IPesPacketBuffer>(wrappedBuffer),
              ref_ptr<ISleeper>(new SleeperMock()),
              ref_ptr<ISleeper>(new SleeperMock()));

            TS_ASSERT(buffer.IsEmpty());

            wrappedBuffer->PutPesPacket(ref_ptr<PesAudioPacket>(
              new PesAudioPacket(20)));
            TS_ASSERT(!buffer.IsEmpty());
        }

        void TestPutSleepsIfBufferIsFull()
        {
            StringMessageMock mock;
            SynchedPesPacketBuffer buffer(
              ref_ptr<IPesPacketBuffer>(new CircularPesPacketBuffer(1)),
              ref_ptr<ISleeper>(new SleeperMock(mock, "getSleeper")),
              ref_ptr<ISleeper>(new SleeperMock(mock, "putSleeper")));

            ref_ptr<PesAudioPacket> packet(new PesAudioPacket(20));

            TS_ASSERT(buffer.PutPesPacket(packet))
            mock.Verify();

            mock.ExpectMethod("putSleeper:Sleep");
            TS_ASSERT(!buffer.PutPesPacket(packet))
            mock.Verify();
        }

        void TestGetSleepsIfBufferIsEmpty()
        {
            StringMessageMock mock;
            SynchedPesPacketBuffer buffer(
              ref_ptr<IPesPacketBuffer>(new CircularPesPacketBuffer(1)),
              ref_ptr<ISleeper>(new SleeperMock(mock, "getSleeper")),
              ref_ptr<ISleeper>(new SleeperMock(mock, "putSleeper")));
            ref_ptr<PesAudioPacket> packet(new PesAudioPacket(20));
            buffer.PutPesPacket(packet);

            TS_ASSERT_EQUALS(packet.get(), buffer.GetPesPacket().get());
            mock.Verify();

            mock.ExpectMethod("getSleeper:Sleep");
            TS_ASSERT(NULL == buffer.GetPesPacket().get());
            mock.Verify();
        }
};
