Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qdeclarativebehaviors / tst_qdeclarativebehaviors.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 #include <QtTest/QtTest>
42 #include <qsignalspy.h>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QtQuick/qquickview.h>
46 #include <QtQuick/private/qquickrectangle_p.h>
47 #include <QtQuick/private/qquicktext_p.h>
48 #include <QtQuick/private/qdeclarativebehavior_p.h>
49 #include <QtQuick/private/qdeclarativeanimation_p.h>
50 #include <private/qquickitem_p.h>
51 #include "../../shared/util.h"
52
53 class tst_qdeclarativebehaviors : public QDeclarativeDataTest
54 {
55     Q_OBJECT
56 public:
57     tst_qdeclarativebehaviors() {}
58
59 private slots:
60     void init() { qApp->processEvents(); }  //work around animation timer bug (QTBUG-22865)
61     void simpleBehavior();
62     void scriptTriggered();
63     void cppTriggered();
64     void loop();
65     void colorBehavior();
66     void parentBehavior();
67     void replaceBinding();
68     //void transitionOverrides();
69     void group();
70     void valueType();
71     void emptyBehavior();
72     void explicitSelection();
73     void nonSelectingBehavior();
74     void reassignedAnimation();
75     void disabled();
76     void dontStart();
77     void startup();
78     void groupedPropertyCrash();
79     void runningTrue();
80     void sameValue();
81     void delayedRegistration();
82     void startOnCompleted();
83 };
84
85 void tst_qdeclarativebehaviors::simpleBehavior()
86 {
87     QDeclarativeEngine engine;
88     QDeclarativeComponent c(&engine, testFileUrl("simple.qml"));
89     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
90     QTRY_VERIFY(rect);
91     QTRY_VERIFY(qobject_cast<QDeclarativeBehavior*>(rect->findChild<QDeclarativeBehavior*>("MyBehavior"))->animation());
92
93     QQuickItemPrivate::get(rect)->setState("moved");
94     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
95     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
96     //i.e. the behavior has been triggered
97
98     delete rect;
99 }
100
101 void tst_qdeclarativebehaviors::scriptTriggered()
102 {
103     QDeclarativeEngine engine;
104     QDeclarativeComponent c(&engine, testFileUrl("scripttrigger.qml"));
105     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
106     QTRY_VERIFY(rect);
107
108     rect->setColor(QColor("red"));
109     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
110     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
111     //i.e. the behavior has been triggered
112
113     delete rect;
114 }
115
116 void tst_qdeclarativebehaviors::cppTriggered()
117 {
118     QDeclarativeEngine engine;
119     QDeclarativeComponent c(&engine, testFileUrl("cpptrigger.qml"));
120     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
121     QTRY_VERIFY(rect);
122
123     QQuickRectangle *innerRect = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"));
124     QTRY_VERIFY(innerRect);
125
126     innerRect->setProperty("x", 200);
127     QTRY_VERIFY(innerRect->x() > 0);
128     QTRY_VERIFY(innerRect->x() < 200);  //i.e. the behavior has been triggered
129
130     delete rect;
131 }
132
133 void tst_qdeclarativebehaviors::loop()
134 {
135     QDeclarativeEngine engine;
136     QDeclarativeComponent c(&engine, testFileUrl("loop.qml"));
137     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
138     QTRY_VERIFY(rect);
139
140     //don't crash
141     QQuickItemPrivate::get(rect)->setState("moved");
142
143     delete rect;
144 }
145
146 void tst_qdeclarativebehaviors::colorBehavior()
147 {
148     QDeclarativeEngine engine;
149     QDeclarativeComponent c(&engine, testFileUrl("color.qml"));
150     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
151     QTRY_VERIFY(rect);
152
153     QQuickItemPrivate::get(rect)->setState("red");
154     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->color() != QColor("red"));
155     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->color() != QColor("green"));
156     //i.e. the behavior has been triggered
157
158     delete rect;
159 }
160
161 void tst_qdeclarativebehaviors::parentBehavior()
162 {
163     QDeclarativeEngine engine;
164     QDeclarativeComponent c(&engine, testFileUrl("parent.qml"));
165     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
166     QTRY_VERIFY(rect);
167
168     QQuickItemPrivate::get(rect)->setState("reparented");
169     QTRY_VERIFY(rect->findChild<QQuickRectangle*>("MyRect")->parentItem() != rect->findChild<QQuickItem*>("NewParent"));
170     QTRY_VERIFY(rect->findChild<QQuickRectangle*>("MyRect")->parentItem() == rect->findChild<QQuickItem*>("NewParent"));
171
172     delete rect;
173 }
174
175 void tst_qdeclarativebehaviors::replaceBinding()
176 {
177     QDeclarativeEngine engine;
178     QDeclarativeComponent c(&engine, testFileUrl("binding.qml"));
179     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
180     QTRY_VERIFY(rect);
181
182     QQuickItemPrivate::get(rect)->setState("moved");
183     QQuickRectangle *innerRect = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"));
184     QTRY_VERIFY(innerRect);
185     QTRY_VERIFY(innerRect->x() > 0);
186     QTRY_VERIFY(innerRect->x() < 200);
187     //i.e. the behavior has been triggered
188     QTRY_COMPARE(innerRect->x(), (qreal)200);
189     rect->setProperty("basex", 10);
190     QTRY_COMPARE(innerRect->x(), (qreal)200);
191     rect->setProperty("movedx", 210);
192     QTRY_COMPARE(innerRect->x(), (qreal)210);
193
194     QQuickItemPrivate::get(rect)->setState("");
195     QTRY_VERIFY(innerRect->x() > 10);
196     QTRY_VERIFY(innerRect->x() < 210);  //i.e. the behavior has been triggered
197     QTRY_COMPARE(innerRect->x(), (qreal)10);
198     rect->setProperty("movedx", 200);
199     QTRY_COMPARE(innerRect->x(), (qreal)10);
200     rect->setProperty("basex", 20);
201     QTRY_COMPARE(innerRect->x(), (qreal)20);
202
203     delete rect;
204 }
205
206 void tst_qdeclarativebehaviors::group()
207 {
208     /* XXX TODO Create a test element for this case.
209     {
210         QDeclarativeEngine engine;
211         QDeclarativeComponent c(&engine, testFileUrl("groupProperty.qml")));
212         QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
213         qDebug() << c.errorString();
214         QTRY_VERIFY(rect);
215
216         QQuickItemPrivate::get(rect)->setState("moved");
217         //QTest::qWait(200);
218         QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
219         QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
220         //i.e. the behavior has been triggered
221
222         delete rect;
223     }
224     */
225
226     {
227         QDeclarativeEngine engine;
228         QDeclarativeComponent c(&engine, testFileUrl("groupProperty2.qml"));
229         QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
230         QTRY_VERIFY(rect);
231
232         QQuickItemPrivate::get(rect)->setState("moved");
233         QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->border()->width() > 0);
234         QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->border()->width() < 4);
235         //i.e. the behavior has been triggered
236
237         delete rect;
238     }
239 }
240
241 void tst_qdeclarativebehaviors::valueType()
242 {
243     QDeclarativeEngine engine;
244     QDeclarativeComponent c(&engine, testFileUrl("valueType.qml"));
245     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
246     QVERIFY(rect);
247
248     //QTBUG-20827
249     QCOMPARE(rect->color(), QColor::fromRgb(255,0,255));
250
251     delete rect;
252 }
253
254 void tst_qdeclarativebehaviors::emptyBehavior()
255 {
256     QDeclarativeEngine engine;
257     QDeclarativeComponent c(&engine, testFileUrl("empty.qml"));
258     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
259     QVERIFY(rect);
260
261     QQuickItemPrivate::get(rect)->setState("moved");
262     qreal x = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x();
263     QCOMPARE(x, qreal(200));    //should change immediately
264
265     delete rect;
266 }
267
268 void tst_qdeclarativebehaviors::explicitSelection()
269 {
270     QDeclarativeEngine engine;
271     QDeclarativeComponent c(&engine, testFileUrl("explicit.qml"));
272     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
273     QVERIFY(rect);
274
275     QQuickItemPrivate::get(rect)->setState("moved");
276     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() > 0);
277     QTRY_VERIFY(qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x() < 200);
278     //i.e. the behavior has been triggered
279
280     delete rect;
281 }
282
283 void tst_qdeclarativebehaviors::nonSelectingBehavior()
284 {
285     QDeclarativeEngine engine;
286     QDeclarativeComponent c(&engine, testFileUrl("nonSelecting2.qml"));
287     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
288     QVERIFY(rect);
289
290     QQuickItemPrivate::get(rect)->setState("moved");
291     qreal x = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x();
292     QCOMPARE(x, qreal(200));    //should change immediately
293
294     delete rect;
295 }
296
297 void tst_qdeclarativebehaviors::reassignedAnimation()
298 {
299     QDeclarativeEngine engine;
300     QDeclarativeComponent c(&engine, testFileUrl("reassignedAnimation.qml"));
301     QString warning = testFileUrl("reassignedAnimation.qml").toString() + ":9:9: QML Behavior: Cannot change the animation assigned to a Behavior.";
302     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
303     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
304     QVERIFY(rect);
305     QCOMPARE(qobject_cast<QDeclarativeNumberAnimation*>(
306                  rect->findChild<QDeclarativeBehavior*>("MyBehavior")->animation())->duration(), 200);
307
308     delete rect;
309 }
310
311 void tst_qdeclarativebehaviors::disabled()
312 {
313     QDeclarativeEngine engine;
314     QDeclarativeComponent c(&engine, testFileUrl("disabled.qml"));
315     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
316     QVERIFY(rect);
317     QCOMPARE(rect->findChild<QDeclarativeBehavior*>("MyBehavior")->enabled(), false);
318
319     QQuickItemPrivate::get(rect)->setState("moved");
320     qreal x = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("MyRect"))->x();
321     QCOMPARE(x, qreal(200));    //should change immediately
322
323     delete rect;
324 }
325
326 void tst_qdeclarativebehaviors::dontStart()
327 {
328     QDeclarativeEngine engine;
329
330     QDeclarativeComponent c(&engine, testFileUrl("dontStart.qml"));
331
332     QString warning = c.url().toString() + ":13:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
333     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
334     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
335     QVERIFY(rect);
336
337     QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
338     QVERIFY(myAnim && myAnim->qtAnimation());
339     QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
340
341     delete rect;
342 }
343
344 void tst_qdeclarativebehaviors::startup()
345 {
346     {
347         QDeclarativeEngine engine;
348         QDeclarativeComponent c(&engine, testFileUrl("startup.qml"));
349         QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
350         QVERIFY(rect);
351
352         QQuickRectangle *innerRect = rect->findChild<QQuickRectangle*>("innerRect");
353         QVERIFY(innerRect);
354
355         QCOMPARE(innerRect->x(), qreal(100));    //should be set immediately
356
357         delete rect;
358     }
359
360     {
361         QDeclarativeEngine engine;
362         QDeclarativeComponent c(&engine, testFileUrl("startup2.qml"));
363         QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
364         QVERIFY(rect);
365
366         QQuickRectangle *innerRect = rect->findChild<QQuickRectangle*>("innerRect");
367         QVERIFY(innerRect);
368
369         QQuickText *text = rect->findChild<QQuickText*>();
370         QVERIFY(text);
371
372         QCOMPARE(innerRect->x(), text->width());    //should be set immediately
373
374         delete rect;
375     }
376 }
377
378 //QTBUG-10799
379 void tst_qdeclarativebehaviors::groupedPropertyCrash()
380 {
381     QDeclarativeEngine engine;
382     QDeclarativeComponent c(&engine, testFileUrl("groupedPropertyCrash.qml"));
383     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
384     QVERIFY(rect);  //don't crash
385
386     delete rect;
387 }
388
389 //QTBUG-5491
390 void tst_qdeclarativebehaviors::runningTrue()
391 {
392     QDeclarativeEngine engine;
393     QDeclarativeComponent c(&engine, testFileUrl("runningTrue.qml"));
394     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
395     QVERIFY(rect);
396
397     QDeclarativeAbstractAnimation *animation = rect->findChild<QDeclarativeAbstractAnimation*>("rotAnim");
398     QVERIFY(animation);
399
400     QSignalSpy runningSpy(animation, SIGNAL(runningChanged(bool)));
401     rect->setProperty("myValue", 180);
402     QTRY_VERIFY(runningSpy.count() > 0);
403
404     delete rect;
405 }
406
407 //QTBUG-12295
408 void tst_qdeclarativebehaviors::sameValue()
409 {
410     QDeclarativeEngine engine;
411     QDeclarativeComponent c(&engine, testFileUrl("qtbug12295.qml"));
412     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
413     QVERIFY(rect);
414
415     QQuickRectangle *target = rect->findChild<QQuickRectangle*>("myRect");
416     QVERIFY(target);
417
418     target->setX(100);
419     QCOMPARE(target->x(), qreal(100));
420
421     target->setProperty("x", 0);
422     QTRY_VERIFY(target->x() != qreal(0) && target->x() != qreal(100));
423     QTRY_VERIFY(target->x() == qreal(0));   //make sure Behavior has finished.
424
425     target->setX(100);
426     QCOMPARE(target->x(), qreal(100));
427
428     //this is the main point of the test -- the behavior needs to be triggered again
429     //even though we set 0 twice in a row.
430     target->setProperty("x", 0);
431     QTRY_VERIFY(target->x() != qreal(0) && target->x() != qreal(100));
432
433     delete rect;
434 }
435
436 //QTBUG-18362
437 void tst_qdeclarativebehaviors::delayedRegistration()
438 {
439     QDeclarativeEngine engine;
440
441     QDeclarativeComponent c(&engine, testFileUrl("delayedRegistration.qml"));
442     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
443     QVERIFY(rect != 0);
444
445     QQuickItem *innerRect = rect->property("myItem").value<QQuickItem*>();
446     QVERIFY(innerRect != 0);
447
448     QCOMPARE(innerRect->property("x").toInt(), int(0));
449
450     QTRY_COMPARE(innerRect->property("x").toInt(), int(100));
451 }
452
453 //QTBUG-22555
454 void tst_qdeclarativebehaviors::startOnCompleted()
455 {
456     QDeclarativeEngine engine;
457
458     QDeclarativeComponent c(&engine, testFileUrl("startOnCompleted.qml"));
459     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
460     QVERIFY(rect != 0);
461
462     QQuickItem *innerRect = rect->findChild<QQuickRectangle*>();
463     QVERIFY(innerRect != 0);
464
465     QCOMPARE(innerRect->property("x").toInt(), int(0));
466
467     QTRY_COMPARE(innerRect->property("x").toInt(), int(100));
468
469     delete rect;
470 }
471
472 QTEST_MAIN(tst_qdeclarativebehaviors)
473
474 #include "tst_qdeclarativebehaviors.moc"