aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorErik Verbruggen <erik.verbruggen@qt.io>2018-10-08 15:13:50 +0200
committerErik Verbruggen <erik.verbruggen@qt.io>2018-10-15 10:19:22 +0000
commit67fbdadb3ba1aa9f86ae2d6ca3bff69479fb12be (patch)
tree8f3e5e56f14350c64276cdda88b6a477806fe8f6 /tests
parent50be4991d205bd392d6998b6fec88bb92a9e9e9a (diff)
Fix translation contexts for paths with drive letters on Windows
Inside method_qtTr, the filename is assumed to be a (correct) URL. When a (normalized) path with a windows drive letter is passed to QJSEngine::evaluate, the URL will have a scheme that is the drive letter. We cannot correct this in method_qtTr, because at that point we might get in files that do not come from the file system, but through actual URLs. The place where we know for sure that the filename is a real file name and not a URL, is in QJSEngine::evaluate. So at that point, make sure that the filename is a valid URL. Task-number: QTBUG-70425 Change-Id: Ia41859c4024ac46e6f8c3d96057a5dffdecd8f56 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 6bc0359483..a38224ef81 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -177,6 +177,8 @@ private slots:
void translateScriptUnicodeIdBased_data();
void translateScriptUnicodeIdBased();
void translateFromBuiltinCallback();
+ void translationFilePath_data();
+ void translationFilePath();
void installConsoleFunctions();
void logging();
@@ -3915,6 +3917,73 @@ void tst_QJSEngine::translateFromBuiltinCallback()
eng.evaluate("[10,20].forEach(foo)", "script.js");
}
+void tst_QJSEngine::translationFilePath_data()
+{
+ QTest::addColumn<QString>("filename");
+
+ QTest::newRow("relative") << QStringLiteral("script.js");
+ QTest::newRow("absolute unix") << QStringLiteral("/script.js");
+ QTest::newRow("absolute /windows/") << QStringLiteral("c:/script.js");
+#ifdef Q_OS_WIN
+ QTest::newRow("absolute \\windows\\") << QStringLiteral("c:\\script.js");
+#endif
+ QTest::newRow("relative url") << QStringLiteral("file://script.js");
+ QTest::newRow("absolute url unix") << QStringLiteral("file:///script.js");
+ QTest::newRow("absolute url windows") << QStringLiteral("file://c:/script.js");
+}
+
+class DummyTranslator : public QTranslator
+{
+ Q_OBJECT
+
+public:
+ DummyTranslator(const char *sourceText)
+ : srcTxt(sourceText)
+ {}
+
+ QString translate(const char *context, const char *sourceText, const char *disambiguation, int n) const override
+ {
+ Q_UNUSED(disambiguation);
+ Q_UNUSED(n);
+
+ if (srcTxt == sourceText)
+ ctxt = QByteArray(context);
+
+ return QString(sourceText);
+ }
+
+ bool isEmpty() const override
+ {
+ return false;
+ }
+
+ const char *sourceText() const
+ { return srcTxt.constData(); }
+
+ QByteArray context() const
+ { return ctxt; }
+
+private:
+ QByteArray srcTxt;
+ mutable QByteArray ctxt;
+};
+
+void tst_QJSEngine::translationFilePath()
+{
+ QFETCH(QString, filename);
+
+ DummyTranslator translator("some text");
+ QCoreApplication::installTranslator(&translator);
+ QByteArray scriptContent = QByteArray("qsTr('%1')").replace("%1", translator.sourceText());
+
+ QJSEngine engine;
+ engine.installExtensions(QJSEngine::TranslationExtension);
+ QJSValue result = engine.evaluate(scriptContent, filename);
+ QCOMPARE(translator.context(), QByteArray("script"));
+
+ QCoreApplication::removeTranslator(&translator);
+}
+
void tst_QJSEngine::installConsoleFunctions()
{
QJSEngine engine;