on 147eeee help sign in
unity8/tests/plugins/Utils/TimeFormatterTest.cpp
155 lines 5.4 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 testTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
49 void testTimeFormatter_Format()
50 {
51 QFETCH(QDateTime, time);
52 const QString format = "hh:mm dd.MM.yy";
54 TimeFormatter formatter;
55 formatter.setTime(time.toMSecsSinceEpoch() * 1000);
56 formatter.setFormat(format);
58 QCOMPARE(formatter.timeString(), time.toString(format));
59 }
61 void tesGDatetTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
62 void tesGDatetTimeFormatter_Format()
63 {
64 QFETCH(QDateTime, time);
65 const QString format = "%d-%m-%Y %I:%M%p";
67 GDateTimeFormatter formatter;
68 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
69 formatter.setFormat(format);
71 QCOMPARE(formatter.timeString(), time.toString("dd-MM-yyyy hh:mmAP"));
72 }
74 void tesRelativeTimeFormatter_FarBack_data() { addTestData(QList<int>() << -200 << -20 << -7); }
75 void tesRelativeTimeFormatter_FarBack()
76 {
77 QFETCH(QDateTime, time);
79 RelativeTimeFormatter formatter;
80 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
82 QCOMPARE(formatter.timeString(), time.toString("ddd dd MMM\u2003HH:mm"));
83 }
85 void tesRelativeTimeFormatter_WeekBack_data() { addTestData(QList<int>() << -6 << -2); }
86 void tesRelativeTimeFormatter_WeekBack()
87 {
88 QFETCH(QDateTime, time);
90 RelativeTimeFormatter formatter;
91 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
93 QCOMPARE(formatter.timeString(), time.toString("ddd\u2003HH:mm"));
94 }
96 void tesRelativeTimeFormatter_Yesterday_data() { addTestData(QList<int>() << -1); }
97 void tesRelativeTimeFormatter_Yesterday()
98 {
99 QFETCH(QDateTime, time);
101 RelativeTimeFormatter formatter;
102 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
104 QCOMPARE(formatter.timeString(), QString("Yesterday\u2003%1").arg(time.toString("HH:mm")));
105 }
107 void tesRelativeTimeFormatter_Today_data() { addTestData(QList<int>() << 0); }
108 void tesRelativeTimeFormatter_Today()
109 {
110 QFETCH(QDateTime, time);
112 RelativeTimeFormatter formatter;
113 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
115 QCOMPARE(formatter.timeString(), time.toString("HH:mm"));
116 }
118 void tesRelativeTimeFormatter_Tomorrow_data() { addTestData(QList<int>() << 1); }
119 void tesRelativeTimeFormatter_Tomorrow()
120 {
121 QFETCH(QDateTime, time);
123 RelativeTimeFormatter formatter;
124 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
126 QCOMPARE(formatter.timeString(), QString("Tomorrow\u2003%1").arg(time.toString("HH:mm")));
127 }
130 void tesRelativeTimeFormatter_WeekForward_data() { addTestData(QList<int>() << 2 << 6); }
131 void tesRelativeTimeFormatter_WeekForward()
132 {
133 QFETCH(QDateTime, time);
135 RelativeTimeFormatter formatter;
136 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
138 QCOMPARE(formatter.timeString(), time.toString("ddd\u2003HH:mm"));
139 }
141 void tesRelativeTimeFormatter_FarForward_data() { addTestData(QList<int>() << 7 << 20 << 200); }
142 void tesRelativeTimeFormatter_FarForward()
143 {
144 QFETCH(QDateTime, time);
146 RelativeTimeFormatter formatter;
147 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
149 QCOMPARE(formatter.timeString(), time.toString("ddd dd MMM\u2003HH:mm"));
150 }
151};
153QTEST_GUILESS_MAIN(TimeFormatterTest)
155#include "TimeFormatterTest.moc"