~unity-team/unity8/trunk

« back to all changes in this revision

Viewing changes to tests/plugins/Utils/qlimitproxymodeltest.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
16
 
 
17
// local
 
18
#include "qlimitproxymodelqml.h"
 
19
#include "modeltest.h"
 
20
 
 
21
// Qt
 
22
#include <QTest>
 
23
#include <QSignalSpy>
 
24
#include <QModelIndex>
 
25
#include <QAbstractListModel>
 
26
#include <QDebug>
 
27
 
 
28
 
 
29
class MockListModel : public QAbstractListModel
 
30
{
 
31
    Q_OBJECT
 
32
 
 
33
public:
 
34
    MockListModel(QObject* parent = 0)
 
35
        : QAbstractListModel(parent)
 
36
    {
 
37
    }
 
38
 
 
39
    int rowCount(const QModelIndex& /* parent */ = QModelIndex()) const
 
40
    {
 
41
        return m_list.size();
 
42
    }
 
43
 
 
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
    }
 
51
 
 
52
    QHash<int, QByteArray> roleNames() const
 
53
    {
 
54
        return m_roles;
 
55
    }
 
56
 
 
57
    void setRoles(const QHash<int,QByteArray> &roles) {
 
58
        m_roles = roles;
 
59
    }
 
60
 
 
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
    }
 
69
 
 
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
    }
 
76
 
 
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
    }
 
85
 
 
86
private:
 
87
    QStringList m_list;
 
88
    QHash<int, QByteArray> m_roles;
 
89
};
 
90
 
 
91
class QLimitProxyModelTest : public QObject
 
92
{
 
93
    Q_OBJECT
 
94
 
 
95
private Q_SLOTS:
 
96
 
 
97
    void initTestCase() {
 
98
        qRegisterMetaType<QModelIndex>("QModelIndex");
 
99
    }
 
100
 
 
101
    void testRoleNamesSetAfter()
 
102
    {
 
103
        QLimitProxyModelQML proxy;
 
104
        MockListModel model;
 
105
        QHash<int, QByteArray> roles;
 
106
 
 
107
        proxy.setModel(&model);
 
108
 
 
109
        roles[0] = "role0";
 
110
        roles[1] = "role1";
 
111
        model.setRoles(roles);
 
112
        QCOMPARE(model.roleNames(), proxy.roleNames());
 
113
    }
 
114
 
 
115
    void testRoleNamesSetBefore()
 
116
    {
 
117
        QLimitProxyModelQML proxy;
 
118
        MockListModel model;
 
119
        QHash<int, QByteArray> roles;
 
120
 
 
121
        roles[0] = "role0";
 
122
        roles[1] = "role1";
 
123
        model.setRoles(roles);
 
124
 
 
125
        proxy.setModel(&model);
 
126
        QCOMPARE(model.roleNames(), proxy.roleNames());
 
127
    }
 
128
 
 
129
    void testCountSetAfter()
 
130
    {
 
131
        QLimitProxyModelQML proxy;
 
132
        MockListModel model;
 
133
        model.insertRows(0, 5);
 
134
 
 
135
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
136
 
 
137
        proxy.setModel(&model);
 
138
        QCOMPARE(proxy.rowCount(), 5);
 
139
        QVERIFY(spyOnCountChanged.count() >= 1);
 
140
    }
 
141
 
 
142
    void testCountInsert()
 
143
    {
 
144
        QLimitProxyModelQML proxy;
 
145
        MockListModel model;
 
146
 
 
147
        proxy.setModel(&model);
 
148
 
 
149
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
150
 
 
151
        model.insertRows(0, 5);
 
152
        QCOMPARE(proxy.rowCount(), 5);
 
153
        QCOMPARE(spyOnCountChanged.count(), 1);
 
154
    }
 
155
 
 
156
    void testCountRemove()
 
157
    {
 
158
        QLimitProxyModelQML proxy;
 
159
        MockListModel model;
 
160
        model.insertRows(0, 5);
 
161
 
 
162
        proxy.setModel(&model);
 
163
 
 
164
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
165
 
 
166
        model.removeRows(0, 3);
 
167
        QCOMPARE(proxy.rowCount(), 2);
 
168
        QCOMPARE(spyOnCountChanged.count(), 1);
 
169
    }
 
170
 
 
171
    void testLimitCount()
 
172
    {
 
173
        QLimitProxyModelQML proxy;
 
174
        MockListModel model;
 
175
 
 
176
        proxy.setModel(&model);
 
177
        proxy.setLimit(3);
 
178
 
 
179
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
180
 
 
181
        model.insertRows(0, 5);
 
182
        QCOMPARE(proxy.rowCount(), 3);
 
183
        QCOMPARE(spyOnCountChanged.count(), 1);
 
184
    }
 
185
 
 
186
    void testLimitLesserThanCount()
 
187
    {
 
188
        QLimitProxyModelQML proxy;
 
189
        MockListModel model;
 
190
        QList<QVariant> arguments;
 
191
        model.insertRows(0, 10);
 
192
 
 
193
        proxy.setModel(&model);
 
194
 
 
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()));
 
