~gerboland/unity/8-refactor-wm-and-test

« back to all changes in this revision

Viewing changes to tests/plugins/Utils/qsortfilterproxymodeltest.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 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Florian Boucault <florian.boucault@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
// local
 
21
#include "qsortfilterproxymodelqml.h"
 
22
#include "modeltest.h"
 
23
 
 
24
// Qt
 
25
#include <QTest>
 
26
#include <QSignalSpy>
 
27
#include <QModelIndex>
 
28
#include <QAbstractListModel>
 
29
#include <QDebug>
 
30
 
 
31
 
 
32
class MockListModel : public QAbstractListModel
 
33
{
 
34
    Q_OBJECT
 
35
 
 
36
public:
 
37
    MockListModel(QObject* parent = 0)
 
38
        : QAbstractListModel(parent)
 
39
    {
 
40
    }
 
41
 
 
42
    int rowCount(const QModelIndex& /* parent */ = QModelIndex()) const
 
43
    {
 
44
        return m_list.size();
 
45
    }
 
46
 
 
47
    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
 
48
    {
 
49
        if (!index.isValid() || index.row() < 0 || index.row() >= m_list.size() || role != Qt::DisplayRole) {
 
50
           return QVariant();
 
51
        }
 
52
        return QVariant(m_list[index.row()]);
 
53
    }
 
54
 
 
55
    QHash<int, QByteArray> roleNames() const
 
56
    {
 
57
        return m_roles;
 
58
    }
 
59
 
 
60
    void setRoles(const QHash<int,QByteArray> &roles) {
 
61
        m_roles = roles;
 
62
    }
 
63
 
 
64
    bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) {
 
65
        beginInsertRows(parent, row, row+count-1);
 
66
        for (int i=0; i<count; i++) {
 
67
            m_list.insert(i+row, "test"+i);
 
68
        }
 
69
        endInsertRows();
 
70
        return true;
 
71
    }
 
72
 
 
73
    bool appendRows(QStringList &rows, const QModelIndex &parent=QModelIndex()) {
 
74
        beginInsertRows(parent, rowCount(), rowCount() + rows.count() - 1);
 
75
        m_list.append(rows);
 
76
        endInsertRows();
 
77
        return true;
 
78
    }
 
79
 
 
80
    bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) {
 
81
        beginRemoveRows(parent, row, row+count-1);
 
82
        for (int i=0; i<count; i++) {
 
83
            m_list.removeAt(row);
 
84
        }
 
85
        endRemoveRows();
 
86
        return true;
 
87
    }
 
88
 
 
89
private:
 
90
    QStringList m_list;
 
91
    QHash<int, QByteArray> m_roles;
 
92
};
 
93
 
 
94
class QSortFilterProxyModelTest : public QObject
 
95
{
 
96
    Q_OBJECT
 
97
 
 
98
private Q_SLOTS:
 
99
 
 
100
    void initTestCase() {
 
101
        qRegisterMetaType<QModelIndex>("QModelIndex");
 
102
    }
 
103
 
 
104
    void testRoleNamesSetAfter()
 
105
    {
 
106
        QSortFilterProxyModelQML proxy;
 
107
        MockListModel model;
 
108
        QHash<int, QByteArray> roles;
 
109
 
 
110
        proxy.setModel(&model);
 
111
 
 
112
        roles[0] = "role0";
 
113
        roles[1] = "role1";
 
114
        model.setRoles(roles);
 
115
        QCOMPARE(model.roleNames(), proxy.roleNames());
 
116
    }
 
117
 
 
118
    void testRoleNamesSetBefore()
 
119
    {
 
120
        QSortFilterProxyModelQML proxy;
 
121
        MockListModel model;
 
122
        QHash<int, QByteArray> roles;
 
123
 
 
124
        roles[0] = "role0";
 
125
        roles[1] = "role1";
 
126
        model.setRoles(roles);
 
127
 
 
128
        proxy.setModel(&model);
 
129
        QCOMPARE(model.roleNames(), proxy.roleNames());
 
130
    }
 
131
 
 
132
    void testCountSetAfter()
 
133
    {
 
134
        QSortFilterProxyModelQML proxy;
 
135
        MockListModel model;
 
136
        model.insertRows(0, 5);
 
137
 
 
138
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
139
 
 
140
        proxy.setModel(&model);
 
141
        QCOMPARE(proxy.count(), 5);
 
142
        QVERIFY(spyOnCountChanged.count() >= 1);
 
143
    }
 
144
 
 
145
    void testCountInsert()
 
146
    {
 
147
        QSortFilterProxyModelQML proxy;
 
148
        MockListModel model;
 
149
 
 
150
        proxy.setModel(&model);
 
151
 
 
152
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
153
 
 
154
        model.insertRows(0, 5);
 
155
        QCOMPARE(proxy.count(), 5);
 
156
        QCOMPARE(spyOnCountChanged.count(), 1);
 
157
    }
 
158
 
 
159
    void testCountRemove()
 
160
    {
 
161
        QSortFilterProxyModelQML proxy;
 
162
        MockListModel model;
 
163
        model.insertRows(0, 5);
 
164
 
 
165
        proxy.setModel(&model);
 
166
        QCOMPARE(proxy.count(), 5);
 
167
 
 
168
        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));
 
169
 
 
170
        model.removeRows(0, 3);
 
171
        QCOMPARE(proxy.count(), 2);
 
172
        QCOMPARE(spyOnCountChanged.count(), 1);
 
173
    }
 
