1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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>
52 #include "../../shared/util.h"
54 class tst_qdeclarativelistreference : public QDeclarativeDataTest
58 tst_qdeclarativelistreference() {}
62 void qmllistreference();
63 void qmllistreference_invalid();
66 void listElementType();
76 void qmlmetaproperty();
81 class TestType : public QObject
84 Q_PROPERTY(QDeclarativeListProperty<TestType> data READ dataProperty)
85 Q_PROPERTY(int intProperty READ intProperty)
88 TestType() : property(this, data) {}
89 QDeclarativeListProperty<TestType> dataProperty() { return property; }
90 int intProperty() const { return 10; }
92 QList<TestType *> data;
93 QDeclarativeListProperty<TestType> property;
96 void tst_qdeclarativelistreference::initTestCase()
98 QDeclarativeDataTest::initTestCase();
99 qmlRegisterType<TestType>();
102 void tst_qdeclarativelistreference::qmllistreference()
106 QDeclarativeListReference r(&tt, "data");
107 QVERIFY(r.isValid() == true);
108 QCOMPARE(r.count(), 0);
111 QCOMPARE(r.count(), 1);
114 void tst_qdeclarativelistreference::qmllistreference_invalid()
120 QDeclarativeListReference r;
121 QVERIFY(r.isValid() == false);
122 QVERIFY(r.object() == 0);
123 QVERIFY(r.listElementType() == 0);
124 QVERIFY(r.canAt() == false);
125 QVERIFY(r.canClear() == false);
126 QVERIFY(r.canCount() == false);
127 QVERIFY(r.append(0) == false);
128 QVERIFY(r.at(10) == 0);
129 QVERIFY(r.clear() == false);
130 QVERIFY(r.count() == 0);
135 QDeclarativeListReference r(&tt, "blah");
136 QVERIFY(r.isValid() == false);
137 QVERIFY(r.object() == 0);
138 QVERIFY(r.listElementType() == 0);
139 QVERIFY(r.canAt() == false);
140 QVERIFY(r.canClear() == false);
141 QVERIFY(r.canCount() == false);
142 QVERIFY(r.append(0) == false);
143 QVERIFY(r.at(10) == 0);
144 QVERIFY(r.clear() == false);
145 QVERIFY(r.count() == 0);
150 QDeclarativeListReference r(&tt, "intProperty");
151 QVERIFY(r.isValid() == false);
152 QVERIFY(r.object() == 0);
153 QVERIFY(r.listElementType() == 0);
154 QVERIFY(r.canAt() == false);
155 QVERIFY(r.canClear() == false);
156 QVERIFY(r.canCount() == false);
157 QVERIFY(r.append(0) == false);
158 QVERIFY(r.at(10) == 0);
159 QVERIFY(r.clear() == false);
160 QVERIFY(r.count() == 0);
164 void tst_qdeclarativelistreference::isValid()
166 TestType *tt = new TestType;
169 QDeclarativeListReference ref;
170 QVERIFY(ref.isValid() == false);
174 QDeclarativeListReference ref(tt, "blah");
175 QVERIFY(ref.isValid() == false);
179 QDeclarativeListReference ref(tt, "data");
180 QVERIFY(ref.isValid() == true);
182 QVERIFY(ref.isValid() == false);
186 void tst_qdeclarativelistreference::object()
188 TestType *tt = new TestType;
191 QDeclarativeListReference ref;
192 QVERIFY(ref.object() == 0);
196 QDeclarativeListReference ref(tt, "blah");
197 QVERIFY(ref.object() == 0);
201 QDeclarativeListReference ref(tt, "data");
202 QVERIFY(ref.object() == tt);
204 QVERIFY(ref.object() == 0);
208 void tst_qdeclarativelistreference::listElementType()
210 TestType *tt = new TestType;
213 QDeclarativeListReference ref;
214 QVERIFY(ref.listElementType() == 0);
218 QDeclarativeListReference ref(tt, "blah");
219 QVERIFY(ref.listElementType() == 0);
223 QDeclarativeListReference ref(tt, "data");
224 QVERIFY(ref.listElementType() == &TestType::staticMetaObject);
226 QVERIFY(ref.listElementType() == 0);
230 void tst_qdeclarativelistreference::canAppend()
232 TestType *tt = new TestType;
235 QDeclarativeListReference ref;
236 QVERIFY(ref.canAppend() == false);
240 QDeclarativeListReference ref(tt, "blah");
241 QVERIFY(ref.canAppend() == false);
245 QDeclarativeListReference ref(tt, "data");
246 QVERIFY(ref.canAppend() == true);
248 QVERIFY(ref.canAppend() == false);
253 tt.property.append = 0;
254 QDeclarativeListReference ref(&tt, "data");
255 QVERIFY(ref.canAppend() == false);
259 void tst_qdeclarativelistreference::canAt()
261 TestType *tt = new TestType;
264 QDeclarativeListReference ref;
265 QVERIFY(ref.canAt() == false);
269 QDeclarativeListReference ref(tt, "blah");
270 QVERIFY(ref.canAt() == false);
274 QDeclarativeListReference ref(tt, "data");
275 QVERIFY(ref.canAt() == true);
277 QVERIFY(ref.canAt() == false);
283 QDeclarativeListReference ref(&tt, "data");
284 QVERIFY(ref.canAt() == false);
288 void tst_qdeclarativelistreference::canClear()
290 TestType *tt = new TestType;
293 QDeclarativeListReference ref;
294 QVERIFY(ref.canClear() == false);
298 QDeclarativeListReference ref(tt, "blah");
299 QVERIFY(ref.canClear() == false);
303 QDeclarativeListReference ref(tt, "data");
304 QVERIFY(ref.canClear() == true);
306 QVERIFY(ref.canClear() == false);
311 tt.property.clear = 0;
312 QDeclarativeListReference ref(&tt, "data");
313 QVERIFY(ref.canClear() == false);
317 void tst_qdeclarativelistreference::canCount()
319 TestType *tt = new TestType;
322 QDeclarativeListReference ref;
323 QVERIFY(ref.canCount() == false);
327 QDeclarativeListReference ref(tt, "blah");
328 QVERIFY(ref.canCount() == false);
332 QDeclarativeListReference ref(tt, "data");
333 QVERIFY(ref.canCount() == true);
335 QVERIFY(ref.canCount() == false);
340 tt.property.count = 0;
341 QDeclarativeListReference ref(&tt, "data");
342 QVERIFY(ref.canCount() == false);
346 void tst_qdeclarativelistreference::append()
348 TestType *tt = new TestType;
352 QDeclarativeListReference ref;
353 QVERIFY(ref.append(tt) == false);
357 QDeclarativeListReference ref(tt, "blah");
358 QVERIFY(ref.append(tt) == false);
362 QDeclarativeListReference ref(tt, "data");
363 QVERIFY(ref.append(tt) == true);
364 QVERIFY(tt->data.count() == 1);
365 QVERIFY(tt->data.at(0) == tt);
366 QVERIFY(ref.append(&object) == false);
367 QVERIFY(tt->data.count() == 1);
368 QVERIFY(tt->data.at(0) == tt);
369 QVERIFY(ref.append(0) == true);
370 QVERIFY(tt->data.count() == 2);
371 QVERIFY(tt->data.at(0) == tt);
372 QVERIFY(tt->data.at(1) == 0);
374 QVERIFY(ref.append(0) == false);
379 tt.property.append = 0;
380 QDeclarativeListReference ref(&tt, "data");
381 QVERIFY(ref.append(&tt) == false);
385 void tst_qdeclarativelistreference::at()
387 TestType *tt = new TestType;
393 QDeclarativeListReference ref;
394 QVERIFY(ref.at(0) == 0);
398 QDeclarativeListReference ref(tt, "blah");
399 QVERIFY(ref.at(0) == 0);
403 QDeclarativeListReference ref(tt, "data");
404 QVERIFY(ref.at(0) == tt);
405 QVERIFY(ref.at(1) == 0);
406 QVERIFY(ref.at(2) == tt);
408 QVERIFY(ref.at(0) == 0);
415 QDeclarativeListReference ref(&tt, "data");
416 QVERIFY(ref.at(0) == 0);
420 void tst_qdeclarativelistreference::clear()
422 TestType *tt = new TestType;
428 QDeclarativeListReference ref;
429 QVERIFY(ref.clear() == false);
433 QDeclarativeListReference ref(tt, "blah");
434 QVERIFY(ref.clear() == false);
438 QDeclarativeListReference ref(tt, "data");
439 QVERIFY(ref.clear() == true);
440 QVERIFY(tt->data.count() == 0);
442 QVERIFY(ref.clear() == false);
447 tt.property.clear = 0;
448 QDeclarativeListReference ref(&tt, "data");
449 QVERIFY(ref.clear() == false);
453 void tst_qdeclarativelistreference::count()
455 TestType *tt = new TestType;
461 QDeclarativeListReference ref;
462 QVERIFY(ref.count() == 0);
466 QDeclarativeListReference ref(tt, "blah");
467 QVERIFY(ref.count() == 0);
471 QDeclarativeListReference ref(tt, "data");
472 QVERIFY(ref.count() == 3);
473 tt->data.removeAt(1);
474 QVERIFY(ref.count() == 2);
476 QVERIFY(ref.count() == 0);
482 tt.property.count = 0;
483 QDeclarativeListReference ref(&tt, "data");
484 QVERIFY(ref.count() == 0);
488 void tst_qdeclarativelistreference::copy()
495 QDeclarativeListReference *r1 = new QDeclarativeListReference(&tt, "data");
496 QVERIFY(r1->count() == 3);
498 QDeclarativeListReference r2(*r1);
499 QDeclarativeListReference r3;
502 QVERIFY(r2.count() == 3);
503 QVERIFY(r3.count() == 3);
507 QVERIFY(r2.count() == 3);
508 QVERIFY(r3.count() == 3);
512 QVERIFY(r2.count() == 2);
513 QVERIFY(r3.count() == 2);
516 void tst_qdeclarativelistreference::qmlmetaproperty()
523 QDeclarativeProperty prop(&tt, QLatin1String("data"));
524 QVariant v = prop.read();
525 QVERIFY(v.userType() == qMetaTypeId<QDeclarativeListReference>());
526 QDeclarativeListReference ref = qvariant_cast<QDeclarativeListReference>(v);
527 QVERIFY(ref.count() == 3);
528 QVERIFY(ref.listElementType() == &TestType::staticMetaObject);
531 void tst_qdeclarativelistreference::engineTypes()
533 QDeclarativeEngine engine;
534 QDeclarativeComponent component(&engine, testFileUrl("engineTypes.qml"));
536 QObject *o = component.create();
539 QDeclarativeProperty p1(o, QLatin1String("myList"));
540 QVERIFY(p1.propertyTypeCategory() == QDeclarativeProperty::Normal);
542 QDeclarativeProperty p2(o, QLatin1String("myList"), engine.rootContext());
543 QVERIFY(p2.propertyTypeCategory() == QDeclarativeProperty::List);
544 QVariant v = p2.read();
545 QVERIFY(v.userType() == qMetaTypeId<QDeclarativeListReference>());
546 QDeclarativeListReference ref = qvariant_cast<QDeclarativeListReference>(v);
547 QVERIFY(ref.count() == 2);
548 QVERIFY(ref.listElementType());
549 QVERIFY(ref.listElementType() != &QObject::staticMetaObject);
554 void tst_qdeclarativelistreference::variantToList()
556 QDeclarativeEngine engine;
557 QDeclarativeComponent component(&engine, testFileUrl("variantToList.qml"));
559 QObject *o = component.create();
562 QVERIFY(o->property("value").userType() == qMetaTypeId<QDeclarativeListReference>());
563 QCOMPARE(o->property("test").toInt(), 1);
568 QTEST_MAIN(tst_qdeclarativelistreference)
570 #include "tst_qdeclarativelistreference.moc"