941d3a9be466b16f07e17dc3c2ea8efcd3688e56
[profile/ivi/qtbase.git] / tests / auto / corelib / kernel / qtranslator / tst_qtranslator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include <QWidget>
44 #include <qtranslator.h>
45 #include <qfile.h>
46
47 class tst_QTranslator : public QWidget
48 {
49     Q_OBJECT
50
51 public:
52     tst_QTranslator();
53 protected:
54     bool event(QEvent *event);
55 private slots:
56     void initTestCase();
57
58     void load();
59     void load2();
60     void threadLoad();
61     void testLanguageChange();
62     void plural();
63     void translate_qm_file_generated_with_msgfmt();
64     void loadFromResource();
65     void loadDirectory();
66
67 private:
68     int languageChangeEventCounter;
69 };
70
71 tst_QTranslator::tst_QTranslator()
72     : languageChangeEventCounter(0)
73 {
74     show();
75     hide();
76 }
77
78 void tst_QTranslator::initTestCase()
79 {
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));
84 }
85
86 bool tst_QTranslator::event(QEvent *event)
87 {
88     if (event->type() == QEvent::LanguageChange)
89         ++languageChangeEventCounter;
90     return QWidget::event(event);
91 }
92
93 void tst_QTranslator::load()
94 {
95
96     QTranslator tor( 0 );
97     tor.load("hellotr_la");
98     QVERIFY(!tor.isEmpty());
99     QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!"));
100 }
101
102 void tst_QTranslator::load2()
103 {
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!"));
111 }
112
113 class TranslatorThread : public QThread
114 {
115     void run() {
116         QTranslator tor( 0 );
117         tor.load("hellotr_la");
118
119         if (tor.isEmpty())
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");
123     }
124 };
125
126
127 void tst_QTranslator::threadLoad()
128 {
129     TranslatorThread thread;
130     thread.start();
131     QVERIFY(thread.wait(10 * 1000));
132 }
133
134 void tst_QTranslator::testLanguageChange()
135 {
136     languageChangeEventCounter = 0;
137
138     QTranslator *tor = new QTranslator;
139     tor->load("hellotr_la.qm");
140     qApp->sendPostedEvents();
141     qApp->sendPostedEvents();
142     QCOMPARE(languageChangeEventCounter, 0);
143
144     tor->load("doesn't exist, same as clearing");
145     qApp->sendPostedEvents();
146     qApp->sendPostedEvents();
147     QCOMPARE(languageChangeEventCounter, 0);
148
149     tor->load("hellotr_la.qm");
150     qApp->sendPostedEvents();
151     qApp->sendPostedEvents();
152     QCOMPARE(languageChangeEventCounter, 0);
153
154     qApp->installTranslator(tor);
155     qApp->sendPostedEvents();
156     qApp->sendPostedEvents();
157     QCOMPARE(languageChangeEventCounter, 1);
158
159     tor->load("doesn't exist, same as clearing");
160     qApp->sendPostedEvents();
161     qApp->sendPostedEvents();
162     QCOMPARE(languageChangeEventCounter, 2);
163
164     tor->load("hellotr_la.qm");
165     qApp->sendPostedEvents();
166     qApp->sendPostedEvents();
167     QCOMPARE(languageChangeEventCounter, 3);
168
169     qApp->removeTranslator(tor);
170     qApp->sendPostedEvents();
171     qApp->sendPostedEvents();
172     QCOMPARE(languageChangeEventCounter, 4);
173
174     tor->load("doesn't exist, same as clearing");
175     qApp->sendPostedEvents();
176     qApp->sendPostedEvents();
177     QCOMPARE(languageChangeEventCounter, 4);
178
179     qApp->installTranslator(tor);
180     qApp->sendPostedEvents();
181     qApp->sendPostedEvents();
182     QCOMPARE(languageChangeEventCounter, 4);
183
184     tor->load("hellotr_la.qm");
185     qApp->sendPostedEvents();
186     qApp->sendPostedEvents();
187     QCOMPARE(languageChangeEventCounter, 5);
188
189     delete tor;
190     tor = 0;
191     qApp->sendPostedEvents();
192     qApp->sendPostedEvents();
193     QCOMPARE(languageChangeEventCounter, 6);
194 }
195
196
197 void tst_QTranslator::plural()
198 {
199
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!"));
208 }
209
210 void tst_QTranslator::translate_qm_file_generated_with_msgfmt()
211 {
212     QTranslator translator;
213     translator.load("msgfmt_from_po");
214     qApp->installTranslator(&translator);
215
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"));
224
225     qApp->removeTranslator(&translator);
226 }
227
228 void tst_QTranslator::loadFromResource()
229 {
230     QTranslator tor;
231     tor.load(":/tst_qtranslator/hellotr_la.qm");
232     QVERIFY(!tor.isEmpty());
233     QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!"));
234 }
235
236 void tst_QTranslator::loadDirectory()
237 {
238     QString current_base = QDir::current().dirName();
239     QVERIFY(QFileInfo("../" + current_base).isDir());
240
241     QTranslator tor;
242     tor.load(current_base, "..");
243     QVERIFY(tor.isEmpty());
244 }
245
246 QTEST_MAIN(tst_QTranslator)
247 #include "tst_qtranslator.moc"