author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 12:15:23 +0300 | |
branch | RCL_3 |
changeset 12 | cc75c76972ee |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <QWidget> |
|
45 |
#include <qtranslator.h> |
|
46 |
#include <qfile.h> |
|
47 |
||
48 |
//TESTED_CLASS= |
|
49 |
//TESTED_FILES= |
|
50 |
||
51 |
class tst_QTranslator : public QWidget |
|
52 |
{ |
|
53 |
Q_OBJECT |
|
54 |
||
55 |
public: |
|
56 |
tst_QTranslator(); |
|
57 |
virtual ~tst_QTranslator(); |
|
58 |
||
59 |
public slots: |
|
60 |
void init(); |
|
61 |
void cleanup(); |
|
62 |
||
63 |
protected: |
|
64 |
bool event(QEvent *event); |
|
65 |
||
66 |
private slots: |
|
67 |
void load(); |
|
68 |
void load2(); |
|
69 |
void threadLoad(); |
|
70 |
void testLanguageChange(); |
|
71 |
void plural(); |
|
72 |
void translate_qm_file_generated_with_msgfmt(); |
|
73 |
||
74 |
private: |
|
75 |
int languageChangeEventCounter; |
|
76 |
}; |
|
77 |
||
78 |
||
79 |
tst_QTranslator::tst_QTranslator() |
|
80 |
: languageChangeEventCounter(0) |
|
81 |
{ |
|
82 |
show(); |
|
83 |
hide(); |
|
84 |
} |
|
85 |
||
86 |
tst_QTranslator::~tst_QTranslator() |
|
87 |
{ |
|
88 |
} |
|
89 |
||
90 |
void tst_QTranslator::init() |
|
91 |
{ |
|
92 |
} |
|
93 |
||
94 |
void tst_QTranslator::cleanup() |
|
95 |
{ |
|
96 |
} |
|
97 |
||
98 |
bool tst_QTranslator::event(QEvent *event) |
|
99 |
{ |
|
100 |
if (event->type() == QEvent::LanguageChange) |
|
101 |
++languageChangeEventCounter; |
|
102 |
return QWidget::event(event); |
|
103 |
} |
|
104 |
||
105 |
void tst_QTranslator::load() |
|
106 |
{ |
|
107 |
||
108 |
QTranslator tor( 0 ); |
|
109 |
tor.load("hellotr_la"); |
|
110 |
QVERIFY(!tor.isEmpty()); |
|
111 |
QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!")); |
|
112 |
} |
|
113 |
||
114 |
void tst_QTranslator::load2() |
|
115 |
{ |
|
116 |
QTranslator tor( 0 ); |
|
117 |
QFile file("hellotr_la.qm"); |
|
118 |
file.open(QFile::ReadOnly); |
|
119 |
QByteArray data = file.readAll(); |
|
120 |
tor.load((const uchar *)data.constData(), data.length()); |
|
121 |
QVERIFY(!tor.isEmpty()); |
|
122 |
QCOMPARE(tor.translate("QPushButton", "Hello world!"), QString::fromLatin1("Hallo Welt!")); |
|
123 |
} |
|
124 |
||
125 |
class TranslatorThread : public QThread |
|
126 |
{ |
|
127 |
void run() { |
|
128 |
QTranslator tor( 0 ); |
|
129 |
tor.load("hellotr_la"); |
|
130 |
||
131 |
if (tor.isEmpty()) |
|
132 |
qFatal("Could not load translation"); |
|
133 |
if (tor.translate("QPushButton", "Hello world!") != QString::fromLatin1("Hallo Welt!")) |
|
134 |
qFatal("Test string was not translated correctlys"); |
|
135 |
} |
|
136 |
}; |
|
137 |
||
138 |
||
139 |
void tst_QTranslator::threadLoad() |
|
140 |
{ |
|
141 |
TranslatorThread thread; |
|
142 |
thread.start(); |
|
143 |
QVERIFY(thread.wait(10 * 1000)); |
|
144 |
} |
|
145 |
||
146 |
void tst_QTranslator::testLanguageChange() |
|
147 |
{ |
|
148 |
languageChangeEventCounter = 0; |
|
149 |
||
150 |
QTranslator *tor = new QTranslator; |
|
151 |
tor->load("hellotr_la.qm"); |
|
152 |
qApp->sendPostedEvents(); |
|
153 |
qApp->sendPostedEvents(); |
|
154 |
QCOMPARE(languageChangeEventCounter, 0); |
|
155 |
||
156 |
tor->load("doesn't exist, same as clearing"); |
|
157 |
qApp->sendPostedEvents(); |
|
158 |
qApp->sendPostedEvents(); |
|
159 |
QCOMPARE(languageChangeEventCounter, 0); |
|
160 |
||
161 |
tor->load("hellotr_la.qm"); |
|
162 |
qApp->sendPostedEvents(); |
|
163 |
qApp->sendPostedEvents(); |
|
164 |
QCOMPARE(languageChangeEventCounter, 0); |
|
165 |
||
166 |
qApp->installTranslator(tor); |
|
167 |
qApp->sendPostedEvents(); |
|
168 |
qApp->sendPostedEvents(); |
|
169 |
QCOMPARE(languageChangeEventCounter, 1); |
|
170 |
||
171 |
tor->load("doesn't exist, same as clearing"); |
|
172 |
qApp->sendPostedEvents(); |
|
173 |
qApp->sendPostedEvents(); |
|
174 |
QCOMPARE(languageChangeEventCounter, 2); |
|
175 |
||
176 |
tor->load("hellotr_la.qm"); |
|
177 |
qApp->sendPostedEvents(); |
|
178 |
qApp->sendPostedEvents(); |
|
179 |
QCOMPARE(languageChangeEventCounter, 3); |
|
180 |
||
181 |
qApp->removeTranslator(tor); |
|
182 |
qApp->sendPostedEvents(); |
|
183 |
qApp->sendPostedEvents(); |
|
184 |
QCOMPARE(languageChangeEventCounter, 4); |
|
185 |
||
186 |
tor->load("doesn't exist, same as clearing"); |
|
187 |
qApp->sendPostedEvents(); |
|
188 |
qApp->sendPostedEvents(); |
|
189 |
QCOMPARE(languageChangeEventCounter, 4); |
|
190 |
||
191 |
qApp->installTranslator(tor); |
|
192 |
qApp->sendPostedEvents(); |
|
193 |
qApp->sendPostedEvents(); |
|
194 |
QCOMPARE(languageChangeEventCounter, 4); |
|
195 |
||
196 |
tor->load("hellotr_la.qm"); |
|
197 |
qApp->sendPostedEvents(); |
|
198 |
qApp->sendPostedEvents(); |
|
199 |
QCOMPARE(languageChangeEventCounter, 5); |
|
200 |
||
201 |
delete tor; |
|
202 |
tor = 0; |
|
203 |
qApp->sendPostedEvents(); |
|
204 |
qApp->sendPostedEvents(); |
|
205 |
QCOMPARE(languageChangeEventCounter, 6); |
|
206 |
} |
|
207 |
||
208 |
||
209 |
void tst_QTranslator::plural() |
|
210 |
{ |
|
211 |
||
212 |
QTranslator tor( 0 ); |
|
213 |
tor.load("hellotr_la"); |
|
214 |
QVERIFY(!tor.isEmpty()); |
|
215 |
QCoreApplication::installTranslator(&tor); |
|
216 |
QCoreApplication::Encoding e = QCoreApplication::UnicodeUTF8; |
|
217 |
QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, e, 0), QString::fromLatin1("Hallo 0 Welten!")); |
|
218 |
QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, e, 1), QString::fromLatin1("Hallo 1 Welt!")); |
|
219 |
QCOMPARE(QCoreApplication::translate("QPushButton", "Hello %n world(s)!", 0, e, 2), QString::fromLatin1("Hallo 2 Welten!")); |
|
220 |
} |
|
221 |
||
222 |
void tst_QTranslator::translate_qm_file_generated_with_msgfmt() |
|
223 |
{ |
|
224 |
QTranslator translator; |
|
225 |
translator.load("msgfmt_from_po"); |
|
226 |
qApp->installTranslator(&translator); |
|
227 |
||
228 |
QCOMPARE(QCoreApplication::translate("", "Intro"), QLatin1String("Einleitung")); |
|
229 |
// The file is converted from a po file, thus it does not have any context info. |
|
230 |
// The following should then not be translated |
|
231 |
QCOMPARE(QCoreApplication::translate("contekst", "Intro"), QLatin1String("Intro")); |
|
232 |
QCOMPARE(QCoreApplication::translate("contekst", "Intro\0\0"), QLatin1String("Intro")); |
|
233 |
QCOMPARE(QCoreApplication::translate("contekst", "Intro\0x"), QLatin1String("Intro")); |
|
234 |
QCOMPARE(QCoreApplication::translate("", "Intro\0\0"), QLatin1String("Einleitung")); |
|
235 |
QCOMPARE(QCoreApplication::translate("", "Intro\0x"), QLatin1String("Einleitung")); |
|
236 |
||
237 |
qApp->removeTranslator(&translator); |
|
238 |
} |
|
239 |
||
240 |
QTEST_MAIN(tst_QTranslator) |
|
241 |
#include "tst_qtranslator.moc" |