fd7515068b89e2fc10a9c5b450d21d82899579c9
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativetranslation / tst_qdeclarativetranslation.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 <qtest.h>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeComponent>
45 #include <QTranslator>
46 #include "../../shared/util.h"
47
48 class tst_qdeclarativetranslation : public QDeclarativeDataTest
49 {
50     Q_OBJECT
51 public:
52     tst_qdeclarativetranslation() {}
53
54 private slots:
55     void translation();
56     void idTranslation();
57     void translationInQrc();
58 };
59
60 void tst_qdeclarativetranslation::translation()
61 {
62     QTranslator translator;
63     translator.load(QLatin1String("qml_fr"), dataDirectory());
64     QCoreApplication::installTranslator(&translator);
65
66     QDeclarativeEngine engine;
67     QDeclarativeComponent component(&engine, testFileUrl("translation.qml"));
68     QObject *object = component.create();
69     QVERIFY(object != 0);
70
71     QCOMPARE(object->property("basic").toString(), QLatin1String("bonjour"));
72     QCOMPARE(object->property("basic2").toString(), QLatin1String("au revoir"));
73     QCOMPARE(object->property("basic3").toString(), QLatin1String("bonjour"));
74     QCOMPARE(object->property("disambiguation").toString(), QLatin1String("salut"));
75     QCOMPARE(object->property("disambiguation2").toString(), QString::fromUtf8("\xc3\xa0 plus tard"));
76     QCOMPARE(object->property("disambiguation3").toString(), QLatin1String("salut"));
77     QCOMPARE(object->property("noop").toString(), QLatin1String("bonjour"));
78     QCOMPARE(object->property("noop2").toString(), QLatin1String("au revoir"));
79     QCOMPARE(object->property("singular").toString(), QLatin1String("1 canard"));
80     QCOMPARE(object->property("singular2").toString(), QLatin1String("1 canard"));
81     QCOMPARE(object->property("plural").toString(), QLatin1String("2 canards"));
82     QCOMPARE(object->property("plural2").toString(), QLatin1String("2 canards"));
83
84     QCoreApplication::removeTranslator(&translator);
85     delete object;
86 }
87
88 void tst_qdeclarativetranslation::idTranslation()
89 {
90     QTranslator translator;
91     translator.load(QLatin1String("qmlid_fr"), dataDirectory());
92     QCoreApplication::installTranslator(&translator);
93
94     QDeclarativeEngine engine;
95     QDeclarativeComponent component(&engine, testFileUrl("idtranslation.qml"));
96     QObject *object = component.create();
97     QVERIFY(object != 0);
98
99     QCOMPARE(object->property("idTranslation").toString(), QLatin1String("bonjour tout le monde"));
100     QCOMPARE(object->property("idTranslation2").toString(), QLatin1String("bonjour tout le monde"));
101     QCOMPARE(object->property("idTranslation3").toString(), QLatin1String("bonjour tout le monde"));
102
103     QCoreApplication::removeTranslator(&translator);
104     delete object;
105 }
106
107 void tst_qdeclarativetranslation::translationInQrc()
108 {
109     QTranslator translator;
110     translator.load(":/qml_fr.qm");
111     QCoreApplication::installTranslator(&translator);
112
113     QDeclarativeEngine engine;
114     QDeclarativeComponent component(&engine, QUrl("qrc:/translation.qml"));
115     QObject *object = component.create();
116     QVERIFY(object != 0);
117
118     QCOMPARE(object->property("basic").toString(), QLatin1String("bonjour"));
119     QCOMPARE(object->property("basic2").toString(), QLatin1String("au revoir"));
120     QCOMPARE(object->property("basic3").toString(), QLatin1String("bonjour"));
121     QCOMPARE(object->property("disambiguation").toString(), QLatin1String("salut"));
122     QCOMPARE(object->property("disambiguation2").toString(), QString::fromUtf8("\xc3\xa0 plus tard"));
123     QCOMPARE(object->property("disambiguation3").toString(), QLatin1String("salut"));
124     QCOMPARE(object->property("noop").toString(), QLatin1String("bonjour"));
125     QCOMPARE(object->property("noop2").toString(), QLatin1String("au revoir"));
126     QCOMPARE(object->property("singular").toString(), QLatin1String("1 canard"));
127     QCOMPARE(object->property("singular2").toString(), QLatin1String("1 canard"));
128     QCOMPARE(object->property("plural").toString(), QLatin1String("2 canards"));
129     QCOMPARE(object->property("plural2").toString(), QLatin1String("2 canards"));
130
131     QCoreApplication::removeTranslator(&translator);
132     delete object;
133 }
134
135 QTEST_MAIN(tst_qdeclarativetranslation)
136
137 #include "tst_qdeclarativetranslation.moc"