1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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>
52 class tst_qdeclarativebehaviors : public QObject
56 tst_qdeclarativebehaviors() {}
59 void simpleBehavior();
60 void scriptTriggered();
64 void parentBehavior();
65 void replaceBinding();
66 //void transitionOverrides();
69 void explicitSelection();
70 void nonSelectingBehavior();
71 void reassignedAnimation();
75 void groupedPropertyCrash();
78 void delayedRegistration();
81 void tst_qdeclarativebehaviors::simpleBehavior()
83 QDeclarativeEngine engine;
84 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/simple.qml"));
85 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
87 QTRY_VERIFY(qobject_cast<QDeclarative1Behavior*>(rect->findChild<QDeclarative1Behavior*>("MyBehavior"))->animation());
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
97 void tst_qdeclarativebehaviors::scriptTriggered()
99 QDeclarativeEngine engine;
100 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/scripttrigger.qml"));
101 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
112 void tst_qdeclarativebehaviors::cppTriggered()
114 QDeclarativeEngine engine;
115 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/cpptrigger.qml"));
116 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
119 QDeclarative1Rectangle *innerRect = qobject_cast<QDeclarative1Rectangle*>(rect->findChild<QDeclarative1Rectangle*>("MyRect"));
120 QTRY_VERIFY(innerRect);
122 innerRect->setProperty("x", 200);
123 QTRY_VERIFY(innerRect->x() > 0);
124 QTRY_VERIFY(innerRect->x() < 200); //i.e. the behavior has been triggered
129 void tst_qdeclarativebehaviors::loop()
131 QDeclarativeEngine engine;
132 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/loop.qml"));
133 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
137 QDeclarativeItemPrivate::get(rect)->setState("moved");
142 void tst_qdeclarativebehaviors::colorBehavior()
144 QDeclarativeEngine engine;
145 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/color.qml"));
146 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
157 void tst_qdeclarativebehaviors::parentBehavior()
159 QDeclarativeEngine engine;
160 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/parent.qml"));
161 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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"));
171 void tst_qdeclarativebehaviors::replaceBinding()
173 QDeclarativeEngine engine;
174 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/binding.qml"));
175 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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);
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);
202 void tst_qdeclarativebehaviors::group()
205 QDeclarativeEngine engine;
206 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/groupProperty.qml"));
207 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
210 QDeclarativeItemPrivate::get(rect)->setState("moved");
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
220 QDeclarativeEngine engine;
221 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/groupProperty2.qml"));
222 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
234 void tst_qdeclarativebehaviors::emptyBehavior()
236 QDeclarativeEngine engine;
237 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/empty.qml"));
238 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
248 void tst_qdeclarativebehaviors::explicitSelection()
250 QDeclarativeEngine engine;
251 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/explicit.qml"));
252 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
263 void tst_qdeclarativebehaviors::nonSelectingBehavior()
265 QDeclarativeEngine engine;
266 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/nonSelecting2.qml"));
267 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
277 void tst_qdeclarativebehaviors::reassignedAnimation()
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());
285 QCOMPARE(qobject_cast<QDeclarative1NumberAnimation*>(
286 rect->findChild<QDeclarative1Behavior*>("MyBehavior")->animation())->duration(), 200);
291 void tst_qdeclarativebehaviors::disabled()
293 QDeclarativeEngine engine;
294 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/disabled.qml"));
295 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
297 QCOMPARE(rect->findChild<QDeclarative1Behavior*>("MyBehavior")->enabled(), false);
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
306 void tst_qdeclarativebehaviors::dontStart()
308 QDeclarativeEngine engine;
310 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontStart.qml"));
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());
317 QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
318 QVERIFY(myAnim && myAnim->qtAnimation());
319 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
324 void tst_qdeclarativebehaviors::startup()
327 QDeclarativeEngine engine;
328 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/startup.qml"));
329 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
332 QDeclarative1Rectangle *innerRect = rect->findChild<QDeclarative1Rectangle*>("innerRect");
335 QCOMPARE(innerRect->x(), qreal(100)); //should be set immediately
341 QDeclarativeEngine engine;
342 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/startup2.qml"));
343 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
346 QDeclarative1Rectangle *innerRect = rect->findChild<QDeclarative1Rectangle*>("innerRect");
349 QDeclarative1Text *text = rect->findChild<QDeclarative1Text*>();
352 QCOMPARE(innerRect->x(), text->width()); //should be set immediately
359 void tst_qdeclarativebehaviors::groupedPropertyCrash()
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
368 void tst_qdeclarativebehaviors::runningTrue()
370 QDeclarativeEngine engine;
371 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/runningTrue.qml"));
372 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
375 QDeclarative1AbstractAnimation *animation = rect->findChild<QDeclarative1AbstractAnimation*>("rotAnim");
378 QSignalSpy runningSpy(animation, SIGNAL(runningChanged(bool)));
379 rect->setProperty("myValue", 180);
380 QTRY_VERIFY(runningSpy.count() > 0);
384 void tst_qdeclarativebehaviors::sameValue()
386 QDeclarativeEngine engine;
387 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/qtbug12295.qml"));
388 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
391 QDeclarative1Rectangle *target = rect->findChild<QDeclarative1Rectangle*>("myRect");
395 QCOMPARE(target->x(), qreal(100));
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.
402 QCOMPARE(target->x(), qreal(100));
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));
411 void tst_qdeclarativebehaviors::delayedRegistration()
413 QDeclarativeEngine engine;
415 QDeclarativeComponent c(&engine, SRCDIR "/data/delayedRegistration.qml");
416 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
419 QDeclarativeItem *innerRect = rect->property("myItem").value<QDeclarativeItem*>();
420 QVERIFY(innerRect != 0);
422 QCOMPARE(innerRect->property("x").toInt(), int(0));
424 QTRY_COMPARE(innerRect->property("x").toInt(), int(100));
427 QTEST_MAIN(tst_qdeclarativebehaviors)
429 #include "tst_qdeclarativebehaviors.moc"