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