1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include <qtranslator.h>
47 class tst_QTranslator : public QWidget
54 bool event(QEvent *event);
61 void testLanguageChange();
63 void translate_qm_file_generated_with_msgfmt();
64 void loadFromResource();
68 int languageChangeEventCounter;
71 tst_QTranslator::tst_QTranslator()
72 : languageChangeEventCounter(0)
78 void tst_QTranslator::initTestCase()
80 // chdir into the directory containing our testdata,
81 // to make the code simpler (load testdata via relative paths)
82 QString testdata_dir = QFileInfo(QFINDTESTDATA("hellotr_la.qm")).absolutePath();
83 QVERIFY2(QDir::setCurrent(testdata_dir), qPrintable("Could not chdir to " + testdata_dir));
86 bool tst_QTranslator::event(QEvent *event)
88 if (event->type() == QEvent::LanguageChange)
89 ++languageChangeEventCounter;
90 return QWidget::event(event);
93 void tst_QTranslator::load()
97 tor.load("hellotr_la");
98 QVERIFY(!tor.isEmpty());
99 QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!"));
102 void tst_QTranslator::load2()
104 QTranslator tor( 0 );
105 QFile file("hellotr_la.qm");
106 file.open(QFile::ReadOnly);
107 QByteArray data = file.readAll();
108 tor.load((const uchar *)data.constData(), data.length());
109 QVERIFY(!tor.isEmpty());
110 QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!"));
113 class TranslatorThread : public QThread
116 QTranslator tor( 0 );
117 tor.load("hellotr_la");
120 qFatal("Could not load translation");
121 if (tor.translate("QPushButton", "Hello world!") != QString::fromLatin1("Hallo Welt!"))
122 qFatal("Test string was not translated correctlys");
127 void tst_QTranslator::threadLoad()
129 TranslatorThread thread;
131 QVERIFY(thread.wait(10 * 1000));
134 void tst_QTranslator::testLanguageChange()
136 languageChangeEventCounter = 0;
138 QTranslator *tor = new QTranslator;
139 tor->load("hellotr_la.qm");
140 qApp->sendPostedEvents();
141 qApp->sendPostedEvents();
142 QCOMPARE(languageChangeEventCounter, 0);
144 tor->load("doesn't exist, same as clearing");
145 qApp->sendPostedEvents();
146 qApp->sendPostedEvents();
147 QCOMPARE(languageChangeEventCounter, 0);
149 tor->load("hellotr_la.qm");
150 qApp->sendPostedEvents();
151 qApp->sendPostedEvents();
152 QCOMPARE(languageChangeEventCounter, 0);
154 qApp->installTranslator(tor);
155 qApp->sendPostedEvents();
156 qApp->sendPostedEvents();
157 QCOMPARE(languageChangeEventCounter, 1);
159 tor->load("doesn't exist, same as clearing");
160 qApp->sendPostedEvents();
161 qApp->sendPostedEvents();
162 QCOMPARE(languageChangeEventCounter, 2);
164 tor->load("hellotr_la.qm");
165 qApp->sendPostedEvents();
166 qApp->sendPostedEvents();
167 QCOMPARE(languageChangeEventCounter, 3);
169 qApp->removeTranslator(tor);
170 qApp->sendPostedEvents();
171 qApp->sendPostedEvents();
172 QCOMPARE(languageChangeEventCounter, 4);
174 tor->load("doesn't exist, same as clearing");
175 qApp->sendPostedEvents();
176 qApp->sendPostedEvents();
177 QCOMPARE(languageChangeEventCounter, 4);
179 qApp->installTranslator(tor);
180 qApp->sendPostedEvents();
181 qApp->sendPostedEvents();
182 QCOMPARE(languageChangeEventCounter, 4);
184 tor->load("hellotr_la.qm");
185 qApp->sendPostedEvents();
186 qApp->sendPostedEvents();
187 QCOMPARE(languageChangeEventCounter, 5);
191 qApp->sendPostedEvents();
192 qApp->sendPostedEvents();
193 QCOMPARE(languageChangeEventCounter, 6);
197 void tst_QTranslator::plural()
200 QTranslator tor( 0 );
201 tor.load("hellotr_la");
202 QVERIFY(!tor.isEmpty());
203 QCoreApplication::installTranslator(&tor);
204 QCoreApplication::Encoding e = QCoreApplication::UnicodeUTF8;
205 QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, e, 0), QString::fromLatin1("Hallo 0 Welten!"));
206 QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, e, 1), QString::fromLatin1("Hallo 1 Welt!"));
207 QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, e, 2), QString::fromLatin1("Hallo 2 Welten!"));
210 void tst_QTranslator::translate_qm_file_generated_with_msgfmt()
212 QTranslator translator;
213 translator.load("msgfmt_from_po");
214 qApp->installTranslator(&translator);
216 QCOMPARE(QCoreApplication::translate("", "Intro"), QLatin1String("Einleitung"));
217 // The file is converted from a po file, thus it does not have any context info.
218 // The following should then not be translated
219 QCOMPARE(QCoreApplication::translate("contekst", "Intro"), QLatin1String("Intro"));
220 QCOMPARE(QCoreApplication::translate("contekst", "Intro\0\0"), QLatin1String("Intro"));
221 QCOMPARE(QCoreApplication::translate("contekst", "Intro\0x"), QLatin1String("Intro"));
222 QCOMPARE(QCoreApplication::translate("", "Intro\0\0"), QLatin1String("Einleitung"));
223 QCOMPARE(QCoreApplication::translate("", "Intro\0x"), QLatin1String("Einleitung"));
225 qApp->removeTranslator(&translator);
228 void tst_QTranslator::loadFromResource()
231 tor.load(":/tst_qtranslator/hellotr_la.qm");
232 QVERIFY(!tor.isEmpty());
233 QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!"));
236 void tst_QTranslator::loadDirectory()
238 QString current_base = QDir::current().dirName();
239 QVERIFY(QFileInfo("../" + current_base).isDir());
242 tor.load(current_base, "..");
243 QVERIFY(tor.isEmpty());
246 QTEST_MAIN(tst_QTranslator)
247 #include "tst_qtranslator.moc"