2 * Copyright 2013 Canonical Ltd.
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.
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.
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/>.
16 * Author: Lars Uebernickel <lars.uebernickel@canonical.com>
19#include "timeformatter.h"
20#include "relativetimeformatter.h"
27class TimeFormatterTest : public QObject
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;
48 void testTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
49 void testTimeFormatter_Format()
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));
61 void tesGDatetTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
62 void tesGDatetTimeFormatter_Format()
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"));
74 void tesRelativeTimeFormatter_FarBack_data() { addTestData(QList<int>() << -200 << -20 << -7); }
75 void tesRelativeTimeFormatter_FarBack()
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"));
85 void tesRelativeTimeFormatter_WeekBack_data() { addTestData(QList<int>() << -6 << -2); }
86 void tesRelativeTimeFormatter_WeekBack()
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"));
96 void tesRelativeTimeFormatter_Yesterday_data() { addTestData(QList<int>() << -1); }
97 void tesRelativeTimeFormatter_Yesterday()
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")));
107 void tesRelativeTimeFormatter_Today_data() { addTestData(QList<int>() << 0); }
108 void tesRelativeTimeFormatter_Today()
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"));
118 void tesRelativeTimeFormatter_Tomorrow_data() { addTestData(QList<int>() << 1); }
119 void tesRelativeTimeFormatter_Tomorrow()
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")));
130 void tesRelativeTimeFormatter_WeekForward_data() { addTestData(QList<int>() << 2 << 6); }
131 void tesRelativeTimeFormatter_WeekForward()
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"));
141 void tesRelativeTimeFormatter_FarForward_data() { addTestData(QList<int>() << 7 << 20 << 200); }
142 void tesRelativeTimeFormatter_FarForward()
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"));
153QTEST_GUILESS_MAIN(TimeFormatterTest)
155#include "TimeFormatterTest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list