1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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>
51 #include "../../../shared/util.h"
53 class tst_qdeclarativebehaviors : public QObject
57 tst_qdeclarativebehaviors() {}
60 void simpleBehavior();
61 void scriptTriggered();
65 void parentBehavior();
66 void replaceBinding();
67 //void transitionOverrides();
70 void explicitSelection();
71 void nonSelectingBehavior();
72 void reassignedAnimation();
76 void groupedPropertyCrash();
79 void delayedRegistration();
82 void tst_qdeclarativebehaviors::simpleBehavior()
84 QDeclarativeEngine engine;
85 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/simple.qml"));
86 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
88 QTRY_VERIFY(qobject_cast<QDeclarative1Behavior*>(rect->findChild<QDeclarative1Behavior*>("MyBehavior"))->animation());
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
98 void tst_qdeclarativebehaviors::scriptTriggered()
100 QDeclarativeEngine engine;
101 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/scripttrigger.qml"));
102 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
113 void tst_qdeclarativebehaviors::cppTriggered()
115 QDeclarativeEngine engine;
116 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/cpptrigger.qml"));
117 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
120 QDeclarative1Rectangle *innerRect = qobject_cast<QDeclarative1Rectangle*>(rect->findChild<QDeclarative1Rectangle*>("MyRect"));
121 QTRY_VERIFY(innerRect);
123 innerRect->setProperty("x", 200);
124 QTRY_VERIFY(innerRect->x() > 0);
125 QTRY_VERIFY(innerRect->x() < 200); //i.e. the behavior has been triggered
130 void tst_qdeclarativebehaviors::loop()
132 QDeclarativeEngine engine;
133 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/loop.qml"));
134 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
138 QDeclarativeItemPrivate::get(rect)->setState("moved");
143 void tst_qdeclarativebehaviors::colorBehavior()
145 QDeclarativeEngine engine;
146 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/color.qml"));
147 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
158 void tst_qdeclarativebehaviors::parentBehavior()
160 QDeclarativeEngine engine;
161 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/parent.qml"));
162 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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"));
172 void tst_qdeclarativebehaviors::replaceBinding()
174 QDeclarativeEngine engine;
175 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/binding.qml"));
176 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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);
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);
203 void tst_qdeclarativebehaviors::group()
206 QDeclarativeEngine engine;
207 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/groupProperty.qml"));
208 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
211 QDeclarativeItemPrivate::get(rect)->setState("moved");
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
221 QDeclarativeEngine engine;
222 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/groupProperty2.qml"));
223 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
235 void tst_qdeclarativebehaviors::emptyBehavior()
237 QDeclarativeEngine engine;
238 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/empty.qml"));
239 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
249 void tst_qdeclarativebehaviors::explicitSelection()
251 QDeclarativeEngine engine;
252 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/explicit.qml"));
253 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
264 void tst_qdeclarativebehaviors::nonSelectingBehavior()
266 QDeclarativeEngine engine;
267 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/nonSelecting2.qml"));
268 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
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
278 void tst_qdeclarativebehaviors::reassignedAnimation()
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());
286 QCOMPARE(qobject_cast<QDeclarative1NumberAnimation*>(
287 rect->findChild<QDeclarative1Behavior*>("MyBehavior")->animation())->duration(), 200);
292 void tst_qdeclarativebehaviors::disabled()
294 QDeclarativeEngine engine;
295 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/disabled.qml"));
296 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
298 QCOMPARE(rect->findChild<QDeclarative1Behavior*>("MyBehavior")->enabled(), false);
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
307 void tst_qdeclarativebehaviors::dontStart()
309 QDeclarativeEngine engine;
311 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontStart.qml"));
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());
318 QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
319 QVERIFY(myAnim && myAnim->qtAnimation());
320 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
325 void tst_qdeclarativebehaviors::startup()
328 QDeclarativeEngine engine;
329 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/startup.qml"));
330 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
333 QDeclarative1Rectangle *innerRect = rect->findChild<QDeclarative1Rectangle*>("innerRect");
336 QCOMPARE(innerRect->x(), qreal(100)); //should be set immediately
342 QDeclarativeEngine engine;
343 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/startup2.qml"));
344 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
347 QDeclarative1Rectangle *innerRect = rect->findChild<QDeclarative1Rectangle*>("innerRect");
350 QDeclarative1Text *text = rect->findChild<QDeclarative1Text*>();
353 QCOMPARE(innerRect->x(), text->width()); //should be set immediately
360 void tst_qdeclarativebehaviors::groupedPropertyCrash()
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
369 void tst_qdeclarativebehaviors::runningTrue()
371 QDeclarativeEngine engine;
372 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/runningTrue.qml"));
373 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
376 QDeclarative1AbstractAnimation *animation = rect->findChild<QDeclarative1AbstractAnimation*>("rotAnim");
379 QSignalSpy runningSpy(animation, SIGNAL(runningChanged(bool)));
380 rect->setProperty("myValue", 180);
381 QTRY_VERIFY(runningSpy.count() > 0);
385 void tst_qdeclarativebehaviors::sameValue()
387 QDeclarativeEngine engine;
388 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/qtbug12295.qml"));
389 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
392 QDeclarative1Rectangle *target = rect->findChild<QDeclarative1Rectangle*>("myRect");
396 QCOMPARE(target->x(), qreal(100));
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.
403 QCOMPARE(target->x(), qreal(100));
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));
412 void tst_qdeclarativebehaviors::delayedRegistration()
414 QDeclarativeEngine engine;
416 QDeclarativeComponent c(&engine, SRCDIR "/data/delayedRegistration.qml");
417 QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
420 QDeclarativeItem *innerRect = rect->property("myItem").value<QDeclarativeItem*>();
421 QVERIFY(innerRect != 0);
423 QCOMPARE(innerRect->property("x").toInt(), int(0));
425 QTRY_COMPARE(innerRect->property("x").toInt(), int(100));
428 QTEST_MAIN(tst_qdeclarativebehaviors)
430 #include "tst_qdeclarativebehaviors.moc"