Debian Codegraph @

tests/unit/i18n13/RelativeTime/src/tst_i18n_RelativeTime.cpp

raw symbols SCIP
1 /*
2 * Copyright 2015 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: Nick Dedekind <[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_RelativeTime : public QObject
44 {
45 Q_OBJECT
46
47 private:
48 QQuickView *view;
49
50 public:
51 tst_I18n_RelativeTime() :
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() + "/lomiri-ui-toolkit");
81 setenv("APP_DIR", testAppDir.toUtf8(), 1);
82
83 // Verify that we set it correctly
84 QVERIFY(QFileInfo(testAppDir + "/share/locale/en/LC_MESSAGES/lomiri-ui-toolkit.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_RelativeTime()
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/RelativeTime.qml");
117 QVERIFY(root);
118 QQuickItem *mainView = root;
119
120 // There no translation happens
121 QQuickItem* page(testItem(mainView, "page"));
122 QVERIFY(page);
123
124 QQuickItem* timeFarAway(testItem(page, "timeFarAway"));
125 QVERIFY(timeFarAway);
126 QCOMPARE(timeFarAway->property("text").toString(),
127 QDateTime(QDate(2000,1,1), QTime(0,0,0,0)).toString("ddd d MMM'\u2003'HH:mm"));
128 QQuickItem* timeNow(testItem(page, "timeNow"));
129 QVERIFY(timeNow);
130 QCOMPARE(timeNow->property("text").toString(), QString("Now"));
131 QQuickItem* timeMinuteBefore(testItem(page, "timeMinuteBefore"));
132 QVERIFY(timeMinuteBefore);
133 QCOMPARE(timeMinuteBefore->property("text").toString(), QString("1 minute ago"));
134 QQuickItem* timeMinuteAfter(testItem(page, "timeMinuteAfter"));
135 QVERIFY(timeMinuteAfter);
136 QCOMPARE(timeMinuteAfter->property("text").toString(), QString("1 minute"));
137 QQuickItem* tenMinutesBefore(testItem(page, "tenMinutesBefore"));
138 QVERIFY(tenMinutesBefore);
139 QCOMPARE(tenMinutesBefore->property("text").toString(), QString("10 minutes ago"));
140 QQuickItem* tenMinutesAfter(testItem(page, "tenMinutesAfter"));
141 QVERIFY(tenMinutesAfter);
142 QCOMPARE(tenMinutesAfter->property("text").toString(), QString("10 minutes"));
143
144 // There no translation happens in C++ either
145 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime()), QString("Now"));
146 QCOMPARE(i18n->relativeDateTime(QDateTime(QDate(2000,1,1), QTime(0,0,0,0))),
147 QDateTime(QDate(2000,1,1), QTime(0,0,0,0)).toString("ddd d MMM'\u2003'HH:mm"));
148 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-60)), QString("1 minute ago"));
149 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(60)), QString("1 minute"));
150 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-600)), QString("10 minutes ago"));
151 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(600)), QString("10 minutes"));
152
153 // Was the locale folder detected and set?
154 QString boundDomain(C::bindtextdomain(i18n->domain().toUtf8(), ((const char*)0)));
155 QString testAppDir(QDir::currentPath() + "/lomiri-ui-toolkit");
156 QString expectedLocalePath(QDir(testAppDir).filePath("share/locale"));
157 QCOMPARE(boundDomain, expectedLocalePath);
158 // Is the domain gettext uses correct?
159 QString gettextDomain(C::textdomain(((const char*)0)));
160 QCOMPARE(gettextDomain, i18n->domain());
161 // Is the compiled en_US message catalog in the right location?
162 QString messageCatalog(boundDomain + "/en/LC_MESSAGES/lomiri-ui-toolkit.mo");
163 QVERIFY(QFileInfo(messageCatalog).exists());
164
165 // Check if system has en_US locale, otherwise gettext won't work
166 QProcess localeA;
167 localeA.start("locale -a");
168 QVERIFY(localeA.waitForFinished());
169 QVERIFY(QString(localeA.readAll()).split("\n").contains("en_US.utf8"));
170
171 i18n->setLanguage("en_US.utf8");
172 QSignalSpy spy(i18n, SIGNAL(languageChanged()));
173 spy.wait();
174
175 // Inspect translated strings in QML
176 QCOMPARE(timeNow->property("text").toString(), QString("tr:Now"));
177 QCOMPARE(timeMinuteBefore->property("text").toString(), QString("tr:1 minute ago"));
178 QCOMPARE(timeMinuteAfter->property("text").toString(), QString("tr:1 minute"));
179 QCOMPARE(tenMinutesBefore->property("text").toString(), QString("tr:10 minutes ago"));
180 QCOMPARE(tenMinutesAfter->property("text").toString(), QString("tr:10 minutes"));
181 QCOMPARE(timeFarAway->property("text").toString(), QString("tr:FarAway"));
182
183 // Translate in C++
184 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime()), QString("tr:Now"));
185 QCOMPARE(i18n->relativeDateTime(QDateTime(QDate(2000,1,1), QTime(0,0,0,0))), QString("tr:FarAway"));
186 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-60)), QString("tr:1 minute ago"));
187 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(60)), QString("tr:1 minute"));
188 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-600)), QString("tr:10 minutes ago"));
189 QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(600)), QString("tr:10 minutes"));
190 }
191 };
192
193 // The C++ equivalent of QTEST_MAIN(tst_I18n_RelativeTime) with added initialization
194 int main(int argc, char *argv[])
195 {
196 // LC_ALL would fail the test case; it must be unset before execution
197 unsetenv("LC_ALL");
198
199 QGuiApplication app(argc, argv);
200 app.setAttribute(Qt::AA_Use96Dpi, true);
201 tst_I18n_RelativeTime* testObject = new tst_I18n_RelativeTime();
202 return QTest::qExec(static_cast<QObject*>(testObject), argc, argv);
203 }
204
205 #include "tst_i18n_RelativeTime.moc"