diff options
author | Karsten Heimrich <karsten.heimrich@qt.io> | 2020-12-01 14:20:12 +0100 |
---|---|---|
committer | Karsten Heimrich <karsten.heimrich@qt.io> | 2021-05-31 20:32:35 +0200 |
commit | d6181cc7044f0d198bbd2a5c3aec3d162006da91 (patch) | |
tree | fad69f55aa583cf876c655863bf9b6c1344df01f | |
parent | QComboBox: add space for scrollbar if needed after showing popup (diff) | |
download | qtbase-d6181cc7044f0d198bbd2a5c3aec3d162006da91.tar.xz qtbase-d6181cc7044f0d198bbd2a5c3aec3d162006da91.zip |
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: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r-- | tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp index 9fde7da816..4a4fd89987 100644 --- a/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp +++ b/tests/auto/corelib/kernel/qtranslator/tst_qtranslator.cpp @@ -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() { |