File indexing completed on 2024-05-12 16:25:34

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "textconvertertest.h"
0008 #include "emoticons/emojimanager.h"
0009 #include "messages/message.h"
0010 #include "ruqola_autotest_helper.h"
0011 #include "textconverter.h"
0012 #include "utils.h"
0013 #include <QJsonObject>
0014 #include <QRegularExpression>
0015 
0016 #include <KColorScheme>
0017 
0018 QTEST_GUILESS_MAIN(TextConverterTest)
0019 
0020 namespace
0021 {
0022 QString prepareExpectedOutput(QString output)
0023 {
0024     KColorScheme scheme;
0025     const auto codeBackgroundColor = scheme.background(KColorScheme::AlternateBackground).color().name();
0026     const auto codeBorderColor = scheme.foreground(KColorScheme::InactiveText).color().name();
0027 
0028     const auto userHighlightForegroundColor = scheme.foreground(KColorScheme::PositiveText).color().name();
0029     const auto userHighlightBackgroundColor = scheme.background(KColorScheme::PositiveBackground).color().name();
0030 
0031     output.replace(QLatin1String("$BGCOLOR$"), codeBackgroundColor);
0032     output.replace(QLatin1String("$BORDERCOLOR$"), codeBorderColor);
0033     output.replace(QLatin1String("$USERCOLOR$"), userHighlightForegroundColor);
0034     output.replace(QLatin1String("$USERBGCOLOR$"), userHighlightBackgroundColor);
0035 
0036     output.prepend(QLatin1String("<qt>"));
0037     output.append(QLatin1String("</qt>"));
0038 
0039     return output;
0040 }
0041 }
0042 
0043 TextConverterTest::TextConverterTest(QObject *parent)
0044     : QObject(parent)
0045 {
0046 }
0047 
0048 void TextConverterTest::shouldConvertText_data()
0049 {
0050     QTest::addColumn<QString>("input");
0051     QTest::addColumn<QString>("output");
0052     QTest::newRow("empty") << QString() << QString();
0053     QTest::newRow("simpletext") << QStringLiteral("foo") << QStringLiteral("<div>foo</div>");
0054     QTest::newRow("customemojiwithoutmanager") << QStringLiteral(":foo:") << QStringLiteral("<div>:foo:</div>");
0055 
0056     QTest::newRow("testurl") << QStringLiteral("http://www.kde.org http://www.kde.org")
0057                              << QStringLiteral(
0058                                     "<div><a href=\"http://www.kde.org\">http://www.kde.org</a> <a href=\"http://www.kde.org\">http://www.kde.org</a></div>");
0059     QTest::newRow("named-url") << QStringLiteral("[example.com](http://example.com)")
0060                                << QStringLiteral("<div><a href=\"http://example.com\">example.com</a></div>");
0061     QTest::newRow("bold") << QStringLiteral("*bla*") << QStringLiteral("<div><b>bla</b></div>");
0062     QTest::newRow("multi star") << QStringLiteral("**bla**") << QStringLiteral("<div><b>bla</b></div>");
0063     QTest::newRow("multi star2") << QStringLiteral("***bla***") << QStringLiteral("<div>***bla***</div>");
0064     QTest::newRow("multi star3") << QStringLiteral("***bla ******") << QStringLiteral("<div>***bla ******</div>");
0065     QTest::newRow("Remove <br/>") << QStringLiteral("foo<br />") << QStringLiteral("<div>foo</div>");
0066 
0067     QTest::newRow("0.6.3") << QStringLiteral("0.6.3") << QStringLiteral("<div>0.6.3</div>");
0068     // Bug 391520
0069     QTest::newRow("multi-line") << QStringLiteral("These are the options:\n- a\n- b")
0070                                 << QStringLiteral("<div>These are the options:<br />\n- a<br />\n- b</div>");
0071 
0072     QTest::newRow("word@") << QStringLiteral("@foo") << QStringLiteral("<div><a href='ruqola:/user/foo'>@foo</a></div>");
0073     QTest::newRow("word@-2") << QStringLiteral("@foo.bla") << QStringLiteral("<div><a href='ruqola:/user/foo.bla'>@foo.bla</a></div>");
0074     QTest::newRow("word@-3") << QStringLiteral("@foo.bla.bli") << QStringLiteral("<div><a href='ruqola:/user/foo.bla.bli'>@foo.bla.bli</a></div>");
0075     QTest::newRow("word@-4") << QStringLiteral("@foo.bla.bli dd") << QStringLiteral("<div><a href='ruqola:/user/foo.bla.bli'>@foo.bla.bli</a> dd</div>");
0076     QTest::newRow("word@-5") << QStringLiteral("bla bla 21 @foo.bla.bli dd")
0077                              << QStringLiteral("<div>bla bla 21 <a href='ruqola:/user/foo.bla.bli'>@foo.bla.bli</a> dd</div>");
0078     QTest::newRow("word@-6") << QStringLiteral("@foo-bla") << QStringLiteral("<div><a href='ruqola:/user/foo-bla'>@foo-bla</a></div>");
0079     QTest::newRow("word@-7") << QStringLiteral("@foo_bla") << QStringLiteral("<div><a href='ruqola:/user/foo_bla'>@foo_bla</a></div>");
0080     QTest::newRow("word@-8") << QStringLiteral("bli@foo_bla") << QStringLiteral("<div>bli@foo_bla</div>");
0081     QTest::newRow("word@-9") << QStringLiteral("@réunion") << QStringLiteral("<div><a href='ruqola:/user/réunion'>@réunion</a></div>");
0082 
0083     QTest::newRow("word#") << QStringLiteral("#foo") << QStringLiteral("<div><a href='ruqola:/room/foo'>#foo</a></div>");
0084     QTest::newRow("word#-2") << QStringLiteral("#foo.bla") << QStringLiteral("<div><a href='ruqola:/room/foo.bla'>#foo.bla</a></div>");
0085     QTest::newRow("word#-3") << QStringLiteral("#foo.bla.bli") << QStringLiteral("<div><a href='ruqola:/room/foo.bla.bli'>#foo.bla.bli</a></div>");
0086     QTest::newRow("word#-4") << QStringLiteral("#foo.bla.bli dd") << QStringLiteral("<div><a href='ruqola:/room/foo.bla.bli'>#foo.bla.bli</a> dd</div>");
0087     QTest::newRow("word#-5") << QStringLiteral("bla bla 21 #foo.bla.bli dd")
0088                              << QStringLiteral("<div>bla bla 21 <a href='ruqola:/room/foo.bla.bli'>#foo.bla.bli</a> dd</div>");
0089     QTest::newRow("word#-6") << QStringLiteral("#foo-bla") << QStringLiteral("<div><a href='ruqola:/room/foo-bla'>#foo-bla</a></div>");
0090     QTest::newRow("word#-7") << QStringLiteral("#foo_bla") << QStringLiteral("<div><a href='ruqola:/room/foo_bla'>#foo_bla</a></div>");
0091     QTest::newRow("word#-8") << QStringLiteral("#réunion") << QStringLiteral("<div><a href='ruqola:/room/réunion'>#réunion</a></div>");
0092     // Test parsing when it's in an url... don't replace it.
0093 
0094     QTest::newRow("url") << QStringLiteral("http://www.kde.org#foo_bla")
0095                          << QStringLiteral("<div><a href=\"http://www.kde.org#foo_bla\">http://www.kde.org#foo_bla</a></div>");
0096 
0097     QTest::newRow("0.6.3") << QStringLiteral("0.6.3") << QStringLiteral("<div>0.6.3</div>");
0098     QTest::newRow("`code`")
0099         << QStringLiteral("foo `bla` bar `asdf` test")
0100         << QStringLiteral("<div>foo <code style='background-color:$BGCOLOR$'>bla</code> bar <code style='background-color:$BGCOLOR$'>asdf</code> test</div>");
0101     QTest::newRow("`code \\` escaped`") << QStringLiteral("foo `bla\\` bar \\`asdf` test")
0102                                         << QStringLiteral("<div>foo <code style='background-color:$BGCOLOR$'>bla\\` bar \\`asdf</code> test</div>");
0103     QTest::newRow("quote<")
0104         << QStringLiteral("`inside quotes: <<....` this is outside the quotes <<.")
0105         << QStringLiteral("<div><code style='background-color:$BGCOLOR$'>inside quotes: &lt;&lt;....</code> this is outside the quotes &lt;&lt;.</div>");
0106 
0107     QTest::newRow("quotedtext1") << QStringLiteral("> toto") << QStringLiteral("<div><code style='background-color:$BGCOLOR$'>toto</code></div>");
0108 
0109     QTest::newRow("quotedtext2") << QStringLiteral("bla \n> toto")
0110                                  << QStringLiteral("<div>bla <br />\n<code style='background-color:$BGCOLOR$'>toto</code></div>");
0111 
0112     QTest::newRow("quotedtext3")
0113         << QStringLiteral("bla \n> toto \n> bla")
0114         << QStringLiteral(
0115                "<div>bla <br />\n<code style='background-color:$BGCOLOR$'>toto</code><br /><code style='background-color:$BGCOLOR$'>bla</code></div>");
0116 
0117     QTest::newRow("quotedtext4")
0118         << QStringLiteral("bla \n> toto \n> *bla*")
0119         << QStringLiteral(
0120                "<div>bla <br />\n<code style='background-color:$BGCOLOR$'>toto</code><br /><code style='background-color:$BGCOLOR$'><b>bla</b></code></div>");
0121 
0122     QTest::newRow("noquotedtext") << QStringLiteral("bla > toto") << QStringLiteral("<div>bla &gt; toto</div>");
0123 }
0124 
0125 void TextConverterTest::shouldConvertText()
0126 {
0127     QFETCH(QString, input);
0128     QFETCH(QString, output);
0129 
0130     output = prepareExpectedOutput(output);
0131 
0132     QEXPECT_FAIL("multi star", "Bug in KF5", Continue);
0133     QEXPECT_FAIL("Remove <br/>", "Bug in KF5", Continue);
0134 
0135     QString needUpdateMessageId;
0136     const TextConverter::ConvertMessageTextSettings settings(input, QString(), {}, {}, nullptr, nullptr, {}, {});
0137     int recursiveIndex = 0;
0138     QCOMPARE(TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex),
0139              output); // TODO add autotests for highlightwords
0140 }
0141 
0142 void TextConverterTest::shouldHighlightWords_data()
0143 {
0144     QTest::addColumn<QString>("input");
0145     QTest::addColumn<QString>("username");
0146     QTest::addColumn<QStringList>("highlightWords");
0147     QTest::addColumn<QString>("output");
0148     QTest::newRow("empty") << QString() << QString() << QStringList{} << QString();
0149     const QStringList highlightWords{QStringLiteral("ruqola"), QStringLiteral("kde")};
0150     QTest::newRow("lowercase") << QStringLiteral("Ruqola") << QStringLiteral("foo") << highlightWords
0151                                << QStringLiteral("<div><a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">Ruqola</a></div>");
0152     QTest::newRow("two-word") << QStringLiteral("Ruqola kde") << QStringLiteral("foo") << highlightWords
0153                               << QStringLiteral(
0154                                      "<div><a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">Ruqola</a> <a "
0155                                      "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a></div>");
0156 
0157     QTest::newRow("words") << QStringLiteral("Ruqola bla kde KDE.") << QStringLiteral("foo") << highlightWords
0158                            << QStringLiteral(
0159                                   "<div><a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">Ruqola</a> bla <a "
0160                                   "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a> <a "
0161                                   "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">KDE</a>.</div>");
0162 
0163     QTest::newRow("wordinurl") << QStringLiteral("https://www.kde.org/~/bla/bli.txt") << QStringLiteral("bla") << highlightWords
0164                                << QStringLiteral(
0165                                       "<div><a href=\"https://www.kde.org/~/bla/bli.txt\">https://www.<a "
0166                                       "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a>.org/~/bla/bli.txt</a></div>");
0167     QTest::newRow("channelruqola")
0168         << QStringLiteral("#ruqola-bla bla kde KDE.") << QStringLiteral("foo") << highlightWords
0169         << QStringLiteral(
0170                "<div><a href='ruqola:/room/ruqola-bla'>#<a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">ruqola</a>-bla</a> bla <a "
0171                "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a> <a "
0172                "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">KDE</a>.</div>");
0173 }
0174 
0175 void TextConverterTest::shouldHighlightWords()
0176 {
0177     QFETCH(QString, input);
0178     QFETCH(QString, username);
0179     QFETCH(QStringList, highlightWords);
0180     QFETCH(QString, output);
0181 
0182     output = prepareExpectedOutput(output);
0183 
0184     QString needUpdateMessageId;
0185     int recursiveIndex = 0;
0186     const TextConverter::ConvertMessageTextSettings settings(input, username, {}, highlightWords, nullptr, nullptr, {}, {});
0187     QCOMPARE(TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex), output);
0188 }
0189 
0190 void TextConverterTest::shouldHighlightText_data()
0191 {
0192     QTest::addColumn<QString>("input");
0193     QTest::addColumn<QString>("username");
0194     QTest::addColumn<QString>("output");
0195     QTest::newRow("empty") << QString() << QString() << QString();
0196     QTest::newRow("word@1") << QStringLiteral("@foo") << QString() << QStringLiteral("<div><a href='ruqola:/user/foo'>@foo</a></div>");
0197     KColorScheme colorScheme;
0198     const auto userMentionForegroundColor = colorScheme.foreground(KColorScheme::NegativeText).color().name();
0199     const auto userMentionBackgroundColor = colorScheme.background(KColorScheme::NegativeBackground).color().name();
0200     QTest::newRow("word@1-username") << QStringLiteral("@foo") << QStringLiteral("foo")
0201                                      << QStringLiteral("<div><a href='ruqola:/user/foo' style=\"color:%1;background-color:%2;font-weight:bold\">@foo</a></div>")
0202                                             .arg(userMentionForegroundColor, userMentionBackgroundColor);
0203     QTest::newRow("word@2-username") << QStringLiteral("bla bla @foo") << QStringLiteral("foo")
0204                                      << QStringLiteral(
0205                                             "<div>bla bla <a href='ruqola:/user/foo' style=\"color:%1;background-color:%2;font-weight:bold\">@foo</a></div>")
0206                                             .arg(userMentionForegroundColor, userMentionBackgroundColor);
0207 }
0208 
0209 void TextConverterTest::shouldHighlightText()
0210 {
0211     QFETCH(QString, input);
0212     QFETCH(QString, username);
0213     QFETCH(QString, output);
0214 
0215     output = prepareExpectedOutput(output);
0216     QString needUpdateMessageId;
0217     int recursiveIndex = 0;
0218     const TextConverter::ConvertMessageTextSettings settings(input, username, {}, {}, nullptr, nullptr, {}, {});
0219     QCOMPARE(TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex), output);
0220 }
0221 
0222 void TextConverterTest::shouldConvertTextWithEmoji_data()
0223 {
0224     QTest::addColumn<QString>("input");
0225     QTest::addColumn<QString>("output");
0226     QTest::addColumn<QString>("serverUrl");
0227     QTest::newRow("empty") << QString() << QString() << QStringLiteral("www.kde.org");
0228     QTest::newRow("bold") << QStringLiteral("*foo*") << QStringLiteral("<div><b>foo</b></div>") << QStringLiteral("www.kde.org");
0229     QTest::newRow("italic") << QStringLiteral("_foo_") << QStringLiteral("<div><i>foo</i></div>") << QStringLiteral("www.kde.org");
0230     // TODO error
0231     QTest::newRow("italic2") << QStringLiteral("_personal: theming related tasks_") << QStringLiteral("<div><i>personal: theming related tasks</i></div>")
0232                              << QStringLiteral("www.kde.org");
0233     QTest::newRow("simpletext") << QStringLiteral("foo") << QStringLiteral("<div>foo</div>") << QStringLiteral("www.kde.org");
0234     QTest::newRow("customemojiwithmanager") << QStringLiteral(":foo:") << QStringLiteral("<div>:foo:</div>") << QStringLiteral("www.kde.org");
0235 
0236     QString smiley;
0237     smiley += QChar(0xD83D);
0238     smiley += QChar(0xDE42);
0239     const auto smileyText = QStringLiteral("<span style=\"font: x-large %2\" title=\":slight_smile:\">%1</span>").arg(smiley, Utils::emojiFontName());
0240     QTest::newRow("ascii-smiley") << QStringLiteral(":)") << QStringLiteral("<div>%1</div>").arg(smileyText) << QStringLiteral("www.kde.org");
0241     QTest::newRow("multi-smiley") << QStringLiteral(":):slight_smile::):)") << QStringLiteral("<div>%1</div>").arg(smileyText.repeated(4))
0242                                   << QStringLiteral("www.kde.org");
0243 
0244     QTest::newRow("url") << QStringLiteral("https://www.kde.org") << QStringLiteral("<div><a href=\"https://www.kde.org\">https://www.kde.org</a></div>")
0245                          << QStringLiteral("www.kde.org");
0246 
0247     QTest::newRow("\")") << QStringLiteral("\")") << QStringLiteral("<div>&quot;)</div>") << QStringLiteral("www.kde.org");
0248 
0249     //    QTest::newRow("customemojiwithmanager1") << QStringLiteral(":vader:")
0250     //                                             << QStringLiteral("<div><img height='22' width='22' src='http://www.kde.org/emoji-custom/vader.png'
0251     //                                             title=':vader:'/></div>")
0252     //                                             << QStringLiteral("www.kde.org");
0253     //    QTest::newRow("customemojiwithmanager2") << QStringLiteral(":vader::vader:")
0254     //                                             << QStringLiteral(
0255     //        "<div><img height='22' width='22' src='http://www.kde.org/emoji-custom/vader.png' title=':vader:'/><img height='22' width='22'
0256     //        src='http://www.kde.org/emoji-custom/vader.png' title=':vader:'/></div>")
0257     //                                             << QStringLiteral("www.kde.org");
0258 
0259     //    //Use server with http://
0260     //    QTest::newRow("customemojiwithmanager3") << QStringLiteral("the icon for vader is :vader::vader:")
0261     //                                             << QStringLiteral(
0262     //        "<div>the icon for vader is <img height='22' width='22' src='http://www.kde.org/emoji-custom/vader.png' title=':vader:'/><img height='22'
0263     //        width='22' src='http://www.kde.org/emoji-custom/vader.png' title=':vader:'/></div>")
0264     //                                             << QStringLiteral("http://www.kde.org");
0265 
0266     //    //alias support
0267     //    QTest::newRow("customemojiwithmanager4-with-alias") << QStringLiteral(":darth::vader:")
0268     //                                                        << QStringLiteral(
0269     //        "<div><img height='22' width='22' src='http://www.kde.org/emoji-custom/vader.png' title=':vader:'/><img height='22' width='22'
0270     //        src='http://www.kde.org/emoji-custom/vader.png' title=':vader:'/></div>")
0271     //                                                        << QStringLiteral("http://www.kde.org");
0272 
0273     QTest::newRow("quotedcode1") << QStringLiteral("bla```foo```blub")
0274                                  << QStringLiteral(
0275                                         "<div>bla</div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0276                                         "$BORDERCOLOR$'><code>foo</code></td></tr></table><div>blub</div>")
0277                                  << QStringLiteral("www.kde.org");
0278     QTest::newRow("quotedcode2") << QStringLiteral("bla\n```foo```bli")
0279                                  << QStringLiteral(
0280                                         "<div>bla</div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0281                                         "$BORDERCOLOR$'><code>foo</code></td></tr></table><div>bli</div>")
0282                                  << QStringLiteral("www.kde.org");
0283     QTest::newRow("quotedcode3") << QStringLiteral("bla\n```foo```")
0284                                  << QStringLiteral(
0285                                         "<div>bla</div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0286                                         "$BORDERCOLOR$'><code>foo</code></td></tr></table>")
0287                                  << QStringLiteral("www.kde.org");
0288     QTest::newRow("quotedcode4") << QStringLiteral("```foo```\nff")
0289                                  << QStringLiteral(
0290                                         "<table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0291                                         "$BORDERCOLOR$'><code>foo</code></td></tr></table><div>ff</div>")
0292                                  << QStringLiteral("www.kde.org");
0293     QTest::newRow("quotedcode5") << QStringLiteral("bla\n```\nfoo\n```\nff")
0294                                  << QStringLiteral(
0295                                         "<div>bla</div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0296                                         "$BORDERCOLOR$'><code>foo</code></td></tr></table><div>ff</div>")
0297                                  << QStringLiteral("www.kde.org");
0298     QTest::newRow("quotedcode6")
0299         << QStringLiteral("*foo*\n```\nfoo\n```\n*bar*\n```blub```\n*asdf*")
0300         << QStringLiteral(
0301                "<div><b>foo</b></div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0302                "$BORDERCOLOR$'><code>foo</code></td></tr></table><div><b>bar</b></div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: "
0303                "1px solid $BORDERCOLOR$'><code>blub</code></td></tr></table><div><b>asdf</b></div>")
0304         << QStringLiteral("www.kde.org");
0305     QTest::newRow("quotedcode7") << QStringLiteral(":)\n```\n:)\n```\n:)")
0306                                  << QStringLiteral(
0307                                         "<div>%1</div><table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid "
0308                                         "$BORDERCOLOR$'><code>:)</code></td></tr></table><div>%1</div>")
0309                                         .arg(smileyText)
0310                                  << QStringLiteral("www.kde.org");
0311     QTest::newRow("quotedcode8")
0312         << QStringLiteral("```javascript\ncode\n```")
0313         << QStringLiteral(
0314                "<table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid $BORDERCOLOR$'><code>code</code></td></tr></table>")
0315         << QStringLiteral("www.kde.org");
0316     QTest::newRow("quotedcode9")
0317         << QStringLiteral("```blub\ncode\n```")
0318         << QStringLiteral(
0319                "<table><tr><td style='background-color:$BGCOLOR$; padding: 5px; border: 1px solid $BORDERCOLOR$'><code>blub<br>code</code></td></tr></table>")
0320         << QStringLiteral("www.kde.org");
0321 
0322     QTest::newRow("inline-code-with-brackets")
0323         << QStringLiteral("`[[test]]` and `a[b` something")
0324         << QStringLiteral(
0325                "<div><code style='background-color:$BGCOLOR$'>[[test]]</code> and <code style='background-color:$BGCOLOR$'>a[b</code> "
0326                "something</div>")
0327         << QStringLiteral("www.kde.org");
0328 
0329     QTest::newRow("inline-code-with-tilde") << QStringLiteral("`auto [a, b] = std::minmax_element(~~~);`")
0330                                             << QStringLiteral(
0331                                                    "<div><code style='background-color:$BGCOLOR$'>auto [a, b] = "
0332                                                    "std::minmax_element(~~~);</code></div>")
0333                                             << QStringLiteral("www.kde.org");
0334 
0335     QTest::newRow("inline-code-with-smiley") << QStringLiteral(":) `:)` :)")
0336                                              << QStringLiteral("<div>%1 <code style='background-color:$BGCOLOR$'>:)</code> %1</div>").arg(smileyText)
0337                                              << QStringLiteral("www.kde.org");
0338 
0339     QTest::newRow("url-with-emoji") << QStringLiteral("https://www.kde.org/:x:/bla.html")
0340                                     << QStringLiteral("<div>%1 <code style='background-color:$BGCOLOR$'>:)</code> %1</div>").arg(smileyText)
0341                                     << QStringLiteral("www.kde.org");
0342 }
0343 
0344 void TextConverterTest::shouldConvertTextWithEmoji()
0345 {
0346     QFETCH(QString, input);
0347     QFETCH(QString, output);
0348     QFETCH(QString, serverUrl);
0349 
0350     output = prepareExpectedOutput(output);
0351 
0352     // Load emoji
0353     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/restapi/emojiparent.json");
0354     const QJsonObject obj = AutoTestHelper::loadJsonObject(originalJsonFile);
0355     EmojiManager manager(nullptr);
0356     manager.loadCustomEmoji(obj);
0357     manager.setServerUrl(serverUrl);
0358 
0359     QString needUpdateMessageId;
0360     int recursiveIndex = 0;
0361     const TextConverter::ConvertMessageTextSettings settings(input, QString(), {}, {}, &manager, nullptr, {}, {});
0362     auto actualOutput = TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex);
0363     if (QLatin1String(QTest::currentDataTag()) == QLatin1String("quotedcode7")) {
0364         // remove additional highlighting of the ':)' symbols within the <code> block
0365         // the text color is syntax highlighting theme dependent, so hard for us to check
0366         actualOutput.replace(QRegularExpression(QStringLiteral("<code><span style=\".+\">:\\)</span></code>")), QStringLiteral("<code>:)</code>"));
0367     }
0368     QEXPECT_FAIL("url-with-emoji", "Currently it if we have a emoji char in url", Continue);
0369     QCOMPARE(actualOutput, output); // TODO add autotest for highlightwords
0370 }
0371 
0372 void TextConverterTest::shouldShowChannels_data()
0373 {
0374     QTest::addColumn<QString>("input");
0375     QTest::addColumn<QString>("output");
0376     QTest::addColumn<QMap<QString, QString>>("mentions");
0377     QTest::addColumn<QMap<QString, QString>>("channels");
0378 
0379     {
0380         QMap<QString, QString> mentions;
0381         QMap<QString, QString> channels;
0382         QTest::newRow("empty") << QString() << QString() << mentions << channels;
0383     }
0384     {
0385         QMap<QString, QString> mentions;
0386         QMap<QString, QString> channels;
0387         channels.insert(QStringLiteral("foo"), QStringLiteral("idd"));
0388         QTest::newRow("word#") << QStringLiteral("#foo") << QStringLiteral("<div><a href='ruqola:/room/idd'>#foo</a></div>") << mentions << channels;
0389     }
0390     {
0391         QMap<QString, QString> mentions;
0392         QMap<QString, QString> channels;
0393         channels.insert(QStringLiteral("bla"), QStringLiteral("idd"));
0394         QTest::newRow("not existing room") << QStringLiteral("#foo") << QStringLiteral("<div><a href='ruqola:/room/foo'>#foo</a></div>") << mentions
0395                                            << channels;
0396     }
0397     {
0398         QMap<QString, QString> mentions;
0399         QMap<QString, QString> channels;
0400         channels.insert(QStringLiteral("bli"), QStringLiteral("112"));
0401         channels.insert(QStringLiteral("oss"), QStringLiteral("kli"));
0402         QTest::newRow("multi channel") << QStringLiteral("foo #bli blass #oss")
0403                                        << QStringLiteral("<div>foo <a href='ruqola:/room/112'>#bli</a> blass <a href='ruqola:/room/kli'>#oss</a></div>")
0404                                        << mentions << channels;
0405     }
0406 }
0407 
0408 void TextConverterTest::shouldShowChannels()
0409 {
0410     using map = QMap<QString, QString>;
0411     QFETCH(QString, input);
0412     QFETCH(QString, output);
0413     QFETCH(map, mentions);
0414     QFETCH(map, channels);
0415 
0416     output = prepareExpectedOutput(output);
0417     QString needUpdateMessageId;
0418     int recursiveIndex = 0;
0419     const TextConverter::ConvertMessageTextSettings settings(input, {}, {}, {}, nullptr, nullptr, mentions, channels);
0420     QCOMPARE(TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex), output);
0421 }
0422 
0423 void TextConverterTest::shouldShowUsers()
0424 {
0425     using map = QMap<QString, QString>;
0426     QFETCH(QString, input);
0427     QFETCH(QString, output);
0428     QFETCH(map, mentions);
0429     QFETCH(map, channels);
0430 
0431     output = prepareExpectedOutput(output);
0432     QString needUpdateMessageId;
0433     int recursiveIndex = 0;
0434     const TextConverter::ConvertMessageTextSettings settings(input, {}, {}, {}, nullptr, nullptr, mentions, channels);
0435     QCOMPARE(TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex), output);
0436 }
0437 
0438 void TextConverterTest::shouldShowUsers_data()
0439 {
0440     QTest::addColumn<QString>("input");
0441     QTest::addColumn<QString>("output");
0442     QTest::addColumn<QMap<QString, QString>>("mentions");
0443     QTest::addColumn<QMap<QString, QString>>("channels");
0444 
0445     {
0446         QMap<QString, QString> mentions;
0447         QMap<QString, QString> channels;
0448         QTest::newRow("empty") << QString() << QString() << mentions << channels;
0449     }
0450     {
0451         QMap<QString, QString> mentions;
0452         mentions.insert(QStringLiteral("kde"), QStringLiteral("bb"));
0453         QMap<QString, QString> channels;
0454         channels.insert(QStringLiteral("foo"), QStringLiteral("idd"));
0455         QTest::newRow("channel-user1") << QStringLiteral("#foo @kde")
0456                                        << QStringLiteral("<div><a href='ruqola:/room/idd'>#foo</a> <a href='ruqola:/user/bb'>@kde</a></div>") << mentions
0457                                        << channels;
0458     }
0459 
0460     {
0461         QMap<QString, QString> mentions;
0462         mentions.insert(QStringLiteral("kde1"), QStringLiteral("bb"));
0463         QMap<QString, QString> channels;
0464         channels.insert(QStringLiteral("foo2"), QStringLiteral("idd"));
0465         QTest::newRow("channel-user-unknown") << QStringLiteral("#foo @kde")
0466                                               << QStringLiteral("<div><a href='ruqola:/room/foo'>#foo</a> <a href='ruqola:/user/kde'>@kde</a></div>")
0467                                               << mentions << channels;
0468     }
0469 }
0470 
0471 void TextConverterTest::shouldShowSearchedText_data()
0472 {
0473     QTest::addColumn<QString>("input");
0474     QTest::addColumn<QString>("username");
0475     QTest::addColumn<QStringList>("highlightWords");
0476     QTest::addColumn<QString>("searchedText");
0477     QTest::addColumn<QString>("output");
0478     QTest::newRow("empty") << QString() << QString() << QStringList{} << QString() << QString();
0479     const QStringList highlightWords{QStringLiteral("ruqola"), QStringLiteral("kde")};
0480     QTest::newRow("lowercase") << QStringLiteral("Ruqola") << QStringLiteral("foo") << highlightWords << QString()
0481                                << QStringLiteral("<div><a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">Ruqola</a></div>");
0482     QTest::newRow("two-word") << QStringLiteral("Ruqola kde") << QStringLiteral("foo") << highlightWords << QString()
0483                               << QStringLiteral(
0484                                      "<div><a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">Ruqola</a> <a "
0485                                      "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a></div>");
0486 
0487     QTest::newRow("words") << QStringLiteral("Ruqola bla kde KDE.") << QStringLiteral("foo") << highlightWords << QString()
0488                            << QStringLiteral(
0489                                   "<div><a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">Ruqola</a> bla <a "
0490                                   "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a> <a "
0491                                   "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">KDE</a>.</div>");
0492 
0493     QTest::newRow("wordinurl") << QStringLiteral("https://www.kde.org/~/bla/bli.txt") << QStringLiteral("bla") << highlightWords << QString()
0494                                << QStringLiteral(
0495                                       "<div><a href=\"https://www.kde.org/~/bla/bli.txt\">https://www.<a "
0496                                       "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a>.org/~/bla/bli.txt</a></div>");
0497     QTest::newRow("channelruqola")
0498         << QStringLiteral("#ruqola-bla bla kde KDE.") << QStringLiteral("foo") << highlightWords << QString()
0499         << QStringLiteral(
0500                "<div><a href='ruqola:/room/ruqola-bla'>#<a style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">ruqola</a>-bla</a> bla <a "
0501                "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">kde</a> <a "
0502                "style=\"color:$USERCOLOR$;background-color:$USERBGCOLOR$;\">KDE</a>.</div>");
0503 }
0504 
0505 void TextConverterTest::shouldShowSearchedText()
0506 {
0507     QFETCH(QString, input);
0508     QFETCH(QString, username);
0509     QFETCH(QStringList, highlightWords);
0510     QFETCH(QString, searchedText);
0511     QFETCH(QString, output);
0512 
0513     output = prepareExpectedOutput(output);
0514 
0515     QString needUpdateMessageId;
0516     int recursiveIndex = 0;
0517     const TextConverter::ConvertMessageTextSettings settings(input, username, {}, highlightWords, nullptr, nullptr, {}, {}, searchedText);
0518     QCOMPARE(TextConverter::convertMessageText(settings, needUpdateMessageId, recursiveIndex), output);
0519 }
0520 
0521 #include "moc_textconvertertest.cpp"