aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlbinding
diff options
context:
space:
mode:
authorTasuku Suzuki <stasuku@gmail.com>2012-11-25 04:36:33 +0900
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-11-29 17:48:46 +0100
commite0cb13a510c0099784fee00d5b4b7c608dd42dd2 (patch)
tree8d61daaaa70df2fa7b26a0c989d748fc3718e15f /tests/auto/qml/qqmlbinding
parent55f6a109e99ea2eb3359fa941a1826d4b4e11bf8 (diff)
Move no-Gui related QML types from QtQuick into QtQml
Task-number: QTBUG-26340 Change-Id: I9049128db2598bf3c7a9d677b774eaae53b54eb5 Reviewed-by: Alan Alpert <aalpert@rim.com> Reviewed-by: Lars Knoll <lars.knoll@digia.com> Reviewed-by: Gunnar Sletta <gunnar.sletta@digia.com>
Diffstat (limited to 'tests/auto/qml/qqmlbinding')
-rw-r--r--tests/auto/qml/qqmlbinding/data/deletedObject.qml24
-rw-r--r--tests/auto/qml/qqmlbinding/data/restoreBinding.qml26
-rw-r--r--tests/auto/qml/qqmlbinding/data/restoreBindingWithLoop.qml23
-rw-r--r--tests/auto/qml/qqmlbinding/data/restoreBindingWithoutCrash.qml28
-rw-r--r--tests/auto/qml/qqmlbinding/data/test-binding.qml16
-rw-r--r--tests/auto/qml/qqmlbinding/data/test-binding2.qml16
-rw-r--r--tests/auto/qml/qqmlbinding/qqmlbinding.pro14
-rw-r--r--tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp237
8 files changed, 384 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlbinding/data/deletedObject.qml b/tests/auto/qml/qqmlbinding/data/deletedObject.qml
new file mode 100644
index 0000000000..f9cf869ba3
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/data/deletedObject.qml
@@ -0,0 +1,24 @@
+import QtQuick 2.0
+
+Rectangle {
+ id: wrapper
+ width: 400
+ height: 400
+
+ property bool activateBinding: false
+
+ Binding {
+ id: binding
+ target: Qt.createQmlObject('import QtQuick 2.0; Item { property real value: 10 }', wrapper)
+ property: "value"
+ when: activateBinding
+ value: x + y
+ }
+
+ Component.onCompleted: binding.target.destroy();
+
+// MouseArea {
+// anchors.fill: parent
+// onClicked: activateBinding = true;
+// }
+}
diff --git a/tests/auto/qml/qqmlbinding/data/restoreBinding.qml b/tests/auto/qml/qqmlbinding/data/restoreBinding.qml
new file mode 100644
index 0000000000..9491c0f1d3
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/data/restoreBinding.qml
@@ -0,0 +1,26 @@
+import QtQuick 2.0
+
+Rectangle {
+ width: 400
+ height: 400
+
+ Rectangle {
+ id: myItem
+ objectName: "myItem"
+ width: 100
+ height: 100
+ color: "green"
+ x: 100 - myItem.y
+
+ Binding on x {
+ when: myItem.y > 50
+ value: myItem.y
+ }
+
+ /*NumberAnimation on y {
+ loops: Animation.Infinite
+ to: 100
+ duration: 1000
+ }*/
+ }
+}
diff --git a/tests/auto/qml/qqmlbinding/data/restoreBindingWithLoop.qml b/tests/auto/qml/qqmlbinding/data/restoreBindingWithLoop.qml
new file mode 100644
index 0000000000..ee07104817
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/data/restoreBindingWithLoop.qml
@@ -0,0 +1,23 @@
+import QtQuick 2.0
+
+Rectangle {
+ width: 400
+ height: 400
+
+ property bool activateBinding: false
+
+ Rectangle {
+ id: myItem
+ objectName: "myItem"
+ width: 100
+ height: 100
+ color: "green"
+ x: myItem.y + 100
+ onXChanged: { if (x == 188) y = 90; } //create binding loop
+
+ Binding on x {
+ when: activateBinding
+ value: myItem.y
+ }
+ }
+}
diff --git a/tests/auto/qml/qqmlbinding/data/restoreBindingWithoutCrash.qml b/tests/auto/qml/qqmlbinding/data/restoreBindingWithoutCrash.qml
new file mode 100644
index 0000000000..0c63a16213
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/data/restoreBindingWithoutCrash.qml
@@ -0,0 +1,28 @@
+import QtQuick 2.0
+
+Rectangle {
+ width: 400
+ height: 400
+
+ states: State {
+ name: "state1"
+ PropertyChanges {
+ target: myItem
+ x: 200 - myItem.y
+ }
+ }
+
+ Rectangle {
+ id: myItem
+ objectName: "myItem"
+ width: 100
+ height: 100
+ color: "green"
+ x: 100 - myItem.y
+
+ Binding on x {
+ when: myItem.y > 50
+ value: myItem.y
+ }
+ }
+}
diff --git a/tests/auto/qml/qqmlbinding/data/test-binding.qml b/tests/auto/qml/qqmlbinding/data/test-binding.qml
new file mode 100644
index 0000000000..87aabe975a
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/data/test-binding.qml
@@ -0,0 +1,16 @@
+import QtQuick 2.0
+
+Rectangle {
+ id: screen
+ width: 320; height: 240
+ property string text
+ property bool changeColor: false
+
+ Text { id: s1; text: "Hello" }
+ Rectangle { id: r1; width: 1; height: 1; color: "yellow" }
+ Rectangle { id: r2; width: 1; height: 1; color: "red" }
+
+ Binding { target: screen; property: "text"; value: s1.text; objectName: "binding1" }
+ Binding { target: screen; property: "color"; value: r1.color }
+ Binding { target: screen; property: "color"; when: screen.changeColor == true; value: r2.color; objectName: "binding3" }
+}
diff --git a/tests/auto/qml/qqmlbinding/data/test-binding2.qml b/tests/auto/qml/qqmlbinding/data/test-binding2.qml
new file mode 100644
index 0000000000..4a08141d11
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/data/test-binding2.qml
@@ -0,0 +1,16 @@
+import QtQuick 2.0
+
+Rectangle {
+ id: screen
+ width: 320; height: 240
+ property string text
+ property bool changeColor: false
+
+ Text { id: s1; text: "Hello" }
+ Rectangle { id: r1; width: 1; height: 1; color: "yellow" }
+ Rectangle { id: r2; width: 1; height: 1; color: "red" }
+
+ Binding { target: screen; property: "text"; value: s1.text }
+ Binding { target: screen; property: "color"; value: r1.color }
+ Binding { target: screen; property: "color"; value: r2.color; when: screen.changeColor == true }
+}
diff --git a/tests/auto/qml/qqmlbinding/qqmlbinding.pro b/tests/auto/qml/qqmlbinding/qqmlbinding.pro
new file mode 100644
index 0000000000..35a5f1ca74
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/qqmlbinding.pro
@@ -0,0 +1,14 @@
+CONFIG += testcase
+TARGET = tst_qqmlbinding
+macx:CONFIG -= app_bundle
+
+SOURCES += tst_qqmlbinding.cpp
+
+include (../../shared/util.pri)
+
+TESTDATA = data/*
+
+CONFIG += parallel_test
+
+QT += core-private gui-private qml-private quick-private testlib
+DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0
diff --git a/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp b/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp
new file mode 100644
index 0000000000..4c411fb5ec
--- /dev/null
+++ b/tests/auto/qml/qqmlbinding/tst_qqmlbinding.cpp
@@ -0,0 +1,237 @@
+/****************************************************************************
+**
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include <qtest.h>
+#include <QtQml/qqmlengine.h>
+#include <QtQml/qqmlcomponent.h>
+#include <private/qqmlbind_p.h>
+#include <QtQuick/private/qquickrectangle_p.h>
+#include "../../shared/util.h"
+
+class tst_qqmlbinding : public QQmlDataTest
+{
+ Q_OBJECT
+public:
+ tst_qqmlbinding();
+
+private slots:
+ void binding();
+ void whenAfterValue();
+ void restoreBinding();
+ void restoreBindingWithLoop();
+ void restoreBindingWithoutCrash();
+ void deletedObject();
+
+private:
+ QQmlEngine engine;
+};
+
+tst_qqmlbinding::tst_qqmlbinding()
+{
+}
+
+void tst_qqmlbinding::binding()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("test-binding.qml"));
+ QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+ QVERIFY(rect != 0);
+
+ QQmlBind *binding3 = qobject_cast<QQmlBind*>(rect->findChild<QQmlBind*>("binding3"));
+ QVERIFY(binding3 != 0);
+
+ QCOMPARE(rect->color(), QColor("yellow"));
+ QCOMPARE(rect->property("text").toString(), QString("Hello"));
+ QCOMPARE(binding3->when(), false);
+
+ rect->setProperty("changeColor", true);
+ QCOMPARE(rect->color(), QColor("red"));
+
+ QCOMPARE(binding3->when(), true);
+
+ QQmlBind *binding = qobject_cast<QQmlBind*>(rect->findChild<QQmlBind*>("binding1"));
+ QVERIFY(binding != 0);
+ QCOMPARE(binding->object(), qobject_cast<QObject*>(rect));
+ QCOMPARE(binding->property(), QLatin1String("text"));
+ QCOMPARE(binding->value().toString(), QLatin1String("Hello"));
+
+ delete rect;
+}
+
+void tst_qqmlbinding::whenAfterValue()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("test-binding2.qml"));
+ QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+
+ QVERIFY(rect != 0);
+ QCOMPARE(rect->color(), QColor("yellow"));
+ QCOMPARE(rect->property("text").toString(), QString("Hello"));
+
+ rect->setProperty("changeColor", true);
+ QCOMPARE(rect->color(), QColor("red"));
+
+ delete rect;
+}
+
+void tst_qqmlbinding::restoreBinding()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("restoreBinding.qml"));
+ QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+ QVERIFY(rect != 0);
+
+ QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
+ QVERIFY(myItem != 0);
+
+ myItem->setY(25);
+ QCOMPARE(myItem->x(), qreal(100-25));
+
+ myItem->setY(13);
+ QCOMPARE(myItem->x(), qreal(100-13));
+
+ //Binding takes effect
+ myItem->setY(51);
+ QCOMPARE(myItem->x(), qreal(51));
+
+ myItem->setY(88);
+ QCOMPARE(myItem->x(), qreal(88));
+
+ //original binding restored
+ myItem->setY(49);
+ QCOMPARE(myItem->x(), qreal(100-49));
+
+ delete rect;
+}
+
+void tst_qqmlbinding::restoreBindingWithLoop()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("restoreBindingWithLoop.qml"));
+ QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+ QVERIFY(rect != 0);
+
+ QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
+ QVERIFY(myItem != 0);
+
+ myItem->setY(25);
+ QCOMPARE(myItem->x(), qreal(25 + 100));
+
+ myItem->setY(13);
+ QCOMPARE(myItem->x(), qreal(13 + 100));
+
+ //Binding takes effect
+ rect->setProperty("activateBinding", true);
+ myItem->setY(51);
+ QCOMPARE(myItem->x(), qreal(51));
+
+ myItem->setY(88);
+ QCOMPARE(myItem->x(), qreal(88));
+
+ //original binding restored
+ QString warning = c.url().toString() + QLatin1String(":9:5: QML Rectangle: Binding loop detected for property \"x\"");
+ QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
+ rect->setProperty("activateBinding", false);
+ QCOMPARE(myItem->x(), qreal(88 + 100)); //if loop handling changes this could be 90 + 100
+
+ myItem->setY(49);
+ QCOMPARE(myItem->x(), qreal(49 + 100));
+
+ delete rect;
+}
+
+void tst_qqmlbinding::restoreBindingWithoutCrash()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("restoreBindingWithoutCrash.qml"));
+ QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+ QVERIFY(rect != 0);
+
+ QQuickRectangle *myItem = qobject_cast<QQuickRectangle*>(rect->findChild<QQuickRectangle*>("myItem"));
+ QVERIFY(myItem != 0);
+
+ myItem->setY(25);
+ QCOMPARE(myItem->x(), qreal(100-25));
+
+ myItem->setY(13);
+ QCOMPARE(myItem->x(), qreal(100-13));
+
+ //Binding takes effect
+ myItem->setY(51);
+ QCOMPARE(myItem->x(), qreal(51));
+
+ myItem->setY(88);
+ QCOMPARE(myItem->x(), qreal(88));
+
+ //state sets a new binding
+ rect->setState("state1");
+ //this binding temporarily takes effect. We may want to change this behavior in the future
+ QCOMPARE(myItem->x(), qreal(112));
+
+ //Binding still controls this value
+ myItem->setY(104);
+ QCOMPARE(myItem->x(), qreal(104));
+
+ //original binding restored
+ myItem->setY(49);
+ QCOMPARE(myItem->x(), qreal(100-49));
+
+ delete rect;
+}
+
+//QTBUG-20692
+void tst_qqmlbinding::deletedObject()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, testFileUrl("deletedObject.qml"));
+ QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
+ QVERIFY(rect != 0);
+
+ QGuiApplication::sendPostedEvents(0, QEvent::DeferredDelete);
+
+ //don't crash
+ rect->setProperty("activateBinding", true);
+
+ delete rect;
+}
+
+QTEST_MAIN(tst_qqmlbinding)
+
+#include "tst_qqmlbinding.moc"