/*
 * 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: mp3info_test.cc 3212 2006-04-14 23:30:32Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "mp3info.h"
#include <fstream>

using namespace std;

#define FRAME_HEADER_MP1L3_128KBIT 0xFFFB

class Mp3InfoTest: public CxxTest::TestSuite
{
    public:
        void TestInvalidStream()
        {
            stringstream nonMp3Stream;
            nonMp3Stream << "mp3-samples/5,0sec-mpeg1l3-cbr-48.mp3";
            Mp3Info mp3Info;
            mp3Info.ReadFromStream(nonMp3Stream);
            TS_ASSERT_EQUALS(0, mp3Info.GetTotalTime());
        }

        void TestCbr48Kbs()
        {
            ifstream mp3Stream("mp3-samples/5,0sec-mpeg1l3-cbr-48.mp3");
            Mp3Info mp3Info;
            mp3Info.ReadFromStream(mp3Stream);
            TS_ASSERT_EQUALS(5, mp3Info.GetTotalTime());
        }

        void TestCbr64Kbs()
        {
            ifstream mp3Stream("mp3-samples/5,0sec-mpeg1l3-cbr-64.mp3");
            Mp3Info mp3Info;
            mp3Info.ReadFromStream(mp3Stream);
            TS_ASSERT_EQUALS(5, mp3Info.GetTotalTime());
        }

        void TestVbrXing()
        {
            ifstream mp3Stream("mp3-samples/5,0sec-mpeg1l3-vbr-xing.mp3");
            Mp3Info mp3Info;
            mp3Info.ReadFromStream(mp3Stream);
            TS_ASSERT_EQUALS(5, mp3Info.GetTotalTime());
        }

        void TestMpeg2()
        {
            ifstream mp3Stream("mp3-samples/2,5sec-mpeg2l3-cbr-32.mp3");
            Mp3Info mp3Info;
            mp3Info.ReadFromStream(mp3Stream);
            TS_ASSERT_EQUALS(2, mp3Info.GetTotalTime());
        }

        void TestDurationCountedFromFirstFrameNotBeginningOfFrame()
        {
            stringstream mp3Stream;
            ifstream mp3Data("mp3-samples/5,0sec-mpeg1l3-cbr-64.mp3");
            Mp3Info mp3Info;

            for (int i=0 ; i < 1024; i++)
            {
                mp3Stream << "0123456789";
            }
            mp3Stream << mp3Data.rdbuf();
            mp3Info.ReadFromStream(mp3Stream);
            TS_ASSERT_EQUALS(5, mp3Info.GetTotalTime());
        }
};
