Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativelistreference / tst_qdeclarativelistreference.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
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 #include "../../shared/util.h"
53
54 class tst_qdeclarativelistreference : public QDeclarativeDataTest
55 {
56     Q_OBJECT
57 public:
58     tst_qdeclarativelistreference() {}
59
60 private slots:
61     void initTestCase();
62     void qmllistreference();
63     void qmllistreference_invalid();
64     void isValid();
65     void object();
66     void listElementType();
67     void canAppend();
68     void canAt();
69     void canClear();
70     void canCount();
71     void append();
72     void at();
73     void clear();
74     void count();
75     void copy();
76     void qmlmetaproperty();
77     void engineTypes();
78     void variantToList();
79 };
80
81 class TestType : public QObject
82 {
83     Q_OBJECT
84     Q_PROPERTY(QDeclarativeListProperty<TestType> data READ dataProperty)
85     Q_PROPERTY(int intProperty READ intProperty)
86
87 public:
88     TestType() : property(this, data) {}
89     QDeclarativeListProperty<TestType> dataProperty() { return property; }
90     int intProperty() const { return 10; }
91
92     QList<TestType *> data;
93     QDeclarativeListProperty<TestType> property;
94 };
95
96 void tst_qdeclarativelistreference::initTestCase()
97 {
98     QDeclarativeDataTest::initTestCase();
99     qmlRegisterType<TestType>();
100 }
101
102 void tst_qdeclarativelistreference::qmllistreference()
103 {
104     TestType tt;
105
106     QDeclarativeListReference r(&tt, "data");
107     QVERIFY(r.isValid() == true);
108     QCOMPARE(r.count(), 0);
109
110     tt.data.append(&tt);
111     QCOMPARE(r.count(), 1);
112 }
113
114 void tst_qdeclarativelistreference::qmllistreference_invalid()
115 {
116     TestType tt;
117
118     // Invalid
119     {
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);
131     }
132
133     // Non-property
134     {
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);
146     }
147
148     // Non-list property
149     {
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);
161     }
162 }
163
164 void tst_qdeclarativelistreference::isValid()
165 {
166     TestType *tt = new TestType;
167
168     {
169     QDeclarativeListReference ref;
170     QVERIFY(ref.isValid() == false);
171     }
172
173     {
174     QDeclarativeListReference ref(tt, "blah");
175     QVERIFY(ref.isValid() == false);
176     }
177
178     {
179     QDeclarativeListReference ref(tt, "data");
180     QVERIFY(ref.isValid() == true);
181     delete tt;
182     QVERIFY(ref.isValid() == false);
183     }
184 }
185
186 void tst_qdeclarativelistreference::object()
187 {
188     TestType *tt = new TestType;
189
190     {
191     QDeclarativeListReference ref;
192     QVERIFY(ref.object() == 0);
193     }
194
195     {
196     QDeclarativeListReference ref(tt, "blah");
197     QVERIFY(ref.object() == 0);
198     }
199
200     {
201     QDeclarativeListReference ref(tt, "data");
202     QVERIFY(ref.object() == tt);
203     delete tt;
204     QVERIFY(ref.object() == 0);
205     }
206 }
207
208 void tst_qdeclarativelistreference::listElementType()
209 {
210     TestType *tt = new TestType;
211
212     {
213     QDeclarativeListReference ref;
214     QVERIFY(ref.listElementType() == 0);
215     }
216
217     {
218     QDeclarativeListReference ref(tt, "blah");
219     QVERIFY(ref.listElementType() == 0);
220     }
221
222     {
223     QDeclarativeListReference ref(tt, "data");
224     QVERIFY(ref.listElementType() == &TestType::staticMetaObject);
225     delete tt;
226     QVERIFY(ref.listElementType() == 0);
227     }
228 }
229
230 void tst_qdeclarativelistreference::canAppend()
231 {
232     TestType *tt = new TestType;
233
234     {
235     QDeclarativeListReference ref;
236     QVERIFY(ref.canAppend() == false);
237     }
238
239     {
240     QDeclarativeListReference ref(tt, "blah");
241     QVERIFY(ref.canAppend() == false);
242     }
243
244     {
245     QDeclarativeListReference ref(tt, "data");
246     QVERIFY(ref.canAppend() == true);
247     delete tt;
248     QVERIFY(ref.canAppend() == false);
249     }
250
251     {
252     TestType tt;
253     tt.property.append = 0;
254     QDeclarativeListReference ref(&tt, "data");
255     QVERIFY(ref.canAppend() == false);
256     }
257 }
258
259 void tst_qdeclarativelistreference::canAt()
260 {
261     TestType *tt = new TestType;
262
263     {
264     QDeclarativeListReference ref;
265     QVERIFY(ref.canAt() == false);
266     }
267
268     {
269     QDeclarativeListReference ref(tt, "blah");
270     QVERIFY(ref.canAt() == false);
271     }
272
273     {
274     QDeclarativeListReference ref(tt, "data");
275     QVERIFY(ref.canAt() == true);
276     delete tt;
277     QVERIFY(ref.canAt() == false);
278     }
279
280     {
281     TestType tt;
282     tt.property.at = 0;
283     QDeclarativeListReference ref(&tt, "data");
284     QVERIFY(ref.canAt() == false);
285     }
286 }
287
288 void tst_qdeclarativelistreference::canClear()
289 {
290     TestType *tt = new TestType;
291
292     {
293     QDeclarativeListReference ref;
294     QVERIFY(ref.canClear() == false);
295     }
296
297     {
298     QDeclarativeListReference ref(tt, "blah");
299     QVERIFY(ref.canClear() == false);
300     }
301
302     {
303     QDeclarativeListReference ref(tt, "data");
304     QVERIFY(ref.canClear() == true);
305     delete tt;
306     QVERIFY(ref.canClear() == false);
307     }
308
309     {
310     TestType tt;
311     tt.property.clear = 0;
312     QDeclarativeListReference ref(&tt, "data");
313     QVERIFY(ref.canClear() == false);
314     }
315 }
316
317 void tst_qdeclarativelistreference::canCount()
318 {
319     TestType *tt = new TestType;
320
321     {
322     QDeclarativeListReference ref;
323     QVERIFY(ref.canCount() == false);
324     }
325
326     {
327     QDeclarativeListReference ref(tt, "blah");
328     QVERIFY(ref.canCount() == false);
329     }
330
331     {
332     QDeclarativeListReference ref(tt, "data");
333     QVERIFY(ref.canCount() == true);
334     delete tt;
335     QVERIFY(ref.canCount() == false);
336     }
337
338     {
339     TestType tt;
340     tt.property.count = 0;
341     QDeclarativeListReference ref(&tt, "data");
342     QVERIFY(ref.canCount() == false);
343     }
344 }
345
346 void tst_qdeclarativelistreference::append()
347 {
348     TestType *tt = new TestType;
349     QObject object;
350
351     {
352     QDeclarativeListReference ref;
353     QVERIFY(ref.append(tt) == false);
354     }
355
356     {
357     QDeclarativeListReference ref(tt, "blah");
358     QVERIFY(ref.append(tt) == false);
359     }
360
361     {
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);
373     delete tt;
374     QVERIFY(ref.append(0) == false);
375     }
376
377     {
378     TestType tt;
379     tt.property.append = 0;
380     QDeclarativeListReference ref(&tt, "data");
381     QVERIFY(ref.append(&tt) == false);
382     }
383 }
384
385 void tst_qdeclarativelistreference::at()
386 {
387     TestType *tt = new TestType;
388     tt->data.append(tt);
389     tt->data.append(0);
390     tt->data.append(tt);
391
392     {
393     QDeclarativeListReference ref;
394     QVERIFY(ref.at(0) == 0);
395     }
396
397     {
398     QDeclarativeListReference ref(tt, "blah");
399     QVERIFY(ref.at(0) == 0);
400     }
401
402     {
403     QDeclarativeListReference ref(tt, "data");
404     QVERIFY(ref.at(0) == tt);
405     QVERIFY(ref.at(1) == 0);
406     QVERIFY(ref.at(2) == tt);
407     delete tt;
408     QVERIFY(ref.at(0) == 0);
409     }
410
411     {
412     TestType tt;
413     tt.data.append(&tt);
414     tt.property.at = 0;
415     QDeclarativeListReference ref(&tt, "data");
416     QVERIFY(ref.at(0) == 0);
417     }
418 }
419
420 void tst_qdeclarativelistreference::clear()
421 {
422     TestType *tt = new TestType;
423     tt->data.append(tt);
424     tt->data.append(0);
425     tt->data.append(tt);
426
427     {
428     QDeclarativeListReference ref;
429     QVERIFY(ref.clear() == false);
430     }
431
432     {
433     QDeclarativeListReference ref(tt, "blah");
434     QVERIFY(ref.clear() == false);
435     }
436
437     {
438     QDeclarativeListReference ref(tt, "data");
439     QVERIFY(ref.clear() == true);
440     QVERIFY(tt->data.count() == 0);
441     delete tt;
442     QVERIFY(ref.clear() == false);
443     }
444
445     {
446     TestType tt;
447     tt.property.clear = 0;
448     QDeclarativeListReference ref(&tt, "data");
449     QVERIFY(ref.clear() == false);
450     }
451 }
452
453 void tst_qdeclarativelistreference::count()
454 {
455     TestType *tt = new TestType;
456     tt->data.append(tt);
457     tt->data.append(0);
458     tt->data.append(tt);
459
460     {
461     QDeclarativeListReference ref;
462     QVERIFY(ref.count() == 0);
463     }
464
465     {
466     QDeclarativeListReference ref(tt, "blah");
467     QVERIFY(ref.count() == 0);
468     }
469
470     {
471     QDeclarativeListReference ref(tt, "data");
472     QVERIFY(ref.count() == 3);
473     tt->data.removeAt(1);
474     QVERIFY(ref.count() == 2);
475     delete tt;
476     QVERIFY(ref.count() == 0);
477     }
478
479     {
480     TestType tt;
481     tt.data.append(&tt);
482     tt.property.count = 0;
483     QDeclarativeListReference ref(&tt, "data");
484     QVERIFY(ref.count() == 0);
485     }
486 }
487
488 void tst_qdeclarativelistreference::copy()
489 {
490     TestType tt;
491     tt.data.append(&tt);
492     tt.data.append(0);
493     tt.data.append(&tt);
494
495     QDeclarativeListReference *r1 = new QDeclarativeListReference(&tt, "data");
496     QVERIFY(r1->count() == 3);
497
498     QDeclarativeListReference r2(*r1);
499     QDeclarativeListReference r3;
500     r3 = *r1;
501
502     QVERIFY(r2.count() == 3);
503     QVERIFY(r3.count() == 3);
504
505     delete r1;
506
507     QVERIFY(r2.count() == 3);
508     QVERIFY(r3.count() == 3);
509
510     tt.data.removeAt(2);
511
512     QVERIFY(r2.count() == 2);
513     QVERIFY(r3.count() == 2);
514 }
515
516 void tst_qdeclarativelistreference::qmlmetaproperty()
517 {
518     TestType tt;
519     tt.data.append(&tt);
520     tt.data.append(0);
521     tt.data.append(&tt);
522
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);
529 }
530
531 void tst_qdeclarativelistreference::engineTypes()
532 {
533     QDeclarativeEngine engine;
534     QDeclarativeComponent component(&engine, testFileUrl("engineTypes.qml"));
535
536     QObject *o = component.create();
537     QVERIFY(o);
538
539     QDeclarativeProperty p1(o, QLatin1String("myList"));
540     QVERIFY(p1.propertyTypeCategory() == QDeclarativeProperty::Normal);
541
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);
550
551     delete o;
552 }
553
554 void tst_qdeclarativelistreference::variantToList()
555 {
556     QDeclarativeEngine engine;
557     QDeclarativeComponent component(&engine, testFileUrl("variantToList.qml"));
558
559     QObject *o = component.create();
560     QVERIFY(o);
561
562     QVERIFY(o->property("value").userType() == qMetaTypeId<QDeclarativeListReference>());
563     QCOMPARE(o->property("test").toInt(), 1);
564
565     delete o;
566 }
567
568 QTEST_MAIN(tst_qdeclarativelistreference)
569
570 #include "tst_qdeclarativelistreference.moc"