-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpollblocktest.cpp
More file actions
100 lines (84 loc) · 3.41 KB
/
Copy pathpollblocktest.cpp
File metadata and controls
100 lines (84 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include <QObject>
#include <QSignalSpy>
#include <QTest>
#include <Quotient/connection.h>
#include <Quotient/quotient_common.h>
#include <Quotient/syncdata.h>
#include "events/pollevent.h"
#include "pollblock.h"
#include "testutils.h"
using namespace Quotient;
using namespace Blocks;
class PollBlockTest : public QObject
{
Q_OBJECT
private:
Connection *connection = nullptr;
TestUtils::TestRoom *room = nullptr;
private Q_SLOTS:
void initTestCase();
void nullObject();
void pollEndWithoutRelation();
void poll();
};
void PollBlockTest::initTestCase()
{
connection = Connection::makeMockConnection(u"@bob:kde.org"_s);
room = new TestUtils::TestRoom(connection, u"#myroom:kde.org"_s, u"test-pollhandlerstart-sync.json"_s);
}
// Basically don't crash.
void PollBlockTest::nullObject()
{
auto pollBlock = PollBlock(Poll, {}, nullptr, this);
QCOMPARE(pollBlock.hasEnded(), false);
QCOMPARE(pollBlock.numAnswers(), 0);
QCOMPARE(pollBlock.question(), QString());
QCOMPARE(pollBlock.kind(), PollKind::Disclosed);
}
void PollBlockTest::poll()
{
auto startEvent = eventCast<const PollStartEvent>(room->messageEvents().at(0).get());
auto pollBlock = PollBlock(Poll, startEvent->id(), room, this);
QList<Quotient::EventContent::Answer> options = {EventContent::Answer{"option1"_L1, "option1"_L1}, EventContent::Answer{"option2"_L1, "option2"_L1}};
const auto answer0 = pollBlock.answerAtRow(0);
const auto answer1 = pollBlock.answerAtRow(1);
QCOMPARE(pollBlock.hasEnded(), false);
QCOMPARE(pollBlock.numAnswers(), 2);
QCOMPARE(pollBlock.question(), u"test"_s);
QCOMPARE(answer0.id, "option1"_L1);
QCOMPARE(answer1.id, "option2"_L1);
QCOMPARE(answer0.text, "option1text"_L1);
QCOMPARE(answer1.text, "option2text"_L1);
QCOMPARE(pollBlock.answerCountAtId(answer0.id), 0);
QCOMPARE(pollBlock.answerCountAtId(answer1.id), 0);
QCOMPARE(pollBlock.checkMemberSelectedId(connection->userId(), answer0.id), false);
QCOMPARE(pollBlock.checkMemberSelectedId(connection->userId(), answer1.id), false);
QCOMPARE(pollBlock.kind(), PollKind::Undisclosed);
}
void PollBlockTest::pollEndWithoutRelation()
{
auto startEvent = eventCast<const PollStartEvent>(room->messageEvents().at(0).get());
auto pollBlock = PollBlock(Poll, startEvent->id(), room, this);
std::vector<std::unique_ptr<Quotient::RoomEvent>> events;
events.push_back(loadEvent<RoomEvent>(QJsonObject{
{u"content"_s, QJsonObject()},
{u"event_id"_s, u"$12383:example.org"_s},
{u"origin_server_ts"_s, 14324654},
{u"room_id"_s, u"!jEsUZKDJdhlrceRyVU:example.org"_s},
{u"sender"_s, u"@example:example.org"_s},
{u"type"_s, u"org.matrix.msc3381.poll.end"_s},
}));
events.push_back(loadEvent<RoomEvent>(QJsonObject{
{u"content"_s, QJsonObject()},
{u"event_id"_s, u"$12383123156:example.org"_s},
{u"origin_server_ts"_s, 14324654},
{u"room_id"_s, u"!jEsUZKDJdhlrceRyVU:example.org"_s},
{u"sender"_s, u"@example:example.org"_s},
{u"type"_s, u"org.matrix.msc3381.poll.response"_s},
}));
Q_EMIT room->aboutToAddNewMessages(RoomEventsRange(events.begin(), events.end()));
}
QTEST_GUILESS_MAIN(PollBlockTest)
#include "pollblocktest.moc"