Index: pimcommon/src/widgets/autotests/CMakeLists.txt =================================================================== --- pimcommon/src/widgets/autotests/CMakeLists.txt +++ pimcommon/src/widgets/autotests/CMakeLists.txt @@ -1,5 +1,12 @@ -ecm_add_test(kactionmenuchangecasetest.cpp ../kactionmenuchangecase.cpp - TEST_NAME kactionmenuchangecasetest - NAME_PREFIX pimcommon-widget - LINK_LIBRARIES Qt5::Test Qt5::Gui KF5::WidgetsAddons KF5::I18n KF5::XmlGui KF5::PimCommon -) +# convenience macro to add qtest unit tests +macro(pimcommon_widget_unittest _source) + get_filename_component(_name ${_source} NAME_WE) + ecm_add_test(${_source} + TEST_NAME ${_name} + NAME_PREFIX "pimcommon_widget" + LINK_LIBRARIES Qt5::Test Qt5::Gui KF5::WidgetsAddons KF5::I18n KF5::XmlGui KF5::PimCommon KF5::Completion + ) +endmacro () + +pimcommon_widget_unittest(kactionmenuchangecasetest.cpp) +pimcommon_widget_unittest(lineeditwithkeywordstest.cpp) Index: pimcommon/src/widgets/autotests/lineeditwithkeywordstest.cpp =================================================================== --- /dev/null +++ pimcommon/src/widgets/autotests/lineeditwithkeywordstest.cpp @@ -0,0 +1,58 @@ +#include "pimcommon/lineeditwithkeywords.h" +#include "qtest.h" +#include "klineedit.h" +#include + +using namespace PimCommon; + +class LineEditWithKeywordsTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void shouldHaveDefaultValue() + { + LineEditWithKeywords lineEdit; + QVERIFY(lineEdit.text().isEmpty()); + KCompletion *comp = lineEdit.completionObject(); + QVERIFY(!comp->items().isEmpty()); + } + + void shouldBeAlwaysSameList() + { + LineEditWithKeywords lineEdit; + QStringList list1; + list1 << lineEdit.completionObject()->items(); + + LineEditWithKeywords lineEdit2; + QStringList list2; + list2 << lineEdit2.completionObject()->items(); + QCOMPARE(list1,list2); + } + + void shouldAddNewWord() + { + LineEditWithKeywords lineEdit; + KCompletion *comp = lineEdit.completionObject(); + + QVERIFY(!comp->items().contains(QStringLiteral("word1"))); + int nb = comp->items().size(); + comp->addItem(QStringLiteral("word1")); + QCOMPARE(comp->items().size(),nb+1); + QVERIFY(comp->items().contains(QStringLiteral("word1"))); + } + + void shouldHaveDefaultListAfterClearHistory() + { + LineEditWithKeywords lineEdit; + KCompletion *comp = lineEdit.completionObject(); + QMetaObject::invokeMethod(&lineEdit, "slotClearHistory"); + + LineEditWithKeywords lineEdit2; + KCompletion *comp2 = lineEdit2.completionObject(); + QVERIFY(!comp->items().isEmpty()); + QCOMPARE(comp->items(),comp2->items()); + } +}; + +QTEST_MAIN(LineEditWithKeywordsTest) +#include "lineeditwithkeywordstest.moc"