Warning, file /network/neochat/autotests/reactionmodeltest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include <QObject>
0005 #include <QSignalSpy>
0006 #include <QTest>
0007 
0008 #include "models/reactionmodel.h"
0009 
0010 #include <Quotient/events/roommessageevent.h>
0011 
0012 #include "testutils.h"
0013 
0014 using namespace Quotient;
0015 
0016 class ReactionModelTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private:
0021     Connection *connection = nullptr;
0022     TestUtils::TestRoom *room = nullptr;
0023 
0024 private Q_SLOTS:
0025     void initTestCase();
0026 
0027     void nullModel();
0028     void basicReaction();
0029     void newReaction();
0030 };
0031 
0032 void ReactionModelTest::initTestCase()
0033 {
0034     connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org"));
0035     room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-reactionmodel-sync.json"));
0036 }
0037 
0038 void ReactionModelTest::nullModel()
0039 {
0040     auto model = ReactionModel(nullptr, nullptr);
0041 
0042     QCOMPARE(model.rowCount(), 0);
0043     QCOMPARE(model.data(model.index(0), ReactionModel::TextContentRole), QVariant());
0044 }
0045 
0046 void ReactionModelTest::basicReaction()
0047 {
0048     auto event = eventCast<const RoomMessageEvent>(room->messageEvents().at(0).get());
0049     auto model = ReactionModel(event, room);
0050 
0051     QCOMPARE(model.rowCount(), 1);
0052     QCOMPARE(model.data(model.index(0), ReactionModel::TextContentRole), QStringLiteral("<span style=\"font-family: 'emoji';\">👍</span>"));
0053     QCOMPARE(model.data(model.index(0), ReactionModel::ReactionRole), QStringLiteral("👍"));
0054     QCOMPARE(model.data(model.index(0), ReactionModel::ToolTipRole),
0055              QStringLiteral("@alice:matrix.org reacted with <span style=\"font-family: 'emoji';\">👍</span>"));
0056     auto authorList = QVariantList{room->getUser(room->user(QStringLiteral("@alice:matrix.org")))};
0057     QCOMPARE(model.data(model.index(0), ReactionModel::AuthorsRole), authorList);
0058     QCOMPARE(model.data(model.index(0), ReactionModel::HasLocalUser), false);
0059 }
0060 
0061 void ReactionModelTest::newReaction()
0062 {
0063     auto event = eventCast<const RoomMessageEvent>(room->messageEvents().at(0).get());
0064     auto model = new ReactionModel(event, room);
0065 
0066     QCOMPARE(model->rowCount(), 1);
0067     QCOMPARE(model->data(model->index(0), ReactionModel::ToolTipRole),
0068              QStringLiteral("@alice:matrix.org reacted with <span style=\"font-family: 'emoji';\">👍</span>"));
0069 
0070     QSignalSpy spy(model, SIGNAL(modelReset()));
0071 
0072     room->syncNewEvents(QLatin1String("test-reactionmodel-extra-sync.json"));
0073     QCOMPARE(model->rowCount(), 2);
0074     QCOMPARE(spy.count(), 2); // Once for each of the 2 new reactions.
0075     QCOMPARE(model->data(model->index(1), ReactionModel::ReactionRole), QStringLiteral("😆"));
0076     QCOMPARE(model->data(model->index(0), ReactionModel::ToolTipRole),
0077              QStringLiteral("@alice:matrix.org and @bob:example.org reacted with <span style=\"font-family: 'emoji';\">👍</span>"));
0078 
0079     delete model;
0080 }
0081 
0082 QTEST_MAIN(ReactionModelTest)
0083 #include "reactionmodeltest.moc"