/***************************************************************************
 * tabsongtest.cpp
 *
 * This file is part of KTabEdit, a KDE tabulature editor
 *
 * copyright (C) 2004-2007 the KTabEdit development team <mrgwen@gmail.com>
 ***************************************************************************/

/***************************************************************************
 * 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.
 *
 * See the file COPYING for more information.
 ***************************************************************************/

#include "trackpagelisttest.h"

#include "src/header.h"

#include "src/tablature/tabbeateffects.h"
#include "src/tablature/tabchord.h"
#include "src/tablature/tabduration.h"

#include "src/tablature/tabbend.h"
#include "src/tablature/tabdynamic.h"

#include "src/tablature/tabgracenotetransition.h"
#include "src/tablature/tabgracenote.h"

#include "src/tablature/tabharmonic.h"
#include "src/tablature/tabslide.h"
#include "src/tablature/tabtrill.h"
#include "src/tablature/tabnoteeffect.h"

#include "src/tablature/tabnote.h"

#include "src/tablature/tabmixtablechanges.h"
#include "src/tablature/tabtimes.h"
#include "src/tablature/tabbarproperties.h"

#include "src/tablature/tabbar.h"

#include "src/tablature/trackmidiproperties.h"
#include "src/tablature/timesignature.h"
#include "src/tablature/trackproperties.h"

#include "src/tablature/tabtrack.h"

#include "src/tablature/songkey.h"
#include "src/tablature/tablyrics.h"
#include "src/tablature/tabproperties.h"

#include "src/tablature/tabsong.h"
#include "src/tablature/trackpos.h"

#include "src/track/trackpageview.h"
#include "src/track/trackpagelist.h"

const uint TrackPageListTest::sDpi = 72;
const Cm TrackPageListTest::sMargins = Cm(1.00, sDpi);
const QSizeCm TrackPageListTest::sPageSize = QSizeCm(Cm(21.0, sDpi), Cm(29.7, sDpi));

TrackPageView* TrackPageListTest::createPage(uint pageNumber) const
{
	TrackPageView* trackPage = new TrackPageView(sDpi);
	QPointCm position = QPointCm(Cm(0.0, sDpi), sPageSize.height() * pageNumber);
	
	trackPage->setPageSize(sPageSize);
	trackPage->setLeftMargins(Cm(1.00, sDpi));
	trackPage->setTopMargins(sMargins);
	trackPage->setPageNumber(pageNumber);
	trackPage->setPosition(position);
	
	return trackPage;
}

void TrackPageListTest::testGraphicsScene()
{
	TrackPageList pageList;
	QGraphicsScene* scene = new QGraphicsScene();
	
	pageList.setGraphicsScene(scene);
	QCOMPARE( pageList.graphicsScene(), scene );
	
	try {
		pageList.setGraphicsScene(0);
	} catch (const ktabedit::ContractException& e) {
	}
	
	QCOMPARE( pageList.graphicsScene(), scene );
	
	delete scene;
}

void TrackPageListTest::testSongProperties()
{
	TrackPageList pageList;
	TabProperties* tabProperties = new TabProperties;
	TabProperties* properties = new TabProperties;
	
	properties->setTitle("Pruple Haze");
	properties->setArtist("Jimi Hendrix");
	
	try {
		pageList.setSongProperties(properties);
	} catch (const ktabedit::ContractException& e) {
	}
	
	QCOMPARE( pageList.songProperties(), tabProperties );
	
	pageList.push_back(new TrackPageView(sDpi));
	
	try {
		pageList.setSongProperties(properties);
	} catch (const ktabedit::ContractException& e) {
	}
	
	QCOMPARE( pageList.songProperties(), properties );
}

void TrackPageListTest::testTabTrack()
{
	TrackPageList pageList;
	TabTrack* tabTrack = new TabTrack;
	
	tabTrack->setName("Guitare 1");
	
	pageList.setTabTrack(tabTrack);
	QCOMPARE( pageList.tabTrack(), tabTrack );
}

