on daa5708 help sign in
unity8/tests/plugins/Utils/timeformattertest.cpp
165 lines 5.5 kB history raw
1/*
2 * Copyright 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: Lars Uebernickel <lars.uebernickel@canonical.com>
17 */
19#include "timeformatter.h"
20#include "relativetimeformatter.h"
22#include <locale.h>
24#include <QtTest>
25#include <QDebug>
27class TimeFormatterTest : public QObject
29 Q_OBJECT
30private:
31 void addTestData(QList<int> daysToAdd = QList<int>()) {
32 QTest::addColumn<QDateTime>("time");
34 Q_FOREACH(int dayToAdd, daysToAdd) {
35 QDateTime time = QDateTime::currentDateTime().addDays(dayToAdd);
36 QDateTime am(time); am.setTime(QTime(1, 1, 1));
37 QDateTime midday(time); midday.setTime(QTime(12, 0, 0));
38 QDateTime pm(time); pm.setTime(QTime(17, 59, 59));
40 QTest::newRow(QString("%1day-am").arg(dayToAdd).toLatin1()) << am;
41 QTest::newRow(QString("%1day-midday").arg(dayToAdd).toLatin1()) << midday;
42 QTest::newRow(QString("%1day-pm").arg(dayToAdd).toLatin1()) << pm;
43 }
45 }
47private Q_SLOTS:
48 void initTestCase()
49 {
50 setenv("UNITY_TEST_ENV", "1", 1);
51 }
53 void cleanupTestCase()
54 {
55 unsetenv("UNITY_TEST_ENV");
56 }
58 void testTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
59 void testTimeFormatter_Format()
60 {
61 QFETCH(QDateTime, time);
62 const QString format = "hh:mm dd.MM.yy";
64 TimeFormatter formatter;
65 formatter.setTime(time.toMSecsSinceEpoch() * 1000);
66 formatter.setFormat(format);
68 QCOMPARE(formatter.timeString(), time.toString(format));
69 }
71 void tesGDatetTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
72 void tesGDatetTimeFormatter_Format()
73 {
74 QFETCH(QDateTime, time);
75 const QString format = "%d-%m-%Y %I:%M%p";
77 GDateTimeFormatter formatter;
78 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
79 formatter.setFormat(format);
81 QCOMPARE(formatter.timeString(), time.toString("dd-MM-yyyy hh:mmAP"));
82 }
84 void tesRelativeTimeFormatter_FarBack_data() { addTestData(QList<int>() << -200 << -20 << -7); }
85 void tesRelativeTimeFormatter_FarBack()
86 {
87 QFETCH(QDateTime, time);
89 RelativeTimeFormatter formatter;
90 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
92 QCOMPARE(formatter.timeString(), time.toString("ddd dd MMM\u2003HH:mm"));
93 }
95 void tesRelativeTimeFormatter_WeekBack_data() { addTestData(QList<int>() << -6 << -2); }
96 void tesRelativeTimeFormatter_WeekBack()
97 {
98 QFETCH(QDateTime, time);
100 RelativeTimeFormatter formatter;
101 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
103 QCOMPARE(formatter.timeString(), time.toString("ddd\u2003HH:mm"));
104 }
106 void tesRelativeTimeFormatter_Yesterday_data() { addTestData(QList<int>() << -1); }
107 void tesRelativeTimeFormatter_Yesterday()
108 {
109 QFETCH(QDateTime, time);
111 RelativeTimeFormatter formatter;
112 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
114 QCOMPARE(formatter.timeString(), QString("Yesterday\u2003%1").arg(time.toString("HH:mm")));
115 }
117 void tesRelativeTimeFormatter_Today_data() { addTestData(QList<int>() << 0); }
118 void tesRelativeTimeFormatter_Today()
119 {
120 QFETCH(QDateTime, time);
122 RelativeTimeFormatter formatter;
123 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
125 QCOMPARE(formatter.timeString(), time.toString("HH:mm"));
126 }
128 void tesRelativeTimeFormatter_Tomorrow_data() { addTestData(QList<int>() << 1); }
129 void tesRelativeTimeFormatter_Tomorrow()
130 {
131 QFETCH(QDateTime, time);
133 RelativeTimeFormatter formatter;
134 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
136 QCOMPARE(formatter.timeString(), QString("Tomorrow\u2003%1").arg(time.toString("HH:mm")));
137 }
140 void tesRelativeTimeFormatter_WeekForward_data() { addTestData(QList<int>() << 2 << 6); }
141 void tesRelativeTimeFormatter_WeekForward()
142 {
143 QFETCH(QDateTime, time);
145 RelativeTimeFormatter formatter;
146 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
148 QCOMPARE(formatter.timeString(), time.toString("ddd\u2003HH:mm"));
149 }
151 void tesRelativeTimeFormatter_FarForward_data() { addTestData(QList<int>() << 7 << 20 << 200); }
152 void tesRelativeTimeFormatter_FarForward()
153 {
154 QFETCH(QDateTime, time);
156 RelativeTimeFormatter formatter;
157 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
159 QCOMPARE(formatter.timeString(), time.toString("ddd dd MMM\u2003HH:mm"));
160 }
161};
163QTEST_GUILESS_MAIN(TimeFormatterTest)
165#include "timeformattertest.moc"