Debian Codegraph @

tests/unit/i18n13/LocalizedApp/src/tst_i18n_LocalizedApp.cpp

raw symbols SCIP
1 /*
2 * Copyright 2012-2013 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Christian Dywan <[email protected]>
17 */
18
19 namespace C {
20 #include <libintl.h>
21 }
22
23 #include <QtCore/QCoreApplication>
24 #include <QtCore/QDir>
25 #include <QtCore/QFileInfo>
26 #include <QtCore/QProcessEnvironment>
27 #include <QtCore/QStandardPaths>
28 #include <QtCore/QString>
29 #include <QtCore/QTextCodec>
30 #include <QtCore/QThread>
31 #include <QtQml/QQmlEngine>
32 #include <QtQuick/QQuickItem>
33 #include <QtQuick/QQuickView>
34 #include <QtTest/QTest>
35 #include <QtTest/QtTestGui>
36 #include <QtTest/QSignalSpy>
37 #include <LomiriToolkit/lomiritoolkitmodule.h>
38 #include <LomiriToolkit/private/ucunits_p.h>
39 #include <LomiriToolkit/private/i18n_p.h>
40
41 UT_USE_NAMESPACE
42
43 class tst_I18n_LocalizedApp : public QObject
44 {
45 Q_OBJECT
46
47 private:
48 QQuickView *view;
49
50 public:
51 tst_I18n_LocalizedApp() :
52 view(0)
53 {
54 }
55
56 QQuickItem *loadTest(const QString &document)
57 {
58 // load the document
59 view->setSource(QUrl::fromLocalFile(document));
60 QCoreApplication::processEvents();
61
62 return view->rootObject();
63 }
64
65 QQuickItem *testItem(QQuickItem *that, const QString &identifier)
66 {
67 if (that->property(identifier.toLocal8Bit()).isValid())
68 return that->property(identifier.toLocal8Bit()).value<QQuickItem*>();
69
70 QList<QQuickItem*> children = that->findChildren<QQuickItem*>(identifier);
71 return (children.count() > 0) ? children[0] : 0;
72 }
73
74 private Q_SLOTS:
75
76 void initTestCase()
77 {
78 // Set test locale folder in the environment
79 // Using setenv because QProcessEnvironment ignores changes
80 QString testAppDir(QDir::currentPath() + "/localizedApp");
81 setenv("APP_DIR", testAppDir.toUtf8(), 1);
82
83 // Verify that we set it correctly
84 QVERIFY(QFileInfo(testAppDir + "/share/locale/en/LC_MESSAGES/localizedApp.mo").exists());
85
86 QString modules(LOMIRI_QML_IMPORT_PATH);
87 QVERIFY(QDir(modules).exists());
88
89 view = new QQuickView;
90 QQmlEngine *quickEngine = view->engine();
91 LomiriToolkitModule::initializeContextProperties(quickEngine);
92
93 view->setGeometry(0,0, UCUnits::instance()->gu(40), UCUnits::instance()->gu(30));
94 //add modules folder so we have access to the plugin from QML
95 QStringList imports = quickEngine->importPathList();
96 imports.prepend(QDir(modules).absolutePath());
97 quickEngine->setImportPathList(imports);
98 }
99
100 void cleanupTestCase()
101 {
102 delete view;
103 }
104
105 void testCase_LocalizedApp()
106 {
107 QQmlEngine engine;
108 LomiriToolkitModule::initializeContextProperties(&engine);
109 LomiriI18n* i18n = LomiriI18n::instance();
110 // By default no domain is set
111 QCOMPARE(i18n->domain(), QString(""));
112
113 // Start out with no localization
114 i18n->setLanguage("C");
115 // Load the app which should pick up the locale we prepared
116 QQuickItem *root = loadTest("src/LocalizedApp.qml");
117 QVERIFY(root);
118 QQuickItem *mainView = root;
119 // Sanity checks to avoid confusion
120 QString applicationName(mainView->property("applicationName").toString());
121 QCOMPARE(applicationName, QString("localizedApp"));
122 QCOMPARE(applicationName, QCoreApplication::applicationName());
123 QCOMPARE(applicationName, i18n->domain());
124
125 // There no translation happens
126 QQuickItem* page(testItem(mainView, "page"));
127 QVERIFY(page);
128 QCOMPARE(page->property("title").toString(), QString("Welcome"));
129 QQuickItem* button(testItem(page, "button"));
130 QVERIFY(button);
131 QCOMPARE(button->property("text").toString(), QString("Count the kilometres"));
132 QQuickItem* all1(testItem(page, "all1"));
133 QVERIFY(all1);
134 QCOMPARE(all1->property("text").toString(), QString("All"));
135 QQuickItem* all2(testItem(page, "all2"));
136 QVERIFY(all2);
137 QCOMPARE(all2->property("text").toString(), QString("All"));
138
139 // There no translation happens in C++ either
140 QCOMPARE(i18n->dtr(i18n->domain(), QString("Welcome")), QString("Welcome"));
141 QCOMPARE(i18n->tr(QString("Count the kilometres")), QString("Count the kilometres"));
142 QCOMPARE(i18n->ctr(QString("All Contacts"), QString("All")), QString("All"));
143 QCOMPARE(i18n->ctr(QString("All Calls"), QString("All")), QString("All"));
144
145 // Was the locale folder detected and set?
146 QString boundDomain(C::bindtextdomain(i18n->domain().toUtf8(), ((const char*)0)));
147 QString testAppDir(QDir::currentPath() + "/localizedApp");
148 QString expectedLocalePath(QDir(testAppDir).filePath("share/locale"));
149 QCOMPARE(boundDomain, expectedLocalePath);
150 // Is the domain gettext uses correct?
151 QString gettextDomain(C::textdomain(((const char*)0)));
152 QCOMPARE(gettextDomain, i18n->domain());
153 // Is the compiled en_US message catalog in the right location?
154 QString messageCatalog(boundDomain + "/en/LC_MESSAGES/localizedApp.mo");
155 QVERIFY(QFileInfo(messageCatalog).exists());
156
157 /* For manual testing one can do something like
158 env LANGUAGE=en_US TEXTDOMAINDIR=./tests/unit/tst_i18n/locale/ gettext localizedApp 'Welcome'
159 */
160
161 // Check if system has en_US locale, otherwise gettext won't work
162 QProcess localeA;
163 localeA.start("locale -a");
164 QVERIFY(localeA.waitForFinished());
165 QVERIFY(QString(localeA.readAll()).split("\n").contains("en_US.utf8"));
166
167 i18n->setLanguage("en_US.utf8");
168 QSignalSpy spy(i18n, SIGNAL(languageChanged()));
169 spy.wait();
170
171 // Inspect translated strings in QML
172 QCOMPARE(page->property("title").toString(), QString("Greets"));
173 QCOMPARE(button->property("text").toString(), QString("Count the clicks"));
174 QCOMPARE(all1->property("text").toString(), QString("Todos"));
175 QCOMPARE(all2->property("text").toString(), QString("Todas"));
176 // Only tagged, not actually translated
177 QQuickItem* button2(testItem(page, "button2"));
178 QVERIFY(button2);
179 QCOMPARE(button2->property("text").toString(), QString("Count the kittens"));
180 QQuickItem* all3(testItem(page, "all3"));
181 QVERIFY(all3);
182 QCOMPARE(all3->property("text").toString(), QString("All"));
183
184 // Translate in C++
185 QCOMPARE(i18n->dtr(i18n->domain(), QString("Welcome")), QString("Greets"));
186 QCOMPARE(i18n->tr(QString("Count the kilometres")), QString("Count the clicks"));
187 QCOMPARE(i18n->ctr(QString("All Contacts"), QString("All")), QString("Todos"));
188 QCOMPARE(i18n->ctr(QString("All Calls"), QString("All")), QString("Todas"));
189 // Only tagged, not actually translated
190 QCOMPARE(i18n->tag(QString("All kittens")), QString("All kittens"));
191 QCOMPARE(i18n->tag(QString("All Cats"), QString("All")), QString("All"));
192 // Sanity-check that the test strings would otherwise work and not no-op by accident
193 QCOMPARE(i18n->tr(QString("Count the kittens")), QString("Contar los gatitos"));
194 QCOMPARE(i18n->ctr(QString("All Cats"), QString("All")), QString("Cada"));
195 }
196 };
197
198 // The C++ equivalent of QTEST_MAIN(tst_I18n_LocalizedApp) with added initialization
199 int main(int argc, char *argv[])
200 {
201 // LC_ALL would fail the test case; it must be unset before execution
202 unsetenv("LC_ALL");
203
204 QGuiApplication app(argc, argv);
205 app.setAttribute(Qt::AA_Use96Dpi, true);
206 tst_I18n_LocalizedApp* testObject = new tst_I18n_LocalizedApp();
207 return QTest::qExec(static_cast<QObject*>(testObject), argc, argv);
208 }
209
210 #include "tst_i18n_LocalizedApp.moc"