I hate models
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
56
autotests/roomtreemodeltest.cpp
Normal file
56
autotests/roomtreemodeltest.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
// SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu>
|
||||
// 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"
|
||||
@@ -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)
|
||||
{
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user