diff --git a/src/core/autotests/emojimanagertest.cpp b/src/core/autotests/emojimanagertest.cpp index 61d3e548..caea44ea 100644 --- a/src/core/autotests/emojimanagertest.cpp +++ b/src/core/autotests/emojimanagertest.cpp @@ -1,132 +1,146 @@ /* Copyright (c) 2018-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "emojimanagertest.h" #include "emoticons/emojimanager.h" #include "emoticons/emoji.h" #include #include #include QTEST_GUILESS_MAIN(EmojiManagerTest) EmojiManagerTest::EmojiManagerTest(QObject *parent) : QObject(parent) { } void EmojiManagerTest::shouldHaveDefaultValue() { EmojiManager manager(nullptr, false); QVERIFY(manager.serverUrl().isEmpty()); QCOMPARE(manager.count(), 0); } void EmojiManagerTest::shouldParseEmoji_data() { QTest::addColumn("name"); QTest::addColumn("number"); QTest::addRow("emojiparent") << QStringLiteral("emojiparent") << 7; } void EmojiManagerTest::shouldParseEmoji() { QFETCH(QString, name); QFETCH(int, number); const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/restapi/") + name + QLatin1String(".json"); QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject obj = doc.object(); EmojiManager manager(nullptr, false); manager.loadCustomEmoji(obj); QCOMPARE(manager.count(), number); } void EmojiManagerTest::shouldSupportUnicodeEmojis() { EmojiManager manager; QString grinning; grinning += QChar(0xd800+61); grinning += QChar(0xDC00+512); // A basic emoji that was already there in the initial emoji.json from fairchat QCOMPARE(manager.unicodeEmoticonForEmoji(QStringLiteral(":grinning:")).unicode(), grinning); // The one that made me use https://raw.githubusercontent.com/joypixels/emoji-toolkit/master/emoji.json instead QVERIFY(manager.unicodeEmoticonForEmoji(QStringLiteral(":zany_face:")).isValid()); // A "shortname alternate" QCOMPARE(manager.unicodeEmoticonForEmoji(QStringLiteral(":water_polo_tone5:")).key(), QStringLiteral("1f93d-1f3ff")); // One with multiple values below 0x10000, to catch += vs = bug. QCOMPARE(manager.unicodeEmoticonForEmoji(QStringLiteral(":woman_climbing_tone4:")).unicode().length(), 7); QCOMPARE(manager.unicodeEmoticonForEmoji(QStringLiteral(":man_health_worker_tone1:")).unicode().length(), 7); } +void EmojiManagerTest::shouldOrderUnicodeEmojis() +{ + EmojiManager manager; + QMap> map = manager.unicodeEmojiList(); + QVERIFY(map.contains(QStringLiteral("activity"))); + const QVector symbols = map.value(QStringLiteral("symbols")); + QVERIFY(!symbols.isEmpty()); + QCOMPARE(symbols.at(0).order(), 1); + QCOMPARE(symbols.at(0).identifier(), QStringLiteral(":heart:")); + const QVector regional = map.value(QStringLiteral("regional")); + QVERIFY(!regional.isEmpty()); + QCOMPARE(regional.at(0).identifier(), QStringLiteral(":regional_indicator_z:")); // letters are reversed, weird +} + void EmojiManagerTest::shouldGenerateHtml() { const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/restapi/emojiparent.json"); QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject obj = doc.object(); EmojiManager manager(nullptr, false); manager.loadCustomEmoji(obj); //No serverUrl set. QCOMPARE(manager.replaceEmojiIdentifier(QStringLiteral(":foo:")), QStringLiteral(":foo:")); const QString serverUrl = QStringLiteral("www.kde.org"); manager.setServerUrl(serverUrl); // :foo: doesn't exist QCOMPARE(manager.replaceEmojiIdentifier(QStringLiteral(":foo:")), QStringLiteral(":foo:")); QCOMPARE(manager.customEmojiFileName(QStringLiteral(":foo:")), QString()); // Existing emoji QCOMPARE(manager.replaceEmojiIdentifier(QStringLiteral(":vader:")), QStringLiteral("")); QCOMPARE(manager.customEmojiFileName(QStringLiteral(":vader:")), QStringLiteral("/emoji-custom/vader.png")); // Alias QCOMPARE(manager.replaceEmojiIdentifier(QStringLiteral(":darth:")), QStringLiteral("")); QCOMPARE(manager.customEmojiFileName(QStringLiteral(":darth:")), QStringLiteral("/emoji-custom/vader.png")); } void EmojiManagerTest::shouldChangeServerUrl() { const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/json/restapi/emojiparent.json"); QFile f(originalJsonFile); QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray content = f.readAll(); f.close(); const QJsonDocument doc = QJsonDocument::fromJson(content); const QJsonObject obj = doc.object(); EmojiManager manager(nullptr, false); manager.loadCustomEmoji(obj); QString serverUrl = QStringLiteral("www.kde.org"); manager.setServerUrl(serverUrl); //It exists QCOMPARE(manager.replaceEmojiIdentifier(QStringLiteral(":vader:")), QStringLiteral("").arg(serverUrl)); //Change server url => clear cache serverUrl = QStringLiteral("www.bla.org"); manager.setServerUrl(serverUrl); QCOMPARE(manager.replaceEmojiIdentifier(QStringLiteral(":vader:")), QStringLiteral("").arg(serverUrl)); } diff --git a/src/core/autotests/emojimanagertest.h b/src/core/autotests/emojimanagertest.h index 6a09c4f6..fcf61362 100644 --- a/src/core/autotests/emojimanagertest.h +++ b/src/core/autotests/emojimanagertest.h @@ -1,45 +1,46 @@ /* Copyright (c) 2018-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef EMOJIMANAGERTEST_H #define EMOJIMANAGERTEST_H #include class EmojiManagerTest : public QObject { Q_OBJECT public: explicit EmojiManagerTest(QObject *parent = nullptr); ~EmojiManagerTest() override = default; private Q_SLOTS: void shouldHaveDefaultValue(); void shouldGenerateHtml(); void shouldChangeServerUrl(); void shouldParseEmoji_data(); void shouldParseEmoji(); void shouldSupportUnicodeEmojis(); + void shouldOrderUnicodeEmojis(); }; #endif // EMOJIMANAGERTEST_H diff --git a/src/core/emoticons/unicodeemoticonparser.cpp b/src/core/emoticons/unicodeemoticonparser.cpp index 20317bbe..ad141560 100644 --- a/src/core/emoticons/unicodeemoticonparser.cpp +++ b/src/core/emoticons/unicodeemoticonparser.cpp @@ -1,63 +1,71 @@ /* Copyright (c) 2019-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "unicodeemoticonparser.h" #include #include UnicodeEmoticonParser::UnicodeEmoticonParser() { } UnicodeEmoticonParser::~UnicodeEmoticonParser() { } QMap > UnicodeEmoticonParser::parse(const QJsonObject &o) const { QMap > lstEmoticons; const QStringList keys = o.keys(); for (const QString &key : keys) { UnicodeEmoticon emoticon; QJsonObject emojiObj = o[key].toObject(); emoticon.setKey(key); const QString unicodeStr = emojiObj[QStringLiteral("code_points")].toObject()[QStringLiteral("fully_qualified")].toString(); Q_ASSERT(!unicodeStr.isEmpty()); emoticon.setUnicode(unicodeStr); const QString category = emojiObj[QStringLiteral("category")].toString(); emoticon.setCategory(category); emoticon.setIdentifier(emojiObj[QStringLiteral("shortname")].toString()); - emoticon.setOrder(emojiObj[QStringLiteral("emoji_order")].toString().toInt()); + emoticon.setOrder(emojiObj[QStringLiteral("order")].toInt()); const QJsonArray aliasArray = emojiObj[QStringLiteral("shortname_alternates")].toArray(); if (!aliasArray.isEmpty()) { QStringList lst; const int aliasArrayCount = aliasArray.count(); lst.reserve(aliasArrayCount); for (int i = 0; i < aliasArrayCount; ++i) { lst.append(aliasArray.at(i).toString()); } emoticon.setAliases(lst); } if (emoticon.isValid()) { lstEmoticons[category].append(emoticon); } } + auto compareOrder = [](const UnicodeEmoticon &left, const UnicodeEmoticon &right) { + return left.order() < right.order(); + }; + for (auto it = lstEmoticons.begin(); it != lstEmoticons.end(); ++it) { + QVector emoticons = it.value(); + std::sort(emoticons.begin(), emoticons.end(), compareOrder); + *it = emoticons; + } return lstEmoticons; }