/***************************************************************************
 * 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 "songmodeltest.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/songmodel.h"

#include <QMetaType>
#include <QSignalSpy>

const QString SongModelTest::title("title");
const QString SongModelTest::subtitle("subtitle");
const QString SongModelTest::artist("artist");
const QString SongModelTest::album("album");
const QString SongModelTest::author("author");
const QString SongModelTest::copyright("copyright");
const QString SongModelTest::transcriber("transcriber");
const QString SongModelTest::instructions("instructions");
const QString SongModelTest::comments("comments");
const QString SongModelTest::majorKey("C");
const QString SongModelTest::minorKey("Am");
const QString SongModelTest::lyrics("ThisIsASample");
const uint SongModelTest::tempo = 80;
const int SongModelTest::octave = 0;
const int SongModelTest::lyricsTrack = 1;
const int SongModelTest::lyricsMeasure = 2;

TabProperties SongModelTest::buildProperties() const
{
	TabProperties tabProperties;
	
	tabProperties.setTitle(title);
	tabProperties.setSubTitle(subtitle);
	tabProperties.setArtist(artist);
	tabProperties.setAlbum(album);
	tabProperties.setAuthor(author);
	tabProperties.setCopyright(copyright);
	tabProperties.setTranscriber(transcriber);
	tabProperties.setInstructions(instructions);
	tabProperties.setComments(comments);
	tabProperties.setShuffle(true);
	tabProperties.setTempo(tempo);
	tabProperties.setOctave(octave);
	tabProperties.setKey(0);
	tabProperties.setMinor(true);
	tabProperties.setLyricsTrack(lyricsTrack);
	tabProperties.setLyricsMeasure(lyricsTrack, lyricsMeasure);
	tabProperties.setLyrics(lyricsMeasure, lyrics);
	
	return tabProperties;
}

void SongModelTest::initTestCase()
{
//	qRegisterMetaType<TabProperties>("TabProperties");
//	qRegisterMetaType<TabTrack>("TabTrack");
}

void SongModelTest::testProperties()
{
	SongModel songModel;
	QSignalSpy spy(&songModel, SIGNAL(songPropertertiesChanged(const TabProperties&)));
	
	songModel.setTitle(title);
	QCOMPARE( songModel.title(), title );
	
	songModel.setSubTitle(subtitle);
	QCOMPARE( songModel.subTitle(), subtitle );
	
	songModel.setArtist(artist);
	QCOMPARE( songModel.artist(), artist );
	
	songModel.setAlbum(album);
	QCOMPARE( songModel.album(), album );
	
	songModel.setAuthor(author);
	QCOMPARE( songModel.author(), author );
	
	songModel.setCopyright(copyright);
	QCOMPARE( songModel.copyright(), copyright );
	
	songModel.setTranscriber(transcriber);
	QCOMPARE( songModel.transcriber(), transcriber );
	
	songModel.setInstructions(instructions);
	QCOMPARE( songModel.instructions(), instructions );
	
	songModel.setComments(comments);
	QCOMPARE( songModel.comments(), comments );
	
	songModel.setShuffle(true);
	QCOMPARE( songModel.isShuffle(), true );
	
	songModel.setTempo(tempo);
	QCOMPARE( songModel.tempo(), tempo );
	
	songModel.setOctave(octave);
	QCOMPARE( songModel.octave(), octave );
	
	songModel.setKey(0);
	QCOMPARE( songModel.nameOfKey(), majorKey );
	
	songModel.setMinor(true);
	QCOMPARE( songModel.nameOfKey(), minorKey );
	
	songModel.setLyricsTrack(lyricsTrack);
	QCOMPARE( songModel.lyricsTrack(), lyricsTrack );
	
	songModel.setLyricsMeasure(lyricsTrack, lyricsMeasure);
	QCOMPARE( songModel.lyricsMeasure(lyricsTrack), lyricsMeasure );
	
	songModel.setLyrics(lyricsMeasure, lyrics);
	QCOMPARE( songModel.lyrics(lyricsMeasure), lyrics );
	
	//QCOMPARE( spy.count(), 17);
	//QCOMPARE( qvariant_cast<TabProperties>(spy.at(16).at(0)), buildProperties() );
}

void SongModelTest::testTabProperties()
{
	TabProperties* tabProperties = new TabProperties(buildProperties());
	SongModel songModel;
	QSignalSpy spy(&songModel, SIGNAL(songPropertertiesChanged(const TabProperties&)));
	
	songModel.setProperties(tabProperties);
	QCOMPARE( songModel.properties(), tabProperties );
	
	//QCOMPARE( spy.count(), 1);
	//QCOMPARE( qvariant_cast<TabProperties>(spy.at(0).at(0)), buildProperties() );
}

void SongModelTest::testSongKeyProperties()
{
	TabProperties tabProperties;
	SongKey* songKey = new SongKey;
	SongModel songModel;
	QSignalSpy spy(&songModel, SIGNAL(songPropertertiesChanged(const TabProperties&)));
	
	songKey->setKey(0);
	songKey->setMinor(true);
	
	songModel.setSongKey(songKey);
	QCOMPARE( songModel.songKey(), songKey );
	
	tabProperties.setSongKey(songKey);
	QCOMPARE( spy.count(), 1);
//	QCOMPARE( qvariant_cast<TabProperties>(spy.at(0).at(0)), tabProperties );
}

void SongModelTest::testLyricsProperties()
{
	TabProperties tabProperties;
	TabLyrics* lyrics = new TabLyrics;
	SongModel songModel;
	QSignalSpy spy(&songModel, SIGNAL(songPropertertiesChanged(const TabProperties&)));
	
	lyrics->setLyricsTrack(lyricsTrack);
	lyrics->setLyricsMeasure(lyricsTrack, lyricsMeasure);
	lyrics->setLyrics(lyricsMeasure, SongModelTest::lyrics);
	
	songModel.setTabLyrics(lyrics);
	QCOMPARE( songModel.tabLyrics(), lyrics );
	
	tabProperties.setTabLyrics(lyrics);
	//QCOMPARE( spy.count(), 1);
	//QCOMPARE( qvariant_cast<TabProperties>(spy.at(0).at(0)), tabProperties );
}

void SongModelTest::TestOperatorEqual()
{
	SongModel songModel;
	TabSong tabSong;
	QSignalSpy spySongDeleted(&songModel, SIGNAL(songDeleted()));
	QSignalSpy spyTrackAdded(&songModel, SIGNAL(trackAdded(TabTrack*)));
	
	tabSong.setTitle(title);
	tabSong.setSubTitle(subtitle);
	tabSong.push_front(new TabTrack);
	tabSong.push_front(new TabTrack);
	
	songModel = tabSong;
	QCOMPARE( *dynamic_cast<const TabSong*>(&songModel), tabSong );
	
	QCOMPARE( spySongDeleted.count(), 1);
	QCOMPARE( spyTrackAdded.count(), 2);
}

void SongModelTest::testPushFront()
{
/*	TabSong tempSong("TITLE", 120);
	TabTrack* tempTrack = new TabTrack;
	
	songModel.push_front(tempTrack);
	QCOMPARE( songModel.front(), tempTrack );
	QCOMPARE( songModel.size(), (size_t)1 );
	
	try {
		songModel.push_front(0x0);
	} catch(const std::exception& e) {
	}
	
	QCOMPARE( songModel.front(), tempTrack );
	QCOMPARE( songModel.size(), (size_t)1 );
	*/
}

void SongModelTest::testAt()
{
	/*TabSong tempSong("TITLE", 120);
	TabTrack* tempTrack = new TabTrack;
	TabTrack* nullTrack;
	
	songModel.push_front(tempTrack);
	QCOMPARE( songModel.at(0), tempTrack );
	
	try {
		nullTrack = songModel.at(1);
	} catch (const std::exception& e) {
		nullTrack = 0x0;
	}
	
	QCOMPARE( nullTrack, static_cast<TabTrack*>(0x0) );*/
}

void SongModelTest::testDrumTrack()
{
	/*TabSong tempSong("TITLE", 120);
	TabTrack* tempTrack = new TabTrack;
	
	songModel.push_front(tempTrack);
	tempTrack = new TabTrack;
	tempTrack->setMode(DrumTab);
	songModel.push_front(tempTrack);
	
	QCOMPARE( songModel.isDrumTrack(), true );
	QCOMPARE( songModel.drumTrack(), 1 );*/
}

QTEST_MAIN(SongModelTest)
#include "songmodeltest.moc"
