2010-08-15 06:31:48 -04:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2012-04-23 10:57:08 -04:00
|
|
|
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/
|
2010-08-15 06:31:48 -04:00
|
|
|
**
|
2011-12-25 14:53:33 -05:00
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
2010-08-15 06:31:48 -04:00
|
|
|
**
|
2011-12-25 14:53:33 -05:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this
|
|
|
|
** 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.
|
2010-08-15 06:31:48 -04:00
|
|
|
**
|
2011-12-25 14:53:33 -05:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
2010-08-15 06:31:48 -04:00
|
|
|
**
|
2011-12-25 14:53:33 -05:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** 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$
|
2010-08-15 06:31:48 -04:00
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
|
2010-08-15 06:31:48 -04:00
|
|
|
#include "modeltest.h"
|
|
|
|
|
2011-12-26 13:34:34 -05:00
|
|
|
#include <QtCore/QDebug>
|
|
|
|
#include <QtTest/QTest>
|
2011-12-25 14:53:33 -05:00
|
|
|
|
|
|
|
Q_DECLARE_METATYPE ( QModelIndex )
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Connect to all of the models signals. Whenever anything happens recheck everything.
|
|
|
|
*/
|
2011-12-25 14:53:33 -05:00
|
|
|
ModelTest::ModelTest ( QAbstractItemModel *_model, QObject *parent ) : QObject ( parent ), model ( _model ), fetchingMore ( false )
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
if (!model)
|
|
|
|
qFatal("%s: model must not be null", Q_FUNC_INFO);
|
|
|
|
|
|
|
|
connect ( model, SIGNAL ( columnsAboutToBeInserted ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( columnsAboutToBeRemoved ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( columnsInserted ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( columnsRemoved ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( dataChanged ( const QModelIndex &, const QModelIndex & ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( headerDataChanged ( Qt::Orientation, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( layoutAboutToBeChanged () ), this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( layoutChanged () ), this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( modelReset () ), this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( rowsAboutToBeInserted ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( rowsAboutToBeRemoved ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( rowsInserted ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
|
|
|
connect ( model, SIGNAL ( rowsRemoved ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( runAllTests() ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Special checks for inserting/removing
|
2011-12-25 14:53:33 -05:00
|
|
|
connect ( model, SIGNAL ( layoutAboutToBeChanged() ),
|
|
|
|
this, SLOT ( layoutAboutToBeChanged() ) );
|
|
|
|
connect ( model, SIGNAL ( layoutChanged() ),
|
|
|
|
this, SLOT ( layoutChanged() ) );
|
|
|
|
|
|
|
|
connect ( model, SIGNAL ( rowsAboutToBeInserted ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( rowsAboutToBeInserted ( const QModelIndex &, int, int ) ) );
|
|
|
|
connect ( model, SIGNAL ( rowsAboutToBeRemoved ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( rowsAboutToBeRemoved ( const QModelIndex &, int, int ) ) );
|
|
|
|
connect ( model, SIGNAL ( rowsInserted ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( rowsInserted ( const QModelIndex &, int, int ) ) );
|
|
|
|
connect ( model, SIGNAL ( rowsRemoved ( const QModelIndex &, int, int ) ),
|
|
|
|
this, SLOT ( rowsRemoved ( const QModelIndex &, int, int ) ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2012-04-23 10:57:08 -04:00
|
|
|
connect ( model, SIGNAL (rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
|
|
|
|
this, SLOT (rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)) );
|
|
|
|
connect ( model, SIGNAL (rowsMoved(QModelIndex,int,int,QModelIndex,int)),
|
|
|
|
this, SLOT (rowsMoved(QModelIndex,int,int,QModelIndex,int)) );
|
|
|
|
|
|
|
|
|
2010-08-15 06:31:48 -04:00
|
|
|
runAllTests();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelTest::runAllTests()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( fetchingMore )
|
2010-08-15 06:31:48 -04:00
|
|
|
return;
|
|
|
|
nonDestructiveBasicTest();
|
|
|
|
rowCount();
|
|
|
|
columnCount();
|
|
|
|
hasIndex();
|
|
|
|
index();
|
|
|
|
parent();
|
|
|
|
data();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
nonDestructiveBasicTest tries to call a number of the basic functions (not all)
|
|
|
|
to make sure the model doesn't outright segfault, testing the functions that makes sense.
|
|
|
|
*/
|
|
|
|
void ModelTest::nonDestructiveBasicTest()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( model->buddy ( QModelIndex() ) == QModelIndex() );
|
|
|
|
model->canFetchMore ( QModelIndex() );
|
|
|
|
QVERIFY( model->columnCount ( QModelIndex() ) >= 0 );
|
|
|
|
QVERIFY( model->data ( QModelIndex() ) == QVariant() );
|
2010-08-15 06:31:48 -04:00
|
|
|
fetchingMore = true;
|
2011-12-25 14:53:33 -05:00
|
|
|
model->fetchMore ( QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
fetchingMore = false;
|
2011-12-25 14:53:33 -05:00
|
|
|
Qt::ItemFlags flags = model->flags ( QModelIndex() );
|
|
|
|
QVERIFY( flags == Qt::ItemIsDropEnabled || flags == 0 );
|
|
|
|
model->hasChildren ( QModelIndex() );
|
|
|
|
model->hasIndex ( 0, 0 );
|
|
|
|
model->headerData ( 0, Qt::Horizontal );
|
|
|
|
model->index ( 0, 0 );
|
|
|
|
model->itemData ( QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
QVariant cache;
|
2011-12-25 14:53:33 -05:00
|
|
|
model->match ( QModelIndex(), -1, cache );
|
2010-08-15 06:31:48 -04:00
|
|
|
model->mimeTypes();
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( model->parent ( QModelIndex() ) == QModelIndex() );
|
|
|
|
QVERIFY( model->rowCount() >= 0 );
|
2010-08-15 06:31:48 -04:00
|
|
|
QVariant variant;
|
2011-12-25 14:53:33 -05:00
|
|
|
model->setData ( QModelIndex(), variant, -1 );
|
|
|
|
model->setHeaderData ( -1, Qt::Horizontal, QVariant() );
|
|
|
|
model->setHeaderData ( 999999, Qt::Horizontal, QVariant() );
|
2010-08-15 06:31:48 -04:00
|
|
|
QMap<int, QVariant> roles;
|
2011-12-25 14:53:33 -05:00
|
|
|
model->sibling ( 0, 0, QModelIndex() );
|
|
|
|
model->span ( QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
model->supportedDropActions();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tests model's implementation of QAbstractItemModel::rowCount() and hasChildren()
|
|
|
|
|
|
|
|
Models that are dynamically populated are not as fully tested here.
|
|
|
|
*/
|
|
|
|
void ModelTest::rowCount()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
// qDebug() << "rc";
|
2010-08-15 06:31:48 -04:00
|
|
|
// check top row
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
|
|
|
|
int rows = model->rowCount ( topIndex );
|
|
|
|
QVERIFY( rows >= 0 );
|
|
|
|
if ( rows > 0 )
|
|
|
|
QVERIFY( model->hasChildren ( topIndex ) );
|
|
|
|
|
|
|
|
QModelIndex secondLevelIndex = model->index ( 0, 0, topIndex );
|
|
|
|
if ( secondLevelIndex.isValid() ) { // not the top level
|
2010-08-15 06:31:48 -04:00
|
|
|
// check a row count where parent is valid
|
2011-12-25 14:53:33 -05:00
|
|
|
rows = model->rowCount ( secondLevelIndex );
|
|
|
|
QVERIFY( rows >= 0 );
|
|
|
|
if ( rows > 0 )
|
|
|
|
QVERIFY( model->hasChildren ( secondLevelIndex ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The models rowCount() is tested more extensively in checkChildren(),
|
|
|
|
// but this catches the big mistakes
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tests model's implementation of QAbstractItemModel::columnCount() and hasChildren()
|
|
|
|
*/
|
|
|
|
void ModelTest::columnCount()
|
|
|
|
{
|
|
|
|
// check top row
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
|
|
|
|
QVERIFY( model->columnCount ( topIndex ) >= 0 );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// check a column count where parent is valid
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex childIndex = model->index ( 0, 0, topIndex );
|
|
|
|
if ( childIndex.isValid() )
|
|
|
|
QVERIFY( model->columnCount ( childIndex ) >= 0 );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// columnCount() is tested more extensively in checkChildren(),
|
|
|
|
// but this catches the big mistakes
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tests model's implementation of QAbstractItemModel::hasIndex()
|
|
|
|
*/
|
|
|
|
void ModelTest::hasIndex()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
// qDebug() << "hi";
|
2010-08-15 06:31:48 -04:00
|
|
|
// Make sure that invalid values returns an invalid index
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( !model->hasIndex ( -2, -2 ) );
|
|
|
|
QVERIFY( !model->hasIndex ( -2, 0 ) );
|
|
|
|
QVERIFY( !model->hasIndex ( 0, -2 ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
int rows = model->rowCount();
|
|
|
|
int columns = model->columnCount();
|
|
|
|
|
|
|
|
// check out of bounds
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( !model->hasIndex ( rows, columns ) );
|
|
|
|
QVERIFY( !model->hasIndex ( rows + 1, columns + 1 ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( rows > 0 )
|
|
|
|
QVERIFY( model->hasIndex ( 0, 0 ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// hasIndex() is tested more extensively in checkChildren(),
|
|
|
|
// but this catches the big mistakes
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tests model's implementation of QAbstractItemModel::index()
|
|
|
|
*/
|
|
|
|
void ModelTest::index()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
// qDebug() << "i";
|
2010-08-15 06:31:48 -04:00
|
|
|
// Make sure that invalid values returns an invalid index
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( model->index ( -2, -2 ) == QModelIndex() );
|
|
|
|
QVERIFY( model->index ( -2, 0 ) == QModelIndex() );
|
|
|
|
QVERIFY( model->index ( 0, -2 ) == QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
int rows = model->rowCount();
|
|
|
|
int columns = model->columnCount();
|
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( rows == 0 )
|
2010-08-15 06:31:48 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Catch off by one errors
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( model->index ( rows, columns ) == QModelIndex() );
|
|
|
|
QVERIFY( model->index ( 0, 0 ).isValid() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Make sure that the same index is *always* returned
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex a = model->index ( 0, 0 );
|
|
|
|
QModelIndex b = model->index ( 0, 0 );
|
|
|
|
QVERIFY( a == b );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// index() is tested more extensively in checkChildren(),
|
|
|
|
// but this catches the big mistakes
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tests model's implementation of QAbstractItemModel::parent()
|
|
|
|
*/
|
|
|
|
void ModelTest::parent()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
// qDebug() << "p";
|
2010-08-15 06:31:48 -04:00
|
|
|
// Make sure the model wont crash and will return an invalid QModelIndex
|
|
|
|
// when asked for the parent of an invalid index.
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( model->parent ( QModelIndex() ) == QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( model->rowCount() == 0 )
|
2010-08-15 06:31:48 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Column 0 | Column 1 |
|
|
|
|
// QModelIndex() | |
|
|
|
|
// \- topIndex | topIndex1 |
|
|
|
|
// \- childIndex | childIndex1 |
|
|
|
|
|
|
|
|
// Common error test #1, make sure that a top level index has a parent
|
|
|
|
// that is a invalid QModelIndex.
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
|
|
|
|
QVERIFY( model->parent ( topIndex ) == QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Common error test #2, make sure that a second level index has a parent
|
|
|
|
// that is the first level index.
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( model->rowCount ( topIndex ) > 0 ) {
|
|
|
|
QModelIndex childIndex = model->index ( 0, 0, topIndex );
|
|
|
|
QVERIFY( model->parent ( childIndex ) == topIndex );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common error test #3, the second column should NOT have the same children
|
|
|
|
// as the first column in a row.
|
|
|
|
// Usually the second column shouldn't have children.
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex topIndex1 = model->index ( 0, 1, QModelIndex() );
|
|
|
|
if ( model->rowCount ( topIndex1 ) > 0 ) {
|
|
|
|
QModelIndex childIndex = model->index ( 0, 0, topIndex );
|
|
|
|
QModelIndex childIndex1 = model->index ( 0, 0, topIndex1 );
|
|
|
|
QVERIFY( childIndex != childIndex1 );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Full test, walk n levels deep through the model making sure that all
|
|
|
|
// parent's children correctly specify their parent.
|
2011-12-25 14:53:33 -05:00
|
|
|
checkChildren ( QModelIndex() );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Called from the parent() test.
|
|
|
|
|
|
|
|
A model that returns an index of parent X should also return X when asking
|
|
|
|
for the parent of the index.
|
|
|
|
|
|
|
|
This recursive function does pretty extensive testing on the whole model in an
|
|
|
|
effort to catch edge cases.
|
|
|
|
|
|
|
|
This function assumes that rowCount(), columnCount() and index() already work.
|
|
|
|
If they have a bug it will point it out, but the above tests should have already
|
|
|
|
found the basic bugs because it is easier to figure out the problem in
|
|
|
|
those tests then this one.
|
|
|
|
*/
|
2011-12-25 14:53:33 -05:00
|
|
|
void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
|
|
|
// First just try walking back up the tree.
|
|
|
|
QModelIndex p = parent;
|
2011-12-25 14:53:33 -05:00
|
|
|
while ( p.isValid() )
|
2010-08-15 06:31:48 -04:00
|
|
|
p = p.parent();
|
|
|
|
|
|
|
|
// For models that are dynamically populated
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( model->canFetchMore ( parent ) ) {
|
2010-08-15 06:31:48 -04:00
|
|
|
fetchingMore = true;
|
2011-12-25 14:53:33 -05:00
|
|
|
model->fetchMore ( parent );
|
2010-08-15 06:31:48 -04:00
|
|
|
fetchingMore = false;
|
|
|
|
}
|
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
int rows = model->rowCount ( parent );
|
|
|
|
int columns = model->columnCount ( parent );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( rows > 0 )
|
|
|
|
QVERIFY( model->hasChildren ( parent ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Some further testing against rows(), columns(), and hasChildren()
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( rows >= 0 );
|
|
|
|
QVERIFY( columns >= 0 );
|
|
|
|
if ( rows > 0 )
|
|
|
|
QVERIFY( model->hasChildren ( parent ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
//qDebug() << "parent:" << model->data(parent).toString() << "rows:" << rows
|
|
|
|
// << "columns:" << columns << "parent column:" << parent.column();
|
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( !model->hasIndex ( rows + 1, 0, parent ) );
|
|
|
|
for ( int r = 0; r < rows; ++r ) {
|
|
|
|
if ( model->canFetchMore ( parent ) ) {
|
2010-08-15 06:31:48 -04:00
|
|
|
fetchingMore = true;
|
2011-12-25 14:53:33 -05:00
|
|
|
model->fetchMore ( parent );
|
2010-08-15 06:31:48 -04:00
|
|
|
fetchingMore = false;
|
|
|
|
}
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( !model->hasIndex ( r, columns + 1, parent ) );
|
|
|
|
for ( int c = 0; c < columns; ++c ) {
|
|
|
|
QVERIFY( model->hasIndex ( r, c, parent ) );
|
|
|
|
QModelIndex index = model->index ( r, c, parent );
|
2010-08-15 06:31:48 -04:00
|
|
|
// rowCount() and columnCount() said that it existed...
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( index.isValid() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// index() should always return the same index when called twice in a row
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex modifiedIndex = model->index ( r, c, parent );
|
|
|
|
QVERIFY( index == modifiedIndex );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Make sure we get the same index if we request it twice in a row
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex a = model->index ( r, c, parent );
|
|
|
|
QModelIndex b = model->index ( r, c, parent );
|
|
|
|
QVERIFY( a == b );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Some basic checking on the index that is returned
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( index.model() == model );
|
|
|
|
QCOMPARE( index.row(), r );
|
|
|
|
QCOMPARE( index.column(), c );
|
2010-08-15 06:31:48 -04:00
|
|
|
// While you can technically return a QVariant usually this is a sign
|
2011-12-25 14:53:33 -05:00
|
|
|
// of a bug in data(). Disable if this really is ok in your model.
|
|
|
|
// QVERIFY( model->data ( index, Qt::DisplayRole ).isValid() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// If the next test fails here is some somewhat useful debug you play with.
|
2011-12-25 14:53:33 -05:00
|
|
|
|
2010-08-15 06:31:48 -04:00
|
|
|
if (model->parent(index) != parent) {
|
|
|
|
qDebug() << r << c << currentDepth << model->data(index).toString()
|
|
|
|
<< model->data(parent).toString();
|
|
|
|
qDebug() << index << parent << model->parent(index);
|
2011-12-25 14:53:33 -05:00
|
|
|
// And a view that you can even use to show the model.
|
|
|
|
// QTreeView view;
|
|
|
|
// view.setModel(model);
|
|
|
|
// view.show();
|
|
|
|
}
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// Check that we can get back our real parent.
|
2011-12-25 14:53:33 -05:00
|
|
|
QCOMPARE( model->parent ( index ), parent );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// recursively go down the children
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( model->hasChildren ( index ) && currentDepth < 10 ) {
|
2010-08-15 06:31:48 -04:00
|
|
|
//qDebug() << r << c << "has children" << model->rowCount(index);
|
2011-12-25 14:53:33 -05:00
|
|
|
checkChildren ( index, ++currentDepth );
|
2010-08-15 06:31:48 -04:00
|
|
|
}/* else { if (currentDepth >= 10) qDebug() << "checked 10 deep"; };*/
|
|
|
|
|
|
|
|
// make sure that after testing the children that the index doesn't change.
|
2011-12-25 14:53:33 -05:00
|
|
|
QModelIndex newerIndex = model->index ( r, c, parent );
|
|
|
|
QVERIFY( index == newerIndex );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Tests model's implementation of QAbstractItemModel::data()
|
|
|
|
*/
|
|
|
|
void ModelTest::data()
|
|
|
|
{
|
|
|
|
// Invalid index should return an invalid qvariant
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( !model->data ( QModelIndex() ).isValid() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
if ( model->rowCount() == 0 )
|
2010-08-15 06:31:48 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
// A valid index should have a valid QVariant data
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( model->index ( 0, 0 ).isValid() );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// shouldn't be able to set data on an invalid index
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( !model->setData ( QModelIndex(), QLatin1String ( "foo" ), Qt::DisplayRole ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
|
|
|
|
// General Purpose roles that should return a QString
|
2011-12-25 14:53:33 -05:00
|
|
|
QVariant variant = model->data ( model->index ( 0, 0 ), Qt::ToolTipRole );
|
|
|
|
if ( variant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QString> ( variant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
2011-12-25 14:53:33 -05:00
|
|
|
variant = model->data ( model->index ( 0, 0 ), Qt::StatusTipRole );
|
|
|
|
if ( variant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QString> ( variant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
2011-12-25 14:53:33 -05:00
|
|
|
variant = model->data ( model->index ( 0, 0 ), Qt::WhatsThisRole );
|
|
|
|
if ( variant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QString> ( variant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// General Purpose roles that should return a QSize
|
2011-12-25 14:53:33 -05:00
|
|
|
variant = model->data ( model->index ( 0, 0 ), Qt::SizeHintRole );
|
|
|
|
if ( variant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QSize> ( variant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// General Purpose roles that should return a QFont
|
2011-12-25 14:53:33 -05:00
|
|
|
QVariant fontVariant = model->data ( model->index ( 0, 0 ), Qt::FontRole );
|
|
|
|
if ( fontVariant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QFont> ( fontVariant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the alignment is one we know about
|
2011-12-25 14:53:33 -05:00
|
|
|
QVariant textAlignmentVariant = model->data ( model->index ( 0, 0 ), Qt::TextAlignmentRole );
|
|
|
|
if ( textAlignmentVariant.isValid() ) {
|
2010-08-15 06:31:48 -04:00
|
|
|
int alignment = textAlignmentVariant.toInt();
|
2011-12-25 14:53:33 -05:00
|
|
|
QCOMPARE( alignment, ( alignment & ( Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask ) ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// General Purpose roles that should return a QColor
|
2011-12-25 14:53:33 -05:00
|
|
|
QVariant colorVariant = model->data ( model->index ( 0, 0 ), Qt::BackgroundColorRole );
|
|
|
|
if ( colorVariant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QColor> ( colorVariant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
2011-12-25 14:53:33 -05:00
|
|
|
colorVariant = model->data ( model->index ( 0, 0 ), Qt::TextColorRole );
|
|
|
|
if ( colorVariant.isValid() ) {
|
|
|
|
QVERIFY( qVariantCanConvert<QColor> ( colorVariant ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the "check state" is one we know about.
|
2011-12-25 14:53:33 -05:00
|
|
|
QVariant checkStateVariant = model->data ( model->index ( 0, 0 ), Qt::CheckStateRole );
|
|
|
|
if ( checkStateVariant.isValid() ) {
|
2010-08-15 06:31:48 -04:00
|
|
|
int state = checkStateVariant.toInt();
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( state == Qt::Unchecked ||
|
2010-08-15 06:31:48 -04:00
|
|
|
state == Qt::PartiallyChecked ||
|
2011-12-25 14:53:33 -05:00
|
|
|
state == Qt::Checked );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Store what is about to be inserted to make sure it actually happens
|
|
|
|
|
|
|
|
\sa rowsInserted()
|
|
|
|
*/
|
2011-12-25 14:53:33 -05:00
|
|
|
void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int end )
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
|
|
|
Q_UNUSED(end);
|
2011-12-25 14:53:33 -05:00
|
|
|
// 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) );
|
2010-08-15 06:31:48 -04:00
|
|
|
Changing c;
|
|
|
|
c.parent = parent;
|
2011-12-25 14:53:33 -05:00
|
|
|
c.oldSize = model->rowCount ( parent );
|
|
|
|
c.last = model->data ( model->index ( start - 1, 0, parent ) );
|
|
|
|
c.next = model->data ( model->index ( start, 0, parent ) );
|
|
|
|
insert.push ( c );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Confirm that what was said was going to happen actually did
|
|
|
|
|
|
|
|
\sa rowsAboutToBeInserted()
|
|
|
|
*/
|
2011-12-25 14:53:33 -05:00
|
|
|
void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end )
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
|
|
|
Changing c = insert.pop();
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( c.parent == parent );
|
|
|
|
// qDebug() << "rowsInserted" << "start=" << start << "end=" << end << "oldsize=" << c.oldSize
|
|
|
|
// << "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 ) ) );
|
|
|
|
|
2010-08-15 06:31:48 -04:00
|
|
|
if (c.next != model->data(model->index(end + 1, 0, c.parent))) {
|
|
|
|
qDebug() << start << end;
|
|
|
|
for (int i=0; i < model->rowCount(); ++i)
|
|
|
|
qDebug() << model->index(i, 0).data().toString();
|
|
|
|
qDebug() << c.next << model->data(model->index(end + 1, 0, c.parent));
|
|
|
|
}
|
2011-12-25 14:53:33 -05:00
|
|
|
|
|
|
|
QVERIFY( c.next == model->data ( model->index ( end + 1, 0, c.parent ) ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelTest::layoutAboutToBeChanged()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
for ( int i = 0; i < qBound ( 0, model->rowCount(), 100 ); ++i )
|
|
|
|
changing.append ( QPersistentModelIndex ( model->index ( i, 0 ) ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelTest::layoutChanged()
|
|
|
|
{
|
2011-12-25 14:53:33 -05:00
|
|
|
for ( int i = 0; i < changing.count(); ++i ) {
|
2010-08-15 06:31:48 -04:00
|
|
|
QPersistentModelIndex p = changing[i];
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( p == model->index ( p.row(), p.column(), p.parent() ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
changing.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Store what is about to be inserted to make sure it actually happens
|
|
|
|
|
|
|
|
\sa rowsRemoved()
|
|
|
|
*/
|
2011-12-25 14:53:33 -05:00
|
|
|
void ModelTest::rowsAboutToBeRemoved ( const QModelIndex &parent, int start, int end )
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
2012-04-14 11:55:19 -04:00
|
|
|
//qDebug() << "ratbr" << parent << start << end;
|
2010-08-15 06:31:48 -04:00
|
|
|
Changing c;
|
|
|
|
c.parent = parent;
|
2011-12-25 14:53:33 -05:00
|
|
|
c.oldSize = model->rowCount ( parent );
|
|
|
|
c.last = model->data ( model->index ( start - 1, 0, parent ) );
|
|
|
|
c.next = model->data ( model->index ( end + 1, 0, parent ) );
|
|
|
|
remove.push ( c );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Confirm that what was said was going to happen actually did
|
|
|
|
|
|
|
|
\sa rowsAboutToBeRemoved()
|
|
|
|
*/
|
2011-12-25 14:53:33 -05:00
|
|
|
void ModelTest::rowsRemoved ( const QModelIndex & parent, int start, int end )
|
2010-08-15 06:31:48 -04:00
|
|
|
{
|
2012-04-14 11:55:19 -04:00
|
|
|
// qDebug() << "rr" << parent << start << end;
|
2010-08-15 06:31:48 -04:00
|
|
|
Changing c = remove.pop();
|
2011-12-25 14:53:33 -05:00
|
|
|
QVERIFY( c.parent == parent );
|
|
|
|
QVERIFY( c.oldSize - ( end - start + 1 ) == model->rowCount ( parent ) );
|
|
|
|
QVERIFY( c.last == model->data ( model->index ( start - 1, 0, c.parent ) ) );
|
|
|
|
QVERIFY( c.next == model->data ( model->index ( start, 0, c.parent ) ) );
|
2010-08-15 06:31:48 -04:00
|
|
|
}
|
2012-04-23 10:57:08 -04:00
|
|
|
|
|
|
|
void ModelTest::rowsAboutToBeMoved( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow )
|
|
|
|
{
|
|
|
|
Changing cs;
|
|
|
|
cs.parent = srcParent;
|
|
|
|
cs.oldSize = model->rowCount ( srcParent );
|
|
|
|
cs.last = model->data ( model->index ( start - 1, 0, srcParent ) );
|
|
|
|
cs.next = model->data ( model->index ( end + 1, 0, srcParent ) );
|
|
|
|
remove.push ( cs );
|
|
|
|
Changing cd;
|
|
|
|
cd.parent = destParent;
|
|
|
|
cd.oldSize = model->rowCount ( destParent );
|
|
|
|
cd.last = model->data ( model->index ( destinationRow - 1, 0, destParent ) );
|
|
|
|
cd.next = model->data ( model->index ( destinationRow, 0, destParent ) );
|
|
|
|
insert.push ( cd );
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelTest::rowsMoved( const QModelIndex &srcParent, int start, int end, const QModelIndex &destParent, int destinationRow )
|
|
|
|
{
|
|
|
|
Changing cd = insert.pop();
|
|
|
|
Q_ASSERT ( cd.parent == destParent );
|
|
|
|
if (srcParent == destParent) {
|
|
|
|
Q_ASSERT ( cd.oldSize == model->rowCount ( destParent ) );
|
|
|
|
|
|
|
|
// TODO: Find out what I can assert here about last and next.
|
|
|
|
// Q_ASSERT ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
|
|
|
|
// Q_ASSERT ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Q_ASSERT ( cd.oldSize + ( end - start + 1 ) == model->rowCount ( destParent ) );
|
|
|
|
|
|
|
|
Q_ASSERT ( cd.last == model->data ( model->index ( destinationRow - 1, 0, cd.parent ) ) );
|
|
|
|
Q_ASSERT ( cd.next == model->data ( model->index ( destinationRow + (end - start + 1), 0, cd.parent ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
Changing cs = remove.pop();
|
|
|
|
Q_ASSERT ( cs.parent == srcParent );
|
|
|
|
if (srcParent == destParent) {
|
|
|
|
Q_ASSERT ( cs.oldSize == model->rowCount ( srcParent ) );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Q_ASSERT ( cs.oldSize - ( end - start + 1 ) == model->rowCount ( srcParent ) );
|
|
|
|
|
|
|
|
Q_ASSERT ( cs.last == model->data ( model->index ( start - 1, 0, srcParent ) ) );
|
|
|
|
Q_ASSERT ( cs.next == model->data ( model->index ( start, 0, srcParent ) ) );
|
|
|
|
}
|
|
|
|
}
|