diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index f12a8e67d..8efa3d322 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -40,3 +40,9 @@ ecm_add_test( LINK_LIBRARIES neochat Qt::Test TEST_NAME chatbarcachetest ) + +ecm_add_test( + chatdocumenthandlertest.cpp + LINK_LIBRARIES neochat Qt::Test + TEST_NAME chatdocumenthandlertest +) diff --git a/autotests/chatdocumenthandlertest.cpp b/autotests/chatdocumenthandlertest.cpp new file mode 100644 index 000000000..7194ec92c --- /dev/null +++ b/autotests/chatdocumenthandlertest.cpp @@ -0,0 +1,36 @@ +// SPDX-FileCopyrightText: 2023 James Graham +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + +#include +#include + +#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" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc9d7fe1c..ee7b67822 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -311,6 +311,13 @@ ecm_qt_declare_logging_category(neochat 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 main.cpp ) diff --git a/src/chatdocumenthandler.cpp b/src/chatdocumenthandler.cpp index 303e42b6f..a980010f0 100644 --- a/src/chatdocumenthandler.cpp +++ b/src/chatdocumenthandler.cpp @@ -14,6 +14,8 @@ #include #include +#include "chatdocumenthandler_logging.h" + class SyntaxHighlighter : public QSyntaxHighlighter { public: @@ -98,7 +100,7 @@ ChatDocumentHandler::ChatDocumentHandler(QObject *parent) , m_document(nullptr) , m_cursorPosition(-1) , m_highlighter(new SyntaxHighlighter(this)) - , m_completionModel(new CompletionModel()) + , m_completionModel(new CompletionModel(this)) { connect(this, &ChatDocumentHandler::roomChanged, this, [this]() { m_completionModel->setRoom(m_room); @@ -213,6 +215,15 @@ void ChatDocumentHandler::setChatBarCache(ChatBarCache *chatBarCache) 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) { 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(); @@ -296,15 +307,17 @@ void ChatDocumentHandler::setSelectionEnd(int position) QString ChatDocumentHandler::getText() const { - if (!m_room) { - return QString(); + if (!m_chatBarCache) { + qCWarning(ChatDocumentHandling) << "getText called with m_chatBarCache set to nullptr."; + return {}; } return m_chatBarCache->text(); } void ChatDocumentHandler::pushMention(const Mention mention) const { - if (!m_room) { + if (!m_chatBarCache) { + qCWarning(ChatDocumentHandling) << "pushMention called with m_chatBarCache set to nullptr."; return; } m_chatBarCache->mentions()->push_back(mention); diff --git a/src/chatdocumenthandler.h b/src/chatdocumenthandler.h index 617b9ce16..2e22e30c4 100644 --- a/src/chatdocumenthandler.h +++ b/src/chatdocumenthandler.h @@ -88,7 +88,7 @@ class ChatDocumentHandler : public QObject * This is typically provided to a qml component to visualise the current * 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. @@ -133,7 +133,6 @@ public: Q_INVOKABLE void complete(int index); - void updateCompletions(); CompletionModel *completionModel() const; [[nodiscard]] QColor mentionColor() const; @@ -147,7 +146,6 @@ Q_SIGNALS: void cursorPositionChanged(); void roomChanged(); void chatBarCacheChanged(); - void completionModelChanged(); void selectionStartChanged(); void selectionEndChanged(); void errorColorChanged();