~mterry/+junk/u8.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2011 Canonical, Ltd.
 *
 * Authors:
 *  Florian Boucault <florian.boucault@canonical.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// local
#include "qsortfilterproxymodelqml.h"
#include "modeltest.h"

// Qt
#include <QTest>
#include <QSignalSpy>
#include <QModelIndex>
#include <QAbstractListModel>
#include <QDebug>


class MockListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    MockListModel(QObject* parent = 0)
        : QAbstractListModel(parent)
    {
    }

    int rowCount(const QModelIndex& /* parent */ = QModelIndex()) const
    {
        return m_list.size();
    }

    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
    {
        if (!index.isValid() || index.row() < 0 || index.row() >= m_list.size() || role != Qt::DisplayRole) {
           return QVariant();
        }
        return QVariant(m_list[index.row()]);
    }

    QHash<int, QByteArray> roleNames() const
    {
        return m_roles;
    }

    void setRoles(const QHash<int,QByteArray> &roles) {
        m_roles = roles;
    }

    bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) {
        beginInsertRows(parent, row, row+count-1);
        for (int i=0; i<count; i++) {
            m_list.insert(i+row, QString("test%1").arg(i));
        }
        endInsertRows();
        return true;
    }

    bool appendRows(QStringList &rows, const QModelIndex &parent=QModelIndex()) {
        beginInsertRows(parent, rowCount(), rowCount() + rows.count() - 1);
        m_list.append(rows);
        endInsertRows();
        return true;
    }

    bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) {
        beginRemoveRows(parent, row, row+count-1);
        for (int i=0; i<count; i++) {
            m_list.removeAt(row);
        }
        endRemoveRows();
        return true;
    }

private:
    QStringList m_list;
    QHash<int, QByteArray> m_roles;
};

class QSortFilterProxyModelTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:

    void initTestCase() {
        qRegisterMetaType<QModelIndex>("QModelIndex");
    }

    void testRoleNamesSetAfter()
    {
        QSortFilterProxyModelQML proxy;
        MockListModel model;
        QHash<int, QByteArray> roles;

        proxy.setModel(&model);

        roles[0] = "role0";
        roles[1] = "role1";
        model.setRoles(roles);
        QCOMPARE(model.roleNames(), proxy.roleNames());
    }

    void testRoleNamesSetBefore()
    {
        QSortFilterProxyModelQML proxy;
        MockListModel model;
        QHash<int, QByteArray> roles;

        roles[0] = "role0";
        roles[1] = "role1";
        model.setRoles(roles);

        proxy.setModel(&model);
        QCOMPARE(model.roleNames(), proxy.roleNames());
    }

    void testCountSetAfter()
    {
        QSortFilterProxyModelQML proxy;
        MockListModel model;
        model.insertRows(0, 5);

        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));

        proxy.setModel(&model);
        QCOMPARE(proxy.count(), 5);
        QVERIFY(spyOnCountChanged.count() >= 1);
    }

    void testCountInsert()
    {
        QSortFilterProxyModelQML proxy;
        MockListModel model;

        proxy.setModel(&model);

        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));

        model.insertRows(0, 5);
        QCOMPARE(proxy.count(), 5);
        QCOMPARE(spyOnCountChanged.count(), 1);
    }

    void testCountRemove()
    {
        QSortFilterProxyModelQML proxy;
        MockListModel model;
        model.insertRows(0, 5);

        proxy.setModel(&model);
        QCOMPARE(proxy.count(), 5);

        QSignalSpy spyOnCountChanged(&proxy, SIGNAL(countChanged()));

        model.removeRows(0, 3);
        QCOMPARE(proxy.count(), 2);
        QCOMPARE(spyOnCountChanged.count(), 1);
    }

    void testInvertMatch() {
        QSortFilterProxyModelQML proxy;
        MockListModel model;

        proxy.setModel(&model);
        proxy.setDynamicSortFilter(true);

        QStringList rows;
        rows << "a/foobar/b" << "foobar" << "foobarbaz" << "hello";
        model.appendRows(rows);

        // Check that without a filterRegExp all rows are accepted regardless of invertMatch
        QCOMPARE(model.rowCount(), rows.count());
        QCOMPARE(proxy.rowCount(), rows.count());
        for (int i=0; i<rows.count(); i++) {
            QCOMPARE(proxy.index(i, 0).data().toString(), model.index(i, 0).data().toString());
        }
        proxy.setInvertMatch(true);
        QCOMPARE(model.rowCount(), rows.count());
        QCOMPARE(proxy.rowCount(), rows.count());
        for (int i=0; i<rows.count(); i++) {
            QCOMPARE(proxy.index(i, 0).data().toString(), model.index(i, 0).data().toString());
        }


        // Test non-anchored regexp with invertMatch active
        proxy.setFilterRegExp("foobar");
        QCOMPARE(proxy.rowCount(), 1);
        QCOMPARE(proxy.index(0, 0).data().toString(), rows.last());

        // Test anchored regexp with invertMatch active
        proxy.setFilterRegExp("^foobar$");
        QCOMPARE(proxy.rowCount(), 3);
        QCOMPARE(proxy.index(0, 0).data().toString(), rows.at(0));
        QCOMPARE(proxy.index(1, 0).data().toString(), rows.at(2));
        QCOMPARE(proxy.index(2, 0).data().toString(), rows.at(3));

        // Test regexp with OR and invertMatch active
        proxy.setFilterRegExp("foobar|hello");
        QCOMPARE(proxy.count(), 0);
    }

    void testNestedProxyRoleNames() {
        QSortFilterProxyModelQML proxy1, proxy2;
        MockListModel model;
        QHash<int, QByteArray> roles;
        roles[0] = "role0";
        roles[1] = "role1";
        model.setRoles(roles);

        proxy1.setModel(&model);
        proxy2.setModel(&proxy1);

        QCOMPARE(proxy2.roleNames(), model.roleNames());
    }

    void testModelTest() {
        QSortFilterProxyModelQML proxy;
        MockListModel model;

        proxy.setModel(&model);
        proxy.setDynamicSortFilter(true);

        QStringList rows;
        rows << "a/foobar/b" << "foobar" << "foobarbaz" << "hello";
        model.appendRows(rows);

        proxy.setInvertMatch(true);
        proxy.setFilterRegExp("^foobar$");

        ModelTest t1(&proxy);
    }

    void testModelChanged() {
        QSortFilterProxyModelQML proxy;
        MockListModel model, model2;

        QSignalSpy spyOnModelChanged(&proxy, SIGNAL(modelChanged()));

        proxy.setModel(&model);
        QCOMPARE(spyOnModelChanged.count(), 1);

        proxy.setModel(&model2);
        QCOMPARE(spyOnModelChanged.count(), 2);

        proxy.setModel(&model);
        QCOMPARE(spyOnModelChanged.count(), 3);

        proxy.setModel(&model);
        QCOMPARE(spyOnModelChanged.count(), 3);
    }

    void testData() {
        QSortFilterProxyModelQML proxy;
        MockListModel model, model2;

        QStringList rows;
        rows << "a" << "c" << "b";
        model.appendRows(rows);

        proxy.setModel(&model);
        proxy.sort(0);

        QCOMPARE(proxy.data(-1, Qt::DisplayRole), QVariant());
        QCOMPARE(proxy.data(3, Qt::DisplayRole), QVariant());
        QCOMPARE(proxy.data(0, Qt::DisplayRole - 1), QVariant());
        QCOMPARE(proxy.data(0, Qt::DisplayRole + 1), QVariant());
        QCOMPARE(proxy.data(0, Qt::DisplayRole), QVariant("a"));
        QCOMPARE(proxy.data(1, Qt::DisplayRole), QVariant("b"));
        QCOMPARE(proxy.data(2, Qt::DisplayRole), QVariant("c"));
    }
};

QTEST_GUILESS_MAIN(QSortFilterProxyModelTest)

#include "qsortfilterproxymodeltest.moc"