1
/****************************************************************************
3
** Copyright (C) 2015 The Qt Company Ltd.
4
** Contact: http://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL21$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see http://www.qt.io/terms-conditions. For further
15
** information use the contact form at http://www.qt.io/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 or version 3 as published by the Free
20
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22
** following information to ensure the GNU Lesser General Public License
23
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26
** As a special exception, The Qt Company gives you certain additional
27
** rights. These rights are described in The Qt Company LGPL Exception
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32
****************************************************************************/
39
Connect to all of the models signals. Whenever anything happens recheck everything.
41
ModelTest::ModelTest ( QAbstractItemModel *_model, QObject *parent ) : QObject ( parent ), model ( _model ), fetchingMore ( false )
44
qFatal("%s: model must not be null", Q_FUNC_INFO);
46
connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
47
this, SLOT(runAllTests()) );
48
connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
49
this, SLOT(runAllTests()) );
50
connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
51
this, SLOT(runAllTests()) );
52
connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
53
this, SLOT(runAllTests()) );
54
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
55
this, SLOT(runAllTests()) );
56
connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
57
this, SLOT(runAllTests()) );
58
connect(model, SIGNAL(layoutAboutToBeChanged()), this, SLOT(runAllTests()) );
59
connect(model, SIGNAL(layoutChanged()), this, SLOT(runAllTests()) );
60
connect(model, SIGNAL(modelReset()), this, SLOT(runAllTests()) );
61
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
62
this, SLOT(runAllTests()) );
63
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
64
this, SLOT(runAllTests()) );
65
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
66
this, SLOT(runAllTests()) );
67
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
68
this, SLOT(runAllTests()) );
70
// Special checks for changes
71
connect(model, SIGNAL(layoutAboutToBeChanged()),
72
this, SLOT(layoutAboutToBeChanged()) );
73
connect(model, SIGNAL(layoutChanged()),
74
this, SLOT(layoutChanged()) );
76
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
77
this, SLOT(rowsAboutToBeInserted(QModelIndex,int,int)) );
78
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
79
this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)) );
80
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
81
this, SLOT(rowsInserted(QModelIndex,int,int)) );
82
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
83
this, SLOT(rowsRemoved(QModelIndex,int,int)) );
84
connect(model, SIGNAL (rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
85
this, SLOT (rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)) );
86
connect(model, SIGNAL (rowsMoved(QModelIndex,int,int,QModelIndex,int)),
87
this, SLOT (rowsMoved(QModelIndex,int,int,QModelIndex,int)) );
88
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
89
this, SLOT(dataChanged(QModelIndex,QModelIndex)) );
90
connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
91
this, SLOT(headerDataChanged(Qt::Orientation,int,int)) );
96
void ModelTest::runAllTests()
100
nonDestructiveBasicTest();
110
nonDestructiveBasicTest tries to call a number of the basic functions (not all)
111
to make sure the model doesn't outright segfault, testing the functions that makes sense.
113
void ModelTest::nonDestructiveBasicTest()
115
QVERIFY( model->buddy ( QModelIndex() ) == QModelIndex() );
116
model->canFetchMore ( QModelIndex() );
117
QVERIFY( model->columnCount ( QModelIndex() ) >= 0 );
118
QVERIFY( model->data ( QModelIndex() ) == QVariant() );
120
model->fetchMore ( QModelIndex() );
121
fetchingMore = false;
122
Qt::ItemFlags flags = model->flags ( QModelIndex() );
123
QVERIFY( flags == Qt::ItemIsDropEnabled || flags == 0 );
124
model->hasChildren ( QModelIndex() );
125
model->hasIndex ( 0, 0 );
126
model->headerData ( 0, Qt::Horizontal );
127
model->index ( 0, 0 );
128
model->itemData ( QModelIndex() );
130
model->match ( QModelIndex(), -1, cache );
132
QVERIFY( model->parent ( QModelIndex() ) == QModelIndex() );
133
QVERIFY( model->rowCount() >= 0 );
135
model->setData ( QModelIndex(), variant, -1 );
136
model->setHeaderData ( -1, Qt::Horizontal, QVariant() );
137
model->setHeaderData ( 999999, Qt::Horizontal, QVariant() );
138
QMap<int, QVariant> roles;
139
model->sibling ( 0, 0, QModelIndex() );
140
model->span ( QModelIndex() );
141
model->supportedDropActions();
145
Tests model's implementation of QAbstractItemModel::rowCount() and hasChildren()
147
Models that are dynamically populated are not as fully tested here.
149
void ModelTest::rowCount()
153
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
154
int rows = model->rowCount ( topIndex );
155
QVERIFY( rows >= 0 );
157
QVERIFY( model->hasChildren ( topIndex ) );
159
QModelIndex secondLevelIndex = model->index ( 0, 0, topIndex );
160
if ( secondLevelIndex.isValid() ) { // not the top level
161
// check a row count where parent is valid
162
rows = model->rowCount ( secondLevelIndex );
163
QVERIFY( rows >= 0 );
165
QVERIFY( model->hasChildren ( secondLevelIndex ) );
168
// The models rowCount() is tested more extensively in checkChildren(),
169
// but this catches the big mistakes
173
Tests model's implementation of QAbstractItemModel::columnCount() and hasChildren()
175
void ModelTest::columnCount()
178
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
179
QVERIFY( model->columnCount ( topIndex ) >= 0 );
181
// check a column count where parent is valid
182
QModelIndex childIndex = model->index ( 0, 0, topIndex );
183
if ( childIndex.isValid() )
184
QVERIFY( model->columnCount ( childIndex ) >= 0 );
186
// columnCount() is tested more extensively in checkChildren(),
187
// but this catches the big mistakes
191
Tests model's implementation of QAbstractItemModel::hasIndex()
193
void ModelTest::hasIndex()
196
// Make sure that invalid values returns an invalid index
197
QVERIFY( !model->hasIndex ( -2, -2 ) );
198
QVERIFY( !model->hasIndex ( -2, 0 ) );
199
QVERIFY( !model->hasIndex ( 0, -2 ) );
201
int rows = model->rowCount();
202
int columns = model->columnCount();
204
// check out of bounds
205
QVERIFY( !model->hasIndex ( rows, columns ) );
206
QVERIFY( !model->hasIndex ( rows + 1, columns + 1 ) );
209
QVERIFY( model->hasIndex ( 0, 0 ) );
211
// hasIndex() is tested more extensively in checkChildren(),
212
// but this catches the big mistakes
216
Tests model's implementation of QAbstractItemModel::index()
218
void ModelTest::index()
221
// Make sure that invalid values returns an invalid index
222
QVERIFY( model->index ( -2, -2 ) == QModelIndex() );
223
QVERIFY( model->index ( -2, 0 ) == QModelIndex() );
224
QVERIFY( model->index ( 0, -2 ) == QModelIndex() );
226
int rows = model->rowCount();
227
int columns = model->columnCount();
232
// Catch off by one errors
233
QVERIFY( model->index ( rows, columns ) == QModelIndex() );
234
QVERIFY( model->index ( 0, 0 ).isValid() );
236
// Make sure that the same index is *always* returned
237
QModelIndex a = model->index ( 0, 0 );
238
QModelIndex b = model->index ( 0, 0 );
241
// index() is tested more extensively in checkChildren(),
242
// but this catches the big mistakes
246
Tests model's implementation of QAbstractItemModel::parent()
248
void ModelTest::parent()
251
// Make sure the model won't crash and will return an invalid QModelIndex
252
// when asked for the parent of an invalid index.
253
QVERIFY( model->parent ( QModelIndex() ) == QModelIndex() );
255
if ( model->rowCount() == 0 )
258
// Column 0 | Column 1 |
260
// \- topIndex | topIndex1 |
261
// \- childIndex | childIndex1 |
263
// Common error test #1, make sure that a top level index has a parent
264
// that is a invalid QModelIndex.
265
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
266
QVERIFY( model->parent ( topIndex ) == QModelIndex() );
268
// Common error test #2, make sure that a second level index has a parent
269
// that is the first level index.
270
if ( model->rowCount ( topIndex ) > 0 ) {
271
QModelIndex childIndex = model->index ( 0, 0, topIndex );
272
QVERIFY( model->parent ( childIndex ) == topIndex );
275
// Common error test #3, the second column should NOT have the same children
276
// as the first column in a row.
277
// Usually the second column shouldn't have children.
278
QModelIndex topIndex1 = model->index ( 0, 1, QModelIndex() );
279
if ( model->rowCount ( topIndex1 ) > 0 ) {
280
QModelIndex childIndex = model->index ( 0, 0, topIndex );
281
QModelIndex childIndex1 = model->index ( 0, 0, topIndex1 );
282
QVERIFY( childIndex != childIndex1 );
285
// Full test, walk n levels deep through the model making sure that all
286
// parent's children correctly specify their parent.
287
checkChildren ( QModelIndex() );
291
Called from the parent() test.
293
A model that returns an index of parent X should also return X when asking
294
for the parent of the index.
296
This recursive function does pretty extensive testing on the whole model in an
297
effort to catch edge cases.
299
This function assumes that rowCount(), columnCount() and index() already work.
300
If they have a bug it will point it out, but the above tests should have already
301
found the basic bugs because it is easier to figure out the problem in
302
those tests then this one.
304
void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
306
// First just try walking back up the tree.
307
QModelIndex p = parent;
308
while ( p.isValid() )
311
// For models that are dynamically populated
312
if ( model->canFetchMore ( parent ) ) {
314
model->fetchMore ( parent );
315
fetchingMore = false;
318
int rows = model->rowCount ( parent );
319
int columns = model->columnCount ( parent );
322
QVERIFY( model->hasChildren ( parent ) );
324
// Some further testing against rows(), columns(), and hasChildren()
325
QVERIFY( rows >= 0 );
326
QVERIFY( columns >= 0 );
328
QVERIFY( model->hasChildren ( parent ) );
330
//qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows
331
// << "columns:" << columns << "parent column:" << parent.column();
333
const QModelIndex topLeftChild = model->index( 0, 0, parent );
335
QVERIFY( !model->hasIndex ( rows + 1, 0, parent ) );
336
for ( int r = 0; r < rows; ++r ) {
337
if ( model->canFetchMore ( parent ) ) {
339
model->fetchMore ( parent );
340
fetchingMore = false;
342
QVERIFY( !model->hasIndex ( r, columns + 1, parent ) );
343
for ( int c = 0; c < columns; ++c ) {
344
QVERIFY( model->hasIndex ( r, c, parent ) );
345
QModelIndex index = model->index ( r, c, parent );
346
// rowCount() and columnCount() said that it existed...
347
QVERIFY( index.isValid() );
349
// index() should always return the same index when called twice in a row
350
QModelIndex modifiedIndex = model->index ( r, c, parent );
351
QVERIFY( index == modifiedIndex );
353
// Make sure we get the same index if we request it twice in a row
354
QModelIndex a = model->index ( r, c, parent );
355
QModelIndex b = model->index ( r, c, parent );
359
const QModelIndex sibling = model->sibling( r, c, topLeftChild );
360
QVERIFY( index == sibling );
363
const QModelIndex sibling = topLeftChild.sibling( r, c );
364
QVERIFY( index == sibling );
367
// Some basic checking on the index that is returned
368
QVERIFY( index.model() == model );
369
QCOMPARE( index.row(), r );
370
QCOMPARE( index.column(), c );
371
// While you can technically return a QVariant usually this is a sign
372
// of a bug in data(). Disable if this really is ok in your model.
373
// QVERIFY( model->data ( index, Qt::DisplayRole ).isValid() );
375
// If the next test fails here is some somewhat useful debug you play with.
377
if (model->parent(index) != parent) {
378
qDebug() << r << c << currentDepth << model->data(index).toString()
379
<< model->data(parent).toString();
380
qDebug() << index << parent << model->parent(index);
381
// And a view that you can even use to show the model.
383
// view.setModel(model);
387
// Check that we can get back our real parent.
388
QCOMPARE( model->parent ( index ), parent );
390
// recursively go down the children
391
if ( model->hasChildren ( index ) && currentDepth < 10 ) {
392
//qDebug() << r << c << "has children" << model->rowCount(index);
393
checkChildren ( index, ++currentDepth );
394
}/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/
396
// make sure that after testing the children that the index doesn't change.
397
QModelIndex newerIndex = model->index ( r, c, parent );
398
QVERIFY( index == newerIndex );
404
Tests model's implementation of QAbstractItemModel::data()
406
void ModelTest::data()
408
// Invalid index should return an invalid qvariant
409
QVERIFY( !model->data ( QModelIndex() ).isValid() );
411
if ( model->rowCount() == 0 )
414
// A valid index should have a valid QVariant data
415
QVERIFY( model->index ( 0, 0 ).isValid() );
417
// shouldn't be able to set data on an invalid index
418
QVERIFY( !model->setData ( QModelIndex(), QLatin1String ( "foo" ), Qt::DisplayRole ) );
420
// General Purpose roles that should return a QString
421
QVariant variant = model->data ( model->index ( 0, 0 ), Qt::ToolTipRole );
422
if ( variant.isValid() ) {
423
QVERIFY( variant.canConvert<QString>() );
425
variant = model->data ( model->index ( 0, 0 ), Qt::StatusTipRole );
426
if ( variant.isValid() ) {
427
QVERIFY( variant.canConvert<QString>() );
429
variant = model->data ( model->index ( 0, 0 ), Qt::WhatsThisRole );
430
if ( variant.isValid() ) {
431
QVERIFY( variant.canConvert<QString>() );
434
// General Purpose roles that should return a QSize
435
variant = model->data ( model->index ( 0, 0 ), Qt::SizeHintRole );
436
if ( variant.isValid() ) {
437
QVERIFY( variant.canConvert<QSize>() );
440
// General Purpose roles that should return a QFont
441
QVariant fontVariant = model->data ( model->index ( 0, 0 ), Qt::FontRole );
442
if ( fontVariant.isValid() ) {
443
QVERIFY( fontVariant.canConvert<QFont>() );
446
// Check that the alignment is one we know about
447
QVariant textAlignmentVariant = model->data ( model->index ( 0, 0 ), Qt::TextAlignmentRole );
448
if ( textAlignmentVariant.isValid() ) {
449
int alignment = textAlignmentVariant.toInt();
450
QCOMPARE( alignment, static_cast<int>( alignment & ( Qt::AlignHorizontal_Mask
451
| Qt::AlignVertical_Mask ) ) );
454
// General Purpose roles that should return a QColor
455
QVariant colorVariant = model->data ( model->index ( 0, 0 ), Qt::BackgroundColorRole );
456
if ( colorVariant.isValid() ) {
457
QVERIFY( colorVariant.canConvert<QColor>() );
460
colorVariant = model->data ( model->index ( 0, 0 ), Qt::TextColorRole );
461
if ( colorVariant.isValid() ) {
462
QVERIFY( colorVariant.canConvert<QColor>() );
465
// Check that the "check state" is one we know about.
466
QVariant checkStateVariant = model->data ( model->index ( 0, 0 ), Qt::CheckStateRole );
467
if ( checkStateVariant.isValid() ) {
468
int state = checkStateVariant.toInt();
469
QVERIFY( state == Qt::Unchecked ||
470
state == Qt::PartiallyChecked ||
471
state == Qt::Checked );
476
Store what is about to be inserted to make sure it actually happens
480
void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int /* end */)
483
// qDebug() << "rowsAboutToBeInserted" << "start=" << start << "end=" << end << "parent=" << model->data ( parent ).toString()
484
// << "current count of parent=" << model->rowCount ( parent ); // << "display of last=" << model->data( model->index(start-1, 0, parent) );
485
// qDebug() << model->index(start-1, 0, parent) << model->data( model->index(start-1, 0, parent) );
488
c.oldSize = model->rowCount ( parent );
489
c.last = model->data ( model->index ( start - 1, 0, parent ) );
490
c.next = model->data ( model->index ( start, 0, parent ) );
495
Confirm that what was said was going to happen actually did
497
\sa rowsAboutToBeInserted()
499
void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end )
501
Changing c = insert.pop();
502
QVERIFY( c.parent == parent );
503
// qDebug() << "rowsInserted" << "start=" << start << "end=" << end << "oldsize=" << c.oldSize
504
// << "parent=" << model->data ( parent ).toString() << "current rowcount of parent=" << model->rowCount ( parent );
506
// for (int ii=start; ii <= end; ii++)
508
// qDebug() << "itemWasInserted:" << ii << model->data ( model->index ( ii, 0, parent ));
512
QVERIFY( c.oldSize + ( end - start + 1 ) == model->rowCount ( parent ) );
513
QVERIFY( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
515
if (c.next != model->data(model->index(end + 1, 0, c.parent))) {
516
qDebug() << start << end;
517
for (int i=0; i < model->rowCount(); ++i)
518
qDebug() << model->index(i, 0).data().toString();
519
qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent));
522
QVERIFY( c.next == model->data ( model->index ( end + 1, 0, c.parent ) ) );
525
void ModelTest::layoutAboutToBeChanged()
527
for ( int i = 0; i < qBound ( 0, model->rowCount(), 100 ); ++i )
528
changing.append ( QPersistentModelIndex ( model->index ( i, 0 ) ) );
531
void ModelTest::layoutChanged()
533
for ( int i = 0; i < changing.count(); ++i ) {
534
QPersistentModelIndex p = changing[i];
535
QVERIFY( p == model->index ( p.row(), p.column(), p.parent() ) );
541
Store what is about to be inserted to make sure it actually happens
545
void ModelTest::rowsAboutToBeRemoved ( const QModelIndex &parent, int start, int end )
547
//qDebug() << "ratbr" << parent << start << end;
550
c.oldSize = model->rowCount ( parent );
551
c.last = model->data ( model->index ( start - 1, 0, parent ) );
552
c.next = model->data ( model->index ( end + 1, 0, parent ) );
557
Confirm that what was said was going to happen actually did
559
\sa rowsAboutToBeRemoved()
561
void ModelTest::rowsRemoved ( const QModelIndex & parent, int start, int end )
563
// qDebug() << "rr" << parent << start << end;
564
Changing c = remove.pop();
565
QVERIFY( c.parent == parent );
566
QVERIFY( c.oldSize - ( end - start + 1 ) == model->rowCount ( parent ) );
567
QVERIFY( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
568
QVERIFY( c.next == model->data ( model->index ( start, 0, c.parent ) ) );
571
void ModelTest::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
573
QVERIFY(topLeft.isValid());
574
QVERIFY(bottomRight.isValid());
575
QModelIndex commonParent = bottomRight.parent();
576
QVERIFY(topLeft.parent() == commonParent);
577
QVERIFY(topLeft.row() <= bottomRight.row());
578
QVERIFY(topLeft.column() <= bottomRight.column());
579
int rowCount = model->rowCount(commonParent);
580
int columnCount = model->columnCount(commonParent);
581
QVERIFY(bottomRight.row() < rowCount);
582
QVERIFY(bottomRight.column() < columnCount);
585
void ModelTest::headerDataChanged(Qt::Orientation orientation, int start, int end)
589
QVERIFY(start <= end);
590
int itemCount = orientation == Qt::Vertical ? model->rowCount() : model->columnCount();
591
QVERIFY(start < itemCount);
592
QVERIFY(end < itemCount);
595
void ModelTest::rowsAboutToBeMoved( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow )
598
cs.parent = srcParent;
599
cs.oldSize = model->rowCount ( srcParent );
600
cs.last = model->data ( model->index ( start - 1, 0, srcParent ) );
601
cs.next = model->data ( model->index ( end + 1, 0, srcParent ) );
604
cd.parent = destParent;
605
cd.oldSize = model->rowCount ( destParent );
606
cd.last = model->data ( model->index ( destinationRow - 1, 0, destParent ) );
607
cd.next = model->data ( model->index ( destinationRow, 0, destParent ) );
611
void ModelTest::rowsMoved( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow )
613
Changing cd = insert.pop();
614
QVERIFY ( cd.parent == destParent );
615
if (srcParent == destParent) {
616
QVERIFY ( cd.oldSize == model->rowCount ( destParent ) );
618
// TODO: Find out what I can assert here about last and next.
619
// Q_ASSERT ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
620
// Q_ASSERT ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
623
QVERIFY ( cd.oldSize + ( end - start + 1 ) == model->rowCount ( destParent ) );
625
QVERIFY ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
626
QVERIFY ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
629
Changing cs = remove.pop();
630
QVERIFY ( cs.parent == srcParent );
631
if (srcParent == destParent) {
632
QVERIFY ( cs.oldSize == model->rowCount ( srcParent ) );
635
QVERIFY ( cs.oldSize - ( end - start + 1 ) == model->rowCount ( srcParent ) );
637
QVERIFY ( cs.last == model->data ( model->index ( start - 1, 0, srcParent ) ) );
638
QVERIFY ( cs.next == model->data ( model->index ( start, 0, srcParent ) ) );