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

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 "utilstest.h"
0008 #include "utils.h"
0009 #include <QJsonArray>
0010 #include <QJsonDocument>
0011 #include <QJsonObject>
0012 #include <QTest>
0013 
0014 QTEST_GUILESS_MAIN(UtilsTest)
0015 
0016 UtilsTest::UtilsTest(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 void UtilsTest::shouldGenerateServerUrl_data()
0022 {
0023     QTest::addColumn<QString>("input");
0024     QTest::addColumn<QUrl>("output");
0025     QTest::newRow("empty") << QString() << QUrl();
0026     QTest::newRow("http") << QStringLiteral("http://foo.kde.org/") << QUrl(QStringLiteral("ws://foo.kde.org//websocket"));
0027     QTest::newRow("http2") << QStringLiteral("http://foo.kde.org") << QUrl(QStringLiteral("ws://foo.kde.org/websocket"));
0028     QTest::newRow("https") << QStringLiteral("https://foo.kde.org") << QUrl(QStringLiteral("wss://foo.kde.org/websocket"));
0029     QTest::newRow("withoutscheme") << QStringLiteral("foo.kde.org") << QUrl(QStringLiteral("wss://foo.kde.org/websocket"));
0030 }
0031 
0032 void UtilsTest::shouldGenerateServerUrl()
0033 {
0034     QFETCH(QString, input);
0035     QFETCH(QUrl, output);
0036     QCOMPARE(Utils::generateServerUrl(input), output);
0037 }
0038 
0039 void UtilsTest::shouldExtractRoomUserFromUrl_data()
0040 {
0041     QTest::addColumn<QString>("input");
0042     QTest::addColumn<QString>("output");
0043     QTest::newRow("empty") << QString() << QString();
0044 
0045     QTest::newRow("extractuser") << QStringLiteral("ruqola:/user/foo") << QStringLiteral("foo");
0046     QTest::newRow("extractroom") << QStringLiteral("ruqola:/room/foo") << QStringLiteral("foo");
0047     QTest::newRow("extractuser2") << QStringLiteral("ruqola:/user2/foo") << QStringLiteral("ruqola:/user2/foo");
0048 }
0049 
0050 void UtilsTest::shouldExtractRoomUserFromUrl()
0051 {
0052     QFETCH(QString, input);
0053     QFETCH(QString, output);
0054     QCOMPARE(Utils::extractRoomUserFromUrl(input), output);
0055 }
0056 
0057 void UtilsTest::shouldConvertTextWithUrl_data()
0058 {
0059     QTest::addColumn<QString>("text");
0060     QTest::addColumn<QString>("convertedText");
0061     QTest::newRow("empty") << QString() << QString();
0062     QTest::newRow("onlytext") << QStringLiteral("foo bla bli") << QStringLiteral("foo bla bli");
0063     QTest::newRow("test1") << QStringLiteral("[blo](http://www.kde.org)") << QStringLiteral("<a href='http://www.kde.org'>blo</a>");
0064     QTest::newRow("test2") << QStringLiteral("[](http://www.kde.org)") << QStringLiteral("(http://www.kde.org)");
0065     QTest::newRow("test3") << QStringLiteral("bla bla [blo](http://www.kde.org)") << QStringLiteral("bla bla <a href='http://www.kde.org'>blo</a>");
0066     QTest::newRow("test4") << QStringLiteral("bla bla [blo](http://www.kde.org) bli [blu](http://www.kdi.org)")
0067                            << QStringLiteral("bla bla <a href='http://www.kde.org'>blo</a> bli <a href='http://www.kdi.org'>blu</a>");
0068     QTest::newRow("test5") << QStringLiteral("bla bla [blo]") << QStringLiteral("bla bla [blo]");
0069     QTest::newRow("test6") << QStringLiteral("bla bla [blo] bli") << QStringLiteral("bla bla [blo] bli");
0070     // Test <https://www.kde.org|bla>
0071     QTest::newRow("[https://www.kde.org|bla]") << QStringLiteral(
0072         "bla [<a href=\"https://www.kde.org.|https://www.kde.org\">https://www.kde.org.|https://www.kde.org</a>]")
0073                                                << QStringLiteral("bla <a href=\"https://www.kde.org\">https://www.kde.org.</a>");
0074 #if 0
0075     QTest::newRow("[https://www.kde.org|bla]") << QStringLiteral("[https://www.kde.org|https://www.kde.org/bla]")
0076                                                << QStringLiteral("<a href='https://www.kde.org/bla'>https://www.kde.org</a>");
0077     QTest::newRow("[https://www.kde.com/pages/viewpage.action?pageId=111111.|https://www.kde.com/pages/viewpage.action?pageId=111111]")
0078         << QStringLiteral("[https://www.kde.com//pages/viewpage.action?pageId=111111.|https://www.kde.com//pages/viewpage.action?pageId=111111]")
0079         << QStringLiteral("<a href='https://www.kde.com//pages/viewpage.action?pageId=111111'>https://www.kde.com//pages/viewpage.action?pageId=111111.</a>");
0080     QTest::newRow("blabla [https://www.kde.org|https://www.kde.org/bla]") << QStringLiteral("blabla [https://www.kde.org|https://www.kde.org/bla]")
0081                                                                           << QStringLiteral("blabla <a href='https://www.kde.org/bla'>https://www.kde.org</a>");
0082 
0083     QTest::newRow("blabla [https://www.kde.org|https://www.kde.org/bla] 2 ")
0084         << QStringLiteral(
0085                "ideas: [https://www.kde.com/pages/viewpage.action?pageId=11111.|https://www.kde.com/pages/viewpage.action?pageId=11111]\r\n [~vvvv] can")
0086         << QStringLiteral(
0087                "ideas: <a href='https://www.kde.com/pages/viewpage.action?pageId=11111'>https://www.kde.com/pages/viewpage.action?pageId=11111.</a>\r\n "
0088                "[~vvvv] can");
0089 #endif
0090 }
0091 
0092 void UtilsTest::shouldConvertTextWithUrl()
0093 {
0094     QFETCH(QString, text);
0095     QFETCH(QString, convertedText);
0096     QCOMPARE(Utils::convertTextWithUrl(text), convertedText);
0097 }
0098 
0099 void UtilsTest::shouldGenerateAvatarUrl_data()
0100 {
0101     QTest::addColumn<QString>("serverUrl");
0102     QTest::addColumn<Utils::AvatarInfo>("avatarInfo");
0103     QTest::addColumn<QUrl>("result");
0104     {
0105         Utils::AvatarInfo avatarInfo;
0106         QTest::newRow("empty") << QString() << avatarInfo << QUrl();
0107     }
0108     {
0109         Utils::AvatarInfo avatarInfo;
0110         avatarInfo.identifier = QStringLiteral("user1");
0111         avatarInfo.avatarType = Utils::AvatarType::User;
0112         QTest::newRow("user1") << QStringLiteral("http://www.kde.org") << avatarInfo
0113                                << QUrl(QStringLiteral("http://www.kde.org/avatar/%1?format=png&size=22").arg(avatarInfo.identifier));
0114     }
0115     {
0116         Utils::AvatarInfo avatarInfo;
0117         avatarInfo.identifier = QStringLiteral("user1");
0118         avatarInfo.avatarType = Utils::AvatarType::User;
0119         avatarInfo.etag = QStringLiteral("etag-user-identifier");
0120         QTest::newRow("user1-etag")
0121             << QStringLiteral("http://www.kde.org") << avatarInfo
0122             << QUrl(QStringLiteral("http://www.kde.org/avatar/%1?format=png&etag=%2&size=22").arg(avatarInfo.identifier, avatarInfo.etag));
0123     }
0124     {
0125         Utils::AvatarInfo avatarInfo;
0126         avatarInfo.identifier = QStringLiteral("room1");
0127         avatarInfo.avatarType = Utils::AvatarType::Room;
0128         QTest::newRow("room1") << QStringLiteral("http://www.kde.org") << avatarInfo
0129                                << QUrl(QStringLiteral("http://www.kde.org/avatar/room/%1?format=png&size=22").arg(avatarInfo.identifier));
0130     }
0131     {
0132         Utils::AvatarInfo avatarInfo;
0133         avatarInfo.identifier = QStringLiteral("room1");
0134         avatarInfo.avatarType = Utils::AvatarType::Room;
0135         avatarInfo.etag = QStringLiteral("etagIdentifier");
0136         QTest::newRow("room1-etag")
0137             << QStringLiteral("http://www.kde.org") << avatarInfo
0138             << QUrl(QStringLiteral("http://www.kde.org/avatar/room/%1?format=png&etag=%2&size=22").arg(avatarInfo.identifier, avatarInfo.etag));
0139     }
0140     {
0141         Utils::AvatarInfo avatarInfo;
0142         avatarInfo.identifier = QStringLiteral("room1");
0143         avatarInfo.avatarType = Utils::AvatarType::Room;
0144         avatarInfo.etag = QStringLiteral("etagIdentifier");
0145         QTest::newRow("room1-etag-without-protocol")
0146             << QStringLiteral("www.kde.org") << avatarInfo
0147             << QUrl(QStringLiteral("https://www.kde.org/avatar/room/%1?format=png&etag=%2&size=22").arg(avatarInfo.identifier, avatarInfo.etag));
0148     }
0149 }
0150 
0151 void UtilsTest::shouldGenerateCheckMark_data()
0152 {
0153     QTest::addColumn<QString>("text");
0154     QTest::addColumn<QString>("convertedText");
0155     QTest::newRow("empty") << QString() << QString();
0156     QTest::newRow("unckecked1") << QStringLiteral("- [ ] vvv") << QStringLiteral(":white_medium_square: vvv");
0157     QTest::newRow("unckecked2") << QStringLiteral("- [ ] vvv\n- [ ] bla2") << QStringLiteral(":white_medium_square: vvv\n:white_medium_square: bla2");
0158 
0159     QTest::newRow("ckecked1") << QStringLiteral("- [x] vvv") << QStringLiteral(":ballot_box_with_check: vvv");
0160     QTest::newRow("ckecked2") << QStringLiteral("- [x] vvv\n- [x] bla2") << QStringLiteral(":ballot_box_with_check: vvv\n:ballot_box_with_check: bla2");
0161 
0162     QTest::newRow("mixted1") << QStringLiteral("- [x] vvv\n- [ ] bla2") << QStringLiteral(":ballot_box_with_check: vvv\n:white_medium_square: bla2");
0163 }
0164 
0165 void UtilsTest::shouldGenerateCheckMark()
0166 {
0167     QFETCH(QString, text);
0168     QFETCH(QString, convertedText);
0169     QCOMPARE(Utils::convertTextWithCheckMark(text), convertedText);
0170 }
0171 
0172 void UtilsTest::shouldGenerateAvatarUrl()
0173 {
0174     QFETCH(QString, serverUrl);
0175     QFETCH(Utils::AvatarInfo, avatarInfo);
0176     QFETCH(QUrl, result);
0177     QCOMPARE(Utils::avatarUrl(serverUrl, avatarInfo), result);
0178 }
0179 
0180 void UtilsTest::shouldTestAvatarInfoValues()
0181 {
0182     Utils::AvatarInfo info;
0183     QVERIFY(!info.isValid());
0184     QVERIFY(info.etag.isEmpty());
0185     QVERIFY(info.identifier.isEmpty());
0186     QCOMPARE(info.avatarType, Utils::AvatarType::Unknown);
0187 
0188     info.identifier = QStringLiteral("bla");
0189     QCOMPARE(info.generateAvatarIdentifier(), QStringLiteral("bla"));
0190     info.etag = QStringLiteral("foo");
0191     QCOMPARE(info.generateAvatarIdentifier(), QStringLiteral("bla-foo"));
0192 }
0193 
0194 void UtilsTest::shouldGenerateHeader_data()
0195 {
0196     QTest::addColumn<QString>("text");
0197     QTest::addColumn<QString>("convertedText");
0198     QTest::newRow("empty") << QString() << QString();
0199     QTest::newRow("h1") << QStringLiteral("# bla") << QStringLiteral("<h1>bla</h1>");
0200     QTest::newRow("h1-1") << QStringLiteral("# bla\nsss") << QStringLiteral("<h1>bla</h1>sss");
0201     QTest::newRow("h1-2") << QStringLiteral("# bla\nsss \n sdfsdf") << QStringLiteral("<h1>bla</h1>sss \n sdfsdf");
0202 
0203     QTest::newRow("h2") << QStringLiteral("## bla") << QStringLiteral("<h2>bla</h2>");
0204     QTest::newRow("h2-1") << QStringLiteral("## bla\nsss") << QStringLiteral("<h2>bla</h2>sss");
0205     QTest::newRow("h2-2") << QStringLiteral("## bla\nsss \n sdfsdf") << QStringLiteral("<h2>bla</h2>sss \n sdfsdf");
0206 
0207     QTest::newRow("h3") << QStringLiteral("### bla") << QStringLiteral("<h3>bla</h3>");
0208     QTest::newRow("h3-1") << QStringLiteral("### bla\nsss") << QStringLiteral("<h3>bla</h3>sss");
0209     QTest::newRow("h3-2") << QStringLiteral("### bla\nsss \n sdfsdf") << QStringLiteral("<h3>bla</h3>sss \n sdfsdf");
0210 
0211     QTest::newRow("h4") << QStringLiteral("#### bla") << QStringLiteral("<h4>bla</h4>");
0212     QTest::newRow("h4-1") << QStringLiteral("#### bla\nsss") << QStringLiteral("<h4>bla</h4>sss");
0213     QTest::newRow("h4-2") << QStringLiteral("#### bla\nsss \n sdfsdf") << QStringLiteral("<h4>bla</h4>sss \n sdfsdf");
0214 
0215     QTest::newRow("h5") << QStringLiteral("##### bla") << QStringLiteral("<h5>bla</h5>");
0216     QTest::newRow("h5-1") << QStringLiteral("##### bla\nsss") << QStringLiteral("<h5>bla</h5>sss");
0217     QTest::newRow("h5-2") << QStringLiteral("##### bla\nsss \n sdfsdf") << QStringLiteral("<h5>bla</h5>sss \n sdfsdf");
0218 
0219     QTest::newRow("h6") << QStringLiteral("###### bla") << QStringLiteral("<h6>bla</h6>");
0220     QTest::newRow("h6-1") << QStringLiteral("###### bla\nsss") << QStringLiteral("<h6>bla</h6>sss");
0221     QTest::newRow("h6-2") << QStringLiteral("###### bla\nsss \n sdfsdf") << QStringLiteral("<h6>bla</h6>sss \n sdfsdf");
0222 
0223     QTest::newRow("h1-h6") << QStringLiteral("# bla\n## bli\n### blo\ntest") << QStringLiteral("<h1>bla</h1><h2>bli</h2><h3>blo</h3>test");
0224     QTest::newRow("h1-h6-2") << QStringLiteral("# bla\nfoo ss\n## bli\nbla bla bla\n### blo\ntest")
0225                              << QStringLiteral("<h1>bla</h1>foo ss\n<h2>bli</h2>bla bla bla\n<h3>blo</h3>test");
0226 }
0227 
0228 void UtilsTest::shouldGenerateHeader()
0229 {
0230     QFETCH(QString, text);
0231     QFETCH(QString, convertedText);
0232     QCOMPARE(Utils::convertTextHeaders(text), convertedText);
0233 }
0234 
0235 #include "moc_utilstest.cpp"