diff options
author | Chris Adams <christopher.adams@nokia.com> | 2012-03-21 13:24:58 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-26 04:55:30 +0200 |
commit | d3bcce569f699177ccb98d7a21d96519dd70949a (patch) | |
tree | 01b804433f8ab381da976c8fecd9c967c83c1b41 /tests | |
parent | 6cdfad980d760ef87b8f425cf1affda16fa76a62 (diff) |
Add templated module api registration function
This commit adds a templated module api registration function which
allows clients to provide type information at registration time.
We use this typeinformation in V4 if available, in order to allow
module APIs to be used in v4 bindings.
This commit also clarifies the ownership semantics of QObject module
apis, and updates some documentation references which were missed
during the rename of Declarative to Qml.
Task-number: QTBUG-24894
Change-Id: Iebb61ca8d8eacbb15218549eab715e22f52a1474
Reviewed-by: Michael Brasser <michael.brasser@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlecmascript/data/moduleapi/qobjectModuleApiLegacy.qml | 13 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/testtypes.cpp | 17 | ||||
-rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 11 | ||||
-rw-r--r-- | tests/auto/qml/qquickconnection/tst_qquickconnection.cpp | 2 | ||||
-rw-r--r-- | tests/auto/qml/v4/data/moduleApi.qml | 12 | ||||
-rw-r--r-- | tests/auto/qml/v4/tst_v4.cpp | 41 |
6 files changed, 87 insertions, 9 deletions
diff --git a/tests/auto/qml/qqmlecmascript/data/moduleapi/qobjectModuleApiLegacy.qml b/tests/auto/qml/qqmlecmascript/data/moduleapi/qobjectModuleApiLegacy.qml new file mode 100644 index 0000000000..e282ad64f2 --- /dev/null +++ b/tests/auto/qml/qqmlecmascript/data/moduleapi/qobjectModuleApiLegacy.qml @@ -0,0 +1,13 @@ +import QtQuick 2.0 + +import Qt.test.legacyModuleApi 1.0 as ModApi // was registered with non-templated function + +QtObject { + property int legacyModulePropertyTest: ModApi.qobjectTestProperty + property int legacyModuleMethodTest + + Component.onCompleted: { + legacyModuleMethodTest = ModApi.qobjectTestMethod(); + } +} + diff --git a/tests/auto/qml/qqmlecmascript/testtypes.cpp b/tests/auto/qml/qqmlecmascript/testtypes.cpp index a79207a1c8..d674fa3eb6 100644 --- a/tests/auto/qml/qqmlecmascript/testtypes.cpp +++ b/tests/auto/qml/qqmlecmascript/testtypes.cpp @@ -187,14 +187,15 @@ void registerTypes() qRegisterMetaType<MyQmlObject::MyType>("MyQmlObject::MyType"); - qmlRegisterModuleApi("Qt.test",1,0,script_api); // register (script) module API for an existing uri which contains elements - qmlRegisterModuleApi("Qt.test",1,0,qobject_api); // register (qobject) for an existing uri for which another module API was previously regd. Should replace! - qmlRegisterModuleApi("Qt.test.scriptApi",1,0,script_api); // register (script) module API for a uri which doesn't contain elements - qmlRegisterModuleApi("Qt.test.scriptApi",2,0,readonly_script_api); // register (script) module API for a uri which doesn't contain elements - will be made read-only - qmlRegisterModuleApi("Qt.test.qobjectApi",1,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements - qmlRegisterModuleApi("Qt.test.qobjectApi",1,3,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, minor version set - qmlRegisterModuleApi("Qt.test.qobjectApi",2,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, major version set - qmlRegisterModuleApi("Qt.test.qobjectApiParented",1,0,qobject_api_engine_parent); // register (parented qobject) module API for a uri which doesn't contain elements + qmlRegisterModuleApi("Qt.test",1,0,script_api); // register (script) module API for an existing uri which contains elements + qmlRegisterModuleApi<testQObjectApi>("Qt.test",1,0,qobject_api); // register (qobject) for an existing uri for which another module API was previously regd. Should replace! + qmlRegisterModuleApi("Qt.test.scriptApi",1,0,script_api); // register (script) module API for a uri which doesn't contain elements + qmlRegisterModuleApi("Qt.test.scriptApi",2,0,readonly_script_api); // register (script) module API for a uri which doesn't contain elements - will be made read-only + qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApi",1,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements + qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApi",1,3,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, minor version set + qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApi",2,0,qobject_api); // register (qobject) module API for a uri which doesn't contain elements, major version set + qmlRegisterModuleApi<testQObjectApi>("Qt.test.qobjectApiParented",1,0,qobject_api_engine_parent); // register (parented qobject) module API for a uri which doesn't contain elements + qmlRegisterModuleApi("Qt.test.legacyModuleApi", 1, 0, qobject_api); // this registration function doesn't provide type information. qRegisterMetaType<MyQmlObject::MyEnum2>("MyEnum2"); qRegisterMetaType<Qt::MouseButtons>("Qt::MouseButtons"); diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index fa1a6a5920..eaa6d39ea4 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -3372,6 +3372,17 @@ void tst_qqmlecmascript::moduleApi_data() << QVariantList() << QStringList() << QVariantList(); + + QTest::newRow("legacy module api registration") + << testFileUrl("moduleapi/qobjectModuleApiLegacy.qml") + << QString() + << QStringList() // warning doesn't occur in the test, but in registerTypes() + << (QStringList() << "legacyModulePropertyTest" << "legacyModuleMethodTest") + << (QVariantList() << 20 << 2) + << QStringList() + << QVariantList() + << QStringList() + << QVariantList(); } void tst_qqmlecmascript::moduleApi() diff --git a/tests/auto/qml/qquickconnection/tst_qquickconnection.cpp b/tests/auto/qml/qquickconnection/tst_qquickconnection.cpp index ad687dd085..1167281eb0 100644 --- a/tests/auto/qml/qquickconnection/tst_qquickconnection.cpp +++ b/tests/auto/qml/qquickconnection/tst_qquickconnection.cpp @@ -263,7 +263,7 @@ static QObject *module_api_factory(QQmlEngine *engine, QJSEngine *scriptEngine) // QTBUG-20937 void tst_qquickconnection::moduleApiTarget() { - qmlRegisterModuleApi("MyTestModuleApi", 1, 0, module_api_factory); + qmlRegisterModuleApi<MyTestModuleApi>("MyTestModuleApi", 1, 0, module_api_factory); QQmlComponent component(&engine, testFileUrl("moduleapi-target.qml")); QObject *object = component.create(); QVERIFY(object != 0); diff --git a/tests/auto/qml/v4/data/moduleApi.qml b/tests/auto/qml/v4/data/moduleApi.qml new file mode 100644 index 0000000000..9f3bf0ca8c --- /dev/null +++ b/tests/auto/qml/v4/data/moduleApi.qml @@ -0,0 +1,12 @@ +import Qt.test 1.0 as ModApi +import QtQuick 2.0 + +Item { + property int testProp: ModApi.ip + property int testProp2: 2 + + function getRandom() { + testProp2 = ModApi.random(); + // testProp should also have changed. + } +} diff --git a/tests/auto/qml/v4/tst_v4.cpp b/tests/auto/qml/v4/tst_v4.cpp index 8c811f230e..47fa10b595 100644 --- a/tests/auto/qml/v4/tst_v4.cpp +++ b/tests/auto/qml/v4/tst_v4.cpp @@ -81,6 +81,7 @@ private slots: void mathCeil(); void mathMax(); void mathMin(); + void moduleApi(); private: QQmlEngine engine; @@ -589,6 +590,46 @@ void tst_v4::mathMin() delete o; } +class V4ModuleApi : public QObject +{ + Q_OBJECT + Q_PROPERTY(int ip READ ip WRITE setIp NOTIFY ipChanged FINAL) +public: + V4ModuleApi() : m_ip(12) {} + ~V4ModuleApi() {} + + Q_INVOKABLE int random() { static int prng = 3; prng++; m_ip++; emit ipChanged(); return prng; } + + int ip() const { return m_ip; } + void setIp(int v) { m_ip = v; emit ipChanged(); } + +signals: + void ipChanged(); + +private: + int m_ip; +}; + +static QObject *v4_module_api_factory(QQmlEngine*, QJSEngine*) +{ + return new V4ModuleApi; +} + +void tst_v4::moduleApi() +{ + // register module api, providing typeinfo via template + qmlRegisterModuleApi<V4ModuleApi>("Qt.test", 1, 0, v4_module_api_factory); + QQmlComponent component(&engine, testFileUrl("moduleApi.qml")); + QObject *o = component.create(); + QVERIFY(o != 0); + QCOMPARE(o->property("testProp").toInt(), 12); + QCOMPARE(o->property("testProp2").toInt(), 2); + QMetaObject::invokeMethod(o, "getRandom"); + QCOMPARE(o->property("testProp").toInt(), 13); + QCOMPARE(o->property("testProp2").toInt(), 4); + delete o; +} + QTEST_MAIN(tst_v4) #include "tst_v4.moc" |