198
 
 
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();
 
209
 
 
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();
 
220
 
 
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
    }
 
232
 
 
233
    void testLimitGreaterThanCount()
 
234
    {
 
235
        QLimitProxyModelQML proxy;
 
236
        MockListModel model;
 
237
        QList<QVariant> arguments;
 
238
        model.insertRows(0, 5);
 
239
 
 
240
        proxy.setModel(&model);
 
241
 
 
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()));
 
245
 
 
246
        proxy.setLimit(7);
 
247
        QCOMPARE(spyOnRowsRemoved.count(), 0);
 
248
        QCOMPARE(spyOnRowsInserted.count(), 0);
 
249
        QCOMPARE(spyOnCountChanged.count(), 0);
 
250
        QCOMPARE(proxy.rowCount(), 5);
 
251
 
 
252
        proxy.setLimit(5);
 
253
        QCOMPARE(spyOnRowsRemoved.count(), 0);
 
254
        QCOMPARE(spyOnRowsInserted.count(), 0);
 
255
        QCOMPARE(spyOnCountChanged.count(), 0);
 
256
        QCOMPARE(proxy.rowCount(), 5);
 
257
 
 
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();
 
268
 
 
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();
 
279
 
 
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
    }
 
291
 
 
292
    void testLimitMinusOne()
 
293
    {
 
294
        QLimitProxyModelQML proxy;
 
295
        MockListModel model;
 
296
        QList<QVariant> arguments;
 
297
        model.insertRows(0, 5);
 
298
 
 
299
        proxy.setModel(&model);
 
300
 
 
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()));
 
304
 
 
305
        proxy.setLimit(7);
 
306
        QCOMPARE(spyOnRowsRemoved.count(), 0);
 
307
        QCOMPARE(spyOnRowsInserted.count(), 0);
 
308
        QCOMPARE(spyOnCountChanged.count(), 0);
 
309
        QCOMPARE(proxy.rowCount(), 5);
 
310
 
 
311
        proxy.setLimit(-1);
 
312
        QCOMPARE(spyOnRowsRemoved.count(), 0);
 
313
        QCOMPARE(spyOnRowsInserted.count(), 0);
 
314
        QCOMPARE(spyOnCountChanged.count(), 0);
 
315
        QCOMPARE(proxy.rowCount(), 5);
 
316
 
 
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();
 
327
 
 
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
    }
 
339
 
 
340
    void testLimitInsert() {
 
341
        QLimitProxyModelQML proxy;
 
342
        MockListModel model;
 
343
        QList<QVariant> arguments;
 
344
 
 
345
        proxy.setModel(&model);
 
346
        proxy.setLimit(7);
 
347
 
 
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()));
 
351
 
 
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();
 
362
 
 
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();
 
373
 
 
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
    }
 
380
 
 
381
    void testLimitRemove() {
 
382
        QLimitProxyModelQML proxy;
 
383
        MockListModel model;
 
384
        QList<QVariant> arguments;
 
385
 
 
386
        proxy.setModel(&model);
 
387
        proxy.setLimit(7);
 
388
 
 
389
        model.insertRows(0, 12);
 
390
 
 
391
        QCOMPARE(proxy.rowCount(), 7);
 
392
 
 
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()));
 
396
 
 
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);
 
402
 
 
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();
 
413
 
 
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
    }
 
423
 
 
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);
 
431
 
 
432
        proxy1.setModel(&model);
 
433
        proxy2.setModel(&proxy1);
 
434
 
 
435
        QCOMPARE(proxy2.roleNames(), model.roleNames());
 
436
    }
 
437
 
 
438
    void testModelTest() {
 
439
        QLimitProxyModelQML proxy;
 
440
        MockListModel model;
 
441
 
 
442
        proxy.setModel(&model);
 
443
        proxy.setLimit(7);
 
444
 
 
445
        model.insertRows(0, 12);
 
446
 
 
447
        ModelTest t1(&proxy);
 
448
    }
 
449
    void testModelChanged() {
 
450
        QLimitProxyModelQML proxy;
 
451
        MockListModel model, model2;
 
452
 
 
453
        QSignalSpy spyOnModelChanged(&proxy, SIGNAL(modelChanged()));
 
454
 
 
455
        proxy.setModel(&model);
 
456
        QCOMPARE(spyOnModelChanged.count(), 1);
 
457
 
 
458
        proxy.setModel(&model2);
 
459
        QCOMPARE(spyOnModelChanged.count(), 2);
 
460
 
 
461
        proxy.setModel(&model);
 
462
        QCOMPARE(spyOnModelChanged.count(), 3);
 
463
 
 
464
        proxy.setModel(&model);
 
465
        QCOMPARE(spyOnModelChanged.count(), 3);
 
466
    }
 
467
};
 
468
 
 
469
QTEST_MAIN(QLimitProxyModelTest)
 
470
 
 
471
#include "qlimitproxymodeltest.moc"