File indexing completed on 2024-05-12 04:44:28

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 "settranslation.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 TestSetTranslation : public QObject
0033 {
0034     Q_OBJECT
0035 
0036 public:
0037     explicit TestSetTranslation(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::setTranslation(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::setTranslation(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::setTranslation(QCoreApplication::instance(), //
0087                                         QStringList(QStringLiteral("CA")));
0088         PerceptualColor::ColorDialog test3(m_srgbBuildinColorSpace);
0089         // Test if the window title is translated.
0090         QCOMPARE(test3.windowTitle(), //
0091                  QStringLiteral("Selecció de color"));
0092 
0093         // Test if removing all translations works
0094         QLocale::setDefault(QLocale(QString()));
0095         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0096                                         QStringList(QString()));
0097         PerceptualColor::ColorDialog test4(m_srgbBuildinColorSpace);
0098         // Test if the window title is actually not translated.
0099         QCOMPARE(test4.windowTitle(), QStringLiteral("Select color"));
0100     }
0101 
0102     void testSetTranslationDoesNotCrash()
0103     {
0104         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0105                                         QLocale().uiLanguages());
0106     }
0107 
0108     void testSetTranslationDoesNotCrashOnSuccessiveCalls()
0109     {
0110         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0111                                         QLocale().uiLanguages());
0112         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0113                                         QLocale().uiLanguages());
0114         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0115                                         QLocale().uiLanguages());
0116         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0117                                         QLocale().uiLanguages());
0118         PerceptualColor::setTranslation(QCoreApplication::instance(), //
0119                                         QLocale().uiLanguages());
0120     }
0121 };
0122 
0123 } // namespace PerceptualColor
0124 
0125 QTEST_MAIN(PerceptualColor::TestSetTranslation)
0126 
0127 // The following “include” is necessary because we do not use a header file:
0128 #include "testsettranslation.moc"