File indexing completed on 2024-06-16 10:43:48

0001 // SPDX-FileCopyrightText: Lukas Sommer <sommerluk@gmail.com>
0002 // SPDX-License-Identifier: BSD-2-Clause OR MIT
0003 
0004 // First included header is the public header of the class we are testing;
0005 // this forces the header to be self-contained.
0006 #include "initializetranslation.h"
0007 
0008 #include "colordialog.h"
0009 #include "rgbcolorspacefactory.h"
0010 #include <qcoreapplication.h>
0011 #include <qglobal.h>
0012 #include <qlocale.h>
0013 #include <qobject.h>
0014 #include <qstringliteral.h>
0015 #include <qtest.h>
0016 #include <qtestcase.h>
0017 
0018 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
0019 #include <qcontainerfwd.h>
0020 #include <qlist.h>
0021 #include <qstring.h>
0022 #include <qtmetamacros.h>
0023 #else
0024 #include <qobjectdefs.h>
0025 #include <qstring.h>
0026 #include <qstringlist.h>
0027 #endif
0028 
0029 namespace PerceptualColor
0030 {
0031 
0032 class TestInitializeTranslation : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     explicit TestInitializeTranslation(QObject *parent = nullptr)
0038         : QObject(parent)
0039     {
0040     }
0041 
0042 private Q_SLOTS:
0043     void initTestCase()
0044     {
0045         // Called before the first test function is executed
0046     }
0047 
0048     void cleanupTestCase()
0049     {
0050         // Called after the last test function was executed
0051     }
0052 
0053     void init()
0054     {
0055         // Called before each test function is executed
0056     }
0057 
0058     void cleanup()
0059     {
0060         // Called after every test function
0061     }
0062 
0063     // NOTE This MUST be the very first test, so that still
0064     // PerceptualColor::initializeTranslation() has never been called before.
0065     void testIfTranslationIsActuallyLoaded()
0066     {
0067         auto m_srgbBuildinColorSpace = RgbColorSpaceFactory::createSrgb();
0068 
0069         QLocale::setDefault(QLocale(QStringLiteral("nl")));
0070         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0071                                                QLocale().uiLanguages());
0072         PerceptualColor::ColorDialog test1(m_srgbBuildinColorSpace);
0073         // Test if the window title is translated.
0074         QCOMPARE(test1.windowTitle(), QStringLiteral("Kleur selecteren"));
0075 
0076         // Try another language
0077         QLocale::setDefault(QLocale(QStringLiteral("es")));
0078         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0079                                                QLocale().uiLanguages());
0080         PerceptualColor::ColorDialog test2(m_srgbBuildinColorSpace);
0081         // Test if the window title is translated.
0082         QCOMPARE(test2.windowTitle(), QStringLiteral("Seleccionar color"));
0083 
0084         // try if capital language codes work
0085         QLocale::setDefault(QLocale(QStringLiteral("CA")));
0086         PerceptualColor::initializeTranslation( //
0087             QCoreApplication::instance(), //
0088             QStringList(QStringLiteral("CA")));
0089         PerceptualColor::ColorDialog test3(m_srgbBuildinColorSpace);
0090         // Test if the window title is translated.
0091         QCOMPARE(test3.windowTitle(), //
0092                  QStringLiteral("Selecció de color"));
0093 
0094         // Test if removing all translations works
0095         QLocale::setDefault(QLocale(QString()));
0096         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0097                                                QStringList(QString()));
0098         PerceptualColor::ColorDialog test4(m_srgbBuildinColorSpace);
0099         // Test if the window title is actually not translated.
0100         QCOMPARE(test4.windowTitle(), QStringLiteral("Select color"));
0101     }
0102 
0103     void testInitializeDoesNotCrash()
0104     {
0105         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0106                                                QLocale().uiLanguages());
0107     }
0108 
0109     void testInitializeDoesNotCrashOnSuccessiveCalls()
0110     {
0111         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0112                                                QLocale().uiLanguages());
0113         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0114                                                QLocale().uiLanguages());
0115         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0116                                                QLocale().uiLanguages());
0117         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0118                                                QLocale().uiLanguages());
0119         PerceptualColor::initializeTranslation(QCoreApplication::instance(), //
0120                                                QLocale().uiLanguages());
0121     }
0122 };
0123 
0124 } // namespace PerceptualColor
0125 
0126 QTEST_MAIN(PerceptualColor::TestInitializeTranslation)
0127 
0128 // The following “include” is necessary because we do not use a header file:
0129 #include "testinitializetranslation.moc"