~ubuntu-sdk-team/ubuntu-ui-toolkit/noDietForPopovers

« back to all changes in this revision

Viewing changes to tests/unit/tst_i18n/tst_i18n_RelativeTime/src/tst_i18n_RelativeTime.cpp

  • Committer: Tarmac
  • Author(s): nick.dedekind at canonical, Nick Dedekind
  • Date: 2015-08-12 11:32:51 UTC
  • mfrom: (1602.1.9 LiveTimer)
  • Revision ID: tarmac-20150812113251-jidi067kc9dl3uxv
Added relative date time i18n.

Approved by PS Jenkins bot, Zsombor Egri.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Copyright 2015 Canonical Ltd.
 
4
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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/>.
 
16
 *
 
17
 * Author: Nick Dedekind <nick.dedekind@gmail.com>
 
18
 */
 
19
 
 
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>
 
35
 
 
36
namespace C {
 
37
#include <libintl.h>
 
38
}
 
39
 
 
40
#include "ucunits.h"
 
41
#include "i18n.h"
 
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
        QTest::waitForEvents();
 
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() + "/ubuntu-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/ubuntu-ui-toolkit.mo").exists());
 
85
 
 
86
        QString modules(UBUNTU_QML_IMPORT_PATH);
 
87
        QVERIFY(QDir(modules).exists());
 
88
 
 
89
        view = new QQuickView;
 
90
        QQmlEngine *quickEngine = view->engine();
 
91
 
 
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);
 
97
    }
 
98
 
 
99
    void cleanupTestCase()
 
100
    {
 
101
        delete view;
 
102
    }
 
103
 
 
104
    void testCase_RelativeTime()
 
105
    {
 
106
        UbuntuI18n* i18n = &UbuntuI18n::instance();
 
107
        // By default no domain is set
 
108
        QCOMPARE(i18n->domain(), QString(""));
 
109
 
 
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");
 
114
        QVERIFY(root);
 
115
        QQuickItem *mainView = root;
 
116
 
 
117
        // There no translation happens
 
118
        QQuickItem* page(testItem(mainView, "page"));
 
119
        QVERIFY(page);
 
120
 
 
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"));
 
126
        QVERIFY(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"));
 
140
 
 
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"));
 
149
 
 
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());
 
161
 
 
162
        // Check if system has en_US locale, otherwise gettext won't work
 
163
        QProcess localeA;
 
164
        localeA.start("locale -a");
 
165
        QVERIFY(localeA.waitForFinished());
 
166
        QVERIFY(QString(localeA.readAll()).split("\n").contains("en_US.utf8"));
 
167
 
 
168
        i18n->setLanguage("en_US.utf8");
 
169
        QSignalSpy spy(i18n, SIGNAL(languageChanged()));
 
170
        spy.wait();
 
171
 
 
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"));
 
179
 
 
180
        // Translate in C++
 
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"));
 
187
    }
 
188
};
 
189
 
 
190
// The C++ equivalent of QTEST_MAIN(tst_I18n_RelativeTime) with added initialization
 
191
int main(int argc, char *argv[])
 
192
{
 
193
    // LC_ALL would fail the test case; it must be unset before execution
 
194
    unsetenv("LC_ALL");
 
195
 
 
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);
 
200
}
 
201
 
 
202
#include "tst_i18n_RelativeTime.moc"