Renamed QQuickItem::pos property to position
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qquickbinding / tst_qquickbinding.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
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"
47
48 class tst_qquickbinding : public QQmlDataTest
49 {
50     Q_OBJECT
51 public:
52     tst_qquickbinding();
53
54 private slots:
55     void binding();
56     void whenAfterValue();
57     void restoreBinding();
58     void restoreBindingWithLoop();
59     void restoreBindingWithoutCrash();
60     void deletedObject();
61
62 private:
63     QQmlEngine engine;
64 };
65
66 tst_qquickbinding::tst_qquickbinding()
67 {
68 }
69
70 void tst_qquickbinding::binding()
71 {
72     QQmlEngine engine;
73     QQmlComponent c(&engine, testFileUrl("test-binding.qml"));
74     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
75     QVERIFY(rect != 0);
76
77     QQuickBind *binding3 = qobject_cast<QQuickBind*>(rect->findChild<QQuickBind*>("binding3"));
78     QVERIFY(binding3 != 0);
79
80     QCOMPARE(rect->color(), QColor("yellow"));
81     QCOMPARE(rect->property("text").toString(), QString("Hello"));
82     QCOMPARE(binding3->when(), false);
83
84     rect->setProperty("changeColor", true);
85     QCOMPARE(rect->color(), QColor("red"));
86
87     QCOMPARE(binding3->when(), true);
88
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"));
94
95     delete rect;
96 }
97
98 void tst_qquickbinding::whenAfterValue()
99 {
100     QQmlEngine engine;
101     QQmlComponent c(&engine, testFileUrl("test-binding2.qml"));
102     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
103
104     QVERIFY(rect != 0);
105     QCOMPARE(rect->color(), QColor("yellow"));
106     QCOMPARE(rect->property("text").toString(), QString("Hello"));
107
108     rect->setProperty("changeColor", true);
109     QCOMPARE(rect->color(), QColor("red"));
110
111     delete rect;
112 }
113
114 void tst_qquickbinding::restoreBinding()
115 {
116     QQmlEngine engine;
117     QQmlComponent c(&engine, testFileUrl("restoreBinding.qml"));
118     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
119     QVERIFY(rect != 0);
120
121     QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
122     QVERIFY(myItem != 0);
123
124     myItem->setY(25);
125     QCOMPARE(myItem->x(), qreal(100-25));
126
127     myItem->setY(13);
128     QCOMPARE(myItem->x(), qreal(100-13));
129
130     //Binding takes effect
131     myItem->setY(51);
132     QCOMPARE(myItem->x(), qreal(51));
133
134     myItem->setY(88);
135     QCOMPARE(myItem->x(), qreal(88));
136
137     //original binding restored
138     myItem->setY(49);
139     QCOMPARE(myItem->x(), qreal(100-49));
140
141     delete rect;
142 }
143
144 void tst_qquickbinding::restoreBindingWithLoop()
145 {
146     QQmlEngine engine;
147     QQmlComponent c(&engine, testFileUrl("restoreBindingWithLoop.qml"));
148     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
149     QVERIFY(rect != 0);
150
151     QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
152     QVERIFY(myItem != 0);
153
154     myItem->setY(25);
155     QCOMPARE(myItem->x(), qreal(25 + 100));
156
157     myItem->setY(13);
158     QCOMPARE(myItem->x(), qreal(13 + 100));
159
160     //Binding takes effect
161     rect->setProperty("activateBinding", true);
162     myItem->setY(51);
163     QCOMPARE(myItem->x(), qreal(51));
164
165     myItem->setY(88);
166     QCOMPARE(myItem->x(), qreal(88));
167
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
173
174     myItem->setY(49);
175     QCOMPARE(myItem->x(), qreal(49 + 100));
176
177     delete rect;
178 }
179
180 void tst_qquickbinding::restoreBindingWithoutCrash()
181 {
182     QQmlEngine engine;
183     QQmlComponent c(&engine, testFileUrl("restoreBindingWithoutCrash.qml"));
184     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
185     QVERIFY(rect != 0);
186
187     QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
188     QVERIFY(myItem != 0);
189
190     myItem->setY(25);
191     QCOMPARE(myItem->x(), qreal(100-25));
192
193     myItem->setY(13);
194     QCOMPARE(myItem->x(), qreal(100-13));
195
196     //Binding takes effect
197     myItem->setY(51);
198     QCOMPARE(myItem->x(), qreal(51));
199
200     myItem->setY(88);
201     QCOMPARE(myItem->x(), qreal(88));
202
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));
207
208     //Binding still controls this value
209     myItem->setY(104);
210     QCOMPARE(myItem->x(), qreal(104));
211
212     //original binding restored
213     myItem->setY(49);
214     QCOMPARE(myItem->x(), qreal(100-49));
215
216     delete rect;
217 }
218
219 //QTBUG-20692
220 void tst_qquickbinding::deletedObject()
221 {
222     QQmlEngine engine;
223     QQmlComponent c(&engine, testFileUrl("deletedObject.qml"));
224     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
225     QVERIFY(rect != 0);
226
227     QGuiApplication::sendPostedEvents(0, QEvent::DeferredDelete);
228
229     //don't crash
230     rect->setProperty("activateBinding", true);
231
232     delete rect;
233 }
234
235 QTEST_MAIN(tst_qquickbinding)
236
237 #include "tst_qquickbinding.moc"