void TrackPageListTest::testPageSize()
{
	TrackPageList pageList;
	
	pageList.setPageSize(sPageSize);
	QCOMPARE( pageList.pageSize(), sPageSize );
}

void TrackPageListTest::testMargins()
{
	TrackPageList pageList;
	const Cm leftMargins(1.5, sDpi);
	const Cm bottomMargins(2.5, sDpi);
	
	pageList.setMargins(leftMargins, bottomMargins);
	QCOMPARE( pageList.leftMargins(), leftMargins );
	QCOMPARE( pageList.bottomMargins(), bottomMargins );
	
	try {
		pageList.setMargins(Cm(-1, sDpi), Cm(-1, sDpi));
	} catch (const ktabedit::ContractException& e) {
	}
	
	QCOMPARE( pageList.leftMargins(), leftMargins );
	QCOMPARE( pageList.bottomMargins(), bottomMargins );
}

void TrackPageListTest::testDpi()
{
	TrackPageList pageList;
	
	pageList.setDpi(sDpi);
	QCOMPARE( pageList.dpi(), sDpi );
}

void TrackPageListTest::testPush()
{
	TrackPageList pageList;
	TrackPageView* pageView1 = new TrackPageView(sDpi);
	TrackPageView* pageView2 = new TrackPageView(sDpi);
	
	try {
		pageList.push_front(0);
	} catch (const ktabedit::ContractException& e) {
	}
	
	try {
		pageList.push_back(static_cast<TrackPageView*>(0));
	} catch (const ktabedit::ContractException& e) {
	}
	
	QCOMPARE( pageList.size(), 0 );
	
	pageList.push_front(pageView1);
	pageList.push_front(pageView2);
	
	QCOMPARE( pageList.size(), 2 );
	QCOMPARE( pageList.back(), pageView1 );
	QCOMPARE( pageList.front(), pageView2 );
}

void TrackPageListTest::testPage()
{
	TrackPageList pageList;
	TrackPageList::iterator it = pageList.begin();
	TrackPageList::iterator it2 = pageList.begin();
	
	try {
		it = pageList.page(0);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( it, it2 );
	
	try {
		it = pageList.page(1);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( it, it2 );
	
	pageList.push_front(new TrackPageView(sDpi));
	it2 = pageList.begin();
	try {
		it = pageList.page(1);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( it, it2 );
}

void TrackPageListTest::testPageConst() const
{
	TrackPageList pageList;
	TrackPageList::const_iterator it = pageList.begin();
	TrackPageList::const_iterator it2 = pageList.begin();
	
	try {
		it = pageList.page(0);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( it, it2 );
	
	try {
		it = pageList.page(1);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( it, it2 );
	
	pageList.push_front(new TrackPageView(sDpi));
	it2 = pageList.begin();
	try {
		it = pageList.page(1);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( it, it2 );
}

void TrackPageListTest::testBarAt()
{
	TrackPageList pageList;
	TrackPos pos(0,0,0);
	TrackBarView* bar = 0;
	
	try {
		pageList.barAt(pos);
	} catch (const ktabedit::ContractException& e) {
	}
	QCOMPARE( bar, static_cast<TrackBarView*>(0) );
}

void TrackPageListTest::testIsObscured()
{
	TrackPageList pageList;
	TrackPageView* pageOne = new TrackPageView(sDpi);
	TrackPageView* pageTwo = new TrackPageView(sDpi);
	
	pageList.push_front(pageOne);
	pageList.push_front(pageTwo);
	
	QCOMPARE( pageList.isObscured(), true );
	
	pageList.clear();
	pageOne = createPage(0);
	pageTwo = createPage(1);
	
	pageList.push_front(pageOne);
	pageList.push_front(pageTwo);
	
	QCOMPARE( pageList.isObscured(), false );
}

QTEST_MAIN(TrackPageListTest)
#include "trackpagelisttest.moc"
