/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
#include <QtTest/QtTest>
#include "util.h"
#include "lineedit.h"
#include <QApplication>
#include <QWidget>
#include <QLineEdit>

static QString xml = "<lineedit name=\"mylineedit\"/>\n";

class TestLineEdit: public QObject
{
Q_OBJECT
private slots:
  void creation() { TEST_CREATION(LineEdit); }

  void memleak() {
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    MacroWindow *w = createMacroWindow();
    TEST_MEMLEAK(LineEdit le(e, w));
    delete w;
  }

  void disable() { TEST_DISABLE(LineEdit); }
  void value() { TEST_VALUE(LineEdit); }
  void edit()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    LineEdit le(e, createMacroWindow());
		QString teststring("hello");
    QTest::keyClicks(le.qwidget(), teststring);
    QCOMPARE(le.value(), teststring);
  }

	void changeEmits()
	{
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    LineEdit le(e, createMacroWindow());
		QSignalSpy spy(&le, SIGNAL(wasChanged()));
    QTest::keyClicks(le.qwidget(), "h");
		QCOMPARE(spy.count(), 1);
	}

  void tabOut()
  {
		QDomDocument doc;	doc.setContent(xml);
    QDomElement e = doc.documentElement();
    QWidget main;
    LineEdit le(e, createMacroWindow());
    le.qwidget()->setParent(&main);
    QLineEdit qle(&main);
    main.show();

    QApplication::processEvents();
 
    QTest::mouseClick(le.qwidget(), Qt::LeftButton);
    QApplication::processEvents();
    QCOMPARE(QApplication::focusWidget(), le.qwidget());
    QTest::keyClick(le.qwidget(), Qt::Key_Enter);
    QApplication::processEvents();
    QVERIFY(QApplication::focusWidget() != le.qwidget());

    QTest::mouseClick(le.qwidget(), Qt::LeftButton);
    QApplication::processEvents();
    QCOMPARE(QApplication::focusWidget(), le.qwidget());
    QTest::keyClick(le.qwidget(), Qt::Key_Return);
    QApplication::processEvents();
    QVERIFY(QApplication::focusWidget() != le.qwidget());
 
    QTest::mouseClick(le.qwidget(), Qt::LeftButton);
    QApplication::processEvents();
    QCOMPARE(QApplication::focusWidget(), le.qwidget());
    QTest::keyClick(le.qwidget(), Qt::Key_Tab);
    QApplication::processEvents();
    QVERIFY(QApplication::focusWidget() != le.qwidget());
  }
};

QTEST_MAIN(TestLineEdit)
#include "testlineedit.moc"