diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index bea7e4fe5..4d35c43b3 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -23,6 +23,11 @@ ecm_add_test( TEST_NAME delegatesizehelpertest ) +ecm_add_test( + roomtreemodeltest.cpp + LINK_LIBRARIES neochat Qt::Test +) + ecm_add_test( mediasizehelpertest.cpp LINK_LIBRARIES neochat Qt::Test diff --git a/autotests/neochatroomtest.cpp b/autotests/neochatroomtest.cpp index d696665c8..aa6a64140 100644 --- a/autotests/neochatroomtest.cpp +++ b/autotests/neochatroomtest.cpp @@ -5,9 +5,7 @@ #include #include -#include -#include -#include +#include "neochatconnection.h" #include "testutils.h" @@ -27,7 +25,8 @@ private Q_SLOTS: void NeoChatRoomTest::initTestCase() { - connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); + auto connection = new NeoChatConnection; + Connection::makeMockConnection(connection, QStringLiteral("@bob:kde.org")); room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), "test-min-sync.json"_ls); } diff --git a/autotests/roomtreemodeltest.cpp b/autotests/roomtreemodeltest.cpp new file mode 100644 index 000000000..28ca5b2ff --- /dev/null +++ b/autotests/roomtreemodeltest.cpp @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: 2024 Carl Schwan +// SPDX-License-Identifier: LGPL-2.0-or-later + +#include +#include + +#include "enums/neochatroomtype.h" +#include "models/roomtreemodel.h" +#include "neochatconnection.h" +#include "testutils.h" + +using namespace Quotient; + +class RoomTreeModelTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testTreeModel(); +}; + +void RoomTreeModelTest::testTreeModel() +{ + auto connection = new NeoChatConnection; + Connection::makeMockConnection(connection, QStringLiteral("@bob:kde.org")); + + auto room = dynamic_cast(new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QStringLiteral("test-min-sync.json"))); + QVERIFY(room); + connection->addRoom(room); + + RoomTreeModel model; + model.setConnection(connection); + QAbstractItemModelTester tester(&model); + + QCOMPARE(model.rowCount(), static_cast(NeoChatRoomType::TypesCount)); + + // Check data category + auto category = static_cast(NeoChatRoomType::typeForRoom(room)); + auto normalCategoryIdx = model.index(category, 0); + QCOMPARE(model.data(normalCategoryIdx, RoomTreeModel::DisplayNameRole).toString(), QStringLiteral("Normal")); + QCOMPARE(model.data(normalCategoryIdx, RoomTreeModel::DelegateTypeRole).toString(), QStringLiteral("section")); + QCOMPARE(model.data(normalCategoryIdx, RoomTreeModel::IconRole).toString(), QStringLiteral("group")); + QCOMPARE(model.data(normalCategoryIdx, RoomTreeModel::CategoryRole).toInt(), category); + QCOMPARE(model.rowCount(normalCategoryIdx), 1); + + // Check data room + auto roomIdx = model.index(0, 0, normalCategoryIdx); + QCOMPARE(model.data(roomIdx, RoomTreeModel::CurrentRoomRole).value(), room); + QCOMPARE(model.data(roomIdx, RoomTreeModel::CategoryRole).toInt(), category); + + QVERIFY(false); +} + +QTEST_MAIN(RoomTreeModelTest) + +#include "roomtreemodeltest.moc" diff --git a/src/models/roomtreemodel.cpp b/src/models/roomtreemodel.cpp index e55c5848c..8b8116631 100644 --- a/src/models/roomtreemodel.cpp +++ b/src/models/roomtreemodel.cpp @@ -174,6 +174,11 @@ std::optional TreeItem::position(Quotient::Room *room) const return std::nullopt; } +TreeItem::TreeData TreeItem::treeData() const +{ + return m_treeData; +} + RoomTreeModel::RoomTreeModel(QObject *parent) : QAbstractItemModel(parent) { @@ -191,8 +196,7 @@ void RoomTreeModel::initializeCategories() TreeItem *RoomTreeModel::getItem(const QModelIndex &index) const { if (index.isValid()) { - if (auto *item = static_cast(index.internalPointer())) - return item; + return static_cast(index.internalPointer()); } return m_rootItem.get(); } @@ -236,6 +240,7 @@ void RoomTreeModel::newRoom(Room *r) auto categoryItem = m_rootItem->child(type); beginInsertRows(index(type, 0), categoryItem->childCount(), categoryItem->childCount()); categoryItem->appendChild(std::make_unique(room)); + qWarning() << "append" << index(type, 0) << categoryItem->childCount(); connectRoomSignals(room); endInsertRows(); } @@ -280,7 +285,8 @@ void RoomTreeModel::moveRoom(Quotient::Room *room) if (oldRow == -1) { return; } - const auto newType = NeoChatRoomType::typeForRoom(dynamic_cast(room)); + auto neochatRoom = dynamic_cast(room); + const auto newType = NeoChatRoomType::typeForRoom(neochatRoom); if (newType == oldType) { return; } @@ -302,7 +308,7 @@ void RoomTreeModel::moveRoom(Quotient::Room *room) endRemoveRows(); beginInsertRows(newParent, newParentItem->childCount(), newParentItem->childCount()); - newParentItem->appendChild(std::make_unique(room)); + newParentItem->appendChild(std::make_unique(neochatRoom)); endInsertRows(); } @@ -358,22 +364,22 @@ int RoomTreeModel::columnCount(const QModelIndex &parent) const int RoomTreeModel::rowCount(const QModelIndex &parent) const { - if (parent.isValid() && parent.column() > 0) { - return 0; - } - const TreeItem *parentItem = getItem(parent); return parentItem ? parentItem->childCount() : 0; } QModelIndex RoomTreeModel::parent(const QModelIndex &index) const { - if (!index.isValid()) + qWarning() << "getting parent from" << index; + if (!index.isValid()) { return {}; + } TreeItem *childItem = getItem(index); TreeItem *parentItem = childItem ? childItem->parentItem() : nullptr; + qWarning() << parentItem << m_rootItem.get(); + return (parentItem != m_rootItem.get() && parentItem != nullptr) ? createIndex(parentItem->row(), 0, parentItem) : QModelIndex{}; } @@ -384,9 +390,7 @@ QModelIndex RoomTreeModel::index(int row, int column, const QModelIndex &parent) } TreeItem *parentItem = getItem(parent); - if (!parentItem) { - return {}; - } + Q_ASSERT(parentItem); if (auto *childItem = parentItem->child(row)) { return createIndex(row, column, childItem); diff --git a/src/neochatconnection.cpp b/src/neochatconnection.cpp index 1c1dce35c..8edbf7bf0 100644 --- a/src/neochatconnection.cpp +++ b/src/neochatconnection.cpp @@ -482,4 +482,9 @@ QString NeoChatConnection::accountDataJsonString(const QString &type) const return QString::fromUtf8(QJsonDocument(accountDataJson(type)).toJson()); } +void NeoChatConnection::addRoom(Quotient::Room *room) +{ + Connection::addRoom(room, false); +} + #include "moc_neochatconnection.cpp" diff --git a/src/neochatconnection.h b/src/neochatconnection.h index af03f74e2..b1948a11d 100644 --- a/src/neochatconnection.h +++ b/src/neochatconnection.h @@ -147,6 +147,12 @@ public: bool isOnline() const; + /** + * Add room directly in the connection. + * @internal for tests + */ + void addRoom(Quotient::Room *room); + Q_SIGNALS: void labelChanged(); void directChatNotificationsChanged();