/***************************************************************************
*   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
*   geiseri@yahoo.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.                                   *
*                                                                         *
*   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.             *
***************************************************************************/
#include "spellingtests.h"
#include "qtextdocumentspellchecker.h"
#include "spellchecker.h"
#include "qaspell.h"
#include "plaintextedit.h"
#include "block.h"
#include "mindmap.h"
#include "blockpropertymodel.h"
#include "mindmapspellchecker.h"
#include <QtTest/QtTest>
#include <QTextDocument>
#include <QTextCursor>
#include <QUndoStack>
#include <QGraphicsScene>

QTEST_MAIN(SpellingTests);

SpellingTests::SpellingTests(QObject *parent)
		: QObject(parent)
{}


SpellingTests::~SpellingTests()
{}

void SpellingTests::testCheckQString( )
{
	QString word = "cheese";
	QString wordd = "chese";

	QASpell spell_checker;
	QVERIFY( spell_checker.configureSpellchecker( "en_US" ) );
	QVERIFY( spell_checker.checkWord(word));
	QVERIFY( !spell_checker.checkWord(wordd));

}

void SpellingTests::testGetSuggestions( )
{
	QString wordd = "chese";
	QStringList suggestionList;

	QASpell spell_checker;
	QVERIFY(spell_checker.configureSpellchecker( "en_US" ) );

	suggestionList = spell_checker.getSuggestions( wordd );

	QCOMPARE(suggestionList.value(0), QString("cheese"));
}

void SpellingTests::testSearchReplaceQTextDocument( )
{
	QTextDocument doc;
	doc.setPlainText("This is a test of a document, that I am going to walk.");
	QTextCursor cursor( &doc );
	QTextDocumentSpellChecker spellchecker(&doc);
	spellchecker.replaceStringInDocument(cursor, "walk", "test");

	QCOMPARE( doc.toPlainText(), QString("This is a test of a document, that I am going to test."));
}

void SpellingTests::testSearchReplaceMultiple( )
{
	QTextDocument doc;
	doc.setPlainText("This is a test of a document, that I am going to test.");
	QTextCursor cursor( &doc );
	QTextDocumentSpellChecker spellchecker(&doc);
	spellchecker.replaceStringInDocument(cursor, "test", "foo", true);

	QCOMPARE( doc.toPlainText(), QString("This is a foo of a document, that I am going to foo."));
}

void SpellingTests::testSearchReplaceOne( )
{
	QTextDocument doc;
	doc.setPlainText("This is a test of a document, that I am going to test.");
	QTextCursor cursor( &doc );
	QTextDocumentSpellChecker spellchecker(&doc);
	spellchecker.replaceStringInDocument( cursor, "test", "foo", false);

	QCOMPARE( doc.toPlainText(), QString("This is a foo of a document, that I am going to test."));
}

void SpellingTests::testFindMisspellingInDocument( )
{
	QTextDocument doc;
	doc.setPlainText("This is a tesst of a document, that I am going to test.");
	QTextCursor cursor( &doc );
	QTextDocumentSpellChecker spellchecker(&doc);

	QCOMPARE( spellchecker.findNextMisspelling(cursor).position(), 10);
}

void SpellingTests::testFixMisspellingInDocument( )
{
	QTextDocument doc;
	doc.setPlainText("This is a tesst of a document, that I am going to test.");
	QTextCursor cursor( &doc );
	QTextDocumentSpellChecker spellchecker(&doc);

	QTextCursor misspelledCursor = spellchecker.findNextMisspelling(cursor);
	misspelledCursor.select(QTextCursor::WordUnderCursor);
	QString replace = misspelledCursor.selectedText();

	QVERIFY(spellchecker.replaceStringInDocument( misspelledCursor, replace, "test" ));
	QCOMPARE( doc.toPlainText(), QString("This is a test of a document, that I am going to test."));
}

void SpellingTests::testSpellingDialog( )
{
	PlainTextEdit doc;
	doc.setPlainText("This is a tesst of a document, that I am going to tsest.");

	doc.show();

	SpellChecker::checkDocument( doc.document(), &doc );

	QCOMPARE( doc.toPlainText(), QString("This is a test of a document, that I am going to test."));
}

void SpellingTests::testASpellHang()
{
	PlainTextEdit doc;
	doc.setPlainText("This is a tst of a document.");

	doc.show();

	SpellChecker::checkDocument( doc.document(), &doc );

	QCOMPARE( doc.toPlainText(), QString("This is a test of a document."));
}

void SpellingTests::testSpellcheckBlock()
{
	Block *block = new Block();
	block->setTitle("Tesst");
	block->setText("This iis a tset");

	MindMap map;
	block->setMindmap(&map);
	map.scene()->addItem(block);

	map.setRoot( block );

	MindMapSpellChecker checker( &map );
	checker.replaceStringInProperty(block, "title", "Tesst", "Test");
	checker.replaceStringInProperty(block, "text", "iis", "is");
	checker.replaceStringInProperty(block, "text", "tset", "test");

	QCOMPARE( block->title(), QString("Test"));
	QCOMPARE( block->text(), QString("This is a test"));

}

