-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmessagecontentmodeltest.cpp
More file actions
105 lines (81 loc) · 3.45 KB
/
Copy pathmessagecontentmodeltest.cpp
File metadata and controls
105 lines (81 loc) · 3.45 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
101
102
103
104
105
// SPDX-FileCopyrightText: 2024 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/roommember.h>
#include <Quotient/syncdata.h>
#include "block.h"
#include "models/eventmessagecontentmodel.h"
#include "neochatconnection.h"
#include "testutils.h"
using namespace Quotient;
using namespace Qt::Literals::StringLiterals;
class MessageContentModelTest : public QObject
{
Q_OBJECT
private:
Connection *connection = nullptr;
private Q_SLOTS:
void initTestCase();
void missingEvent();
void hideMedia();
void threadModelNonRoomMessageEvent();
};
void MessageContentModelTest::initTestCase()
{
connection = new NeoChatConnection;
}
void MessageContentModelTest::missingEvent()
{
auto room = new TestUtils::TestRoom(connection, u"#firstRoom:kde.org"_s);
auto model1 = EventMessageContentModel(room, u"$153456789:example.org"_s);
QCOMPARE(model1.rowCount(), 1);
QCOMPARE(model1.data(model1.index(0), MessageContentModel::ComponentTypeRole), Blocks::Loading);
QCOMPARE(model1.data(model1.index(0), MessageContentModel::BlockRole).value<Blocks::BasicTextBlock *>()->display(), u"Loading…"_s);
auto model2 = EventMessageContentModel(room, u"$153456789:example.org"_s, true);
QCOMPARE(model2.rowCount(), 1);
QCOMPARE(model2.data(model2.index(0), MessageContentModel::ComponentTypeRole), Blocks::Loading);
QCOMPARE(model2.data(model1.index(0), MessageContentModel::BlockRole).value<Blocks::BasicTextBlock *>()->display(), u"Loading reply…"_s);
room->syncNewEvents(u"test-min-sync.json"_s);
QCOMPARE(model1.rowCount(), 2);
QCOMPARE(model1.data(model1.index(0), MessageContentModel::ComponentTypeRole), Blocks::Author);
QCOMPARE(model1.data(model1.index(1), MessageContentModel::ComponentTypeRole), Blocks::Text);
QCOMPARE(model1.data(model1.index(1), MessageContentModel::BlockRole).value<Blocks::TextBlock *>()->item()->initialFragment().toPlainText(),
u"This is an example\ntext message"_s);
}
void MessageContentModelTest::hideMedia()
{
auto room = new TestUtils::TestRoom(connection, u"#firstRoom:kde.org"_s);
auto model1 = EventMessageContentModel(room, u"$153456789:example.org"_s);
QList<QString> hiddenEvents;
model1.setSetMediaHidden([&hiddenEvents](const QString &eventId, bool hidden) {
if (hidden) {
hiddenEvents.push_back(eventId);
} else {
hiddenEvents.removeAll(eventId);
}
});
model1.setMediaShouldBeHidden([&hiddenEvents](const QString &eventId) {
return hiddenEvents.contains(eventId);
});
room->syncNewEvents(u"test-min-sync.json"_s);
QCOMPARE(model1.rowCount(), 2);
// Should be false in this case because that's the default without config
QCOMPARE(model1.isMediaHidden(), false);
model1.hideMedia();
QCOMPARE(model1.isMediaHidden(), true);
model1.showMedia();
QCOMPARE(model1.isMediaHidden(), false);
}
void MessageContentModelTest::threadModelNonRoomMessageEvent()
{
// Only checks that this does not crash
auto room = new TestUtils::TestRoom(connection, u"#firstRoom:kde.org"_s);
room->postJson(u"org.kde.foo"_s, {});
ThreadModel(u"$153456789:example.org"_s, room);
}
QTEST_MAIN(MessageContentModelTest)
#include "messagecontentmodeltest.moc"