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

#include <cxxtest/TestSuite.h>
#include "itemmenupresenter.h"
#include "servicelocatorstub.h"
#include "listmenumock.h"
#include "downloadpoolmock.h"
#include "downloadcachemock.h"
#include "vdr-stub/ccontrolstub.h"

using namespace std;

namespace
{
    class ItemMenuPresenterTest: public CxxTest::TestSuite
    {
        private:
            ServiceLocatorStub serviceLocator;
            DownloadCacheMock downloadCache;
            DownloadPoolMock downloadPool;

        private:
            Feed CreateFeedWithTwoItems(string item1Title, string item1Mp3Url,
              string item2Title, string item2Mp3Url)
            {
                Feed feed("foo://bar");
                Item item1(item1Title, Rfc822DateTime(""), "", item1Mp3Url);
                Item item2(item2Title, Rfc822DateTime(""), "", item2Mp3Url);
                feed.AddItem(item1);
                feed.AddItem(item2);
                return feed;
            }

            void TestMovingSelectionSetsButtonHelp(ItemMenuPresenter& presenter,
              int selectedItem, string expectedButtonHelp, string& buttonHelp)
            {
                eKeys keys[] = { kDown, kUp, kLeft, kRight, kNone };

                for (int i=0; i < 5; i++)
                {
                    buttonHelp = "buttonNotSet";
                    presenter.ProcessKey(selectedItem, keys[i], osContinue);
                    TS_ASSERT_EQUALS(expectedButtonHelp, buttonHelp);

                    buttonHelp = "buttonNotSet";
                    presenter.ProcessKey(selectedItem, keys[i], osUnknown);
                    TS_ASSERT_EQUALS(expectedButtonHelp, buttonHelp);
                }
            }

        public:
            void setUp()
            {
                serviceLocator.downloadCache = &downloadCache;
                serviceLocator.itemAssignedToPlayerControl = "";
                downloadCache.filesAreCached = false;
                serviceLocator.downloadPool = &downloadPool;
            }

            void TestInitPopulatingItemListAndSettingMenuTitle()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "");
                feed.SetTitle("FeedTitle");
                ItemMenuPresenter presenter(serviceLocator, feed);

                menu.ExpectMethod("SetTitle", "FeedTitle");
                menu.ExpectMethod("AddItem", "??.??.??\tfoo");
                menu.ExpectMethod("AddItem", "??.??.??\tbar");
                menu.ExpectMethod("Refresh");

                presenter.Initialize(&menu);

