Repository navigation
Expand file tree
/
Copy pathtestrecipientmodel.cpp
More file actions
144 lines (124 loc) · 6.47 KB
/
Copy pathtestrecipientmodel.cpp
File metadata and controls
144 lines (124 loc) · 6.47 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QAbstractItemModelTester>
#include <QSignalSpy>
#include <QTest>
#include <memory>
#include "gui/sharing/recipientmodel.h"
#include "gui/sharing/unifiedshare.h"
#include "syncenginetestutils.h"
using namespace OCC;
using namespace OCC::Gui::Sharing;
using namespace Qt::StringLiterals;
namespace
{
std::unique_ptr<Share> shareFromJson(const QJsonObject &data, const AccountPtr &account)
{
return Share::fromJson(QJsonDocument{QJsonObject{{"ocs"_L1, QJsonObject{{"data"_L1, data}}}}}, account);
}
}
class TestRecipientModel : public QObject
{
Q_OBJECT
private Q_SLOTS:
void exposesRecipientDataAndRoles()
{
FakeFolder fakeFolder{{}, {}, {}, false};
const auto share = shareFromJson(
QJsonObject{
{"recipients"_L1,
QJsonArray{
QJsonObject{
{"class"_L1, "federated-user"_L1},
{"display_name"_L1, "Alice"_L1},
{"value"_L1, "alice"_L1},
{"instance"_L1, "cloud.example.com"_L1},
{"icon"_L1, QJsonObject{{"svg"_L1, "<svg/>"_L1}, {"light"_L1, "light.svg"_L1}, {"dark"_L1, "dark.svg"_L1}}},
{"secret"_L1, QJsonObject{{"updatable"_L1, true}, {"value"_L1, "secret"_L1}, {"url"_L1, "https://example.com/s/secret"_L1}}},
{"initiator"_L1, QJsonObject{{"display_name"_L1, "Bob"_L1}}},
},
QJsonObject{
{"class"_L1, "user"_L1},
{"display_name"_L1, "Carol"_L1},
{"value"_L1, "carol"_L1},
},
}},
},
fakeFolder.account());
RecipientModel model;
QAbstractItemModelTester modelTester{&model};
model.setShare(share.get());
QCOMPARE(model.roleNames().value(RecipientModel::LabelRole), "label"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::ClassNameRole), "className"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::ValueRole), "value"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::InstanceRole), "instance"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::IconSvgUrlRole), "iconSvgUrl"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::IconLightRole), "iconLight"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::IconDarkRole), "iconDark"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::SecretUpdatableRole), "secretUpdatable"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::SecretValueRole), "secretValue"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::SecretUrlRole), "secretUrl"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::InitiatorDisplayNameRole), "initiatorDisplayName"_ba);
QCOMPARE(model.roleNames().value(RecipientModel::RecipientRole), "recipient"_ba);
QCOMPARE(model.rowCount(), 2);
const auto firstIndex = model.index(0);
QCOMPARE(model.data(firstIndex, RecipientModel::LabelRole).toString(), "Alice"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::ClassNameRole).toString(), "federated-user"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::ValueRole).toString(), "alice"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::InstanceRole).toString(), "cloud.example.com"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::IconSvgUrlRole).toString(), "data:image/svg+xml;base64,PHN2Zy8+"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::IconLightRole).toString(), "light.svg"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::IconDarkRole).toString(), "dark.svg"_L1);
QVERIFY(model.data(firstIndex, RecipientModel::SecretUpdatableRole).toBool());
QCOMPARE(model.data(firstIndex, RecipientModel::SecretValueRole).toString(), "secret"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::SecretUrlRole).toString(), "https://example.com/s/secret"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::InitiatorDisplayNameRole).toString(), "Bob"_L1);
QCOMPARE(model.data(firstIndex, RecipientModel::RecipientRole).value<Recipient *>(), share->recipients().constFirst());
QVERIFY(model.data(model.index(1), RecipientModel::InstanceRole).toString().isEmpty());
QCOMPARE(model.rowCount(model.index(0)), 0);
}
void resetsForRecipientUpdatesAndIgnoresOldShares()
{
FakeFolder fakeFolder{{}, {}, {}, false};
const auto firstShare = shareFromJson(
QJsonObject{{"recipients"_L1, QJsonArray{QJsonObject{{"class"_L1, "user"_L1}, {"display_name"_L1, "Alice"_L1}, {"value"_L1, "alice"_L1}}}}},
fakeFolder.account());
const auto secondShare = shareFromJson(
QJsonObject{{"recipients"_L1, QJsonArray{QJsonObject{{"class"_L1, "user"_L1}, {"display_name"_L1, "Bob"_L1}, {"value"_L1, "bob"_L1}}}}},
fakeFolder.account());
RecipientModel model;
model.setShare(firstShare.get());
model.setShare(secondShare.get());
QAbstractItemModelTester modelTester{&model};
QSignalSpy resetSpy{&model, &QAbstractItemModel::modelReset};
const auto emptyRecipientsUpdate = [](const auto &share) {
share->updateFromJson(QJsonDocument{QJsonObject{
{"ocs"_L1, QJsonObject{{"data"_L1, QJsonObject{{"recipients"_L1, QJsonArray{}}}}}},
}});
};
emptyRecipientsUpdate(firstShare);
QCOMPARE(resetSpy.count(), 0);
QCOMPARE(model.rowCount(), 1);
emptyRecipientsUpdate(secondShare);
QCOMPARE(resetSpy.count(), 1);
QCOMPARE(model.rowCount(), 0);
}
void clearsWhenShareIsDestroyed()
{
FakeFolder fakeFolder{{}, {}, {}, false};
auto share = shareFromJson(
QJsonObject{{"recipients"_L1, QJsonArray{QJsonObject{{"class"_L1, "user"_L1}, {"display_name"_L1, "Alice"_L1}, {"value"_L1, "alice"_L1}}}}},
fakeFolder.account());
RecipientModel model;
model.setShare(share.get());
QSignalSpy resetSpy{&model, &QAbstractItemModel::modelReset};
share.reset();
QCOMPARE(model.share(), nullptr);
QCOMPARE(model.rowCount(), 0);
QCOMPARE(resetSpy.count(), 1);
}
};
QTEST_MAIN(TestRecipientModel)
#include "testrecipientmodel.moc"