From 223313479bf8ec80158ba0f6cba4dd5e74d92718 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 28 Feb 2013 17:03:43 -0800 Subject: Add a URL interceptor to the QML engine Allows for custom file handling to a greater extent than the QNetworkAccessManager. Change-Id: Ifd3946bf33530c40ca2edeeb9f441f712e4941f6 Reviewed-by: Matthew Vogt --- .../data/interception/qmldir/Intercepted.qml | 5 ++ .../data/interception/qmldir/intercepted.js | 1 + .../qmldir/intercepted/Intercepted.qml | 6 ++ .../qmldir/intercepted/Intercepted2.qml | 5 ++ .../interception/qmldir/intercepted/intercepted.js | 1 + .../qmldir/intercepted/urlInterceptor.qml | 11 +++ .../data/interception/qmldir/urlInterceptor.qml | 12 +++ .../data/interception/strings/Intercepted.qml | 5 ++ .../data/interception/strings/intercepted.js | 1 + .../strings/intercepted/Intercepted.qml | 5 ++ .../strings/intercepted/intercepted.js | 1 + .../strings/intercepted/urlInterceptor.qml | 11 +++ .../data/interception/strings/urlInterceptor.qml | 11 +++ .../data/interception/types/Intercepted.qml | 5 ++ .../data/interception/types/Intercepted2.qml | 5 ++ .../data/interception/types/intercepted.js | 1 + .../interception/types/intercepted/Intercepted.qml | 6 ++ .../types/intercepted/Intercepted2.qml | 5 ++ .../interception/types/intercepted/intercepted.js | 1 + .../types/intercepted/urlInterceptor.qml | 11 +++ .../data/interception/types/urlInterceptor.qml | 11 +++ tests/auto/qml/qqmlengine/tst_qqmlengine.cpp | 96 ++++++++++++++++++++++ 22 files changed, 216 insertions(+) create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/Intercepted.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted.js create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted2.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/intercepted.js create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/urlInterceptor.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/qmldir/urlInterceptor.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/strings/Intercepted.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/strings/intercepted.js create mode 100644 tests/auto/qml/qqmlengine/data/interception/strings/intercepted/Intercepted.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/strings/intercepted/intercepted.js create mode 100644 tests/auto/qml/qqmlengine/data/interception/strings/intercepted/urlInterceptor.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/strings/urlInterceptor.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/Intercepted.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/Intercepted2.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/intercepted.js create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted2.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/intercepted/intercepted.js create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/intercepted/urlInterceptor.qml create mode 100644 tests/auto/qml/qqmlengine/data/interception/types/urlInterceptor.qml (limited to 'tests/auto/qml/qqmlengine') diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/Intercepted.qml b/tests/auto/qml/qqmlengine/data/interception/qmldir/Intercepted.qml new file mode 100644 index 0000000000..0331a01ad4 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/Intercepted.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted.js b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted.js new file mode 100644 index 0000000000..6f54ebcdc5 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted.js @@ -0,0 +1 @@ +var myStr = "base file" diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted.qml b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted.qml new file mode 100644 index 0000000000..ef5c28f87b --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted.qml @@ -0,0 +1,6 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" + property Intercepted2 compilationIsTest: Intercepted2{} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted2.qml b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted2.qml new file mode 100644 index 0000000000..0331a01ad4 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/Intercepted2.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/intercepted.js b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/intercepted.js new file mode 100644 index 0000000000..6eeee6e72f --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/intercepted.js @@ -0,0 +1 @@ +var myStr = "intercepted" diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/urlInterceptor.qml b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/urlInterceptor.qml new file mode 100644 index 0000000000..bd4aee056a --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/intercepted/urlInterceptor.qml @@ -0,0 +1,11 @@ +import QtQml 2.0 +import "intercepted.js" as Script + +QtObject { + property url filePath: "FailsTest" + property url resolvedUrl: Qt.resolvedUrl("FailsTest"); + property url absoluteUrl: Qt.resolvedUrl("file:///FailsTest"); + property string childString: child.myStr + property string scriptString: Script.myStr + property Intercepted child: Intercepted {} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/qmldir/urlInterceptor.qml b/tests/auto/qml/qqmlengine/data/interception/qmldir/urlInterceptor.qml new file mode 100644 index 0000000000..22a09e5522 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/qmldir/urlInterceptor.qml @@ -0,0 +1,12 @@ +import QtQml 2.0 +import "." +import "intercepted.js" as Script + +QtObject { + property url filePath: "doesNotExist.file" + property url resolvedUrl: Qt.resolvedUrl("doesNotExist.file"); + property url absoluteUrl: Qt.resolvedUrl("file:///doesNotExist.file"); + property string childString: child.myStr + property string scriptString: Script.myStr + property Intercepted child: Intercepted {} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/strings/Intercepted.qml b/tests/auto/qml/qqmlengine/data/interception/strings/Intercepted.qml new file mode 100644 index 0000000000..449207e0e3 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/strings/Intercepted.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "base file" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/strings/intercepted.js b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted.js new file mode 100644 index 0000000000..6f54ebcdc5 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted.js @@ -0,0 +1 @@ +var myStr = "base file" diff --git a/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/Intercepted.qml b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/Intercepted.qml new file mode 100644 index 0000000000..0331a01ad4 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/Intercepted.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/intercepted.js b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/intercepted.js new file mode 100644 index 0000000000..6eeee6e72f --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/intercepted.js @@ -0,0 +1 @@ +var myStr = "intercepted" diff --git a/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/urlInterceptor.qml b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/urlInterceptor.qml new file mode 100644 index 0000000000..bd4aee056a --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/strings/intercepted/urlInterceptor.qml @@ -0,0 +1,11 @@ +import QtQml 2.0 +import "intercepted.js" as Script + +QtObject { + property url filePath: "FailsTest" + property url resolvedUrl: Qt.resolvedUrl("FailsTest"); + property url absoluteUrl: Qt.resolvedUrl("file:///FailsTest"); + property string childString: child.myStr + property string scriptString: Script.myStr + property Intercepted child: Intercepted {} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/strings/urlInterceptor.qml b/tests/auto/qml/qqmlengine/data/interception/strings/urlInterceptor.qml new file mode 100644 index 0000000000..be86195bd8 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/strings/urlInterceptor.qml @@ -0,0 +1,11 @@ +import QtQml 2.0 +import "intercepted.js" as Script + +QtObject { + property url filePath: "doesNotExist.file" + property url resolvedUrl: Qt.resolvedUrl("doesNotExist.file"); + property url absoluteUrl: Qt.resolvedUrl("file:///doesNotExist.file"); + property string childString: child.myStr + property string scriptString: Script.myStr + property Intercepted child: Intercepted {} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/types/Intercepted.qml b/tests/auto/qml/qqmlengine/data/interception/types/Intercepted.qml new file mode 100644 index 0000000000..449207e0e3 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/Intercepted.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "base file" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/types/Intercepted2.qml b/tests/auto/qml/qqmlengine/data/interception/types/Intercepted2.qml new file mode 100644 index 0000000000..0331a01ad4 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/Intercepted2.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/types/intercepted.js b/tests/auto/qml/qqmlengine/data/interception/types/intercepted.js new file mode 100644 index 0000000000..6f54ebcdc5 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/intercepted.js @@ -0,0 +1 @@ +var myStr = "base file" diff --git a/tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted.qml b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted.qml new file mode 100644 index 0000000000..ef5c28f87b --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted.qml @@ -0,0 +1,6 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" + property Intercepted2 compilationIsTest: Intercepted2{} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted2.qml b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted2.qml new file mode 100644 index 0000000000..0331a01ad4 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/Intercepted2.qml @@ -0,0 +1,5 @@ +import QtQml 2.0 + +QtObject { + property string myStr: "intercepted" +} diff --git a/tests/auto/qml/qqmlengine/data/interception/types/intercepted/intercepted.js b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/intercepted.js new file mode 100644 index 0000000000..6eeee6e72f --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/intercepted.js @@ -0,0 +1 @@ +var myStr = "intercepted" diff --git a/tests/auto/qml/qqmlengine/data/interception/types/intercepted/urlInterceptor.qml b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/urlInterceptor.qml new file mode 100644 index 0000000000..be86195bd8 --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/intercepted/urlInterceptor.qml @@ -0,0 +1,11 @@ +import QtQml 2.0 +import "intercepted.js" as Script + +QtObject { + property url filePath: "doesNotExist.file" + property url resolvedUrl: Qt.resolvedUrl("doesNotExist.file"); + property url absoluteUrl: Qt.resolvedUrl("file:///doesNotExist.file"); + property string childString: child.myStr + property string scriptString: Script.myStr + property Intercepted child: Intercepted {} +} diff --git a/tests/auto/qml/qqmlengine/data/interception/types/urlInterceptor.qml b/tests/auto/qml/qqmlengine/data/interception/types/urlInterceptor.qml new file mode 100644 index 0000000000..bd4aee056a --- /dev/null +++ b/tests/auto/qml/qqmlengine/data/interception/types/urlInterceptor.qml @@ -0,0 +1,11 @@ +import QtQml 2.0 +import "intercepted.js" as Script + +QtObject { + property url filePath: "FailsTest" + property url resolvedUrl: Qt.resolvedUrl("FailsTest"); + property url absoluteUrl: Qt.resolvedUrl("file:///FailsTest"); + property string childString: child.myStr + property string scriptString: Script.myStr + property Intercepted child: Intercepted {} +} diff --git a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp index 9177ff58f7..d604118b58 100644 --- a/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp +++ b/tests/auto/qml/qqmlengine/tst_qqmlengine.cpp @@ -54,6 +54,7 @@ #include #include #include +#include class tst_qqmlengine : public QQmlDataTest { @@ -79,6 +80,8 @@ private slots: void multipleEngines(); void qtqmlModule_data(); void qtqmlModule(); + void urlInterceptor_data(); + void urlInterceptor(); public slots: QObject *createAQObjectForOwnershipTest () @@ -674,6 +677,99 @@ void tst_qqmlengine::qtqmlModule() } } +class CustomSelector : public QQmlAbstractUrlInterceptor +{ +public: + virtual QUrl intercept(const QUrl &url, QQmlAbstractUrlInterceptor::DataType d) + { + if (url.scheme() != QStringLiteral("file")) + return url; + if (!m_interceptionPoints.contains(d)) + return url; + + QString alteredPath = url.path(); + int a = alteredPath.lastIndexOf('/'); + if (a < 0) + a = 0; + alteredPath.insert(a, QStringLiteral("/intercepted")); + + QUrl ret = url; + ret.setPath(alteredPath); + return ret; + } + QList m_interceptionPoints; +}; + +Q_DECLARE_METATYPE(QList); +void tst_qqmlengine::urlInterceptor_data() +{ + QTest::addColumn("testFile"); + QTest::addColumn >("interceptionPoint"); + QTest::addColumn("expectedFilePath"); + QTest::addColumn("expectedChildString"); + QTest::addColumn("expectedScriptString"); + QTest::addColumn("expectedResolvedUrl"); + QTest::addColumn("expectedAbsoluteUrl"); + + QTest::newRow("InterceptTypes") + << testFileUrl("interception/types/urlInterceptor.qml") + << (QList() << QQmlAbstractUrlInterceptor::QmlFile << QQmlAbstractUrlInterceptor::JavaScriptFile << QQmlAbstractUrlInterceptor::UrlString) + << testFileUrl("interception/types/intercepted/doesNotExist.file").toString() + << QStringLiteral("intercepted") + << QStringLiteral("intercepted") + << testFileUrl("interception/types/intercepted/doesNotExist.file").toString() + << QStringLiteral("file:///intercepted/doesNotExist.file"); + + QTest::newRow("InterceptQmlDir") + << testFileUrl("interception/qmldir/urlInterceptor.qml") + << (QList() << QQmlAbstractUrlInterceptor::QmldirFile << QQmlAbstractUrlInterceptor::UrlString) + << testFileUrl("interception/qmldir/intercepted/doesNotExist.file").toString() + << QStringLiteral("intercepted") + << QStringLiteral("base file") + << testFileUrl("interception/qmldir/intercepted/doesNotExist.file").toString() + << QStringLiteral("file:///intercepted/doesNotExist.file"); + + QTest::newRow("InterceptStrings") + << testFileUrl("interception/strings/urlInterceptor.qml") + << (QList() << QQmlAbstractUrlInterceptor::UrlString) + << testFileUrl("interception/strings/intercepted/doesNotExist.file").toString() + << QStringLiteral("base file") + << QStringLiteral("base file") + << testFileUrl("interception/strings/intercepted/doesNotExist.file").toString() + << QStringLiteral("file:///intercepted/doesNotExist.file"); +} + +void tst_qqmlengine::urlInterceptor() +{ + + QFETCH(QUrl, testFile); + QFETCH(QList, interceptionPoint); + QFETCH(QString, expectedFilePath); + QFETCH(QString, expectedChildString); + QFETCH(QString, expectedScriptString); + QFETCH(QString, expectedResolvedUrl); + QFETCH(QString, expectedAbsoluteUrl); + + QQmlEngine e; + CustomSelector cs; + cs.m_interceptionPoints = interceptionPoint; + e.setUrlInterceptor(&cs); + QQmlComponent c(&e, testFile); //Note that this can get intercepted too + QObject *o = c.create(); + if (!o) + qDebug() << c.errorString(); + QVERIFY(o); + //Test a URL as a property initialization + QCOMPARE(o->property("filePath").toString(), expectedFilePath); + //Test a URL as a Type location + QCOMPARE(o->property("childString").toString(), expectedChildString); + //Test a URL as a Script location + QCOMPARE(o->property("scriptString").toString(), expectedScriptString); + //Test a URL as a resolveUrl() call + QCOMPARE(o->property("resolvedUrl").toString(), expectedResolvedUrl); + QCOMPARE(o->property("absoluteUrl").toString(), expectedAbsoluteUrl); +} + QTEST_MAIN(tst_qqmlengine) #include "tst_qqmlengine.moc" -- cgit v1.2.3