diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 2016c74ad..17bed3116 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,15 +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 cb069a5d6..3493285c0 100644 --- a/autotests/flatkeylistmodeltest.cpp +++ b/autotests/abstractkeylistmodeltest.cpp @@ -1,105 +1,91 @@ /* - autotests/flatkeylistmodeltest.cpp + 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 */ +#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 testAddGroup_nullGroup(); - void testAddGroup_new(); - void testAddGroup_existing(); - 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::testAddGroup_nullGroup() +void AbstractKeyListModelTest::testAddGroup_nullGroup() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); const KeyGroup emptyGroup; const QModelIndex index = model->addGroup(emptyGroup); QVERIFY( !index.isValid() ); QCOMPARE( model->rowCount(), 0 ); } -void FlatKeyListModelTest::testAddGroup_new() +void AbstractKeyListModelTest::testAddGroup_new() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); const KeyGroup group("test", std::vector()); const QModelIndex index = model->addGroup(group); QVERIFY( index.isValid() ); QCOMPARE( model->rowCount(), 1 ); // group retrieved from model matches added group const KeyGroup g = model->group(index); QCOMPARE( g.name(), group.name() ); QVERIFY( g.keys().empty() ); const QModelIndex retrievedIndex = model->index(group); QVERIFY( retrievedIndex.isValid() ); } -void FlatKeyListModelTest::testAddGroup_existing() +void AbstractKeyListModelTest::testAddGroup_existing() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); const KeyGroup group("test", std::vector()); const QModelIndex index = model->addGroup(group); QVERIFY( index.isValid() ); QCOMPARE( model->rowCount(), 1 ); const KeyGroup updatedGroup("test", std::vector({Key()})); const QModelIndex updatedIndex = model->addGroup(updatedGroup); QVERIFY( updatedIndex.isValid() ); QCOMPARE( model->rowCount(), 1 ); // group retrieved from model matches updated group const KeyGroup g = model->group(updatedIndex); QCOMPARE( g.name(), updatedGroup.name() ); QVERIFY( !g.keys().empty() ); // group is still found in model by looking for initial group const QModelIndex retrievedIndex = model->index(group); QVERIFY( retrievedIndex.isValid() ); } -void FlatKeyListModelTest::testClear() +void AbstractKeyListModelTest::testClear() { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); + QScopedPointer model(createModel()); const KeyGroup group("test", std::vector()); model->addGroup(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..aedb6eff7 --- /dev/null +++ b/autotests/abstractkeylistmodeltest.h @@ -0,0 +1,35 @@ +/* + 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 testAddGroup_nullGroup(); + void testAddGroup_new(); + void testAddGroup_existing(); + void testClear(); + +private: + virtual Kleo::AbstractKeyListModel *createModel() = 0; +}; + +#endif // ABSTRACTKEYLISTMODELTEST_H diff --git a/autotests/flatkeylistmodeltest.cpp b/autotests/flatkeylistmodeltest.cpp index cb069a5d6..a821a7be2 100644 --- a/autotests/flatkeylistmodeltest.cpp +++ b/autotests/flatkeylistmodeltest.cpp @@ -1,105 +1,29 @@ /* 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 +#include "abstractkeylistmodeltest.h" -#include - -class FlatKeyListModelTest: public QObject -{ - Q_OBJECT -private Q_SLOTS: - void testCreation(); - void testAddGroup_nullGroup(); - void testAddGroup_new(); - void testAddGroup_existing(); - void testClear(); -}; +#include "models/keylistmodel.h" using namespace Kleo; -using namespace GpgME; -void FlatKeyListModelTest::testCreation() +class FlatKeyListModelTest: public AbstractKeyListModelTest { - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - QCOMPARE( model->rowCount(), 0 ); -} - -void FlatKeyListModelTest::testAddGroup_nullGroup() -{ - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - - const KeyGroup emptyGroup; - const QModelIndex index = model->addGroup(emptyGroup); - QVERIFY( !index.isValid() ); - QCOMPARE( model->rowCount(), 0 ); -} - -void FlatKeyListModelTest::testAddGroup_new() -{ - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - const QModelIndex index = model->addGroup(group); - QVERIFY( index.isValid() ); - QCOMPARE( model->rowCount(), 1 ); - - // group retrieved from model matches added group - const KeyGroup g = model->group(index); - QCOMPARE( g.name(), group.name() ); - QVERIFY( g.keys().empty() ); - - const QModelIndex retrievedIndex = model->index(group); - QVERIFY( retrievedIndex.isValid() ); -} - -void FlatKeyListModelTest::testAddGroup_existing() -{ - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - const QModelIndex index = model->addGroup(group); - QVERIFY( index.isValid() ); - QCOMPARE( model->rowCount(), 1 ); - - const KeyGroup updatedGroup("test", std::vector({Key()})); - const QModelIndex updatedIndex = model->addGroup(updatedGroup); - QVERIFY( updatedIndex.isValid() ); - QCOMPARE( model->rowCount(), 1 ); - - // group retrieved from model matches updated group - const KeyGroup g = model->group(updatedIndex); - QCOMPARE( g.name(), updatedGroup.name() ); - QVERIFY( !g.keys().empty() ); - - // group is still found in model by looking for initial group - const QModelIndex retrievedIndex = model->index(group); - QVERIFY( retrievedIndex.isValid() ); -} - -void FlatKeyListModelTest::testClear() -{ - QScopedPointer model(AbstractKeyListModel::createFlatKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - model->addGroup(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 dc6ab3c83..3cb23fc5d 100644 --- a/autotests/hierarchicalkeylistmodeltest.cpp +++ b/autotests/hierarchicalkeylistmodeltest.cpp @@ -1,105 +1,29 @@ /* 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 +#include "abstractkeylistmodeltest.h" -#include - -class HierarchicalKeyListModelTest: public QObject -{ - Q_OBJECT -private Q_SLOTS: - void testCreation(); - void testAddGroup_nullGroup(); - void testAddGroup_new(); - void testAddGroup_existing(); - void testClear(); -}; +#include "models/keylistmodel.h" using namespace Kleo; -using namespace GpgME; -void HierarchicalKeyListModelTest::testCreation() +class HierarchicalKeyListModelTest: public AbstractKeyListModelTest { - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - QCOMPARE( model->rowCount(), 0 ); -} - -void HierarchicalKeyListModelTest::testAddGroup_nullGroup() -{ - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - - const KeyGroup emptyGroup; - const QModelIndex index = model->addGroup(emptyGroup); - QVERIFY( !index.isValid() ); - QCOMPARE( model->rowCount(), 0 ); -} - -void HierarchicalKeyListModelTest::testAddGroup_new() -{ - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - const QModelIndex index = model->addGroup(group); - QVERIFY( index.isValid() ); - QCOMPARE( model->rowCount(), 1 ); - - // group retrieved from model matches added group - const KeyGroup g = model->group(index); - QCOMPARE( g.name(), group.name() ); - QVERIFY( g.keys().empty() ); - - const QModelIndex retrievedIndex = model->index(group); - QVERIFY( retrievedIndex.isValid() ); -} - -void HierarchicalKeyListModelTest::testAddGroup_existing() -{ - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - const QModelIndex index = model->addGroup(group); - QVERIFY( index.isValid() ); - QCOMPARE( model->rowCount(), 1 ); - - const KeyGroup updatedGroup("test", std::vector({Key()})); - const QModelIndex updatedIndex = model->addGroup(updatedGroup); - QVERIFY( updatedIndex.isValid() ); - QCOMPARE( model->rowCount(), 1 ); - - // group retrieved from model matches updated group - const KeyGroup g = model->group(updatedIndex); - QCOMPARE( g.name(), updatedGroup.name() ); - QVERIFY( !g.keys().empty() ); - - // group is still found in model by looking for initial group - const QModelIndex retrievedIndex = model->index(group); - QVERIFY( retrievedIndex.isValid() ); -} - -void HierarchicalKeyListModelTest::testClear() -{ - QScopedPointer model(AbstractKeyListModel::createHierarchicalKeyListModel(this)); - - const KeyGroup group("test", std::vector()); - model->addGroup(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"