/*
 * 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: syncheddownloadpool_test.cc 3219 2006-04-16 15:06:35Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "syncheddownloadpool.h"
#include "downloadpoolmock.h"
#include "sleepermock.h"

using namespace std;

class SynchedDownloadPoolTest: public CxxTest::TestSuite
{
    public:
        void TestWrappedDownloadPool()
        {
            DownloadPoolMock* wrappedDownloadPool = new DownloadPoolMock();
            ref_ptr<IDownloadPool> wrappedDownloadPoolPtr(wrappedDownloadPool);
            SynchedDownloadPool downloadPool(wrappedDownloadPoolPtr,
              ref_ptr<ISleeper>(new SleeperMock()));
            ref_ptr<Download> download(new Download("foo"));

            wrappedDownloadPool->ExpectMethod("AddDownload", "foo");
            wrappedDownloadPool->ExpectMethod("GetNextDownload");
            wrappedDownloadPool->ExpectMethod("RemoveDownload", "foo");

            downloadPool.AddDownload(download);
            wrappedDownloadPool->returnedDownload = download;
            TS_ASSERT(NULL != downloadPool.GetNextDownload().get());
            downloadPool.RemoveDownload(download);
            wrappedDownloadPool->Verify();
        }

        void TestSleepIfPoolIsEmpty()
        {
            StringMessageMock mock;
            DownloadPoolMock* wrappedDownloadPool = new DownloadPoolMock(mock);
            ref_ptr<IDownloadPool> wrappedDownloadPoolPtr(wrappedDownloadPool);
            SynchedDownloadPool downloadPool(wrappedDownloadPoolPtr,
              ref_ptr<ISleeper>(new SleeperMock(mock)));

            mock.ExpectMethod("GetNextDownload");
            mock.ExpectMethod("Sleep");

            TS_ASSERT(NULL ==  downloadPool.GetNextDownload().get());

            mock.Verify();
        }

        void TestDoNotSleepIfPoolContainsDownloads()
        {
            StringMessageMock mock;
            DownloadPoolMock* wrappedDownloadPool = new DownloadPoolMock(mock);
            ref_ptr<IDownloadPool> wrappedDownloadPoolPtr(wrappedDownloadPool);
            SynchedDownloadPool downloadPool(wrappedDownloadPoolPtr,
              ref_ptr<ISleeper>(new SleeperMock(mock)));
            ref_ptr<Download> download(new Download("foo"));
            wrappedDownloadPool->returnedDownload = download;

            mock.ExpectMethod("GetNextDownload");

            TS_ASSERT(NULL !=  downloadPool.GetNextDownload().get());

            mock.Verify();
        }

        void TestWakeUpWhenAddingNewDownload()
        {
            StringMessageMock mock;
            DownloadPoolMock* wrappedDownloadPool = new DownloadPoolMock(mock);
            ref_ptr<IDownloadPool> wrappedDownloadPoolPtr(wrappedDownloadPool);
            SynchedDownloadPool downloadPool(wrappedDownloadPoolPtr,
              ref_ptr<ISleeper>(new SleeperMock(mock)));
            ref_ptr<Download> download(new Download("foo"));

            mock.ExpectMethod("AddDownload", "foo");
            mock.ExpectMethod("WakeUp");

            downloadPool.AddDownload(download);

            mock.Verify();
        }
};
