3
* Copyright 2015 Canonical Ltd.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Lesser General Public License as published by
7
* the Free Software Foundation; version 3.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Author: Nick Dedekind <nick.dedekind@gmail.com>
20
#include <QtCore/QString>
21
#include <QtCore/QTextCodec>
22
#include <QtCore/QStandardPaths>
23
#include <QtCore/QDir>
24
#include <QtCore/QProcessEnvironment>
25
#include <QtTest/QTest>
26
#include <QtTest/QSignalSpy>
27
#include <QtCore/QCoreApplication>
28
#include <QtQml/QQmlEngine>
29
#include <QtQuick/QQuickView>
30
#include <QtQuick/QQuickItem>
31
#include <QtCore/QThread>
32
#include <QtCore/QFileInfo>
33
#include <QtCore/QDir>
34
#include <QtTest/qtest_gui.h>
43
class tst_I18n_RelativeTime : public QObject
51
tst_I18n_RelativeTime() :
56
QQuickItem *loadTest(const QString &document)
59
view->setSource(QUrl::fromLocalFile(document));
60
QTest::waitForEvents();
62
return view->rootObject();
65
QQuickItem *testItem(QQuickItem *that, const QString &identifier)
67
if (that->property(identifier.toLocal8Bit()).isValid())
68
return that->property(identifier.toLocal8Bit()).value<QQuickItem*>();
70
QList<QQuickItem*> children = that->findChildren<QQuickItem*>(identifier);
71
return (children.count() > 0) ? children[0] : 0;
78
// Set test locale folder in the environment
79
// Using setenv because QProcessEnvironment ignores changes
80
QString testAppDir(QDir::currentPath() + "/ubuntu-ui-toolkit");
81
setenv("APP_DIR", testAppDir.toUtf8(), 1);
83
// Verify that we set it correctly
84
QVERIFY(QFileInfo(testAppDir + "/share/locale/en/LC_MESSAGES/ubuntu-ui-toolkit.mo").exists());
86
QString modules(UBUNTU_QML_IMPORT_PATH);
87
QVERIFY(QDir(modules).exists());
89
view = new QQuickView;
90
QQmlEngine *quickEngine = view->engine();
92
view->setGeometry(0,0, UCUnits::instance().gu(40), UCUnits::instance().gu(30));
93
//add modules folder so we have access to the plugin from QML
94
QStringList imports = quickEngine->importPathList();
95
imports.prepend(QDir(modules).absolutePath());
96
quickEngine->setImportPathList(imports);
99
void cleanupTestCase()
104
void testCase_RelativeTime()
106
UbuntuI18n* i18n = &UbuntuI18n::instance();
107
// By default no domain is set
108
QCOMPARE(i18n->domain(), QString(""));
110
// Start out with no localization
111
i18n->setLanguage("C");
112
// Load the app which should pick up the locale we prepared
113
QQuickItem *root = loadTest("src/RelativeTime.qml");
115
QQuickItem *mainView = root;
117
// There no translation happens
118
QQuickItem* page(testItem(mainView, "page"));
121
QQuickItem* timeFarAway(testItem(page, "timeFarAway"));
122
QVERIFY(timeFarAway);
123
QCOMPARE(timeFarAway->property("text").toString(),
124
QDateTime(QDate(2000,1,1), QTime(0,0,0,0)).toString("ddd d MMM'\u2003'HH:mm"));
125
QQuickItem* timeNow(testItem(page, "timeNow"));
127
QCOMPARE(timeNow->property("text").toString(), QString("Now"));
128
QQuickItem* timeMinuteBefore(testItem(page, "timeMinuteBefore"));
129
QVERIFY(timeMinuteBefore);
130
QCOMPARE(timeMinuteBefore->property("text").toString(), QString("1 minute ago"));
131
QQuickItem* timeMinuteAfter(testItem(page, "timeMinuteAfter"));
132
QVERIFY(timeMinuteAfter);
133
QCOMPARE(timeMinuteAfter->property("text").toString(), QString("1 minute"));
134
QQuickItem* tenMinutesBefore(testItem(page, "tenMinutesBefore"));
135
QVERIFY(tenMinutesBefore);
136
QCOMPARE(tenMinutesBefore->property("text").toString(), QString("10 minutes ago"));
137
QQuickItem* tenMinutesAfter(testItem(page, "tenMinutesAfter"));
138
QVERIFY(tenMinutesAfter);
139
QCOMPARE(tenMinutesAfter->property("text").toString(), QString("10 minutes"));
141
// There no translation happens in C++ either
142
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime()), QString("Now"));
143
QCOMPARE(i18n->relativeDateTime(QDateTime(QDate(2000,1,1), QTime(0,0,0,0))),
144
QDateTime(QDate(2000,1,1), QTime(0,0,0,0)).toString("ddd d MMM'\u2003'HH:mm"));
145
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-60)), QString("1 minute ago"));
146
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(60)), QString("1 minute"));
147
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-600)), QString("10 minutes ago"));
148
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(600)), QString("10 minutes"));
150
// Was the locale folder detected and set?
151
QString boundDomain(C::bindtextdomain(i18n->domain().toUtf8(), ((const char*)0)));
152
QString testAppDir(QDir::currentPath() + "/ubuntu-ui-toolkit");
153
QString expectedLocalePath(QDir(testAppDir).filePath("share/locale"));
154
QCOMPARE(boundDomain, expectedLocalePath);
155
// Is the domain gettext uses correct?
156
QString gettextDomain(C::textdomain(((const char*)0)));
157
QCOMPARE(gettextDomain, i18n->domain());
158
// Is the compiled en_US message catalog in the right location?
159
QString messageCatalog(boundDomain + "/en/LC_MESSAGES/ubuntu-ui-toolkit.mo");
160
QVERIFY(QFileInfo(messageCatalog).exists());
162
// Check if system has en_US locale, otherwise gettext won't work
164
localeA.start("locale -a");
165
QVERIFY(localeA.waitForFinished());
166
QVERIFY(QString(localeA.readAll()).split("\n").contains("en_US.utf8"));
168
i18n->setLanguage("en_US.utf8");
169
QSignalSpy spy(i18n, SIGNAL(languageChanged()));
172
// Inspect translated strings in QML
173
QCOMPARE(timeNow->property("text").toString(), QString("tr:Now"));
174
QCOMPARE(timeMinuteBefore->property("text").toString(), QString("tr:1 minute ago"));
175
QCOMPARE(timeMinuteAfter->property("text").toString(), QString("tr:1 minute"));
176
QCOMPARE(tenMinutesBefore->property("text").toString(), QString("tr:10 minutes ago"));
177
QCOMPARE(tenMinutesAfter->property("text").toString(), QString("tr:10 minutes"));
178
QCOMPARE(timeFarAway->property("text").toString(), QString("tr:FarAway"));
181
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime()), QString("tr:Now"));
182
QCOMPARE(i18n->relativeDateTime(QDateTime(QDate(2000,1,1), QTime(0,0,0,0))), QString("tr:FarAway"));
183
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-60)), QString("tr:1 minute ago"));
184
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(60)), QString("tr:1 minute"));
185
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(-600)), QString("tr:10 minutes ago"));
186
QCOMPARE(i18n->relativeDateTime(QDateTime::currentDateTime().addSecs(600)), QString("tr:10 minutes"));
190
// The C++ equivalent of QTEST_MAIN(tst_I18n_RelativeTime) with added initialization
191
int main(int argc, char *argv[])
193
// LC_ALL would fail the test case; it must be unset before execution
196
QGuiApplication app(argc, argv);
197
app.setAttribute(Qt::AA_Use96Dpi, true);
198
tst_I18n_RelativeTime* testObject = new tst_I18n_RelativeTime();
199
return QTest::qExec(static_cast<QObject*>(testObject), argc, argv);
202
#include "tst_i18n_RelativeTime.moc"