From 8062dface6c0853d2982437f069d9c55d31e8174 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sun, 10 Dec 2023 18:14:51 +0000 Subject: [PATCH] ActionsHandler test Add basic test suite to make sure that ActionsHandler rejects attempts to handleMessage if either m_room or chatBarCacher are nullptr. --- autotests/CMakeLists.txt | 6 +++++ autotests/actionshandlertest.cpp | 43 ++++++++++++++++++++++++++++++++ src/actionshandler.cpp | 7 ++---- 3 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 autotests/actionshandlertest.cpp diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 142e64272..18dedada7 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -52,3 +52,9 @@ ecm_add_test( LINK_LIBRARIES neochat Qt::Test TEST_NAME messageeventmodeltest ) + +ecm_add_test( + actionshandlertest.cpp + LINK_LIBRARIES neochat Qt::Test + TEST_NAME actionshandlertest +) diff --git a/autotests/actionshandlertest.cpp b/autotests/actionshandlertest.cpp new file mode 100644 index 000000000..1ab0f89ed --- /dev/null +++ b/autotests/actionshandlertest.cpp @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: 2023 James Graham +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + +#include + +#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" diff --git a/src/actionshandler.cpp b/src/actionshandler.cpp index 6967473e9..61ae4d313 100644 --- a/src/actionshandler.cpp +++ b/src/actionshandler.cpp @@ -39,7 +39,8 @@ void ActionsHandler::setRoom(NeoChatRoom *room) 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; } @@ -60,10 +61,6 @@ void ActionsHandler::handleMessageEvent(ChatBarCache *chatBarCache) QString ActionsHandler::handleMentions(QString handledText, QList *mentions) { - if (!m_room) { - return QString(); - } - std::sort(mentions->begin(), mentions->end(), [](const auto &a, const auto &b) -> bool { return a.cursor.anchor() > b.cursor.anchor(); });