diff options
author | Albert Astals Cid <albert.astals@canonical.com> | 2013-11-13 11:03:52 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-11-14 08:03:55 +0100 |
commit | 1df8a42e5b7fc718121dc92dd7203046d25015e0 (patch) | |
tree | 140b430265e1f92d70236e792d08e92356a016c8 /tests/auto/qml | |
parent | 3afffa47feabc80e1bc20ffd2143a722a1c360a2 (diff) |
Allow passing qmlRegisterSingletonType QObjects to C++ as QObject*
At the moment you can pass them as their FinalType* or
as one of their ParentType* but not as QObject* which
does not make much sense to me
Task-number: QTBUG-34617
Task-number: QTBUG-30730
Change-Id: Id5cfb7bbb123456ef43f44f33b450f8966a7641a
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/singletonTest.qml | 48 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.cpp | 24 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.h | 10 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 17 |
4 files changed, 99 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmlecmascript/data/singletonTest.qml b/tests/auto/qml/qqmlecmascript/data/singletonTest.qml new file mode 100644 index 0000000000..6d296cd613 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/singletonTest.qml @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Canonical Limited and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names +** of its contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import Test 1.0 + +Item { + property bool qobjectTest: MyInheritedQmlObjectSingleton.isItYouQObject(MyInheritedQmlObjectSingleton) + property bool myQmlObjectTest: MyInheritedQmlObjectSingleton.isItYouMyQmlObject(MyInheritedQmlObjectSingleton) + property bool myInheritedQmlObjectTest: MyInheritedQmlObjectSingleton.isItYouMyInheritedQmlObject(MyInheritedQmlObjectSingleton) +} diff --git a/tests/auto/qml/qqmlecmascript/testtypes.cpp b/tests/auto/qml/qqmlecmascript/testtypes.cpp index a09bd9f3c6..053281f230 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.cpp +++ b/tests/auto/qml/qqmlecmascript/testtypes.cpp @@ -257,10 +257,34 @@ public: } }; +static MyInheritedQmlObject *theSingletonObject = 0; + +static QObject *inheritedQmlObject_provider(QQmlEngine* /* engine */, QJSEngine* /* scriptEngine */) +{ + theSingletonObject = new MyInheritedQmlObject(); + return theSingletonObject; +} + +bool MyInheritedQmlObject::isItYouQObject(QObject *o) +{ + return o && o == theSingletonObject; +} + +bool MyInheritedQmlObject::isItYouMyQmlObject(MyQmlObject *o) +{ + return o && o == theSingletonObject; +} + +bool MyInheritedQmlObject::isItYouMyInheritedQmlObject(MyInheritedQmlObject *o) +{ + return o && o == theSingletonObject; +} + void registerTypes() { qmlRegisterType<MyQmlObject>("Qt.test", 1,0, "MyQmlObjectAlias"); qmlRegisterType<MyQmlObject>("Qt.test", 1,0, "MyQmlObject"); + qmlRegisterSingletonType<MyInheritedQmlObject>("Test", 1, 0, "MyInheritedQmlObjectSingleton", inheritedQmlObject_provider); qmlRegisterType<MyDeferredObject>("Qt.test", 1,0, "MyDeferredObject"); qmlRegisterType<MyVeryDeferredObject>("Qt.test", 1,0, "MyVeryDeferredObject"); qmlRegisterType<MyQmlContainer>("Qt.test", 1,0, "MyQmlContainer"); diff --git a/tests/auto/qml/qqmlecmascript/testtypes.h b/tests/auto/qml/qqmlecmascript/testtypes.h index 52a619637c..6dcef2dc6e 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.h +++ b/tests/auto/qml/qqmlecmascript/testtypes.h @@ -291,6 +291,16 @@ Q_DECLARE_METATYPE(QQmlListProperty<MyQmlObject>) QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES) +class MyInheritedQmlObject : public MyQmlObject +{ + Q_OBJECT +public: + Q_INVOKABLE bool isItYouQObject(QObject *o); + Q_INVOKABLE bool isItYouMyQmlObject(MyQmlObject *o); + Q_INVOKABLE bool isItYouMyInheritedQmlObject(MyInheritedQmlObject *o); +}; +QML_DECLARE_TYPE(MyInheritedQmlObject) + class MyQmlContainer : public QObject { Q_OBJECT diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index defa708de5..609fb1d7a3 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -310,6 +310,7 @@ private slots: void thisObject(); void qtbug_33754(); void qtbug_34493(); + void singletonFromQMLToCpp(); private: // static void propertyVarWeakRefCallback(v8::Persistent<v8::Value> object, void* parameter); @@ -7344,6 +7345,22 @@ void tst_qqmlecmascript::qtbug_34493() QTRY_VERIFY(obj->property("prop").toString() == QLatin1String("Hello World!")); } +// Check that a Singleton can be passed from QML to C++ +// as its type*, it's parent type* and as QObject* +void tst_qqmlecmascript::singletonFromQMLToCpp() +{ + QQmlComponent component(&engine, testFile("singletonTest.qml")); + QScopedPointer<QObject> obj(component.create()); + if (component.errors().size()) + qDebug() << component.errors(); + QVERIFY(component.errors().isEmpty()); + QVERIFY(obj != 0); + + QCOMPARE(obj->property("qobjectTest"), QVariant(true)); + QCOMPARE(obj->property("myQmlObjectTest"), QVariant(true)); + QCOMPARE(obj->property("myInheritedQmlObjectTest"), QVariant(true)); +} + QTEST_MAIN(tst_qqmlecmascript) #include "tst_qqmlecmascript.moc" |