Add more tests to MessageEventModelTest

This commit is contained in:
James Graham
2023-12-03 17:04:21 +00:00
parent 862f4363a9
commit f3bacad460
2 changed files with 167 additions and 17 deletions

View File

@@ -34,14 +34,17 @@ private:
Connection *connection = nullptr;
MessageEventModel *model = nullptr;
TestRoom *setupTestRoom(const QString &syncFileName);
TestRoom *setupTestRoom(const QString &roomName, const QString &syncFileName = {});
void syncNewEvents(TestRoom *room, const QString &syncFileName);
private Q_SLOTS:
void initTestCase();
void init();
void switchRoom();
void switchEmptyRoom();
void switchSyncedRoom();
void simpleTimeline();
void syncNewEvents();
void idToRow();
void cleanup();
@@ -58,8 +61,8 @@ void MessageEventModelTest::init()
model = new MessageEventModel;
}
// Make sure that basic rooms can be switched without crashing.
void MessageEventModelTest::switchRoom()
// Make sure that basic empty rooms can be switched without crashing.
void MessageEventModelTest::switchEmptyRoom()
{
TestRoom *firstRoom = new TestRoom(connection, QStringLiteral("#firstRoom:kde.org"), JoinState::Join);
TestRoom *secondRoom = new TestRoom(connection, QStringLiteral("#secondRoom:kde.org"), JoinState::Join);
@@ -78,26 +81,65 @@ void MessageEventModelTest::switchRoom()
QCOMPARE(model->room(), nullptr);
}
// Make sure that rooms with some events can be switched without crashing
void MessageEventModelTest::switchSyncedRoom()
{
auto firstRoom = setupTestRoom(QStringLiteral("#firstRoom:kde.org"), QLatin1String("test-messageventmodel-sync.json"));
auto secondRoom = setupTestRoom(QStringLiteral("#secondRoom:kde.org"), QLatin1String("test-messageventmodel-sync.json"));
QSignalSpy spy(model, SIGNAL(roomChanged()));
QCOMPARE(model->room(), nullptr);
model->setRoom(firstRoom);
QCOMPARE(spy.count(), 1);
QCOMPARE(model->room()->id(), QStringLiteral("#firstRoom:kde.org"));
model->setRoom(secondRoom);
QCOMPARE(spy.count(), 2);
QCOMPARE(model->room()->id(), QStringLiteral("#secondRoom:kde.org"));
model->setRoom(nullptr);
QCOMPARE(spy.count(), 3);
QCOMPARE(model->room(), nullptr);
}
void MessageEventModelTest::simpleTimeline()
{
auto room = setupTestRoom(QLatin1String("test-min-sync.json"));
auto room = setupTestRoom(QStringLiteral("#myroom:kde.org"), QLatin1String("test-messageventmodel-sync.json"));
model->setRoom(room);
QCOMPARE(model->rowCount(), 1);
QCOMPARE(model->data(model->index(0)), QStringLiteral("<b>This is an example<br>text message</b>"));
QCOMPARE(model->data(model->index(0), MessageEventModel::DelegateTypeRole), DelegateType::Message);
QCOMPARE(model->data(model->index(0), MessageEventModel::PlainText), QStringLiteral("This is an example\ntext message"));
QCOMPARE(model->data(model->index(0), MessageEventModel::EventIdRole), QStringLiteral("$153456789:example.org"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(model->data(model->index(0), MessageEventModel::DelegateTypeRole), DelegateType::State);
QCOMPARE(model->data(model->index(0)), QStringLiteral("changed their display name to Example Changed"));
QCOMPARE(model->data(model->index(1)), QStringLiteral("<b>This is an example<br>text message</b>"));
QCOMPARE(model->data(model->index(1), MessageEventModel::DelegateTypeRole), DelegateType::Message);
QCOMPARE(model->data(model->index(1), MessageEventModel::PlainText), QStringLiteral("This is an example\ntext message"));
QCOMPARE(model->data(model->index(1), MessageEventModel::EventIdRole), QStringLiteral("$153456789:example.org"));
QTest::ignoreMessage(QtWarningMsg, "Index QModelIndex(-1,-1,0x0,QObject(0x0)) is not valid (expected valid)");
QCOMPARE(model->data(model->index(-1)), QVariant());
QTest::ignoreMessage(QtWarningMsg, "Index QModelIndex(-1,-1,0x0,QObject(0x0)) is not valid (expected valid)");
QCOMPARE(model->data(model->index(1)), QVariant());
QCOMPARE(model->data(model->index(model->rowCount())), QVariant());
}
// Sync some events into the MessageEventModel's current room and don't crash.
void MessageEventModelTest::syncNewEvents()
{
auto room = setupTestRoom(QStringLiteral("#myroom:kde.org"));
QSignalSpy spy(room, SIGNAL(aboutToAddNewMessages(Quotient::RoomEventsRange)));
model->setRoom(room);
QCOMPARE(model->rowCount(), 0);
syncNewEvents(room, QLatin1String("test-messageventmodel-sync.json"));
QCOMPARE(model->rowCount(), 2);
QCOMPARE(spy.count(), 1);
}
void MessageEventModelTest::idToRow()
{
auto room = setupTestRoom(QLatin1String("test-min-sync.json"));
auto room = setupTestRoom(QStringLiteral("#myroom:kde.org"), QLatin1String("test-min-sync.json"));
model->setRoom(room);
QCOMPARE(model->eventIdToRow(QStringLiteral("$153456789:example.org")), 0);
@@ -110,10 +152,15 @@ void MessageEventModelTest::cleanup()
QCOMPARE(model, nullptr);
}
TestRoom *MessageEventModelTest::setupTestRoom(const QString &syncFileName)
TestRoom *MessageEventModelTest::setupTestRoom(const QString &roomName, const QString &syncFileName)
{
auto room = new TestRoom(connection, QStringLiteral("#myroom:kde.org"), JoinState::Join);
auto room = new TestRoom(connection, roomName, JoinState::Join);
syncNewEvents(room, syncFileName);
return room;
}
void MessageEventModelTest::syncNewEvents(TestRoom *room, const QString &syncFileName)
{
if (!syncFileName.isEmpty()) {
QFile testSyncFile;
testSyncFile.setFileName(QLatin1String(DATA_DIR) + u'/' + syncFileName);
@@ -122,9 +169,7 @@ TestRoom *MessageEventModelTest::setupTestRoom(const QString &syncFileName)
SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, testSyncJson.object());
room->update(std::move(roomData));
}
return room;
}
QTEST_GUILESS_MAIN(MessageEventModelTest)
QTEST_MAIN(MessageEventModelTest)
#include "messageeventmodeltest.moc"