File indexing completed on 2024-04-28 15:22:10

0001 /*
0002     SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "autotestbase.h"
0008 
0009 #include <QObject>
0010 #include <QTest>
0011 #include <QStandardPaths>
0012 
0013 #include "../src/integrationplugin/ktexttohtml.h"
0014 #include <kemoticons.h>
0015 
0016 class KTextToHTMLPluginTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private:
0021     QString htmlForSmiley(const QString &emoticon, const QString &name) const;
0022 
0023 private Q_SLOTS:
0024     void initTestCase();
0025     void parseEmoticonsTest_data();
0026     void parseEmoticonsTest();
0027 
0028 private:
0029     QString mEmoticonsThemePath;
0030 };
0031 
0032 QTEST_MAIN(KTextToHTMLPluginTest)
0033 
0034 void KTextToHTMLPluginTest::initTestCase()
0035 {
0036     QStandardPaths::setTestModeEnabled(true);
0037     QString dataPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0038     QString destThemePath = dataPath + QLatin1String("/emoticons/");
0039     QVERIFY(QDir().mkpath(destThemePath));
0040     mEmoticonsThemePath = destThemePath + QStringLiteral("default-testtheme");
0041     if (QFileInfo::exists(mEmoticonsThemePath)) {
0042         QVERIFY(QDir(mEmoticonsThemePath).removeRecursively());
0043     }
0044     QDir themeDir(destThemePath);
0045     QVERIFY(copyTheme(QFINDTESTDATA("default-testtheme"), themeDir, QStringLiteral("default-testtheme")));
0046 
0047     KEmoticons::setTheme(QStringLiteral("default-testtheme"));
0048     QVERIFY(!mEmoticonsThemePath.isEmpty());
0049 }
0050 
0051 QString KTextToHTMLPluginTest::htmlForSmiley(const QString &emoticon, const QString &name) const
0052 {
0053     return QStringLiteral("<img align=\"center\" title=\"%1\" alt=\"%1\" src=\"file://%2/%3.png\" width=\"22\" height=\"22\" />")
0054                 .arg(emoticon,
0055                      mEmoticonsThemePath,
0056                      name);
0057 }
0058 
0059 
0060 void KTextToHTMLPluginTest::parseEmoticonsTest_data()
0061 {
0062 
0063     QTest::addColumn<QString>("input");
0064     QTest::addColumn<bool>("strict");
0065     QTest::addColumn<QStringList>("exclude");
0066     QTest::addColumn<QString>("expected");
0067 
0068     QTest::newRow("simple")
0069         << "Hello :-)"
0070         << false << QStringList()
0071         << QStringLiteral("Hello %1").arg(htmlForSmiley(QStringLiteral(":-)"), QStringLiteral("smile")));
0072 
0073     QTest::newRow("between strings")
0074         << "Hello :-) How are you?"
0075         << false << QStringList()
0076         << QStringLiteral("Hello %1 How are you?").arg(htmlForSmiley(QStringLiteral(":-)"), QStringLiteral("smile")));
0077 
0078     QTest::newRow("excluded")
0079         << "Bye :-("
0080         << false << (QStringList() << QStringLiteral(":-("))
0081         << "Bye :-(";
0082 
0083     QTest::newRow("don't mix in HTML")
0084         << "<b>:(</b>"
0085         << false << QStringList()
0086         << QStringLiteral("<b>%1</b>").arg(htmlForSmiley(QStringLiteral(":("), QStringLiteral("sad")));
0087 
0088     QTest::newRow("strict parsing of smileys without space")
0089         << "Very happy! :-):-)"
0090         << true << QStringList()
0091         << "Very happy! :-):-)";
0092 
0093     QTest::newRow("nonstrict parsing of smileys without space")
0094         << "Very happy! :-):-)"
0095         << false << QStringList()
0096         << QStringLiteral("Very happy! %1%1").arg(htmlForSmiley(QStringLiteral(":-)"), QStringLiteral("smile")));
0097 
0098     QTest::newRow("smiley in HTML")
0099         << "<img src=\"...\" title=\":-)\" />"
0100         << false << QStringList()
0101         << "<img src=\"...\" title=\":-)\" />";
0102 }
0103 
0104 
0105 void KTextToHTMLPluginTest::parseEmoticonsTest()
0106 {
0107     QFETCH(QString, input);
0108     QFETCH(bool, strict);
0109     QFETCH(QStringList, exclude);
0110     QFETCH(QString, expected);
0111 
0112     KTextToHTMLEmoticons emoticons;
0113     const QString actual = emoticons.parseEmoticons(input, strict, exclude);
0114 
0115     QCOMPARE(actual, expected);
0116 }
0117 
0118 #include "ktexttohtmlplugintest.moc"