Changeset View
Changeset View
Standalone View
Standalone View
autotests/kcolumnheadersmodeltest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (c) 2019 Arjen Hiemstra <ahiemstra@heimr.nl> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or modify it | ||||
5 | under the terms of the GNU Library General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||||
7 | option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||||
12 | License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Library General Public License | ||||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||||
17 | 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include <QTest> | ||||
21 | #include <QStandardItemModel> | ||||
22 | #include <QAbstractItemModelTester> | ||||
23 | #include <QSignalSpy> | ||||
24 | | ||||
25 | #include <KColumnHeadersModel> | ||||
26 | | ||||
27 | class KColumnHeadersModelTest : public QObject | ||||
28 | { | ||||
29 | Q_OBJECT | ||||
30 | | ||||
31 | private Q_SLOTS: | ||||
32 | void testStatic() | ||||
33 | { | ||||
34 | auto model = new KColumnHeadersModel{}; | ||||
35 | | ||||
36 | auto sourceModel = new QStandardItemModel{}; | ||||
37 | sourceModel->setHorizontalHeaderLabels({ | ||||
38 | QStringLiteral("Test 1"), | ||||
39 | QStringLiteral("Test 2"), | ||||
40 | QStringLiteral("Test 3"), | ||||
41 | QStringLiteral("Test 4"), | ||||
42 | QStringLiteral("Test 5") | ||||
43 | }); | ||||
44 | | ||||
45 | model->setSourceModel(sourceModel); | ||||
46 | | ||||
47 | auto tester = new QAbstractItemModelTester(model); | ||||
48 | Q_UNUSED(tester); | ||||
49 | | ||||
50 | QCOMPARE(model->rowCount(), 5); | ||||
51 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
52 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
53 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
54 | QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4")); | ||||
55 | QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5")); | ||||
56 | | ||||
57 | QSignalSpy spy{model, &QAbstractItemModel::dataChanged}; | ||||
58 | QVERIFY(spy.isValid()); | ||||
59 | | ||||
60 | sourceModel->setHorizontalHeaderLabels({ | ||||
61 | QStringLiteral("Test 5"), | ||||
62 | QStringLiteral("Test 4"), | ||||
63 | QStringLiteral("Test 3"), | ||||
64 | QStringLiteral("Test 2"), | ||||
65 | QStringLiteral("Test 1") | ||||
66 | }); | ||||
67 | | ||||
68 | QCOMPARE(spy.count(), 4); | ||||
69 | } | ||||
70 | | ||||
71 | void testAddColumns() | ||||
72 | { | ||||
73 | auto model = new KColumnHeadersModel{}; | ||||
74 | auto sourceModel = new QStandardItemModel{}; | ||||
75 | sourceModel->setHorizontalHeaderLabels({ | ||||
76 | QStringLiteral("Test 1"), | ||||
77 | QStringLiteral("Test 2") | ||||
78 | }); | ||||
79 | model->setSourceModel(sourceModel); | ||||
80 | | ||||
81 | auto tester = new QAbstractItemModelTester(model); | ||||
82 | Q_UNUSED(tester); | ||||
83 | | ||||
84 | QSignalSpy spy{model, &QAbstractItemModel::rowsInserted}; | ||||
85 | QVERIFY(spy.isValid()); | ||||
86 | | ||||
87 | QCOMPARE(model->rowCount(), 2); | ||||
88 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
89 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
90 | | ||||
91 | sourceModel->setHorizontalHeaderLabels({ | ||||
92 | QStringLiteral("Test 1"), | ||||
93 | QStringLiteral("Test 2"), | ||||
94 | QStringLiteral("Test 3") | ||||
95 | }); | ||||
96 | | ||||
97 | QCOMPARE(spy.count(), 1); | ||||
98 | QCOMPARE(model->rowCount(), 3); | ||||
99 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
100 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
101 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
102 | | ||||
103 | sourceModel->setHorizontalHeaderLabels({ | ||||
104 | QStringLiteral("Test 1"), | ||||
105 | QStringLiteral("Test 2"), | ||||
106 | QStringLiteral("Test 3"), | ||||
107 | QStringLiteral("Test 4"), | ||||
108 | QStringLiteral("Test 5") | ||||
109 | }); | ||||
110 | | ||||
111 | QCOMPARE(spy.count(), 2); | ||||
112 | QCOMPARE(model->rowCount(), 5); | ||||
113 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
114 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
115 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
116 | QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4")); | ||||
117 | QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5")); | ||||
118 | | ||||
119 | sourceModel->setHorizontalHeaderLabels({ | ||||
120 | QStringLiteral("Test 1"), | ||||
121 | QStringLiteral("Test 2"), | ||||
122 | QStringLiteral("Test 6"), | ||||
123 | QStringLiteral("Test 3"), | ||||
124 | QStringLiteral("Test 4"), | ||||
125 | QStringLiteral("Test 5") | ||||
126 | }); | ||||
127 | | ||||
128 | QCOMPARE(spy.count(), 3); | ||||
129 | QCOMPARE(model->rowCount(), 6); | ||||
130 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
131 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
132 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 6")); | ||||
133 | QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
134 | QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4")); | ||||
135 | QCOMPARE(model->data(model->index(5, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5")); | ||||
136 | } | ||||
137 | | ||||
138 | void testRemoveColumns() | ||||
139 | { | ||||
140 | auto model = new KColumnHeadersModel{}; | ||||
141 | | ||||
142 | auto sourceModel = new QStandardItemModel{}; | ||||
143 | sourceModel->setHorizontalHeaderLabels({ | ||||
144 | QStringLiteral("Test 1"), | ||||
145 | QStringLiteral("Test 2"), | ||||
146 | QStringLiteral("Test 3"), | ||||
147 | QStringLiteral("Test 4"), | ||||
148 | QStringLiteral("Test 5") | ||||
149 | }); | ||||
150 | | ||||
151 | model->setSourceModel(sourceModel); | ||||
152 | | ||||
153 | auto tester = new QAbstractItemModelTester(model); | ||||
154 | Q_UNUSED(tester); | ||||
155 | | ||||
156 | QSignalSpy spy{model, &QAbstractItemModel::rowsRemoved}; | ||||
157 | QVERIFY(spy.isValid()); | ||||
158 | | ||||
159 | QCOMPARE(model->rowCount(), 5); | ||||
160 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
161 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
162 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
163 | QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4")); | ||||
164 | QCOMPARE(model->data(model->index(4, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 5")); | ||||
165 | | ||||
166 | sourceModel->takeColumn(4); | ||||
167 | | ||||
168 | QCOMPARE(spy.count(), 1); | ||||
169 | | ||||
170 | QCOMPARE(model->rowCount(), 4); | ||||
171 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
172 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 2")); | ||||
173 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
174 | QCOMPARE(model->data(model->index(3, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4")); | ||||
175 | | ||||
176 | sourceModel->takeColumn(1); | ||||
177 | | ||||
178 | QCOMPARE(spy.count(), 2); | ||||
179 | | ||||
180 | QCOMPARE(model->rowCount(), 3); | ||||
181 | QCOMPARE(model->data(model->index(0, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 1")); | ||||
182 | QCOMPARE(model->data(model->index(1, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 3")); | ||||
183 | QCOMPARE(model->data(model->index(2, 0), Qt::DisplayRole).toString(), QStringLiteral("Test 4")); | ||||
184 | } | ||||
185 | }; | ||||
186 | | ||||
187 | QTEST_MAIN(KColumnHeadersModelTest) | ||||
188 | | ||||
189 | #include "kcolumnheadersmodeltest.moc" |