-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcompletionmodeltest.cpp
More file actions
177 lines (147 loc) · 7.03 KB
/
Copy pathcompletionmodeltest.cpp
File metadata and controls
177 lines (147 loc) · 7.03 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: 2026 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 <QAbstractItemModelTester>
#include <QObject>
#include <QTest>
#include "completionlist.h"
#include "completionmodel.h"
using namespace Qt::Literals::StringLiterals;
class TestCompletionList : public CompletionList
{
Q_OBJECT
QML_ELEMENT
public:
explicit TestCompletionList(QObject *parent = nullptr, qsizetype size = 0, const QString &listName = {})
: CompletionList(parent)
, m_size(size)
, m_listName(listName)
{
}
qsizetype size() const override
{
return m_size;
}
QVariant data(qsizetype row, int role = Qt::DisplayRole) const override
{
if (row < 0 || row >= m_size) {
return {};
}
switch (role) {
case CompletionModel::TitleRole:
return u"Title %1 Index %2"_s.arg(m_listName, QString::number(row));
case CompletionModel::DescriptionRole:
return u"Description %1 Index %2"_s.arg(m_listName, QString::number(row));
case CompletionModel::AvatarSourceRole:
return QUrl(u"Avatar %1 Index %2"_s.arg(m_listName, QString::number(row)));
case CompletionModel::StartSequenceRole:
return startSequence();
case CompletionModel::MatchSequencesRole:
return QStringList{u"Match Sequence %1 Index %2"_s.arg(m_listName, QString::number(row))};
case CompletionModel::ReplaceStringRole:
return u"Replace String %1 Index %2"_s.arg(m_listName, QString::number(row));
case CompletionModel::HRefRole:
return QUrl(u"hRef %1 Index %2"_s.arg(m_listName, QString::number(row)));
default:
return {};
}
}
QString startSequence() const override
{
return m_listName;
}
void setSize(qsizetype size)
{
if (size > m_size) {
Q_EMIT CompletionList::completionsAdded(this, m_size, size - 1);
m_size = size;
} else if (size < m_size) {
Q_EMIT CompletionList::completionsRemoved(this, size, m_size - 1);
m_size = size;
}
}
private:
qsizetype m_size;
QString m_listName;
};
class CompletionModelTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void multipleLists();
void longCurrentText();
};
void CompletionModelTest::multipleLists()
{
auto model = new CompletionModel(this);
auto tester = new QAbstractItemModelTester(model, model);
tester->setUseFetchMore(true);
const auto list1 = new TestCompletionList(this, 4, u"1"_s);
const auto list2 = new TestCompletionList(this, 3, u"2"_s);
const auto list3 = new TestCompletionList(this, 5, u"3"_s);
const auto list4 = new TestCompletionList(this, 2, u"3"_s);
// large fake data to make sure to produce invalid indexes if wrongly considered part of the model data
const auto list5 = new TestCompletionList(this, 1000, u"5"_s);
model->setCompletionLists({list1, list2, list3, list4, list5});
QCOMPARE(model->rowCount(), 0);
model->setCurrentText(u"1"_s);
QCOMPARE(model->rowCount(), 4);
// This item should be in list 1
QCOMPARE(model->index(0).data(CompletionModel::TitleRole), u"Title 1 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::DescriptionRole), u"Description 1 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::AvatarSourceRole), QUrl(u"Avatar 1 Index 0"_s));
QCOMPARE(model->index(0).data(CompletionModel::StartSequenceRole), u"1"_s);
QCOMPARE(model->index(0).data(CompletionModel::MatchSequencesRole), QStringList{u"Match Sequence 1 Index 0"_s});
QCOMPARE(model->index(0).data(CompletionModel::ReplaceStringRole), u"Replace String 1 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::HRefRole), QUrl(u"hRef 1 Index 0"_s));
model->setCurrentText(u"2"_s);
QCOMPARE(model->rowCount(), 3);
// This item should be in list 2
QCOMPARE(model->index(0).data(CompletionModel::TitleRole), u"Title 2 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::DescriptionRole), u"Description 2 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::AvatarSourceRole), QUrl(u"Avatar 2 Index 0"_s));
QCOMPARE(model->index(0).data(CompletionModel::StartSequenceRole), u"2"_s);
QCOMPARE(model->index(0).data(CompletionModel::MatchSequencesRole), QStringList{u"Match Sequence 2 Index 0"_s});
QCOMPARE(model->index(0).data(CompletionModel::ReplaceStringRole), u"Replace String 2 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::HRefRole), QUrl(u"hRef 2 Index 0"_s));
model->setCurrentText(u"3"_s);
QCOMPARE(model->rowCount(), 7);
// This item should be in list 3 index 0
QCOMPARE(model->index(0).data(CompletionModel::TitleRole), u"Title 3 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::DescriptionRole), u"Description 3 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::AvatarSourceRole), QUrl(u"Avatar 3 Index 0"_s));
QCOMPARE(model->index(0).data(CompletionModel::StartSequenceRole), u"3"_s);
QCOMPARE(model->index(0).data(CompletionModel::MatchSequencesRole), QStringList{u"Match Sequence 3 Index 0"_s});
QCOMPARE(model->index(0).data(CompletionModel::ReplaceStringRole), u"Replace String 3 Index 0"_s);
QCOMPARE(model->index(0).data(CompletionModel::HRefRole), QUrl(u"hRef 3 Index 0"_s));
// This item should be in list 4 index 0
QCOMPARE(model->index(5).data(CompletionModel::TitleRole), u"Title 3 Index 0"_s);
QCOMPARE(model->index(5).data(CompletionModel::DescriptionRole), u"Description 3 Index 0"_s);
QCOMPARE(model->index(5).data(CompletionModel::AvatarSourceRole), QUrl(u"Avatar 3 Index 0"_s));
QCOMPARE(model->index(5).data(CompletionModel::StartSequenceRole), u"3"_s);
QCOMPARE(model->index(5).data(CompletionModel::MatchSequencesRole), QStringList{u"Match Sequence 3 Index 0"_s});
QCOMPARE(model->index(5).data(CompletionModel::ReplaceStringRole), u"Replace String 3 Index 0"_s);
QCOMPARE(model->index(5).data(CompletionModel::HRefRole), QUrl(u"hRef 3 Index 0"_s));
// These should not cause any change visible in the QAbstractItemModel interface of CompletionModel, in particular
// no signals or calls that mention indexes not actually in the model.
list5->setSize(2000);
QCOMPARE(model->rowCount(), 7);
list5->setSize(500);
QCOMPARE(model->rowCount(), 7);
}
void CompletionModelTest::longCurrentText()
{
auto model = new CompletionModel(this);
auto tester = new QAbstractItemModelTester(model, model);
tester->setUseFetchMore(true);
const auto list1 = new TestCompletionList(this, 4, u"1"_s);
model->setCompletionLists({list1});
QCOMPARE(model->rowCount(), 0);
model->setCurrentText(u"1test"_s);
QCOMPARE(model->rowCount(), 4);
model->setCurrentText(u"1testtest"_s);
QCOMPARE(model->rowCount(), 4);
model->setCurrentText({});
QCOMPARE(model->rowCount(), 0);
}
QTEST_MAIN(CompletionModelTest)
#include "completionmodeltest.moc"