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/tst_qdeclarativeconnection.cpp | |
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/tst_qdeclarativeconnection.cpp')
-rw-r--r-- | tests/auto/declarative/qdeclarativeconnection/tst_qdeclarativeconnection.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
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" |