/*
 * 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: feedmenupresenter_test.cc 2491 2006-01-26 23:32:06Z svntobi $
 *
 */

#include <vector>
#include <sstream>
#include <cxxtest/TestSuite.h>
#include "feedmenupresenter.h"
#include "servicelocatorstub.h"
#include "downloadpoolmock.h"
#include "downloadpoolmock.h"
#include "feedparser.h"
#include "feedsources.h"
#include "listmenu.h"
#include "listmenumock.h"

using namespace std;

namespace
{
    class FeedMenuPresenterTest: public CxxTest::TestSuite,
      public IFeedSources, IFeedParser
    {
        private:
            bool feedsCanBeParsed;
            DownloadPoolMock downloadPool;
            ListMenuMock menu;
            ServiceLocatorStub serviceLocator;
            FeedMenuPresenter* presenter;

        public:
            // <IFeedSources>
            void GetFeeds(FeedList& feeds)
            {
                Feed feed1("foo");
                Feed feed2("bar");
                feeds.push_back(feed1);
                feeds.push_back(feed2);
            }
            // </IFeedSources>

            // <IFeedParser>
            bool Parse(Feed& feed)
            {
                if (feedsCanBeParsed)
                {
                    feed.SetTitle(feed.GetUrl() + ": Loaded Title");
                    return true;
                }
                else
                {
                    return false;
                }
            }
            // <IFeedParser>

            void setUp()
            {
                downloadPool.ResetMock();
                menu.ResetMock();
                serviceLocator.downloadPool = &downloadPool;
                serviceLocator.feedSources = this;
                serviceLocator.feedParser = this;
                presenter = new FeedMenuPresenter(serviceLocator);
            }

            void tearDown()
            {
                delete presenter;
            }

            void TestInitializeSettingMenuTitleAndFewedList()
            {
                feedsCanBeParsed = true;
                menu.ExpectMethod("SetTitle", "i18n:Select Feed");
                menu.ExpectMethod("AddItem", "foo: Loaded Title");
                menu.ExpectMethod("AddItem", "bar: Loaded Title");
                presenter->Initialize(&menu);
                menu.Verify();
            }

            void TestInitializeSettingRedButton()
            {
                menu.red = "";
                presenter->Initialize(&menu);
                TS_ASSERT_EQUALS("i18n:Update", menu.red)
            }

            void TestParseError()
            {
                feedsCanBeParsed = false;

                menu.ExpectMethod("SetTitle", "i18n:Select Feed");
                menu.ExpectMethod("AddItem", "??\?");
                menu.ExpectMethod("AddItem", "??\?");

                presenter->Initialize(&menu);
                menu.Verify();
            }

            void TestUnusedKeyPressed()
            {
                TS_ASSERT_EQUALS(osContinue, presenter->ProcessKey(0, k1, osContinue));
                TS_ASSERT_EQUALS(osUnknown, presenter->ProcessKey(0, k1, osUnknown));
            }

            void TestKeyAlreadyProcessedByChild()
            {
                TS_ASSERT_EQUALS(osBack, presenter->ProcessKey(0, kOk, osBack));
            }

            void TestRedKeyForcesUpdateOfSelectedFeed()
            {
                DownloadPoolMock downloadPool;
                serviceLocator.downloadPool = &downloadPool;
                int selectedFeed;
                presenter->Initialize(&menu);

                downloadPool.ExpectMethod("AddDownload", "bar");
                downloadPool.ExpectMethod("AddDownload", "foo");

                selectedFeed = 1;
                TS_ASSERT_EQUALS(osContinue, presenter->ProcessKey(selectedFeed, kRed, osUnknown));

                selectedFeed = 0;
                TS_ASSERT_EQUALS(osContinue, presenter->ProcessKey(selectedFeed, kRed, osUnknown));

                downloadPool.Verify();
            }

            void TestOkOpensItemMenu()
            {
                int selectedFeed = 0;
                presenter->Initialize(&menu);

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

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

                TS_ASSERT_EQUALS(osContinue, presenter->ProcessKey(selectedFeed, kOk, osUnknown));

                menu.Verify();
            }
    };
};
