2020-08-25 16:05:56 +02:00
|
|
|
/* === This file is part of Calamares - <https://calamares.io> ===
|
2019-01-08 11:30:49 +01:00
|
|
|
*
|
2020-08-17 15:09:20 +02:00
|
|
|
* SPDX-FileCopyrightText: 2019-2020 Adriaan de Groot <groot@kde.org>
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2019-01-08 11:30:49 +01:00
|
|
|
*
|
2020-08-25 16:05:56 +02:00
|
|
|
* Calamares is Free Software: see the License-Identifier above.
|
2019-01-08 11:30:49 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-08-06 12:32:48 +02:00
|
|
|
#include "Config.h"
|
2019-01-08 11:30:49 +01:00
|
|
|
#include "LocaleConfiguration.h"
|
2020-04-14 15:10:04 +02:00
|
|
|
#include "timezonewidget/TimeZoneImage.h"
|
2019-01-08 11:30:49 +01:00
|
|
|
|
2020-04-14 11:59:45 +02:00
|
|
|
#include "locale/TimeZone.h"
|
2020-08-06 01:27:03 +02:00
|
|
|
#include "utils/Logger.h"
|
2020-04-14 11:59:45 +02:00
|
|
|
|
2019-01-08 11:30:49 +01:00
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2020-04-21 12:43:45 +02:00
|
|
|
#include <set>
|
|
|
|
|
2020-08-06 12:14:15 +02:00
|
|
|
class LocaleTests : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
LocaleTests();
|
|
|
|
~LocaleTests() override;
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
|
|
|
void initTestCase();
|
|
|
|
// Check the sample config file is processed correctly
|
|
|
|
void testEmptyLocaleConfiguration();
|
|
|
|
void testDefaultLocaleConfiguration();
|
|
|
|
void testSplitLocaleConfiguration();
|
|
|
|
|
|
|
|
// Check the TZ images for consistency
|
2020-08-17 15:09:20 +02:00
|
|
|
void testTZSanity();
|
2020-08-06 12:14:15 +02:00
|
|
|
void testTZImages(); // No overlaps in images
|
|
|
|
void testTZLocations(); // No overlaps in locations
|
|
|
|
void testSpecificLocations();
|
2020-08-06 12:32:48 +02:00
|
|
|
|
|
|
|
// Check the Config loading
|
|
|
|
void testConfigInitialization();
|
2020-08-06 12:14:15 +02:00
|
|
|
};
|
|
|
|
|
2020-04-14 15:10:04 +02:00
|
|
|
QTEST_MAIN( LocaleTests )
|
2019-01-08 11:30:49 +01:00
|
|
|
|
|
|
|
|
2020-04-16 15:48:17 +02:00
|
|
|
LocaleTests::LocaleTests() {}
|
2019-01-08 11:30:49 +01:00
|
|
|
|
2020-04-16 15:48:17 +02:00
|
|
|
LocaleTests::~LocaleTests() {}
|
2019-01-08 11:30:49 +01:00
|
|
|
|
2019-09-07 16:58:37 +02:00
|
|
|
void
|
|
|
|
LocaleTests::initTestCase()
|
2019-01-08 11:30:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:58:37 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testEmptyLocaleConfiguration()
|
2019-01-08 11:30:49 +01:00
|
|
|
{
|
|
|
|
LocaleConfiguration lc;
|
|
|
|
|
|
|
|
QVERIFY( lc.isEmpty() );
|
|
|
|
QCOMPARE( lc.toBcp47(), QString() );
|
|
|
|
}
|
|
|
|
|
2019-09-07 16:58:37 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testDefaultLocaleConfiguration()
|
2019-01-08 11:30:49 +01:00
|
|
|
{
|
|
|
|
LocaleConfiguration lc( "en_US.UTF-8" );
|
|
|
|
QVERIFY( !lc.isEmpty() );
|
2020-02-21 18:24:39 +01:00
|
|
|
QCOMPARE( lc.language(), QStringLiteral( "en_US.UTF-8" ) );
|
|
|
|
QCOMPARE( lc.toBcp47(), QStringLiteral( "en" ) );
|
2019-01-08 13:23:16 +01:00
|
|
|
|
|
|
|
LocaleConfiguration lc2( "de_DE.UTF-8" );
|
|
|
|
QVERIFY( !lc2.isEmpty() );
|
2020-02-21 18:24:39 +01:00
|
|
|
QCOMPARE( lc2.language(), QStringLiteral( "de_DE.UTF-8" ) );
|
|
|
|
QCOMPARE( lc2.toBcp47(), QStringLiteral( "de" ) );
|
2019-01-08 13:23:16 +01:00
|
|
|
}
|
|
|
|
|
2019-09-07 16:58:37 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testSplitLocaleConfiguration()
|
2019-01-08 13:23:16 +01:00
|
|
|
{
|
|
|
|
LocaleConfiguration lc( "en_US.UTF-8", "de_DE.UTF-8" );
|
|
|
|
QVERIFY( !lc.isEmpty() );
|
2020-02-21 18:24:39 +01:00
|
|
|
QCOMPARE( lc.language(), QStringLiteral( "en_US.UTF-8" ) );
|
|
|
|
QCOMPARE( lc.toBcp47(), QStringLiteral( "en" ) );
|
2019-01-08 13:23:16 +01:00
|
|
|
QCOMPARE( lc.lc_numeric, QStringLiteral( "de_DE.UTF-8" ) );
|
|
|
|
|
|
|
|
LocaleConfiguration lc2( "de_DE.UTF-8", "da_DK.UTF-8" );
|
|
|
|
QVERIFY( !lc2.isEmpty() );
|
2020-02-21 18:24:39 +01:00
|
|
|
QCOMPARE( lc2.language(), QStringLiteral( "de_DE.UTF-8" ) );
|
|
|
|
QCOMPARE( lc2.toBcp47(), QStringLiteral( "de" ) );
|
|
|
|
QCOMPARE( lc2.lc_numeric, QStringLiteral( "da_DK.UTF-8" ) );
|
2019-01-08 13:23:16 +01:00
|
|
|
|
|
|
|
LocaleConfiguration lc3( "da_DK.UTF-8", "de_DE.UTF-8" );
|
|
|
|
QVERIFY( !lc3.isEmpty() );
|
2020-02-21 18:24:39 +01:00
|
|
|
QCOMPARE( lc3.toBcp47(), QStringLiteral( "da" ) );
|
|
|
|
QCOMPARE( lc3.lc_numeric, QStringLiteral( "de_DE.UTF-8" ) );
|
2019-01-08 11:30:49 +01:00
|
|
|
}
|
2020-04-14 11:59:45 +02:00
|
|
|
|
2020-08-17 15:09:20 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testTZSanity()
|
|
|
|
{
|
|
|
|
// Data source for all TZ info
|
|
|
|
QVERIFY( QFile( "/usr/share/zoneinfo/zone.tab" ).exists() );
|
|
|
|
|
|
|
|
// Contains a sensible number of total zones
|
|
|
|
const CalamaresUtils::Locale::ZonesModel zones;
|
|
|
|
QVERIFY( zones.rowCount( QModelIndex() ) > 100 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-14 11:59:45 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testTZImages()
|
|
|
|
{
|
2020-04-14 16:24:56 +02:00
|
|
|
// This test messes around with log-levels a lot so
|
|
|
|
// that it produces useful output (e.g. listing the problems,
|
|
|
|
// not every check it ever does).
|
2020-04-14 11:59:45 +02:00
|
|
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
2020-04-14 15:10:04 +02:00
|
|
|
|
|
|
|
// Number of zone images
|
|
|
|
//
|
|
|
|
//
|
|
|
|
auto images = TimeZoneImageList::fromDirectory( SOURCE_DIR );
|
2020-04-14 15:24:05 +02:00
|
|
|
QCOMPARE( images.count(), images.zoneCount );
|
2020-04-14 15:10:04 +02:00
|
|
|
|
|
|
|
// All image sizes consistent
|
|
|
|
//
|
|
|
|
//
|
|
|
|
const QSize windowSize( 780, 340 );
|
|
|
|
{
|
|
|
|
QImage background( SOURCE_DIR "/bg.png" );
|
|
|
|
QVERIFY( !background.isNull() );
|
|
|
|
QCOMPARE( background.size(), windowSize );
|
|
|
|
}
|
|
|
|
for ( const auto& image : images )
|
|
|
|
{
|
|
|
|
QCOMPARE( image.size(), windowSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check zones are uniquely-claimed
|
|
|
|
//
|
|
|
|
//
|
2020-04-14 11:59:45 +02:00
|
|
|
using namespace CalamaresUtils::Locale;
|
2020-08-06 01:27:03 +02:00
|
|
|
const ZonesModel m;
|
2020-04-14 11:59:45 +02:00
|
|
|
|
2020-04-14 16:21:24 +02:00
|
|
|
int overlapcount = 0;
|
2020-08-06 01:27:03 +02:00
|
|
|
for ( auto it = m.begin(); it; ++it )
|
2020-04-14 11:59:45 +02:00
|
|
|
{
|
2020-08-06 01:27:03 +02:00
|
|
|
QString region = m.data( m.index( it.index() ), ZonesModel::RegionRole ).toString();
|
|
|
|
QString zoneName = m.data( m.index( it.index() ), ZonesModel::KeyRole ).toString();
|
|
|
|
QVERIFY( !region.isEmpty() );
|
|
|
|
QVERIFY( !zoneName.isEmpty() );
|
|
|
|
const auto* zone = m.find( region, zoneName );
|
|
|
|
const auto* iterzone = *it;
|
|
|
|
|
|
|
|
QVERIFY( iterzone );
|
|
|
|
QVERIFY( zone );
|
|
|
|
QCOMPARE( zone, iterzone );
|
|
|
|
QCOMPARE( zone->zone(), zoneName );
|
|
|
|
QCOMPARE( zone->region(), region );
|
|
|
|
|
|
|
|
int overlap = 0;
|
|
|
|
auto pos = images.getLocationPosition( zone->longitude(), zone->latitude() );
|
|
|
|
QVERIFY( images.index( pos, overlap ) >= 0 );
|
|
|
|
QVERIFY( overlap > 0 ); // At least one image contains the spot
|
|
|
|
if ( overlap > 1 )
|
2020-04-14 11:59:45 +02:00
|
|
|
{
|
2020-08-06 01:27:03 +02:00
|
|
|
Logger::setupLogLevel( Logger::LOGDEBUG );
|
|
|
|
cDebug() << Logger::SubEntry << "Zone" << zone->zone() << pos;
|
|
|
|
(void)images.index( pos, overlap );
|
|
|
|
Logger::setupLogLevel( Logger::LOGERROR );
|
|
|
|
overlapcount++;
|
2020-04-14 11:59:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 16:21:24 +02:00
|
|
|
|
2020-04-21 15:35:10 +02:00
|
|
|
QEXPECT_FAIL( "", "TZ Images not yet all fixed", Continue );
|
2020-04-14 16:21:24 +02:00
|
|
|
QCOMPARE( overlapcount, 0 );
|
2020-04-14 11:59:45 +02:00
|
|
|
}
|
2020-04-16 15:48:17 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
operator<( const QPoint& l, const QPoint& r )
|
|
|
|
{
|
|
|
|
if ( l.x() < r.x() )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( l.x() > r.x() )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return l.y() < r.y();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-08-06 01:27:03 +02:00
|
|
|
listAll( const QPoint& p, const CalamaresUtils::Locale::ZonesModel& zones )
|
2020-04-16 15:48:17 +02:00
|
|
|
{
|
|
|
|
using namespace CalamaresUtils::Locale;
|
2020-08-06 01:27:03 +02:00
|
|
|
for ( auto it = zones.begin(); it; ++it )
|
2020-04-16 15:48:17 +02:00
|
|
|
{
|
2020-08-06 01:27:03 +02:00
|
|
|
const auto* zone = *it;
|
|
|
|
if ( !zone )
|
|
|
|
{
|
|
|
|
cError() << Logger::SubEntry << "NULL zone";
|
|
|
|
return;
|
|
|
|
}
|
2020-04-16 15:48:17 +02:00
|
|
|
if ( p == TimeZoneImageList::getLocationPosition( zone->longitude(), zone->latitude() ) )
|
|
|
|
{
|
|
|
|
cError() << Logger::SubEntry << zone->zone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::testTZLocations()
|
|
|
|
{
|
|
|
|
using namespace CalamaresUtils::Locale;
|
2020-08-06 01:27:03 +02:00
|
|
|
ZonesModel zones;
|
2020-04-16 15:48:17 +02:00
|
|
|
|
2020-08-06 12:14:15 +02:00
|
|
|
QVERIFY( zones.rowCount( QModelIndex() ) > 100 );
|
|
|
|
|
2020-04-16 15:48:17 +02:00
|
|
|
int overlapcount = 0;
|
2020-08-06 12:14:15 +02:00
|
|
|
std::set< QPoint > occupied;
|
2020-08-06 01:27:03 +02:00
|
|
|
for ( auto it = zones.begin(); it; ++it )
|
2020-04-16 15:48:17 +02:00
|
|
|
{
|
2020-08-06 01:27:03 +02:00
|
|
|
const auto* zone = *it;
|
|
|
|
QVERIFY( zone );
|
|
|
|
|
|
|
|
auto pos = TimeZoneImageList::getLocationPosition( zone->longitude(), zone->latitude() );
|
|
|
|
if ( occupied.find( pos ) != occupied.end() )
|
2020-04-16 15:48:17 +02:00
|
|
|
{
|
2020-08-06 01:27:03 +02:00
|
|
|
cError() << "Zone" << zone->zone() << "occupies same spot as ..";
|
|
|
|
listAll( pos, zones );
|
|
|
|
overlapcount++;
|
2020-04-16 15:48:17 +02:00
|
|
|
}
|
2020-08-06 01:27:03 +02:00
|
|
|
occupied.insert( pos );
|
2020-04-16 15:48:17 +02:00
|
|
|
}
|
2020-04-21 12:43:45 +02:00
|
|
|
|
2020-04-21 15:35:10 +02:00
|
|
|
QEXPECT_FAIL( "", "TZ Images contain pixel-overlaps", Continue );
|
2020-04-16 15:48:17 +02:00
|
|
|
QCOMPARE( overlapcount, 0 );
|
|
|
|
}
|
2020-04-16 17:59:12 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
LocaleTests::testSpecificLocations()
|
|
|
|
{
|
2020-08-06 01:27:03 +02:00
|
|
|
CalamaresUtils::Locale::ZonesModel zones;
|
|
|
|
const auto* gibraltar = zones.find( "Europe", "Gibraltar" );
|
|
|
|
const auto* ceuta = zones.find( "Africa", "Ceuta" );
|
2020-04-16 17:59:12 +02:00
|
|
|
QVERIFY( gibraltar );
|
|
|
|
QVERIFY( ceuta );
|
|
|
|
|
|
|
|
auto gpos = TimeZoneImageList::getLocationPosition( gibraltar->longitude(), gibraltar->latitude() );
|
|
|
|
auto cpos = TimeZoneImageList::getLocationPosition( ceuta->longitude(), ceuta->latitude() );
|
2020-04-21 15:35:10 +02:00
|
|
|
QEXPECT_FAIL( "", "Gibraltar and Ceuta are really close", Continue );
|
2020-04-16 17:59:12 +02:00
|
|
|
QVERIFY( gpos != cpos );
|
|
|
|
QVERIFY( gibraltar->latitude() > ceuta->latitude() );
|
2020-04-21 15:35:10 +02:00
|
|
|
QEXPECT_FAIL( "", "Gibraltar and Ceuta are really close", Continue );
|
2020-04-16 17:59:12 +02:00
|
|
|
QVERIFY( gpos.y() < cpos.y() ); // Gibraltar is north of Ceuta
|
|
|
|
}
|
2020-08-06 12:14:15 +02:00
|
|
|
|
2020-08-06 12:32:48 +02:00
|
|
|
void
|
|
|
|
LocaleTests::testConfigInitialization()
|
|
|
|
{
|
|
|
|
Config c;
|
|
|
|
|
|
|
|
QVERIFY( !c.currentLocation() );
|
|
|
|
QVERIFY( !c.currentLocationStatus().isEmpty() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-06 12:14:15 +02:00
|
|
|
#include "utils/moc-warnings.h"
|
|
|
|
|
|
|
|
#include "Tests.moc"
|