Update ModelTest from Qt 4.8.0.

This commit is contained in:
Felix Geyer 2011-12-25 20:53:33 +01:00
parent 256dc89466
commit 227d39ceb2
2 changed files with 319 additions and 278 deletions

View File

@ -1,84 +1,106 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
** **
** This file is part of the Qt Concurrent project on Trolltech Labs. ** This file is part of the test suite of the Qt Toolkit.
** **
** This file may be used under the terms of the GNU General Public ** $QT_BEGIN_LICENSE:LGPL$
** License version 2.0 as published by the Free Software Foundation ** GNU Lesser General Public License Usage
** and appearing in the file LICENSE.GPL-2 included in the packaging of ** This file may be used under the terms of the GNU Lesser General Public
** this file. Please review the following information to ensure GNU ** License version 2.1 as published by the Free Software Foundation and
** General Public Licensing requirements will be met: ** appearing in the file LICENSE.LGPL included in the packaging of this
** http://www.trolltech.com/products/qt/opensource.html ** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** **
** If you are unsure which license is appropriate for your use, please ** In addition, as a special exception, Nokia gives you certain additional
** review the following information: ** rights. These rights are described in the Nokia Qt LGPL Exception
** http://www.trolltech.com/products/qt/licensing.html or contact the ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
** sales department at sales@trolltech.com.
** **
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** GNU General Public License Usage
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
** **
****************************************************************************/ ****************************************************************************/
#include <QtGui/QtGui> #include <QtGui/QtGui>
#include "modeltest.h" #include "modeltest.h"
Q_DECLARE_METATYPE(QModelIndex) #include <QtTest/QtTest>
Q_DECLARE_METATYPE ( QModelIndex )
/*! /*!
Connect to all of the models signals. Whenever anything happens recheck everything. Connect to all of the models signals. Whenever anything happens recheck everything.
*/ */
ModelTest::ModelTest(QAbstractItemModel *_model, QObject *parent) : QObject(parent), model(_model), fetchingMore(false) ModelTest::ModelTest ( QAbstractItemModel *_model, QObject *parent ) : QObject ( parent ), model ( _model ), fetchingMore ( false )
{ {
Q_ASSERT(model); if (!model)
qFatal("%s: model must not be null", Q_FUNC_INFO);
connect(model, SIGNAL(columnsAboutToBeInserted(const QModelIndex &, int, int)), connect ( model, SIGNAL ( columnsAboutToBeInserted ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(columnsAboutToBeRemoved(const QModelIndex &, int, int)), connect ( model, SIGNAL ( columnsAboutToBeRemoved ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(columnsInserted(const QModelIndex &, int, int)), connect ( model, SIGNAL ( columnsInserted ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(columnsRemoved(const QModelIndex &, int, int)), connect ( model, SIGNAL ( columnsRemoved ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), connect ( model, SIGNAL ( dataChanged ( const QModelIndex &, const QModelIndex & ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)), connect ( model, SIGNAL ( headerDataChanged ( Qt::Orientation, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(layoutAboutToBeChanged ()), this, SLOT(runAllTests())); connect ( model, SIGNAL ( layoutAboutToBeChanged () ), this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(layoutChanged ()), this, SLOT(runAllTests())); connect ( model, SIGNAL ( layoutChanged () ), this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(modelReset ()), this, SLOT(runAllTests())); connect ( model, SIGNAL ( modelReset () ), this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsAboutToBeInserted ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsAboutToBeRemoved ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsInserted ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsRemoved ( const QModelIndex &, int, int ) ),
this, SLOT(runAllTests())); this, SLOT ( runAllTests() ) );
// Special checks for inserting/removing // Special checks for inserting/removing
connect(model, SIGNAL(layoutAboutToBeChanged()), connect ( model, SIGNAL ( layoutAboutToBeChanged() ),
this, SLOT(layoutAboutToBeChanged())); this, SLOT ( layoutAboutToBeChanged() ) );
connect(model, SIGNAL(layoutChanged()), connect ( model, SIGNAL ( layoutChanged() ),
this, SLOT(layoutChanged())); this, SLOT ( layoutChanged() ) );
connect(model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsAboutToBeInserted ( const QModelIndex &, int, int ) ),
this, SLOT(rowsAboutToBeInserted(const QModelIndex &, int, int))); this, SLOT ( rowsAboutToBeInserted ( const QModelIndex &, int, int ) ) );
connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsAboutToBeRemoved ( const QModelIndex &, int, int ) ),
this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int))); this, SLOT ( rowsAboutToBeRemoved ( const QModelIndex &, int, int ) ) );
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsInserted ( const QModelIndex &, int, int ) ),
this, SLOT(rowsInserted(const QModelIndex &, int, int))); this, SLOT ( rowsInserted ( const QModelIndex &, int, int ) ) );
connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), connect ( model, SIGNAL ( rowsRemoved ( const QModelIndex &, int, int ) ),
this, SLOT(rowsRemoved(const QModelIndex &, int, int))); this, SLOT ( rowsRemoved ( const QModelIndex &, int, int ) ) );
runAllTests(); runAllTests();
} }
void ModelTest::runAllTests() void ModelTest::runAllTests()
{ {
if (fetchingMore) if ( fetchingMore )
return; return;
nonDestructiveBasicTest(); nonDestructiveBasicTest();
rowCount(); rowCount();
@ -95,34 +117,32 @@ void ModelTest::runAllTests()
*/ */
void ModelTest::nonDestructiveBasicTest() void ModelTest::nonDestructiveBasicTest()
{ {
Q_ASSERT(model->buddy(QModelIndex()) == QModelIndex()); QVERIFY( model->buddy ( QModelIndex() ) == QModelIndex() );
model->canFetchMore(QModelIndex()); model->canFetchMore ( QModelIndex() );
Q_ASSERT(model->columnCount(QModelIndex()) >= 0); QVERIFY( model->columnCount ( QModelIndex() ) >= 0 );
Q_ASSERT(model->data(QModelIndex()) == QVariant()); QVERIFY( model->data ( QModelIndex() ) == QVariant() );
fetchingMore = true; fetchingMore = true;
model->fetchMore(QModelIndex()); model->fetchMore ( QModelIndex() );
fetchingMore = false; fetchingMore = false;
Qt::ItemFlags flags = model->flags(QModelIndex()); Qt::ItemFlags flags = model->flags ( QModelIndex() );
Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0); QVERIFY( flags == Qt::ItemIsDropEnabled || flags == 0 );
model->hasChildren(QModelIndex()); model->hasChildren ( QModelIndex() );
model->hasIndex(0, 0); model->hasIndex ( 0, 0 );
model->headerData(0, Qt::Horizontal); model->headerData ( 0, Qt::Horizontal );
model->index(0, 0); model->index ( 0, 0 );
Q_ASSERT(model->index(-1, -1) == QModelIndex()); model->itemData ( QModelIndex() );
model->itemData(QModelIndex());
QVariant cache; QVariant cache;
model->match(QModelIndex(), -1, cache); model->match ( QModelIndex(), -1, cache );
model->mimeTypes(); model->mimeTypes();
Q_ASSERT(model->parent(QModelIndex()) == QModelIndex()); QVERIFY( model->parent ( QModelIndex() ) == QModelIndex() );
Q_ASSERT(model->rowCount() >= 0); QVERIFY( model->rowCount() >= 0 );
QVariant variant; QVariant variant;
model->setData(QModelIndex(), variant, -1); model->setData ( QModelIndex(), variant, -1 );
model->setHeaderData(-1, Qt::Horizontal, QVariant()); model->setHeaderData ( -1, Qt::Horizontal, QVariant() );
model->setHeaderData(0, Qt::Horizontal, QVariant()); model->setHeaderData ( 999999, Qt::Horizontal, QVariant() );
model->setHeaderData(999999, Qt::Horizontal, QVariant());
QMap<int, QVariant> roles; QMap<int, QVariant> roles;
model->sibling(0, 0, QModelIndex()); model->sibling ( 0, 0, QModelIndex() );
model->span(QModelIndex()); model->span ( QModelIndex() );
model->supportedDropActions(); model->supportedDropActions();
} }
@ -133,20 +153,21 @@ void ModelTest::nonDestructiveBasicTest()
*/ */
void ModelTest::rowCount() void ModelTest::rowCount()
{ {
// qDebug() << "rc";
// check top row // check top row
QModelIndex topIndex = model->index(0, 0, QModelIndex()); QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
int rows = model->rowCount(topIndex); int rows = model->rowCount ( topIndex );
Q_ASSERT(rows >= 0); QVERIFY( rows >= 0 );
if (rows > 0) if ( rows > 0 )
Q_ASSERT(model->hasChildren(topIndex) == true); QVERIFY( model->hasChildren ( topIndex ) );
QModelIndex secondLevelIndex = model->index(0, 0, topIndex); QModelIndex secondLevelIndex = model->index ( 0, 0, topIndex );
if (secondLevelIndex.isValid()) { // not the top level if ( secondLevelIndex.isValid() ) { // not the top level
// check a row count where parent is valid // check a row count where parent is valid
rows = model->rowCount(secondLevelIndex); rows = model->rowCount ( secondLevelIndex );
Q_ASSERT(rows >= 0); QVERIFY( rows >= 0 );
if (rows > 0) if ( rows > 0 )
Q_ASSERT(model->hasChildren(secondLevelIndex) == true); QVERIFY( model->hasChildren ( secondLevelIndex ) );
} }
// The models rowCount() is tested more extensively in checkChildren(), // The models rowCount() is tested more extensively in checkChildren(),
@ -159,13 +180,13 @@ void ModelTest::rowCount()
void ModelTest::columnCount() void ModelTest::columnCount()
{ {
// check top row // check top row
QModelIndex topIndex = model->index(0, 0, QModelIndex()); QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
Q_ASSERT(model->columnCount(topIndex) >= 0); QVERIFY( model->columnCount ( topIndex ) >= 0 );
// check a column count where parent is valid // check a column count where parent is valid
QModelIndex childIndex = model->index(0, 0, topIndex); QModelIndex childIndex = model->index ( 0, 0, topIndex );
if (childIndex.isValid()) if ( childIndex.isValid() )
Q_ASSERT(model->columnCount(childIndex) >= 0); QVERIFY( model->columnCount ( childIndex ) >= 0 );
// columnCount() is tested more extensively in checkChildren(), // columnCount() is tested more extensively in checkChildren(),
// but this catches the big mistakes // but this catches the big mistakes
@ -176,20 +197,21 @@ void ModelTest::columnCount()
*/ */
void ModelTest::hasIndex() void ModelTest::hasIndex()
{ {
// qDebug() << "hi";
// Make sure that invalid values returns an invalid index // Make sure that invalid values returns an invalid index
Q_ASSERT(model->hasIndex(-2, -2) == false); QVERIFY( !model->hasIndex ( -2, -2 ) );
Q_ASSERT(model->hasIndex(-2, 0) == false); QVERIFY( !model->hasIndex ( -2, 0 ) );
Q_ASSERT(model->hasIndex(0, -2) == false); QVERIFY( !model->hasIndex ( 0, -2 ) );
int rows = model->rowCount(); int rows = model->rowCount();
int columns = model->columnCount(); int columns = model->columnCount();
// check out of bounds // check out of bounds
Q_ASSERT(model->hasIndex(rows, columns) == false); QVERIFY( !model->hasIndex ( rows, columns ) );
Q_ASSERT(model->hasIndex(rows + 1, columns + 1) == false); QVERIFY( !model->hasIndex ( rows + 1, columns + 1 ) );
if (rows > 0) if ( rows > 0 )
Q_ASSERT(model->hasIndex(0, 0) == true); QVERIFY( model->hasIndex ( 0, 0 ) );
// hasIndex() is tested more extensively in checkChildren(), // hasIndex() is tested more extensively in checkChildren(),
// but this catches the big mistakes // but this catches the big mistakes
@ -200,25 +222,26 @@ void ModelTest::hasIndex()
*/ */
void ModelTest::index() void ModelTest::index()
{ {
// qDebug() << "i";
// Make sure that invalid values returns an invalid index // Make sure that invalid values returns an invalid index
Q_ASSERT(model->index(-2, -2) == QModelIndex()); QVERIFY( model->index ( -2, -2 ) == QModelIndex() );
Q_ASSERT(model->index(-2, 0) == QModelIndex()); QVERIFY( model->index ( -2, 0 ) == QModelIndex() );
Q_ASSERT(model->index(0, -2) == QModelIndex()); QVERIFY( model->index ( 0, -2 ) == QModelIndex() );
int rows = model->rowCount(); int rows = model->rowCount();
int columns = model->columnCount(); int columns = model->columnCount();
if (rows == 0) if ( rows == 0 )
return; return;
// Catch off by one errors // Catch off by one errors
Q_ASSERT(model->index(rows, columns) == QModelIndex()); QVERIFY( model->index ( rows, columns ) == QModelIndex() );
Q_ASSERT(model->index(0, 0).isValid() == true); QVERIFY( model->index ( 0, 0 ).isValid() );
// Make sure that the same index is *always* returned // Make sure that the same index is *always* returned
QModelIndex a = model->index(0, 0); QModelIndex a = model->index ( 0, 0 );
QModelIndex b = model->index(0, 0); QModelIndex b = model->index ( 0, 0 );
Q_ASSERT(a == b); QVERIFY( a == b );
// index() is tested more extensively in checkChildren(), // index() is tested more extensively in checkChildren(),
// but this catches the big mistakes // but this catches the big mistakes
@ -229,11 +252,12 @@ void ModelTest::index()
*/ */
void ModelTest::parent() void ModelTest::parent()
{ {
// qDebug() << "p";
// Make sure the model wont crash and will return an invalid QModelIndex // Make sure the model wont crash and will return an invalid QModelIndex
// when asked for the parent of an invalid index. // when asked for the parent of an invalid index.
Q_ASSERT(model->parent(QModelIndex()) == QModelIndex()); QVERIFY( model->parent ( QModelIndex() ) == QModelIndex() );
if (model->rowCount() == 0) if ( model->rowCount() == 0 )
return; return;
// Column 0 | Column 1 | // Column 0 | Column 1 |
@ -243,29 +267,29 @@ void ModelTest::parent()
// Common error test #1, make sure that a top level index has a parent // Common error test #1, make sure that a top level index has a parent
// that is a invalid QModelIndex. // that is a invalid QModelIndex.
QModelIndex topIndex = model->index(0, 0, QModelIndex()); QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
Q_ASSERT(model->parent(topIndex) == QModelIndex()); QVERIFY( model->parent ( topIndex ) == QModelIndex() );
// Common error test #2, make sure that a second level index has a parent // Common error test #2, make sure that a second level index has a parent
// that is the first level index. // that is the first level index.
if (model->rowCount(topIndex) > 0) { if ( model->rowCount ( topIndex ) > 0 ) {
QModelIndex childIndex = model->index(0, 0, topIndex); QModelIndex childIndex = model->index ( 0, 0, topIndex );
Q_ASSERT(model->parent(childIndex) == topIndex); QVERIFY( model->parent ( childIndex ) == topIndex );
} }
// Common error test #3, the second column should NOT have the same children // Common error test #3, the second column should NOT have the same children
// as the first column in a row. // as the first column in a row.
// Usually the second column shouldn't have children. // Usually the second column shouldn't have children.
QModelIndex topIndex1 = model->index(0, 1, QModelIndex()); QModelIndex topIndex1 = model->index ( 0, 1, QModelIndex() );
if (model->rowCount(topIndex1) > 0) { if ( model->rowCount ( topIndex1 ) > 0 ) {
QModelIndex childIndex = model->index(0, 0, topIndex); QModelIndex childIndex = model->index ( 0, 0, topIndex );
QModelIndex childIndex1 = model->index(0, 0, topIndex1); QModelIndex childIndex1 = model->index ( 0, 0, topIndex1 );
Q_ASSERT(childIndex != childIndex1); QVERIFY( childIndex != childIndex1 );
} }
// Full test, walk n levels deep through the model making sure that all // Full test, walk n levels deep through the model making sure that all
// parent's children correctly specify their parent. // parent's children correctly specify their parent.
checkChildren(QModelIndex()); checkChildren ( QModelIndex() );
} }
/*! /*!
@ -282,94 +306,90 @@ void ModelTest::parent()
found the basic bugs because it is easier to figure out the problem in found the basic bugs because it is easier to figure out the problem in
those tests then this one. those tests then this one.
*/ */
void ModelTest::checkChildren(const QModelIndex &parent, int currentDepth) void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
{ {
// First just try walking back up the tree. // First just try walking back up the tree.
QModelIndex p = parent; QModelIndex p = parent;
while (p.isValid()) while ( p.isValid() )
p = p.parent(); p = p.parent();
// For models that are dynamically populated // For models that are dynamically populated
if (model->canFetchMore(parent)) { if ( model->canFetchMore ( parent ) ) {
fetchingMore = true; fetchingMore = true;
model->fetchMore(parent); model->fetchMore ( parent );
fetchingMore = false; fetchingMore = false;
} }
int rows = model->rowCount(parent); int rows = model->rowCount ( parent );
int columns = model->columnCount(parent); int columns = model->columnCount ( parent );
if (rows > 0) if ( rows > 0 )
Q_ASSERT(model->hasChildren(parent)); QVERIFY( model->hasChildren ( parent ) );
// Some further testing against rows(), columns(), and hasChildren() // Some further testing against rows(), columns(), and hasChildren()
Q_ASSERT(rows >= 0); QVERIFY( rows >= 0 );
Q_ASSERT(columns >= 0); QVERIFY( columns >= 0 );
if (rows > 0) if ( rows > 0 )
Q_ASSERT(model->hasChildren(parent) == true); QVERIFY( model->hasChildren ( parent ) );
//qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows //qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows
// << "columns:" << columns << "parent column:" << parent.column(); // << "columns:" << columns << "parent column:" << parent.column();
Q_ASSERT(model->hasIndex(rows + 1, 0, parent) == false); QVERIFY( !model->hasIndex ( rows + 1, 0, parent ) );
for (int r = 0; r < rows; ++r) { for ( int r = 0; r < rows; ++r ) {
if (model->canFetchMore(parent)) { if ( model->canFetchMore ( parent ) ) {
fetchingMore = true; fetchingMore = true;
model->fetchMore(parent); model->fetchMore ( parent );
fetchingMore = false; fetchingMore = false;
} }
Q_ASSERT(model->hasIndex(r, columns + 1, parent) == false); QVERIFY( !model->hasIndex ( r, columns + 1, parent ) );
for (int c = 0; c < columns; ++c) { for ( int c = 0; c < columns; ++c ) {
Q_ASSERT(model->hasIndex(r, c, parent) == true); QVERIFY( model->hasIndex ( r, c, parent ) );
QModelIndex index = model->index(r, c, parent); QModelIndex index = model->index ( r, c, parent );
// rowCount() and columnCount() said that it existed... // rowCount() and columnCount() said that it existed...
Q_ASSERT(index.isValid() == true); QVERIFY( index.isValid() );
// index() should always return the same index when called twice in a row // index() should always return the same index when called twice in a row
QModelIndex modifiedIndex = model->index(r, c, parent); QModelIndex modifiedIndex = model->index ( r, c, parent );
Q_ASSERT(index == modifiedIndex); QVERIFY( index == modifiedIndex );
// Make sure we get the same index if we request it twice in a row // Make sure we get the same index if we request it twice in a row
QModelIndex a = model->index(r, c, parent); QModelIndex a = model->index ( r, c, parent );
QModelIndex b = model->index(r, c, parent); QModelIndex b = model->index ( r, c, parent );
Q_ASSERT(a == b); QVERIFY( a == b );
// Some basic checking on the index that is returned // Some basic checking on the index that is returned
Q_ASSERT(index.model() == model); QVERIFY( index.model() == model );
Q_ASSERT(index.row() == r); QCOMPARE( index.row(), r );
Q_ASSERT(index.column() == c); QCOMPARE( index.column(), c );
// While you can technically return a QVariant usually this is a sign // While you can technically return a QVariant usually this is a sign
// of an bug in data() Disable if this really is ok in your model. // of a bug in data(). Disable if this really is ok in your model.
//Q_ASSERT(model->data(index, Qt::DisplayRole).isValid() == true); // QVERIFY( model->data ( index, Qt::DisplayRole ).isValid() );
// If the next test fails here is some somewhat useful debug you play with. // If the next test fails here is some somewhat useful debug you play with.
/*
if (model->parent(index) != parent) { if (model->parent(index) != parent) {
qDebug() << r << c << currentDepth << model->data(index).toString() qDebug() << r << c << currentDepth << model->data(index).toString()
<< model->data(parent).toString(); << model->data(parent).toString();
qDebug() << index << parent << model->parent(index); qDebug() << index << parent << model->parent(index);
// And a view that you can even use to show the model. // And a view that you can even use to show the model.
//QTreeView view; // QTreeView view;
//view.setModel(model); // view.setModel(model);
//view.show(); // view.show();
}*/ }
// Check that we can get back our real parent. // Check that we can get back our real parent.
QModelIndex p = model->parent(index); QCOMPARE( model->parent ( index ), parent );
//qDebug() << "child:" << index;
//qDebug() << p;
//qDebug() << parent;
Q_ASSERT(model->parent(index) == parent);
// recursively go down the children // recursively go down the children
if (model->hasChildren(index) && currentDepth < 10 ) { if ( model->hasChildren ( index ) && currentDepth < 10 ) {
//qDebug() << r << c << "has children" << model->rowCount(index); //qDebug() << r << c << "has children" << model->rowCount(index);
checkChildren(index, ++currentDepth); checkChildren ( index, ++currentDepth );
}/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/ }/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/
// make sure that after testing the children that the index doesn't change. // make sure that after testing the children that the index doesn't change.
QModelIndex newerIndex = model->index(r, c, parent); QModelIndex newerIndex = model->index ( r, c, parent );
Q_ASSERT(index == newerIndex); QVERIFY( index == newerIndex );
} }
} }
} }
@ -380,78 +400,68 @@ void ModelTest::checkChildren(const QModelIndex &parent, int currentDepth)
void ModelTest::data() void ModelTest::data()
{ {
// Invalid index should return an invalid qvariant // Invalid index should return an invalid qvariant
Q_ASSERT(!model->data(QModelIndex()).isValid()); QVERIFY( !model->data ( QModelIndex() ).isValid() );
if (model->rowCount() == 0) if ( model->rowCount() == 0 )
return; return;
// A valid index should have a valid QVariant data // A valid index should have a valid QVariant data
Q_ASSERT(model->index(0, 0).isValid()); QVERIFY( model->index ( 0, 0 ).isValid() );
// shouldn't be able to set data on an invalid index // shouldn't be able to set data on an invalid index
Q_ASSERT(model->setData(QModelIndex(), QLatin1String("foo"), Qt::DisplayRole) == false); QVERIFY( !model->setData ( QModelIndex(), QLatin1String ( "foo" ), Qt::DisplayRole ) );
// General Purpose roles that should return a QString // General Purpose roles that should return a QString
QVariant variant = model->data(model->index(0, 0), Qt::ToolTipRole); QVariant variant = model->data ( model->index ( 0, 0 ), Qt::ToolTipRole );
if (variant.isValid()) { if ( variant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QString>(variant)); QVERIFY( qVariantCanConvert<QString> ( variant ) );
} }
variant = model->data(model->index(0, 0), Qt::StatusTipRole); variant = model->data ( model->index ( 0, 0 ), Qt::StatusTipRole );
if (variant.isValid()) { if ( variant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QString>(variant)); QVERIFY( qVariantCanConvert<QString> ( variant ) );
} }
variant = model->data(model->index(0, 0), Qt::WhatsThisRole); variant = model->data ( model->index ( 0, 0 ), Qt::WhatsThisRole );
if (variant.isValid()) { if ( variant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QString>(variant)); QVERIFY( qVariantCanConvert<QString> ( variant ) );
} }
// General Purpose roles that should return a QSize // General Purpose roles that should return a QSize
variant = model->data(model->index(0, 0), Qt::SizeHintRole); variant = model->data ( model->index ( 0, 0 ), Qt::SizeHintRole );
if (variant.isValid()) { if ( variant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QSize>(variant)); QVERIFY( qVariantCanConvert<QSize> ( variant ) );
} }
// General Purpose roles that should return a QFont // General Purpose roles that should return a QFont
QVariant fontVariant = model->data(model->index(0, 0), Qt::FontRole); QVariant fontVariant = model->data ( model->index ( 0, 0 ), Qt::FontRole );
if (fontVariant.isValid()) { if ( fontVariant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QFont>(fontVariant)); QVERIFY( qVariantCanConvert<QFont> ( fontVariant ) );
} }
// Check that the alignment is one we know about // Check that the alignment is one we know about
QVariant textAlignmentVariant = model->data(model->index(0, 0), Qt::TextAlignmentRole); QVariant textAlignmentVariant = model->data ( model->index ( 0, 0 ), Qt::TextAlignmentRole );
if (textAlignmentVariant.isValid()) { if ( textAlignmentVariant.isValid() ) {
int alignment = textAlignmentVariant.toInt(); int alignment = textAlignmentVariant.toInt();
Q_ASSERT(alignment == Qt::AlignLeft || QCOMPARE( alignment, ( alignment & ( Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask ) ) );
alignment == Qt::AlignRight ||
alignment == Qt::AlignHCenter ||
alignment == Qt::AlignJustify ||
alignment == Qt::AlignTop ||
alignment == Qt::AlignBottom ||
alignment == Qt::AlignVCenter ||
alignment == Qt::AlignCenter ||
alignment == Qt::AlignAbsolute ||
alignment == Qt::AlignLeading ||
alignment == Qt::AlignTrailing);
} }
// General Purpose roles that should return a QColor // General Purpose roles that should return a QColor
QVariant colorVariant = model->data(model->index(0, 0), Qt::BackgroundColorRole); QVariant colorVariant = model->data ( model->index ( 0, 0 ), Qt::BackgroundColorRole );
if (colorVariant.isValid()) { if ( colorVariant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QColor>(colorVariant)); QVERIFY( qVariantCanConvert<QColor> ( colorVariant ) );
} }
colorVariant = model->data(model->index(0, 0), Qt::TextColorRole); colorVariant = model->data ( model->index ( 0, 0 ), Qt::TextColorRole );
if (colorVariant.isValid()) { if ( colorVariant.isValid() ) {
Q_ASSERT(qVariantCanConvert<QColor>(colorVariant)); QVERIFY( qVariantCanConvert<QColor> ( colorVariant ) );
} }
// Check that the "check state" is one we know about. // Check that the "check state" is one we know about.
QVariant checkStateVariant = model->data(model->index(0, 0), Qt::CheckStateRole); QVariant checkStateVariant = model->data ( model->index ( 0, 0 ), Qt::CheckStateRole );
if (checkStateVariant.isValid()) { if ( checkStateVariant.isValid() ) {
int state = checkStateVariant.toInt(); int state = checkStateVariant.toInt();
Q_ASSERT(state == Qt::Unchecked || QVERIFY( state == Qt::Unchecked ||
state == Qt::PartiallyChecked || state == Qt::PartiallyChecked ||
state == Qt::Checked); state == Qt::Checked );
} }
} }
@ -460,15 +470,18 @@ void ModelTest::data()
\sa rowsInserted() \sa rowsInserted()
*/ */
void ModelTest::rowsAboutToBeInserted(const QModelIndex &parent, int start, int end) void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int end )
{ {
Q_UNUSED(end); Q_UNUSED(end);
// qDebug() << "rowsAboutToBeInserted" << "start=" << start << "end=" << end << "parent=" << model->data ( parent ).toString()
// << "current count of parent=" << model->rowCount ( parent ); // << "display of last=" << model->data( model->index(start-1, 0, parent) );
// qDebug() << model->index(start-1, 0, parent) << model->data( model->index(start-1, 0, parent) );
Changing c; Changing c;
c.parent = parent; c.parent = parent;
c.oldSize = model->rowCount(parent); c.oldSize = model->rowCount ( parent );
c.last = model->data(model->index(start - 1, 0, parent)); c.last = model->data ( model->index ( start - 1, 0, parent ) );
c.next = model->data(model->index(start, 0, parent)); c.next = model->data ( model->index ( start, 0, parent ) );
insert.push(c); insert.push ( c );
} }
/*! /*!
@ -476,34 +489,43 @@ void ModelTest::rowsAboutToBeInserted(const QModelIndex &parent, int start, int
\sa rowsAboutToBeInserted() \sa rowsAboutToBeInserted()
*/ */
void ModelTest::rowsInserted(const QModelIndex & parent, int start, int end) void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end )
{ {
Changing c = insert.pop(); Changing c = insert.pop();
Q_ASSERT(c.parent == parent); QVERIFY( c.parent == parent );
Q_ASSERT(c.oldSize + (end - start + 1) == model->rowCount(parent)); // qDebug() << "rowsInserted" << "start=" << start << "end=" << end << "oldsize=" << c.oldSize
Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent))); // << "parent=" << model->data ( parent ).toString() << "current rowcount of parent=" << model->rowCount ( parent );
/*
// for (int ii=start; ii <= end; ii++)
// {
// qDebug() << "itemWasInserted:" << ii << model->data ( model->index ( ii, 0, parent ));
// }
// qDebug();
QVERIFY( c.oldSize + ( end - start + 1 ) == model->rowCount ( parent ) );
QVERIFY( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
if (c.next != model->data(model->index(end + 1, 0, c.parent))) { if (c.next != model->data(model->index(end + 1, 0, c.parent))) {
qDebug() << start << end; qDebug() << start << end;
for (int i=0; i < model->rowCount(); ++i) for (int i=0; i < model->rowCount(); ++i)
qDebug() << model->index(i, 0).data().toString(); qDebug() << model->index(i, 0).data().toString();
qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent)); qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent));
} }
*/
Q_ASSERT(c.next == model->data(model->index(end + 1, 0, c.parent))); QVERIFY( c.next == model->data ( model->index ( end + 1, 0, c.parent ) ) );
} }
void ModelTest::layoutAboutToBeChanged() void ModelTest::layoutAboutToBeChanged()
{ {
for (int i = 0; i < qBound(0, model->rowCount(), 100); ++i) for ( int i = 0; i < qBound ( 0, model->rowCount(), 100 ); ++i )
changing.append(QPersistentModelIndex(model->index(i, 0))); changing.append ( QPersistentModelIndex ( model->index ( i, 0 ) ) );
} }
void ModelTest::layoutChanged() void ModelTest::layoutChanged()
{ {
for (int i = 0; i < changing.count(); ++i) { for ( int i = 0; i < changing.count(); ++i ) {
QPersistentModelIndex p = changing[i]; QPersistentModelIndex p = changing[i];
Q_ASSERT(p == model->index(p.row(), p.column(), p.parent())); QVERIFY( p == model->index ( p.row(), p.column(), p.parent() ) );
} }
changing.clear(); changing.clear();
} }
@ -513,14 +535,15 @@ void ModelTest::layoutChanged()
\sa rowsRemoved() \sa rowsRemoved()
*/ */
void ModelTest::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) void ModelTest::rowsAboutToBeRemoved ( const QModelIndex &parent, int start, int end )
{ {
qDebug() << "ratbr" << parent << start << end;
Changing c; Changing c;
c.parent = parent; c.parent = parent;
c.oldSize = model->rowCount(parent); c.oldSize = model->rowCount ( parent );
c.last = model->data(model->index(start - 1, 0, parent)); c.last = model->data ( model->index ( start - 1, 0, parent ) );
c.next = model->data(model->index(end + 1, 0, parent)); c.next = model->data ( model->index ( end + 1, 0, parent ) );
remove.push(c); remove.push ( c );
} }
/*! /*!
@ -528,12 +551,12 @@ void ModelTest::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int e
\sa rowsAboutToBeRemoved() \sa rowsAboutToBeRemoved()
*/ */
void ModelTest::rowsRemoved(const QModelIndex & parent, int start, int end) void ModelTest::rowsRemoved ( const QModelIndex & parent, int start, int end )
{ {
qDebug() << "rr" << parent << start << end;
Changing c = remove.pop(); Changing c = remove.pop();
Q_ASSERT(c.parent == parent); QVERIFY( c.parent == parent );
Q_ASSERT(c.oldSize - (end - start + 1) == model->rowCount(parent)); QVERIFY( c.oldSize - ( end - start + 1 ) == model->rowCount ( parent ) );
Q_ASSERT(c.last == model->data(model->index(start - 1, 0, c.parent))); QVERIFY( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
Q_ASSERT(c.next == model->data(model->index(start, 0, c.parent))); QVERIFY( c.next == model->data ( model->index ( start, 0, c.parent ) ) );
} }

View File

@ -1,26 +1,45 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2007 Trolltech ASA. All rights reserved. ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
** **
** This file is part of the Qt Concurrent project on Trolltech Labs. ** This file is part of the test suite of the Qt Toolkit.
** **
** This file may be used under the terms of the GNU General Public ** $QT_BEGIN_LICENSE:LGPL$
** License version 2.0 as published by the Free Software Foundation ** GNU Lesser General Public License Usage
** and appearing in the file LICENSE.GPL-2 included in the packaging of ** This file may be used under the terms of the GNU Lesser General Public
** this file. Please review the following information to ensure GNU ** License version 2.1 as published by the Free Software Foundation and
** General Public Licensing requirements will be met: ** appearing in the file LICENSE.LGPL included in the packaging of this
** http://www.trolltech.com/products/qt/opensource.html ** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
** **
** If you are unsure which license is appropriate for your use, please ** In addition, as a special exception, Nokia gives you certain additional
** review the following information: ** rights. These rights are described in the Nokia Qt LGPL Exception
** http://www.trolltech.com/products/qt/licensing.html or contact the ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
** sales department at sales@trolltech.com.
** **
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** GNU General Public License Usage
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
** **
****************************************************************************/ ****************************************************************************/
#ifndef MODELTEST_H #ifndef MODELTEST_H
#define MODELTEST_H #define MODELTEST_H
@ -30,47 +49,46 @@
class ModelTest : public QObject class ModelTest : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
ModelTest(QAbstractItemModel *model, QObject *parent = 0); ModelTest( QAbstractItemModel *model, QObject *parent = 0 );
private Q_SLOTS: private Q_SLOTS:
void nonDestructiveBasicTest(); void nonDestructiveBasicTest();
void rowCount(); void rowCount();
void columnCount(); void columnCount();
void hasIndex(); void hasIndex();
void index(); void index();
void parent(); void parent();
void data(); void data();
protected Q_SLOTS: protected Q_SLOTS:
void runAllTests(); void runAllTests();
void layoutAboutToBeChanged(); void layoutAboutToBeChanged();
void layoutChanged(); void layoutChanged();
void rowsAboutToBeInserted(const QModelIndex &parent, int start, int end); void rowsAboutToBeInserted( const QModelIndex &parent, int start, int end );
void rowsInserted(const QModelIndex & parent, int start, int end); void rowsInserted( const QModelIndex & parent, int start, int end );
void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end); void rowsAboutToBeRemoved( const QModelIndex &parent, int start, int end );
void rowsRemoved(const QModelIndex & parent, int start, int end); void rowsRemoved( const QModelIndex & parent, int start, int end );
private: private:
void checkChildren(const QModelIndex &parent, int currentDepth = 0); void checkChildren( const QModelIndex &parent, int currentDepth = 0 );
QAbstractItemModel *model; QAbstractItemModel *model;
struct Changing struct Changing {
{ QModelIndex parent;
QModelIndex parent; int oldSize;
int oldSize; QVariant last;
QVariant last; QVariant next;
QVariant next; };
}; QStack<Changing> insert;
QStack<Changing> insert; QStack<Changing> remove;
QStack<Changing> remove;
bool fetchingMore; bool fetchingMore;
QList<QPersistentModelIndex> changing; QList<QPersistentModelIndex> changing;
}; };
#endif #endif