File indexing completed on 2024-05-19 05:47:14

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