aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-05-25 11:28:09 +0200
committerUlf Hermann <ulf.hermann@qt.io>2022-05-31 12:54:49 +0200
commitf5514e682b2c97d3dea0369ff7cb56e3376b3cde (patch)
treec1413acb762b1c322d65e6439d800db7d526a792 /tests
parent1acd68c04d1abf9968820a89fa9b1bffb9056475 (diff)
QtQml: Fix and test edge cases of QQmlFile's local file detection
URLs with two slashes after the colon are generally _not_ local as they contain an authority. However, file URLs with two slashes are interpreted as "special" paths by QUrl::toLocalFile(). Therefore, we accept them. URLs without slashes after the colon can be local files. They denote relative paths or special android resources then. Pick-to: 6.2 6.3 Fixes: QTBUG-102944 Change-Id: Iaab3d7501b631e88ee8c1d93f1de8149ba60a5c4 Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qqmlapplicationengine/CMakeLists.txt1
-rw-r--r--tests/auto/qml/qqmlapplicationengine/androidassets/CMakeLists.txt18
-rw-r--r--tests/auto/qml/qqmlapplicationengine/androidassets/qml/main.qml13
-rw-r--r--tests/auto/qml/qqmlapplicationengine/androidassets/qml/pages/MainPage.qml11
-rw-r--r--tests/auto/qml/qqmlapplicationengine/androidassets/tst_androidassets.cpp86
-rw-r--r--tests/auto/qml/qqmlfile/tst_qqmlfile.cpp132
6 files changed, 258 insertions, 3 deletions
diff --git a/tests/auto/qml/qqmlapplicationengine/CMakeLists.txt b/tests/auto/qml/qqmlapplicationengine/CMakeLists.txt
index d9fbf6517d..caf1e2edaa 100644
--- a/tests/auto/qml/qqmlapplicationengine/CMakeLists.txt
+++ b/tests/auto/qml/qqmlapplicationengine/CMakeLists.txt
@@ -55,3 +55,4 @@ qt_internal_extend_target(tst_qqmlapplicationengine CONDITION NOT ANDROID AND NO
QT_QMLTEST_DATADIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}/data\\\"
)
add_subdirectory(testapp)
+add_subdirectory(androidassets)
diff --git a/tests/auto/qml/qqmlapplicationengine/androidassets/CMakeLists.txt b/tests/auto/qml/qqmlapplicationengine/androidassets/CMakeLists.txt
new file mode 100644
index 0000000000..1c0d305311
--- /dev/null
+++ b/tests/auto/qml/qqmlapplicationengine/androidassets/CMakeLists.txt
@@ -0,0 +1,18 @@
+qt_internal_add_test(tst_androidassets
+ SOURCES
+ tst_androidassets.cpp
+ PUBLIC_LIBRARIES
+ Qt::Gui
+ Qt::Qml
+ Qt::Quick
+)
+
+# add qml/*.qml files as assets instead of resources
+
+file(
+ COPY qml/main.qml
+ DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/android-build/assets/qml/")
+
+file(
+ COPY qml/pages/MainPage.qml
+ DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/android-build/assets/qml/pages/")
diff --git a/tests/auto/qml/qqmlapplicationengine/androidassets/qml/main.qml b/tests/auto/qml/qqmlapplicationengine/androidassets/qml/main.qml
new file mode 100644
index 0000000000..6901572b00
--- /dev/null
+++ b/tests/auto/qml/qqmlapplicationengine/androidassets/qml/main.qml
@@ -0,0 +1,13 @@
+import QtQuick
+
+// relative import: has to work when loading from android assets by path or by URL
+import "pages"
+
+Window {
+ width: 640
+ height: 480
+ visible: true
+ title: qsTr("Hello World")
+
+ MainPage { }
+}
diff --git a/tests/auto/qml/qqmlapplicationengine/androidassets/qml/pages/MainPage.qml b/tests/auto/qml/qqmlapplicationengine/androidassets/qml/pages/MainPage.qml
new file mode 100644
index 0000000000..c9549a928a
--- /dev/null
+++ b/tests/auto/qml/qqmlapplicationengine/androidassets/qml/pages/MainPage.qml
@@ -0,0 +1,11 @@
+import QtQuick
+
+Rectangle {
+ anchors.fill: parent
+ color: "#ddd"
+
+ Text {
+ anchors.centerIn: parent
+ text: "Qt 6"
+ }
+}
diff --git a/tests/auto/qml/qqmlapplicationengine/androidassets/tst_androidassets.cpp b/tests/auto/qml/qqmlapplicationengine/androidassets/tst_androidassets.cpp
new file mode 100644
index 0000000000..1926cf4fab
--- /dev/null
+++ b/tests/auto/qml/qqmlapplicationengine/androidassets/tst_androidassets.cpp
@@ -0,0 +1,86 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtQml/qqmlapplicationengine.h>
+#include <QtTest/qsignalspy.h>
+#include <QtTest/qtest.h>
+
+class tst_AndroidAssets : public QObject
+{
+ Q_OBJECT
+private Q_SLOTS:
+ void loadsFromAssetsPath();
+ void loadsFromAssetsUrl();
+
+private:
+
+ static QString pathPrefix()
+ {
+#ifdef Q_OS_ANDROID
+ return QStringLiteral("assets:");
+#else
+ // Even when not running on android we can check that the copying to build dir worked.
+ return QCoreApplication::applicationDirPath() + QStringLiteral("/android-build/assets");
+#endif
+ }
+
+ static QString urlPrefix() {
+#ifdef Q_OS_ANDROID
+ return pathPrefix();
+#else
+ return QStringLiteral("file:") + pathPrefix();
+#endif
+ }
+};
+
+
+void tst_AndroidAssets::loadsFromAssetsPath()
+{
+ QQmlApplicationEngine engine;
+ QSignalSpy failureSpy(&engine, &QQmlApplicationEngine::objectCreationFailed);
+
+ // load QML file from assets, by path:
+ engine.load(pathPrefix() + QStringLiteral("/qml/main.qml"));
+ QTRY_VERIFY(engine.rootObjects().length() == 1);
+ QVERIFY(failureSpy.isEmpty());
+}
+
+void tst_AndroidAssets::loadsFromAssetsUrl()
+{
+ QQmlApplicationEngine engine;
+ QSignalSpy failureSpy(&engine, &QQmlApplicationEngine::objectCreationFailed);
+
+ // load QML file from assets, by URL:
+ engine.load(QUrl(urlPrefix() + QStringLiteral("/qml/main.qml")));
+ QTRY_VERIFY(engine.rootObjects().length() == 1);
+ QVERIFY(failureSpy.isEmpty());
+}
+
+QTEST_MAIN(tst_AndroidAssets)
+
+#include "tst_androidassets.moc"
diff --git a/tests/auto/qml/qqmlfile/tst_qqmlfile.cpp b/tests/auto/qml/qqmlfile/tst_qqmlfile.cpp
index a1c8daddcf..77909d8576 100644
--- a/tests/auto/qml/qqmlfile/tst_qqmlfile.cpp
+++ b/tests/auto/qml/qqmlfile/tst_qqmlfile.cpp
@@ -38,19 +38,145 @@ public:
tst_qqmlfile() {}
private Q_SLOTS:
+ void isLocalFile_data();
+ void isLocalFile();
+
+ void urlToLocalFileOrQrcOverloads_data();
void urlToLocalFileOrQrcOverloads();
+
+private:
+ void urlData();
};
+void tst_qqmlfile::urlData()
+{
+ QTest::addColumn<QString>("urlString");
+ QTest::addColumn<bool>("isLocal");
+ QTest::addColumn<QString>("localPath");
+
+ const QString invalid;
+ const QString relative = QStringLiteral("foo/bar");
+ const QString absolute = QStringLiteral("/foo/bar");
+
+ QTest::addRow("plain empty") << QStringLiteral("") << false << invalid;
+ QTest::addRow("plain no slash") << QStringLiteral("foo/bar") << false << invalid;
+ QTest::addRow("plain 1 slash") << QStringLiteral("/foo/bar") << false << invalid;
+ QTest::addRow("plain 2 slashes") << QStringLiteral("//foo/bar") << false << invalid;
+ QTest::addRow("plain 3 slashes") << QStringLiteral("///foo/bar") << false << invalid;
+
+ QTest::addRow(": empty") << QStringLiteral(":") << false << invalid;
+ QTest::addRow(": no slash") << QStringLiteral(":foo/bar") << false << invalid;
+ QTest::addRow(": 1 slash") << QStringLiteral(":/foo/bar") << false << invalid;
+ QTest::addRow(": 2 slashes") << QStringLiteral("://foo/bar") << false << invalid;
+ QTest::addRow(": 3 slashes") << QStringLiteral(":///foo/bar") << false << invalid;
+
+ QTest::addRow("C empty") << QStringLiteral("C:") << false << invalid;
+ QTest::addRow("C no slash") << QStringLiteral("C:foo/bar") << false << invalid;
+ QTest::addRow("C 1 slash") << QStringLiteral("C:/foo/bar") << false << invalid;
+ QTest::addRow("C 2 slashes") << QStringLiteral("C://foo/bar") << false << invalid;
+ QTest::addRow("C 3 slashes") << QStringLiteral("C:///foo/bar") << false << invalid;
+
+ QTest::addRow("file empty") << QStringLiteral("file:") << true << QString();
+ QTest::addRow("file no slash") << QStringLiteral("file:foo/bar") << true << relative;
+ QTest::addRow("file 1 slash") << QStringLiteral("file:/foo/bar") << true << absolute;
+ QTest::addRow("file 2 slashes") << QStringLiteral("file://foo/bar") << true << QStringLiteral("//foo/bar");
+ QTest::addRow("file 3 slashes") << QStringLiteral("file:///foo/bar") << true << absolute;
+
+ QTest::addRow("qrc empty") << QStringLiteral("qrc:") << true << QStringLiteral(":");
+ QTest::addRow("qrc no slash") << QStringLiteral("qrc:foo/bar") << true << u':' + relative;
+ QTest::addRow("qrc 1 slash") << QStringLiteral("qrc:/foo/bar") << true << u':' + absolute;
+ QTest::addRow("qrc 2 slashes") << QStringLiteral("qrc://foo/bar") << false << invalid;
+ QTest::addRow("qrc 3 slashes") << QStringLiteral("qrc:///foo/bar") << true << u':' + absolute;
+
+ QTest::addRow("file+stuff empty") << QStringLiteral("file+stuff:") << false << invalid;
+ QTest::addRow("file+stuff no slash") << QStringLiteral("file+stuff:foo/bar") << false << invalid;
+ QTest::addRow("file+stuff 1 slash") << QStringLiteral("file+stuff:/foo/bar") << false << invalid;
+ QTest::addRow("file+stuff 2 slashes") << QStringLiteral("file+stuff://foo/bar") << false << invalid;
+ QTest::addRow("file+stuff 3 slashes") << QStringLiteral("file+stuff:///foo/bar") << false << invalid;
+
+ // "assets:" and "content:" URLs are only treated as local files on android. In contrast to
+ // "qrc:" and "file:" we're not trying to be clever about multiple slashes. Two slashes are
+ // prohibited as that says part of what we would recognize as path is actually a URL authority.
+ // Everything else is android's problem.
+
+#ifdef Q_OS_ANDROID
+ const bool hasAssetsAndContent = true;
+#else
+ const bool hasAssetsAndContent = false;
+#endif
+
+ const QString assetsEmpty = hasAssetsAndContent ? QStringLiteral("assets:") : invalid;
+ const QString assetsRelative = hasAssetsAndContent ? (QStringLiteral("assets:") + relative) : invalid;
+ const QString assetsAbsolute = hasAssetsAndContent ? (QStringLiteral("assets:") + absolute) : invalid;
+ const QString assetsThreeSlashes = hasAssetsAndContent ? (QStringLiteral("assets://") + absolute) : invalid;
+
+ QTest::addRow("assets empty") << QStringLiteral("assets:") << hasAssetsAndContent << assetsEmpty;
+ QTest::addRow("assets no slash") << QStringLiteral("assets:foo/bar") << hasAssetsAndContent << assetsRelative;
+ QTest::addRow("assets 1 slash") << QStringLiteral("assets:/foo/bar") << hasAssetsAndContent << assetsAbsolute;
+ QTest::addRow("assets 2 slashes") << QStringLiteral("assets://foo/bar") << false << invalid;
+ QTest::addRow("assets 3 slashes") << QStringLiteral("assets:///foo/bar") << hasAssetsAndContent << assetsThreeSlashes;
+
+ const QString contentEmpty = hasAssetsAndContent ? QStringLiteral("content:") : invalid;
+ const QString contentRelative = hasAssetsAndContent ? (QStringLiteral("content:") + relative) : invalid;
+ const QString contentAbsolute = hasAssetsAndContent ? (QStringLiteral("content:") + absolute) : invalid;
+ const QString contentThreeSlashes = hasAssetsAndContent ? (QStringLiteral("content://") + absolute) : invalid;
+
+ QTest::addRow("content empty") << QStringLiteral("content:") << hasAssetsAndContent << contentEmpty;
+ QTest::addRow("content no slash") << QStringLiteral("content:foo/bar") << hasAssetsAndContent << contentRelative;
+ QTest::addRow("content 1 slash") << QStringLiteral("content:/foo/bar") << hasAssetsAndContent << contentAbsolute;
+ QTest::addRow("content 2 slashes") << QStringLiteral("content://foo/bar") << false << invalid;
+ QTest::addRow("content 3 slashes") << QStringLiteral("content:///foo/bar") << hasAssetsAndContent << contentThreeSlashes;
+
+
+ // These are local files everywhere. Their paths are only meaningful on android, though.
+ // The inner slashes of the path do not influence the URL parsing.
+
+ QTest::addRow("file:assets empty") << QStringLiteral("file:assets:") << true << QStringLiteral("assets:");
+ QTest::addRow("file:assets no slash") << QStringLiteral("file:assets:foo/bar") << true << QStringLiteral("assets:foo/bar");
+ QTest::addRow("file:assets 1 slash") << QStringLiteral("file:assets:/foo/bar") << true << QStringLiteral("assets:/foo/bar");
+ QTest::addRow("file:assets 2 slashes") << QStringLiteral("file:assets://foo/bar") << true << QStringLiteral("assets://foo/bar");
+ QTest::addRow("file:assets 3 slashes") << QStringLiteral("file:assets:///foo/bar") << true << QStringLiteral("assets:///foo/bar");
+
+ QTest::addRow("file:content empty") << QStringLiteral("file:content:") << true << QStringLiteral("content:");
+ QTest::addRow("file:content no slash") << QStringLiteral("file:content:foo/bar") << true << QStringLiteral("content:foo/bar");
+ QTest::addRow("file:content 1 slash") << QStringLiteral("file:content:/foo/bar") << true << QStringLiteral("content:/foo/bar");
+ QTest::addRow("file:content 2 slashes") << QStringLiteral("file:content://foo/bar") << true << QStringLiteral("content://foo/bar");
+ QTest::addRow("file:content 3 slashes") << QStringLiteral("file:content:///foo/bar") << true << QStringLiteral("content:///foo/bar");
+}
+
+void tst_qqmlfile::isLocalFile_data()
+{
+ urlData();
+}
+
+void tst_qqmlfile::isLocalFile()
+{
+ QFETCH(QString, urlString);
+ QFETCH(bool, isLocal);
+
+ const QUrl url(urlString);
+
+ QCOMPARE(QQmlFile::isLocalFile(urlString), isLocal);
+ QCOMPARE(QQmlFile::isLocalFile(url), isLocal);
+}
+
+void tst_qqmlfile::urlToLocalFileOrQrcOverloads_data()
+{
+ urlData();
+}
+
void tst_qqmlfile::urlToLocalFileOrQrcOverloads()
{
- const QString urlString = QStringLiteral("qrc:///example.qml");
+ QFETCH(QString, urlString);
+ QFETCH(QString, localPath);
+
const QUrl url(urlString);
const QString pathForUrlString = QQmlFile::urlToLocalFileOrQrc(urlString);
const QString pathForUrl = QQmlFile::urlToLocalFileOrQrc(url);
- QCOMPARE(pathForUrlString, pathForUrl);
- QCOMPARE(pathForUrlString, QStringLiteral(":/example.qml"));
+ QCOMPARE(pathForUrlString, localPath);
+ QCOMPARE(pathForUrl, localPath);
}
QTEST_GUILESS_MAIN(tst_qqmlfile)