More unit tests

This commit is contained in:
Carl Schwan
2024-03-03 18:57:17 +01:00
parent b9bf37089b
commit a4917d82e9
5 changed files with 23 additions and 4 deletions

View File

@@ -6,6 +6,7 @@
#include "enums/neochatroomtype.h"
#include "models/roomtreemodel.h"
#include "models/sortfilterroomtreemodel.h"
#include "neochatconnection.h"
#include "testutils.h"
@@ -30,12 +31,18 @@ void RoomTreeModelTest::testTreeModel()
RoomTreeModel model;
model.setConnection(connection);
SortFilterRoomTreeModel filterModel;
filterModel.setSourceModel(&model);
QAbstractItemModelTester tester(&model);
QAbstractItemModelTester testerFilter(&filterModel);
QCOMPARE(model.rowCount(), static_cast<int>(NeoChatRoomType::TypesCount));
// Check data category
auto category = static_cast<int>(NeoChatRoomType::typeForRoom(room));
QCOMPARE(category, NeoChatRoomType::Normal);
auto normalCategoryIdx = model.index(category, 0);
QCOMPARE(model.data(normalCategoryIdx, RoomTreeModel::DisplayNameRole).toString(), QStringLiteral("Normal"));
QCOMPARE(model.data(normalCategoryIdx, RoomTreeModel::DelegateTypeRole).toString(), QStringLiteral("section"));
@@ -48,7 +55,14 @@ void RoomTreeModelTest::testTreeModel()
QCOMPARE(model.data(roomIdx, RoomTreeModel::CurrentRoomRole).value<NeoChatRoom *>(), room);
QCOMPARE(model.data(roomIdx, RoomTreeModel::CategoryRole).toInt(), category);
QVERIFY(false);
// Move room
room->setProperty("isFavorite", true);
model.moveRoom(room);
auto newCategory = static_cast<int>(NeoChatRoomType::typeForRoom(room));
QCOMPARE(newCategory, NeoChatRoomType::Favorite);
auto newCategoryIdx = model.index(newCategory, 0);
QVERIFY(newCategoryIdx != normalCategoryIdx);
}
QTEST_MAIN(RoomTreeModelTest)

View File

@@ -41,7 +41,8 @@ public:
if (room->joinState() == Quotient::JoinState::Invite) {
return NeoChatRoomType::Invited;
}
if (room->isFavourite()) {
// HACK for the unit tests
if (room->isFavourite() || room->property("isFavorite").toBool()) {
return NeoChatRoomType::Favorite;
}
if (room->isLowPriority()) {

View File

@@ -44,6 +44,7 @@ bool TreeItem::removeChildren(int position, int count)
for (int row = 0; row < count; ++row) {
m_childItems.erase(m_childItems.cbegin() + position);
qWarning() << "removing" << position;
}
return true;
@@ -246,6 +247,7 @@ void RoomTreeModel::newRoom(Room *r)
categoryItem->appendChild(std::make_unique<TreeItem>(room, categoryItem));
connectRoomSignals(room);
endInsertRows();
qWarning() << "adding room" << type << "new count" << categoryItem->childCount();
}
void RoomTreeModel::leftRoom(Room *r)
@@ -302,8 +304,6 @@ void RoomTreeModel::moveRoom(Quotient::Room *room)
auto newParentItem = getItem(newParent);
Q_ASSERT(newParentItem);
qWarning() << "Moving" << room << "from" << oldType << "row" << oldRow << "to" << newType << "row" << newParentItem->childCount();
// HACK: We're doing this as a remove then insert because moving doesn't work
// properly with DelegateChooser for whatever reason.

View File

@@ -15,6 +15,7 @@ class Room;
class NeoChatConnection;
class NeoChatRoom;
class RoomTreeModelTest;
class TreeItem
{
@@ -122,4 +123,6 @@ private:
void refreshRoomRoles(NeoChatRoom *room, const QList<int> &roles = {});
std::unique_ptr<TreeItem> m_rootItem;
friend RoomTreeModelTest;
};

View File

@@ -127,6 +127,7 @@ bool SortFilterRoomTreeModel::filterAcceptsRow(int source_row, const QModelIndex
const QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
if (!index.isValid()) {
qWarning() << source_row << source_parent << sourceModel()->rowCount(source_parent);
return true;
}
if (sourceModel()->data(index, RoomTreeModel::IsCategoryRole).toBool()) {