24 #include <QtGui/QtGui>
28 Q_DECLARE_METATYPE ( QModelIndex )
37 connect ( model, SIGNAL ( columnsAboutToBeInserted (
const QModelIndex &,
int,
int ) ),
38 this, SLOT ( runAllTests() ) );
39 connect ( model, SIGNAL ( columnsAboutToBeRemoved (
const QModelIndex &,
int,
int ) ),
40 this, SLOT ( runAllTests() ) );
41 connect ( model, SIGNAL ( columnsInserted (
const QModelIndex &,
int,
int ) ),
42 this, SLOT ( runAllTests() ) );
43 connect ( model, SIGNAL ( columnsRemoved (
const QModelIndex &,
int,
int ) ),
44 this, SLOT ( runAllTests() ) );
45 connect ( model, SIGNAL ( dataChanged (
const QModelIndex &,
const QModelIndex & ) ),
46 this, SLOT ( runAllTests() ) );
47 connect ( model, SIGNAL ( headerDataChanged ( Qt::Orientation,
int,
int ) ),
48 this, SLOT ( runAllTests() ) );
49 connect ( model, SIGNAL ( layoutAboutToBeChanged () ),
this, SLOT ( runAllTests() ) );
50 connect ( model, SIGNAL ( layoutChanged () ),
this, SLOT ( runAllTests() ) );
51 connect ( model, SIGNAL ( modelReset () ),
this, SLOT ( runAllTests() ) );
52 connect ( model, SIGNAL ( modelAboutToBeReset () ),
this, SLOT ( modelAboutToBeReset() ) );
53 connect ( model, SIGNAL ( modelReset () ),
this, SLOT ( modelReset() ) );
54 connect ( model, SIGNAL ( rowsAboutToBeInserted (
const QModelIndex &,
int,
int ) ),
55 this, SLOT ( runAllTests() ) );
56 connect ( model, SIGNAL ( rowsAboutToBeRemoved (
const QModelIndex &,
int,
int ) ),
57 this, SLOT ( runAllTests() ) );
58 connect ( model, SIGNAL ( rowsInserted (
const QModelIndex &,
int,
int ) ),
59 this, SLOT ( runAllTests() ) );
60 connect ( model, SIGNAL ( rowsRemoved (
const QModelIndex &,
int,
int ) ),
61 this, SLOT ( runAllTests() ) );
64 connect ( model, SIGNAL ( layoutAboutToBeChanged() ),
65 this, SLOT ( layoutAboutToBeChanged() ) );
66 connect ( model, SIGNAL ( layoutChanged() ),
67 this, SLOT ( layoutChanged() ) );
69 connect ( model, SIGNAL ( rowsAboutToBeInserted (
const QModelIndex &,
int,
int ) ),
70 this, SLOT ( rowsAboutToBeInserted (
const QModelIndex &,
int,
int ) ) );
71 connect ( model, SIGNAL ( rowsAboutToBeRemoved (
const QModelIndex &,
int,
int ) ),
72 this, SLOT ( rowsAboutToBeRemoved (
const QModelIndex &,
int,
int ) ) );
73 connect ( model, SIGNAL ( rowsInserted (
const QModelIndex &,
int,
int ) ),
74 this, SLOT ( rowsInserted (
const QModelIndex &,
int,
int ) ) );
75 connect ( model, SIGNAL ( rowsRemoved (
const QModelIndex &,
int,
int ) ),
76 this, SLOT ( rowsRemoved (
const QModelIndex &,
int,
int ) ) );
78 connect ( model, SIGNAL ( rowsAboutToBeMoved (
const QModelIndex &,
int,
int,
const QModelIndex &,
int ) ),
79 this, SLOT ( rowsAboutToBeMoved (
const QModelIndex &,
int,
int,
const QModelIndex &,
int ) ) );
80 connect ( model, SIGNAL ( rowsMoved (
const QModelIndex &,
int,
int,
const QModelIndex &,
int ) ),
81 this, SLOT ( rowsMoved (
const QModelIndex &,
int,
int,
const QModelIndex &,
int ) ) );
92 nonDestructiveBasicTest();
105 void ModelTest::nonDestructiveBasicTest()
107 Q_ASSERT ( model->buddy ( QModelIndex() ) == QModelIndex() );
109 Q_ASSERT ( model->columnCount ( QModelIndex() ) >= 0 );
110 Q_ASSERT ( model->data ( QModelIndex() ) == QVariant() );
114 Qt::ItemFlags flags = model->flags ( QModelIndex() );
115 Q_ASSERT ( flags == Qt::ItemIsDropEnabled || flags == 0 );
117 model->hasChildren ( QModelIndex() );
118 model->hasIndex ( 0, 0 );
119 model->headerData ( 0, Qt::Horizontal );
120 model->index ( 0, 0 );
121 model->itemData ( QModelIndex() );
123 model->match ( QModelIndex(), -1, cache );
125 Q_ASSERT ( model->parent ( QModelIndex() ) == QModelIndex() );
126 Q_ASSERT ( model->rowCount() >= 0 );
128 model->setData ( QModelIndex(), variant, -1 );
129 model->setHeaderData ( -1, Qt::Horizontal, QVariant() );
130 model->setHeaderData ( 999999, Qt::Horizontal, QVariant() );
131 QMap<
int, QVariant> roles;
132 model->sibling ( 0, 0, QModelIndex() );
133 model->span ( QModelIndex() );
134 model->supportedDropActions();
146 QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
147 int rows = model->rowCount ( topIndex );
148 Q_ASSERT ( rows >= 0 );
150 Q_ASSERT ( model->hasChildren ( topIndex ) == true );
152 QModelIndex secondLevelIndex = model->index ( 0, 0, topIndex );
153 if ( secondLevelIndex.isValid() ) {
155 rows = model->rowCount ( secondLevelIndex );
156 Q_ASSERT ( rows >= 0 );
158 Q_ASSERT ( model->hasChildren ( secondLevelIndex ) == true );
168 void ModelTest::columnCount()
171 QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
172 Q_ASSERT ( model->columnCount ( topIndex ) >= 0 );
175 QModelIndex childIndex = model->index ( 0, 0, topIndex );
176 if ( childIndex.isValid() )
177 Q_ASSERT ( model->columnCount ( childIndex ) >= 0 );
186 void ModelTest::hasIndex()
190 Q_ASSERT ( model->hasIndex ( -2, -2 ) == false );
191 Q_ASSERT ( model->hasIndex ( -2, 0 ) == false );
192 Q_ASSERT ( model->hasIndex ( 0, -2 ) == false );
194 int rows = model->rowCount();
195 int columns = model->columnCount();
198 Q_ASSERT ( model->hasIndex ( rows, columns ) == false );
199 Q_ASSERT ( model->hasIndex ( rows + 1, columns + 1 ) == false );
202 Q_ASSERT ( model->hasIndex ( 0, 0 ) == true );
212 void ModelTest::index()
216 Q_ASSERT ( model->index ( -2, -2 ) == QModelIndex() );
217 Q_ASSERT ( model->index ( -2, 0 ) == QModelIndex() );
218 Q_ASSERT ( model->index ( 0, -2 ) == QModelIndex() );
220 int rows = model->rowCount();
221 int columns = model->columnCount();
227 Q_ASSERT ( model->index ( rows, columns ) == QModelIndex() );
228 Q_ASSERT ( model->index ( 0, 0 ).isValid() == true );
231 QModelIndex a = model->index ( 0, 0 );
232 QModelIndex b = model->index ( 0, 0 );
242 void ModelTest::parent()
247 Q_ASSERT ( model->parent ( QModelIndex() ) == QModelIndex() );
249 if ( model->rowCount() == 0 )
259 QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
260 Q_ASSERT ( model->parent ( topIndex ) == QModelIndex() );
264 if ( model->rowCount ( topIndex ) > 0 ) {
265 QModelIndex childIndex = model->index ( 0, 0, topIndex );
266 Q_ASSERT ( model->parent ( childIndex ) == topIndex );
272 QModelIndex topIndex1 = model->index ( 0, 1, QModelIndex() );
273 if ( model->rowCount ( topIndex1 ) > 0 ) {
274 QModelIndex childIndex = model->index ( 0, 0, topIndex );
275 QModelIndex childIndex1 = model->index ( 0, 0, topIndex1 );
276 Q_ASSERT ( childIndex != childIndex1 );
281 checkChildren ( QModelIndex() );
298 void ModelTest::checkChildren (
const QModelIndex &parent,
int currentDepth )
301 QModelIndex p = parent;
302 while ( p.isValid() )
312 int rows = model->rowCount ( parent );
313 int columns = model->columnCount ( parent );
318 Q_ASSERT(parent.column() <= 0);
319 Q_ASSERT ( model->hasChildren ( parent ) );
323 Q_ASSERT ( rows >= 0 );
324 Q_ASSERT ( columns >= 0 );
326 Q_ASSERT ( model->hasChildren ( parent ) == true );
331 Q_ASSERT ( model->hasIndex ( rows, 0, parent ) == false );
332 Q_ASSERT ( model->index(rows, 0, parent).isValid() == false );
333 for (
int r = 0; r < rows; ++r ) {
339 Q_ASSERT ( model->hasIndex ( r, columns, parent ) == false );
340 Q_ASSERT ( model->index(r, columns, parent).isValid() == false );
341 for (
int c = 0; c < columns; ++c ) {
342 Q_ASSERT ( model->hasIndex ( r, c, parent ) == true );
343 QModelIndex index = model->index ( r, c, parent );
345 Q_ASSERT ( index.isValid() == true );
348 QModelIndex modifiedIndex = model->index ( r, c, parent );
349 Q_ASSERT ( index == modifiedIndex );
352 QModelIndex a = model->index ( r, c, parent );
353 QModelIndex b = model->index ( r, c, parent );
357 Q_ASSERT ( index.model() == model );
358 Q_ASSERT ( index.row() == r );
359 Q_ASSERT ( index.column() == c );
363 Q_ASSERT ( model->data ( index, Qt::DisplayRole ).isValid() == true );
378 Q_ASSERT ( model->parent ( index ) == parent );
381 if ( model->hasChildren ( index ) && currentDepth < 10 ) {
383 checkChildren ( index, ++currentDepth );
387 QModelIndex newerIndex = model->index ( r, c, parent );
388 Q_ASSERT ( index == newerIndex );
396 void ModelTest::data()
399 Q_ASSERT ( !model->data ( QModelIndex() ).isValid() );
401 if ( model->rowCount() == 0 )
405 Q_ASSERT ( model->index ( 0, 0 ).isValid() );
408 Q_ASSERT ( model->setData ( QModelIndex(), QLatin1String (
"foo" ), Qt::DisplayRole ) == false );
411 QVariant variant = model->data ( model->index ( 0, 0 ), Qt::ToolTipRole );
412 if ( variant.isValid() ) {
413 Q_ASSERT ( qVariantCanConvert<QString> ( variant ) );
415 variant = model->data ( model->index ( 0, 0 ), Qt::StatusTipRole );
416 if ( variant.isValid() ) {
417 Q_ASSERT ( qVariantCanConvert<QString> ( variant ) );
419 variant = model->data ( model->index ( 0, 0 ), Qt::WhatsThisRole );
420 if ( variant.isValid() ) {
421 Q_ASSERT ( qVariantCanConvert<QString> ( variant ) );
425 variant = model->data ( model->index ( 0, 0 ), Qt::SizeHintRole );
426 if ( variant.isValid() ) {
427 Q_ASSERT ( qVariantCanConvert<QSize> ( variant ) );
431 QVariant fontVariant = model->data ( model->index ( 0, 0 ), Qt::FontRole );
432 if ( fontVariant.isValid() ) {
433 Q_ASSERT ( qVariantCanConvert<QFont> ( fontVariant ) );
437 QVariant textAlignmentVariant = model->data ( model->index ( 0, 0 ), Qt::TextAlignmentRole );
438 if ( textAlignmentVariant.isValid() ) {
439 int alignment = textAlignmentVariant.toInt();
440 Q_ASSERT ( alignment == ( alignment & ( Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask ) ) );
445 QVariant colorVariant = model->data ( model->index ( 0, 0 ), Qt::BackgroundColorRole );
446 if ( colorVariant.isValid() ) {
447 Q_ASSERT ( qVariantCanConvert<QColor> ( colorVariant ) );
450 colorVariant = model->data ( model->index ( 0, 0 ), Qt::TextColorRole );
451 if ( colorVariant.isValid() ) {
452 Q_ASSERT ( qVariantCanConvert<QColor> ( colorVariant ) );
456 QVariant checkStateVariant = model->data ( model->index ( 0, 0 ), Qt::CheckStateRole );
457 if ( checkStateVariant.isValid() ) {
458 int state = checkStateVariant.toInt();
459 Q_ASSERT ( state == Qt::Unchecked ||
460 state == Qt::PartiallyChecked ||
461 state == Qt::Checked );
474 qDebug() <<
"rowsAboutToBeInserted" <<
"start=" << start <<
"end=" << end <<
"parent=" << model->data ( parent ).value<QString>()
475 <<
"current count of parent=" << model->rowCount ( parent );
479 c.oldSize = model->rowCount ( parent );
480 c.last = model->data ( model->index ( start - 1, 0, parent ) );
481 c.next = model->data ( model->index ( start, 0, parent ) );
492 Changing c = insert.pop();
493 Q_ASSERT ( c.parent == parent );
494 qDebug() <<
"rowsInserted" <<
"start=" << start <<
"end=" << end <<
"oldsize=" << c.oldSize
495 <<
"parent=" << model->data ( parent ).value<QString>() <<
"current rowcount=" << model->rowCount ( parent );
497 for (
int ii=start; ii <= end; ii++)
499 qDebug() <<
"itemWasInserted:" << model->data ( model->index ( ii, 0, parent ));
503 Q_ASSERT ( c.oldSize + ( end - start + 1 ) == model->rowCount ( parent ) );
504 Q_ASSERT ( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
513 Q_ASSERT ( c.next == model->data ( model->index ( end + 1, 0, c.parent ) ) );
519 qDebug() <<
"@@@@@@@@@@@" <<
"modelAboutToBeReset";
524 qDebug() <<
"@@@@@@@@@@@" <<
"modelReset";
529 for (
int i = 0; i < qBound ( 0, model->rowCount(), 100 ); ++i )
530 changing.append ( QPersistentModelIndex ( model->index ( i, 0 ) ) );
535 for (
int i = 0; i < changing.count(); ++i ) {
536 QPersistentModelIndex p = changing[i];
537 Q_ASSERT ( p == model->index ( p.row(), p.column(), p.parent() ) );
544 qDebug() <<
"rowsAboutToBeMoved" << srcParent << start << end << destParent << destinationRow;
546 cs.parent = srcParent;
547 cs.oldSize = model->rowCount ( srcParent );
548 cs.last = model->data ( model->index ( start - 1, 0, srcParent ) );
549 cs.next = model->data ( model->index ( end + 1, 0, srcParent ) );
552 cd.parent = destParent;
553 cd.oldSize = model->rowCount ( destParent );
554 cd.last = model->data ( model->index ( destinationRow - 1, 0, destParent ) );
555 cd.next = model->data ( model->index ( destinationRow, 0, destParent ) );
559 void ModelTest::rowsMoved(
const QModelIndex &srcParent,
int start,
int end,
const QModelIndex &destParent,
int destinationRow )
561 qDebug() <<
"rowsMoved" << srcParent << start << end << destParent << destinationRow;
563 Changing cd = insert.pop();
564 Q_ASSERT ( cd.parent == destParent );
565 if (srcParent == destParent)
567 Q_ASSERT ( cd.oldSize == model->rowCount ( destParent ) );
577 Q_ASSERT ( cd.oldSize + ( end - start + 1 ) == model->rowCount ( destParent ) );
579 Q_ASSERT ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
580 Q_ASSERT ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
582 Changing cs =
remove.pop();
584 Q_ASSERT ( cs.parent == srcParent );
585 if (srcParent == destParent)
587 Q_ASSERT ( cs.oldSize == model->rowCount ( srcParent ) );
591 Q_ASSERT ( cs.oldSize - ( end - start + 1 ) == model->rowCount ( srcParent ) );
593 Q_ASSERT ( cs.last == model->data ( model->index ( start - 1, 0, srcParent ) ) );
594 qDebug() << cs.next << model->data ( model->index ( start, 0, srcParent ) );
595 Q_ASSERT ( cs.next == model->data ( model->index ( start, 0, srcParent ) ) );
607 qDebug() <<
"ratbr" << parent << start << end;
608 for (
int ii=start; ii <= end; ii++)
610 qDebug() <<
"itemwillbe removed:" << model->data ( model->index ( ii, 0, parent ));
616 c.oldSize = model->rowCount ( parent );
617 c.last = model->data ( model->index ( start - 1, 0, parent ) );
618 c.next = model->data ( model->index ( end + 1, 0, parent ) );
629 qDebug() <<
"rr" << parent << start << end;
630 Changing c =
remove.pop();
631 Q_ASSERT ( c.parent == parent );
632 Q_ASSERT ( c.oldSize - ( end - start + 1 ) == model->rowCount ( parent ) );
633 Q_ASSERT ( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
634 Q_ASSERT ( c.next == model->data ( model->index ( start, 0, c.parent ) ) );
void rowsAboutToBeMoved(const QModelIndex &, int, int, const QModelIndex &, int)
void rowsInserted(const QModelIndex &parent, int start, int end)
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
void rowsMoved(const QModelIndex &, int, int, const QModelIndex &, int)
void modelAboutToBeReset()
void rowsRemoved(const QModelIndex &parent, int start, int end)
void layoutAboutToBeChanged()