From dc9602f6bb805751beb3b7963398679d12753e53 Mon Sep 17 00:00:00 2001 From: Bea Lam Date: Wed, 26 May 2010 14:47:11 +1000 Subject: Allow js files with '.pragma library' to be used from WorkerScript --- src/declarative/qml/qdeclarativeworkerscript.cpp | 1 + .../qdeclarativeworkerscript/data/BaseWorker.qml | 24 ++++++++++++ .../qdeclarativeworkerscript/data/script.js | 1 - .../data/script_fixed_return.js | 4 ++ .../qdeclarativeworkerscript/data/script_pragma.js | 6 +++ .../qdeclarativeworkerscript/data/worker.qml | 21 +---------- .../data/worker_pragma.qml | 6 +++ .../tst_qdeclarativeworkerscript.cpp | 43 ++++++++++++++++++---- 8 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativeworkerscript/data/BaseWorker.qml create mode 100644 tests/auto/declarative/qdeclarativeworkerscript/data/script_fixed_return.js create mode 100644 tests/auto/declarative/qdeclarativeworkerscript/data/script_pragma.js create mode 100644 tests/auto/declarative/qdeclarativeworkerscript/data/worker_pragma.qml diff --git a/src/declarative/qml/qdeclarativeworkerscript.cpp b/src/declarative/qml/qdeclarativeworkerscript.cpp index 1550351..2ca030e 100644 --- a/src/declarative/qml/qdeclarativeworkerscript.cpp +++ b/src/declarative/qml/qdeclarativeworkerscript.cpp @@ -295,6 +295,7 @@ void QDeclarativeWorkerScriptEnginePrivate::processLoad(int id, const QUrl &url) ctxt->pushScope(urlContext); ctxt->pushScope(activation); ctxt->setActivationObject(activation); + QDeclarativeScriptParser::extractPragmas(script); workerEngine->baseUrl = url; workerEngine->evaluate(script); diff --git a/tests/auto/declarative/qdeclarativeworkerscript/data/BaseWorker.qml b/tests/auto/declarative/qdeclarativeworkerscript/data/BaseWorker.qml new file mode 100644 index 0000000..d275ca8 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeworkerscript/data/BaseWorker.qml @@ -0,0 +1,24 @@ +import Qt 4.7 + +WorkerScript { + id: worker + + property variant response + + signal done() + + function testSend(value) { + worker.sendMessage(value) + } + + function compareLiteralResponse(expected) { + var e = eval('(' + expected + ')') + return worker.response == e + } + + onMessage: { + worker.response = messageObject + worker.done() + } +} + diff --git a/tests/auto/declarative/qdeclarativeworkerscript/data/script.js b/tests/auto/declarative/qdeclarativeworkerscript/data/script.js index 09199de..90aae26 100644 --- a/tests/auto/declarative/qdeclarativeworkerscript/data/script.js +++ b/tests/auto/declarative/qdeclarativeworkerscript/data/script.js @@ -2,4 +2,3 @@ WorkerScript.onMessage = function(msg) { WorkerScript.sendMessage(msg) } - diff --git a/tests/auto/declarative/qdeclarativeworkerscript/data/script_fixed_return.js b/tests/auto/declarative/qdeclarativeworkerscript/data/script_fixed_return.js new file mode 100644 index 0000000..14f6f17 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeworkerscript/data/script_fixed_return.js @@ -0,0 +1,4 @@ +WorkerScript.onMessage = function(msg) { + WorkerScript.sendMessage('Hello_World') +} + diff --git a/tests/auto/declarative/qdeclarativeworkerscript/data/script_pragma.js b/tests/auto/declarative/qdeclarativeworkerscript/data/script_pragma.js new file mode 100644 index 0000000..cb3b6d3 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeworkerscript/data/script_pragma.js @@ -0,0 +1,6 @@ +.pragma library + +WorkerScript.onMessage = function(msg) { + WorkerScript.sendMessage(msg) +} + diff --git a/tests/auto/declarative/qdeclarativeworkerscript/data/worker.qml b/tests/auto/declarative/qdeclarativeworkerscript/data/worker.qml index 5c7a5ff..1a20098 100644 --- a/tests/auto/declarative/qdeclarativeworkerscript/data/worker.qml +++ b/tests/auto/declarative/qdeclarativeworkerscript/data/worker.qml @@ -1,24 +1,5 @@ import Qt 4.7 -WorkerScript { - id: worker +BaseWorker { source: "script.js" - - property variant response - - signal done() - - function testSend(value) { - worker.sendMessage(value) - } - - function compareLiteralResponse(expected) { - var e = eval('(' + expected + ')') - return worker.response == e - } - - onMessage: { - worker.response = messageObject - worker.done() - } } diff --git a/tests/auto/declarative/qdeclarativeworkerscript/data/worker_pragma.qml b/tests/auto/declarative/qdeclarativeworkerscript/data/worker_pragma.qml new file mode 100644 index 0000000..3b720ef --- /dev/null +++ b/tests/auto/declarative/qdeclarativeworkerscript/data/worker_pragma.qml @@ -0,0 +1,6 @@ +import Qt 4.7 + +BaseWorker { + source: "script_pragma.js" +} + diff --git a/tests/auto/declarative/qdeclarativeworkerscript/tst_qdeclarativeworkerscript.cpp b/tests/auto/declarative/qdeclarativeworkerscript/tst_qdeclarativeworkerscript.cpp index a1dae24..7a4315a 100644 --- a/tests/auto/declarative/qdeclarativeworkerscript/tst_qdeclarativeworkerscript.cpp +++ b/tests/auto/declarative/qdeclarativeworkerscript/tst_qdeclarativeworkerscript.cpp @@ -64,6 +64,7 @@ private slots: void messaging_data(); void messaging_sendQObjectList(); void messaging_sendJsObject(); + void script_with_pragma(); private: void waitForEchoMessage(QDeclarativeWorkerScript *worker) { @@ -82,18 +83,25 @@ private: void tst_QDeclarativeWorkerScript::source() { - QUrl source = QUrl::fromLocalFile(SRCDIR "/data/worker.qml"); - - QDeclarativeComponent component(&m_engine); - component.setData("import Qt 4.7\nWorkerScript { source: '" + source.toString().toUtf8() + "'; }", QUrl()); + QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker.qml"); + QDeclarativeWorkerScript *worker = qobject_cast(component.create()); + QVERIFY(worker != 0); + const QMetaObject *mo = worker->metaObject(); - QDeclarativeWorkerScript *item = qobject_cast(component.create()); - QVERIFY(item != 0); + QVariant value(100); + QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value))); + waitForEchoMessage(worker); + QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value(), value); - QCOMPARE(item->source(), source); + QUrl source = QUrl::fromLocalFile(SRCDIR "/data/script_fixed_return.js"); + worker->setSource(source); + QCOMPARE(worker->source(), source); + QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value))); + waitForEchoMessage(worker); + QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value(), qVariantFromValue(QString("Hello_World"))); qApp->processEvents(); - delete item; + delete worker; } void tst_QDeclarativeWorkerScript::messaging() @@ -177,6 +185,25 @@ void tst_QDeclarativeWorkerScript::messaging_sendJsObject() delete worker; } +void tst_QDeclarativeWorkerScript::script_with_pragma() +{ + QVariant value(100); + + QDeclarativeComponent component(&m_engine, SRCDIR "/data/worker_pragma.qml"); + QDeclarativeWorkerScript *worker = qobject_cast(component.create()); + QVERIFY(worker != 0); + + QVERIFY(QMetaObject::invokeMethod(worker, "testSend", Q_ARG(QVariant, value))); + waitForEchoMessage(worker); + + const QMetaObject *mo = worker->metaObject(); + QCOMPARE(mo->property(mo->indexOfProperty("response")).read(worker).value(), value); + + qApp->processEvents(); + delete worker; +} + + QTEST_MAIN(tst_QDeclarativeWorkerScript) #include "tst_qdeclarativeworkerscript.moc" -- cgit v0.12