// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause #include #include #include "../core/emoji/EmojiModel.h" // What the picker's search box finds. // // The panel now names an emoji by the CLDR name in its tooltip, because that is // the only one of the three names a person can read: the unicode is the glyph // itself, and the shortcode is ":thumbsup:". Telegram searches the same words it // shows, so a search that matched the shortcode alone left the box unable to find // anything it was willing to call by name - typing "thumbs up" returned nothing // where ":thumbsup:" returned the emoji. // // Only the search is under test. The panel round it is QML, and what a tap does // there is EmojiPicker's own business; this is the table it searches, which is // where the two names are both present and only one of them was read. // // The table is filled in the constructor of the singleton, and the search is a // static that reads it, so the instance is touched first: a search run before // anything has built the model would match nothing at all and pass. class EmojiModelTest : public QObject { Q_OBJECT private: static QList matches(const QString &term, bool limit = false) { QList result; const auto found = EmojiModel::filterModel(term, limit); result.reserve(found.size()); for (const auto &entry : found) { result.append(qvariant_cast(entry)); } return result; } // the shortcodes alone, which is what there is to compare two answers with: // Emoji is a gadget and has no operator== of its own static QStringList codes(const QString &term) { QStringList result; for (const auto &emoji : matches(term)) { result.append(emoji.shortName); } return result; } static bool holds(const QList &found, const QString &shortName) { for (const auto &emoji : found) { if (emoji.shortName == shortName) { return true; } } return false; } private Q_SLOTS: void initTestCase() { // the categories are named with i18nc, and without a domain every one of // them warns that it cannot be translated KLocalizedString::setApplicationDomain(QByteArrayLiteral("koutnet")); // the table lives in the singleton, and the search is static EmojiModel::instance(); } void findsByShortcode() { // the shortcode is stored with its colons, and a search matches inside // one rather than needing to be given the whole of it const auto found = matches(QStringLiteral("thumbsup")); QVERIFY(!found.isEmpty()); QVERIFY(holds(found, QStringLiteral(":thumbsup:"))); } void findsByWordsOfTheName_data() { QTest::addColumn("term"); // words of the CLDR name that are not in the shortcode, so only the // description can be what matched QTest::newRow("two words") << QStringLiteral("thumbs up"); QTest::newRow("spaced words") << QStringLiteral("rolling on the floor"); QTest::newRow("lower case") << QStringLiteral("grinning face with big eyes"); QTest::newRow("part of a name") << QStringLiteral("crescent moon"); } void findsByWordsOfTheName() { QFETCH(QString, term); const auto found = matches(term); QVERIFY2(!found.isEmpty(), qPrintable(QStringLiteral("nothing found for \"%1\"").arg(term))); for (const auto &emoji : found) { // every hit is a real answer to what was typed: either the codepoint // contains the term or the name it is shown under does. Anything // else means the search has started answering something else. QVERIFY2(emoji.shortName.contains(term, Qt::CaseInsensitive) || emoji.description.contains(term, Qt::CaseInsensitive), qPrintable(QStringLiteral("\"%1\" is in neither name of %2").arg(term, emoji.shortName))); } } void matchesTheNameWhateverItsCase() { // the shortcode search has always been case insensitive and the name // search came in with it; typing in caps has to find the same emoji QVERIFY(!codes(QStringLiteral("THUMBS UP")).isEmpty()); QCOMPARE(codes(QStringLiteral("THUMBS UP")), codes(QStringLiteral("thumbs up"))); } void nothingForRubbish() { // an empty answer is what makes the panel's placeholder message show QVERIFY(matches(QStringLiteral("qqqzzz")).isEmpty()); } void everyCategoryIsReachable() { // the tabs of the panel are the categories less the history, which the // strip above them holds instead, so each of these has to have emoji in // it or its tab opens onto an empty grid for (const auto category : {EmojiModel::Smileys, EmojiModel::People, EmojiModel::Nature, EmojiModel::Food, EmojiModel::Activities, EmojiModel::Travel, EmojiModel::Objects, EmojiModel::Symbols, EmojiModel::Flags}) { const auto name = QString::number(int(category)); QVERIFY2(!EmojiModel::instance().emojis(category).isEmpty(), qPrintable(QStringLiteral("category %1 is empty").arg(name))); } } void historyIsTheFirstOfTheCategories() { // the panel reads the first entry as the history and starts its tabs at // the one after it; if history ever moved, the strip would be showing a // category and the first tab would be the empty history const auto categories = EmojiModel::instance().categories(); QVERIFY(!categories.isEmpty()); QCOMPARE(categories.first().toMap().value(QStringLiteral("category")).toInt(), int(EmojiModel::History)); } }; QTEST_GUILESS_MAIN(EmojiModelTest) #include "EmojiModelTest.moc"