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 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 General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
import "../../../Components/Time.js" as TimeLocal
24
property var readableDate
26
function test_readableFromNow_dateUndefined() {
27
readableDate = TimeLocal.readableFromNow()
28
compare(readableDate, "", "readable date should have been null")
31
function test_readableFromNow_dateNaN() {
32
readableDate = TimeLocal.readableFromNow("this is a string, not a date")
33
compare(readableDate, "", "readable date should have been null")
36
function test_readableFromNow_justNow() {
37
readableDate = TimeLocal.readableFromNow(new Date())
38
compare(readableDate, "just now", "readable date should have been now")
41
function test_readableFromNow_nowNaN() {
44
TimeLocal.readableFromNow(new Date(), "this is a string, not a date")
48
compare(fail, true, "readable date should have thrown an exception")
52
function cycleTime(diff, previous, next, units, endIterator) {
54
var time = now.getTime()
55
readableDate = TimeLocal.readableFromNow(time - diff, now)
56
compare(readableDate, previous, "different time predicted")
57
readableDate = TimeLocal.readableFromNow(time + diff, now)
58
compare(readableDate, next, "different time predicted")
59
for (var i = 2; i < endIterator; i++) {
60
var tmpDiff = i * diff
61
readableDate = TimeLocal.readableFromNow(time - tmpDiff, now)
62
compare(readableDate, i + " " + units + " ago", "different time predicted")
63
readableDate = TimeLocal.readableFromNow(time + tmpDiff, now)
64
compare(readableDate, "in " + i + " " + units, "different time predicted")
68
function test_readableFromNow_seconds() {
69
cycleTime(1000, "a second ago", "in a second", "seconds", 60)
72
function test_readableFromNow_minutes() {
73
cycleTime(1000 * 60, "a minute ago", "in a minute", "minutes", 60)
76
function test_readableFromNow_hours() {
77
cycleTime(1000 * 60 * 60, "an hour ago", "in an hour", "hours", 24)
80
function test_readableFromNow_days() {
81
cycleTime(1000 * 60 * 60 * 24, "yesterday", "tomorrow", "days", 7)
84
function test_readableFromNow_weeks() {
86
for (var i = 7; i < 30; i++) {
87
readableDate = TimeLocal.readableFromNow(now.getTime() - i * 1000 * 60 * 60 * 24, now)
89
compare(readableDate, "a week ago", "different time predicted")
91
compare(readableDate, "about 2 weeks ago", "different time predicted")
93
compare(readableDate, "about 3 weeks ago", "different time predicted")
95
compare(readableDate, "about 4 weeks ago", "different time predicted")
97
for (var i = 7; i < 30; i++) {
98
readableDate = TimeLocal.readableFromNow(now.getTime() + i * 1000 * 60 * 60 * 24, now)
100
compare(readableDate, "in a week", "different time predicted")
102
compare(readableDate, "in about 2 weeks", "different time predicted")
104
compare(readableDate, "in about 3 weeks", "different time predicted")
106
compare(readableDate, "in about 4 weeks", "different time predicted")
110
function test_readableFromNow_months() {
111
cycleTime(1000 * 60 * 60 * 24 * 30.45, "a month ago", "in a month", "months", 12)
114
function test_readableFromNow_years() {
115
cycleTime(1000 * 60 * 60 * 24 * 366, "a year ago", "in a year", "years", 5)