**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "modeltest.h"
#include <QtCore/QtCore>
#include <QtTest/QtTest>
#if QT_VERSION < QT_VERSION_CHECK(5, 5, 0)
Q_DECLARE_METATYPE(Qt::Alignment)
#endif
Connect to all of the models signals. Whenever anything happens recheck everything.
*/
ModelTest::ModelTest ( QAbstractItemModel *_model, QObject *parent ) : QObject ( parent ), model ( _model ), fetchingMore ( false )
{
if (!model)
qFatal("%s: model must not be null", Q_FUNC_INFO);
connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(dataChanged(QModelIndex,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(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(runAllTests()) );
connect(model, SIGNAL(layoutAboutToBeChanged()),
this, SLOT(layoutAboutToBeChanged()) );
connect(model, SIGNAL(layoutChanged()),
this, SLOT(layoutChanged()) );
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
this, SLOT(rowsAboutToBeInserted(QModelIndex,int,int)) );
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(rowsAboutToBeRemoved(QModelIndex,int,int)) );
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(rowsInserted(QModelIndex,int,int)) );
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(rowsRemoved(QModelIndex,int,int)) );
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(dataChanged(QModelIndex,QModelIndex)) );
connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
this, SLOT(headerDataChanged(Qt::Orientation,int,int)) );
runAllTests();
}
void ModelTest::runAllTests()
{
if ( fetchingMore )
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()
{
QVERIFY(!model->buddy(QModelIndex()).isValid());
model->canFetchMore ( QModelIndex() );
QVERIFY( model->columnCount ( QModelIndex() ) >= 0 );
QCOMPARE(model->data(QModelIndex()), QVariant());
fetchingMore = true;
model->fetchMore ( QModelIndex() );
fetchingMore = false;
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() );
QVariant cache;
model->match ( QModelIndex(), -1, cache );
model->mimeTypes();
QVERIFY(!model->parent(QModelIndex()).isValid());
QVERIFY( model->rowCount() >= 0 );
QVariant variant;
model->setData ( QModelIndex(), variant, -1 );
model->setHeaderData ( -1, Qt::Horizontal, QVariant() );
model->setHeaderData ( 999999, Qt::Horizontal, QVariant() );
QMap<int, QVariant> roles;
model->sibling ( 0, 0, QModelIndex() );
model->span ( QModelIndex() );
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()
{
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() ) {
rows = model->rowCount ( secondLevelIndex );
QVERIFY( rows >= 0 );
if ( rows > 0 )
QVERIFY( model->hasChildren ( secondLevelIndex ) );
}
}
Tests model's implementation of QAbstractItemModel::columnCount() and hasChildren()
*/
void ModelTest::columnCount()
{
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
QVERIFY( model->columnCount ( topIndex ) >= 0 );
QModelIndex childIndex = model->index ( 0, 0, topIndex );
if ( childIndex.isValid() )
QVERIFY( model->columnCount ( childIndex ) >= 0 );
}
Tests model's implementation of QAbstractItemModel::hasIndex()
*/
void ModelTest::hasIndex()
{
QVERIFY( !model->hasIndex ( -2, -2 ) );
QVERIFY( !model->hasIndex ( -2, 0 ) );
QVERIFY( !model->hasIndex ( 0, -2 ) );
int rows = model->rowCount();
int columns = model->columnCount();
QVERIFY( !model->hasIndex ( rows, columns ) );
QVERIFY( !model->hasIndex ( rows + 1, columns + 1 ) );
if ( rows > 0 )
QVERIFY( model->hasIndex ( 0, 0 ) );
}
Tests model's implementation of QAbstractItemModel::index()
*/
void ModelTest::index()
{
QVERIFY(!model->index(-2, -2).isValid());
QVERIFY(!model->index(-2, 0).isValid());
QVERIFY(!model->index(0, -2).isValid());
int rows = model->rowCount();
int columns = model->columnCount();
if ( rows == 0 )
return;
QVERIFY(!model->index(rows, columns).isValid());
QVERIFY(model->index(0, 0).isValid());
QModelIndex a = model->index ( 0, 0 );
QModelIndex b = model->index ( 0, 0 );
QCOMPARE(a, b);
}
Tests model's implementation of QAbstractItemModel::parent()
*/
void ModelTest::parent()
{
QVERIFY(!model->parent(QModelIndex()).isValid());
if ( model->rowCount() == 0 )
return;
QModelIndex topIndex = model->index ( 0, 0, QModelIndex() );
QVERIFY(!model->parent(topIndex).isValid());
if ( model->rowCount ( topIndex ) > 0 ) {
QModelIndex childIndex = model->index ( 0, 0, topIndex );
QCOMPARE(model->parent(childIndex), topIndex);
}
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 );
}
checkChildren ( QModelIndex() );
}
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.
*/
void ModelTest::checkChildren ( const QModelIndex &parent, int currentDepth )
{
QModelIndex p = parent;
while ( p.isValid() )
p = p.parent();
if ( model->canFetchMore ( parent ) ) {
fetchingMore = true;
model->fetchMore ( parent );
fetchingMore = false;
}
int rows = model->rowCount ( parent );
int columns = model->columnCount ( parent );
if ( rows > 0 )
QVERIFY( model->hasChildren ( parent ) );
QVERIFY( rows >= 0 );
QVERIFY( columns >= 0 );
if ( rows > 0 )
QVERIFY( model->hasChildren ( parent ) );
const QModelIndex topLeftChild = model->index( 0, 0, parent );
QVERIFY( !model->hasIndex ( rows + 1, 0, parent ) );
for ( int r = 0; r < rows; ++r ) {
if ( model->canFetchMore ( parent ) ) {
fetchingMore = true;
model->fetchMore ( parent );
fetchingMore = false;
}
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 );
QVERIFY(index.isValid());
QModelIndex modifiedIndex = model->index ( r, c, parent );
QCOMPARE(index, modifiedIndex);
QModelIndex a = model->index ( r, c, parent );
QModelIndex b = model->index ( r, c, parent );
QCOMPARE(a, b);
{
const QModelIndex sibling = model->sibling( r, c, topLeftChild );
QCOMPARE(index, sibling);
}
{
const QModelIndex sibling = topLeftChild.sibling( r, c );
QCOMPARE(index, sibling);
}
QCOMPARE(index.model(), model);
QCOMPARE( index.row(), r );
QCOMPARE( index.column(), c );
if (model->parent(index) != parent) {
qDebug() << r << c << currentDepth << model->data(index).toString()
<< model->data(parent).toString();
qDebug() << index << parent << model->parent(index);
}
QCOMPARE( model->parent ( index ), parent );
if ( model->hasChildren ( index ) && currentDepth < 10 ) {
checkChildren ( index, ++currentDepth );
}
QModelIndex newerIndex = model->index ( r, c, parent );
QCOMPARE(index, newerIndex);
}
}
}
Tests model's implementation of QAbstractItemModel::data()
*/
void ModelTest::data()
{
QVERIFY( !model->data ( QModelIndex() ).isValid() );
if ( model->rowCount() == 0 )
return;
QVERIFY( model->index ( 0, 0 ).isValid() );
QVERIFY( !model->setData ( QModelIndex(), QLatin1String ( "foo" ), Qt::DisplayRole ) );
QVariant variant = model->data ( model->index ( 0, 0 ), Qt::ToolTipRole );
if ( variant.isValid() ) {
QVERIFY( variant.canConvert<QString>() );
}
variant = model->data ( model->index ( 0, 0 ), Qt::StatusTipRole );
if ( variant.isValid() ) {
QVERIFY( variant.canConvert<QString>() );
}
variant = model->data ( model->index ( 0, 0 ), Qt::WhatsThisRole );
if ( variant.isValid() ) {
QVERIFY( variant.canConvert<QString>() );
}
variant = model->data ( model->index ( 0, 0 ), Qt::SizeHintRole );
if ( variant.isValid() ) {
QVERIFY( variant.canConvert<QSize>() );
}
QVariant fontVariant = model->data ( model->index ( 0, 0 ), Qt::FontRole );
if ( fontVariant.isValid() ) {
QVERIFY( fontVariant.canConvert<QFont>() );
}
QVariant textAlignmentVariant = model->data ( model->index ( 0, 0 ), Qt::TextAlignmentRole );
if ( textAlignmentVariant.isValid() ) {
Qt::Alignment alignment = textAlignmentVariant.value<Qt::Alignment>();
QCOMPARE( alignment, ( alignment & ( Qt::AlignHorizontal_Mask | Qt::AlignVertical_Mask ) ) );
}
QVariant colorVariant = model->data ( model->index ( 0, 0 ), Qt::BackgroundColorRole );
if ( colorVariant.isValid() ) {
QVERIFY( colorVariant.canConvert<QColor>() );
}
colorVariant = model->data ( model->index ( 0, 0 ), Qt::TextColorRole );
if ( colorVariant.isValid() ) {
QVERIFY( colorVariant.canConvert<QColor>() );
}
QVariant checkStateVariant = model->data ( model->index ( 0, 0 ), Qt::CheckStateRole );
if ( checkStateVariant.isValid() ) {
int state = checkStateVariant.toInt();
QVERIFY( state == Qt::Unchecked ||
state == Qt::PartiallyChecked ||
state == Qt::Checked );
}
}
Store what is about to be inserted to make sure it actually happens
\sa rowsInserted()
*/
void ModelTest::rowsAboutToBeInserted ( const QModelIndex &parent, int start, int )
{
Changing c;
c.parent = parent;
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 );
}
Confirm that what was said was going to happen actually did
\sa rowsAboutToBeInserted()
*/
void ModelTest::rowsInserted ( const QModelIndex & parent, int start, int end )
{
Changing c = insert.pop();
QCOMPARE(c.parent, parent);
QCOMPARE(c.oldSize + (end - start + 1), model->rowCount(parent));
QCOMPARE(c.last, model->data(model->index(start - 1, 0, c.parent)));
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));
}
QCOMPARE(c.next, model->data(model->index(end + 1, 0, c.parent)));
}
void ModelTest::layoutAboutToBeChanged()
{
for ( int i = 0; i < qBound ( 0, model->rowCount(), 100 ); ++i )
changing.append ( QPersistentModelIndex ( model->index ( i, 0 ) ) );
}
void ModelTest::layoutChanged()
{
for ( int i = 0; i < changing.count(); ++i ) {
QPersistentModelIndex p = changing[i];
QCOMPARE(QModelIndex(p), model->index(p.row(), p.column(), p.parent()));
}
changing.clear();
}
Store what is about to be inserted to make sure it actually happens
\sa rowsRemoved()
*/
void ModelTest::rowsAboutToBeRemoved ( const QModelIndex &parent, int start, int end )
{
qDebug() << "ratbr" << parent << start << end;
Changing c;
c.parent = parent;
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 );
}
Confirm that what was said was going to happen actually did
\sa rowsAboutToBeRemoved()
*/
void ModelTest::rowsRemoved ( const QModelIndex & parent, int start, int end )
{
qDebug() << "rr" << parent << start << end;
Changing c = remove.pop();
QCOMPARE(c.parent, parent);
QCOMPARE(c.oldSize - (end - start + 1), model->rowCount(parent));
QCOMPARE(c.last, model->data(model->index(start - 1, 0, c.parent)));
QCOMPARE(c.next, model->data(model->index(start, 0, c.parent)));
}
void ModelTest::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
QVERIFY(topLeft.isValid());
QVERIFY(bottomRight.isValid());
QModelIndex commonParent = bottomRight.parent();
QCOMPARE(topLeft.parent(), commonParent);
QVERIFY(topLeft.row() <= bottomRight.row());
QVERIFY(topLeft.column() <= bottomRight.column());
int rowCount = model->rowCount(commonParent);
int columnCount = model->columnCount(commonParent);
QVERIFY(bottomRight.row() < rowCount);
QVERIFY(bottomRight.column() < columnCount);
}
void ModelTest::headerDataChanged(Qt::Orientation orientation, int start, int end)
{
QVERIFY(start >= 0);
QVERIFY(end >= 0);
QVERIFY(start <= end);
int itemCount = orientation == Qt::Vertical ? model->rowCount() : model->columnCount();
QVERIFY(start < itemCount);
QVERIFY(end < itemCount);
}