Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativebinding / tst_qdeclarativebinding.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
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"
47
48 class tst_qdeclarativebinding : public QDeclarativeDataTest
49 {
50     Q_OBJECT
51 public:
52     tst_qdeclarativebinding();
53
54 private slots:
55     void binding();
56     void whenAfterValue();
57     void restoreBinding();
58     void restoreBindingWithLoop();
59     void deletedObject();
60
61 private:
62     QDeclarativeEngine engine;
63 };
64
65 tst_qdeclarativebinding::tst_qdeclarativebinding()
66 {
67 }
68
69 void tst_qdeclarativebinding::binding()
70 {
71     QDeclarativeEngine engine;
72     QDeclarativeComponent c(&engine, testFileUrl("test-binding.qml"));
73     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
74     QVERIFY(rect != 0);
75
76     QDeclarativeBind *binding3 = qobject_cast<QDeclarativeBind*>(rect->findChild<QDeclarativeBind*>("binding3"));
77     QVERIFY(binding3 != 0);
78
79     QCOMPARE(rect->color(), QColor("yellow"));
80     QCOMPARE(rect->property("text").toString(), QString("Hello"));
81     QCOMPARE(binding3->when(), false);
82
83     rect->setProperty("changeColor", true);
84     QCOMPARE(rect->color(), QColor("red"));
85
86     QCOMPARE(binding3->when(), true);
87
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"));
93
94     delete rect;
95 }
96
97 void tst_qdeclarativebinding::whenAfterValue()
98 {
99     QDeclarativeEngine engine;
100     QDeclarativeComponent c(&engine, testFileUrl("test-binding2.qml"));
101     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
102
103     QVERIFY(rect != 0);
104     QCOMPARE(rect->color(), QColor("yellow"));
105     QCOMPARE(rect->property("text").toString(), QString("Hello"));
106
107     rect->setProperty("changeColor", true);
108     QCOMPARE(rect->color(), QColor("red"));
109
110     delete rect;
111 }
112
113 void tst_qdeclarativebinding::restoreBinding()
114 {
115     QDeclarativeEngine engine;
116     QDeclarativeComponent c(&engine, testFileUrl("restoreBinding.qml"));
117     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
118     QVERIFY(rect != 0);
119
120     QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
121     QVERIFY(myItem != 0);
122
123     myItem->setY(25);
124     QCOMPARE(myItem->x(), qreal(100-25));
125
126     myItem->setY(13);
127     QCOMPARE(myItem->x(), qreal(100-13));
128
129     //Binding takes effect
130     myItem->setY(51);
131     QCOMPARE(myItem->x(), qreal(51));
132
133     myItem->setY(88);
134     QCOMPARE(myItem->x(), qreal(88));
135
136     //original binding restored
137     myItem->setY(49);
138     QCOMPARE(myItem->x(), qreal(100-49));
139
140     delete rect;
141 }
142
143 void tst_qdeclarativebinding::restoreBindingWithLoop()
144 {
145     QDeclarativeEngine engine;
146     QDeclarativeComponent c(&engine, testFileUrl("restoreBindingWithLoop.qml"));
147     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
148     QVERIFY(rect != 0);
149
150     QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
151     QVERIFY(myItem != 0);
152
153     myItem->setY(25);
154     QCOMPARE(myItem->x(), qreal(25 + 100));
155
156     myItem->setY(13);
157     QCOMPARE(myItem->x(), qreal(13 + 100));
158
159     //Binding takes effect
160     rect->setProperty("activateBinding", true);
161     myItem->setY(51);
162     QCOMPARE(myItem->x(), qreal(51));
163
164     myItem->setY(88);
165     QCOMPARE(myItem->x(), qreal(88));
166
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
172
173     myItem->setY(49);
174     QCOMPARE(myItem->x(), qreal(49 + 100));
175
176     delete rect;
177 }
178
179 //QTBUG-20692
180 void tst_qdeclarativebinding::deletedObject()
181 {
182     QDeclarativeEngine engine;
183     QDeclarativeComponent c(&engine, testFileUrl("deletedObject.qml"));
184     QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
185     QVERIFY(rect != 0);
186
187     QGuiApplication::sendPostedEvents(0, QEvent::DeferredDelete);
188
189     //don't crash
190     rect->setProperty("activateBinding", true);
191
192     delete rect;
193 }
194
195 QTEST_MAIN(tst_qdeclarativebinding)
196
197 #include "tst_qdeclarativebinding.moc"