Commit 8401ffee authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Deduplicate tests of key list models

GnuPG-bug-id: 5175, 5238
parent f4709427
......@@ -9,8 +9,16 @@ if(NOT Qt5Test_FOUND)
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
)
/*
autotests/abstractkeylistmodeltest.cpp
This file is part of libkleopatra's test suite.
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "abstractkeylistmodeltest.h"
#include "kleo/keygroup.h"
#include "models/keylistmodel.h"
#include <gpgme++/key.h>
#include <QTest>
using namespace Kleo;
using namespace GpgME;
void AbstractKeyListModelTest::testCreation()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
QCOMPARE( model->rowCount(), 0 );
}
void AbstractKeyListModelTest::testSetGroups()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const std::vector<KeyGroup> groups = {
KeyGroup("test1", std::vector<Key>())
};
model->setGroups(groups);
QCOMPARE( model->rowCount(), 1 );
QVERIFY( model->index(groups[0]).isValid() );
const std::vector<KeyGroup> otherGroups = {
KeyGroup("test2", std::vector<Key>()),
KeyGroup("test3", std::vector<Key>())
};
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 AbstractKeyListModelTest::testClear()
{
QScopedPointer<AbstractKeyListModel> model(createModel());
const KeyGroup group("test", std::vector<Key>());
model->setGroups({group});
model->clear(AbstractKeyListModel::Keys);
QCOMPARE( model->rowCount(), 1 );
model->clear(AbstractKeyListModel::Groups);
QCOMPARE( model->rowCount(), 0 );
}
/*
autotests/abstractkeylistmodeltest.h
This file is part of libkleopatra's test suite.
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef ABSTRACTKEYLISTMODELTEST_H
#define ABSTRACTKEYLISTMODELTEST_H
#include <QObject>
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
......@@ -8,66 +8,24 @@
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kleo/keygroup.h"
#include "models/keylistmodel.h"
#include "abstractkeylistmodeltest.h"
#include <gpgme++/key.h>
#include "models/keylistmodel.h"
#include <QTest>
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<AbstractKeyListModel> model(AbstractKeyListModel::createFlatKeyListModel(this));
QCOMPARE( model->rowCount(), 0 );
}
void FlatKeyListModelTest::testSetGroups()
class FlatKeyListModelTest: public AbstractKeyListModelTest
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createFlatKeyListModel(this));
const std::vector<KeyGroup> groups = {
KeyGroup("test1", std::vector<Key>())
};
model->setGroups(groups);
QCOMPARE( model->rowCount(), 1 );
QVERIFY( model->index(groups[0]).isValid() );
const std::vector<KeyGroup> otherGroups = {
KeyGroup("test2", std::vector<Key>()),
KeyGroup("test3", std::vector<Key>())
};
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<AbstractKeyListModel> model(AbstractKeyListModel::createFlatKeyListModel(this));
const KeyGroup group("test", std::vector<Key>());
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"
......@@ -8,66 +8,24 @@
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kleo/keygroup.h"
#include "models/keylistmodel.h"
#include "abstractkeylistmodeltest.h"
#include <gpgme++/key.h>
#include "models/keylistmodel.h"
#include <QTest>
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<AbstractKeyListModel> model(AbstractKeyListModel::createHierarchicalKeyListModel(this));
QCOMPARE( model->rowCount(), 0 );
}
void HierarchicalKeyListModelTest::testSetGroups()
class HierarchicalKeyListModelTest: public AbstractKeyListModelTest
{
QScopedPointer<AbstractKeyListModel> model(AbstractKeyListModel::createHierarchicalKeyListModel(this));
const std::vector<KeyGroup> groups = {
KeyGroup("test1", std::vector<Key>())
};
model->setGroups(groups);
QCOMPARE( model->rowCount(), 1 );
QVERIFY( model->index(groups[0]).isValid() );
const std::vector<KeyGroup> otherGroups = {
KeyGroup("test2", std::vector<Key>()),
KeyGroup("test3", std::vector<Key>())
};
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<AbstractKeyListModel> model(AbstractKeyListModel::createHierarchicalKeyListModel(this));
const KeyGroup group("test", std::vector<Key>());
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"
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