File indexing completed on 2024-05-12 05:42:41

0001 #include <cutehmi/autogen/cutehmi.dirs.hpp>
0002 
0003 #include <cutehmi/Internationalizer.hpp>
0004 
0005 #include <cutehmi/test/random.hpp>
0006 
0007 #include <QtTest/QtTest>
0008 
0009 namespace cutehmi {
0010 
0011 class test_Internationalizer:
0012     public QObject
0013 {
0014         Q_OBJECT
0015 
0016     private slots:
0017         void initTestCase();
0018 
0019         void loadTranslation();
0020 
0021         void unloadTranslation();
0022 
0023         void loadQtTranslation();
0024 
0025         void unloadQtTranslation();
0026 
0027         void mixedTranslations();
0028 };
0029 
0030 void test_Internationalizer::initTestCase()
0031 {
0032     QDir toolsDir = QDir::current();
0033     toolsDir.cd(QDir("/" CUTEHMI_DIRS_TESTS_INSTALL_SUBDIR).relativeFilePath("/" CUTEHMI_DIRS_TOOLS_INSTALL_SUBDIR));
0034     QDir::setCurrent(toolsDir.absolutePath());
0035 }
0036 
0037 void test_Internationalizer::loadTranslation()
0038 {
0039     Internationalizer & i18ner = Internationalizer::Instance();
0040     i18ner.setUILanguage("pl_PL");
0041 
0042     i18ner.loadTranslation("CuteHMI.2.test", false);
0043     QVERIFY(i18ner.m->translators.contains("CuteHMI.2.test"));
0044     if (i18ner.m->translators.contains("CuteHMI.2.test"))
0045         QCOMPARE(i18ner.m->translators.value("CuteHMI.2.test")->translate("cutehmi::Error|", "No error."), "Brak błędu.");
0046 
0047     i18ner.loadTranslation("CuteHMI.2.test_Internationalizer");
0048     QVERIFY(i18ner.m->translators.contains("CuteHMI.2.test_Internationalizer"));
0049     if (i18ner.m->translators.contains("CuteHMI.2.test_Internationalizer"))
0050         QCOMPARE(i18ner.m->translators.value("CuteHMI.2.test_Internationalizer")->translate("cutehmi::Error|", "No error."), "Brak błędu.");
0051 }
0052 
0053 void test_Internationalizer::unloadTranslation()
0054 {
0055     Internationalizer & i18ner = Internationalizer::Instance();
0056     i18ner.setUILanguage("pl_PL");
0057 
0058     i18ner.unloadTranslation("CuteHMI.2.test");
0059     QVERIFY(!i18ner.m->translators.contains("CuteHMI.2.test"));
0060 
0061     i18ner.unloadTranslation("CuteHMI.2.test_Internationalizer");
0062     QVERIFY(!i18ner.m->translators.contains("CuteHMI.2.test_Internationalizer"));
0063 }
0064 
0065 void test_Internationalizer::loadQtTranslation()
0066 {
0067     Internationalizer & i18ner = Internationalizer::Instance();
0068     i18ner.setUILanguage("pl_PL");
0069 
0070     QVERIFY(i18ner.m->qtTranslator == nullptr);
0071     i18ner.loadQtTranslation();
0072     QVERIFY(i18ner.m->qtTranslator != nullptr);
0073 }
0074 
0075 void test_Internationalizer::unloadQtTranslation()
0076 {
0077     Internationalizer & i18ner = Internationalizer::Instance();
0078     i18ner.setUILanguage("pl_PL");
0079 
0080     i18ner.unloadQtTranslation();
0081     QVERIFY(i18ner.m->qtTranslator == nullptr);
0082 }
0083 
0084 void test_Internationalizer::mixedTranslations()
0085 {
0086     Internationalizer & i18ner = Internationalizer::Instance();
0087 
0088     loadTranslation();
0089     unloadTranslation();
0090 
0091     loadQtTranslation();
0092     unloadQtTranslation();
0093 
0094     loadQtTranslation();
0095     QVERIFY(i18ner.m->qtTranslator != nullptr);
0096     i18ner.unloadTranslations(false);
0097     QVERIFY(i18ner.m->qtTranslator != nullptr);
0098     i18ner.unloadTranslations();
0099     QVERIFY(i18ner.m->qtTranslator == nullptr);
0100 
0101     loadTranslation();
0102     loadQtTranslation();
0103     i18ner.unloadTranslations();    // Should nullify i18ner.m->qtTranslator as well.
0104     QVERIFY(i18ner.m->qtTranslator == nullptr);
0105     QVERIFY(i18ner.m->translators.isEmpty());
0106 
0107     i18ner.setUILanguage("no_NO");
0108     i18ner.loadTranslation("CuteHMI.2.test", false);
0109     QVERIFY(i18ner.m->translators.contains("CuteHMI.2.test"));
0110     if (i18ner.m->translators.contains("CuteHMI.2.test"))
0111         QCOMPARE(i18ner.m->translators.value("CuteHMI.2.test")->translate("cutehmi::Error|", "No error."), "");
0112     i18ner.setUILanguage("pl_PL");
0113     if (i18ner.m->translators.contains("CuteHMI.2.test_Internationalizer"))
0114         QCOMPARE(i18ner.m->translators.value("CuteHMI.2.test_Internationalizer")->translate("cutehmi::Error|", "No error."), "Brak błędu.");
0115     i18ner.unloadTranslation("CuteHMI.2.test");
0116     if (i18ner.m->translators.contains("CuteHMI.2.test"))
0117         QCOMPARE(i18ner.m->translators.value("CuteHMI.2.test")->translate("cutehmi::Error|", "No error."), "");
0118 }
0119 
0120 }
0121 
0122 QTEST_MAIN(cutehmi::test_Internationalizer)
0123 #include "test_Internationalizer.moc"
0124 
0125 //(c)C: Copyright © 2020-2024, Michał Policht <michal@policht.pl>. All rights reserved.
0126 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
0127 //(c)C: This file is a part of CuteHMI.
0128 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
0129 //(c)C: CuteHMI 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 Lesser General Public License for more details.
0130 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI.  If not, see <https://www.gnu.org/licenses/>.
0131 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
0132 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
0133 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
0134 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.