/*
 * 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: itembuttonsupdater_test.cc 3218 2006-04-15 22:44:42Z svntobi $
 *
 */

#include <cxxtest/TestSuite.h>
#include "itembuttonsupdater.h"
#include "menu.h"
#include "servicelocatorstub.h"
#include "downloadcachemock.h"
#include "downloadpoolmock.h"

using namespace std;

class ItemMenuStub: public IMenu
{
    public:
        string greenHelp;
        string redHelp;

    public:
        // <IMenu>
        void SetGreenHelp(string text)
        {
            greenHelp = text;
        }

        void SetRedHelp(string text)
        {
            redHelp = text;
        }

        void SetYellowHelp(string text) {}
        void SetBlueHelp(string text) {}
        // <IMenu>
};

class ItemButtonsUpdaterTest: public CxxTest::TestSuite
{
    private:
        DownloadCacheMock* downloadCache;
        DownloadPoolMock* downloadPool;
        ServiceLocatorStub serviceLocator;
        Item itemWithoutMp3;
        Item itemWithMp3;

    public:
        ItemButtonsUpdaterTest()
        :itemWithoutMp3("", Rfc822DateTime(""), "", ""),
          itemWithMp3("", Rfc822DateTime(""), "", "mp3Url")
        {
        }

        void setUp()
        {
            downloadCache = new DownloadCacheMock();
            downloadPool = new DownloadPoolMock();
            serviceLocator.downloadCache = downloadCache;
            serviceLocator.downloadPool = downloadPool;
        }

        void tearDown()
        {
            delete serviceLocator.downloadCache;
            delete serviceLocator.downloadPool;
        }

        void TestUpdateSettingPlayHelpForNonMp3()
        {
            ItemMenuStub menu;
            ItemButtonsUpdater presenter(serviceLocator);

            menu.greenHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithoutMp3);
            TS_ASSERT_EQUALS("", menu.greenHelp);
        }

        void TestUpdateSettingPlayHelpForNotDownloadedMp3()
        {
            ItemMenuStub menu;
            downloadCache->filesAreCached = false;
            ItemButtonsUpdater presenter(serviceLocator);

            menu.greenHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithMp3);
            TS_ASSERT_EQUALS("", menu.greenHelp);
        }

        void TestUpdateSettingPlayHelpForDownloadedMp3()
        {
            ItemMenuStub menu;
            downloadCache->filesAreCached = true;
            ItemButtonsUpdater presenter(serviceLocator);

            menu.greenHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithMp3);
            TS_ASSERT_EQUALS("i18n:Play", menu.greenHelp);
        }

        void TestUpdateSettingDownloadHelpForNonMp3()
        {
            ItemMenuStub menu;
            ItemButtonsUpdater presenter(serviceLocator);

            menu.redHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithoutMp3);
            TS_ASSERT_EQUALS("", menu.redHelp);
        }

        void TestUpdateSettingDownloadHelpForMp3()
        {
            ItemMenuStub menu;
            ItemButtonsUpdater presenter(serviceLocator);

            menu.redHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithMp3);
            TS_ASSERT_EQUALS("i18n:Download", menu.redHelp);
        }

        void TestUpdateSettingQueuedIndication()
        {
            ItemMenuStub menu;
            ItemButtonsUpdater presenter(serviceLocator);
            ref_ptr<Download> download(new Download("url"));
            downloadPool->returnedDownload = download;

            menu.redHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithMp3);
            TS_ASSERT_EQUALS("i18n:>queued<", menu.redHelp);
        }

        void TestUpdateSettingDownloadPercentage()
        {
            ItemMenuStub menu;
            ItemButtonsUpdater presenter(serviceLocator);
            ref_ptr<Download> download(new Download("url"));
            download->SetSize(4 * 1024 * 1024);
            download->SetDownloaded(3 * 1024 * 1024);
            downloadPool->returnedDownload = download;

            menu.redHelp = "dummy";
            presenter.UpdateButtonHelp(&menu, itemWithMp3);
            TS_ASSERT_EQUALS("> 75% <", menu.redHelp);
        }
};
