diff options
author | Chris Adams <christopher.adams@nokia.com> | 2011-08-22 15:51:28 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-09-02 01:34:21 +0200 |
commit | 6080375fed90c09bfabb96a0319817f14f693b05 (patch) | |
tree | 2f91acc1e37cf5a747086066b741fa68fa0bbe57 /tests/auto/declarative/qdeclarativeconnection | |
parent | d481f2ff518df1e44103d1850e7d52bd69260c34 (diff) |
Allow conversion of QObject Module API to QVariant
This commit adds a conversion codepath for QV8TypeResource to QVariant
where the resource contains a QObject module API. This allows such a
module API to be used as the "target" in a Connections element.
Task-number: QTBUG-20937
Change-Id: I9214b531968f2e6981a86e643859a97297c6a02a
Reviewed-on: http://codereview.qt.nokia.com/3286
Reviewed-by: Chris Adams <christopher.adams@nokia.com>
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Diffstat (limited to 'tests/auto/declarative/qdeclarativeconnection')
-rw-r--r-- | tests/auto/declarative/qdeclarativeconnection/data/moduleapi-target.qml | 22 | ||||
-rw-r--r-- | tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp | 64 |
2 files changed, 86 insertions, 0 deletions
diff --git a/tests/auto/declarative/qdeclarativeconnection/data/moduleapi-target.qml b/tests/auto/declarative/qdeclarativeconnection/data/moduleapi-target.qml new file mode 100644 index 0000000000..8803f24542 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeconnection/data/moduleapi-target.qml @@ -0,0 +1,22 @@ +import QtQuick 2.0 +import MyTestModuleApi 1.0 as MyTestModuleApi + +Item { + id: rootObject + objectName: "rootObject" + property int newIntPropValue: 12 + + property int moduleIntPropChangedCount: 0 + property int moduleOtherSignalCount: 0 + + function setModuleIntProp() { + MyTestModuleApi.intProp = newIntPropValue; + newIntPropValue = newIntPropValue + 1; + } + + Connections { + target: MyTestModuleApi + onIntPropChanged: moduleIntPropChangedCount = moduleIntPropChangedCount + 1; + onOtherSignal: moduleOtherSignalCount = moduleOtherSignalCount + 1; + } +} diff --git a/tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp b/tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp index 37cce5c578..c726fde0e8 100644 --- a/tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp +++ b/tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp @@ -68,6 +68,7 @@ private slots: void unknownSignals(); void errors_data(); void errors(); + void moduleApiTarget(); private: QDeclarativeEngine engine; @@ -229,6 +230,69 @@ void tst_qdeclarativeconnection::errors() QCOMPARE(errors.at(0).description(), error); } + +class MyTestModuleApi : public QObject +{ +Q_OBJECT +Q_PROPERTY(int intProp READ intProp WRITE setIntProp NOTIFY intPropChanged) + +public: + MyTestModuleApi(QObject *parent = 0) : QObject(parent), m_intProp(0), m_changeCount(0) {} + ~MyTestModuleApi() {} + + Q_INVOKABLE int otherMethod(int val) { return val + 4; } + + int intProp() const { return m_intProp; } + void setIntProp(int val) + { + if (++m_changeCount % 3 == 0) emit otherSignal(); + m_intProp = val; emit intPropChanged(); + } + +signals: + void intPropChanged(); + void otherSignal(); + +private: + int m_intProp; + int m_changeCount; +}; + +static QObject *module_api_factory(QDeclarativeEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + MyTestModuleApi *api = new MyTestModuleApi(); + return api; +} + +// QTBUG-20937 +void tst_qdeclarativeconnection::moduleApiTarget() +{ + qmlRegisterModuleApi("MyTestModuleApi", 1, 0, module_api_factory); + QDeclarativeComponent component(&engine, QUrl(SRCDIR "/data/moduleapi-target.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 0); + QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0); + + QMetaObject::invokeMethod(object, "setModuleIntProp"); + QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 1); + QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0); + + QMetaObject::invokeMethod(object, "setModuleIntProp"); + QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 2); + QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 0); + + // the module API emits otherSignal every 3 times the int property changes. + QMetaObject::invokeMethod(object, "setModuleIntProp"); + QCOMPARE(object->property("moduleIntPropChangedCount").toInt(), 3); + QCOMPARE(object->property("moduleOtherSignalCount").toInt(), 1); + + delete object; +} + QTEST_MAIN(tst_qdeclarativeconnection) #include "tst_qdeclarativeconnection.moc" |