Skip to content

Commit

Permalink
I hate models
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlSchwan committed Mar 3, 2024
1 parent 2309904 commit 1844a90
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 16 deletions.
5 changes: 5 additions & 0 deletions autotests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions autotests/neochatroomtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <QSignalSpy>
#include <QTest>

#include <Quotient/connection.h>
#include <Quotient/quotient_common.h>
#include <Quotient/syncdata.h>
#include "neochatconnection.h"

#include "testutils.h"

Expand All @@ -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);
}

Expand Down
56 changes: 56 additions & 0 deletions autotests/roomtreemodeltest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2024 Carl Schwan <[email protected]>
// SPDX-License-Identifier: LGPL-2.0-or-later

#include <QAbstractItemModelTester>
#include <QTest>

#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<NeoChatRoom *>(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<int>(NeoChatRoomType::TypesCount));

// Check data category
auto category = static_cast<int>(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<NeoChatRoom *>(), room);
QCOMPARE(model.data(roomIdx, RoomTreeModel::CategoryRole).toInt(), category);

QVERIFY(false);
}

QTEST_MAIN(RoomTreeModelTest)

#include "roomtreemodeltest.moc"
28 changes: 16 additions & 12 deletions src/models/roomtreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ std::optional<int> TreeItem::position(Quotient::Room *room) const
return std::nullopt;
}

TreeItem::TreeData TreeItem::treeData() const
{
return m_treeData;
}

RoomTreeModel::RoomTreeModel(QObject *parent)
: QAbstractItemModel(parent)
{
Expand All @@ -191,8 +196,7 @@ void RoomTreeModel::initializeCategories()
TreeItem *RoomTreeModel::getItem(const QModelIndex &index) const
{
if (index.isValid()) {
if (auto *item = static_cast<TreeItem *>(index.internalPointer()))
return item;
return static_cast<TreeItem *>(index.internalPointer());
}
return m_rootItem.get();
}
Expand Down Expand Up @@ -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<TreeItem>(room));
qWarning() << "append" << index(type, 0) << categoryItem->childCount();
connectRoomSignals(room);
endInsertRows();
}
Expand Down Expand Up @@ -280,7 +285,8 @@ void RoomTreeModel::moveRoom(Quotient::Room *room)
if (oldRow == -1) {
return;
}
const auto newType = NeoChatRoomType::typeForRoom(dynamic_cast<NeoChatRoom *>(room));
auto neochatRoom = dynamic_cast<NeoChatRoom *>(room);
const auto newType = NeoChatRoomType::typeForRoom(neochatRoom);
if (newType == oldType) {
return;
}
Expand All @@ -302,7 +308,7 @@ void RoomTreeModel::moveRoom(Quotient::Room *room)
endRemoveRows();

beginInsertRows(newParent, newParentItem->childCount(), newParentItem->childCount());
newParentItem->appendChild(std::make_unique<TreeItem>(room));
newParentItem->appendChild(std::make_unique<TreeItem>(neochatRoom));
endInsertRows();
}

Expand Down Expand Up @@ -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{};
}

Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/neochatconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 6 additions & 0 deletions src/neochatconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ class NeoChatConnection : public Quotient::Connection

bool isOnline() const;

/**
* Add room directly in the connection.
* @internal for tests
*/
void addRoom(Quotient::Room *room);

Q_SIGNALS:
void labelChanged();
void directChatNotificationsChanged();
Expand Down

0 comments on commit 1844a90

Please sign in to comment.