1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(test): Deduplicate MockCoreSettings

Also add Mock sources to all tests, to handle tests that require both project
resources and mock without complicating auto_test
This commit is contained in:
Anthony Bilinski 2022-02-26 09:18:49 -08:00
parent 12b3b05217
commit 40846f886c
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
5 changed files with 150 additions and 98 deletions

View File

@ -22,9 +22,15 @@
enable_testing()
set(MOCK_SOURCES
test/mock/mockcoreidhandler.cpp
test/mock/mockgroupquery.cpp
test/mock/mockcoresettings.cpp
)
function(auto_test subsystem module extra_deps)
add_executable(test_${module}
test/${subsystem}/${module}_test.cpp ${extra_deps})
test/${subsystem}/${module}_test.cpp ${extra_deps} ${MOCK_SOURCES})
target_link_libraries(test_${module}
${PROJECT_NAME}_static
${CHECK_LIBRARIES}
@ -34,11 +40,6 @@ function(auto_test subsystem module extra_deps)
COMMAND ${TEST_CROSSCOMPILING_EMULATOR} test_${module})
endfunction()
set(MOCK_SOURCES
test/mock/mockcoreidhandler.cpp
test/mock/mockgroupquery.cpp
)
auto_test(core core "${${PROJECT_NAME}_RESOURCES}")
auto_test(core core_online "${${PROJECT_NAME}_RESOURCES}")
auto_test(core contactid "")
@ -56,11 +57,11 @@ if(NOT "${SMILEYS}" STREQUAL "DISABLED")
endif()
auto_test(model friendlistmanager "")
auto_test(model friendmessagedispatcher "")
auto_test(model groupmessagedispatcher "${MOCK_SOURCES}")
auto_test(model groupmessagedispatcher "")
auto_test(model messageprocessor "")
auto_test(model sessionchatlog "")
auto_test(model exiftransform "")
auto_test(model notificationgenerator "${MOCK_SOURCES}")
auto_test(model notificationgenerator "")
auto_test(widget filesform "")
if (UNIX)

View File

@ -1,5 +1,5 @@
/*
Copyright © 2019 by The qTox Project Contributors
Copyright © 2022 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -23,6 +23,7 @@
#include "src/model/ibootstraplistgenerator.h"
#include "src/net/bootstrapnodeupdater.h"
#include "src/persistence/settings.h"
#include "test/mock/mockcoresettings.h"
#include <QSignalSpy>
#include <QtGlobal>
@ -38,54 +39,13 @@ Q_DECLARE_METATYPE(ToxPk)
Q_DECLARE_METATYPE(uint32_t)
Q_DECLARE_METATYPE(Status::Status)
class MockSettings : public QObject, public ICoreSettings
{
Q_OBJECT
public:
MockSettings() {
Q_INIT_RESOURCE(res);
qRegisterMetaType<QList<DhtServer>>("QList<DhtServer>");
qRegisterMetaType<ToxPk>("ToxPk");
qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<Status::Status>("Status::Status");
}
bool getEnableIPv6() const override { return false; }
void setEnableIPv6(bool) override { }
bool getForceTCP() const override { return false; }
void setForceTCP(bool) override { }
bool getEnableLanDiscovery() const override { return false; }
void setEnableLanDiscovery(bool) override { }
QString getProxyAddr() const override { return Addr; }
void setProxyAddr(const QString &Addr) override { this->Addr = Addr; }
ProxyType getProxyType() const override { return type; }
void setProxyType(ProxyType type) override { this->type = type; }
quint16 getProxyPort() const override { return port; }
void setProxyPort(quint16 port) override { this->port = port; }
QNetworkProxy getProxy() const override { return QNetworkProxy(QNetworkProxy::ProxyType::NoProxy); }
SIGNAL_IMPL(MockSettings, enableIPv6Changed, bool enabled)
SIGNAL_IMPL(MockSettings, forceTCPChanged, bool enabled)
SIGNAL_IMPL(MockSettings, enableLanDiscoveryChanged, bool enabled)
SIGNAL_IMPL(MockSettings, proxyTypeChanged, ICoreSettings::ProxyType type)
SIGNAL_IMPL(MockSettings, proxyAddressChanged, const QString& address)
SIGNAL_IMPL(MockSettings, proxyPortChanged, quint16 port)
private:
QList<DhtServer> dhtServerList;
QString Addr;
ProxyType type;
quint16 port;
};
class MockNodeListGenerator : public IBootstrapListGenerator
{
public:
MockNodeListGenerator()
{
qRegisterMetaType<QList<DhtServer>>("QList<DhtServer>");
}
QList<DhtServer> getBootstrapnodes() const override;
};
@ -102,6 +62,8 @@ namespace {
class TestCoreOnline : public QObject
{
Q_OBJECT
public:
TestCoreOnline();
private slots:
void init();
void deinit();
@ -117,6 +79,13 @@ private:
ToxCorePtr bob;
};
TestCoreOnline::TestCoreOnline()
{
qRegisterMetaType<ToxPk>("ToxPk");
qRegisterMetaType<uint32_t>("uint32_t");
qRegisterMetaType<Status::Status>("Status::Status");
}
void TestCoreOnline::init()
{
auto settings = std::unique_ptr<MockSettings>(new MockSettings());

View File

@ -23,6 +23,7 @@
#include "src/net/bootstrapnodeupdater.h"
#include "src/model/ibootstraplistgenerator.h"
#include "src/persistence/settings.h"
#include "test/mock/mockcoresettings.h"
#include <QtTest/QtTest>
#include <QtGlobal>
@ -34,49 +35,6 @@
Q_DECLARE_METATYPE(QList<DhtServer>)
class MockSettings : public QObject, public ICoreSettings
{
Q_OBJECT
public:
MockSettings() {
Q_INIT_RESOURCE(res);
qRegisterMetaType<QList<DhtServer>>("QList<DhtServer>");
}
bool getEnableIPv6() const override { return false; }
void setEnableIPv6(bool) override { }
bool getForceTCP() const override { return false; }
void setForceTCP(bool) override { }
bool getEnableLanDiscovery() const override { return false; }
void setEnableLanDiscovery(bool) override { }
QString getProxyAddr() const override { return Addr; }
void setProxyAddr(const QString &Addr) override { this->Addr = Addr; }
ProxyType getProxyType() const override { return type; }
void setProxyType(ProxyType type) override { this->type = type; }
quint16 getProxyPort() const override { return port; }
void setProxyPort(quint16 port) override { this->port = port; }
QNetworkProxy getProxy() const override { return QNetworkProxy(QNetworkProxy::ProxyType::NoProxy); }
SIGNAL_IMPL(MockSettings, enableIPv6Changed, bool enabled)
SIGNAL_IMPL(MockSettings, forceTCPChanged, bool enabled)
SIGNAL_IMPL(MockSettings, enableLanDiscoveryChanged, bool enabled)
SIGNAL_IMPL(MockSettings, proxyTypeChanged, ICoreSettings::ProxyType type)
SIGNAL_IMPL(MockSettings, proxyAddressChanged, const QString& address)
SIGNAL_IMPL(MockSettings, proxyPortChanged, quint16 port)
private:
QList<DhtServer> dhtServerList;
QString Addr;
ProxyType type;
quint16 port;
};
class MockNodeListGenerator : public IBootstrapListGenerator
{
QList<DhtServer> getBootstrapnodes() const override;

View File

@ -0,0 +1,22 @@
/*
Copyright © 2022 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "test/mock/mockcoresettings.h"
MockSettings::~MockSettings() = default;

View File

@ -0,0 +1,102 @@
/*
Copyright © 2022 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "src/core/dhtserver.h"
#include "src/core/icoresettings.h"
#include "src/core/toxpk.h"
#include "src/model/status.h"
#include <QList>
#include <QObject>
class MockSettings : public QObject, public ICoreSettings
{
Q_OBJECT
public:
MockSettings()
{
Q_INIT_RESOURCE(res);
}
~MockSettings();
bool getEnableIPv6() const override
{
return false;
}
void setEnableIPv6(bool) override {}
bool getForceTCP() const override
{
return false;
}
void setForceTCP(bool) override {}
bool getEnableLanDiscovery() const override
{
return false;
}
void setEnableLanDiscovery(bool) override {}
QString getProxyAddr() const override
{
return Addr;
}
void setProxyAddr(const QString& Addr) override
{
this->Addr = Addr;
}
ProxyType getProxyType() const override
{
return type;
}
void setProxyType(ProxyType type) override
{
this->type = type;
}
quint16 getProxyPort() const override
{
return port;
}
void setProxyPort(quint16 port) override
{
this->port = port;
}
QNetworkProxy getProxy() const override
{
return QNetworkProxy(QNetworkProxy::ProxyType::NoProxy);
}
SIGNAL_IMPL(MockSettings, enableIPv6Changed, bool enabled)
SIGNAL_IMPL(MockSettings, forceTCPChanged, bool enabled)
SIGNAL_IMPL(MockSettings, enableLanDiscoveryChanged, bool enabled)
SIGNAL_IMPL(MockSettings, proxyTypeChanged, ICoreSettings::ProxyType type)
SIGNAL_IMPL(MockSettings, proxyAddressChanged, const QString& address)
SIGNAL_IMPL(MockSettings, proxyPortChanged, quint16 port)
private:
QString Addr;
ProxyType type;
quint16 port;
};