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

0001 /*
0002     Tests for Kopete::Message::parseEmoticons
0003 
0004     SPDX-FileCopyrightText: 2002-2005 The Kopete developers <kopete-devel@kde.org>
0005     SPDX-FileCopyrightText: 2004 Richard Smith <kde@metafoo.co.uk>
0006     SPDX-FileCopyrightText: 2005 Duncan Mac-Vicar <duncan@kde.org>
0007     SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include "autotestbase.h"
0013 
0014 #include <QTest>
0015 #include <QDir>
0016 #include <QFile>
0017 #include <QStandardPaths>
0018 
0019 #include <kemoticons.h>
0020 
0021 static const char * default_theme = "__woopwoop__";
0022 
0023 /*
0024   There are three sets of tests, the Kopete 0.7 baseline with tests that were
0025   working properly in Kopete 0.7.x. When these fail it's a real regression.
0026 
0027   The second set are those known to work in the current codebase.
0028   The last set is the set with tests that are known to fail right now.
0029 
0030    the name convention is (working|broken)-number.input|output
0031 */
0032 
0033 class KEmoticonTest : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void initTestCase()
0039     {
0040         QStandardPaths::setTestModeEnabled(true);
0041         cleanupTestCase();
0042 
0043         QString dataPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0044         QString destThemePath = dataPath + QLatin1String("/emoticons/");
0045         QVERIFY(QDir().mkpath(destThemePath));
0046         QDir themeDir(destThemePath);
0047         QVERIFY(copyTheme(QFINDTESTDATA("default-testtheme"), themeDir, QString::fromLatin1(default_theme)));
0048 
0049         // check it can actually be found
0050         themePath = QStandardPaths::locate(
0051                 QStandardPaths::GenericDataLocation,
0052                 QStringLiteral("emoticons/"),
0053                 QStandardPaths::LocateDirectory);
0054         QVERIFY2(!themePath.isEmpty(), qPrintable(themePath));
0055         QCOMPARE(themePath, destThemePath);
0056 
0057         // also copy the xmpp theme
0058         QVERIFY(copyTheme(QFINDTESTDATA("xmpp-testtheme"), themeDir, QStringLiteral("xmpp-testtheme")));
0059     }
0060 
0061     void cleanupTestCase()
0062     {
0063         QString dataPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0064         const QString themePath = dataPath + QLatin1String("/emoticons/");
0065         QVERIFY(QDir(themePath + QString::fromLatin1(default_theme)).removeRecursively());
0066         QVERIFY(QDir(themePath + QStringLiteral("xmpp-testtheme")).removeRecursively());
0067     }
0068 
0069     void testEmoticonParser_data()
0070     {
0071         QTest::addColumn<QString>("inputFileName");
0072         QTest::addColumn<QString>("outputFileName");
0073         QTest::addColumn<QString>("themeName");
0074         QTest::addColumn<bool>("xfail");
0075 
0076         QString basePath = QFINDTESTDATA("emoticon-parser-testcases");
0077         QVERIFY(!basePath.isEmpty());
0078         QDir testCasesDir(basePath);
0079 
0080         const QStringList inputFileNames = testCasesDir.entryList(QStringList(QStringLiteral("*.input")));
0081         for (const QString &fileName : inputFileNames) {
0082             QString outputFileName = fileName;
0083             outputFileName.replace(QStringLiteral("input"), QStringLiteral("output"));
0084             const QString baseName = fileName.section(QLatin1Char('-'), 0, 0);
0085             QTest::newRow(qPrintable(fileName.left(fileName.lastIndexOf(QLatin1Char('.')))))
0086                 << basePath + QLatin1Char('/') + fileName
0087                 << basePath + QLatin1Char('/') + outputFileName
0088                 << (baseName == QLatin1String("xmpp") ? QStringLiteral("xmpp-testtheme") : QString::fromLatin1(default_theme))
0089                 << (baseName == QLatin1String("broken"));
0090         }
0091     }
0092 
0093     void testEmoticonParser()
0094     {
0095         QFETCH(QString, inputFileName);
0096         QFETCH(QString, outputFileName);
0097         QFETCH(QString, themeName);
0098         QFETCH(bool, xfail);
0099 
0100         KEmoticonsTheme emo = KEmoticons().theme(themeName);
0101 
0102         QFile inputFile(inputFileName);
0103         QFile expectedFile(outputFileName);
0104         if (! expectedFile.exists()) {
0105             QSKIP("Warning! expected output for testcase not found. Skipping testcase");
0106         } else if (inputFile.open(QIODevice::ReadOnly) && expectedFile.open(QIODevice::ReadOnly)) {
0107             const QString inputData = QString::fromLatin1(inputFile.readAll().constData());
0108             const QString expectedData = QString::fromLatin1(expectedFile.readAll().constData());
0109 
0110             inputFile.close();
0111             expectedFile.close();
0112 
0113             QString result = emo.parseEmoticons(inputData,
0114                     KEmoticonsTheme::RelaxedParse | KEmoticonsTheme::SkipHTML);
0115             result.remove(QStringLiteral("file://") + themePath + themeName + QLatin1Char('/'));
0116 
0117             if (xfail) {
0118                 QEXPECT_FAIL("", "Checking known-broken testcase", Continue);
0119                 QCOMPARE(result, expectedData);
0120             } else {
0121                 QCOMPARE(result, expectedData);
0122             }
0123         } else {
0124             QSKIP("Warning! can't open testcase files. Skipping testcase");
0125         }
0126     }
0127 
0128     void testPreferredSizes_data()
0129     {
0130         QTest::addColumn<QString>("themeName");
0131 
0132         QString basePath = QFINDTESTDATA("emoticon-parser-testcases");
0133         QVERIFY(!basePath.isEmpty());
0134         QDir testCasesDir(basePath);
0135 
0136         const QStringList inputFileNames = testCasesDir.entryList(QStringList(QStringLiteral("*.input")));
0137         for (const QString &fileName : inputFileNames) {
0138             QString outputFileName = fileName;
0139             outputFileName.replace(QStringLiteral("input"), QStringLiteral("output"));
0140             const QString baseName = fileName.section(QLatin1Char('-'), 0, 0);
0141             QTest::newRow(qPrintable(fileName.left(fileName.lastIndexOf(QLatin1Char('.')))))
0142             << (baseName == QLatin1String("xmpp") ? QStringLiteral("xmpp-testtheme") : QString::fromLatin1(default_theme));
0143         }
0144     }
0145 
0146     void testPreferredSizes()
0147     {
0148         QFETCH(QString, themeName);
0149         KEmoticons kemoticons;
0150         kemoticons.setPreferredEmoticonSize(QSize(99, 77));
0151 
0152         KEmoticonsTheme theme = kemoticons.theme(themeName);
0153 
0154         const QString parsed = theme.parseEmoticons(QStringLiteral(":)"));
0155 
0156         QVERIFY(parsed.contains(QLatin1String("width=\"99\"")));
0157         QVERIFY(parsed.contains(QLatin1String("height=\"77\"")));
0158     }
0159 
0160 private:
0161     QString themePath;
0162 };
0163 
0164 QTEST_MAIN(KEmoticonTest)
0165 
0166 #include <kemoticontest.moc>
0167