2012-12-27 10:55:31 -08:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<!--
|
|
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=769370
|
|
|
|
-->
|
|
|
|
<head>
|
|
|
|
<title>Test for input.valueAsDate</title>
|
|
|
|
<script type="application/javascript" src="/MochiKit/packed.js"></script>
|
|
|
|
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=769370">Mozilla Bug 769370</a>
|
2013-04-02 17:19:24 -07:00
|
|
|
<iframe name="testFrame" style="display: none"></iframe>
|
2012-12-27 10:55:31 -08:00
|
|
|
<p id="display"></p>
|
|
|
|
<pre id="test">
|
|
|
|
<script type="application/javascript">
|
|
|
|
|
|
|
|
/** Test for Bug 769370**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This test is checking .valueAsDate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var element = document.createElement("input");
|
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
var validTypes =
|
|
|
|
[
|
|
|
|
["text", false],
|
|
|
|
["password", false],
|
|
|
|
["search", false],
|
2014-05-04 17:17:31 -07:00
|
|
|
["tel", false],
|
2013-01-21 08:44:42 -08:00
|
|
|
["email", false],
|
|
|
|
["url", false],
|
|
|
|
["hidden", false],
|
|
|
|
["checkbox", false],
|
|
|
|
["radio", false],
|
|
|
|
["file", false],
|
|
|
|
["submit", false],
|
|
|
|
["image", false],
|
|
|
|
["reset", false],
|
|
|
|
["button", false],
|
|
|
|
["number", false],
|
2013-02-16 04:36:02 -08:00
|
|
|
["range", false],
|
2013-01-21 08:44:42 -08:00
|
|
|
["date", true],
|
|
|
|
["time", true],
|
|
|
|
["color", false],
|
|
|
|
];
|
|
|
|
|
|
|
|
var todoTypes =
|
|
|
|
[
|
|
|
|
["datetime", true],
|
|
|
|
["month", true],
|
|
|
|
["week", true],
|
|
|
|
["datetime-local", true],
|
|
|
|
];
|
|
|
|
|
2012-12-27 10:55:31 -08:00
|
|
|
function checkAvailability()
|
|
|
|
{
|
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
for (data of validTypes) {
|
2012-12-27 10:55:31 -08:00
|
|
|
var exceptionCatched = false;
|
|
|
|
element.type = data[0];
|
|
|
|
try {
|
|
|
|
element.valueAsDate;
|
|
|
|
} catch (e) {
|
|
|
|
exceptionCatched = true;
|
|
|
|
}
|
|
|
|
is(exceptionCatched, false,
|
|
|
|
"valueAsDate shouldn't throw exception on getting");
|
|
|
|
|
|
|
|
exceptionCatched = false;
|
|
|
|
try {
|
|
|
|
element.valueAsDate = new Date();
|
|
|
|
} catch (e) {
|
|
|
|
exceptionCatched = true;
|
|
|
|
}
|
|
|
|
is(exceptionCatched, !data[1], "valueAsDate for " + data[0] +
|
|
|
|
" availability is not correct");
|
|
|
|
}
|
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
for (data of todoTypes) {
|
2012-12-27 10:55:31 -08:00
|
|
|
var exceptionCatched = false;
|
|
|
|
element.type = data[0];
|
|
|
|
try {
|
|
|
|
element.valueAsDate;
|
|
|
|
} catch (e) {
|
|
|
|
exceptionCatched = true;
|
|
|
|
}
|
|
|
|
is(exceptionCatched, false,
|
|
|
|
"valueAsDate shouldn't throw exception on getting");
|
|
|
|
|
|
|
|
exceptionCatched = false;
|
|
|
|
try {
|
|
|
|
element.valueAsDate= 42;
|
|
|
|
} catch (e) {
|
|
|
|
exceptionCatched = true;
|
|
|
|
}
|
|
|
|
todo_is(exceptionCatched, !data[1],
|
|
|
|
"valueAsDate for " + data[0] + " availability is not correct");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
function checkGarbageValues()
|
|
|
|
{
|
|
|
|
for (type of validTypes) {
|
|
|
|
if (!type[1]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
type = type[0];
|
|
|
|
|
|
|
|
var element = document.createElement('input');
|
|
|
|
element.type = type;
|
|
|
|
|
|
|
|
element.value = "test";
|
|
|
|
element.valueAsDate = null;
|
|
|
|
is(element.value, "", "valueAsDate should set the value to the empty string");
|
|
|
|
|
|
|
|
element.value = "test";
|
|
|
|
element.valueAsDate = undefined;
|
|
|
|
is(element.value, "", "valueAsDate should set the value to the empty string");
|
|
|
|
|
|
|
|
element.value = "test";
|
|
|
|
element.valueAsDate = new Date(NaN);
|
|
|
|
is(element.value, "", "valueAsDate should set the value to the empty string");
|
|
|
|
|
|
|
|
var illegalValues = [
|
|
|
|
"foobar", 42, {}, function() { return 42; }, function() { return Date(); }
|
|
|
|
];
|
|
|
|
|
|
|
|
for (value of illegalValues) {
|
|
|
|
try {
|
|
|
|
var caught = false;
|
|
|
|
element.valueAsDate = value;
|
|
|
|
} catch(e) {
|
2013-05-13 03:58:48 -07:00
|
|
|
is(e.name, "TypeError", "Exception should be 'TypeError'.");
|
2013-01-21 08:44:42 -08:00
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
ok(caught, "Assigning " + value + " to .valueAsDate should throw");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkDateGet()
|
2012-12-27 10:55:31 -08:00
|
|
|
{
|
|
|
|
var validData =
|
|
|
|
[
|
|
|
|
[ "2012-07-12", 1342051200000 ],
|
|
|
|
[ "1970-01-01", 0 ],
|
|
|
|
[ "1970-01-02", 86400000 ],
|
|
|
|
[ "1969-12-31", -86400000 ],
|
|
|
|
[ "0311-01-31", -52350451200000 ],
|
|
|
|
[ "275760-09-13", 8640000000000000 ],
|
|
|
|
[ "0001-01-01", -62135596800000 ],
|
|
|
|
[ "2012-02-29", 1330473600000 ],
|
|
|
|
[ "2011-02-28", 1298851200000 ],
|
|
|
|
];
|
|
|
|
|
|
|
|
var invalidData =
|
|
|
|
[
|
|
|
|
[ "invaliddate" ],
|
|
|
|
[ "-001-12-31" ],
|
|
|
|
[ "901-12-31" ],
|
|
|
|
[ "1901-13-31" ],
|
|
|
|
[ "1901-12-32" ],
|
|
|
|
[ "1901-00-12" ],
|
|
|
|
[ "1901-01-00" ],
|
|
|
|
[ "1900-02-29" ],
|
|
|
|
[ "0000-01-01" ],
|
|
|
|
[ "" ],
|
|
|
|
// This date is valid for the input element, but is out of
|
|
|
|
// the date object range. In this case, on getting valueAsDate,
|
|
|
|
// a Date object will be created, but it will have a NaN internal value,
|
|
|
|
// and will return the string "Invalid Date".
|
|
|
|
[ "275760-09-14", true ],
|
|
|
|
];
|
|
|
|
|
|
|
|
element.type = "date";
|
|
|
|
for (data of validData) {
|
|
|
|
element.value = data[0];
|
|
|
|
is(element.valueAsDate.valueOf(), data[1],
|
|
|
|
"valueAsDate should return the " +
|
|
|
|
"valid date object representing this date");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (data of invalidData) {
|
|
|
|
element.value = data[0];
|
|
|
|
is(element.valueAsDate, data[1] ? "Invalid Date" : null,
|
|
|
|
"valueAsDate should return null " +
|
|
|
|
"when the element value is not a valid date");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
function checkDateSet()
|
2012-12-27 10:55:31 -08:00
|
|
|
{
|
|
|
|
var testData =
|
|
|
|
[
|
|
|
|
[ 1342051200000, "2012-07-12" ],
|
|
|
|
[ 0, "1970-01-01" ],
|
|
|
|
// Maximum valid date (limited by the ecma date object range).
|
|
|
|
[ 8640000000000000, "275760-09-13" ],
|
|
|
|
// Minimum valid date (limited by the input element minimum valid value).
|
|
|
|
[ -62135596800000 , "0001-01-01" ],
|
|
|
|
[ 1330473600000, "2012-02-29" ],
|
|
|
|
[ 1298851200000, "2011-02-28" ],
|
|
|
|
// "Values must be truncated to valid dates"
|
|
|
|
[ 42.1234, "1970-01-01" ],
|
|
|
|
[ 123.123456789123, "1970-01-01" ],
|
|
|
|
[ 1e-1, "1970-01-01" ],
|
|
|
|
[ 1298851200010, "2011-02-28" ],
|
|
|
|
[ -1, "1969-12-31" ],
|
|
|
|
[ -86400000, "1969-12-31" ],
|
|
|
|
[ 86400000, "1970-01-02" ],
|
|
|
|
// Negative years, this is out of range for the input element,
|
|
|
|
// the corresponding date string is the empty string
|
|
|
|
[ -62135596800001, "" ],
|
|
|
|
// Invalid dates.
|
|
|
|
];
|
|
|
|
|
|
|
|
element.type = "date";
|
|
|
|
for (data of testData) {
|
|
|
|
element.valueAsDate = new Date(data[0]);
|
|
|
|
is(element.value, data[1], "valueAsDate should set the value to "
|
|
|
|
+ data[1]);
|
2013-04-02 17:19:24 -07:00
|
|
|
element.valueAsDate = new testFrame.Date(data[0]);
|
|
|
|
is(element.value, data[1], "valueAsDate with other-global date should " +
|
|
|
|
"set the value to " + data[1]);
|
2012-12-27 10:55:31 -08:00
|
|
|
}
|
2013-01-21 08:44:42 -08:00
|
|
|
}
|
2012-12-27 10:55:31 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
function checkTimeGet()
|
|
|
|
{
|
|
|
|
var tests = [
|
|
|
|
// Some invalid values to begin.
|
|
|
|
{ value: "", result: null },
|
|
|
|
{ value: "foobar", result: null },
|
|
|
|
{ value: "00:", result: null },
|
|
|
|
{ value: "24:00", result: null },
|
|
|
|
{ value: "00:99", result: null },
|
|
|
|
{ value: "00:00:", result: null },
|
|
|
|
{ value: "00:00:99", result: null },
|
|
|
|
{ value: "00:00:00:", result: null },
|
|
|
|
{ value: "00:00:00.", result: null },
|
|
|
|
{ value: "00:00:00.0000", result: null },
|
|
|
|
// Some simple valid values.
|
|
|
|
{ value: "00:00", result: { time: 0, hours: 0, minutes: 0, seconds: 0, ms: 0 } },
|
|
|
|
{ value: "00:01", result: { time: 60000, hours: 0, minutes: 1, seconds: 0, ms: 0 } },
|
|
|
|
{ value: "01:00", result: { time: 3600000, hours: 1, minutes: 0, seconds: 0, ms: 0 } },
|
|
|
|
{ value: "01:01", result: { time: 3660000, hours: 1, minutes: 1, seconds: 0, ms: 0 } },
|
|
|
|
{ value: "13:37", result: { time: 49020000, hours: 13, minutes: 37, seconds: 0, ms: 0 } },
|
|
|
|
// Valid values including seconds.
|
|
|
|
{ value: "00:00:01", result: { time: 1000, hours: 0, minutes: 0, seconds: 1, ms: 0 } },
|
|
|
|
{ value: "13:37:42", result: { time: 49062000, hours: 13, minutes: 37, seconds: 42, ms: 0 } },
|
|
|
|
// Valid values including seconds fractions.
|
|
|
|
{ value: "00:00:00.001", result: { time: 1, hours: 0, minutes: 0, seconds: 0, ms: 1 } },
|
|
|
|
{ value: "00:00:00.123", result: { time: 123, hours: 0, minutes: 0, seconds: 0, ms: 123 } },
|
|
|
|
{ value: "00:00:00.100", result: { time: 100, hours: 0, minutes: 0, seconds: 0, ms: 100 } },
|
|
|
|
{ value: "00:00:00.000", result: { time: 0, hours: 0, minutes: 0, seconds: 0, ms: 0 } },
|
|
|
|
{ value: "20:17:31.142", result: { time: 73051142, hours: 20, minutes: 17, seconds: 31, ms: 142 } },
|
|
|
|
// Highest possible value.
|
|
|
|
{ value: "23:59:59.999", result: { time: 86399999, hours: 23, minutes: 59, seconds: 59, ms: 999 } },
|
|
|
|
// Some values with one or two digits for the fraction of seconds.
|
|
|
|
{ value: "00:00:00.1", result: { time: 100, hours: 0, minutes: 0, seconds: 0, ms: 100 } },
|
|
|
|
{ value: "00:00:00.14", result: { time: 140, hours: 0, minutes: 0, seconds: 0, ms: 140 } },
|
|
|
|
{ value: "13:37:42.7", result: { time: 49062700, hours: 13, minutes: 37, seconds: 42, ms: 700 } },
|
|
|
|
{ value: "23:31:12.23", result: { time: 84672230, hours: 23, minutes: 31, seconds: 12, ms: 230 } },
|
|
|
|
];
|
2012-12-27 10:55:31 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
var element = document.createElement('input');
|
|
|
|
element.type = 'time';
|
|
|
|
|
|
|
|
for (test of tests) {
|
|
|
|
element.value = test.value;
|
|
|
|
if (test.result === null) {
|
|
|
|
is(element.valueAsDate, null, "element.valueAsDate should return null");
|
|
|
|
} else {
|
|
|
|
var date = element.valueAsDate;
|
|
|
|
isnot(date, null, "element.valueAsDate should not be null");
|
|
|
|
|
|
|
|
is(date.getTime(), test.result.time);
|
|
|
|
is(date.getUTCHours(), test.result.hours);
|
|
|
|
is(date.getUTCMinutes(), test.result.minutes);
|
|
|
|
is(date.getUTCSeconds(), test.result.seconds);
|
|
|
|
is(date.getUTCMilliseconds(), test.result.ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-07 07:20:18 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
function checkTimeSet()
|
|
|
|
{
|
|
|
|
var tests = [
|
|
|
|
// Simple tests.
|
|
|
|
{ value: 0, result: "00:00" },
|
|
|
|
{ value: 1, result: "00:00:00.001" },
|
|
|
|
{ value: 100, result: "00:00:00.100" },
|
|
|
|
{ value: 1000, result: "00:00:01" },
|
|
|
|
{ value: 60000, result: "00:01" },
|
|
|
|
{ value: 3600000, result: "01:00" },
|
|
|
|
{ value: 83622234, result: "23:13:42.234" },
|
|
|
|
// Some edge cases.
|
|
|
|
{ value: 86400000, result: "00:00" },
|
|
|
|
{ value: 86400001, result: "00:00:00.001" },
|
|
|
|
{ value: 170022234, result: "23:13:42.234" },
|
|
|
|
{ value: 432000000, result: "00:00" },
|
|
|
|
{ value: -1, result: "23:59:59.999" },
|
|
|
|
{ value: -86400000, result: "00:00" },
|
|
|
|
{ value: -86400001, result: "23:59:59.999" },
|
|
|
|
{ value: -56789, result: "23:59:03.211" },
|
|
|
|
{ value: 0.9, result: "00:00" },
|
2013-01-07 07:20:18 -08:00
|
|
|
];
|
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
var element = document.createElement('input');
|
|
|
|
element.type = 'time';
|
|
|
|
|
|
|
|
for (test of tests) {
|
|
|
|
element.valueAsDate = new Date(test.value);
|
|
|
|
is(element.value, test.result,
|
|
|
|
"element.value should have been changed by setting valueAsDate");
|
2013-01-07 07:20:18 -08:00
|
|
|
}
|
2012-12-27 10:55:31 -08:00
|
|
|
}
|
|
|
|
|
2013-01-07 07:20:53 -08:00
|
|
|
function checkWithBustedPrototype()
|
|
|
|
{
|
2013-01-21 08:44:42 -08:00
|
|
|
for (type of validTypes) {
|
|
|
|
if (!type[1]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
type = type[0];
|
|
|
|
|
|
|
|
var element = document.createElement('input');
|
|
|
|
element.type = type;
|
|
|
|
|
|
|
|
var backupPrototype = {};
|
|
|
|
backupPrototype.getUTCFullYear = Date.prototype.getUTCFullYear;
|
|
|
|
backupPrototype.getUTCMonth = Date.prototype.getUTCMonth;
|
|
|
|
backupPrototype.getUTCDate = Date.prototype.getUTCDate;
|
|
|
|
backupPrototype.getTime = Date.prototype.getTime;
|
|
|
|
backupPrototype.setUTCFullYear = Date.prototype.setUTCFullYear;
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
Date.prototype.getUTCFullYear = function() { return {}; };
|
|
|
|
Date.prototype.getUTCMonth = function() { return {}; };
|
|
|
|
Date.prototype.getUTCDate = function() { return {}; };
|
|
|
|
Date.prototype.getTime = function() { return {}; };
|
|
|
|
Date.prototype.setUTCFullYear = function(y,m,d) { };
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
element.valueAsDate = new Date();
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-05-02 13:33:17 -07:00
|
|
|
isnot(element.valueAsDate, null, ".valueAsDate should not return null");
|
2013-05-13 03:58:48 -07:00
|
|
|
// The object returned by element.valueAsDate should return a Date object
|
|
|
|
// with the same prototype:
|
|
|
|
is(element.valueAsDate.getUTCFullYear, Date.prototype.getUTCFullYear,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.getUTCMonth, Date.prototype.getUTCMonth,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.getUTCDate, Date.prototype.getUTCDate,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.getTime, Date.prototype.getTime,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.setUTCFullYear, Date.prototype.setUTCFullYear,
|
|
|
|
"prototype is the same");
|
|
|
|
|
|
|
|
// However the Date should have the correct information.
|
|
|
|
var witnessDate = new Date(element.valueAsNumber);
|
|
|
|
is(element.valueAsDate.valueOf(), witnessDate.valueOf(), "correct Date");
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
// Same test as above but using NaN instead of {}.
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
Date.prototype.getUTCFullYear = function() { return NaN; };
|
|
|
|
Date.prototype.getUTCMonth = function() { return NaN; };
|
|
|
|
Date.prototype.getUTCDate = function() { return NaN; };
|
|
|
|
Date.prototype.getTime = function() { return NaN; };
|
|
|
|
Date.prototype.setUTCFullYear = function(y,m,d) { };
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
element.valueAsDate = new Date();
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-05-02 13:33:17 -07:00
|
|
|
isnot(element.valueAsDate, null, ".valueAsDate should not return null");
|
2013-05-13 03:58:48 -07:00
|
|
|
// The object returned by element.valueAsDate should return a Date object
|
|
|
|
// with the same prototype:
|
|
|
|
is(element.valueAsDate.getUTCFullYear, Date.prototype.getUTCFullYear,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.getUTCMonth, Date.prototype.getUTCMonth,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.getUTCDate, Date.prototype.getUTCDate,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.getTime, Date.prototype.getTime,
|
|
|
|
"prototype is the same");
|
|
|
|
is(element.valueAsDate.setUTCFullYear, Date.prototype.setUTCFullYear,
|
|
|
|
"prototype is the same");
|
|
|
|
|
|
|
|
// However the Date should have the correct information.
|
|
|
|
var witnessDate = new Date(element.valueAsNumber);
|
|
|
|
is(element.valueAsDate.valueOf(), witnessDate.valueOf(), "correct Date");
|
2013-01-07 07:20:53 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
Date.prototype.getUTCFullYear = backupPrototype.getUTCFullYear;
|
|
|
|
Date.prototype.getUTCMonth = backupPrototype.getUTCMonth;
|
|
|
|
Date.prototype.getUTCDate = backupPrototype.getUTCDate;
|
|
|
|
Date.prototype.getTime = backupPrototype.getTime;
|
|
|
|
Date.prototype.setUTCFullYear = backupPrototype.setUTCFullYear;
|
|
|
|
}
|
2013-01-07 07:20:53 -08:00
|
|
|
}
|
|
|
|
|
2012-12-27 10:55:31 -08:00
|
|
|
checkAvailability();
|
2013-01-21 08:44:42 -08:00
|
|
|
checkGarbageValues();
|
2013-01-07 07:20:53 -08:00
|
|
|
checkWithBustedPrototype();
|
2012-12-27 10:55:31 -08:00
|
|
|
|
2013-01-21 08:44:42 -08:00
|
|
|
// Test <input type='date'>.
|
|
|
|
checkDateGet();
|
|
|
|
checkDateSet();
|
|
|
|
|
|
|
|
// Test <input type='time'>.
|
|
|
|
checkTimeGet();
|
|
|
|
checkTimeSet();
|
|
|
|
|
2012-12-27 10:55:31 -08:00
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|