// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause #include #include #include #include #include #include // What a tap on a face in a column of avatars does. // // A row squeezed down to an avatar has two things a tap can mean and no room to // say which: open the conversation, or open its menu. The menu is otherwise only // a right-click away, and not every pointing device has one, so in that width the // second tap on the same face is what opens it. The first tap opens the // conversation, as a tap on a row does everywhere else in the list. // // This is a reduction of ChatListPage.qml's part of the conversation, the same way // ChatListHandleDragTest is a reduction of Main.qml's columns: the real page // cannot be created on its own, because ChatFolders and RelativeTime are C++ // singletons in the app's own module and the AccountRow footer wants the settings // and the tray besides. What is copied here is the state machine and the branch // off it, unchanged, and the assertions are all about which of the two a given // sequence of taps lands on. The ChatListPage.qml copy is the one to keep in step // if the behaviour changes; these are the cases that caught it being wrong when // it was written. // // The face itself is not a ContactDelegate, for the same reason: AnimatedAvatar // reaches for the media cache and the settings. What is under test is not the // picture, it is what a tap on the picture is worth. static const char *sceneQml = R"QML( import QtQuick import QtQuick.Controls as QQC2 QQC2.ApplicationWindow { id: root width: 1200 height: 700 visible: true // Stands in for the width check: a column narrow enough to be a strip of // avatars. Real code reads this off the width; here it is set from C++ so the // two states can be entered in either order, which is what a user does. property bool avatarOnly: true // The conversation whose face was tapped once and is waiting for a second // tap, or empty when none is. property string avatarAwaitingSecondTap: "" // What each tap did, in order, for C++ to read back. "chat", "menu", // "card" - the three things a tap on a face can mean. property var taps: [] // ChatListPage.qml's avatarTapWantsMenu, unchanged. Asking and arming are one // function because they are one piece of state: a caller that asked without // arming, or armed without asking, would leave a face that opens its menu // without ever having opened its chat. function avatarTapWantsMenu(chatId) { if (chatId.length === 0) return false if (root.avatarAwaitingSecondTap === chatId) { root.avatarAwaitingSecondTap = "" return true } root.avatarAwaitingSecondTap = chatId return false } // ChatListPage.qml's onAvatarOnlyChanged: nothing is waiting for a second tap // in a column wide enough to say what every row is. onAvatarOnlyChanged: { if (!root.avatarOnly) root.avatarAwaitingSecondTap = "" } // The row's onAvatarClicked, unchanged, with the two calls it makes to the // page standing in as the record of what was decided. function faceTapped(chatId) { if (root.avatarOnly) { if (root.avatarTapWantsMenu(chatId)) openMenu() else openChat() return } openCard() } // The row menu being opened, which is what the second tap asks for and what a // right-click gets to as well. The menu is one for every row, so opening it // from a right-click must not leave a face armed for a tap that would open // it all over again. function openMenu() { root.taps.push("menu") root.avatarAwaitingSecondTap = "" } function openChat() { root.taps.push("chat") } function openCard() { root.taps.push("card") } // The row's right-click, in the order ChatListPage.qml does it. function rowRightClicked() { root.openMenu() } } )QML"; class ChatListAvatarTapTest : public QObject { Q_OBJECT static void settle(int rounds = 4) { for (int i = 0; i < rounds; ++i) QCoreApplication::processEvents(QEventLoop::AllEvents, 10); } QTemporaryDir *m_dir = nullptr; QQmlApplicationEngine *m_view = nullptr; QObject *m_scene = nullptr; QObject *scene() const { return m_scene; } void setNarrow(bool narrow) { m_scene->setProperty("avatarOnly", narrow); settle(); } void tapFace(const QString &chatId) { QMetaObject::invokeMethod(m_scene, "faceTapped", Q_ARG(QVariant, chatId)); settle(); } void rightClick() { QMetaObject::invokeMethod(m_scene, "rowRightClicked"); settle(); } // What every tap so far did, in order. QStringList taps() const { const QVariantList log = m_scene->property("taps").toList(); QStringList out; for (const QVariant &entry : log) out << entry.toString(); return out; } QString armed() const { return m_scene->property("avatarAwaitingSecondTap").toString(); } bool start() { delete m_view; m_view = new QQmlApplicationEngine; QQmlComponent c(m_view, QUrl::fromLocalFile(m_dir->filePath(QStringLiteral("scene.qml")))); if (c.isError()) { for (const QQmlError &e : c.errors()) qWarning("%s", qPrintable(e.toString())); delete m_view; m_view = nullptr; return false; } m_scene = c.create(); if (!m_scene) { for (const QQmlError &e : c.errors()) qWarning("%s", qPrintable(e.toString())); delete m_view; m_view = nullptr; m_scene = nullptr; return false; } m_scene->setParent(m_view); auto *window = qobject_cast(m_scene); window->resize(1200, 700); window->show(); settle(); return window->isVisible(); } private Q_SLOTS: void initTestCase() { m_dir = new QTemporaryDir; QVERIFY(m_dir->isValid()); QFile qml(m_dir->filePath(QStringLiteral("scene.qml"))); QVERIFY(qml.open(QIODevice::WriteOnly)); qml.write(sceneQml); qml.close(); } void cleanupTestCase() { delete m_view; delete m_dir; } // The arrangement itself: the first tap opens the conversation and the second // opens the menu. A face tapped once has armed itself and nothing else. void test_first_tap_opens_the_chat_and_the_second_opens_the_menu() { QVERIFY(start()); tapFace(QStringLiteral("a")); QCOMPARE(taps(), QStringList{QStringLiteral("chat")}); QCOMPARE(armed(), QStringLiteral("a")); tapFace(QStringLiteral("a")); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("menu")})); } // And the face is not left armed once its menu has been asked for: a third // tap starts over rather than reopening the menu forever. void test_the_face_is_disarmed_once_its_menu_has_been_asked_for() { QVERIFY(start()); tapFace(QStringLiteral("a")); tapFace(QStringLiteral("a")); tapFace(QStringLiteral("a")); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("menu"), QStringLiteral("chat")})); } // One face at a time. Tapping a second conversation opens it, and does not // count as the second tap on the first - a user moving along a list of faces // is opening them, not choosing between two menus. void test_only_one_face_is_armed_at_a_time() { QVERIFY(start()); tapFace(QStringLiteral("a")); tapFace(QStringLiteral("b")); QCOMPARE(armed(), QStringLiteral("b")); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("chat")})); // b is the armed one, so this is its menu. a is not: it was replaced. tapFace(QStringLiteral("b")); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("chat"), QStringLiteral("menu")})); tapFace(QStringLiteral("a")); QCOMPARE(armed(), QStringLiteral("a")); } // A right-click opens the same menu, and the menu is one for every row, so it // must not leave the face armed - otherwise the next tap on it opens the menu // again instead of doing what the user came for. void test_a_right_click_does_not_leave_a_face_armed() { QVERIFY(start()); tapFace(QStringLiteral("a")); QCOMPARE(armed(), QStringLiteral("a")); rightClick(); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("menu")})); QCOMPARE(armed(), QString()); tapFace(QStringLiteral("a")); QCOMPARE(taps().last(), QStringLiteral("chat")); } // Widening the column gives the rows their names back, and the two-tap // arrangement goes with them - a face that is a picture of a person beside a // name is asked about as a person again. void test_widening_the_column_goes_back_to_the_contact_card() { QVERIFY(start()); tapFace(QStringLiteral("a")); QCOMPARE(armed(), QStringLiteral("a")); setNarrow(false); QCOMPARE(armed(), QString()); tapFace(QStringLiteral("a")); tapFace(QStringLiteral("a")); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("card"), QStringLiteral("card")})); } // Coming back to a narrow column starts the arrangement over, rather than // finding the face the user tapped before the column was widened still // armed and a single tap opening a menu they never asked for. void test_narrowing_again_does_not_resume_an_earlier_tap() { QVERIFY(start()); tapFace(QStringLiteral("a")); setNarrow(false); setNarrow(true); tapFace(QStringLiteral("a")); QCOMPARE(taps().last(), QStringLiteral("chat")); } // A row with no id cannot be armed, or the first tap on any such row would // find the flag already holding "" and open a menu instead of the // conversation. The list has no rows like this today; the guard is there so // that if it ever has one, it opens rather than hangs. void test_a_row_with_no_id_is_never_armed() { QVERIFY(start()); QCOMPARE(m_scene->property("avatarAwaitingSecondTap").toString(), QString()); tapFace(QString()); QCOMPARE(taps(), QStringList{QStringLiteral("chat")}); QCOMPARE(armed(), QString()); tapFace(QString()); QCOMPARE(taps(), (QStringList{QStringLiteral("chat"), QStringLiteral("chat")})); } }; QTEST_MAIN(ChatListAvatarTapTest) #include "ChatListAvatarTapTest.moc"