1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include <QtQml/qqmlengine.h>
43 #include <QtQml/qqmlcomponent.h>
44 #include <private/qquickbind_p.h>
45 #include <QtQuick/private/qquickrectangle_p.h>
46 #include "../../shared/util.h"
48 class tst_qquickbinding : public QQmlDataTest
56 void whenAfterValue();
57 void restoreBinding();
58 void restoreBindingWithLoop();
59 void restoreBindingWithoutCrash();
66 tst_qquickbinding::tst_qquickbinding()
70 void tst_qquickbinding::binding()
73 QQmlComponent c(&engine, testFileUrl("test-binding.qml"));
74 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
77 QQuickBind *binding3 = qobject_cast<QQuickBind*>(rect->findChild<QQuickBind*>("binding3"));
78 QVERIFY(binding3 != 0);
80 QCOMPARE(rect->color(), QColor("yellow"));
81 QCOMPARE(rect->property("text").toString(), QString("Hello"));
82 QCOMPARE(binding3->when(), false);
84 rect->setProperty("changeColor", true);
85 QCOMPARE(rect->color(), QColor("red"));
87 QCOMPARE(binding3->when(), true);
89 QQuickBind *binding = qobject_cast<QQuickBind*>(rect->findChild<QQuickBind*>("binding1"));
90 QVERIFY(binding != 0);
91 QCOMPARE(binding->object(), qobject_cast<QObject*>(rect));
92 QCOMPARE(binding->property(), QLatin1String("text"));
93 QCOMPARE(binding->value().toString(), QLatin1String("Hello"));
98 void tst_qquickbinding::whenAfterValue()
101 QQmlComponent c(&engine, testFileUrl("test-binding2.qml"));
102 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
105 QCOMPARE(rect->color(), QColor("yellow"));
106 QCOMPARE(rect->property("text").toString(), QString("Hello"));
108 rect->setProperty("changeColor", true);
109 QCOMPARE(rect->color(), QColor("red"));
114 void tst_qquickbinding::restoreBinding()
117 QQmlComponent c(&engine, testFileUrl("restoreBinding.qml"));
118 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
121 QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
122 QVERIFY(myItem != 0);
125 QCOMPARE(myItem->x(), qreal(100-25));
128 QCOMPARE(myItem->x(), qreal(100-13));
130 //Binding takes effect
132 QCOMPARE(myItem->x(), qreal(51));
135 QCOMPARE(myItem->x(), qreal(88));
137 //original binding restored
139 QCOMPARE(myItem->x(), qreal(100-49));
144 void tst_qquickbinding::restoreBindingWithLoop()
147 QQmlComponent c(&engine, testFileUrl("restoreBindingWithLoop.qml"));
148 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
151 QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
152 QVERIFY(myItem != 0);
155 QCOMPARE(myItem->x(), qreal(25 + 100));
158 QCOMPARE(myItem->x(), qreal(13 + 100));
160 //Binding takes effect
161 rect->setProperty("activateBinding", true);
163 QCOMPARE(myItem->x(), qreal(51));
166 QCOMPARE(myItem->x(), qreal(88));
168 //original binding restored
169 QString warning = c.url().toString() + QLatin1String(":9:5: QML Rectangle: Binding loop detected for property \"x\"");
170 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
171 rect->setProperty("activateBinding", false);
172 QCOMPARE(myItem->x(), qreal(88 + 100)); //if loop handling changes this could be 90 + 100
175 QCOMPARE(myItem->x(), qreal(49 + 100));
180 void tst_qquickbinding::restoreBindingWithoutCrash()
183 QQmlComponent c(&engine, testFileUrl("restoreBindingWithoutCrash.qml"));
184 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
187 QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
188 QVERIFY(myItem != 0);
191 QCOMPARE(myItem->x(), qreal(100-25));
194 QCOMPARE(myItem->x(), qreal(100-13));
196 //Binding takes effect
198 QCOMPARE(myItem->x(), qreal(51));
201 QCOMPARE(myItem->x(), qreal(88));
203 //state sets a new binding
204 rect->setState("state1");
205 //this binding temporarily takes effect. We may want to change this behavior in the future
206 QCOMPARE(myItem->x(), qreal(112));
208 //Binding still controls this value
210 QCOMPARE(myItem->x(), qreal(104));
212 //original binding restored
214 QCOMPARE(myItem->x(), qreal(100-49));
220 void tst_qquickbinding::deletedObject()
223 QQmlComponent c(&engine, testFileUrl("deletedObject.qml"));
224 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
227 QGuiApplication::sendPostedEvents(0, QEvent::DeferredDelete);
230 rect->setProperty("activateBinding", true);
235 QTEST_MAIN(tst_qquickbinding)
237 #include "tst_qquickbinding.moc"