diff --git a/CMakeLists.txt b/CMakeLists.txt index f81816331..dd5fefa77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ include(ECMSetupVersion) include(KDEInstallDirs) include(ECMFindQmlModule) include(KDECMakeSettings) +include(ECMAddTests) include(KDECompilerSettings NO_POLICY_SCOPE) include(ECMAddAppIcon) include(KDEGitCommitHooks) @@ -141,6 +142,10 @@ install(FILES org.kde.neochat.tray.svg DESTINATION ${KDE_INSTALL_FULL_ICONDIR}/h add_definitions(-DQT_NO_FOREACH) add_subdirectory(src) +if (BUILD_TESTING AND Quotient_VERSION_MINOR GREATER 6) + find_package(Qt5 ${QT_MIN_VERSION} NO_MODULE COMPONENTS Test) + add_subdirectory(autotests) +endif() if(KF5DocTools_FOUND) kdoctools_install(po) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt new file mode 100644 index 000000000..6f7ecd0fe --- /dev/null +++ b/autotests/CMakeLists.txt @@ -0,0 +1,10 @@ +# SPDX-FileCopyrightText: 2022 Carl Schwan +# SPDX-License-Identifier: BSD-2-Clause + +enable_testing() + +ecm_add_test( + neochatroomtest.cpp + LINK_LIBRARIES neochat Qt::Test Quotient + TEST_NAME neochatroomtest +) diff --git a/autotests/neochatroomtest.cpp b/autotests/neochatroomtest.cpp new file mode 100644 index 000000000..95cbecfb1 --- /dev/null +++ b/autotests/neochatroomtest.cpp @@ -0,0 +1,139 @@ +// SPDX-FileCopyrightText: 2022 Carl Schwan +// SPDX-License-Identifier: GPL-3.0-or-later + +#include +#include +#include + +#define protected public // please don't hate me +#include "neochatroom.h" +#undef protected + +#include +#include +#include + +using namespace Quotient; + +class NeoChatRoomTest : public QObject { + Q_OBJECT + +private: + Connection *connection = nullptr; + NeoChatRoom *room = nullptr; + +private Q_SLOTS: + void initTestCase(); + void subtitleTextTest(); + void eventTest(); +}; + +void NeoChatRoomTest::initTestCase() +{ + connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); + room = new NeoChatRoom(connection, QStringLiteral("#myroom:kde.org"), JoinState::Join); + + auto json = QJsonDocument::fromJson(R"EVENT({ + "account_data": { + "events": [ + { + "content": { + "tags": { + "u.work": { + "order": 0.9 + } + } + }, + "type": "m.tag" + }, + { + "content": { + "custom_config_key": "custom_config_value" + }, + "type": "org.example.custom.room.config" + } + ] + }, + "ephemeral": { + "events": [ + { + "content": { + "user_ids": [ + "@alice:matrix.org", + "@bob:example.com" + ] + }, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "type": "m.typing" + } + ] + }, + "state": { + "events": [ + { + "content": { + "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", + "displayname": "Alice Margatroid", + "membership": "join", + "reason": "Looking for support" + }, + "event_id": "$143273582443PhrSn:example.org", + "origin_server_ts": 1432735824653, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@example:example.org", + "state_key": "@alice:example.org", + "type": "m.room.member", + "unsigned": { + "age": 1234 + } + } + ] + }, + "summary": { + "m.heroes": [ + "@alice:example.com", + "@bob:example.com" + ], + "m.invited_member_count": 0, + "m.joined_member_count": 2 + }, + "timeline": { + "events": [ + { + "content": { + "body": "This is an **example** text message", + "format": "org.matrix.custom.html", + "formatted_body": "This is an example text message", + "msgtype": "m.text" + }, + "event_id": "$143273582443PhrSn:example.org", + "origin_server_ts": 1432735824654, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@example:example.org", + "type": "m.room.message", + "unsigned": { + "age": 1235 + } + } + ], + "limited": true, + "prev_batch": "t34-23535_0_0" + } +})EVENT"); + SyncRoomData roomData(QStringLiteral("@bob:kde.org"), JoinState::Join, json.object()); + room->updateData(std::move(roomData)); +} + +void NeoChatRoomTest::subtitleTextTest() +{ + QCOMPARE(room->timelineSize(), 1); + QCOMPARE(room->subtitleText(), QStringLiteral("@example:example.org: This is an example text message")); +} + +void NeoChatRoomTest::eventTest() +{ + QCOMPARE(room->timelineSize(), 1); +} + +QTEST_MAIN(NeoChatRoomTest) +#include "neochatroomtest.moc" diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 2df54cd39..f8657283f 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -312,7 +312,6 @@ QDateTime NeoChatRoom::lastActiveTime() QString NeoChatRoom::subtitleText() { - QString subtitle = this->lastEventToString().size() == 0 ? this->topic() : this->lastEventToString(); static const QRegularExpression blockquote("(\r\n\t|\n|\r\t|)> "); static const QRegularExpression heading("(\r\n\t|\n|\r\t|)\\#{1,6} "); static const QRegularExpression newlines("(\r\n\t|\n|\r\t)"); @@ -323,6 +322,7 @@ QString NeoChatRoom::subtitleText() static const QRegularExpression del("(.*)"); static const QRegularExpression multileLineCode("```([^```]+)```"); static const QRegularExpression singleLinecode("`([^`]+)`"); + QString subtitle = lastEventToString().size() == 0 ? topic() : lastEventToString(); subtitle // replace blockquote, i.e. '> text'