summaryrefslogtreecommitdiffstats
path: root/tests/auto/declarative/qdeclarativexmllistmodel
diff options
context:
space:
mode:
authorBea Lam <bea.lam@nokia.com>2010-03-28 23:35:55 (GMT)
committerBea Lam <bea.lam@nokia.com>2010-03-28 23:41:31 (GMT)
commit2776592d1e8dc8059f3a1c05237294b4a45543d9 (patch)
tree5f1cd583d18fbd9ba8b9b566d96adf42bd75775a /tests/auto/declarative/qdeclarativexmllistmodel
parent6cce231a2d8da6fb232908a8c815ba3a433cfad7 (diff)
downloadQt-2776592d1e8dc8059f3a1c05237294b4a45543d9.zip
Qt-2776592d1e8dc8059f3a1c05237294b4a45543d9.tar.gz
Qt-2776592d1e8dc8059f3a1c05237294b4a45543d9.tar.bz2
Don't set status to Ready until model has been created (currently this
is set as soon as XML data is downloaded, so there is no notification when model is actually ready to be used). This also fixes data() to not crash if it is called before the model is ready. Task-number: QTBUG-7835
Diffstat (limited to 'tests/auto/declarative/qdeclarativexmllistmodel')
-rw-r--r--tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp97
1 files changed, 86 insertions, 11 deletions
diff --git a/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp b/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp
index e3aa5cc..74da79e 100644
--- a/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp
+++ b/tests/auto/declarative/qdeclarativexmllistmodel/tst_qdeclarativexmllistmodel.cpp
@@ -41,6 +41,8 @@
#include <qtest.h>
#include <QtTest/qsignalspy.h>
#include <QtCore/qtimer.h>
+#include <QtCore/qfile.h>
+#include <QtCore/qtemporaryfile.h>
#ifdef QTEST_XMLPATTERNS
#include <QtDeclarative/qdeclarativeengine.h>
@@ -53,6 +55,7 @@ typedef QList<QVariantList> QDeclarativeXmlModelData;
Q_DECLARE_METATYPE(QList<QDeclarativeXmlListRange>)
Q_DECLARE_METATYPE(QDeclarativeXmlModelData)
+Q_DECLARE_METATYPE(QDeclarativeXmlListModel::Status)
class tst_qdeclarativexmllistmodel : public QObject
@@ -62,6 +65,10 @@ public:
tst_qdeclarativexmllistmodel() {}
private slots:
+ void initTestCase() {
+ qRegisterMetaType<QDeclarativeXmlListModel::Status>("QDeclarativeXmlListModel::Status");
+ }
+
void buildModel();
void missingFields();
void cdata();
@@ -69,7 +76,10 @@ private slots:
void roles();
void roleErrors();
void uniqueRoleNames();
- void status();
+ void xml();
+ void xml_data();
+ void source();
+ void source_data();
void data();
void reload();
void useKeys();
@@ -183,7 +193,6 @@ void tst_qdeclarativexmllistmodel::attributes()
QDeclarativeXmlListModel *model = qobject_cast<QDeclarativeXmlListModel*>(component.create());
QVERIFY(model != 0);
QTRY_COMPARE(model->count(), 5);
-
QList<int> roles;
roles << Qt::UserRole;
QHash<int, QVariant> data = model->data(2, roles);
@@ -249,27 +258,93 @@ void tst_qdeclarativexmllistmodel::uniqueRoleNames()
delete model;
}
-void tst_qdeclarativexmllistmodel::status()
+
+void tst_qdeclarativexmllistmodel::xml()
{
- QDeclarativeXmlListModel *model;
- model = new QDeclarativeXmlListModel;
- QCOMPARE(model->status(), QDeclarativeXmlListModel::Null);
+ QFETCH(QString, xml);
+ QFETCH(int, count);
- model->setXml("<data></data>");
+ QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/model.qml"));
+ QDeclarativeXmlListModel *model = qobject_cast<QDeclarativeXmlListModel*>(component.create());
+ QSignalSpy spy(model, SIGNAL(statusChanged(QDeclarativeXmlListModel::Status)));
+
+ QCOMPARE(model->progress(), qreal(0.0));
+ QCOMPARE(model->status(), QDeclarativeXmlListModel::Loading);
+ QTRY_COMPARE(spy.count(), 1); spy.clear();
QCOMPARE(model->status(), QDeclarativeXmlListModel::Ready);
+ QCOMPARE(model->progress(), qreal(1.0));
+ QCOMPARE(model->count(), 9);
+
+ // if xml is empty (i.e. clearing) it won't have any effect if a source is set
+ if (xml.isEmpty())
+ model->setSource(QUrl());
+ model->setXml(xml);
+ QCOMPARE(model->progress(), qreal(1.0)); // immediately goes to 1.0 if using setXml()
+ QTRY_COMPARE(spy.count(), 1); spy.clear();
+ QCOMPARE(model->status(), QDeclarativeXmlListModel::Loading);
+ QTRY_COMPARE(spy.count(), 1); spy.clear();
+ QCOMPARE(model->status(), QDeclarativeXmlListModel::Ready);
+ QCOMPARE(model->count(), count);
+
delete model;
+}
+
+void tst_qdeclarativexmllistmodel::xml_data()
+{
+ QTest::addColumn<QString>("xml");
+ QTest::addColumn<int>("count");
+
+ QTest::newRow("xml with no items") << "<Pets></Pets>" << 0;
+ QTest::newRow("empty xml") << "" << 0;
+ QTest::newRow("one item") << "<Pets><Pet><name>Hobbes</name><type>Tiger</type><age>7</age><size>Large</size></Pet></Pets>" << 1;
+}
+
+void tst_qdeclarativexmllistmodel::source()
+{
+ QFETCH(QUrl, source);
+ QFETCH(int, count);
+ QFETCH(QDeclarativeXmlListModel::Status, status);
QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/model.qml"));
- model = qobject_cast<QDeclarativeXmlListModel*>(component.create());
- QVERIFY(model != 0);
- QCOMPARE(model->status(), QDeclarativeXmlListModel::Loading);
+ QDeclarativeXmlListModel *model = qobject_cast<QDeclarativeXmlListModel*>(component.create());
+ QSignalSpy spy(model, SIGNAL(statusChanged(QDeclarativeXmlListModel::Status)));
- QTRY_COMPARE(model->count(), 9);
+ QCOMPARE(model->progress(), qreal(0.0));
+ QCOMPARE(model->status(), QDeclarativeXmlListModel::Loading);
+ QTRY_COMPARE(spy.count(), 1); spy.clear();
QCOMPARE(model->status(), QDeclarativeXmlListModel::Ready);
+ QCOMPARE(model->progress(), qreal(1.0));
+ QCOMPARE(model->count(), 9);
+
+ model->setSource(source);
+ QCOMPARE(model->progress(), qreal(0.0));
+ QTRY_COMPARE(spy.count(), 1); spy.clear();
+ QCOMPARE(model->status(), QDeclarativeXmlListModel::Loading);
+ QTRY_COMPARE(spy.count(), 1); spy.clear();
+ QCOMPARE(model->status(), status);
+ QCOMPARE(model->count(), count);
+ if (status == QDeclarativeXmlListModel::Ready)
+ QCOMPARE(model->progress(), qreal(1.0));
delete model;
}
+void tst_qdeclarativexmllistmodel::source_data()
+{
+ QTest::addColumn<QUrl>("source");
+ QTest::addColumn<int>("count");
+ QTest::addColumn<QDeclarativeXmlListModel::Status>("status");
+
+ QTest::newRow("valid") << QUrl::fromLocalFile(SRCDIR "/data/model2.xml") << 2 << QDeclarativeXmlListModel::Ready;
+ QTest::newRow("invalid") << QUrl("http://blah.blah/blah.xml") << 0 << QDeclarativeXmlListModel::Error;
+
+ // empty file
+ QTemporaryFile *temp = new QTemporaryFile(this);
+ if (temp->open())
+ QTest::newRow("empty file") << QUrl::fromLocalFile(temp->fileName()) << 0 << QDeclarativeXmlListModel::Ready;
+ temp->close();
+}
+
void tst_qdeclarativexmllistmodel::data()
{
QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/model.qml"));