Add test for QTranslator::load() translation file lookup algorithm

Pick-to: 5.15 6.0
Change-Id: I70f4b3d7dbc46d21065eab21a5af8a38d4a60589
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
This commit is contained in:
Karsten Heimrich 2020-12-01 14:20:12 +01:00
parent bf92f0fee5
commit a0e04e7d2b

View File

@ -29,6 +29,7 @@
#include <QtTest/QtTest>
#include <qtranslator.h>
#include <qfile.h>
#include <qtemporarydir.h>
class tst_QTranslator : public QObject
{
@ -43,6 +44,7 @@ private slots:
void load_data();
void load();
void loadLocale();
void threadLoad();
void testLanguageChange();
void plural();
@ -155,6 +157,73 @@ void tst_QTranslator::load()
}
}
void tst_QTranslator::loadLocale()
{
QLocale locale;
auto localeName = locale.uiLanguages().value(0).replace('-', '_');
if (localeName.isEmpty())
QSKIP("This test requires at least one available UI language.");
QByteArray ba;
{
QFile file(":/tst_qtranslator/hellotr_la.qm");
QVERIFY2(file.open(QFile::ReadOnly), qPrintable(file.errorString()));
ba = file.readAll();
QVERIFY(!ba.isEmpty());
}
QTemporaryDir dir;
QVERIFY(dir.isValid());
auto path = dir.path();
QFile file(path + "/dummy");
QVERIFY2(file.open(QFile::WriteOnly), qPrintable(file.errorString()));
QCOMPARE(file.write(ba), ba.size());
file.close();
/*
Test the following order:
/tmp/tmpDir/foo-en_US.qm
/tmp/tmpDir/foo-en_US
/tmp/tmpDir/foo-en.qm
/tmp/tmpDir/foo-en
/tmp/tmpDir/foo.qm
/tmp/tmpDir/foo-
/tmp/tmpDir/foo
*/
QStringList files;
while (true) {
files.append(path + "/foo-" + localeName + ".qm");
QVERIFY2(file.copy(files.last()), qPrintable(file.errorString()));
files.append(path + "/foo-" + localeName);
QVERIFY2(file.copy(files.last()), qPrintable(file.errorString()));
int rightmost = localeName.lastIndexOf(QLatin1Char('_'));
if (rightmost <= 0)
break;
localeName.truncate(rightmost);
}
files.append(path + "/foo.qm");
QVERIFY2(file.copy(files.last()), qPrintable(file.errorString()));
files.append(path + "/foo-");
QVERIFY2(file.copy(files.last()), qPrintable(file.errorString()));
files.append(path + "/foo");
QVERIFY2(file.rename(files.last()), qPrintable(file.errorString()));
QTranslator tor;
for (const auto &filePath : files) {
QVERIFY(tor.load(locale, "foo", "-", path, ".qm"));
QCOMPARE(tor.filePath(), filePath);
QVERIFY2(file.remove(filePath), qPrintable(file.errorString()));
}
}
class TranslatorThread : public QThread
{
void run() override {