1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
46 #include <QDeclarativeEngine>
47 #include <QDeclarativeComponent>
48 #include <QtDeclarative/qdeclarative.h>
49 #include <QtDeclarative/qdeclarativeprivate.h>
50 #include <QtDeclarative/qdeclarativeproperty.h>
53 inline QUrl TEST_FILE(const QString &filename)
55 QFileInfo fileInfo(__FILE__);
56 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath("data/" + filename));
59 inline QUrl TEST_FILE(const char *filename)
61 return TEST_FILE(QLatin1String(filename));
64 class tst_qdeclarativelistreference : public QObject
68 tst_qdeclarativelistreference() {}
72 void qmllistreference();
73 void qmllistreference_invalid();
76 void listElementType();
86 void qmlmetaproperty();
91 class TestType : public QObject
94 Q_PROPERTY(QDeclarativeListProperty<TestType> data READ dataProperty)
95 Q_PROPERTY(int intProperty READ intProperty)
98 TestType() : property(this, data) {}
99 QDeclarativeListProperty<TestType> dataProperty() { return property; }
100 int intProperty() const { return 10; }
102 QList<TestType *> data;
103 QDeclarativeListProperty<TestType> property;
106 void tst_qdeclarativelistreference::initTestCase()
108 qmlRegisterType<TestType>();
111 void tst_qdeclarativelistreference::qmllistreference()
115 QDeclarativeListReference r(&tt, "data");
116 QVERIFY(r.isValid() == true);
117 QCOMPARE(r.count(), 0);
120 QCOMPARE(r.count(), 1);
123 void tst_qdeclarativelistreference::qmllistreference_invalid()
129 QDeclarativeListReference r;
130 QVERIFY(r.isValid() == false);
131 QVERIFY(r.object() == 0);
132 QVERIFY(r.listElementType() == 0);
133 QVERIFY(r.canAt() == false);
134 QVERIFY(r.canClear() == false);
135 QVERIFY(r.canCount() == false);
136 QVERIFY(r.append(0) == false);
137 QVERIFY(r.at(10) == 0);
138 QVERIFY(r.clear() == false);
139 QVERIFY(r.count() == 0);
144 QDeclarativeListReference r(&tt, "blah");
145 QVERIFY(r.isValid() == false);
146 QVERIFY(r.object() == 0);
147 QVERIFY(r.listElementType() == 0);
148 QVERIFY(r.canAt() == false);
149 QVERIFY(r.canClear() == false);
150 QVERIFY(r.canCount() == false);
151 QVERIFY(r.append(0) == false);
152 QVERIFY(r.at(10) == 0);
153 QVERIFY(r.clear() == false);
154 QVERIFY(r.count() == 0);
159 QDeclarativeListReference r(&tt, "intProperty");
160 QVERIFY(r.isValid() == false);
161 QVERIFY(r.object() == 0);
162 QVERIFY(r.listElementType() == 0);
163 QVERIFY(r.canAt() == false);
164 QVERIFY(r.canClear() == false);
165 QVERIFY(r.canCount() == false);
166 QVERIFY(r.append(0) == false);
167 QVERIFY(r.at(10) == 0);
168 QVERIFY(r.clear() == false);
169 QVERIFY(r.count() == 0);
173 void tst_qdeclarativelistreference::isValid()
175 TestType *tt = new TestType;
178 QDeclarativeListReference ref;
179 QVERIFY(ref.isValid() == false);
183 QDeclarativeListReference ref(tt, "blah");
184 QVERIFY(ref.isValid() == false);
188 QDeclarativeListReference ref(tt, "data");
189 QVERIFY(ref.isValid() == true);
191 QVERIFY(ref.isValid() == false);
195 void tst_qdeclarativelistreference::object()
197 TestType *tt = new TestType;
200 QDeclarativeListReference ref;
201 QVERIFY(ref.object() == 0);
205 QDeclarativeListReference ref(tt, "blah");
206 QVERIFY(ref.object() == 0);
210 QDeclarativeListReference ref(tt, "data");
211 QVERIFY(ref.object() == tt);
213 QVERIFY(ref.object() == 0);
217 void tst_qdeclarativelistreference::listElementType()
219 TestType *tt = new TestType;
222 QDeclarativeListReference ref;
223 QVERIFY(ref.listElementType() == 0);
227 QDeclarativeListReference ref(tt, "blah");
228 QVERIFY(ref.listElementType() == 0);
232 QDeclarativeListReference ref(tt, "data");
233 QVERIFY(ref.listElementType() == &TestType::staticMetaObject);
235 QVERIFY(ref.listElementType() == 0);
239 void tst_qdeclarativelistreference::canAppend()
241 TestType *tt = new TestType;
244 QDeclarativeListReference ref;
245 QVERIFY(ref.canAppend() == false);
249 QDeclarativeListReference ref(tt, "blah");
250 QVERIFY(ref.canAppend() == false);
254 QDeclarativeListReference ref(tt, "data");
255 QVERIFY(ref.canAppend() == true);
257 QVERIFY(ref.canAppend() == false);
262 tt.property.append = 0;
263 QDeclarativeListReference ref(&tt, "data");
264 QVERIFY(ref.canAppend() == false);
268 void tst_qdeclarativelistreference::canAt()
270 TestType *tt = new TestType;
273 QDeclarativeListReference ref;
274 QVERIFY(ref.canAt() == false);
278 QDeclarativeListReference ref(tt, "blah");
279 QVERIFY(ref.canAt() == false);
283 QDeclarativeListReference ref(tt, "data");
284 QVERIFY(ref.canAt() == true);
286 QVERIFY(ref.canAt() == false);
292 QDeclarativeListReference ref(&tt, "data");
293 QVERIFY(ref.canAt() == false);
297 void tst_qdeclarativelistreference::canClear()
299 TestType *tt = new TestType;
302 QDeclarativeListReference ref;
303 QVERIFY(ref.canClear() == false);
307 QDeclarativeListReference ref(tt, "blah");
308 QVERIFY(ref.canClear() == false);
312 QDeclarativeListReference ref(tt, "data");
313 QVERIFY(ref.canClear() == true);
315 QVERIFY(ref.canClear() == false);
320 tt.property.clear = 0;
321 QDeclarativeListReference ref(&tt, "data");
322 QVERIFY(ref.canClear() == false);
326 void tst_qdeclarativelistreference::canCount()
328 TestType *tt = new TestType;
331 QDeclarativeListReference ref;
332 QVERIFY(ref.canCount() == false);
336 QDeclarativeListReference ref(tt, "blah");
337 QVERIFY(ref.canCount() == false);
341 QDeclarativeListReference ref(tt, "data");
342 QVERIFY(ref.canCount() == true);
344 QVERIFY(ref.canCount() == false);
349 tt.property.count = 0;
350 QDeclarativeListReference ref(&tt, "data");
351 QVERIFY(ref.canCount() == false);
355 void tst_qdeclarativelistreference::append()
357 TestType *tt = new TestType;
361 QDeclarativeListReference ref;
362 QVERIFY(ref.append(tt) == false);
366 QDeclarativeListReference ref(tt, "blah");
367 QVERIFY(ref.append(tt) == false);
371 QDeclarativeListReference ref(tt, "data");
372 QVERIFY(ref.append(tt) == true);
373 QVERIFY(tt->data.count() == 1);
374 QVERIFY(tt->data.at(0) == tt);
375 QVERIFY(ref.append(&object) == false);
376 QVERIFY(tt->data.count() == 1);
377 QVERIFY(tt->data.at(0) == tt);
378 QVERIFY(ref.append(0) == true);
379 QVERIFY(tt->data.count() == 2);
380 QVERIFY(tt->data.at(0) == tt);
381 QVERIFY(tt->data.at(1) == 0);
383 QVERIFY(ref.append(0) == false);
388 tt.property.append = 0;
389 QDeclarativeListReference ref(&tt, "data");
390 QVERIFY(ref.append(&tt) == false);
394 void tst_qdeclarativelistreference::at()
396 TestType *tt = new TestType;
402 QDeclarativeListReference ref;
403 QVERIFY(ref.at(0) == 0);
407 QDeclarativeListReference ref(tt, "blah");
408 QVERIFY(ref.at(0) == 0);
412 QDeclarativeListReference ref(tt, "data");
413 QVERIFY(ref.at(0) == tt);
414 QVERIFY(ref.at(1) == 0);
415 QVERIFY(ref.at(2) == tt);
417 QVERIFY(ref.at(0) == 0);
424 QDeclarativeListReference ref(&tt, "data");
425 QVERIFY(ref.at(0) == 0);
429 void tst_qdeclarativelistreference::clear()
431 TestType *tt = new TestType;
437 QDeclarativeListReference ref;
438 QVERIFY(ref.clear() == false);
442 QDeclarativeListReference ref(tt, "blah");
443 QVERIFY(ref.clear() == false);
447 QDeclarativeListReference ref(tt, "data");
448 QVERIFY(ref.clear() == true);
449 QVERIFY(tt->data.count() == 0);
451 QVERIFY(ref.clear() == false);
456 tt.property.clear = 0;
457 QDeclarativeListReference ref(&tt, "data");
458 QVERIFY(ref.clear() == false);
462 void tst_qdeclarativelistreference::count()
464 TestType *tt = new TestType;
470 QDeclarativeListReference ref;
471 QVERIFY(ref.count() == 0);
475 QDeclarativeListReference ref(tt, "blah");
476 QVERIFY(ref.count() == 0);
480 QDeclarativeListReference ref(tt, "data");
481 QVERIFY(ref.count() == 3);
482 tt->data.removeAt(1);
483 QVERIFY(ref.count() == 2);
485 QVERIFY(ref.count() == 0);
491 tt.property.count = 0;
492 QDeclarativeListReference ref(&tt, "data");
493 QVERIFY(ref.count() == 0);
497 void tst_qdeclarativelistreference::copy()
504 QDeclarativeListReference *r1 = new QDeclarativeListReference(&tt, "data");
505 QVERIFY(r1->count() == 3);
507 QDeclarativeListReference r2(*r1);
508 QDeclarativeListReference r3;
511 QVERIFY(r2.count() == 3);
512 QVERIFY(r3.count() == 3);
516 QVERIFY(r2.count() == 3);
517 QVERIFY(r3.count() == 3);
521 QVERIFY(r2.count() == 2);
522 QVERIFY(r3.count() == 2);
525 void tst_qdeclarativelistreference::qmlmetaproperty()
532 QDeclarativeProperty prop(&tt, QLatin1String("data"));
533 QVariant v = prop.read();
534 QVERIFY(v.userType() == qMetaTypeId<QDeclarativeListReference>());
535 QDeclarativeListReference ref = qvariant_cast<QDeclarativeListReference>(v);
536 QVERIFY(ref.count() == 3);
537 QVERIFY(ref.listElementType() == &TestType::staticMetaObject);
540 void tst_qdeclarativelistreference::engineTypes()
542 QDeclarativeEngine engine;
543 QDeclarativeComponent component(&engine, TEST_FILE("engineTypes.qml"));
545 QObject *o = component.create();
548 QDeclarativeProperty p1(o, QLatin1String("myList"));
549 QVERIFY(p1.propertyTypeCategory() == QDeclarativeProperty::Normal);
551 QDeclarativeProperty p2(o, QLatin1String("myList"), engine.rootContext());
552 QVERIFY(p2.propertyTypeCategory() == QDeclarativeProperty::List);
553 QVariant v = p2.read();
554 QVERIFY(v.userType() == qMetaTypeId<QDeclarativeListReference>());
555 QDeclarativeListReference ref = qvariant_cast<QDeclarativeListReference>(v);
556 QVERIFY(ref.count() == 2);
557 QVERIFY(ref.listElementType());
558 QVERIFY(ref.listElementType() != &QObject::staticMetaObject);
563 void tst_qdeclarativelistreference::variantToList()
565 QDeclarativeEngine engine;
566 QDeclarativeComponent component(&engine, TEST_FILE("variantToList.qml"));
568 QObject *o = component.create();
571 QVERIFY(o->property("value").userType() == qMetaTypeId<QDeclarativeListReference>());
572 QCOMPARE(o->property("test").toInt(), 1);
577 QTEST_MAIN(tst_qdeclarativelistreference)
579 #include "tst_qdeclarativelistreference.moc"