2 * Copyright (C) 2011, 2013 Canonical, Ltd.
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.
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.
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/>.
18#include "qlimitproxymodelqml.h"
25#include <QAbstractListModel>
29class MockListModel : public QAbstractListModel
34 MockListModel(QObject* parent = 0)
35 : QAbstractListModel(parent)
39 int rowCount(const QModelIndex& /* parent */ = QModelIndex()) const
44 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
46 if (!index.isValid() || index.row() < 0 || index.row() >= m_list.size() || role != Qt::DisplayRole) {
49 return QVariant(m_list[index.row()]);
52 QHash<int, QByteArray> roleNames() const
57 void setRoles(const QHash<int,QByteArray> &roles) {
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);
70 bool appendRows(QStringList &rows, const QModelIndex &parent=QModelIndex()) {
71 beginInsertRows(parent, rowCount(), rowCount() + rows.count() - 1);
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++) {
88 QHash<int, QByteArray> m_roles;
91class QLimitProxyModelTest : public QObject
98 qRegisterMetaType<QModelIndex>("QModelIndex");
101 void testRoleNamesSetAfter()
103 QLimitProxyModelQML proxy;
105 QHash<int, QByteArray> roles;
107 proxy.setModel(&model);
111 model.setRoles(roles);
112 QCOMPARE(model.roleNames(), proxy.roleNames());
115 void testRoleNamesSetBefore()
117 QLimitProxyModelQML proxy;
119 QHash<int, QByteArray> roles;
123 model.setRoles(roles);
125 proxy.setModel(&model);
126 QCOMPARE(model.roleNames(), proxy.roleNames());
129 void testCountSetAfter()
131 QLimitProxyModelQML proxy;
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);
142 void testCountInsert()
144 QLimitProxyModelQML proxy;
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);
156 void testCountRemove()
158 QLimitProxyModelQML proxy;
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);
171 void testLimitCount()
173 QLimitProxyModelQML proxy;
176 proxy.setModel(&model);
179 QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
181 model.insertRows(0, 5);
182 QCOMPARE(proxy.rowCount(), 3);
183 QCOMPARE(spyOnCountChanged.count(), 1);
186 void testLimitLesserThanCount()
188 QLimitProxyModelQML proxy;
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()));
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();
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();
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();
233 void testLimitGreaterThanCount()
235 QLimitProxyModelQML proxy;
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()));
247 QCOMPARE(spyOnRowsRemoved.count(), 0);
248 QCOMPARE(spyOnRowsInserted.count(), 0);
249 QCOMPARE(spyOnCountChanged.count(), 0);
250 QCOMPARE(proxy.rowCount(), 5);
253 QCOMPARE(spyOnRowsRemoved.count(), 0);
254 QCOMPARE(spyOnRowsInserted.count(), 0);
255 QCOMPARE(spyOnCountChanged.count(), 0);
256 QCOMPARE(proxy.rowCount(), 5);
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();
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();
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();
292 void testLimitMinusOne()
294 QLimitProxyModelQML proxy;
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()));
306 QCOMPARE(spyOnRowsRemoved.count(), 0);
307 QCOMPARE(spyOnRowsInserted.count(), 0);
308 QCOMPARE(spyOnCountChanged.count(), 0);
309 QCOMPARE(proxy.rowCount(), 5);
312 QCOMPARE(spyOnRowsRemoved.count(), 0);
313 QCOMPARE(spyOnRowsInserted.count(), 0);
314 QCOMPARE(spyOnCountChanged.count(), 0);
315 QCOMPARE(proxy.rowCount(), 5);
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();
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();
340 void testLimitInsert() {
341 QLimitProxyModelQML proxy;
343 QList<QVariant> arguments;
345 proxy.setModel(&model);
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);
381 void testLimitRemove() {
382 QLimitProxyModelQML proxy;
384 QList<QVariant> arguments;
386 proxy.setModel(&model);
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);
424 void testNestedProxyRoleNames() {
425 QLimitProxyModelQML proxy1, proxy2;
427 QHash<int, QByteArray> roles;
430 model.setRoles(roles);
432 proxy1.setModel(&model);
433 proxy2.setModel(&proxy1);
435 QCOMPARE(proxy2.roleNames(), model.roleNames());
438 void testModelTest() {
439 QLimitProxyModelQML proxy;
442 proxy.setModel(&model);
445 model.insertRows(0, 12);
447 ModelTest t1(&proxy);
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);
469QTEST_MAIN(QLimitProxyModelTest)
471#include "qlimitproxymodeltest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list