Add test for QTranslator::load() translation file lookup algorithm
Change-Id: I70f4b3d7dbc46d21065eab21a5af8a38d4a60589 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de> (cherry picked from commit a0e04e7d2bfc47891a85378a57ceb5dca1d4c558) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
1f96dc553b
commit
bf88857623
@ -29,6 +29,7 @@
|
|||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
#include <qtranslator.h>
|
#include <qtranslator.h>
|
||||||
#include <qfile.h>
|
#include <qfile.h>
|
||||||
|
#include <qtemporarydir.h>
|
||||||
|
|
||||||
class tst_QTranslator : public QObject
|
class tst_QTranslator : public QObject
|
||||||
{
|
{
|
||||||
@ -43,6 +44,7 @@ private slots:
|
|||||||
|
|
||||||
void load_data();
|
void load_data();
|
||||||
void load();
|
void load();
|
||||||
|
void loadLocale();
|
||||||
void threadLoad();
|
void threadLoad();
|
||||||
void testLanguageChange();
|
void testLanguageChange();
|
||||||
void plural();
|
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
|
class TranslatorThread : public QThread
|
||||||
{
|
{
|
||||||
void run() override {
|
void run() override {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user