File: kconcatenaterowsproxymodeltest.cpp

package info (click to toggle)
kitemmodels 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,844 kB
  • sloc: cpp: 18,018; python: 26; sh: 13; makefile: 7
file content (427 lines) | stat: -rw-r--r-- 17,494 bytes parent folder | download | duplicates (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <QIdentityProxyModel>
#include <QItemSelectionModel>
#include <QSignalSpy>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QTest>

#include "test_model_helpers.h"
#include <kconcatenaterowsproxymodel.h>
using namespace TestModelHelpers;

Q_DECLARE_METATYPE(QModelIndex)

class tst_KConcatenateRowsProxyModel : public QObject
{
    Q_OBJECT

private Q_SLOTS:

    void initTestCase()
    {
    }

    void init()
    {
        // Prepare some source models to use later on
        mod.clear();
        mod.insertRow(0, makeStandardItems(QStringList() << QStringLiteral("A") << QStringLiteral("B") << QStringLiteral("C")));
        mod.setHorizontalHeaderLabels(QStringList() << QStringLiteral("H1") << QStringLiteral("H2") << QStringLiteral("H3"));
        mod.setVerticalHeaderLabels(QStringList() << QStringLiteral("One"));

        mod2.clear();
        mod2.insertRow(0, makeStandardItems(QStringList() << QStringLiteral("D") << QStringLiteral("E") << QStringLiteral("F")));
        mod2.setHorizontalHeaderLabels(QStringList() << QStringLiteral("H1") << QStringLiteral("H2") << QStringLiteral("H3"));
        mod2.setVerticalHeaderLabels(QStringList() << QStringLiteral("Two"));
    }

    void shouldAggregateTwoModelsCorrectly()
    {
        // Given a combining proxy
        KConcatenateRowsProxyModel pm;

        // When adding two source models
        pm.addSourceModel(&mod);
        pm.addSourceModel(&mod2);

        // Then the proxy should show 2 rows
        QCOMPARE(pm.rowCount(), 2);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF"));

        // ... and correct headers
        QCOMPARE(pm.headerData(0, Qt::Horizontal).toString(), QStringLiteral("H1"));
        QCOMPARE(pm.headerData(1, Qt::Horizontal).toString(), QStringLiteral("H2"));
        QCOMPARE(pm.headerData(2, Qt::Horizontal).toString(), QStringLiteral("H3"));
        QCOMPARE(pm.headerData(0, Qt::Vertical).toString(), QStringLiteral("One"));
        QCOMPARE(pm.headerData(1, Qt::Vertical).toString(), QStringLiteral("Two"));

        QVERIFY(!pm.canFetchMore(QModelIndex()));
    }

    void shouldAggregateThenRemoveTwoEmptyModelsCorrectly()
    {
        // Given a combining proxy
        KConcatenateRowsProxyModel pm;

        // When adding two empty models
        QSignalSpy rowATBISpy(&pm, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
        QSignalSpy rowInsertedSpy(&pm, SIGNAL(rowsInserted(QModelIndex, int, int)));
        QSignalSpy rowATBRSpy(&pm, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
        QSignalSpy rowRemovedSpy(&pm, SIGNAL(rowsRemoved(QModelIndex, int, int)));
        QIdentityProxyModel i1;
        QIdentityProxyModel i2;
        pm.addSourceModel(&i1);
        pm.addSourceModel(&i2);

        // Then the proxy should still be empty (and no signals emitted)
        QCOMPARE(pm.rowCount(), 0);
        QCOMPARE(pm.columnCount(), 0);
        QCOMPARE(rowATBISpy.count(), 0);
        QCOMPARE(rowInsertedSpy.count(), 0);

        // When removing the empty models
        pm.removeSourceModel(&i1);
        pm.removeSourceModel(&i2);

        // Then the proxy should still be empty (and no signals emitted)
        QCOMPARE(pm.rowCount(), 0);
        QCOMPARE(pm.columnCount(), 0);
        QCOMPARE(rowATBRSpy.count(), 0);
        QCOMPARE(rowRemovedSpy.count(), 0);
    }

    void shouldAggregateTwoEmptyModelsWhichThenGetFilled()
    {
        // Given a combining proxy
        KConcatenateRowsProxyModel pm;

        // When adding two empty models
        QIdentityProxyModel i1;
        QIdentityProxyModel i2;
        pm.addSourceModel(&i1);
        pm.addSourceModel(&i2);

        QCOMPARE(pm.rowCount(), 0);
        QCOMPARE(pm.columnCount(), 0);

        i1.setSourceModel(&mod);
        i2.setSourceModel(&mod2);

        // Then the proxy should show 2 rows
        QCOMPARE(pm.rowCount(), 2);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF"));

        // ... and correct headers
        QCOMPARE(pm.headerData(0, Qt::Horizontal).toString(), QStringLiteral("H1"));
        QCOMPARE(pm.headerData(1, Qt::Horizontal).toString(), QStringLiteral("H2"));
        QCOMPARE(pm.headerData(2, Qt::Horizontal).toString(), QStringLiteral("H3"));
        QCOMPARE(pm.headerData(0, Qt::Vertical).toString(), QStringLiteral("One"));
        QCOMPARE(pm.headerData(1, Qt::Vertical).toString(), QStringLiteral("Two"));

        QVERIFY(!pm.canFetchMore(QModelIndex()));
    }

    void shouldHandleDataChanged()
    {
        // Given two models combined
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);
        pm.addSourceModel(&mod2);
        QSignalSpy dataChangedSpy(&pm, SIGNAL(dataChanged(QModelIndex, QModelIndex)));

        // When a cell in a source model changes
        mod.item(0, 0)->setData("a", Qt::EditRole);

        // Then the change should be notified to the proxy
        QCOMPARE(dataChangedSpy.count(), 1);
        QCOMPARE(dataChangedSpy.at(0).at(0).toModelIndex(), pm.index(0, 0));
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("aBC"));

        // Same test with the other model
        mod2.item(0, 2)->setData("f", Qt::EditRole);

        QCOMPARE(dataChangedSpy.count(), 2);
        QCOMPARE(dataChangedSpy.at(1).at(0).toModelIndex(), pm.index(1, 2));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEf"));
    }

    void shouldHandleSetData()
    {
        // Given two models combined
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);
        pm.addSourceModel(&mod2);
        QSignalSpy dataChangedSpy(&pm, SIGNAL(dataChanged(QModelIndex, QModelIndex)));

        // When changing a cell using setData
        pm.setData(pm.index(0, 0), "a", Qt::EditRole);

        // Then the change should be notified to the proxy
        QCOMPARE(dataChangedSpy.count(), 1);
        QCOMPARE(dataChangedSpy.at(0).at(0).toModelIndex(), pm.index(0, 0));
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("aBC"));

        // Same test with the other model
        pm.setData(pm.index(1, 2), "f", Qt::EditRole);

        QCOMPARE(dataChangedSpy.count(), 2);
        QCOMPARE(dataChangedSpy.at(1).at(0).toModelIndex(), pm.index(1, 2));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEf"));
    }

    void shouldHandleRowInsertionAndRemoval()
    {
        // Given two models combined
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);
        pm.addSourceModel(&mod2);
        QSignalSpy rowATBISpy(&pm, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
        QSignalSpy rowInsertedSpy(&pm, SIGNAL(rowsInserted(QModelIndex, int, int)));

        // When a source model inserts a new row
        QList<QStandardItem *> row;
        row.append(new QStandardItem(QStringLiteral("1")));
        row.append(new QStandardItem(QStringLiteral("2")));
        row.append(new QStandardItem(QStringLiteral("3")));
        mod2.insertRow(0, row);

        // Then the proxy should notify its users and show changes
        QCOMPARE(rowSpyToText(rowATBISpy), QStringLiteral("1,1"));
        QCOMPARE(rowSpyToText(rowInsertedSpy), QStringLiteral("1,1"));
        QCOMPARE(pm.rowCount(), 3);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("123"));
        QCOMPARE(extractRowTexts(&pm, 2), QStringLiteral("DEF"));

        // When removing that row
        QSignalSpy rowATBRSpy(&pm, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
        QSignalSpy rowRemovedSpy(&pm, SIGNAL(rowsRemoved(QModelIndex, int, int)));
        mod2.removeRow(0);

        // Then the proxy should notify its users and show changes
        QCOMPARE(rowATBRSpy.count(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(1).toInt(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(2).toInt(), 1);
        QCOMPARE(rowRemovedSpy.count(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(1).toInt(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(2).toInt(), 1);
        QCOMPARE(pm.rowCount(), 2);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF"));

        // When removing the last row from mod2
        rowATBRSpy.clear();
        rowRemovedSpy.clear();
        mod2.removeRow(0);

        // Then the proxy should notify its users and show changes
        QCOMPARE(rowATBRSpy.count(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(1).toInt(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(2).toInt(), 1);
        QCOMPARE(rowRemovedSpy.count(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(1).toInt(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(2).toInt(), 1);
        QCOMPARE(pm.rowCount(), 1);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
    }

    void shouldAggregateAnotherModelThenRemoveModels()
    {
        // Given two models combined, and a third model
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);
        pm.addSourceModel(&mod2);

        QStandardItemModel mod3;
        mod3.appendRow(makeStandardItems(QStringList() << QStringLiteral("1") << QStringLiteral("2") << QStringLiteral("3")));
        mod3.appendRow(makeStandardItems(QStringList() << QStringLiteral("4") << QStringLiteral("5") << QStringLiteral("6")));

        QSignalSpy rowATBISpy(&pm, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
        QSignalSpy rowInsertedSpy(&pm, SIGNAL(rowsInserted(QModelIndex, int, int)));

        // When adding the new source model
        pm.addSourceModel(&mod3);

        // Then the proxy should notify its users about the two rows inserted
        QCOMPARE(rowSpyToText(rowATBISpy), QStringLiteral("2,3"));
        QCOMPARE(rowSpyToText(rowInsertedSpy), QStringLiteral("2,3"));
        QCOMPARE(pm.rowCount(), 4);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF"));
        QCOMPARE(extractRowTexts(&pm, 2), QStringLiteral("123"));
        QCOMPARE(extractRowTexts(&pm, 3), QStringLiteral("456"));

        // When removing that source model again
        QSignalSpy rowATBRSpy(&pm, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
        QSignalSpy rowRemovedSpy(&pm, SIGNAL(rowsRemoved(QModelIndex, int, int)));
        pm.removeSourceModel(&mod3);

        // Then the proxy should notify its users about the row removed
        QCOMPARE(rowATBRSpy.count(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(1).toInt(), 2);
        QCOMPARE(rowATBRSpy.at(0).at(2).toInt(), 3);
        QCOMPARE(rowRemovedSpy.count(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(1).toInt(), 2);
        QCOMPARE(rowRemovedSpy.at(0).at(2).toInt(), 3);
        QCOMPARE(pm.rowCount(), 2);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF"));

        // When removing model 2
        rowATBRSpy.clear();
        rowRemovedSpy.clear();
        pm.removeSourceModel(&mod2);
        QCOMPARE(rowATBRSpy.count(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(1).toInt(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(2).toInt(), 1);
        QCOMPARE(rowRemovedSpy.count(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(1).toInt(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(2).toInt(), 1);
        QCOMPARE(pm.rowCount(), 1);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));

        // When removing model 1
        rowATBRSpy.clear();
        rowRemovedSpy.clear();
        pm.removeSourceModel(&mod);
        QCOMPARE(rowATBRSpy.count(), 1);
        QCOMPARE(rowATBRSpy.at(0).at(1).toInt(), 0);
        QCOMPARE(rowATBRSpy.at(0).at(2).toInt(), 0);
        QCOMPARE(rowRemovedSpy.count(), 1);
        QCOMPARE(rowRemovedSpy.at(0).at(1).toInt(), 0);
        QCOMPARE(rowRemovedSpy.at(0).at(2).toInt(), 0);
        QCOMPARE(pm.rowCount(), 0);
    }

    void shouldHandleColumnInsertionAndRemoval()
    {
        // Given two models combined
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);
        pm.addSourceModel(&mod2);
        QSignalSpy colATBISpy(&pm, SIGNAL(columnsAboutToBeInserted(QModelIndex, int, int)));
        QSignalSpy colInsertedSpy(&pm, SIGNAL(columnsInserted(QModelIndex, int, int)));

        // When the first source model inserts a new column
        QCOMPARE(mod.columnCount(), 3);
        mod.setColumnCount(4);

        // Then the proxy should notify its users and show changes
        QCOMPARE(rowSpyToText(colATBISpy), QStringLiteral("3,3"));
        QCOMPARE(rowSpyToText(colInsertedSpy), QStringLiteral("3,3"));
        QCOMPARE(pm.rowCount(), 2);
        QCOMPARE(pm.columnCount(), 4);
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC "));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF "));
    }

    void shouldPropagateLayoutChanged()
    {
        // Given two source models, the second one being a QSFPM
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);

        QStandardItemModel mod3;
        QList<QStandardItem *> row;
        row.append(new QStandardItem(QStringLiteral("1")));
        row.append(new QStandardItem(QStringLiteral("2")));
        row.append(new QStandardItem(QStringLiteral("3")));
        mod3.insertRow(0, row);
        row.clear();
        row.append(new QStandardItem(QStringLiteral("4")));
        row.append(new QStandardItem(QStringLiteral("5")));
        row.append(new QStandardItem(QStringLiteral("6")));
        mod3.appendRow(row);

        QSortFilterProxyModel qsfpm;
        qsfpm.setSourceModel(&mod3);
        pm.addSourceModel(&qsfpm);

        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("123"));
        QCOMPARE(extractRowTexts(&pm, 2), QStringLiteral("456"));

        // And a selection (row 1)
        QItemSelectionModel selection(&pm);
        selection.select(pm.index(1, 0), QItemSelectionModel::Select | QItemSelectionModel::Rows);
        const QModelIndexList lst = selection.selectedIndexes();
        QCOMPARE(lst.count(), 3);
        for (int col = 0; col < lst.count(); ++col) {
            QCOMPARE(lst.at(col).row(), 1);
            QCOMPARE(lst.at(col).column(), col);
        }

        QSignalSpy layoutATBCSpy(&pm, SIGNAL(layoutAboutToBeChanged()));
        QSignalSpy layoutChangedSpy(&pm, SIGNAL(layoutChanged()));

        // When changing the sorting in the QSFPM
        qsfpm.sort(0, Qt::DescendingOrder);

        // Then the proxy should emit the layoutChanged signals, and show re-sorted data
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("456"));
        QCOMPARE(extractRowTexts(&pm, 2), QStringLiteral("123"));
        QCOMPARE(layoutATBCSpy.count(), 1);
        QCOMPARE(layoutChangedSpy.count(), 1);

        // And the selection should be updated accordingly (it became row 2)
        const QModelIndexList lstAfter = selection.selectedIndexes();
        QCOMPARE(lstAfter.count(), 3);
        for (int col = 0; col < lstAfter.count(); ++col) {
            QCOMPARE(lstAfter.at(col).row(), 2);
            QCOMPARE(lstAfter.at(col).column(), col);
        }
    }

    void shouldReactToModelReset()
    {
        // Given two source models, the second one being a QSFPM
        KConcatenateRowsProxyModel pm;
        pm.addSourceModel(&mod);

        QStandardItemModel mod3;
        QList<QStandardItem *> row;
        row.append(new QStandardItem(QStringLiteral("1")));
        row.append(new QStandardItem(QStringLiteral("2")));
        row.append(new QStandardItem(QStringLiteral("3")));
        mod3.insertRow(0, row);
        row.clear();
        row.append(new QStandardItem(QStringLiteral("4")));
        row.append(new QStandardItem(QStringLiteral("5")));
        row.append(new QStandardItem(QStringLiteral("6")));
        mod3.appendRow(row);

        QSortFilterProxyModel qsfpm;
        qsfpm.setSourceModel(&mod3);
        pm.addSourceModel(&qsfpm);

        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("123"));
        QCOMPARE(extractRowTexts(&pm, 2), QStringLiteral("456"));
        QSignalSpy rowATBRSpy(&pm, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)));
        QSignalSpy rowRemovedSpy(&pm, SIGNAL(rowsRemoved(QModelIndex, int, int)));
        QSignalSpy rowATBISpy(&pm, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)));
        QSignalSpy rowInsertedSpy(&pm, SIGNAL(rowsInserted(QModelIndex, int, int)));

        // When changing the source model of the QSFPM
        qsfpm.setSourceModel(&mod2);

        // Then the proxy should emit the row removed/inserted signals, and show the new data
        QCOMPARE(extractRowTexts(&pm, 0), QStringLiteral("ABC"));
        QCOMPARE(extractRowTexts(&pm, 1), QStringLiteral("DEF"));
        QCOMPARE(rowSpyToText(rowATBRSpy), QStringLiteral("1,2"));
        QCOMPARE(rowSpyToText(rowRemovedSpy), QStringLiteral("1,2"));
        QCOMPARE(rowSpyToText(rowATBISpy), QStringLiteral("1,1"));
        QCOMPARE(rowSpyToText(rowInsertedSpy), QStringLiteral("1,1"));
    }

private:
    QStandardItemModel mod;
    QStandardItemModel mod2;
};

QTEST_MAIN(tst_KConcatenateRowsProxyModel)

#include "kconcatenaterowsproxymodeltest.moc"