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