on ab1237b help sign in
unity8/tests/plugins/Utils/qlimitproxymodeltest.cpp
471 lines 14 kB history raw
1/*
2 * Copyright (C) 2011, 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17// local
18#include "qlimitproxymodelqml.h"
19#include "modeltest.h"
21// Qt
22#include <QTest>
23#include <QSignalSpy>
24#include <QModelIndex>
25#include <QAbstractListModel>
26#include <QDebug>
29class MockListModel : public QAbstractListModel
31 Q_OBJECT
33public:
34 MockListModel(QObject* parent = 0)
35 : QAbstractListModel(parent)
36 {
37 }
39 int rowCount(const QModelIndex& /* parent */ = QModelIndex()) const
40 {
41 return m_list.size();
42 }
44 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
45 {
46 if (!index.isValid() || index.row() < 0 || index.row() >= m_list.size() || role != Qt::DisplayRole) {
47 return QVariant();
48 }
49 return QVariant(m_list[index.row()]);
50 }
52 QHash<int, QByteArray> roleNames() const
53 {
54 return m_roles;
55 }
57 void setRoles(const QHash<int,QByteArray> &roles) {
58 m_roles = roles;
59 }
61 bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) {
62 beginInsertRows(parent, row, row+count-1);
63 for (int i=0; i<count; i++) {
64 m_list.insert(i+row, "test"+i);
65 }
66 endInsertRows();
67 return true;
68 }
70 bool appendRows(QStringList &rows, const QModelIndex &parent=QModelIndex()) {
71 beginInsertRows(parent, rowCount(), rowCount() + rows.count() - 1);
72 m_list.append(rows);
73 endInsertRows();
74 return true;
75 }
77 bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) {
78 beginRemoveRows(parent, row, row+count-1);
79 for (int i=0; i<count; i++) {
80 m_list.removeAt(row);
81 }
82 endRemoveRows();
83 return true;
84 }
86private:
87 QStringList m_list;
88 QHash<int, QByteArray> m_roles;
89};
91class QLimitProxyModelTest : public QObject
93 Q_OBJECT
95private Q_SLOTS:
97 void initTestCase() {
98 qRegisterMetaType<QModelIndex>("QModelIndex");
99 }
101 void testRoleNamesSetAfter()
102 {
103 QLimitProxyModelQML proxy;
104 MockListModel model;
105 QHash<int, QByteArray> roles;
107 proxy.setModel(&model);
109 roles[0] = "role0";
110 roles[1] = "role1";
111 model.setRoles(roles);
112 QCOMPARE(model.roleNames(), proxy.roleNames());
113 }
115 void testRoleNamesSetBefore()
116 {
117 QLimitProxyModelQML proxy;
118 MockListModel model;
119 QHash<int, QByteArray> roles;
121 roles[0] = "role0";
122 roles[1] = "role1";
123 model.setRoles(roles);
125 proxy.setModel(&model);
126 QCOMPARE(model.roleNames(), proxy.roleNames());
127 }
129 void testCountSetAfter()
130 {
131 QLimitProxyModelQML proxy;
132 MockListModel model;
133 model.insertRows(0, 5);
135 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
137 proxy.setModel(&model);
138 QCOMPARE(proxy.rowCount(), 5);
139 QVERIFY(spyOnCountChanged.count() >= 1);
140 }
142 void testCountInsert()
143 {
144 QLimitProxyModelQML proxy;
145 MockListModel model;
147 proxy.setModel(&model);
149 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
151 model.insertRows(0, 5);
152 QCOMPARE(proxy.rowCount(), 5);
153 QCOMPARE(spyOnCountChanged.count(), 1);
154 }
156 void testCountRemove()
157 {
158 QLimitProxyModelQML proxy;
159 MockListModel model;
160 model.insertRows(0, 5);
162 proxy.setModel(&model);
164 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
166 model.removeRows(0, 3);
167 QCOMPARE(proxy.rowCount(), 2);
168 QCOMPARE(spyOnCountChanged.count(), 1);
169 }
171 void testLimitCount()
172 {
173 QLimitProxyModelQML proxy;
174 MockListModel model;
176 proxy.setModel(&model);
177 proxy.setLimit(3);
179 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
181 model.insertRows(0, 5);
182 QCOMPARE(proxy.rowCount(), 3);
183 QCOMPARE(spyOnCountChanged.count(), 1);
184 }
186 void testLimitLesserThanCount()
187 {
188 QLimitProxyModelQML proxy;
189 MockListModel model;
190 QList<QVariant> arguments;
191 model.insertRows(0, 10);
193 proxy.setModel(&model);
195 QSignalSpy spyOnRowsRemoved(&proxy, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
196 QSignalSpy spyOnRowsInserted(&proxy, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
197 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
199 proxy.setLimit(5);
200 QCOMPARE(spyOnRowsInserted.count(), 0);
201 QCOMPARE(spyOnCountChanged.count(), 1);
202 QCOMPARE(spyOnRowsRemoved.count(), 1);
203 arguments = spyOnRowsRemoved.takeFirst();
204 QCOMPARE(arguments.at(1).toInt(), 5);
205 QCOMPARE(arguments.at(2).toInt(), 9);
206 QCOMPARE(proxy.rowCount(), 5);
207 spyOnRowsRemoved.clear();
208 spyOnCountChanged.clear();
210 proxy.setLimit(7);
211 QCOMPARE(spyOnRowsInserted.count(), 1);
212 QCOMPARE(spyOnRowsRemoved.count(), 0);
213 QCOMPARE(spyOnCountChanged.count(), 1);
214 arguments = spyOnRowsInserted.takeFirst();
215 QCOMPARE(arguments.at(1).toInt(), 5);
216 QCOMPARE(arguments.at(2).toInt(), 6);
217 QCOMPARE(proxy.rowCount(), 7);
218 spyOnRowsInserted.clear();
219 spyOnCountChanged.clear();
221 proxy.setLimit(3);
222 QCOMPARE(spyOnRowsRemoved.count(), 1);
223 QCOMPARE(spyOnRowsInserted.count(), 0);
224 QCOMPARE(spyOnCountChanged.count(), 1);
225 arguments = spyOnRowsRemoved.takeFirst();
226 QCOMPARE(arguments.at(1).toInt(), 3);
227 QCOMPARE(arguments.at(2).toInt(), 6);
228 QCOMPARE(proxy.rowCount(), 3);
229 spyOnRowsRemoved.clear();
230 spyOnCountChanged.clear();
231 }
233 void testLimitGreaterThanCount()
234 {
235 QLimitProxyModelQML proxy;
236 MockListModel model;
237 QList<QVariant> arguments;
238 model.insertRows(0, 5);
240 proxy.setModel(&model);
242 QSignalSpy spyOnRowsRemoved(&proxy, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
243 QSignalSpy spyOnRowsInserted(&proxy, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
244 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
246 proxy.setLimit(7);
247 QCOMPARE(spyOnRowsRemoved.count(), 0);
248 QCOMPARE(spyOnRowsInserted.count(), 0);
249 QCOMPARE(spyOnCountChanged.count(), 0);
250 QCOMPARE(proxy.rowCount(), 5);
252 proxy.setLimit(5);
253 QCOMPARE(spyOnRowsRemoved.count(), 0);
254 QCOMPARE(spyOnRowsInserted.count(), 0);
255 QCOMPARE(spyOnCountChanged.count(), 0);
256 QCOMPARE(proxy.rowCount(), 5);
258 proxy.setLimit(3);
259 QCOMPARE(spyOnRowsInserted.count(), 0);
260 QCOMPARE(spyOnRowsRemoved.count(), 1);
261 QCOMPARE(spyOnCountChanged.count(), 1);
262 arguments = spyOnRowsRemoved.takeFirst();
263 QCOMPARE(arguments.at(1).toInt(), 3);
264 QCOMPARE(arguments.at(2).toInt(), 4);
265 QCOMPARE(proxy.rowCount(), 3);
266 spyOnRowsRemoved.clear();
267 spyOnCountChanged.clear();
269 proxy.setLimit(4);
270 QCOMPARE(spyOnRowsRemoved.count(), 0);
271 QCOMPARE(spyOnRowsInserted.count(), 1);
272 QCOMPARE(spyOnCountChanged.count(), 1);
273 arguments = spyOnRowsInserted.takeFirst();
274 QCOMPARE(arguments.at(1).toInt(), 3);
275 QCOMPARE(arguments.at(2).toInt(), 3);
276 QCOMPARE(proxy.rowCount(), 4);
277 spyOnRowsInserted.clear();
278 spyOnCountChanged.clear();
280 proxy.setLimit(7);
281 QCOMPARE(spyOnRowsRemoved.count(), 0);
282 QCOMPARE(spyOnRowsInserted.count(), 1);
283 QCOMPARE(spyOnCountChanged.count(), 1);
284 arguments = spyOnRowsInserted.takeFirst();
285 QCOMPARE(arguments.at(1).toInt(), 4);
286 QCOMPARE(arguments.at(2).toInt(), 4);
287 QCOMPARE(proxy.rowCount(), 5);
288 spyOnRowsInserted.clear();
289 spyOnCountChanged.clear();
290 }
292 void testLimitMinusOne()
293 {
294 QLimitProxyModelQML proxy;
295 MockListModel model;
296 QList<QVariant> arguments;
297 model.insertRows(0, 5);
299 proxy.setModel(&model);
301 QSignalSpy spyOnRowsRemoved(&proxy, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
302 QSignalSpy spyOnRowsInserted(&proxy, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
303 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
305 proxy.setLimit(7);
306 QCOMPARE(spyOnRowsRemoved.count(), 0);
307 QCOMPARE(spyOnRowsInserted.count(), 0);
308 QCOMPARE(spyOnCountChanged.count(), 0);
309 QCOMPARE(proxy.rowCount(), 5);
311 proxy.setLimit(-1);
312 QCOMPARE(spyOnRowsRemoved.count(), 0);
313 QCOMPARE(spyOnRowsInserted.count(), 0);
314 QCOMPARE(spyOnCountChanged.count(), 0);
315 QCOMPARE(proxy.rowCount(), 5);
317 proxy.setLimit(3);
318 QCOMPARE(spyOnRowsInserted.count(), 0);
319 QCOMPARE(spyOnRowsRemoved.count(), 1);
320 QCOMPARE(spyOnCountChanged.count(), 1);
321 arguments = spyOnRowsRemoved.takeFirst();
322 QCOMPARE(arguments.at(1).toInt(), 3);
323 QCOMPARE(arguments.at(2).toInt(), 4);
324 QCOMPARE(proxy.rowCount(), 3);
325 spyOnRowsRemoved.clear();
326 spyOnCountChanged.clear();
328 proxy.setLimit(-1);
329 QCOMPARE(spyOnRowsRemoved.count(), 0);
330 QCOMPARE(spyOnRowsInserted.count(), 1);
331 QCOMPARE(spyOnCountChanged.count(), 1);
332 arguments = spyOnRowsInserted.takeFirst();
333 QCOMPARE(arguments.at(1).toInt(), 3);
334 QCOMPARE(arguments.at(2).toInt(), 4);
335 QCOMPARE(proxy.rowCount(), 5);
336 spyOnRowsInserted.clear();
337 spyOnCountChanged.clear();
338 }
340 void testLimitInsert() {
341 QLimitProxyModelQML proxy;
342 MockListModel model;
343 QList<QVariant> arguments;
345 proxy.setModel(&model);
346 proxy.setLimit(7);
348 QSignalSpy spyOnRowsRemoved(&proxy, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
349 QSignalSpy spyOnRowsInserted(&proxy, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
350 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
352 model.insertRows(0, 5);
353 QCOMPARE(spyOnRowsRemoved.count(), 0);
354 QCOMPARE(spyOnRowsInserted.count(), 1);
355 QCOMPARE(spyOnCountChanged.count(), 1);
356 arguments = spyOnRowsInserted.takeFirst();
357 QCOMPARE(arguments.at(1).toInt(), 0);
358 QCOMPARE(arguments.at(2).toInt(), 4);
359 QCOMPARE(proxy.rowCount(), 5);
360 spyOnRowsInserted.clear();
361 spyOnCountChanged.clear();
363 model.insertRows(2, 2);
364 QCOMPARE(spyOnRowsRemoved.count(), 0);
365 QCOMPARE(spyOnRowsInserted.count(), 1);
366 QCOMPARE(spyOnCountChanged.count(), 1);
367 arguments = spyOnRowsInserted.takeFirst();
368 QCOMPARE(arguments.at(1).toInt(), 2);
369 QCOMPARE(arguments.at(2).toInt(), 3);
370 QCOMPARE(proxy.rowCount(), 7);
371 spyOnRowsInserted.clear();
372 spyOnCountChanged.clear();
374 model.insertRows(7, 3);
375 QCOMPARE(proxy.rowCount(), 7);
376 QCOMPARE(spyOnRowsRemoved.count(), 0);
377 QCOMPARE(spyOnRowsInserted.count(), 0);
378 QCOMPARE(spyOnCountChanged.count(), 0);
379 }
381 void testLimitRemove() {
382 QLimitProxyModelQML proxy;
383 MockListModel model;
384 QList<QVariant> arguments;
386 proxy.setModel(&model);
387 proxy.setLimit(7);
389 model.insertRows(0, 12);
391 QCOMPARE(proxy.rowCount(), 7);
393 QSignalSpy spyOnRowsRemoved(&proxy, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
394 QSignalSpy spyOnRowsInserted(&proxy, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
395 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
397 model.removeRows(7, 3);
398 QCOMPARE(proxy.rowCount(), 7);
399 QCOMPARE(spyOnRowsRemoved.count(), 0);
400 QCOMPARE(spyOnRowsInserted.count(), 0);
401 QCOMPARE(spyOnCountChanged.count(), 0);
403 model.removeRows(2, 2);
404 QCOMPARE(spyOnRowsRemoved.count(), 1);
405 QCOMPARE(spyOnRowsInserted.count(), 0);
406 QCOMPARE(spyOnCountChanged.count(), 1);
407 arguments = spyOnRowsRemoved.takeFirst();
408 QCOMPARE(arguments.at(1).toInt(), 2);
409 QCOMPARE(arguments.at(2).toInt(), 3);
410 QCOMPARE(proxy.rowCount(), 7);
411 spyOnRowsInserted.clear();
412 spyOnCountChanged.clear();
414 model.removeRows(0, 7);
415 QCOMPARE(spyOnRowsRemoved.count(), 1);
416 QCOMPARE(spyOnRowsInserted.count(), 0);
417 QCOMPARE(spyOnCountChanged.count(), 1);
418 arguments = spyOnRowsRemoved.takeFirst();
419 QCOMPARE(arguments.at(1).toInt(), 0);
420 QCOMPARE(arguments.at(2).toInt(), 6);
421 QCOMPARE(proxy.rowCount(), 0);
422 }
424 void testNestedProxyRoleNames() {
425 QLimitProxyModelQML proxy1, proxy2;
426 MockListModel model;
427 QHash<int, QByteArray> roles;
428 roles[0] = "role0";
429 roles[1] = "role1";
430 model.setRoles(roles);
432 proxy1.setModel(&model);
433 proxy2.setModel(&proxy1);
435 QCOMPARE(proxy2.roleNames(), model.roleNames());
436 }
438 void testModelTest() {
439 QLimitProxyModelQML proxy;
440 MockListModel model;
442 proxy.setModel(&model);
443 proxy.setLimit(7);
445 model.insertRows(0, 12);
447 ModelTest t1(&proxy);
448 }
449 void testModelChanged() {
450 QLimitProxyModelQML proxy;
451 MockListModel model, model2;
453 QSignalSpy spyOnModelChanged(&proxy, SIGNAL(modelChanged()));
455 proxy.setModel(&model);
456 QCOMPARE(spyOnModelChanged.count(), 1);
458 proxy.setModel(&model2);
459 QCOMPARE(spyOnModelChanged.count(), 2);
461 proxy.setModel(&model);
462 QCOMPARE(spyOnModelChanged.count(), 3);
464 proxy.setModel(&model);
465 QCOMPARE(spyOnModelChanged.count(), 3);
466 }
467};
469QTEST_MAIN(QLimitProxyModelTest)
471#include "qlimitproxymodeltest.moc"