From 02fdd08b98d878168eb81376a44586176dfd9576 Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Sun, 26 Dec 2021 10:40:56 +0100 Subject: Quotest: test sending and receiving custom events This seems to be the crux of #413. --- quotest/quotest.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) (limited to 'quotest') diff --git a/quotest/quotest.cpp b/quotest/quotest.cpp index 7bd9c5c3..5a5642fe 100644 --- a/quotest/quotest.cpp +++ b/quotest/quotest.cpp @@ -101,6 +101,7 @@ private slots: TEST_DECL(sendMessage) TEST_DECL(sendReaction) TEST_DECL(sendFile) + TEST_DECL(sendCustomEvent) TEST_DECL(setTopic) TEST_DECL(changeName) TEST_DECL(sendAndRedact) @@ -125,6 +126,7 @@ private: [[nodiscard]] bool checkRedactionOutcome(const QByteArray& thisTest, const QString& evtIdToRedact); + template [[nodiscard]] bool validatePendingEvent(const QString& txnId); [[nodiscard]] bool checkDirectChat() const; void finishTest(const TestToken& token, bool condition, const char* file, @@ -152,12 +154,14 @@ void TestSuite::doTest(const QByteArray& testName) Q_ARG(TestToken, testName)); } +template bool TestSuite::validatePendingEvent(const QString& txnId) { auto it = targetRoom->findPendingEvent(txnId); return it != targetRoom->pendingEvents().end() && it->deliveryStatus() == EventStatus::Submitted - && (*it)->transactionId() == txnId; + && (*it)->transactionId() == txnId && is(**it) + && (*it)->matrixType() == EventT::matrixTypeId(); } void TestSuite::finishTest(const TestToken& token, bool condition, @@ -341,7 +345,7 @@ TEST_IMPL(loadMembers) TEST_IMPL(sendMessage) { auto txnId = targetRoom->postPlainText("Hello, " % origin % " is here"); - if (!validatePendingEvent(txnId)) { + if (!validatePendingEvent(txnId)) { clog << "Invalid pending event right after submitting" << endl; FAIL_TEST(); } @@ -367,7 +371,7 @@ TEST_IMPL(sendReaction) const auto targetEvtId = targetRoom->messageEvents().back()->id(); const auto key = QStringLiteral("+1"); const auto txnId = targetRoom->postReaction(targetEvtId, key); - if (!validatePendingEvent(txnId)) { + if (!validatePendingEvent(txnId)) { clog << "Invalid pending event right after submitting" << endl; FAIL_TEST(); } @@ -409,7 +413,7 @@ TEST_IMPL(sendFile) clog << "Sending file " << tfName.toStdString() << endl; const auto txnId = targetRoom->postFile( "Test file", new EventContent::FileContent(tfi)); - if (!validatePendingEvent(txnId)) { + if (!validatePendingEvent(txnId)) { clog << "Invalid pending event right after submitting" << endl; tf->deleteLater(); FAIL_TEST(); @@ -518,6 +522,50 @@ bool TestSuite::checkFileSendingOutcome(const TestToken& thisTest, return true; } +class CustomEvent : public RoomEvent { +public: + DEFINE_EVENT_TYPEID("quotest.custom", CustomEvent) + + CustomEvent(const QJsonObject& jo) + : RoomEvent(typeId(), jo) + {} + CustomEvent(int testValue) + : RoomEvent(typeId(), + basicEventJson(matrixTypeId(), + QJsonObject { { "testValue"_ls, + toJson(testValue) } })) + {} + + auto testValue() const { return contentPart("testValue"_ls); } +}; +REGISTER_EVENT_TYPE(CustomEvent) + +TEST_IMPL(sendCustomEvent) +{ + auto txnId = targetRoom->postEvent(new CustomEvent(42)); + if (!validatePendingEvent(txnId)) { + clog << "Invalid pending event right after submitting" << endl; + FAIL_TEST(); + } + connectUntil( + targetRoom, &Room::pendingEventAboutToMerge, this, + [this, thisTest, txnId](const RoomEvent* evt, int pendingIdx) { + const auto& pendingEvents = targetRoom->pendingEvents(); + Q_ASSERT(pendingIdx >= 0 && pendingIdx < int(pendingEvents.size())); + + if (evt->transactionId() != txnId) + return false; + + return switchOnType(*evt, + [this, thisTest, &evt](const CustomEvent& e) { + FINISH_TEST(!evt->id().isEmpty() && e.testValue() == 42); + }, + [this, thisTest] (const RoomEvent&) { FAIL_TEST(); }); + }); + return false; + +} + TEST_IMPL(setTopic) { const auto newTopic = connection()->generateTxnId(); // Just a way to make @@ -567,7 +615,6 @@ TEST_IMPL(changeName) return false; } - TEST_IMPL(showLocalUsername) { auto* const localUser = connection()->user(); -- cgit v1.2.3 From 39b50a13a0379ea32be114c85f3c697d75d4e03b Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Wed, 29 Dec 2021 21:22:26 +0100 Subject: Quotest: build with hidden visibility too --- quotest/CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'quotest') diff --git a/quotest/CMakeLists.txt b/quotest/CMakeLists.txt index cb41141d..ec305620 100644 --- a/quotest/CMakeLists.txt +++ b/quotest/CMakeLists.txt @@ -8,6 +8,11 @@ find_package(${Qt} COMPONENTS Concurrent) add_executable(quotest ${quotest_SRCS}) target_link_libraries(quotest PRIVATE ${Qt}::Core ${Qt}::Test ${Qt}::Concurrent ${PROJECT_NAME}) +set_target_properties(quotest PROPERTIES + VISIBILITY_INLINES_HIDDEN ON + CXX_VISIBILITY_PRESET hidden +) + if (MSVC) target_compile_options(quotest PUBLIC /EHsc /W4 /wd4100 /wd4127 /wd4242 /wd4244 /wd4245 /wd4267 /wd4365 /wd4456 /wd4459 -- cgit v1.2.3