Commit f2efa29f authored by Laurent Montel's avatar Laurent Montel 😁

Add autotest + fix widget

parent 5397fd4b
......@@ -21,7 +21,7 @@
#include "recentaddresswidgettest.h"
#include "../recentaddress/recentaddresswidget.h"
#include <KLineEdit>
#include <QPushButton>
#include <QToolButton>
#include <qlistwidget.h>
#include <qtest.h>
#include <qtestmouse.h>
......@@ -41,10 +41,10 @@ void RecentAddressWidgetTest::shouldHaveDefaultValue()
KLineEdit *lineedit = w.findChild<KLineEdit *>(QStringLiteral("line_edit"));
QVERIFY(lineedit);
QPushButton *newButton = w.findChild<QPushButton *>(QStringLiteral("new_button"));
QToolButton *newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
QVERIFY(newButton);
QPushButton *removeButton = w.findChild<QPushButton *>(QStringLiteral("remove_button"));
QToolButton *removeButton = w.findChild<QToolButton *>(QStringLiteral("remove_button"));
QVERIFY(removeButton);
QListWidget *listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
......@@ -72,7 +72,9 @@ void RecentAddressWidgetTest::shouldInformThatItWasChanged()
{
KPIM::RecentAddressWidget w;
QVERIFY(!w.wasChanged());
QPushButton *newButton = w.findChild<QPushButton *>(QStringLiteral("new_button"));
KLineEdit *lineedit = w.findChild<KLineEdit *>(QStringLiteral("line_edit"));
lineedit->setText(QStringLiteral("foo"));
QToolButton *newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
QVERIFY(newButton);
QTest::mouseClick(newButton, Qt::LeftButton);
QVERIFY(w.wasChanged());
......@@ -80,21 +82,39 @@ void RecentAddressWidgetTest::shouldInformThatItWasChanged()
QCOMPARE(listview->count(), 1);
}
void RecentAddressWidgetTest::shouldNotAddMultiEmptyLine()
void RecentAddressWidgetTest::shouldNotAddMultiSameLine()
{
KPIM::RecentAddressWidget w;
KLineEdit *lineedit = w.findChild<KLineEdit *>(QStringLiteral("line_edit"));
QVERIFY(lineedit);
QPushButton *newButton = w.findChild<QPushButton *>(QStringLiteral("new_button"));
QVERIFY(newButton);
QToolButton *newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
QListWidget *listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
QCOMPARE(listview->count(), 0);
lineedit->setText(QStringLiteral("foo"));
QTest::mouseClick(newButton, Qt::LeftButton);
QCOMPARE(listview->count(), 1);
QTest::mouseClick(newButton, Qt::LeftButton);
QCOMPARE(listview->count(), 1);
}
void RecentAddressWidgetTest::shouldNotAddEmptyLine()
{
KPIM::RecentAddressWidget w;
KLineEdit *lineedit = w.findChild<KLineEdit *>(QStringLiteral("line_edit"));
QToolButton *newButton = w.findChild<QToolButton *>(QStringLiteral("new_button"));
QListWidget *listview = w.findChild<QListWidget *>(QStringLiteral("list_view"));
QCOMPARE(listview->count(), 0);
QVERIFY(lineedit->text().isEmpty());
QTest::mouseClick(newButton, Qt::LeftButton);
QCOMPARE(listview->count(), 0);
QTest::mouseClick(newButton, Qt::LeftButton);
QCOMPARE(listview->count(), 0);
lineedit->setText(QStringLiteral("foo"));
QTest::mouseClick(newButton, Qt::LeftButton);
QCOMPARE(listview->count(), 1);
}
......
......@@ -33,7 +33,8 @@ private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAddAddresses();
void shouldInformThatItWasChanged();
void shouldNotAddMultiEmptyLine();
void shouldNotAddMultiSameLine();
void shouldNotAddEmptyLine();
};
#endif // RECENTADDRESSWIDGETTEST_H
......@@ -80,6 +80,17 @@ RecentAddressWidget::~RecentAddressWidget()
void RecentAddressWidget::slotAddItem()
{
const QString newEmail = mLineEdit->text();
if (newEmail.isEmpty()) {
return;
}
const int numberOfItem(mListView->count());
for (int i = 0; i < numberOfItem; ++i) {
if (mListView->item(i)->text() == newEmail) {
return;
}
}
mListView->insertItem(0, mLineEdit->text());
mListView->setCurrentRow(0, QItemSelectionModel::ClearAndSelect);
mLineEdit->clear();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment