ActionsHandler test
Add basic test suite to make sure that ActionsHandler rejects attempts to handleMessage if either m_room or chatBarCacher are nullptr.
This commit is contained in:
@@ -52,3 +52,9 @@ ecm_add_test(
|
|||||||
LINK_LIBRARIES neochat Qt::Test
|
LINK_LIBRARIES neochat Qt::Test
|
||||||
TEST_NAME messageeventmodeltest
|
TEST_NAME messageeventmodeltest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ecm_add_test(
|
||||||
|
actionshandlertest.cpp
|
||||||
|
LINK_LIBRARIES neochat Qt::Test
|
||||||
|
TEST_NAME actionshandlertest
|
||||||
|
)
|
||||||
|
|||||||
43
autotests/actionshandlertest.cpp
Normal file
43
autotests/actionshandlertest.cpp
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||||
|
|
||||||
|
#include <QTest>
|
||||||
|
|
||||||
|
#include "actionshandler.h"
|
||||||
|
#include "chatbarcache.h"
|
||||||
|
|
||||||
|
#include "testutils.h"
|
||||||
|
|
||||||
|
class ActionsHandlerTest : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private:
|
||||||
|
Quotient::Connection *connection = Quotient::Connection::makeMockConnection(QStringLiteral("@bob:kde.org"));
|
||||||
|
ActionsHandler *actionsHandler = new ActionsHandler(this);
|
||||||
|
|
||||||
|
private Q_SLOTS:
|
||||||
|
void nullObject();
|
||||||
|
};
|
||||||
|
|
||||||
|
void ActionsHandlerTest::nullObject()
|
||||||
|
{
|
||||||
|
QTest::ignoreMessage(QtWarningMsg, "ActionsHandler::handleMessageEvent - called with m_room and/or chatBarCache set to nullptr.");
|
||||||
|
actionsHandler->handleMessageEvent(nullptr);
|
||||||
|
|
||||||
|
auto chatBarCache = new ChatBarCache(this);
|
||||||
|
QTest::ignoreMessage(QtWarningMsg, "ActionsHandler::handleMessageEvent - called with m_room and/or chatBarCache set to nullptr.");
|
||||||
|
actionsHandler->handleMessageEvent(chatBarCache);
|
||||||
|
|
||||||
|
auto room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"));
|
||||||
|
actionsHandler->setRoom(room);
|
||||||
|
QTest::ignoreMessage(QtWarningMsg, "ActionsHandler::handleMessageEvent - called with m_room and/or chatBarCache set to nullptr.");
|
||||||
|
actionsHandler->handleMessageEvent(nullptr);
|
||||||
|
|
||||||
|
// The final one should throw no warning so we make sure.
|
||||||
|
QTest::failOnWarning("ActionsHandler::handleMessageEvent - called with m_room and/or chatBarCache set to nullptr.");
|
||||||
|
actionsHandler->handleMessageEvent(chatBarCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
QTEST_GUILESS_MAIN(ActionsHandlerTest)
|
||||||
|
#include "actionshandlertest.moc"
|
||||||
@@ -39,7 +39,8 @@ void ActionsHandler::setRoom(NeoChatRoom *room)
|
|||||||
|
|
||||||
void ActionsHandler::handleMessageEvent(ChatBarCache *chatBarCache)
|
void ActionsHandler::handleMessageEvent(ChatBarCache *chatBarCache)
|
||||||
{
|
{
|
||||||
if (!chatBarCache) {
|
if (!m_room || !chatBarCache) {
|
||||||
|
qWarning() << "ActionsHandler::handleMessageEvent - called with m_room and/or chatBarCache set to nullptr.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,10 +61,6 @@ void ActionsHandler::handleMessageEvent(ChatBarCache *chatBarCache)
|
|||||||
|
|
||||||
QString ActionsHandler::handleMentions(QString handledText, QList<Mention> *mentions)
|
QString ActionsHandler::handleMentions(QString handledText, QList<Mention> *mentions)
|
||||||
{
|
{
|
||||||
if (!m_room) {
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::sort(mentions->begin(), mentions->end(), [](const auto &a, const auto &b) -> bool {
|
std::sort(mentions->begin(), mentions->end(), [](const auto &a, const auto &b) -> bool {
|
||||||
return a.cursor.anchor() > b.cursor.anchor();
|
return a.cursor.anchor() > b.cursor.anchor();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user