From 00b1ff4607a560a2df674d56a70ecf3bd3e4f3df Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Tue, 6 Nov 2018 10:07:43 +0100 Subject: Fix translation bindings when qtquickcompiler is used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out that the context information is lost when using the compiler. The unit->unitData()->sourceFileIndex is wrong (always 0), which should probably be fixed. This change only works around that by using unit->fileName(); instead. Make sure that the test actually verifies translations happen and have a context. Done-with: Jan Arve Sæther Fixes: QTBUG-71553 Change-Id: Ib5926bd4b9a6267644f5c9328a84c23d61ca5466 Reviewed-by: Simon Hausmann --- tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'tests/auto/qml/qmlcachegen') diff --git a/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp b/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp index 6c399f6874..97ac466e94 100644 --- a/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp +++ b/tests/auto/qml/qmlcachegen/tst_qmlcachegen.cpp @@ -36,6 +36,7 @@ #include #include #include +#include class tst_qmlcachegen: public QObject { @@ -169,11 +170,26 @@ void tst_qmlcachegen::loadGeneratedFile() QVERIFY(unitData->flags & QV4::CompiledData::Unit::StaticData); } +class QTestTranslator : public QTranslator +{ +public: + QString translate(const char *context, const char *sourceText, const char */*disambiguation*/, int /*n*/) const override + { + m_lastContext = QString::fromUtf8(context); + return QString::fromUtf8(sourceText).toUpper(); + } + bool isEmpty() const override { return true; } + mutable QString m_lastContext; +}; + void tst_qmlcachegen::translationExpressionSupport() { QTemporaryDir tempDir; QVERIFY(tempDir.isValid()); + QTestTranslator translator; + qApp->installTranslator(&translator); + const auto writeTempFile = [&tempDir](const QString &fileName, const char *contents) { QFile f(tempDir.path() + '/' + fileName); const bool ok = f.open(QIODevice::WriteOnly | QIODevice::Truncate); @@ -207,7 +223,8 @@ void tst_qmlcachegen::translationExpressionSupport() CleanlyLoadingComponent component(&engine, QUrl::fromLocalFile(testFilePath)); QScopedPointer obj(component.create()); QVERIFY(!obj.isNull()); - QCOMPARE(obj->property("text").toString(), QString("All Ok")); + QCOMPARE(obj->property("text").toString(), QString("ALL Ok")); + QCOMPARE(translator.m_lastContext, QStringLiteral("test")); } void tst_qmlcachegen::signalHandlerParameters() -- cgit v1.2.3