/***************************************************************************
 * tabbartest.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 "tabbartest.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"

void TabBarTest::testConstructor()
{
	TabTimes* time = new TabTimes;
	TabBar bar1;
	QCOMPARE( bar1.size(), (uint)0 );
	QCOMPARE( bar1.keySignature(), (uint)0 );
	
	TabBar bar2(4,4);
	bar2.insertTime(time);
	QCOMPARE( bar2.size(), (uint)1 );
	QCOMPARE( bar2.keySignature(), (uint)0 );
	QCOMPARE( bar2.numerator(), (uchar)4 );
	QCOMPARE( bar2.denominator(), (uchar)4 );
	
	TabBar bar3(bar2);
	QCOMPARE( bar3.size(), (uint)1 );
	QCOMPARE( bar3.keySignature(), (uint)0 );
	QCOMPARE( bar3.numerator(), (uchar)4 );
	QCOMPARE( bar3.denominator(), (uchar)4 );
	QCOMPARE( bar2.time(0) != bar3.time(0), true );
	QCOMPARE( *bar2.time(0), *bar3.time(0));
}

void TabBarTest::testKeySignature()
{
	TabBar bar;
	
	bar.setKeySignature(3);
	QCOMPARE( bar.keySignature(), (uint)3 );
}

void TabBarTest::testTime()
{
	bool result;
	TabBar bar;
	TabTimes* time = new TabTimes;
	time->setText("Hello World");
	
	bar.insertTime(time);
	QCOMPARE( bar.time(0), time );
	
	try {
		result = false;
		time = bar.time(5);
	} catch (const ktabedit::ContractException& e) {
		result = true;
	}
	QCOMPARE( result, true );
	
	try {
		result = false;
		bar.deleteTime(5);
	} catch (const ktabedit::ContractException& e) {
		result = true;
	}
	QCOMPARE( result, true );
	
	bar.deleteTime(0);
	QCOMPARE( bar.size(), (uint)0 );
	
	time = new TabTimes;
	bar.insertTime(time);
	bar.setTimeDuration(0, Quarter);
	QCOMPARE( bar.timeDuration(0), Quarter );
	QCOMPARE( bar.timeRealDuration(0), (double)Quarter );
	
	try {
		result = false;
		bar.setTimeDuration(5, Quarter);
	} catch (const ktabedit::ContractException& e) {
		result = true;
	}
	QCOMPARE( result, true );
	
	time->setRest(true);
	QCOMPARE( bar.isRestTime(0), true );
}

void TabBarTest::testNote()
{
	bool result;
	TabBar bar;
	TabTimes* time = new TabTimes;
	TabNote* note = new TabNote;
	
	try {
		result = false;
		bar.setNote(5, 2, note);
	} catch (const ktabedit::ContractException& e) {
		result = true;
	}
	QCOMPARE( result, true );
	
	bar.insertTime(time);
	bar.setNote(0, 2, note);
	QCOMPARE( bar.note(0, 2), note );
	QCOMPARE( bar.isEmptyNote(0, 2), true );
	QCOMPARE( bar.isTieNote(0, 2), false );
	note->setTieNote(true);
	QCOMPARE( bar.isTieNote(0, 2), true );
}

void TabBarTest::testFret()
{
	bool result;
	TabBar bar;
	TabTimes* time = new TabTimes;
	
	try {
		result = false;
		bar.setFret(5, 2, 25);
	} catch (const ktabedit::ContractException& e) {
		result = true;
	}
	QCOMPARE( result, true );
	
	bar.insertTime(time);
	bar.setFret(0, 2, 25);
	QCOMPARE( bar.fret(0, 2), (char)25 );
}

void TabBarTest::testDuration()
{
	TabBar* bar = new TabBar;
	TabTimes* time;
	
	// 4 / 4
	//
	time = new TabTimes;
	time->setDuration(Quarter);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Quarter);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Quarter);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Quarter);
	bar->insertTime(time);
	QCOMPARE( bar->isFull(), true );
	QCOMPARE( bar->isOverTaken(), false );
	
	time = new TabTimes;
	time->setDuration(Quarter);
	bar->insertTime(time);
	QCOMPARE( bar->isFull(), true );
	QCOMPARE( bar->isOverTaken(), true );
	
	delete bar;
	bar = new TabBar(6, 8);
	// 12 / 8
	//
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	QCOMPARE( bar->isFull(), true );
	QCOMPARE( bar->isOverTaken(), false );
	
	time = new TabTimes;
	time->setDuration(Eighth);
	bar->insertTime(time);
	QCOMPARE( bar->isFull(), true );
	QCOMPARE( bar->isOverTaken(), true );
	
	delete bar;
}

void TabBarTest::testOperators()
{
	
}

QTEST_MAIN(TabBarTest)
#include "tabbartest.moc"
