/*
 * 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: downloadaction_test.cc 2511 2006-01-28 14:05:09Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "downloadaction.h"
#include "stringmessagemock.h"
#include "downloadqueue.h"
#include "downloader.h"

using namespace std;

class MockDownloader: public StringMessageMock, public IDownloader
{
    public:
        bool PerformDownload(Download& download)
        {
            AddMethod("PerformDownload", download.GetUrl());
            return true;
        }
};

class DownloadActionTest: public CxxTest::TestSuite
{
    private:
        MockDownloader downloader;
        DownloadQueue* downloadQueue;
        DownloadAction* downloadAction;

    public:
        void setUp()
        {
            downloader.ResetMock();
            downloadQueue = new DownloadQueue();
            downloadAction = new DownloadAction(*downloadQueue, downloader);
        }

        void tearDown()
        {
            delete downloadAction;
            delete downloadQueue;
        }

        void TestEmptyQueue()
        {
            TS_ASSERT(downloadAction->Action());
            downloader.Verify();
        }

        void TestOneItemInQueue()
        {
            ref_ptr<Download> download(new Download("url"));

            downloadQueue->AddDownload(download);

            downloader.ExpectMethod("PerformDownload", "url");
            TS_ASSERT(downloadAction->Action());

            downloader.Verify();
            TS_ASSERT(NULL == downloadQueue->GetNextDownload().get());
        }

        void TestTwoItemsInQueue()
        {
            ref_ptr<Download> download1(new Download("url1"));
            ref_ptr<Download> download2(new Download("url2"));

            downloadQueue->AddDownload(download1);
            downloadQueue->AddDownload(download2);

            downloader.ExpectMethod("PerformDownload", "url1");
            TS_ASSERT(downloadAction->Action());
            downloader.Verify();

            downloader.ExpectMethod("PerformDownload", "url2");
            TS_ASSERT(downloadAction->Action());
            downloader.Verify();
        }
};