                menu.Verify();
            }

            void TestInitSetsFilterButton()
            {
                ListMenuMock menu;
                ItemMenuPresenter presenter(serviceLocator, Feed(""));

                presenter.Initialize(&menu);

                TS_ASSERT_EQUALS("i18n:MP3 only", menu.yellow);
            }

            void TestInitSetsDownloadAndPlayHelp()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "mp3Url", "bar", "");
                ItemMenuPresenter presenter(serviceLocator, feed);
                menu.red = menu.green = "dummy";

                presenter.Initialize(&menu);

                TS_ASSERT_EQUALS("i18n:Download", menu.red);
                TS_ASSERT_EQUALS("", menu.green);
            }

            void TestUnprocessedKeys()
            {
                ListMenuMock menu;
                ItemMenuPresenter presenter(serviceLocator, Feed(""));

                presenter.Initialize(&menu);

                TS_ASSERT_EQUALS(osUnknown, presenter.ProcessKey(0, k1, osUnknown));
                TS_ASSERT_EQUALS(osBack, presenter.ProcessKey(0, kBack, osBack));
            }

            void TestYellowKeySetsPodcastFilter()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);

                cOsdMenu itemView("");
                serviceLocator.itemView = &itemView;

                menu.ResetMock();
                menu.ExpectMethod("Clear");
                menu.ExpectMethod("AddItem","??.??.??\tbar");
                menu.ExpectMethod("Refresh");

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kYellow, osUnknown));

                menu.Verify();
            }

            void TestYellowKeyTwiceResetsPodcastFilter()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);

                cOsdMenu itemView("");
                serviceLocator.itemView = &itemView;

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kYellow, osUnknown));

                menu.ResetMock();

                menu.ExpectMethod("Clear");
                menu.ExpectMethod("AddItem", "??.??.??\tfoo");
                menu.ExpectMethod("AddItem", "??.??.??\tbar");
                menu.ExpectMethod("Refresh");

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kYellow, osUnknown));

                menu.Verify();
            }

            void TestYellowKeyTogglesYellowHelpButtonText()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);

                presenter.ProcessKey(0, kYellow, osUnknown);
                TS_ASSERT_EQUALS("i18n:all", menu.yellow);

                presenter.ProcessKey(0, kYellow, osUnknown);
                TS_ASSERT_EQUALS("i18n:MP3 only", menu.yellow);
            }

            void TestSelectItemFromUnfilteredListWithOk()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "");
                ItemMenuPresenter presenter(serviceLocator, feed);

                cOsdMenu itemView("");
                serviceLocator.itemView = &itemView;

                presenter.Initialize(&menu);

                menu.ResetMock();
                menu.ExpectMethod("ShowSubMenu", menu.MakeString(&itemView));

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kOk, osUnknown));
                TS_ASSERT_EQUALS("foo", serviceLocator.itemViewWasCreatedFor);

                menu.Verify();
            }

            void TestSelectItemFromFilteredListWithOk()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                cOsdMenu itemView("");
                serviceLocator.itemView = &itemView;

                presenter.Initialize(&menu);
                presenter.ProcessKey(0, kYellow, osUnknown);

                menu.ResetMock();
                menu.ExpectMethod("ShowSubMenu", menu.MakeString(&itemView));

                presenter.ProcessKey(0, kOk, osUnknown);

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kOk, osUnknown));
                TS_ASSERT_EQUALS("bar", serviceLocator.itemViewWasCreatedFor);
            }

            void TestRedKeyForcesDownloadOfMp3()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);
                DownloadPoolMock downloadPool;
                serviceLocator.downloadPool = &downloadPool;

                downloadPool.ExpectMethod("AddDownload", "mp3Url");

                presenter.Initialize(&menu);
                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(1, kRed, osUnknown));

                downloadPool.Verify();
            }

            void TestMovingSelectionOrIdleUpdatesDownloadButtonWhenFilterIsSet()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);
                menu.isActiveMenu = true;
                TS_ASSERT_EQUALS(osContinue,
                  presenter.ProcessKey(0, kYellow, osUnknown));

                TestMovingSelectionSetsButtonHelp(presenter, 0,
                  "i18n:Download", menu.red);
            }

            void TestMovingSelectionOrIdleUpdatesPlayButtonWhenFilterIsSet()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);
                menu.isActiveMenu = true;
                downloadCache.filesAreCached = true;
                presenter.Initialize(&menu);

                TS_ASSERT_EQUALS(osContinue,
                  presenter.ProcessKey(0, kYellow, osUnknown));

                TestMovingSelectionSetsButtonHelp(presenter, 0,
                  "i18n:Play", menu.green);
            }

            void TestIfMenuNotActiveIgnoreCursorKeys()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);
                menu.isActiveMenu = false;

                presenter.ProcessKey(1, kDown, osContinue);

                TS_ASSERT_EQUALS("", menu.red);
            }

            void TestGreenKeyStartsPlayerForItemWithMp3()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);

                downloadCache.filesAreCached = true;
                serviceLocator.playerControl = new cControlStub();
                cControlStub::LaunchedPlayerControl = NULL;

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(1, kGreen, osUnknown));
                TS_ASSERT_EQUALS("bar", serviceLocator.itemAssignedToPlayerControl);
                TS_ASSERT_EQUALS(serviceLocator.playerControl, cControlStub::LaunchedPlayerControl);

                delete serviceLocator.playerControl;
            }

            void TestGreenKeyDoesNothingForItemWithoutMp3()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);

                serviceLocator.playerControl = new cControlStub();
                cControlStub::LaunchedPlayerControl = NULL;

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kGreen, osContinue));
                TS_ASSERT_EQUALS("", serviceLocator.itemAssignedToPlayerControl);
                TS_ASSERT_EQUALS((cControl*)NULL, cControlStub::LaunchedPlayerControl);

                delete serviceLocator.playerControl;
            }

            void TestGreenKeyDoesNothingWhenMp3NotDownloaded()
            {
                ListMenuMock menu;
                Feed feed = CreateFeedWithTwoItems("foo", "", "bar", "mp3Url");
                DownloadPoolMock downloadPool;
                serviceLocator.downloadPool = &downloadPool;
                ItemMenuPresenter presenter(serviceLocator, feed);

                presenter.Initialize(&menu);

                serviceLocator.playerControl = new cControlStub();
                cControlStub::LaunchedPlayerControl = NULL;
                downloadCache.filesAreCached = false;

                TS_ASSERT_EQUALS(osContinue, presenter.ProcessKey(0, kGreen, osContinue));
                TS_ASSERT_EQUALS("", serviceLocator.itemAssignedToPlayerControl);
                TS_ASSERT_EQUALS((cControl*)NULL, cControlStub::LaunchedPlayerControl);

                delete serviceLocator.playerControl;
            }

    };
};
