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

#include <cxxtest/TestSuite.h>
#include <string>
#include "item.h"
#include "itemview.h"
#include "itemviewpresenter.h"
#include "servicelocatorstub.h"
#include "downloadcache.h"
#include "downloadpoolmock.h"
#include "vdr-stub/ccontrolstub.h"
#include "downloadcachemock.h"

using namespace std;

class ItemViewStub: public IItemView
{
    public:
        string description;
        string title;
        string redHelp;
        string greenHelp;

    public:
        // <IItemView>
        void SetDescription(string description)
        {
            this->description = description;
        }

        void SetTitle(string title)
        {
            this->title = title;
        }

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

        void SetGreenHelp(string text)
        {
            greenHelp = text;
        }

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

class ItemViewPresenterTest: public CxxTest::TestSuite
{
    private:
        ItemViewStub view;
        ServiceLocatorStub serviceLocator;
        DownloadCacheMock downloadCache;
        DownloadPoolMock downloadPool;
        Item itemWithoutMp3;
        Item itemWithMp3;

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

        void setUp()
        {
            serviceLocator.downloadCache = &downloadCache;
            serviceLocator.downloadPool = &downloadPool;
            view.title = "";
            view.description = "";
        }

        void TestInitSetsDescription()
        {
            Item item("title", Rfc822DateTime("long_date"), "description", "mp3url");
            ItemViewPresenter presenter(serviceLocator, "", item);

            presenter.Initialize(&view);

            TS_ASSERT_EQUALS("long_date\n\ntitle\n\ndescription", view.description);
        }

        void TestInitSetsFeedTitle()
        {
            ItemViewPresenter presenter(serviceLocator, "FeedTitle", itemWithoutMp3);

            presenter.Initialize(&view);

            TS_ASSERT_EQUALS("FeedTitle", view.title);
        }

        void TestUnprocessedKey()
        {
            ItemViewPresenter presenter(serviceLocator, "", itemWithoutMp3);

            presenter.Initialize(&view);

            TS_ASSERT_EQUALS(osBack, presenter.ProcessKey(osBack, kOk));
        }

        void TestInitSetsDownloadAndPlayHelp()
        {
            ItemViewPresenter presenter(serviceLocator, "", itemWithMp3);
            view.redHelp = view.greenHelp = "dummy";

            presenter.Initialize(&view);

            TS_ASSERT_EQUALS("", view.greenHelp);
            TS_ASSERT_EQUALS("i18n:Download", view.redHelp);
            TS_ASSERT_EQUALS(osBack, presenter.ProcessKey(osBack, kOk));
        }

        void TestIdleKeyUpdatesDownloadAndPlayHelp()
        {
            ItemViewPresenter presenter(serviceLocator, "", itemWithMp3);
            presenter.Initialize(&view);
            view.redHelp = view.greenHelp = "dummy";

            presenter.ProcessKey(osContinue, kNone);

            TS_ASSERT_EQUALS("", view.greenHelp);
            TS_ASSERT_EQUALS("i18n:Download", view.redHelp);
            TS_ASSERT_EQUALS(osBack, presenter.ProcessKey(osBack, kOk));
        }

        void TestColorAndIdleKeysShouldBeProcessedByParent()
        {
            ItemViewPresenter presenter(serviceLocator, "", itemWithoutMp3);

            presenter.Initialize(&view);

            TS_ASSERT_EQUALS(osUnknown, presenter.ProcessKey(osContinue, kRed));
            TS_ASSERT_EQUALS(osUnknown, presenter.ProcessKey(osContinue, kGreen));
            TS_ASSERT_EQUALS(osUnknown, presenter.ProcessKey(osContinue, kNone));
        }
};
