From 1d8ffb22ee777474cceccba6f95f6a50a1672647 Mon Sep 17 00:00:00 2001 From: James Graham Date: Sat, 9 Dec 2023 19:32:48 +0000 Subject: [PATCH] TestUtils Create TestUtils with TestRoom and use this for all tests which require it --- autotests/chatbarcachetest.cpp | 24 ++---------- autotests/eventhandlertest.cpp | 24 ++---------- autotests/messageeventmodeltest.cpp | 57 +++++++---------------------- autotests/neochatroomtest.cpp | 26 ++----------- autotests/testutils.h | 41 +++++++++++++++++++++ autotests/texthandlertest.cpp | 24 ++---------- 6 files changed, 70 insertions(+), 126 deletions(-) create mode 100644 autotests/testutils.h diff --git a/autotests/chatbarcachetest.cpp b/autotests/chatbarcachetest.cpp index 2157ad6b9..b34856879 100644 --- a/autotests/chatbarcachetest.cpp +++ b/autotests/chatbarcachetest.cpp @@ -12,26 +12,17 @@ #include "chatbarcache.h" #include "neochatroom.h" +#include "testutils.h" + using namespace Quotient; -class TestRoom : public NeoChatRoom -{ -public: - using NeoChatRoom::NeoChatRoom; - - void update(SyncRoomData &&data, bool fromCache = false) - { - Room::updateData(std::move(data), fromCache); - } -}; - class ChatBarCacheTest : public QObject { Q_OBJECT private: Connection *connection = nullptr; - TestRoom *room = nullptr; + TestUtils::TestRoom *room = nullptr; private Q_SLOTS: void initTestCase(); @@ -47,14 +38,7 @@ private Q_SLOTS: void ChatBarCacheTest::initTestCase() { connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); - room = new TestRoom(connection, QStringLiteral("#myroom:kde.org"), JoinState::Join); - - QFile testMinSyncFile; - testMinSyncFile.setFileName(QLatin1String(DATA_DIR) + u'/' + QLatin1String("test-min-sync.json")); - testMinSyncFile.open(QIODevice::ReadOnly); - const auto testMinSyncJson = QJsonDocument::fromJson(testMinSyncFile.readAll()); - SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, testMinSyncJson.object()); - room->update(std::move(roomData)); + room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-min-sync.json")); } void ChatBarCacheTest::empty() diff --git a/autotests/eventhandlertest.cpp b/autotests/eventhandlertest.cpp index 818049de7..d1101f014 100644 --- a/autotests/eventhandlertest.cpp +++ b/autotests/eventhandlertest.cpp @@ -18,26 +18,17 @@ #include "neochatroom.h" #include "utils.h" +#include "testutils.h" + using namespace Quotient; -class TestRoom : public NeoChatRoom -{ -public: - using NeoChatRoom::NeoChatRoom; - - void update(SyncRoomData &&data, bool fromCache = false) - { - Room::updateData(std::move(data), fromCache); - } -}; - class EventHandlerTest : public QObject { Q_OBJECT private: Connection *connection = nullptr; - TestRoom *room = nullptr; + TestUtils::TestRoom *room = nullptr; EventHandler eventHandler; EventHandler emptyHandler; EventHandler noEventHandler; @@ -101,14 +92,7 @@ private Q_SLOTS: void EventHandlerTest::initTestCase() { connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); - room = new TestRoom(connection, QStringLiteral("#myroom:kde.org"), JoinState::Join); - - QFile testEventHandlerSyncFile; - testEventHandlerSyncFile.setFileName(QLatin1String(DATA_DIR) + u'/' + QLatin1String("test-eventhandler-sync.json")); - testEventHandlerSyncFile.open(QIODevice::ReadOnly); - const auto testEventHandlerSyncJson = QJsonDocument::fromJson(testEventHandlerSyncFile.readAll()); - SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, testEventHandlerSyncJson.object()); - room->update(std::move(roomData)); + room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-eventhandler-sync.json")); eventHandler.setRoom(room); noEventHandler.setRoom(room); diff --git a/autotests/messageeventmodeltest.cpp b/autotests/messageeventmodeltest.cpp index 50efcc9f7..0c3b54872 100644 --- a/autotests/messageeventmodeltest.cpp +++ b/autotests/messageeventmodeltest.cpp @@ -13,19 +13,10 @@ #include "models/messageeventmodel.h" #include "neochatroom.h" +#include "testutils.h" + using namespace Quotient; -class TestRoom : public NeoChatRoom -{ -public: - using NeoChatRoom::NeoChatRoom; - - void update(SyncRoomData &&data, bool fromCache = false) - { - Room::updateData(std::move(data), fromCache); - } -}; - class MessageEventModelTest : public QObject { Q_OBJECT @@ -34,9 +25,6 @@ private: Connection *connection = nullptr; MessageEventModel *model = nullptr; - TestRoom *setupTestRoom(const QString &roomName, const QString &syncFileName = {}); - void syncNewEvents(TestRoom *room, const QString &syncFileName); - private Q_SLOTS: void initTestCase(); void init(); @@ -66,8 +54,8 @@ void MessageEventModelTest::init() // 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); + auto firstRoom = new TestUtils::TestRoom(connection, QStringLiteral("#firstRoom:kde.org")); + auto secondRoom = new TestUtils::TestRoom(connection, QStringLiteral("#secondRoom:kde.org")); QSignalSpy spy(model, SIGNAL(roomChanged())); @@ -86,8 +74,8 @@ void MessageEventModelTest::switchEmptyRoom() // 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")); + auto firstRoom = new TestUtils::TestRoom(connection, QStringLiteral("#firstRoom:kde.org"), QLatin1String("test-messageventmodel-sync.json")); + auto secondRoom = new TestUtils::TestRoom(connection, QStringLiteral("#secondRoom:kde.org"), QLatin1String("test-messageventmodel-sync.json")); QSignalSpy spy(model, SIGNAL(roomChanged())); @@ -105,7 +93,7 @@ void MessageEventModelTest::switchSyncedRoom() void MessageEventModelTest::simpleTimeline() { - auto room = setupTestRoom(QStringLiteral("#myroom:kde.org"), QLatin1String("test-messageventmodel-sync.json")); + auto room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-messageventmodel-sync.json")); model->setRoom(room); QCOMPARE(model->rowCount(), 2); @@ -127,13 +115,13 @@ void MessageEventModelTest::simpleTimeline() // Sync some events into the MessageEventModel's current room and don't crash. void MessageEventModelTest::syncNewEvents() { - auto room = setupTestRoom(QStringLiteral("#myroom:kde.org")); + auto room = new TestUtils::TestRoom(connection, 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")); + room->syncNewEvents(QLatin1String("test-messageventmodel-sync.json")); QCOMPARE(model->rowCount(), 2); QCOMPARE(spy.count(), 1); @@ -146,7 +134,7 @@ void MessageEventModelTest::pendingEvent() QSignalSpy spyRemove(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); QSignalSpy spyChanged(model, SIGNAL(dataChanged(const QModelIndex, const QModelIndex, const QList &))); - auto room = setupTestRoom(QStringLiteral("#myroom:kde.org")); + auto room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org")); model->setRoom(room); QCOMPARE(model->rowCount(), 0); @@ -197,20 +185,20 @@ void MessageEventModelTest::pendingEvent() // Make sure that the signals are disconnecting correctly when a room is switched. void MessageEventModelTest::disconnect() { - auto room = setupTestRoom(QStringLiteral("#myroom:kde.org")); + auto room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org")); model->setRoom(room); QSignalSpy spy(model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); model->setRoom(nullptr); - syncNewEvents(room, QLatin1String("test-messageventmodel-sync.json")); + room->syncNewEvents(QLatin1String("test-messageventmodel-sync.json")); QCOMPARE(spy.count(), 0); } void MessageEventModelTest::idToRow() { - auto room = setupTestRoom(QStringLiteral("#myroom:kde.org"), QLatin1String("test-min-sync.json")); + auto room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-min-sync.json")); model->setRoom(room); QCOMPARE(model->eventIdToRow(QStringLiteral("$153456789:example.org")), 0); @@ -223,24 +211,5 @@ void MessageEventModelTest::cleanup() QCOMPARE(model, nullptr); } -TestRoom *MessageEventModelTest::setupTestRoom(const QString &roomName, const QString &syncFileName) -{ - 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); - testSyncFile.open(QIODevice::ReadOnly); - const auto testSyncJson = QJsonDocument::fromJson(testSyncFile.readAll()); - SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, testSyncJson.object()); - room->update(std::move(roomData)); - } -} - QTEST_MAIN(MessageEventModelTest) #include "messageeventmodeltest.moc" diff --git a/autotests/neochatroomtest.cpp b/autotests/neochatroomtest.cpp index 5190ae712..e52e652c8 100644 --- a/autotests/neochatroomtest.cpp +++ b/autotests/neochatroomtest.cpp @@ -5,31 +5,20 @@ #include #include -#include "neochatroom.h" - #include #include #include +#include "testutils.h" + using namespace Quotient; -class TestRoom : public NeoChatRoom -{ -public: - using NeoChatRoom::NeoChatRoom; - - void update(SyncRoomData &&data, bool fromCache = false) - { - Room::updateData(std::move(data), fromCache); - } -}; - class NeoChatRoomTest : public QObject { Q_OBJECT private: Connection *connection = nullptr; - TestRoom *room = nullptr; + TestUtils::TestRoom *room = nullptr; private Q_SLOTS: void initTestCase(); @@ -40,14 +29,7 @@ private Q_SLOTS: void NeoChatRoomTest::initTestCase() { connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); - room = new TestRoom(connection, QStringLiteral("#myroom:kde.org"), JoinState::Join); - - QFile testMinSyncFile; - testMinSyncFile.setFileName(QLatin1String(DATA_DIR) + u'/' + QLatin1String("test-min-sync.json")); - testMinSyncFile.open(QIODevice::ReadOnly); - const auto testMinSyncJson = QJsonDocument::fromJson(testMinSyncFile.readAll()); - SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, testMinSyncJson.object()); - room->update(std::move(roomData)); + room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), "test-min-sync.json"_ls); } void NeoChatRoomTest::subtitleTextTest() diff --git a/autotests/testutils.h b/autotests/testutils.h new file mode 100644 index 000000000..b32d9cd43 --- /dev/null +++ b/autotests/testutils.h @@ -0,0 +1,41 @@ +// SPDX-FileCopyrightText: 2023 James Graham +// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + +#include + +#include "neochatroom.h" + +namespace Quotient +{ +class Connection; +} + +namespace TestUtils +{ +class TestRoom : public NeoChatRoom +{ +public: + TestRoom(Quotient::Connection *connection, const QString &roomName, const QString &syncFileName = {}) + : NeoChatRoom(connection, roomName, Quotient::JoinState::Join) + { + syncNewEvents(syncFileName); + } + + void update(Quotient::SyncRoomData &&data, bool fromCache = false) + { + Room::updateData(std::move(data), fromCache); + } + + void syncNewEvents(const QString &syncFileName) + { + if (!syncFileName.isEmpty()) { + QFile testSyncFile; + testSyncFile.setFileName(QLatin1String(DATA_DIR) + u'/' + syncFileName); + testSyncFile.open(QIODevice::ReadOnly); + const auto testSyncJson = QJsonDocument::fromJson(testSyncFile.readAll()); + Quotient::SyncRoomData roomData(id(), Quotient::JoinState::Join, testSyncJson.object()); + update(std::move(roomData)); + } + } +}; +} diff --git a/autotests/texthandlertest.cpp b/autotests/texthandlertest.cpp index 523a6f248..5eff65e66 100644 --- a/autotests/texthandlertest.cpp +++ b/autotests/texthandlertest.cpp @@ -14,26 +14,17 @@ #include "neochatconnection.h" #include "utils.h" +#include "testutils.h" + using namespace Quotient; -class TestRoom : public NeoChatRoom -{ -public: - using NeoChatRoom::NeoChatRoom; - - void update(SyncRoomData &&data, bool fromCache = false) - { - Room::updateData(std::move(data), fromCache); - } -}; - class TextHandlerTest : public QObject { Q_OBJECT private: Connection *connection = nullptr; - TestRoom *room = nullptr; + TestUtils::TestRoom *room = nullptr; private Q_SLOTS: void initTestCase(); @@ -83,8 +74,6 @@ private Q_SLOTS: void TextHandlerTest::initTestCase() { connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); - room = new TestRoom(connection, QStringLiteral("#myroom:kde.org"), JoinState::Join); - connection->setAccountData("im.ponies.user_emotes"_ls, QJsonObject{{"images"_ls, QJsonObject{{"test"_ls, @@ -93,12 +82,7 @@ void TextHandlerTest::initTestCase() {"usage"_ls, QJsonArray{"emoticon"_ls}}}}}}}); CustomEmojiModel::instance().setConnection(static_cast(connection)); - QFile testTextHandlerSyncFile; - testTextHandlerSyncFile.setFileName(QLatin1String(DATA_DIR) + u'/' + QLatin1String("test-texthandler-sync.json")); - testTextHandlerSyncFile.open(QIODevice::ReadOnly); - const auto testTextHandlerSyncJson = QJsonDocument::fromJson(testTextHandlerSyncFile.readAll()); - SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, testTextHandlerSyncJson.object()); - room->update(std::move(roomData)); + room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-texthandler-sync.json")); } void TextHandlerTest::allowedAttributes()