1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <private/qdeclarativebind_p.h>
45 #include <QtQuick/private/qquickrectangle_p.h>
46 #include "../../shared/util.h"
48 class tst_qdeclarativebinding : public QDeclarativeDataTest
52 tst_qdeclarativebinding();
56 void whenAfterValue();
57 void restoreBinding();
58 void restoreBindingWithLoop();
62 QDeclarativeEngine engine;
65 tst_qdeclarativebinding::tst_qdeclarativebinding()
69 void tst_qdeclarativebinding::binding()
71 QDeclarativeEngine engine;
72 QDeclarativeComponent c(&engine, testFileUrl("test-binding.qml"));
73 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
76 QDeclarativeBind *binding3 = qobject_cast<QDeclarativeBind*>(rect->findChild<QDeclarativeBind*>("binding3"));
77 QVERIFY(binding3 != 0);
79 QCOMPARE(rect->color(), QColor("yellow"));
80 QCOMPARE(rect->property("text").toString(), QString("Hello"));
81 QCOMPARE(binding3->when(), false);
83 rect->setProperty("changeColor", true);
84 QCOMPARE(rect->color(), QColor("red"));
86 QCOMPARE(binding3->when(), true);
88 QDeclarativeBind *binding = qobject_cast<QDeclarativeBind*>(rect->findChild<QDeclarativeBind*>("binding1"));
89 QVERIFY(binding != 0);
90 QCOMPARE(binding->object(), qobject_cast<QObject*>(rect));
91 QCOMPARE(binding->property(), QLatin1String("text"));
92 QCOMPARE(binding->value().toString(), QLatin1String("Hello"));
97 void tst_qdeclarativebinding::whenAfterValue()
99 QDeclarativeEngine engine;
100 QDeclarativeComponent c(&engine, testFileUrl("test-binding2.qml"));
101 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
104 QCOMPARE(rect->color(), QColor("yellow"));
105 QCOMPARE(rect->property("text").toString(), QString("Hello"));
107 rect->setProperty("changeColor", true);
108 QCOMPARE(rect->color(), QColor("red"));
113 void tst_qdeclarativebinding::restoreBinding()
115 QDeclarativeEngine engine;
116 QDeclarativeComponent c(&engine, testFileUrl("restoreBinding.qml"));
117 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
120 QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
121 QVERIFY(myItem != 0);
124 QCOMPARE(myItem->x(), qreal(100-25));
127 QCOMPARE(myItem->x(), qreal(100-13));
129 //Binding takes effect
131 QCOMPARE(myItem->x(), qreal(51));
134 QCOMPARE(myItem->x(), qreal(88));
136 //original binding restored
138 QCOMPARE(myItem->x(), qreal(100-49));
143 void tst_qdeclarativebinding::restoreBindingWithLoop()
145 QDeclarativeEngine engine;
146 QDeclarativeComponent c(&engine, testFileUrl("restoreBindingWithLoop.qml"));
147 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
150 QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
151 QVERIFY(myItem != 0);
154 QCOMPARE(myItem->x(), qreal(25 + 100));
157 QCOMPARE(myItem->x(), qreal(13 + 100));
159 //Binding takes effect
160 rect->setProperty("activateBinding", true);
162 QCOMPARE(myItem->x(), qreal(51));
165 QCOMPARE(myItem->x(), qreal(88));
167 //original binding restored
168 QString warning = c.url().toString() + QLatin1String(":9:5: QML Rectangle: Binding loop detected for property \"x\"");
169 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
170 rect->setProperty("activateBinding", false);
171 QCOMPARE(myItem->x(), qreal(88 + 100)); //if loop handling changes this could be 90 + 100
174 QCOMPARE(myItem->x(), qreal(49 + 100));
180 void tst_qdeclarativebinding::deletedObject()
182 QDeclarativeEngine engine;
183 QDeclarativeComponent c(&engine, testFileUrl("deletedObject.qml"));
184 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
187 QGuiApplication::sendPostedEvents(0, QEvent::DeferredDelete);
190 rect->setProperty("activateBinding", true);
195 QTEST_MAIN(tst_qdeclarativebinding)
197 #include "tst_qdeclarativebinding.moc"