// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause #include #include #include #include #include #include #include // Where a category of the emoji panel is left, and what the tabs are. // // Two things about the panel's arrangement are state rather than layout, and both // are there to make it behave the way Telegram's does: // // - Telegram keeps the scroll top of each category tab, so going back to a // category finds it where it was left (TabbedSelector::Tab::saveScrollTop), // while a fresh search starts at the top of the grid // (EmojiPickerOverlay::refreshGridEmojis calls scrollToY(0)). // - Telegram lists the recently used emoji in a strip above the grid rather // than as a tab of its own, so its first category is the one the strip holds // and the tabs start at the one after it. KOutNet's model still lists the // history first, so the panel reads that as the strip and starts its tabs one // along. A tab that did not skip it would open onto an empty grid, and a tab // built from the model unshifted would point at the wrong category. // // This is a reduction of EmojiPicker.qml, the same way ChatListAvatarTapTest is a // reduction of ChatListPage.qml: the real panel cannot be created on its own, // because EmojiModel and Kirigami are singletons of the app's own module. What // is copied here is the tab list, the grid and the two functions that move // between them, unchanged, and the assertions are all about where the grid ended // up. The EmojiPicker.qml copy is the one to keep in step if the behaviour // changes; these are the cases that catch it being wrong when it was written. // // The grid is a plain GridView over a row count rather than over the emoji // table: what is under test is the position it is left at, and a row count gives // every category a height to be scrolled within. static const char *sceneQml = R"QML( import QtQuick import QtQuick.Controls as QQC2 QQC2.ApplicationWindow { id: root width: 400 height: 300 visible: true // stands in for EmojiModel.categories: the history first, as the model has // it, and enough rows behind each category that it is taller than the grid // and can be scrolled within at all readonly property var categories: [ { category: 0, name: "History", rows: 4 }, { category: 1, name: "Smileys", rows: 200 }, { category: 2, name: "People", rows: 100 }, { category: 3, name: "Nature", rows: 400 }, { category: 4, name: "Food", rows: 60 } ] readonly property int tabCount: Math.max(0, root.categories.length - 1) readonly property var currentCategory: tabs.currentIndex >= 0 ? root.categories[tabs.currentIndex + 1].category : 1 property var shownCategory: -1 property var scrollTops: ({}) // how many rows the grid is showing: the category's own, or what a search // left. Off the category rather than off the tab, the way the picker's model // binding is off its currentCategory, so that the grid is given the new one // after the change below has been announced. property int searchRows: 0 readonly property var gridModel: root.searchRows > 0 ? root.searchRows : root.categories[root.currentCategory].rows // The top of the category being left is taken here and not from a handler on // the grid's model, because this runs first: a grid handed a shorter model // puts itself back at the top there and then, and a top read after that is // the top of the category just arrived at rather than the one left behind. function showCategory() { if (root.currentCategory === root.shownCategory) { return; } root.rememberScrollTop(); root.shownCategory = root.currentCategory; root.restoreScrollTop(); } function rememberScrollTop() { if (root.shownCategory >= 0) { root.scrollTops[root.shownCategory] = grid.contentY; } } function restoreScrollTop() { const saved = root.scrollTops[root.shownCategory]; Qt.callLater(() => { grid.contentY = saved === undefined ? 0 : saved; }); } onCurrentCategoryChanged: showCategory() // what the picker's search field does through onTextChanged: a search // starts at the top, and emptying it goes back where the category was onSearchRowsChanged: if (searchRows === 0) { root.restoreScrollTop(); } else { grid.contentY = 0; } ListView { id: tabs objectName: "tabs" width: parent.width height: 20 orientation: ListView.Horizontal model: root.tabCount currentIndex: 0 } GridView { id: grid objectName: "grid" width: parent.width // five rows of thirty, so that a category of more than sixty-five rows // is taller than this and can be scrolled within height: 150 clip: true model: root.gridModel cellWidth: 30 cellHeight: 30 // the cells themselves are of no interest here, only the grid they are in, // but a grid with no delegate of its own reports itself empty in a // headless run and would never be a height to scroll within delegate: Rectangle { } } } )QML"; class EmojiPanelScrollTest : public QObject { Q_OBJECT static void settle(int rounds = 6) { for (int i = 0; i < rounds; ++i) { QCoreApplication::processEvents(QEventLoop::AllEvents, 20); } } QTemporaryDir *m_dir = nullptr; QQmlApplicationEngine *m_view = nullptr; QObject *m_scene = nullptr; // read a property off the scene, so the assertions are about what the panel // decided rather than about the C++ that drove it QVariant property(const char *name) const { return m_scene->property(name); } QQuickItem *item(const char *name) const { return m_scene->findChild(QString::fromLatin1(name)); } QObject *grid() const { return item("grid"); } QObject *tabs() const { return item("tabs"); } // Where the grid is scrolled to. A scroll top is put back after the model // has been through the grid again, on a later turn of the event loop, so a // read taken the instant after a change would catch the panel before it had // decided and pass by accident. int contentY() { settle(); return grid()->property("contentY").toInt(); } // move to a tab, and let the panel put the grid where it keeps that category int goToTab(int index) { if (!tabs()->setProperty("currentIndex", index)) { return -1; } return contentY(); } private Q_SLOTS: void initTestCase() { m_dir = new QTemporaryDir; QVERIFY(m_dir->isValid()); QFile scene(m_dir->filePath(QStringLiteral("scene.qml"))); QVERIFY(scene.open(QIODevice::WriteOnly)); QCOMPARE(scene.write(sceneQml), qint64(qstrlen(sceneQml))); } void cleanupTestCase() { delete m_dir; } void init() { delete m_view; m_view = new QQmlApplicationEngine; QQmlComponent c(m_view, QUrl::fromLocalFile(m_dir->filePath(QStringLiteral("scene.qml")))); if (c.isError()) { for (const auto &e : c.errors()) { qWarning("%s", qPrintable(e.toString())); } QFAIL("the scene does not load"); } m_scene = c.create(); QVERIFY(m_scene); m_scene->setParent(m_view); auto *window = qobject_cast(m_scene); QVERIFY(window); window->resize(400, 300); window->show(); settle(); QVERIFY(window->isVisible()); } void cleanup() { delete m_view; m_view = nullptr; m_scene = nullptr; } void startsAtTheTop() { // the grid opens where every category is opened for the first time QCOMPARE(property("shownCategory").toInt(), 1); QCOMPARE(contentY(), 0); } void aCategoryComesBackWhereItWasLeft_data() { QTest::addColumn("leftAt"); // a scroll top is a number of pixels into a grid of a known height, so // what matters is that a position which is not the top survives. Smileys // is two hundred rows of thirty in a hundred and fifty of grid, so it // can be scrolled three hundred and thirty pixels at most. QTest::newRow("far down") << 300; QTest::newRow("one row down") << 30; QTest::newRow("the last row") << 330; } void aCategoryComesBackWhereItWasLeft() { QFETCH(int, leftAt); // smileys, away from there, and off to people, which is too short to // have been anywhere grid()->setProperty("contentY", leftAt); QCOMPARE(contentY(), leftAt); QCOMPARE(goToTab(1), 0); QCOMPARE(property("shownCategory").toInt(), 2); QCOMPARE(goToTab(0), leftAt); QCOMPARE(property("shownCategory").toInt(), 1); } void eachCategoryRemembersItsOwnPlace() { // leaving smileys, nature and people each part-way down, so that none of // them can be showing another's grid()->setProperty("contentY", 200); QCOMPARE(goToTab(2), 0); // nature, the tallest of them grid()->setProperty("contentY", 600); QCOMPARE(goToTab(1), 0); // people grid()->setProperty("contentY", 60); QCOMPARE(goToTab(0), 200); QCOMPARE(goToTab(2), 600); QCOMPARE(goToTab(1), 60); } void aSearchStartsAtTheTop() { grid()->setProperty("contentY", 300); QCOMPARE(contentY(), 300); // three rows of hits have no room for a scroll top at all, and Telegram // puts the grid at the top of them either way QVERIFY(m_scene->setProperty("searchRows", 3)); QCOMPARE(contentY(), 0); } void clearingASearchGoesBackWhereTheCategoryWas() { // away and back first, so that the place smileys was left at is one the // panel has actually been told to keep grid()->setProperty("contentY", 300); QCOMPARE(goToTab(1), 0); QCOMPARE(goToTab(0), 300); QVERIFY(m_scene->setProperty("searchRows", 3)); QCOMPARE(contentY(), 0); // and the category's own place is remembered across the search, the way // the panel restores it when the search field is emptied QVERIFY(m_scene->setProperty("searchRows", 0)); QCOMPARE(contentY(), 300); } void theTabsStartOnePastTheHistory_data() { QTest::addColumn("expected"); QTest::addColumn("name"); // the model has the history first and the panel shows it as the strip QTest::newRow("smileys") << 1 << QStringLiteral("Smileys"); QTest::newRow("people") << 2 << QStringLiteral("People"); QTest::newRow("the last one") << 4 << QStringLiteral("Food"); } void theTabsStartOnePastTheHistory() { QFETCH(int, expected); QFETCH(QString, name); // one tab fewer than there are categories, because one of them is the // strip, and the category behind a tab is the one after the history QCOMPARE(property("tabCount").toInt(), 4); goToTab(expected - 1); QCOMPARE(property("currentCategory").toInt(), expected); const auto categories = property("categories").toList(); QCOMPARE(categories.at(expected).toMap().value(QStringLiteral("name")).toString(), name); } }; QTEST_MAIN(EmojiPanelScrollTest) #include "EmojiPanelScrollTest.moc"