File indexing completed on 2024-12-01 04:35:25

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 "messagetest.h"
0008 #include "messages/message.h"
0009 #include "ruqola_autotest_helper.h"
0010 #include <QCborMap>
0011 #include <QCborValue>
0012 #include <QJsonDocument>
0013 
0014 QTEST_GUILESS_MAIN(MessageTest)
0015 MessageTest::MessageTest(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 void MessageTest::shouldHaveDefaultValues()
0021 {
0022     Message m;
0023     QVERIFY(!m.pendingMessage());
0024     QVERIFY(!m.showIgnoredMessage());
0025     QVERIFY(m.showTranslatedMessage());
0026     QVERIFY(!m.unread());
0027     QVERIFY(!m.isEditingMode());
0028     QVERIFY(!m.hoverHighlight());
0029     QVERIFY(m.localTranslation().isEmpty());
0030     QVERIFY(m.blocks().isEmpty());
0031     QVERIFY(m.attachments().isEmpty());
0032     QCOMPARE(m.discussionCount(), 0);
0033     QVERIFY(!m.goToMessageBackgroundColor().isValid());
0034 }
0035 
0036 // TODO add check for default value ???
0037 
0038 void MessageTest::shouldParseMessage_data()
0039 {
0040     QTest::addColumn<QString>("name");
0041     QTest::addColumn<Message>("expectedMessage");
0042 
0043     {
0044         Message firstMessageRef;
0045         firstMessageRef.setMessageId(QStringLiteral("blaid"));
0046         firstMessageRef.setAlias(QStringLiteral("aliasname"));
0047         firstMessageRef.setGroupable(false);
0048         firstMessageRef.setTimeStamp(1504509615057);
0049         firstMessageRef.setUsername(QStringLiteral("foo.ff"));
0050         firstMessageRef.setName(QStringLiteral("newname"));
0051         firstMessageRef.setUserId(QStringLiteral("qt9uNY9FxyL5QT5no"));
0052         firstMessageRef.setMessageType(Message::NormalText);
0053         firstMessageRef.setUpdatedAt(1504509615063);
0054         firstMessageRef.setEditedAt(-1);
0055         firstMessageRef.setIsStarred(false);
0056         firstMessageRef.setRoomId(QStringLiteral("qt9uNY9FxyL5QT5nouKK39zoewTkdacidH"));
0057         MessageAttachment att;
0058         att.setLink(QStringLiteral("/file-upload/3zfLR3Cjr8YnvD6cS/dd.pdf"));
0059         att.setTitle(QStringLiteral("dd.pdf"));
0060         att.setAttachmentType(MessageAttachment::File);
0061         firstMessageRef.setAttachments({att});
0062         QTest::addRow("first") << QStringLiteral("first") << firstMessageRef;
0063     }
0064     {
0065         Message urlMessageRef;
0066         urlMessageRef.setMessageId(QStringLiteral("9kHnbbjbHKHjXXQp7"));
0067         urlMessageRef.setAlias(QStringLiteral("alias"));
0068         urlMessageRef.setGroupable(false);
0069         urlMessageRef.setName(QStringLiteral("name"));
0070         urlMessageRef.setTimeStamp(1504596899771);
0071         urlMessageRef.setUsername(QStringLiteral("username"));
0072         urlMessageRef.setUserId(QStringLiteral("bjBueJtHsHQECdkmH"));
0073         urlMessageRef.setMessageType(Message::NormalText);
0074         urlMessageRef.setUpdatedAt(1504596901803);
0075         urlMessageRef.setEditedAt(-1);
0076         urlMessageRef.setIsStarred(false);
0077         urlMessageRef.setRoomId(QStringLiteral("dBWXYy4nyBHn8Q7dv"));
0078         urlMessageRef.setText(QStringLiteral("https://foo.com/event/whoa"));
0079         MessageUrl url;
0080         url.setUrl(QStringLiteral("https://foo.com/event/C8CT"));
0081         url.setPageTitle(QStringLiteral("Trainers Panel I"));
0082         url.setDescription(QStringLiteral("View more about this event at CppCon 2017"));
0083         url.setImageUrl(QStringLiteral("http://foo.com/img/app-icon.png?1504596901"));
0084         url.setSiteUrl(QStringLiteral("https://foo.com/event/C8CT"));
0085         url.setImageWidth(200);
0086         url.setImageHeight(200);
0087         urlMessageRef.setUrls({url});
0088 
0089         QTest::addRow("url") << QStringLiteral("url") << urlMessageRef;
0090     }
0091     {
0092         Message urlMessageRef;
0093         urlMessageRef.setMessageId(QStringLiteral("yZPPxBQ79M9jG5hS6"));
0094         urlMessageRef.setGroupable(false);
0095         urlMessageRef.setName(QStringLiteral("test1 test2"));
0096         urlMessageRef.setTimeStamp(1704484544725);
0097         urlMessageRef.setUsername(QStringLiteral("test1"));
0098         urlMessageRef.setUserId(QStringLiteral("uKK39zoekdacidH"));
0099         urlMessageRef.setMessageType(Message::NormalText);
0100         urlMessageRef.setUpdatedAt(1704484544886);
0101         urlMessageRef.setEditedAt(-1);
0102         urlMessageRef.setIsStarred(false);
0103         urlMessageRef.setParseUrls(true);
0104         urlMessageRef.setRoomId(QStringLiteral("wWTrQstz3mex5Mm"));
0105         urlMessageRef.setText(QStringLiteral("https://foo/pull/48/files#diff-142b45cf8aa373eab25fd7a81c45c11fb2bb07e4556b34162497ecb66b4aefb4R149"));
0106         MessageUrl url;
0107         url.setUrl(QStringLiteral("https://foo/pull/48/files#diff-142b45cf8aa373eab25fd7a81c45c11fb2bb07e4556b34162497ecb66b4aefb4R149"));
0108         url.setPageTitle(QStringLiteral("Deferred connection evaluation by blabla · Pull Request #48 · "));
0109         url.setDescription(QStringLiteral("Closes #23"));
0110         url.setImageUrl(QStringLiteral("https://avatar.foo.com/u/84974957?s=400&v=4"));
0111         url.setSiteUrl(QStringLiteral("https://foo/pull/48"));
0112         url.setSiteName(QStringLiteral("GitHub"));
0113         url.setImageWidth(1200);
0114         url.setImageHeight(600);
0115         urlMessageRef.setUrls({url});
0116         QTest::addRow("url1") << QStringLiteral("url1") << urlMessageRef;
0117     }
0118     {
0119         // Image
0120         Message imageMessageRef;
0121         imageMessageRef.setMessageId(QStringLiteral("CD2LsyS4dNbj6TTjQ"));
0122         imageMessageRef.setAlias(QStringLiteral("aliasname"));
0123         imageMessageRef.setName(QStringLiteral("name"));
0124         imageMessageRef.setGroupable(false);
0125         imageMessageRef.setTimeStamp(1505399177844);
0126         imageMessageRef.setUsername(QStringLiteral("username"));
0127         imageMessageRef.setUserId(QStringLiteral("FwHz8w8PWWQRqrJjM"));
0128         imageMessageRef.setMessageType(Message::NormalText);
0129         imageMessageRef.setUpdatedAt(1505399177846);
0130         imageMessageRef.setEditedAt(-1);
0131         imageMessageRef.setIsStarred(false);
0132         imageMessageRef.setRoomId(QStringLiteral("dBWXYy4nyBHn8Q7dv"));
0133 
0134         MessageAttachment attImage;
0135         attImage.setTitle(QStringLiteral("Clipboard"));
0136         attImage.setLink(QStringLiteral("/file-upload/Kt7DBWPe7pnadXDQH/test file"));
0137         attImage.setImageUrlPreview(QStringLiteral("/file-upload/Kt7DBWPe7pnadXDQH/test file"));
0138         attImage.setMimeType(QStringLiteral("image/png"));
0139         attImage.setImageWidth(120);
0140         attImage.setImageHeight(120);
0141         attImage.setAttachmentType(MessageAttachment::Image);
0142         imageMessageRef.setAttachments({attImage});
0143 
0144         QTest::addRow("image") << QStringLiteral("image") << imageMessageRef;
0145         // TODO add Mentions
0146     }
0147 
0148     {
0149         // Video
0150         Message videoMessageRef;
0151         videoMessageRef.setMessageId(QStringLiteral("KCy2KiFYmS5NuakPm"));
0152         videoMessageRef.setAlias(QStringLiteral("Laurent Montel"));
0153         videoMessageRef.setName(QStringLiteral("Laurent Montel"));
0154         videoMessageRef.setGroupable(false);
0155         videoMessageRef.setTimeStamp(1515580853098);
0156         videoMessageRef.setUsername(QStringLiteral("laurent"));
0157         videoMessageRef.setUserId(QStringLiteral("uKK39zoewTkdacidH"));
0158         videoMessageRef.setMessageType(Message::NormalText);
0159         videoMessageRef.setUpdatedAt(1515580853102);
0160         videoMessageRef.setEditedAt(-1);
0161         videoMessageRef.setIsStarred(false);
0162         videoMessageRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6"));
0163 
0164         MessageAttachment attVideo;
0165         attVideo.setTitle(QStringLiteral("SampleVideo_1280x720_1mb.mp4"));
0166         attVideo.setLink(QStringLiteral("/file-upload/sLgmaWowyttg4d2ZD/SampleVideo_1280x720_1mb.mp4"));
0167         attVideo.setDescription(QStringLiteral("test"));
0168         attVideo.setAttachmentType(MessageAttachment::Video);
0169         // Add video size/video type etc.
0170         videoMessageRef.setAttachments({attVideo});
0171 
0172         QTest::addRow("video") << QStringLiteral("video") << videoMessageRef;
0173     }
0174     {
0175         // Audio
0176         Message audioMessageRef;
0177         audioMessageRef.setMessageId(QStringLiteral("AwDsjWKJaW2wCP2ht"));
0178         audioMessageRef.setAlias(QStringLiteral("Laurent Montel"));
0179         audioMessageRef.setName(QStringLiteral("Laurent Montel"));
0180         audioMessageRef.setGroupable(false);
0181         audioMessageRef.setTimeStamp(1515588347098);
0182         audioMessageRef.setUsername(QStringLiteral("laurent"));
0183         audioMessageRef.setUserId(QStringLiteral("uKK39zoewTkdacidH"));
0184         audioMessageRef.setMessageType(Message::NormalText);
0185         audioMessageRef.setUpdatedAt(1515588347102);
0186         audioMessageRef.setEditedAt(-1);
0187         audioMessageRef.setIsStarred(false);
0188         audioMessageRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6"));
0189 
0190         MessageAttachment attAudio;
0191         attAudio.setTitle(QStringLiteral("joint.wav"));
0192         attAudio.setLink(QStringLiteral("/file-upload/9E8YBGgq3H6GbASf3/joint.wav"));
0193         attAudio.setDescription(QStringLiteral("dfgsdfgsdfg sdfgd dfsg sfd g"));
0194         attAudio.setAttachmentType(MessageAttachment::Audio);
0195         // Add video size/video type etc.
0196         audioMessageRef.setAttachments({attAudio});
0197 
0198         QTest::addRow("audio") << QStringLiteral("audio") << audioMessageRef;
0199     }
0200     {
0201         // Message Starred
0202         Message messageStarredRef;
0203         messageStarredRef.setMessageId(QStringLiteral("R6AkSJ7orEkYHNcZJ"));
0204         messageStarredRef.setAlias(QStringLiteral("Laurent"));
0205         messageStarredRef.setName(QStringLiteral("Laurent"));
0206         messageStarredRef.setGroupable(false);
0207         messageStarredRef.setTimeStamp(1516711967347);
0208         messageStarredRef.setUsername(QStringLiteral("laurent"));
0209         messageStarredRef.setUserId(QStringLiteral("uKK39zoewTkdacidH"));
0210         messageStarredRef.setMessageType(Message::NormalText);
0211         messageStarredRef.setUpdatedAt(1516721464681);
0212         messageStarredRef.setEditedAt(1516712012309);
0213         messageStarredRef.setEditedByUsername(QStringLiteral("laurent"));
0214         messageStarredRef.setEditedByUserId(QStringLiteral("uKK39zoewTkdacidH"));
0215         messageStarredRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6"));
0216         messageStarredRef.setIsStarred(true);
0217         messageStarredRef.setText(QStringLiteral("sefssssdfsefdsdfsd"));
0218 
0219         QTest::addRow("messagestarred") << QStringLiteral("messagestarred") << messageStarredRef;
0220     }
0221     {
0222         // Message Starred
0223         Message messagePinnedRef;
0224         messagePinnedRef.setMessageId(QStringLiteral("4eawfwyFnt7L8wTwE"));
0225         messagePinnedRef.setAlias(QStringLiteral("Laurent"));
0226         messagePinnedRef.setName(QStringLiteral("Laurent Montel"));
0227         messagePinnedRef.setGroupable(false);
0228         messagePinnedRef.setTimeStamp(1550240990669);
0229         messagePinnedRef.setUsername(QStringLiteral("laurent"));
0230         messagePinnedRef.setUserId(QStringLiteral("uKK39zoewTkdacidH"));
0231         messagePinnedRef.setMessageType(Message::NormalText);
0232         messagePinnedRef.setUpdatedAt(1550240994061);
0233         messagePinnedRef.setRoomId(QStringLiteral("zMHhMfsEPvKjgFuyE"));
0234         messagePinnedRef.setIsStarred(false);
0235         MessagePinned pinned;
0236         pinned.setPinned(true);
0237         pinned.setPinnedBy(QStringLiteral("laurent"));
0238         messagePinnedRef.setMessagePinned(pinned);
0239         messagePinnedRef.setText(QStringLiteral("sdfgsdfg"));
0240 
0241         QTest::addRow("messagepinned") << QStringLiteral("messagepinned") << messagePinnedRef;
0242     }
0243 
0244     {
0245         // Message Remove Starred status
0246         Message messageStarredRemovedRef;
0247         messageStarredRemovedRef.setMessageId(QStringLiteral("R6AkSJ7orEkYHNcZJ"));
0248         messageStarredRemovedRef.setAlias(QStringLiteral("Laurent"));
0249         messageStarredRemovedRef.setName(QStringLiteral("Laurent"));
0250         messageStarredRemovedRef.setGroupable(false);
0251         messageStarredRemovedRef.setTimeStamp(1516711967347);
0252         messageStarredRemovedRef.setUsername(QStringLiteral("laurent"));
0253         messageStarredRemovedRef.setUserId(QStringLiteral("uKK39zoewTkdacidH"));
0254         messageStarredRemovedRef.setMessageType(Message::NormalText);
0255         messageStarredRemovedRef.setUpdatedAt(1516721464681);
0256         messageStarredRemovedRef.setEditedAt(1516712012309);
0257         messageStarredRemovedRef.setEditedByUsername(QStringLiteral("laurent"));
0258         messageStarredRemovedRef.setEditedByUserId(QStringLiteral("uKK39zoewTkdacidH"));
0259         messageStarredRemovedRef.setRoomId(QStringLiteral("kGtPa6bu7xHrS5xz6"));
0260         messageStarredRemovedRef.setIsStarred(false);
0261         messageStarredRemovedRef.setText(QStringLiteral("sefssssdfsefdsdfsd"));
0262 
0263         QTest::addRow("messageremovedstarred") << QStringLiteral("messageremovedstarred") << messageStarredRemovedRef;
0264     }
0265     {
0266         // messageattachmentfile
0267         Message messageAttachmentFileRef;
0268         messageAttachmentFileRef.setMessageId(QStringLiteral("messageid"));
0269         messageAttachmentFileRef.setAlias(QStringLiteral("Laurent"));
0270         messageAttachmentFileRef.setName(QStringLiteral("Laurent Montel"));
0271         messageAttachmentFileRef.setGroupable(false);
0272         messageAttachmentFileRef.setTimeStamp(1520540888833);
0273         messageAttachmentFileRef.setUsername(QStringLiteral("laurent"));
0274         messageAttachmentFileRef.setUserId(QStringLiteral("39zoewTkdacidH"));
0275         messageAttachmentFileRef.setMessageType(Message::NormalText);
0276         messageAttachmentFileRef.setUpdatedAt(1520540888836);
0277         messageAttachmentFileRef.setEditedAt(-1);
0278         messageAttachmentFileRef.setRoomId(QStringLiteral("Pa6bu7xHrS5xz6"));
0279         messageAttachmentFileRef.setIsStarred(false);
0280 
0281         MessageAttachment fileAttachment;
0282         fileAttachment.setTitle(QStringLiteral("webkit.txt"));
0283         fileAttachment.setLink(QStringLiteral("/file-upload/tmqfdJTCmJ3oqG/webkit.txt"));
0284         fileAttachment.setDescription(QStringLiteral("description"));
0285         fileAttachment.setAttachmentType(MessageAttachment::File);
0286         // Add video size/video type etc.
0287         messageAttachmentFileRef.setAttachments({fileAttachment});
0288 
0289         QTest::addRow("messageattachmentfile") << QStringLiteral("messageattachmentfile") << messageAttachmentFileRef;
0290     }
0291 }
0292 
0293 void MessageTest::shouldParseMessage()
0294 {
0295     QFETCH(QString, name);
0296     QFETCH(Message, expectedMessage);
0297     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/") + name + QLatin1String(".json");
0298     QFile f(originalJsonFile);
0299     QVERIFY(f.open(QIODevice::ReadOnly));
0300     const QByteArray content = f.readAll();
0301     f.close();
0302     const QJsonDocument doc = QJsonDocument::fromJson(content);
0303     QJsonObject obj = doc.object();
0304     Message originalMessage;
0305     originalMessage.parseMessage(obj);
0306     const bool messageIsEqual = (originalMessage == expectedMessage);
0307     if (!messageIsEqual) {
0308         qDebug() << "originalMessage " << originalMessage;
0309         qDebug() << "ExpectedMessage " << expectedMessage;
0310     }
0311     QVERIFY(messageIsEqual);
0312 }
0313 
0314 void MessageTest::shouldSerializeData()
0315 {
0316     {
0317         Message input;
0318 
0319         input.setMessageId(QStringLiteral("ff"));
0320         input.setRoomId(QStringLiteral("room1"));
0321         input.setText(QStringLiteral("message1"));
0322         input.setTimeStamp(42);
0323         input.setUsername(QStringLiteral("user1"));
0324         input.setUserId(QStringLiteral("userid1"));
0325         input.setUpdatedAt(45);
0326         input.setEditedAt(89);
0327         input.setEditedByUsername(QStringLiteral("editeduser1"));
0328         input.setEditedByUserId(QStringLiteral("editedbyid1"));
0329         input.setAlias(QStringLiteral("ali"));
0330         input.setAvatar(QStringLiteral("avatar1"));
0331         input.setSystemMessageType(QStringLiteral("type"));
0332         input.setGroupable(true);
0333         input.setParseUrls(true);
0334         input.setRole(QStringLiteral("leader"));
0335         input.setMessageType(Message::MessageType::NormalText);
0336         // It will break as it's not supported yet
0337         input.setIsStarred(true);
0338 
0339         QVector<MessageAttachment> lstAttachement;
0340         MessageAttachment attachment;
0341         attachment.setDescription(QStringLiteral("foo1"));
0342         attachment.setTitle(QStringLiteral("foo2"));
0343         attachment.setLink(QStringLiteral("foo3"));
0344         lstAttachement.append(std::move(attachment));
0345         MessageAttachment attachment2;
0346         attachment2.setDescription(QStringLiteral("foo5"));
0347         attachment2.setTitle(QStringLiteral("foo6"));
0348         attachment2.setLink(QStringLiteral("foo7"));
0349         lstAttachement.append(std::move(attachment2));
0350         input.setAttachments(lstAttachement);
0351 
0352         QVector<MessageUrl> lstUrls;
0353         MessageUrl url1;
0354         url1.setUrl(QStringLiteral("foo1"));
0355         url1.setPageTitle(QStringLiteral("foo2"));
0356         lstUrls.append(std::move(url1));
0357         MessageUrl url2;
0358         url2.setUrl(QStringLiteral("foo5"));
0359         url2.setPageTitle(QStringLiteral("foo6"));
0360         lstUrls.append(std::move(url2));
0361         input.setUrls(lstUrls);
0362 
0363         const QByteArray ba = Message::serialize(input);
0364         // Message output = Message::fromJSon(QJsonObject(QJsonDocument::fromBinaryData(ba).object()));
0365         const Message output = Message::deserialize(QCborValue::fromCbor(ba).toMap().toJsonObject());
0366         QCOMPARE(input, output);
0367         // TODO add Mentions
0368 
0369         QVERIFY(output.wasEdited());
0370     }
0371     {
0372         Message input;
0373 
0374         input.setMessageId(QStringLiteral("ff"));
0375         input.setRoomId(QStringLiteral("room1"));
0376         input.setText(QStringLiteral("message1"));
0377         input.setTimeStamp(42);
0378         input.setUsername(QStringLiteral("user1"));
0379         input.setUserId(QStringLiteral("userid1"));
0380         input.setUpdatedAt(45);
0381         input.setEditedAt(89);
0382         input.setEditedByUsername(QStringLiteral("editeduser1"));
0383         input.setEditedByUserId(QStringLiteral("editedbyid1"));
0384         input.setAlias(QStringLiteral("ali"));
0385         input.setAvatar(QStringLiteral("avatar1"));
0386         input.setSystemMessageType(QStringLiteral("type"));
0387         input.setGroupable(true);
0388         input.setParseUrls(true);
0389         input.setRole(QStringLiteral("leader"));
0390         input.setMessageType(Message::MessageType::NormalText);
0391         // It will break as it's not supported yet
0392         input.setIsStarred(true);
0393 
0394         // Attachments
0395         QVector<MessageAttachment> lstAttachement;
0396         {
0397             MessageAttachment attachment;
0398             attachment.setDescription(QStringLiteral("foo1"));
0399             attachment.setTitle(QStringLiteral("foo2"));
0400             attachment.setLink(QStringLiteral("foo3"));
0401             lstAttachement.append(std::move(attachment));
0402         }
0403         MessageAttachment attachment2;
0404         {
0405             attachment2.setDescription(QStringLiteral("foo5"));
0406             attachment2.setTitle(QStringLiteral("foo6"));
0407             attachment2.setLink(QStringLiteral("foo7"));
0408             lstAttachement.append(std::move(attachment2));
0409             input.setAttachments(lstAttachement);
0410         }
0411 
0412         // Urls
0413         QVector<MessageUrl> lstUrls;
0414         {
0415             MessageUrl url1;
0416             url1.setUrl(QStringLiteral("foo1"));
0417             url1.setPageTitle(QStringLiteral("foo2"));
0418             lstUrls.append(std::move(url1));
0419         }
0420         {
0421             MessageUrl url2;
0422             url2.setUrl(QStringLiteral("foo5"));
0423             url2.setPageTitle(QStringLiteral("foo6"));
0424             lstUrls.append(std::move(url2));
0425         }
0426         input.setUrls(lstUrls);
0427 
0428         // Reactions
0429         QVector<Reaction> reacts;
0430         {
0431             Reaction a;
0432             a.setUserNames({QStringLiteral("bla"), QStringLiteral("foo")});
0433             a.setReactionName(QStringLiteral(":)"));
0434             reacts.append(a);
0435         }
0436         {
0437             Reaction a;
0438             a.setUserNames({QStringLiteral("bli"), QStringLiteral("aa")});
0439             a.setReactionName(QStringLiteral(":foo:"));
0440             reacts.append(a);
0441         }
0442 
0443         Reactions reactions;
0444         reactions.setReactions(reacts);
0445         input.setReactions(reactions);
0446 
0447         // Message Translation
0448         MessageTranslation l;
0449         l.setTranslatedString({{QStringLiteral("foo"), QStringLiteral("bla")}});
0450         input.setMessageTranslation(l);
0451 
0452         // LocalTranslation
0453         input.setLocalTranslation(QStringLiteral("message fr"));
0454 
0455         // Mention
0456         QMap<QString, QString> mentions;
0457         mentions.insert(QStringLiteral("bla1"), QStringLiteral("foo1"));
0458         mentions.insert(QStringLiteral("bla2"), QStringLiteral("foo2"));
0459         mentions.insert(QStringLiteral("bla3"), QStringLiteral("foo3"));
0460         mentions.insert(QStringLiteral("bla4"), QStringLiteral("foo4"));
0461         input.setMentions(mentions);
0462 
0463         // Channels
0464         QMap<QString, QString> channels;
0465         channels.insert(QStringLiteral("channel1"), QStringLiteral("foo-1"));
0466         channels.insert(QStringLiteral("channel2"), QStringLiteral("foo-2"));
0467         channels.insert(QStringLiteral("channel3"), QStringLiteral("foo-3"));
0468         channels.insert(QStringLiteral("channel4"), QStringLiteral("foo-4"));
0469         input.setChannels(channels);
0470 
0471         // Starred
0472         MessageStarred starred;
0473         starred.setIsStarred(true);
0474         input.setMessageStarred(starred);
0475 
0476         // MessagePinned
0477         MessagePinned pinned;
0478         pinned.setPinned(true);
0479         pinned.setPinnedBy(QStringLiteral("kla"));
0480         input.setMessagePinned(pinned);
0481 
0482         // Blocks
0483         QVector<Block> blocks;
0484         {
0485             Block b;
0486             b.setBlockId(QStringLiteral("block-id1"));
0487             b.setAppId(QStringLiteral("appid-1"));
0488             b.setBlockType(Block::VideoConf);
0489             blocks.append(b);
0490         }
0491         {
0492             Block b;
0493             b.setBlockId(QStringLiteral("block-id2"));
0494             b.setAppId(QStringLiteral("appid-2"));
0495             b.setBlockType(Block::VideoConf);
0496             blocks.append(b);
0497         }
0498         input.setBlocks(blocks);
0499 
0500         // Replies
0501         input.setReplies({QStringLiteral("reply1"), QStringLiteral("reply2")});
0502 
0503         // Discussion
0504         input.setDiscussionRoomId(QStringLiteral("discussion1"));
0505         input.setDiscussionCount(55);
0506         input.setDiscussionLastMessage(666);
0507 
0508         // Thread
0509         input.setThreadMessageId(QStringLiteral("thread1"));
0510         input.setThreadLastMessage(7777);
0511         input.setThreadCount(4);
0512 
0513         const QByteArray ba = Message::serialize(input);
0514         // Message output = Message::fromJSon(QJsonObject(QJsonDocument::fromBinaryData(ba).object()));
0515         const Message output = Message::deserialize(QCborValue::fromCbor(ba).toMap().toJsonObject());
0516         const bool compare = (input == output);
0517         if (!compare) {
0518             qDebug() << "input: " << input;
0519             qDebug() << "output: " << output << QCborValue::fromCbor(ba).toMap().toJsonObject();
0520         }
0521         QCOMPARE(input, output);
0522         QVERIFY(output.wasEdited());
0523     }
0524 }
0525 
0526 void MessageTest::shouldParseJsonMessage_data()
0527 {
0528     QTest::addColumn<QString>("fileName");
0529     QTest::newRow("empty") << QStringLiteral("empty");
0530     QTest::newRow("standardmessage") << QStringLiteral("standardmessage");
0531     QTest::newRow("attachmentimage") << QStringLiteral("attachmentimage");
0532     QTest::newRow("attachment-author") << QStringLiteral("attachment-author");
0533     QTest::newRow("message-url") << QStringLiteral("message-url");
0534     QTest::newRow("message-url-meta-info") << QStringLiteral("message-url-meta-info");
0535     QTest::newRow("message-reactions") << QStringLiteral("message-reactions");
0536     QTest::newRow("animatedimage") << QStringLiteral("animatedimage");
0537     QTest::newRow("emojimessage") << QStringLiteral("emojimessage");
0538     QTest::newRow("attachmentaudio") << QStringLiteral("attachmentaudio");
0539     QTest::newRow("attachmentimage-preview") << QStringLiteral("attachmentimage-preview");
0540     QTest::newRow("message-attachment-link") << QStringLiteral("message-attachment-link");
0541     // TODO fix them
0542     QTest::newRow("message-bug-435718") << QStringLiteral("message-bug-435718");
0543     QTest::newRow("message-bug-435718-2") << QStringLiteral("message-bug-435718-2");
0544     QTest::newRow("message-bug-435718-3") << QStringLiteral("message-bug-435718-3");
0545     QTest::newRow("message-channel-mentions") << QStringLiteral("message-channel-mentions");
0546 }
0547 
0548 void MessageTest::shouldParseJsonMessage()
0549 {
0550     QFETCH(QString, fileName);
0551     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/messages/") + fileName + QLatin1String(".json");
0552     QFile f(originalJsonFile);
0553     QVERIFY(f.open(QIODevice::ReadOnly));
0554     const QByteArray content = f.readAll();
0555     f.close();
0556     const QJsonDocument doc = QJsonDocument::fromJson(content);
0557     const QJsonObject fields = doc.object();
0558 
0559     Message r;
0560     r.parseMessage(fields);
0561     // qDebug() << " fields"<<fields;
0562 
0563     const QByteArray ba = Message::serialize(r, false);
0564     // qDebug() << " ba " << ba;
0565     const QJsonDocument docSerialized = QJsonDocument::fromJson(ba);
0566 
0567     const QByteArray jsonIndented = docSerialized.toJson(QJsonDocument::Indented);
0568     AutoTestHelper::compareFile(QStringLiteral("/messages/"), jsonIndented, fileName);
0569 
0570     Message m = Message::deserialize(docSerialized.object());
0571     bool compareMessage = (r == m);
0572     if (!compareMessage) {
0573         qDebug() << "loaded message" << r;
0574         qDebug() << "fromJson " << m;
0575     }
0576     QVERIFY(compareMessage);
0577 }
0578 
0579 void MessageTest::shouldUpdateJsonMessage_data()
0580 {
0581     QTest::addColumn<QString>("fileNameinit");
0582     QTest::addColumn<QStringList>("fileNameupdate");
0583     QTest::newRow("standardmessage") << QStringLiteral("standardmessage") << QStringList();
0584     QTest::newRow("message1-init") << QStringLiteral("message1-init") << QStringList();
0585     QTest::newRow("message1") << QStringLiteral("message1") << (QStringList() << QStringLiteral("message1-updated"));
0586     QTest::newRow("message2") << QStringLiteral("message2")
0587                               << (QStringList() << QStringLiteral("message2-updated") << QStringLiteral("message2-updated-stared"));
0588     QTest::newRow("message3") << QStringLiteral("message3") << (QStringList() << QStringLiteral("message3-updated"));
0589     // TODO add more !
0590 }
0591 
0592 void MessageTest::shouldUpdateJsonMessage()
0593 {
0594     QFETCH(QString, fileNameinit);
0595     QFETCH(QStringList, fileNameupdate);
0596 
0597     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/messages-updated/") + fileNameinit + QLatin1String(".json");
0598     QFile f(originalJsonFile);
0599     QVERIFY(f.open(QIODevice::ReadOnly));
0600     const QByteArray content = f.readAll();
0601     f.close();
0602     const QJsonDocument doc = QJsonDocument::fromJson(content);
0603     const QJsonObject fields = doc.object();
0604 
0605     Message r;
0606     r.parseMessage(fields);
0607 
0608     for (const QString &updateFile : fileNameupdate) {
0609         const QString originalUpdateJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/messages-updated/") + updateFile + QLatin1String(".json");
0610         QFile f(originalUpdateJsonFile);
0611         QVERIFY(f.open(QIODevice::ReadOnly));
0612         const QByteArray content = f.readAll();
0613         f.close();
0614         const QJsonDocument doc = QJsonDocument::fromJson(content);
0615         const QJsonObject fields = doc.object();
0616 
0617         r.parseMessage(fields);
0618     }
0619 
0620     // qDebug() << " fields"<<fields;
0621     const QByteArray ba = Message::serialize(r, false);
0622     // qDebug() << " ba " << ba;
0623     const QJsonDocument docSerialized = QJsonDocument::fromJson(ba);
0624 
0625     const QByteArray jsonIndented = docSerialized.toJson(QJsonDocument::Indented);
0626     AutoTestHelper::compareFile(QStringLiteral("/messages-updated/"), jsonIndented, fileNameinit);
0627 
0628     Message m = Message::deserialize(docSerialized.object());
0629     const bool compareMessage = (r == m);
0630     if (!compareMessage) {
0631         qDebug() << "loaded message" << r;
0632         qDebug() << "fromJson " << m;
0633     }
0634     QVERIFY(compareMessage);
0635 }
0636 
0637 #include "moc_messagetest.cpp"