2019-06-18 15:27:59 +02:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
2019-06-18 15:27:59 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
2020-05-30 16:15:03 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
* License-Filename: LICENSE
|
|
|
|
*
|
2019-06-18 15:27:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Tests.h"
|
|
|
|
|
|
|
|
#include "locale/LabelModel.h"
|
2019-11-26 11:19:30 +01:00
|
|
|
#include "locale/TimeZone.h"
|
2019-08-05 17:57:32 +02:00
|
|
|
#include "locale/TranslatableConfiguration.h"
|
|
|
|
|
|
|
|
#include "CalamaresVersion.h"
|
2019-06-18 15:27:59 +02:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN( LocaleTests )
|
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
LocaleTests::LocaleTests() {}
|
2019-06-18 15:27:59 +02:00
|
|
|
|
2019-08-04 22:17:12 +02:00
|
|
|
LocaleTests::~LocaleTests() {}
|
2019-06-18 15:27:59 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::initTestCase()
|
|
|
|
{
|
2019-08-07 12:31:52 +02:00
|
|
|
// Otherwise plain get() is dubious in the TranslatableConfiguration tests
|
2020-04-21 13:41:04 +02:00
|
|
|
QLocale::setDefault( QLocale( QStringLiteral( "en_US" ) ) );
|
2019-08-07 12:31:52 +02:00
|
|
|
QVERIFY( ( QLocale().name() == "C" ) || ( QLocale().name() == "en_US" ) );
|
2019-06-18 15:27:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::testLanguageModelCount()
|
|
|
|
{
|
|
|
|
const auto* m = CalamaresUtils::Locale::availableTranslations();
|
|
|
|
|
|
|
|
QVERIFY( m );
|
|
|
|
QVERIFY( m->rowCount( QModelIndex() ) > 1 );
|
|
|
|
|
|
|
|
int dutch = m->find( QLocale( "nl_NL" ) );
|
|
|
|
QVERIFY( dutch > 0 );
|
|
|
|
QCOMPARE( m->find( "NL" ), dutch );
|
|
|
|
// must be capitals
|
|
|
|
QCOMPARE( m->find( "nl" ), -1 );
|
|
|
|
QCOMPARE( m->find( QLocale( "nl" ) ), dutch );
|
|
|
|
|
|
|
|
// Belgium speaks Dutch as well
|
|
|
|
QCOMPARE( m->find( "BE" ), dutch );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::testEsperanto()
|
|
|
|
{
|
|
|
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
|
|
|
|
|
|
|
const auto* m = CalamaresUtils::Locale::availableTranslations();
|
|
|
|
|
|
|
|
QVERIFY( m );
|
|
|
|
|
|
|
|
// Cursory test that all the locales found have a sensible language,
|
|
|
|
// and that some specific languages have sensible corresponding data.
|
|
|
|
//
|
|
|
|
// This fails on Esperanto (or, if Esperanto is added to Qt, then
|
|
|
|
// this will pass and the test after the loop will fail.
|
|
|
|
for ( int i = 0; i < m->rowCount( QModelIndex() ); ++i )
|
|
|
|
{
|
|
|
|
const auto& label = m->locale( i );
|
|
|
|
const auto locale = label.locale();
|
|
|
|
cDebug() << label.label() << locale;
|
|
|
|
|
|
|
|
QVERIFY( locale.language() == QLocale::Greek ? locale.script() == QLocale::GreekScript : true );
|
|
|
|
QVERIFY( locale.language() == QLocale::Korean ? locale.script() == QLocale::KoreanScript : true );
|
|
|
|
QVERIFY( locale.language() == QLocale::Lithuanian ? locale.country() == QLocale::Lithuania : true );
|
|
|
|
QVERIFY( locale.language() != QLocale::C );
|
|
|
|
}
|
2019-08-05 18:30:50 +02:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK( 5, 12, 2 )
|
2019-06-18 15:27:59 +02:00
|
|
|
QCOMPARE( QLocale( "eo" ).language(), QLocale::C );
|
2019-08-05 18:30:50 +02:00
|
|
|
#else
|
|
|
|
QCOMPARE( QLocale( "eo" ).language(), QLocale::Esperanto );
|
|
|
|
#endif
|
2019-06-18 15:27:59 +02:00
|
|
|
}
|
2019-08-05 17:57:32 +02:00
|
|
|
|
|
|
|
static const QStringList&
|
|
|
|
someLanguages()
|
|
|
|
{
|
2019-08-05 23:54:53 +02:00
|
|
|
static QStringList languages { "nl", "de", "da", "nb", "sr@latin", "ar", "ru" };
|
2019-08-05 17:57:32 +02:00
|
|
|
return languages;
|
2019-08-05 23:54:53 +02:00
|
|
|
}
|
2019-08-05 17:57:32 +02:00
|
|
|
|
2019-08-05 23:54:53 +02:00
|
|
|
|
2019-08-06 11:53:35 +02:00
|
|
|
/** @brief Check consistency of test data
|
|
|
|
* Check that all the languages used in testing, are actually enabled
|
|
|
|
* in Calamares translations.
|
|
|
|
*/
|
2019-08-05 23:54:53 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testTranslatableLanguages()
|
2019-08-05 17:57:32 +02:00
|
|
|
{
|
|
|
|
QStringList availableLanguages = QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' );
|
|
|
|
cDebug() << "Translation languages:" << availableLanguages;
|
2019-08-05 23:54:53 +02:00
|
|
|
for ( const auto& language : someLanguages() )
|
2019-08-05 17:57:32 +02:00
|
|
|
{
|
|
|
|
// Could be QVERIFY, but then we don't see what language code fails
|
|
|
|
QCOMPARE( availableLanguages.contains( language ) ? language : QString(), language );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 11:53:35 +02:00
|
|
|
/** @brief Test strings with no translations
|
|
|
|
*/
|
2019-08-05 23:54:53 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testTranslatableConfig1()
|
2019-08-05 17:57:32 +02:00
|
|
|
{
|
2019-08-06 11:53:35 +02:00
|
|
|
CalamaresUtils::Locale::TranslatedString ts0;
|
|
|
|
QVERIFY( ts0.isEmpty() );
|
|
|
|
QCOMPARE( ts0.count(), 1 ); // the empty string
|
|
|
|
|
2019-08-05 17:57:32 +02:00
|
|
|
CalamaresUtils::Locale::TranslatedString ts1( "Hello" );
|
|
|
|
QCOMPARE( ts1.count(), 1 );
|
2019-08-06 11:53:35 +02:00
|
|
|
QVERIFY( !ts1.isEmpty() );
|
2019-08-05 23:54:53 +02:00
|
|
|
|
2020-02-21 17:58:55 +01:00
|
|
|
QCOMPARE( ts1.get(), QStringLiteral( "Hello" ) );
|
|
|
|
QCOMPARE( ts1.get( QLocale( "nl" ) ), QStringLiteral( "Hello" ) );
|
2019-08-05 23:54:53 +02:00
|
|
|
|
2019-08-05 17:57:32 +02:00
|
|
|
QVariantMap map;
|
|
|
|
map.insert( "description", "description (no language)" );
|
2019-08-05 23:54:53 +02:00
|
|
|
CalamaresUtils::Locale::TranslatedString ts2( map, "description" );
|
2019-08-05 17:57:32 +02:00
|
|
|
QCOMPARE( ts2.count(), 1 );
|
2019-08-06 11:53:35 +02:00
|
|
|
QVERIFY( !ts2.isEmpty() );
|
2019-08-05 23:54:53 +02:00
|
|
|
|
2020-02-21 17:58:55 +01:00
|
|
|
QCOMPARE( ts2.get(), QStringLiteral( "description (no language)" ) );
|
|
|
|
QCOMPARE( ts2.get( QLocale( "nl" ) ), QStringLiteral( "description (no language)" ) );
|
2019-08-05 17:57:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-06 11:53:35 +02:00
|
|
|
/** @bref Test strings with translations.
|
|
|
|
*/
|
2019-08-05 23:54:53 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testTranslatableConfig2()
|
2019-08-05 17:57:32 +02:00
|
|
|
{
|
|
|
|
QVariantMap map;
|
2019-08-05 23:54:53 +02:00
|
|
|
|
|
|
|
for ( const auto& language : someLanguages() )
|
2019-08-05 17:57:32 +02:00
|
|
|
{
|
2019-08-05 23:54:53 +02:00
|
|
|
map.insert( QString( "description[%1]" ).arg( language ),
|
|
|
|
QString( "description (language %1)" ).arg( language ) );
|
2019-08-05 23:30:51 +02:00
|
|
|
if ( language != "nl" )
|
|
|
|
{
|
2019-08-05 23:54:53 +02:00
|
|
|
map.insert( QString( "name[%1]" ).arg( language ), QString( "name (language %1)" ).arg( language ) );
|
2019-08-05 23:30:51 +02:00
|
|
|
}
|
2019-08-05 17:57:32 +02:00
|
|
|
}
|
2019-08-05 23:54:53 +02:00
|
|
|
|
2019-08-06 11:53:35 +02:00
|
|
|
// If there's no untranslated string in the map, it is considered empty
|
|
|
|
CalamaresUtils::Locale::TranslatedString ts0( map, "description" );
|
|
|
|
QVERIFY( ts0.isEmpty() ); // Because no untranslated string
|
|
|
|
QCOMPARE( ts0.count(),
|
|
|
|
someLanguages().count() + 1 ); // But there are entries for the translations, plus an empty string
|
|
|
|
|
|
|
|
// expand the map with untranslated entries
|
|
|
|
map.insert( QString( "description" ), "description (no language)" );
|
|
|
|
map.insert( QString( "name" ), "name (no language)" );
|
|
|
|
|
2019-08-05 23:54:53 +02:00
|
|
|
CalamaresUtils::Locale::TranslatedString ts1( map, "description" );
|
2019-08-05 17:57:32 +02:00
|
|
|
// The +1 is because "" is always also inserted
|
2019-08-05 23:54:53 +02:00
|
|
|
QCOMPARE( ts1.count(), someLanguages().count() + 1 );
|
2019-08-06 11:53:35 +02:00
|
|
|
QVERIFY( !ts1.isEmpty() );
|
2019-08-05 23:54:53 +02:00
|
|
|
|
2020-02-21 17:58:55 +01:00
|
|
|
QCOMPARE( ts1.get(), QStringLiteral( "description (no language)" ) ); // it wasn't set
|
|
|
|
QCOMPARE( ts1.get( QLocale( "nl" ) ), QStringLiteral( "description (language nl)" ) );
|
2019-08-06 00:05:24 +02:00
|
|
|
for ( const auto& language : someLanguages() )
|
|
|
|
{
|
|
|
|
// Skip Serbian (latin) because QLocale() constructed with it
|
|
|
|
// doesn't retain the @latin part.
|
|
|
|
if ( language == "sr@latin" )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Could be QVERIFY, but then we don't see what language code fails
|
|
|
|
QCOMPARE( ts1.get( language ) == QString( "description (language %1)" ).arg( language ) ? language : QString(),
|
|
|
|
language );
|
|
|
|
}
|
|
|
|
QCOMPARE( ts1.get( QLocale( QLocale::Language::Serbian, QLocale::Script::LatinScript, QLocale::Country::Serbia ) ),
|
2020-02-21 17:58:55 +01:00
|
|
|
QStringLiteral( "description (language sr@latin)" ) );
|
2019-08-05 23:37:25 +02:00
|
|
|
|
2019-08-05 23:54:53 +02:00
|
|
|
CalamaresUtils::Locale::TranslatedString ts2( map, "name" );
|
2019-08-05 23:30:51 +02:00
|
|
|
// We skipped dutch this time
|
|
|
|
QCOMPARE( ts2.count(), someLanguages().count() );
|
2019-08-06 11:53:35 +02:00
|
|
|
QVERIFY( !ts2.isEmpty() );
|
|
|
|
|
|
|
|
// This key doesn't exist
|
|
|
|
CalamaresUtils::Locale::TranslatedString ts3( map, "front" );
|
|
|
|
QVERIFY( ts3.isEmpty() );
|
|
|
|
QCOMPARE( ts3.count(), 1 ); // The empty string
|
2019-08-05 17:57:32 +02:00
|
|
|
}
|
2019-11-26 11:19:30 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::testSimpleZones()
|
|
|
|
{
|
|
|
|
using namespace CalamaresUtils::Locale;
|
|
|
|
|
|
|
|
{
|
|
|
|
TZRegion r;
|
|
|
|
QVERIFY( r.tr().isEmpty() );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TZZone n;
|
|
|
|
QVERIFY( n.tr().isEmpty() );
|
|
|
|
}
|
|
|
|
{
|
2020-01-07 10:41:14 +01:00
|
|
|
TZZone r0( "xAmsterdam" );
|
2019-11-26 11:19:30 +01:00
|
|
|
QCOMPARE( r0.tr(), QStringLiteral( "xAmsterdam" ) );
|
2020-01-07 10:41:14 +01:00
|
|
|
TZZone r1( r0 );
|
2019-11-26 11:19:30 +01:00
|
|
|
QCOMPARE( r0.tr(), QStringLiteral( "xAmsterdam" ) );
|
|
|
|
QCOMPARE( r1.tr(), QStringLiteral( "xAmsterdam" ) );
|
2020-01-07 10:41:14 +01:00
|
|
|
TZZone r2( std::move( r0 ) );
|
2019-11-26 11:19:30 +01:00
|
|
|
QCOMPARE( r2.tr(), QStringLiteral( "xAmsterdam" ) );
|
|
|
|
QCOMPARE( r0.tr(), QString() );
|
|
|
|
}
|
2019-11-26 12:28:41 +01:00
|
|
|
{
|
|
|
|
TZZone r0( nullptr );
|
|
|
|
QVERIFY( r0.tr().isEmpty() );
|
|
|
|
TZZone r1( r0 );
|
|
|
|
QVERIFY( r1.tr().isEmpty() );
|
|
|
|
TZZone r2( std::move( r0 ) );
|
|
|
|
QVERIFY( r2.tr().isEmpty() );
|
|
|
|
}
|
2019-11-26 11:19:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::testComplexZones()
|
|
|
|
{
|
2019-11-26 12:28:41 +01:00
|
|
|
using namespace CalamaresUtils::Locale;
|
|
|
|
|
|
|
|
{
|
|
|
|
TZZone r0( "America/New_York" );
|
|
|
|
TZZone r1( "America/New York" );
|
|
|
|
|
|
|
|
QCOMPARE( r0.tr(), r1.tr() );
|
|
|
|
QCOMPARE( r0.tr(), QStringLiteral( "America/New York" ) );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
TZZone r( "zxc,;*_vm" );
|
|
|
|
QVERIFY( !r.tr().isEmpty() );
|
|
|
|
QCOMPARE( r.tr(), QStringLiteral( "zxc,;* vm" ) ); // Only _ is special
|
|
|
|
}
|
2019-11-26 11:19:30 +01:00
|
|
|
}
|