diff options
author | Martin Jones <martin.jones@nokia.com> | 2010-12-23 03:13:36 (GMT) |
---|---|---|
committer | Martin Jones <martin.jones@nokia.com> | 2010-12-23 03:36:24 (GMT) |
commit | 16d08f97eaa7dd0469d7c9006546f86f1fd763f6 (patch) | |
tree | 3bc0dbbc9e97957ce669154d5fcc33de80f088cd /tests/auto | |
parent | 34630042ded25177b49f8e54b41269db1be42935 (diff) | |
download | Qt-16d08f97eaa7dd0469d7c9006546f86f1fd763f6.zip Qt-16d08f97eaa7dd0469d7c9006546f86f1fd763f6.tar.gz Qt-16d08f97eaa7dd0469d7c9006546f86f1fd763f6.tar.bz2 |
Models which load incrementally via fetchMore() don't work.
Call canFetchMore()/fetchMore() on setModel(), setRootIndex() and when
the last item is created.
Task-number: QTBUG-16039
Reviewed-by: Bea Lam
Diffstat (limited to 'tests/auto')
4 files changed, 187 insertions, 1 deletions
diff --git a/tests/auto/declarative/qdeclarativelistview/incrementalmodel.cpp b/tests/auto/declarative/qdeclarativelistview/incrementalmodel.cpp new file mode 100644 index 0000000..b2c9df5 --- /dev/null +++ b/tests/auto/declarative/qdeclarativelistview/incrementalmodel.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "incrementalmodel.h" +#include <QApplication> +#include <QDebug> + +IncrementalModel::IncrementalModel(QObject *parent) + : QAbstractListModel(parent), count(0) +{ + for (int i = 0; i < 100; ++i) + list.append("Item " + QString::number(i)); +} + +int IncrementalModel::rowCount(const QModelIndex & /* parent */) const +{ + return count; +} + +QVariant IncrementalModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() >= list.size() || index.row() < 0) + return QVariant(); + + if (role == Qt::DisplayRole) + return list.at(index.row()); + return QVariant(); +} + +bool IncrementalModel::canFetchMore(const QModelIndex & /* index */) const +{ + if (count < list.size()) + return true; + else + return false; +} + +void IncrementalModel::fetchMore(const QModelIndex & /* index */) +{ + int remainder = list.size() - count; + int itemsToFetch = qMin(5, remainder); + + beginInsertRows(QModelIndex(), count, count+itemsToFetch-1); + + count += itemsToFetch; + + endInsertRows(); +} diff --git a/tests/auto/declarative/qdeclarativelistview/incrementalmodel.h b/tests/auto/declarative/qdeclarativelistview/incrementalmodel.h new file mode 100644 index 0000000..b1f7407 --- /dev/null +++ b/tests/auto/declarative/qdeclarativelistview/incrementalmodel.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IncrementalModel_H +#define IncrementalModel_H + +#include <QAbstractListModel> +#include <QList> +#include <QStringList> + +class IncrementalModel : public QAbstractListModel +{ + Q_OBJECT + +public: + IncrementalModel(QObject *parent = 0); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + +protected: + bool canFetchMore(const QModelIndex &parent) const; + void fetchMore(const QModelIndex &parent); + +private: + QStringList list; + int count; +}; + +#endif diff --git a/tests/auto/declarative/qdeclarativelistview/qdeclarativelistview.pro b/tests/auto/declarative/qdeclarativelistview/qdeclarativelistview.pro index 2c5a859..8c99f08 100644 --- a/tests/auto/declarative/qdeclarativelistview/qdeclarativelistview.pro +++ b/tests/auto/declarative/qdeclarativelistview/qdeclarativelistview.pro @@ -2,7 +2,8 @@ load(qttest_p4) contains(QT_CONFIG,declarative): QT += declarative macx:CONFIG -= app_bundle -SOURCES += tst_qdeclarativelistview.cpp +HEADERS += incrementalmodel.h +SOURCES += tst_qdeclarativelistview.cpp incrementalmodel.cpp symbian: { importFiles.sources = data diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index dba0cc4..e76cb15 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -51,6 +51,7 @@ #include <QtDeclarative/private/qdeclarativelistmodel_p.h> #include <QtDeclarative/private/qlistmodelinterface_p.h> #include "../../../shared/util.h" +#include "incrementalmodel.h" #ifdef Q_OS_SYMBIAN // In Symbian OS test data is located in applications private dir @@ -106,6 +107,7 @@ private slots: void resizeDelegate(); void QTBUG_16037(); void indexAt(); + void incrementalModel(); private: template <class T> void items(); @@ -1998,6 +2000,32 @@ void tst_QDeclarativeListView::indexAt() delete canvas; } +void tst_QDeclarativeListView::incrementalModel() +{ + QDeclarativeView *canvas = createView(); + + IncrementalModel model; + QDeclarativeContext *ctxt = canvas->rootContext(); + ctxt->setContextProperty("testModel", &model); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/displaylist.qml")); + qApp->processEvents(); + + QDeclarativeListView *listview = findItem<QDeclarativeListView>(canvas->rootObject(), "list"); + QTRY_VERIFY(listview != 0); + + QDeclarativeItem *contentItem = listview->contentItem(); + QTRY_VERIFY(contentItem != 0); + + QTRY_COMPARE(listview->count(), 20); + + listview->positionViewAtIndex(10, QDeclarativeListView::Beginning); + + QTRY_COMPARE(listview->count(), 25); + + delete canvas; +} + void tst_QDeclarativeListView::qListModelInterface_items() { items<TestModel>(); |