File indexing completed on 2024-05-05 05:01:18
0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com> 0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0003 0004 #include <QObject> 0005 #include <QSignalSpy> 0006 #include <QTest> 0007 0008 #include <Quotient/connection.h> 0009 #include <Quotient/quotient_common.h> 0010 #include <Quotient/syncdata.h> 0011 0012 #include "events/pollevent.h" 0013 #include "pollhandler.h" 0014 #include "testutils.h" 0015 0016 using namespace Quotient; 0017 0018 class PollHandlerTest : public QObject 0019 { 0020 Q_OBJECT 0021 0022 private: 0023 Connection *connection = nullptr; 0024 TestUtils::TestRoom *room = nullptr; 0025 0026 private Q_SLOTS: 0027 void initTestCase(); 0028 void nullObject(); 0029 void poll(); 0030 }; 0031 0032 void PollHandlerTest::initTestCase() 0033 { 0034 connection = Connection::makeMockConnection(QStringLiteral("@bob:kde.org")); 0035 room = new TestUtils::TestRoom(connection, QStringLiteral("#myroom:kde.org"), "test-pollhandlerstart-sync.json"_ls); 0036 } 0037 0038 // Basically don't crash. 0039 void PollHandlerTest::nullObject() 0040 { 0041 auto pollHandler = PollHandler(); 0042 0043 QCOMPARE(pollHandler.hasEnded(), false); 0044 QCOMPARE(pollHandler.answerCount(), 0); 0045 QCOMPARE(pollHandler.question(), QString()); 0046 QCOMPARE(pollHandler.options(), QJsonArray()); 0047 QCOMPARE(pollHandler.answers(), QJsonObject()); 0048 QCOMPARE(pollHandler.counts(), QJsonObject()); 0049 QCOMPARE(pollHandler.kind(), QString()); 0050 } 0051 0052 void PollHandlerTest::poll() 0053 { 0054 auto startEvent = eventCast<const PollStartEvent>(room->messageEvents().at(0).get()); 0055 auto pollHandler = PollHandler(room, startEvent); 0056 0057 auto options = QJsonArray{QJsonObject{{"id"_ls, "option1"_ls}, {"org.matrix.msc1767.text"_ls, "option1"_ls}}, 0058 QJsonObject{{"id"_ls, "option2"_ls}, {"org.matrix.msc1767.text"_ls, "option2"_ls}}}; 0059 0060 QCOMPARE(pollHandler.hasEnded(), false); 0061 QCOMPARE(pollHandler.answerCount(), 0); 0062 QCOMPARE(pollHandler.question(), QStringLiteral("test")); 0063 QCOMPARE(pollHandler.options(), options); 0064 QCOMPARE(pollHandler.answers(), QJsonObject()); 0065 QCOMPARE(pollHandler.counts(), QJsonObject()); 0066 QCOMPARE(pollHandler.kind(), QStringLiteral("org.matrix.msc3381.poll.disclosed")); 0067 } 0068 0069 QTEST_GUILESS_MAIN(PollHandlerTest) 0070 #include "pollhandlertest.moc"