Improve ChatDocumentHandler handling of null properties

Improve ChatDocumentHandler handling of null properties
This commit is contained in:
James Graham
2023-11-04 14:43:31 +00:00
parent d1aac971bf
commit 2bfb2fa1f9
5 changed files with 67 additions and 7 deletions

View File

@@ -40,3 +40,9 @@ ecm_add_test(
LINK_LIBRARIES neochat Qt::Test LINK_LIBRARIES neochat Qt::Test
TEST_NAME chatbarcachetest TEST_NAME chatbarcachetest
) )
ecm_add_test(
chatdocumenthandlertest.cpp
LINK_LIBRARIES neochat Qt::Test
TEST_NAME chatdocumenthandlertest
)

View File

@@ -0,0 +1,36 @@
// 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 <QObject>
#include <QTest>
#include "chatdocumenthandler.h"
#include "neochatconfig.h"
class ChatDocumentHandlerTest : public QObject
{
Q_OBJECT
private:
ChatDocumentHandler emptyHandler;
private Q_SLOTS:
void initTestCase();
void nullComplete();
};
void ChatDocumentHandlerTest::initTestCase()
{
// HACK: this is to stop KStatusNotifierItem SEGFAULTING on cleanup.
NeoChatConfig::self()->setSystemTray(false);
}
void ChatDocumentHandlerTest::nullComplete()
{
QTest::ignoreMessage(QtWarningMsg, "complete called with m_document set to nullptr.");
emptyHandler.complete(0);
}
QTEST_MAIN(ChatDocumentHandlerTest)
#include "chatdocumenthandlertest.moc"

View File

@@ -311,6 +311,13 @@ ecm_qt_declare_logging_category(neochat
DEFAULT_SEVERITY Info DEFAULT_SEVERITY Info
) )
ecm_qt_declare_logging_category(neochat
HEADER "chatdocumenthandler_logging.h"
IDENTIFIER "ChatDocumentHandling"
CATEGORY_NAME "org.kde.neochat.chatdocumenthandler"
DEFAULT_SEVERITY Info
)
add_executable(neochat-app add_executable(neochat-app
main.cpp main.cpp
) )

View File

@@ -14,6 +14,8 @@
#include <Sonnet/BackgroundChecker> #include <Sonnet/BackgroundChecker>
#include <Sonnet/Settings> #include <Sonnet/Settings>
#include "chatdocumenthandler_logging.h"
class SyntaxHighlighter : public QSyntaxHighlighter class SyntaxHighlighter : public QSyntaxHighlighter
{ {
public: public:
@@ -98,7 +100,7 @@ ChatDocumentHandler::ChatDocumentHandler(QObject *parent)
, m_document(nullptr) , m_document(nullptr)
, m_cursorPosition(-1) , m_cursorPosition(-1)
, m_highlighter(new SyntaxHighlighter(this)) , m_highlighter(new SyntaxHighlighter(this))
, m_completionModel(new CompletionModel()) , m_completionModel(new CompletionModel(this))
{ {
connect(this, &ChatDocumentHandler::roomChanged, this, [this]() { connect(this, &ChatDocumentHandler::roomChanged, this, [this]() {
m_completionModel->setRoom(m_room); m_completionModel->setRoom(m_room);
@@ -213,6 +215,15 @@ void ChatDocumentHandler::setChatBarCache(ChatBarCache *chatBarCache)
void ChatDocumentHandler::complete(int index) void ChatDocumentHandler::complete(int index)
{ {
if (m_document == nullptr) {
qCWarning(ChatDocumentHandling) << "complete called with m_document set to nullptr.";
return;
}
if (m_completionModel->autoCompletionType() == CompletionModel::None) {
qCWarning(ChatDocumentHandling) << "complete called with m_completionModel->autoCompletionType() == CompletionModel::None.";
return;
}
if (m_completionModel->autoCompletionType() == CompletionModel::User) { if (m_completionModel->autoCompletionType() == CompletionModel::User) {
auto name = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::DisplayNameRole).toString(); auto name = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::DisplayNameRole).toString();
auto id = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::SubtitleRole).toString(); auto id = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::SubtitleRole).toString();
@@ -296,15 +307,17 @@ void ChatDocumentHandler::setSelectionEnd(int position)
QString ChatDocumentHandler::getText() const QString ChatDocumentHandler::getText() const
{ {
if (!m_room) { if (!m_chatBarCache) {
return QString(); qCWarning(ChatDocumentHandling) << "getText called with m_chatBarCache set to nullptr.";
return {};
} }
return m_chatBarCache->text(); return m_chatBarCache->text();
} }
void ChatDocumentHandler::pushMention(const Mention mention) const void ChatDocumentHandler::pushMention(const Mention mention) const
{ {
if (!m_room) { if (!m_chatBarCache) {
qCWarning(ChatDocumentHandling) << "pushMention called with m_chatBarCache set to nullptr.";
return; return;
} }
m_chatBarCache->mentions()->push_back(mention); m_chatBarCache->mentions()->push_back(mention);

View File

@@ -88,7 +88,7 @@ class ChatDocumentHandler : public QObject
* This is typically provided to a qml component to visualise the current * This is typically provided to a qml component to visualise the current
* completion results. * completion results.
*/ */
Q_PROPERTY(CompletionModel *completionModel READ completionModel NOTIFY completionModelChanged) Q_PROPERTY(CompletionModel *completionModel READ completionModel CONSTANT)
/** /**
* @brief The current room that the the text document is being handled for. * @brief The current room that the the text document is being handled for.
@@ -133,7 +133,6 @@ public:
Q_INVOKABLE void complete(int index); Q_INVOKABLE void complete(int index);
void updateCompletions();
CompletionModel *completionModel() const; CompletionModel *completionModel() const;
[[nodiscard]] QColor mentionColor() const; [[nodiscard]] QColor mentionColor() const;
@@ -147,7 +146,6 @@ Q_SIGNALS:
void cursorPositionChanged(); void cursorPositionChanged();
void roomChanged(); void roomChanged();
void chatBarCacheChanged(); void chatBarCacheChanged();
void completionModelChanged();
void selectionStartChanged(); void selectionStartChanged();
void selectionEndChanged(); void selectionEndChanged();
void errorColorChanged(); void errorColorChanged();