From 778e1fd21f5a6c94656dfbb99a7613656d0569a1 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Thu, 25 Mar 2010 14:44:35 +1000 Subject: Fix where data() is called before xml is loaded. Also add status tests. --- src/declarative/util/qdeclarativexmllistmodel.cpp | 6 ++- .../tst_qdeclarativexmllistmodel.cpp | 43 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/declarative/util/qdeclarativexmllistmodel.cpp b/src/declarative/util/qdeclarativexmllistmodel.cpp index e148469..3e08854 100644 --- a/src/declarative/util/qdeclarativexmllistmodel.cpp +++ b/src/declarative/util/qdeclarativexmllistmodel.cpp @@ -532,7 +532,7 @@ QHash QDeclarativeXmlListModel::data(int index, const QList & for (int i = 0; i < roles.size(); ++i) { int role = roles.at(i); int roleIndex = d->roles.indexOf(role); - rv.insert(role, roleIndex == -1 ? QVariant() : d->data.at(roleIndex).at(index)); + rv.insert(role, roleIndex == -1 ? QVariant() : d->data.value(roleIndex).value(index)); } return rv; } @@ -541,7 +541,7 @@ QVariant QDeclarativeXmlListModel::data(int index, int role) const { Q_D(const QDeclarativeXmlListModel); int roleIndex = d->roles.indexOf(role); - return (roleIndex == -1) ? QVariant() : d->data.at(roleIndex).at(index); + return (roleIndex == -1) ? QVariant() : d->data.value(roleIndex).value(index); } /*! @@ -728,6 +728,8 @@ void QDeclarativeXmlListModel::componentComplete() Otherwise, items are only added if the model does not already contain items with matching key role values. + + \sa XmlRole::isKey */ void QDeclarativeXmlListModel::reload() { diff --git a/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp b/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp index 81cc922..e3aa5cc 100644 --- a/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp +++ b/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp @@ -69,6 +69,8 @@ private slots: void roles(); void roleErrors(); void uniqueRoleNames(); + void status(); + void data(); void reload(); void useKeys(); void useKeys_data(); @@ -247,6 +249,47 @@ void tst_qdeclarativexmllistmodel::uniqueRoleNames() delete model; } +void tst_qdeclarativexmllistmodel::status() +{ + QDeclarativeXmlListModel *model; + model = new QDeclarativeXmlListModel; + QCOMPARE(model->status(), QDeclarativeXmlListModel::Null); + + model->setXml(""); + QCOMPARE(model->status(), QDeclarativeXmlListModel::Ready); + delete model; + + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/model.qml")); + model = qobject_cast(component.create()); + QVERIFY(model != 0); + QCOMPARE(model->status(), QDeclarativeXmlListModel::Loading); + + QTRY_COMPARE(model->count(), 9); + QCOMPARE(model->status(), QDeclarativeXmlListModel::Ready); + + delete model; +} + +void tst_qdeclarativexmllistmodel::data() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/model.qml")); + QDeclarativeXmlListModel *model = qobject_cast(component.create()); + QVERIFY(model != 0); + + QHash blank; + for (int i=0; iroles().count(); i++) + blank.insert(model->roles()[i], QVariant()); + for (int i=0; i<9; i++) { + QCOMPARE(model->data(i, model->roles()), blank); + for (int j=0; jroles().count(); j++) { + QCOMPARE(model->data(i, j), QVariant()); + } + } + QTRY_COMPARE(model->count(), 9); + + delete model; +} + void tst_qdeclarativexmllistmodel::reload() { // If no keys are used, the model should be rebuilt from scratch when -- cgit v0.12