174
 
 
175
    void testInvertMatch() {
 
176
        QSortFilterProxyModelQML proxy;
 
177
        MockListModel model;
 
178
 
 
179
        proxy.setModel(&model);
 
180
        proxy.setDynamicSortFilter(true);
 
181
 
 
182
        QStringList rows;
 
183
        rows << "a/foobar/b" << "foobar" << "foobarbaz" << "hello";
 
184
        model.appendRows(rows);
 
185
 
 
186
        // Check that without a filterRegExp all rows are accepted regardless of invertMatch
 
187
        QCOMPARE(model.rowCount(), rows.count());
 
188
        QCOMPARE(proxy.rowCount(), rows.count());
 
189
        for (int i=0; i<rows.count(); i++) {
 
190
            QCOMPARE(proxy.index(i, 0).data().toString(), model.index(i, 0).data().toString());
 
191
        }
 
192
        proxy.setInvertMatch(true);
 
193
        QCOMPARE(model.rowCount(), rows.count());
 
194
        QCOMPARE(proxy.rowCount(), rows.count());
 
195
        for (int i=0; i<rows.count(); i++) {
 
196
            QCOMPARE(proxy.index(i, 0).data().toString(), model.index(i, 0).data().toString());
 
197
        }
 
198
 
 
199
 
 
200
        // Test non-anchored regexp with invertMatch active
 
201
        proxy.setFilterRegExp("foobar");
 
202
        QCOMPARE(proxy.rowCount(), 1);
 
203
        QCOMPARE(proxy.index(0, 0).data().toString(), rows.last());
 
204
 
 
205
        // Test anchored regexp with invertMatch active
 
206
        proxy.setFilterRegExp("^foobar$");
 
207
        QCOMPARE(proxy.rowCount(), 3);
 
208
        QCOMPARE(proxy.index(0, 0).data().toString(), rows.at(0));
 
209
        QCOMPARE(proxy.index(1, 0).data().toString(), rows.at(2));
 
210
        QCOMPARE(proxy.index(2, 0).data().toString(), rows.at(3));
 
211
 
 
212
        // Test regexp with OR and invertMatch active
 
213
        proxy.setFilterRegExp("foobar|hello");
 
214
        QCOMPARE(proxy.count(), 0);
 
215
    }
 
216
 
 
217
    void testNestedProxyRoleNames() {
 
218
        QSortFilterProxyModelQML proxy1, proxy2;
 
219
        MockListModel model;
 
220
        QHash<int, QByteArray> roles;
 
221
        roles[0] = "role0";
 
222
        roles[1] = "role1";
 
223
        model.setRoles(roles);
 
224
 
 
225
        proxy1.setModel(&model);
 
226
        proxy2.setModel(&proxy1);
 
227
 
 
228
        QCOMPARE(proxy2.roleNames(), model.roleNames());
 
229
    }
 
230
 
 
231
    void testModelTest() {
 
232
        QSortFilterProxyModelQML proxy;
 
233
        MockListModel model;
 
234
 
 
235
        proxy.setModel(&model);
 
236
        proxy.setDynamicSortFilter(true);
 
237
 
 
238
        QStringList rows;
 
239
        rows << "a/foobar/b" << "foobar" << "foobarbaz" << "hello";
 
240
        model.appendRows(rows);
 
241
 
 
242
        proxy.setInvertMatch(true);
 
243
        proxy.setFilterRegExp("^foobar$");
 
244
 
 
245
        ModelTest t1(&proxy);
 
246
    }
 
247
 
 
248
    void testModelChanged() {
 
249
        QSortFilterProxyModelQML proxy;
 
250
        MockListModel model, model2;
 
251
 
 
252
        QSignalSpy spyOnModelChanged(&proxy, SIGNAL(modelChanged()));
 
253
 
 
254
        proxy.setModel(&model);
 
255
        QCOMPARE(spyOnModelChanged.count(), 1);
 
256
 
 
257
        proxy.setModel(&model2);
 
258
        QCOMPARE(spyOnModelChanged.count(), 2);
 
259
 
 
260
        proxy.setModel(&model);
 
261
        QCOMPARE(spyOnModelChanged.count(), 3);
 
262
 
 
263
        proxy.setModel(&model);
 
264
        QCOMPARE(spyOnModelChanged.count(), 3);
 
265
    }
 
266
 
 
267
    void testData() {
 
268
        QSortFilterProxyModelQML proxy;
 
269
        MockListModel model, model2;
 
270
 
 
271
        QStringList rows;
 
272
        rows << "a" << "c" << "b";
 
273
        model.appendRows(rows);
 
274
 
 
275
        proxy.setModel(&model);
 
276
        proxy.sort(0);
 
277
 
 
278
        QCOMPARE(proxy.data(-1, Qt::DisplayRole), QVariant());
 
279
        QCOMPARE(proxy.data(3, Qt::DisplayRole), QVariant());
 
280
        QCOMPARE(proxy.data(0, Qt::DisplayRole - 1), QVariant());
 
281
        QCOMPARE(proxy.data(0, Qt::DisplayRole + 1), QVariant());
 
282
        QCOMPARE(proxy.data(0, Qt::DisplayRole), QVariant("a"));
 
283
        QCOMPARE(proxy.data(1, Qt::DisplayRole), QVariant("b"));
 
284
        QCOMPARE(proxy.data(2, Qt::DisplayRole), QVariant("c"));
 
285
    }
 
286
};
 
287
 
 
288
QTEST_MAIN(QSortFilterProxyModelTest)
 
289
 
 
290
#include "qsortfilterproxymodeltest.moc"