/*
 * 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: curldownloader_test.cc 3211 2006-04-14 21:01:37Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "curldownloader.h"
#include <fstream>
#include <sstream>
#include "stringmessagemock.h"
#include "download.h"
#include "downloadcachemock.h"

using namespace std;

class CurlDownloaderTest: public CxxTest::TestSuite
{
    public:
        void tearDown()
        {
            unlink("test.download");
        }

        void TestWriteData()
        {
            std::stringstream testStream;
            char* buffer = "MemBlock1MemBlock2MemBlock3";

            TS_ASSERT_EQUALS(9*2, CurlDownloader::WriteDataToStream(buffer, 9, 2, &testStream));

            TS_ASSERT_EQUALS("MemBlock1MemBlock2", testStream.str());
        }

        void TestWriteDataCausingWriteException()
        {
            std::filebuf streamBuf;
            std::ostream testStream(&streamBuf);
            testStream.exceptions(ofstream::badbit);

            char* buffer = "X";

            TS_ASSERT_EQUALS(0, CurlDownloader::WriteDataToStream(buffer, 1, 1, &testStream));
        }

        void TestDownload()
        {
            DownloadCacheMock downloadCache;
            CurlDownloader downloader(downloadCache);
            Download download("file://test.download");

            system("echo -n foo >test.download");

            downloadCache.ExpectMethod("Put", "foo", download.GetUrl());

            TS_ASSERT(downloader.PerformDownload(download));

            downloadCache.Verify();
        }

        void TestDownloadFailure()
        {
            DownloadCacheMock downloadCache;
            CurlDownloader downloader(downloadCache);
            Download download("file://test.download");

            unlink("test.download");

            TS_ASSERT(!downloader.PerformDownload(download));

            downloadCache.Verify();
        }

        void TestProgressCallBack()
        {
            DownloadCacheMock downloadCache;
            CurlDownloader downloader(downloadCache);
            Download download("foo");

            TS_ASSERT_EQUALS(0, CurlDownloader::ProgressCallback(
              &download, 100, 30, 0, 0));

            TS_ASSERT_EQUALS(100, download.GetSize());
            TS_ASSERT_EQUALS(30, download.GetDownloaded());
        }
};
