File indexing completed on 2024-05-12 05:01:53

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "roommodeltest.h"
0008 #include "model/roommodel.h"
0009 #include "room.h"
0010 #include "ruqola_autotest_helper.h"
0011 #include "test_model_helpers.h"
0012 
0013 #include <QJsonArray>
0014 #include <QJsonDocument>
0015 #include <QJsonObject>
0016 #include <QSignalSpy>
0017 #include <QStandardPaths>
0018 
0019 #include "rocketchataccount.h"
0020 #include <qglobal.h>
0021 
0022 QTEST_GUILESS_MAIN(RoomModelTest)
0023 
0024 // TEST signal/slot
0025 
0026 RoomModelTest::RoomModelTest(QObject *parent)
0027     : QObject(parent)
0028 {
0029     QStandardPaths::setTestModeEnabled(true);
0030 }
0031 
0032 void RoomModelTest::shouldHaveDefaultValues()
0033 {
0034     RoomModel sampleModel;
0035     QCOMPARE(sampleModel.rowCount(), 0);
0036 }
0037 
0038 void RoomModelTest::shouldReturnRowCount()
0039 {
0040     RoomModel sampleModel;
0041 
0042     QSignalSpy rowInsertedSpy(&sampleModel, &RoomModel::rowsInserted);
0043     QSignalSpy rowABTInserted(&sampleModel, &RoomModel::rowsAboutToBeInserted);
0044     QSignalSpy rowRemovedSpy(&sampleModel, &RoomModel::rowsRemoved);
0045     QSignalSpy rowABTRemoved(&sampleModel, &RoomModel::rowsAboutToBeRemoved);
0046 
0047     QCOMPARE(sampleModel.rowCount(), 0);
0048 
0049     sampleModel.addRoom(QStringLiteral("myRoomID1"), QStringLiteral("myRoom1"));
0050     QCOMPARE(sampleModel.rowCount(), 1);
0051 
0052     QCOMPARE(rowInsertedSpy.count(), 1);
0053     QCOMPARE(rowABTInserted.count(), 1);
0054     QCOMPARE(rowRemovedSpy.count(), 0);
0055     QCOMPARE(rowABTRemoved.count(), 0);
0056     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,0"));
0057     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,0"));
0058 
0059     rowInsertedSpy.clear();
0060     rowABTInserted.clear();
0061     rowRemovedSpy.clear();
0062     rowABTRemoved.clear();
0063 
0064     sampleModel.addRoom(QStringLiteral("myRoomID1"), QStringLiteral("bla bla"));
0065     QCOMPARE(sampleModel.rowCount(), 1);
0066 
0067     // Fix it. We remove + re-add after that ! it's not optimal
0068     QCOMPARE(rowInsertedSpy.count(), 0);
0069     QCOMPARE(rowABTInserted.count(), 0);
0070     QCOMPARE(rowRemovedSpy.count(), 0);
0071     QCOMPARE(rowABTRemoved.count(), 0);
0072 
0073     rowInsertedSpy.clear();
0074     rowABTInserted.clear();
0075     rowRemovedSpy.clear();
0076     rowABTRemoved.clear();
0077 
0078     sampleModel.addRoom(QStringLiteral("myRoomID2"), QStringLiteral("myRoom2"), true);
0079     QCOMPARE(sampleModel.rowCount(), 2);
0080     QCOMPARE(rowInsertedSpy.count(), 1);
0081     QCOMPARE(rowABTInserted.count(), 1);
0082     QCOMPARE(rowRemovedSpy.count(), 0);
0083     QCOMPARE(rowABTRemoved.count(), 0);
0084     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("1,1"));
0085     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("1,1"));
0086 }
0087 
0088 void RoomModelTest::shouldFindRoom()
0089 {
0090     RoomModel sampleModel;
0091     Room *room = nullptr;
0092 
0093     sampleModel.addRoom(QStringLiteral("RA15"), QStringLiteral("master"));
0094     room = sampleModel.findRoom(QStringLiteral("RA151100ECE"));
0095     QVERIFY(!room);
0096 
0097     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0098     room = sampleModel.findRoom(QStringLiteral("RA151100ECE"));
0099     QVERIFY(room);
0100     QCOMPARE(QStringLiteral("myRoom"), room->name());
0101 }
0102 
0103 void RoomModelTest::shouldAddRoom()
0104 {
0105     RoomModel sampleModel;
0106     Room *room = nullptr;
0107 
0108     QCOMPARE(sampleModel.rowCount(), 0);
0109     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0110     QCOMPARE(sampleModel.rowCount(), 1);
0111 
0112     room = sampleModel.findRoom(QStringLiteral("RA151100ECE"));
0113     QVERIFY(room);
0114     QCOMPARE(QStringLiteral("myRoom"), room->name());
0115 }
0116 
0117 void RoomModelTest::shouldUpdateRoom()
0118 {
0119     // FIXME!!
0120     /*
0121     RoomModel sampleModel;
0122     RoomWrapper *sampleWrapper = nullptr;
0123 
0124     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0125 
0126     QSignalSpy spy(&sampleModel, &RoomModel::dataChanged);
0127     const QString Id = QStringLiteral("RA151100ECE");
0128     const QString name = QStringLiteral("newName");
0129     const QString topic = QStringLiteral("myTopic");
0130     const QString announcement = QStringLiteral("Happy New Year announcement");
0131     const QString description = QStringLiteral("description");
0132     sampleModel.updateRoom(name, Id, topic, announcement, true, description);
0133     sampleWrapper = sampleModel.findRoomWrapper(Id);
0134     QVERIFY(sampleWrapper);
0135 
0136     QCOMPARE(sampleWrapper->name(), name);
0137     QCOMPARE(sampleWrapper->topic(), topic);
0138     QCOMPARE(sampleWrapper->announcement(), announcement);
0139     QCOMPARE(sampleWrapper->description(), description);
0140     QVERIFY(sampleWrapper->readOnly());
0141     QCOMPARE(spy.count(), 1);
0142 
0143     delete sampleWrapper;
0144     */
0145 }
0146 
0147 void RoomModelTest::shouldUpdateRoomFromQJsonObject()
0148 {
0149     RoomModel sampleModel;
0150     Room *room = nullptr;
0151     QJsonObject roomData;
0152 
0153     const QString name = QStringLiteral("newName");
0154     const QString topic = QStringLiteral("myTopic");
0155     const QString announcement = QStringLiteral("Happy New Year announcement");
0156     roomData.insert(QStringLiteral("rid"), QJsonValue(QLatin1String("RA151100ECE")));
0157     roomData.insert(QStringLiteral("name"), QJsonValue(name));
0158     roomData.insert(QStringLiteral("announcement"), announcement);
0159     roomData.insert(QStringLiteral("topic"), topic);
0160 
0161     QCOMPARE(sampleModel.rowCount(), 0);
0162     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0163     QCOMPARE(sampleModel.rowCount(), 1);
0164 
0165     QSignalSpy spy(&sampleModel, &RoomModel::dataChanged);
0166     sampleModel.updateRoom(roomData);
0167     room = sampleModel.findRoom(QStringLiteral("RA151100ECE"));
0168     QVERIFY(room);
0169 
0170     QCOMPARE(name, room->name());
0171     QCOMPARE(topic, room->topic());
0172     QCOMPARE(announcement, room->announcement());
0173     QCOMPARE(room->readOnly(), false);
0174     QCOMPARE(spy.count(), 1);
0175 }
0176 
0177 void RoomModelTest::shouldUpdateSubcriptionActionRemoved()
0178 {
0179     RoomModel sampleModel;
0180     QJsonArray input;
0181     QJsonObject roomData;
0182     roomData.insert(QStringLiteral("rid"), QJsonValue(QLatin1String("RA151100ECE")));
0183     input.append(QJsonValue(QLatin1String("removed")));
0184     input.append(QJsonValue(roomData));
0185 
0186     QCOMPARE(sampleModel.rowCount(), 0);
0187     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0188     QCOMPARE(sampleModel.rowCount(), 1);
0189     sampleModel.updateSubscription(input);
0190     QCOMPARE(sampleModel.rowCount(), 0);
0191 
0192     roomData.insert(QStringLiteral("rid"), QJsonValue(QLatin1String("RA151100ECE_NEW")));
0193     input.pop_back();
0194     input.append(QJsonValue(roomData));
0195     sampleModel.updateSubscription(input);
0196     QCOMPARE(sampleModel.rowCount(), 0);
0197 }
0198 
0199 void RoomModelTest::shouldUpdateSubcriptionActionInserted()
0200 {
0201     RoomModel sampleModel;
0202     QJsonArray input;
0203     QJsonObject roomData;
0204     roomData.insert(QStringLiteral("rid"), QJsonValue(QLatin1String("RA151100ECE")));
0205     roomData.insert(QStringLiteral("name"), QJsonValue(QLatin1String("myRoom")));
0206     input.append(QJsonValue(QLatin1String("inserted")));
0207     input.append(QJsonValue(roomData));
0208 
0209     QCOMPARE(sampleModel.rowCount(), 0);
0210     sampleModel.updateSubscription(input);
0211     QCOMPARE(sampleModel.rowCount(), 1);
0212 
0213     sampleModel.updateSubscription(input);
0214     QCOMPARE(sampleModel.rowCount(), 1);
0215 }
0216 
0217 void RoomModelTest::shouldUpdateSubcriptionActionUpdated()
0218 {
0219     // TODO rename autotests as it's not updatesubscrition but updateroom
0220     // Update subscription doesn't update topic and co
0221     RoomModel sampleModel;
0222     // QJsonArray input;
0223     QJsonObject roomData;
0224     Room *room = nullptr;
0225 
0226     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0227 
0228     const QString name = QStringLiteral("newName");
0229     const QString topic = QStringLiteral("myTopic");
0230     const QString announcement = QStringLiteral("Happy New Year announcement");
0231     roomData.insert(QStringLiteral("rid"), QJsonValue(QLatin1String("RA151100ECE")));
0232     roomData.insert(QStringLiteral("name"), QJsonValue(name));
0233     roomData.insert(QStringLiteral("announcement"), announcement);
0234     roomData.insert(QStringLiteral("topic"), topic);
0235     //    input.append(QJsonValue(QLatin1String("updated")));
0236     //    input.append(roomData);
0237 
0238     QCOMPARE(sampleModel.rowCount(), 1);
0239     QSignalSpy spy(&sampleModel, &RoomModel::dataChanged);
0240     sampleModel.updateRoom(roomData);
0241     QCOMPARE(sampleModel.rowCount(), 1);
0242 
0243     room = sampleModel.findRoom(QStringLiteral("RA151100ECE"));
0244     QVERIFY(room);
0245 
0246     QCOMPARE(name, room->name());
0247     QCOMPARE(topic, room->topic());
0248     QCOMPARE(announcement, room->announcement());
0249     QCOMPARE(room->readOnly(), false);
0250     QCOMPARE(spy.count(), 1);
0251 }
0252 
0253 void RoomModelTest::shouldClear()
0254 {
0255     RoomModel sampleModel;
0256     QSignalSpy rowInsertedSpy(&sampleModel, &RoomModel::rowsInserted);
0257     QSignalSpy rowABTInserted(&sampleModel, &RoomModel::rowsAboutToBeInserted);
0258 
0259     QCOMPARE(sampleModel.rowCount(), 0);
0260     sampleModel.addRoom(QStringLiteral("RA151100ECE"), QStringLiteral("myRoom"));
0261     QCOMPARE(sampleModel.rowCount(), 1);
0262 
0263     QCOMPARE(rowInsertedSpy.count(), 1);
0264     QCOMPARE(rowABTInserted.count(), 1);
0265     QCOMPARE(TestModelHelpers::rowSpyToText(rowInsertedSpy), QStringLiteral("0,0"));
0266     QCOMPARE(TestModelHelpers::rowSpyToText(rowABTInserted), QStringLiteral("0,0"));
0267 
0268     rowInsertedSpy.clear();
0269     rowABTInserted.clear();
0270 
0271     sampleModel.clear();
0272 
0273     QCOMPARE(sampleModel.rowCount(), 0);
0274     QCOMPARE(rowInsertedSpy.count(), 0);
0275     QCOMPARE(rowABTInserted.count(), 0);
0276 
0277     for (int i = 0; i < 15; i++) {
0278         sampleModel.addRoom(QStringLiteral("RA151100ECE%1").arg(i), QStringLiteral("myRoom%1").arg(i));
0279     }
0280     QCOMPARE(sampleModel.rowCount(), 15);
0281 
0282     rowInsertedSpy.clear();
0283     rowABTInserted.clear();
0284 
0285     sampleModel.clear();
0286 
0287     QCOMPARE(rowInsertedSpy.count(), 0);
0288     QCOMPARE(rowABTInserted.count(), 0);
0289 }
0290 
0291 void RoomModelTest::shouldReset()
0292 {
0293     RoomModel sampleModel;
0294     // RoomWrapper *sampleWrapper;
0295 
0296     const QString Id = QStringLiteral("RA151100ECE");
0297     const QString name = QStringLiteral("myRoom");
0298     QCOMPARE(sampleModel.rowCount(), 0);
0299     sampleModel.addRoom(Id, name);
0300     QCOMPARE(sampleModel.rowCount(), 1);
0301     // TODO: should uncomment this after enabling cache in roomModel
0302     /*
0303         sampleModel.reset();
0304         QCOMPARE(1, sampleModel.rowCount());
0305         sampleWrapper = sampleModel.findRoomWrapper(Id);
0306         QCOMPARE(name, sampleWrapper->name());
0307         */
0308 }
0309 
0310 void RoomModelTest::shouldReturnDataDefault()
0311 {
0312     RoomModel sampleModel;
0313     QVariant output;
0314     QString Id = QStringLiteral("RA151100ECE");
0315     QString name = QStringLiteral("myRoom");
0316     sampleModel.addRoom(Id, name);
0317 
0318     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomName);
0319     QCOMPARE(output.toString(), name);
0320     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomId);
0321     QCOMPARE(output.toString(), Id);
0322     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomSelected);
0323     QCOMPARE(output.toBool(), false);
0324     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomType); // channel type
0325     QCOMPARE(output.value<Room::RoomType>(), Room::RoomType::Unknown);
0326     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomOwnerUserId);
0327     QVERIFY(output.toString().isEmpty());
0328     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomOwnerUserName);
0329     QVERIFY(output.toString().isEmpty());
0330     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomTopic);
0331     QVERIFY(output.toString().isEmpty());
0332     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomMutedUsers);
0333     QVERIFY(output.toStringList().isEmpty());
0334     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomJitsiTimeout);
0335     QCOMPARE(output, QVariant(qint64(-1)));
0336     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomReadOnly);
0337     QCOMPARE(output.toBool(), false);
0338     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomAnnouncement);
0339     QVERIFY(output.toString().isEmpty());
0340     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomUnread);
0341     QCOMPARE(output, QVariant(int(0))); // quint64 not used in room.cpp???
0342     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomFavorite);
0343     QCOMPARE(output.toBool(), false);
0344     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomOpen);
0345     QCOMPARE(output.toBool(), false);
0346     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomSection);
0347     QVERIFY(output.toString().isEmpty());
0348     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomIcon);
0349     QCOMPARE(output, QVariant(QIcon()));
0350 }
0351 
0352 void RoomModelTest::shouldReturnData()
0353 {
0354     Room input(nullptr);
0355     const QString Id = QStringLiteral("RA151100ECE");
0356     const QString name = QStringLiteral("myRoom");
0357     const bool selected = true;
0358     const QString roomType = QStringLiteral("p");
0359     const QString userId = QStringLiteral("sdfsdfs");
0360     const QString userName = QStringLiteral("pp");
0361     const QString topic = QStringLiteral("topic");
0362     const QStringList mutedUsers = QStringList{QStringLiteral("mutedUsers"), QStringLiteral("muted2")};
0363     const qint64 time = 55;
0364     const bool readOnly = true;
0365     const QString announcement = QStringLiteral("AA");
0366     const int unread = 66;
0367     const bool favorite = true;
0368     const bool open = true;
0369 
0370     input.setRoomId(Id);
0371     input.setName(name);
0372     input.setSelected(selected);
0373     input.setChannelType(Room::roomTypeFromString(roomType));
0374     input.setRoomCreatorUserId(userId);
0375     input.setRoomCreatorUserName(userName);
0376     input.setTopic(topic);
0377     input.setMutedUsers(mutedUsers);
0378     input.setJitsiTimeout(time);
0379     input.setReadOnly(readOnly);
0380     input.setAnnouncement(announcement);
0381     input.setUnread(unread);
0382     input.setFavorite(favorite);
0383     input.setOpen(open);
0384 
0385     RoomModel sampleModel;
0386     QVariant output;
0387     QVERIFY(sampleModel.addRoom(&input)); // don't pass address. pass pointer variable
0388 
0389     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomName);
0390     QCOMPARE(output.toString(), name);
0391     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomId);
0392     QCOMPARE(output.toString(), Id);
0393     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomSelected);
0394     QCOMPARE(output.toBool(), selected);
0395     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomType); // channel type
0396     QCOMPARE(Room::roomFromRoomType(output.value<Room::RoomType>()), roomType);
0397     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomOwnerUserId);
0398     QCOMPARE(output.toString(), userId);
0399     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomOwnerUserName);
0400     QCOMPARE(output.toString(), userName);
0401     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomTopic);
0402     QCOMPARE(output.toString(), topic);
0403     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomMutedUsers);
0404     QCOMPARE(output.toStringList(), mutedUsers);
0405     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomJitsiTimeout);
0406     QCOMPARE(output, QVariant(time));
0407     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomReadOnly);
0408     QCOMPARE(output.toBool(), readOnly);
0409     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomAnnouncement);
0410     QCOMPARE(output.toString(), announcement);
0411     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomUnread);
0412     QCOMPARE(output, QVariant(unread)); // quint64 not used in room.cpp???
0413     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomFavorite);
0414     QCOMPARE(output.toBool(), favorite);
0415     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomOpen);
0416     QCOMPARE(output.toBool(), open);
0417     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomSection);
0418     QCOMPARE(output.value<RoomModel::Section>(), RoomModel::Section::Favorites); // first priority for favorites and then to channels
0419     output = sampleModel.data(sampleModel.index(0), RoomModel::RoomIcon);
0420     QCOMPARE(output, QVariant(QIcon::fromTheme(QStringLiteral("lock"))));
0421 }
0422 
0423 void RoomModelTest::shouldInsertRoom_data()
0424 {
0425     QTest::addColumn<QString>("insertRoomFileName");
0426     QTest::addColumn<QString>("roomId");
0427     QTest::newRow("insertroom1") << QStringLiteral("insertroom1") << QStringLiteral("fooid");
0428     QTest::newRow("insertroom2") << QStringLiteral("insertroom2") << QStringLiteral("bla1");
0429 }
0430 
0431 void RoomModelTest::shouldInsertRoom()
0432 {
0433     QFETCH(QString, insertRoomFileName);
0434     QFETCH(QString, roomId);
0435 
0436     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/insert-rooms/") + insertRoomFileName + QLatin1String(".json");
0437     const QJsonObject fields = AutoTestHelper::loadJsonObject(originalJsonFile);
0438 
0439     RoomModel sampleModel;
0440     const QString generatedRoomId = sampleModel.insertRoom(fields);
0441     QCOMPARE(generatedRoomId, roomId);
0442     QCOMPARE(sampleModel.rowCount(), 1);
0443     Room *r = sampleModel.findRoom(generatedRoomId);
0444     QVERIFY(r);
0445 
0446     // qDebug() << " fields"<<fields;
0447 
0448     const QByteArray ba = Room::serialize(r, false);
0449     // qDebug() << " ba " << ba;
0450     const QJsonDocument docSerialized = QJsonDocument::fromJson(ba);
0451 
0452     const QByteArray jsonIndented = docSerialized.toJson(QJsonDocument::Indented);
0453     AutoTestHelper::compareFile(QStringLiteral("/insert-rooms/"), jsonIndented, insertRoomFileName);
0454 
0455     auto m = Room::deserialize(docSerialized.object());
0456     QCOMPARE(*r, *m);
0457 }
0458 
0459 // TODO add autotest for notification update.
0460 
0461 #include "moc_roommodeltest.cpp"