File indexing completed on 2024-05-05 05:01:17

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 <QJsonDocument>
0005 #include <QJsonObject>
0006 #include <QObject>
0007 #include <QTest>
0008 
0009 #include <Quotient/syncdata.h>
0010 #include <qtestcase.h>
0011 
0012 #include "chatbarcache.h"
0013 #include "neochatroom.h"
0014 
0015 #include "testutils.h"
0016 
0017 using namespace Quotient;
0018 
0019 class ChatBarCacheTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private:
0024     Connection *connection = nullptr;
0025     TestUtils::TestRoom *room = nullptr;
0026 
0027 private Q_SLOTS:
0028     void initTestCase();
0029 
0030     void empty();
0031     void noRoom();
0032     void badParent();
0033     void reply();
0034     void edit();
0035     void attachment();
0036 };
0037 
0038 void ChatBarCacheTest::initTestCase()
0039 {
0040     connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org"));
0041     room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), QLatin1String("test-min-sync.json"));
0042 }
0043 
0044 void ChatBarCacheTest::empty()
0045 {
0046     QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room));
0047 
0048     QCOMPARE(chatBarCache->text(), QString());
0049     QCOMPARE(chatBarCache->isReplying(), false);
0050     QCOMPARE(chatBarCache->replyId(), QString());
0051     QCOMPARE(chatBarCache->isEditing(), false);
0052     QCOMPARE(chatBarCache->editId(), QString());
0053     QCOMPARE(chatBarCache->relationUser(), room->getUser(nullptr));
0054     QCOMPARE(chatBarCache->relationMessage(), QString());
0055     QCOMPARE(chatBarCache->attachmentPath(), QString());
0056 }
0057 
0058 void ChatBarCacheTest::noRoom()
0059 {
0060     QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache());
0061     chatBarCache->setReplyId(QLatin1String("$153456789:example.org"));
0062 
0063     // These should return empty even though a reply ID has been set because the
0064     // ChatBarCache has no parent.
0065 
0066     QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.");
0067     QCOMPARE(chatBarCache->relationUser(), QVariantMap());
0068 
0069     QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation.");
0070     QCOMPARE(chatBarCache->relationMessage(), QString());
0071 }
0072 
0073 void ChatBarCacheTest::badParent()
0074 {
0075     QScopedPointer<QObject> badParent(new QObject());
0076     QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(badParent.get()));
0077     chatBarCache->setReplyId(QLatin1String("$153456789:example.org"));
0078 
0079     // These should return empty even though a reply ID has been set because the
0080     // ChatBarCache has no parent.
0081 
0082     QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.");
0083     QCOMPARE(chatBarCache->relationUser(), QVariantMap());
0084 
0085     QTest::ignoreMessage(QtWarningMsg, "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation.");
0086     QCOMPARE(chatBarCache->relationMessage(), QString());
0087 }
0088 
0089 void ChatBarCacheTest::reply()
0090 {
0091     QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room));
0092     chatBarCache->setText(QLatin1String("some text"));
0093     chatBarCache->setAttachmentPath(QLatin1String("some/path"));
0094     chatBarCache->setReplyId(QLatin1String("$153456789:example.org"));
0095 
0096     QCOMPARE(chatBarCache->text(), QLatin1String("some text"));
0097     QCOMPARE(chatBarCache->isReplying(), true);
0098     QCOMPARE(chatBarCache->replyId(), QLatin1String("$153456789:example.org"));
0099     QCOMPARE(chatBarCache->isEditing(), false);
0100     QCOMPARE(chatBarCache->editId(), QString());
0101     QCOMPARE(chatBarCache->relationUser(), room->getUser(room->user(QLatin1String("@example:example.org"))));
0102     QCOMPARE(chatBarCache->relationMessage(), QLatin1String("This is an example\ntext message"));
0103     QCOMPARE(chatBarCache->attachmentPath(), QString());
0104 }
0105 
0106 void ChatBarCacheTest::edit()
0107 {
0108     QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room));
0109     chatBarCache->setText(QLatin1String("some text"));
0110     chatBarCache->setAttachmentPath(QLatin1String("some/path"));
0111     chatBarCache->setEditId(QLatin1String("$153456789:example.org"));
0112 
0113     QCOMPARE(chatBarCache->text(), QLatin1String("some text"));
0114     QCOMPARE(chatBarCache->isReplying(), false);
0115     QCOMPARE(chatBarCache->replyId(), QString());
0116     QCOMPARE(chatBarCache->isEditing(), true);
0117     QCOMPARE(chatBarCache->editId(), QLatin1String("$153456789:example.org"));
0118     QCOMPARE(chatBarCache->relationUser(), room->getUser(room->user(QLatin1String("@example:example.org"))));
0119     QCOMPARE(chatBarCache->relationMessage(), QLatin1String("This is an example\ntext message"));
0120     QCOMPARE(chatBarCache->attachmentPath(), QString());
0121 }
0122 
0123 void ChatBarCacheTest::attachment()
0124 {
0125     QScopedPointer<ChatBarCache> chatBarCache(new ChatBarCache(room));
0126     chatBarCache->setText(QLatin1String("some text"));
0127     chatBarCache->setEditId(QLatin1String("$153456789:example.org"));
0128     chatBarCache->setAttachmentPath(QLatin1String("some/path"));
0129 
0130     QCOMPARE(chatBarCache->text(), QLatin1String("some text"));
0131     QCOMPARE(chatBarCache->isReplying(), false);
0132     QCOMPARE(chatBarCache->replyId(), QString());
0133     QCOMPARE(chatBarCache->isEditing(), false);
0134     QCOMPARE(chatBarCache->editId(), QString());
0135     QCOMPARE(chatBarCache->relationUser(), room->getUser(nullptr));
0136     QCOMPARE(chatBarCache->relationMessage(), QString());
0137     QCOMPARE(chatBarCache->attachmentPath(), QLatin1String("some/path"));
0138 }
0139 
0140 QTEST_MAIN(ChatBarCacheTest)
0141 #include "chatbarcachetest.moc"