Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativelistreference / tst_qdeclarativelistreference.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qtest.h>
43 #include <QUrl>
44 #include <QFileInfo>
45 #include <QDir>
46 #include <QDeclarativeEngine>
47 #include <QDeclarativeComponent>
48 #include <QtDeclarative/qdeclarative.h>
49 #include <QtDeclarative/qdeclarativeprivate.h>
50 #include <QtDeclarative/qdeclarativeproperty.h>
51 #include <QDebug>
52
53 inline QUrl TEST_FILE(const QString &filename)
54 {
55     QFileInfo fileInfo(__FILE__);
56     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath("data/" + filename));
57 }
58
59 inline QUrl TEST_FILE(const char *filename)
60 {
61     return TEST_FILE(QLatin1String(filename));
62 }
63
64 class tst_qdeclarativelistreference : public QObject
65 {
66     Q_OBJECT
67 public:
68     tst_qdeclarativelistreference() {}
69
70 private slots:
71     void initTestCase();
72     void qmllistreference();
73     void qmllistreference_invalid();
74     void isValid();
75     void object();
76     void listElementType();
77     void canAppend();
78     void canAt();
79     void canClear();
80     void canCount();
81     void append();
82     void at();
83     void clear();
84     void count();
85     void copy();
86     void qmlmetaproperty();
87     void engineTypes();
88     void variantToList();
89 };
90
91 class TestType : public QObject
92 {
93     Q_OBJECT
94     Q_PROPERTY(QDeclarativeListProperty<TestType> data READ dataProperty)
95     Q_PROPERTY(int intProperty READ intProperty)
96
97 public:
98     TestType() : property(this, data) {}
99     QDeclarativeListProperty<TestType> dataProperty() { return property; }
100     int intProperty() const { return 10; }
101
102     QList<TestType *> data;
103     QDeclarativeListProperty<TestType> property;
104 };
105
106 void tst_qdeclarativelistreference::initTestCase()
107 {
108     qmlRegisterType<TestType>();
109 }
110
111 void tst_qdeclarativelistreference::qmllistreference()
112 {
113     TestType tt;
114
115     QDeclarativeListReference r(&tt, "data");
116     QVERIFY(r.isValid() == true);
117     QCOMPARE(r.count(), 0);
118
119     tt.data.append(&tt);
120     QCOMPARE(r.count(), 1);
121 }
122
123 void tst_qdeclarativelistreference::qmllistreference_invalid()
124 {
125     TestType tt;
126
127     // Invalid
128     {
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);
140     }
141
142     // Non-property
143     {
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);
155     }
156
157     // Non-list property
158     {
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);
170     }
171 }
172
173 void tst_qdeclarativelistreference::isValid()
174 {
175     TestType *tt = new TestType;
176
177     {
178     QDeclarativeListReference ref;
179     QVERIFY(ref.isValid() == false);
180     }
181
182     {
183     QDeclarativeListReference ref(tt, "blah");
184     QVERIFY(ref.isValid() == false);
185     }
186
187     {
188     QDeclarativeListReference ref(tt, "data");
189     QVERIFY(ref.isValid() == true);
190     delete tt;
191     QVERIFY(ref.isValid() == false);
192     }
193 }
194
195 void tst_qdeclarativelistreference::object()
196 {
197     TestType *tt = new TestType;
198
199     {
200     QDeclarativeListReference ref;
201     QVERIFY(ref.object() == 0);
202     }
203
204     {
205     QDeclarativeListReference ref(tt, "blah");
206     QVERIFY(ref.object() == 0);
207     }
208
209     {
210     QDeclarativeListReference ref(tt, "data");
211     QVERIFY(ref.object() == tt);
212     delete tt;
213     QVERIFY(ref.object() == 0);
214     }
215 }
216
217 void tst_qdeclarativelistreference::listElementType()
218 {
219     TestType *tt = new TestType;
220
221     {
222     QDeclarativeListReference ref;
223     QVERIFY(ref.listElementType() == 0);
224     }
225
226     {
227     QDeclarativeListReference ref(tt, "blah");
228     QVERIFY(ref.listElementType() == 0);
229     }
230
231     {
232     QDeclarativeListReference ref(tt, "data");
233     QVERIFY(ref.listElementType() == &TestType::staticMetaObject);
234     delete tt;
235     QVERIFY(ref.listElementType() == 0);
236     }
237 }
238
239 void tst_qdeclarativelistreference::canAppend()
240 {
241     TestType *tt = new TestType;
242
243     {
244     QDeclarativeListReference ref;
245     QVERIFY(ref.canAppend() == false);
246     }
247
248     {
249     QDeclarativeListReference ref(tt, "blah");
250     QVERIFY(ref.canAppend() == false);
251     }
252
253     {
254     QDeclarativeListReference ref(tt, "data");
255     QVERIFY(ref.canAppend() == true);
256     delete tt;
257     QVERIFY(ref.canAppend() == false);
258     }
259
260     {
261     TestType tt;
262     tt.property.append = 0;
263     QDeclarativeListReference ref(&tt, "data");
264     QVERIFY(ref.canAppend() == false);
265     }
266 }
267
268 void tst_qdeclarativelistreference::canAt()
269 {
270     TestType *tt = new TestType;
271
272     {
273     QDeclarativeListReference ref;
274     QVERIFY(ref.canAt() == false);
275     }
276
277     {
278     QDeclarativeListReference ref(tt, "blah");
279     QVERIFY(ref.canAt() == false);
280     }
281
282     {
283     QDeclarativeListReference ref(tt, "data");
284     QVERIFY(ref.canAt() == true);
285     delete tt;
286     QVERIFY(ref.canAt() == false);
287     }
288
289     {
290     TestType tt;
291     tt.property.at = 0;
292     QDeclarativeListReference ref(&tt, "data");
293     QVERIFY(ref.canAt() == false);
294     }
295 }
296
297 void tst_qdeclarativelistreference::canClear()
298 {
299     TestType *tt = new TestType;
300
301     {
302     QDeclarativeListReference ref;
303     QVERIFY(ref.canClear() == false);
304     }
305
306     {
307     QDeclarativeListReference ref(tt, "blah");
308     QVERIFY(ref.canClear() == false);
309     }
310
311     {
312     QDeclarativeListReference ref(tt, "data");
313     QVERIFY(ref.canClear() == true);
314     delete tt;
315     QVERIFY(ref.canClear() == false);
316     }
317
318     {
319     TestType tt;
320     tt.property.clear = 0;
321     QDeclarativeListReference ref(&tt, "data");
322     QVERIFY(ref.canClear() == false);
323     }
324 }
325
326 void tst_qdeclarativelistreference::canCount()
327 {
328     TestType *tt = new TestType;
329
330     {
331     QDeclarativeListReference ref;
332     QVERIFY(ref.canCount() == false);
333     }
334
335     {
336     QDeclarativeListReference ref(tt, "blah");
337     QVERIFY(ref.canCount() == false);
338     }
339
340     {
341     QDeclarativeListReference ref(tt, "data");
342     QVERIFY(ref.canCount() == true);
343     delete tt;
344     QVERIFY(ref.canCount() == false);
345     }
346
347     {
348     TestType tt;
349     tt.property.count = 0;
350     QDeclarativeListReference ref(&tt, "data");
351     QVERIFY(ref.canCount() == false);
352     }
353 }
354
355 void tst_qdeclarativelistreference::append()
356 {
357     TestType *tt = new TestType;
358     QObject object;
359
360     {
361     QDeclarativeListReference ref;
362     QVERIFY(ref.append(tt) == false);
363     }
364
365     {
366     QDeclarativeListReference ref(tt, "blah");
367     QVERIFY(ref.append(tt) == false);
368     }
369
370     {
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);
382     delete tt;
383     QVERIFY(ref.append(0) == false);
384     }
385
386     {
387     TestType tt;
388     tt.property.append = 0;
389     QDeclarativeListReference ref(&tt, "data");
390     QVERIFY(ref.append(&tt) == false);
391     }
392 }
393
394 void tst_qdeclarativelistreference::at()
395 {
396     TestType *tt = new TestType;
397     tt->data.append(tt);
398     tt->data.append(0);
399     tt->data.append(tt);
400
401     {
402     QDeclarativeListReference ref;
403     QVERIFY(ref.at(0) == 0);
404     }
405
406     {
407     QDeclarativeListReference ref(tt, "blah");
408     QVERIFY(ref.at(0) == 0);
409     }
410
411     {
412     QDeclarativeListReference ref(tt, "data");
413     QVERIFY(ref.at(0) == tt);
414     QVERIFY(ref.at(1) == 0);
415     QVERIFY(ref.at(2) == tt);
416     delete tt;
417     QVERIFY(ref.at(0) == 0);
418     }
419
420     {
421     TestType tt;
422     tt.data.append(&tt);
423     tt.property.at = 0;
424     QDeclarativeListReference ref(&tt, "data");
425     QVERIFY(ref.at(0) == 0);
426     }
427 }
428
429 void tst_qdeclarativelistreference::clear()
430 {
431     TestType *tt = new TestType;
432     tt->data.append(tt);
433     tt->data.append(0);
434     tt->data.append(tt);
435
436     {
437     QDeclarativeListReference ref;
438     QVERIFY(ref.clear() == false);
439     }
440
441     {
442     QDeclarativeListReference ref(tt, "blah");
443     QVERIFY(ref.clear() == false);
444     }
445
446     {
447     QDeclarativeListReference ref(tt, "data");
448     QVERIFY(ref.clear() == true);
449     QVERIFY(tt->data.count() == 0);
450     delete tt;
451     QVERIFY(ref.clear() == false);
452     }
453
454     {
455     TestType tt;
456     tt.property.clear = 0;
457     QDeclarativeListReference ref(&tt, "data");
458     QVERIFY(ref.clear() == false);
459     }
460 }
461
462 void tst_qdeclarativelistreference::count()
463 {
464     TestType *tt = new TestType;
465     tt->data.append(tt);
466     tt->data.append(0);
467     tt->data.append(tt);
468
469     {
470     QDeclarativeListReference ref;
471     QVERIFY(ref.count() == 0);
472     }
473
474     {
475     QDeclarativeListReference ref(tt, "blah");
476     QVERIFY(ref.count() == 0);
477     }
478
479     {
480     QDeclarativeListReference ref(tt, "data");
481     QVERIFY(ref.count() == 3);
482     tt->data.removeAt(1);
483     QVERIFY(ref.count() == 2);
484     delete tt;
485     QVERIFY(ref.count() == 0);
486     }
487
488     {
489     TestType tt;
490     tt.data.append(&tt);
491     tt.property.count = 0;
492     QDeclarativeListReference ref(&tt, "data");
493     QVERIFY(ref.count() == 0);
494     }
495 }
496
497 void tst_qdeclarativelistreference::copy()
498 {
499     TestType tt;
500     tt.data.append(&tt);
501     tt.data.append(0);
502     tt.data.append(&tt);
503
504     QDeclarativeListReference *r1 = new QDeclarativeListReference(&tt, "data");
505     QVERIFY(r1->count() == 3);
506
507     QDeclarativeListReference r2(*r1);
508     QDeclarativeListReference r3;
509     r3 = *r1;
510
511     QVERIFY(r2.count() == 3);
512     QVERIFY(r3.count() == 3);
513
514     delete r1;
515
516     QVERIFY(r2.count() == 3);
517     QVERIFY(r3.count() == 3);
518
519     tt.data.removeAt(2);
520
521     QVERIFY(r2.count() == 2);
522     QVERIFY(r3.count() == 2);
523 }
524
525 void tst_qdeclarativelistreference::qmlmetaproperty()
526 {
527     TestType tt;
528     tt.data.append(&tt);
529     tt.data.append(0);
530     tt.data.append(&tt);
531
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);
538 }
539
540 void tst_qdeclarativelistreference::engineTypes()
541 {
542     QDeclarativeEngine engine;
543     QDeclarativeComponent component(&engine, TEST_FILE("engineTypes.qml"));
544
545     QObject *o = component.create();
546     QVERIFY(o);
547
548     QDeclarativeProperty p1(o, QLatin1String("myList"));
549     QVERIFY(p1.propertyTypeCategory() == QDeclarativeProperty::Normal);
550
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);
559
560     delete o;
561 }
562
563 void tst_qdeclarativelistreference::variantToList()
564 {
565     QDeclarativeEngine engine;
566     QDeclarativeComponent component(&engine, TEST_FILE("variantToList.qml"));
567
568     QObject *o = component.create();
569     QVERIFY(o);
570
571     QVERIFY(o->property("value").userType() == qMetaTypeId<QDeclarativeListReference>());
572     QCOMPARE(o->property("test").toInt(), 1);
573
574     delete o;
575 }
576
577 QTEST_MAIN(tst_qdeclarativelistreference)
578
579 #include "tst_qdeclarativelistreference.moc"