diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 9e480bc7b..17bed3116 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,16 +1,24 @@ remove_definitions(-DQT_NO_CAST_FROM_ASCII) include(ECMAddTests) find_package(Qt5Test ${REQUIRED_QT_VERSION} CONFIG QUIET) if(NOT Qt5Test_FOUND) message(STATUS "Qt5Test not found, autotests will not be built.") return() endif() -ecm_add_tests( +ecm_add_test( flatkeylistmodeltest.cpp + abstractkeylistmodeltest.cpp + TEST_NAME flatkeylistmodeltest + LINK_LIBRARIES KF5::Libkleo Qt5::Test +) + +ecm_add_test( hierarchicalkeylistmodeltest.cpp + abstractkeylistmodeltest.cpp + TEST_NAME hierarchicalkeylistmodeltest LINK_LIBRARIES KF5::Libkleo Qt5::Test ) diff --git a/autotests/flatkeylistmodeltest.cpp b/autotests/abstractkeylistmodeltest.cpp similarity index 65% copy from autotests/flatkeylistmodeltest.cpp copy to autotests/abstractkeylistmodeltest.cpp index a646effd4..f9ab1527e 100644 --- a/autotests/flatkeylistmodeltest.cpp +++ b/autotests/abstractkeylistmodeltest.cpp @@ -1,73 +1,63 @@ /* - autotests/flatkeylistmodeltest.cpp + autotests/abstractkeylistmodeltest.cpp This file is part of libkleopatra's test suite. SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ +#include "abstractkeylistmodeltest.h" + #include "kleo/keygroup.h" #include "models/keylistmodel.h" #include #include -class FlatKeyListModelTest: public QObject -{ - Q_OBJECT -private Q_SLOTS: - void testCreation(); - void testSetGroups(); - void testClear(); -}; - using namespace Kleo; using namespace GpgME; -void FlatKeyListModelTest::testCreation() +void AbstractKeyListModelTest::testCreation() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); QCOMPARE( model->rowCount(), 0 ); } -void FlatKeyListModelTest::testSetGroups() +void AbstractKeyListModelTest::testSetGroups() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); const std::vector groups = { KeyGroup("test1", std::vector()) }; model->setGroups(groups); QCOMPARE( model->rowCount(), 1 ); QVERIFY( model->index(groups[0]).isValid() ); const std::vector otherGroups = { KeyGroup("test2", std::vector()), KeyGroup("test3", std::vector()) }; model->setGroups(otherGroups); QCOMPARE( model->rowCount(), 2 ); QVERIFY( model->index(otherGroups[0]).isValid() ); QVERIFY( model->index(otherGroups[1]).isValid() ); QVERIFY( !model->index(groups[0]).isValid() ); } -void FlatKeyListModelTest::testClear() +void AbstractKeyListModelTest::testClear() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); const KeyGroup group("test", std::vector()); model->setGroups({group}); model->clear(AbstractKeyListModel::Keys); QCOMPARE( model->rowCount(), 1 ); model->clear(AbstractKeyListModel::Groups); QCOMPARE( model->rowCount(), 0 ); } - -QTEST_MAIN(FlatKeyListModelTest) -#include "flatkeylistmodeltest.moc" diff --git a/autotests/abstractkeylistmodeltest.h b/autotests/abstractkeylistmodeltest.h new file mode 100644 index 000000000..ed00ba5e2 --- /dev/null +++ b/autotests/abstractkeylistmodeltest.h @@ -0,0 +1,33 @@ +/* + autotests/abstractkeylistmodeltest.h + + This file is part of libkleopatra's test suite. + SPDX-FileCopyrightText: 2021 g10 Code GmbH + SPDX-FileContributor: Ingo Klöcker + + SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef ABSTRACTKEYLISTMODELTEST_H +#define ABSTRACTKEYLISTMODELTEST_H + +#include + +namespace Kleo +{ +class AbstractKeyListModel; +} + +class AbstractKeyListModelTest: public QObject +{ + Q_OBJECT +private Q_SLOTS: + void testCreation(); + void testSetGroups(); + void testClear(); + +private: + virtual Kleo::AbstractKeyListModel *createModel() = 0; +}; + +#endif // ABSTRACTKEYLISTMODELTEST_H diff --git a/autotests/flatkeylistmodeltest.cpp b/autotests/flatkeylistmodeltest.cpp index a646effd4..f78b4b143 100644 --- a/autotests/flatkeylistmodeltest.cpp +++ b/autotests/flatkeylistmodeltest.cpp @@ -1,73 +1,31 @@ /* autotests/flatkeylistmodeltest.cpp This file is part of libkleopatra's test suite. SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ -#include "kleo/keygroup.h" -#include "models/keylistmodel.h" +#include "abstractkeylistmodeltest.h" -#include +#include "models/keylistmodel.h" #include -class FlatKeyListModelTest: public QObject -{ - Q_OBJECT -private Q_SLOTS: - void testCreation(); - void testSetGroups(); - void testClear(); -}; - using namespace Kleo; -using namespace GpgME; - -void FlatKeyListModelTest::testCreation() -{ - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - QCOMPARE( model->rowCount(), 0 ); -} -void FlatKeyListModelTest::testSetGroups() +class FlatKeyListModelTest: public AbstractKeyListModelTest { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - - const std::vector groups = { - KeyGroup("test1", std::vector()) - }; - model->setGroups(groups); - QCOMPARE( model->rowCount(), 1 ); - QVERIFY( model->index(groups[0]).isValid() ); - - const std::vector otherGroups = { - KeyGroup("test2", std::vector()), - KeyGroup("test3", std::vector()) - }; - model->setGroups(otherGroups); - QCOMPARE( model->rowCount(), 2 ); - QVERIFY( model->index(otherGroups[0]).isValid() ); - QVERIFY( model->index(otherGroups[1]).isValid() ); - QVERIFY( !model->index(groups[0]).isValid() ); -} - -void FlatKeyListModelTest::testClear() -{ - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - model->setGroups({group}); - - model->clear(AbstractKeyListModel::Keys); - QCOMPARE( model->rowCount(), 1 ); + Q_OBJECT - model->clear(AbstractKeyListModel::Groups); - QCOMPARE( model->rowCount(), 0 ); -} +private: + AbstractKeyListModel *createModel() override + { + return AbstractKeyListModel::createFlatKeyListModel(this); + } +}; QTEST_MAIN(FlatKeyListModelTest) #include "flatkeylistmodeltest.moc" diff --git a/autotests/hierarchicalkeylistmodeltest.cpp b/autotests/hierarchicalkeylistmodeltest.cpp index cc998337a..cabd86658 100644 --- a/autotests/hierarchicalkeylistmodeltest.cpp +++ b/autotests/hierarchicalkeylistmodeltest.cpp @@ -1,73 +1,31 @@ /* autotests/hierarchicalkeylistmodeltest.cpp This file is part of libkleopatra's test suite. SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ -#include "kleo/keygroup.h" -#include "models/keylistmodel.h" +#include "abstractkeylistmodeltest.h" -#include +#include "models/keylistmodel.h" #include -class HierarchicalKeyListModelTest: public QObject -{ - Q_OBJECT -private Q_SLOTS: - void testCreation(); - void testSetGroups(); - void testClear(); -}; - using namespace Kleo; -using namespace GpgME; - -void HierarchicalKeyListModelTest::testCreation() -{ - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - QCOMPARE( model->rowCount(), 0 ); -} -void HierarchicalKeyListModelTest::testSetGroups() +class HierarchicalKeyListModelTest: public AbstractKeyListModelTest { - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - - const std::vector groups = { - KeyGroup("test1", std::vector()) - }; - model->setGroups(groups); - QCOMPARE( model->rowCount(), 1 ); - QVERIFY( model->index(groups[0]).isValid() ); - - const std::vector otherGroups = { - KeyGroup("test2", std::vector()), - KeyGroup("test3", std::vector()) - }; - model->setGroups(otherGroups); - QCOMPARE( model->rowCount(), 2 ); - QVERIFY( model->index(otherGroups[0]).isValid() ); - QVERIFY( model->index(otherGroups[1]).isValid() ); - QVERIFY( !model->index(groups[0]).isValid() ); -} - -void HierarchicalKeyListModelTest::testClear() -{ - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - model->setGroups({group}); - - model->clear(AbstractKeyListModel::Keys); - QCOMPARE( model->rowCount(), 1 ); + Q_OBJECT - model->clear(AbstractKeyListModel::Groups); - QCOMPARE( model->rowCount(), 0 ); -} +private: + AbstractKeyListModel *createModel() override + { + return AbstractKeyListModel::createHierarchicalKeyListModel(this); + } +}; QTEST_MAIN(HierarchicalKeyListModelTest) #include "hierarchicalkeylistmodeltest.moc"