<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="/tests/SimpleTest/test.css" type="text/css"?>
<!--
  XUL Widget Test for timepicker
  -->
<window title="timepicker" width="500" height="600"
        onload="setTimeout(testtag_timepicker, 0);"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>  
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>  

<timepicker id="timepicker"/>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

<script>
<![CDATA[

SimpleTest.waitForExplicitFinish();

function testtag_timepicker()
{
  var tp = document.getElementById("timepicker");
  
  var testid = "timepicker ";

  var today = new Date();
  var thour = today.getHours();
  var tminute = today.getMinutes();
  var tsecond = today.getSeconds();

  // testtag_comparetime(tp, testid + "initial", thour, tminute, tsecond);

  // check that setting the value property works
  tp.value = testtag_gettimestring(thour, tminute, tsecond);
  testtag_comparetime(tp, testid + "set value", thour, tminute, tsecond);

  // check that setting the dateValue property works
  tp.dateValue = today;
  testtag_comparetime(tp, testid + "set dateValue", thour, tminute, tsecond);
  ok(tp.value !== today, testid + " set dateValue different time");

  ok(!tp.readOnly, testid + "readOnly");
  tp.readOnly = true;
  ok(tp.readOnly, testid + "set readOnly");
  tp.readOnly = false;
  ok(!tp.readOnly, testid + "clear readOnly");

  function setTimeField(field, value, expectException,
                        expectedHour, expectedMinute, expectedSecond)
  {
    var exh = false;
    try {
      tp[field] = value;
    } catch (ex) { exh = true; }
    is(exh, expectException, testid + "set " + field + " " + value);
    testtag_comparetime(tp, testid + "set " + field + " " + value,
                        expectedHour, expectedMinute, expectedSecond);
  }

  // check the value property
  setTimeField("value", "0:0:0", false, 0, 0, 0);
  setTimeField("value", "21:1:40", false, 21, 1, 40);
  setTimeField("value", "7:11:8", false, 7, 11, 8);
  setTimeField("value", "04:07:02", false, 4, 7, 2);
  setTimeField("value", "10:42:20", false, 10, 42, 20);

  // check that the hour, minute and second fields can be set properly
  setTimeField("hour", 7, false, 7, 42, 20);
  setTimeField("hour", 0, false, 0, 42, 20);
  setTimeField("hour", 21, false, 21, 42, 20);
  setTimeField("hour", -1, true, 21, 42, 20);
  setTimeField("hour", 24, true, 21, 42, 20);

  setTimeField("minute", 0, false, 21, 0, 20);
  setTimeField("minute", 9, false, 21, 9, 20);
  setTimeField("minute", 10, false, 21, 10, 20);
  setTimeField("minute", 35, false, 21, 35, 20);
  setTimeField("minute", -1, true, 21, 35, 20);
  setTimeField("minute", 60, true, 21, 35, 20);

  setTimeField("second", 0, false, 21, 35, 0);
  setTimeField("second", 9, false, 21, 35, 9);
  setTimeField("second", 10, false, 21, 35, 10);
  setTimeField("second", 51, false, 21, 35, 51);
  setTimeField("second", -1, true, 21, 35, 51);
  setTimeField("second", 60, true, 21, 35, 51);

  // check when seconds is not specified
  setTimeField("value", "06:05", false, 6, 5, 0);
  setTimeField("value", "06:15", false, 6, 15, 0);
  setTimeField("value", "16:15", false, 16, 15, 0);

  // check that times overflow properly
  setTimeField("value", "5:65:21", false, 6, 5, 21);
  setTimeField("value", "5:25:72", false, 5, 26, 12);

  // check invalid values for the value and dateValue properties
  tp.value = "14:25:48";
  setTimeField("value", "", true, 14, 25, 48);
  setTimeField("value", "1:5:6:6", true, 14, 25, 48);
  setTimeField("value", "2:a:19", true, 14, 25, 48);
  setTimeField("dateValue", "none", true, 14, 25, 48);

  // check the fields
  ok(tp.hourField instanceof HTMLInputElement, testid + "hourField");
  ok(tp.minuteField instanceof HTMLInputElement, testid + "minuteField");
  ok(tp.secondField instanceof HTMLInputElement, testid + "secondField");

  testtag_timepicker_UI(tp, testid);

  tp.readOnly = true;

  // check that keyboard usage doesn't change the value when the timepicker
  // is read only
  testtag_timepicker_UI_key(tp, testid + "readonly ", "14:25:48",
                            tp.hourField, 14, 25, 48, 14, 25, 48);
  testtag_timepicker_UI_key(tp, testid + "readonly ", "14:25:48",
                            tp.minuteField, 14, 25, 48, 14, 25, 48);
  testtag_timepicker_UI_key(tp, testid + "readonly ", "14:25:48",
                            tp.secondField, 14, 25, 48, 14, 25, 48);

  SimpleTest.finish();
}

function testtag_timepicker_UI(tp, testid)
{
  testid += "UI";

  // test adjusting the time with the up and down keys
  testtag_timepicker_UI_key(tp, testid, "0:12:25", tp.hourField, 1, 12, 25, 0, 12, 25);
  testtag_timepicker_UI_key(tp, testid, "11:12:25", tp.hourField, 12, 12, 25, 11, 12, 25);
  testtag_timepicker_UI_key(tp, testid, "7:12:25", tp.hourField, 8, 12, 25, 7, 12, 25);
  testtag_timepicker_UI_key(tp, testid, "16:12:25", tp.hourField, 17, 12, 25, 16, 12, 25);
  testtag_timepicker_UI_key(tp, testid, "23:12:25", tp.hourField, 0, 12, 25, 23, 12, 25);

  testtag_timepicker_UI_key(tp, testid, "15:23:46", tp.minuteField, 15, 24, 46, 15, 23, 46);
  testtag_timepicker_UI_key(tp, testid, "15:0:46", tp.minuteField, 15, 1, 46, 15, 0, 46);
  testtag_timepicker_UI_key(tp, testid, "15:59:46", tp.minuteField, 15, 0, 46, 15, 59, 46);

  testtag_timepicker_UI_key(tp, testid, "11:50:46", tp.secondField, 11, 50, 47, 11, 50, 46);
  testtag_timepicker_UI_key(tp, testid, "11:50:0", tp.secondField, 11, 50, 1, 11, 50, 0);
  testtag_timepicker_UI_key(tp, testid, "11:50:59", tp.secondField, 11, 50, 0, 11, 50, 59);
}

function testtag_timepicker_UI_key(tp, testid, value, field,
                                   uhour, uminute, usecond,
                                   dhour, dminute, dsecond)
{
  tp.value = value;
  field.focus();

  var eventTarget = tp.readOnly ? null : tp;

  var testname = testid + " " + value + " key up";
  synthesizeKeyExpectEvent("VK_UP", { }, eventTarget, "change", testname);
  testtag_comparetime(tp, testname, uhour, uminute, usecond);

  testname = testid + " " + value + " key down";
  synthesizeKeyExpectEvent("VK_DOWN", { }, eventTarget, "change", testname);
  testtag_comparetime(tp, testname, dhour, dminute, dsecond);
}

function testtag_gettimestring(hour, minute, second)
{
  if (minute < 10)
    minute = "0" + minute;
  if (second < 10)
    second = "0" + second;
  return hour + ":" + minute + ":" + second;
}

function testtag_comparetime(tp, testid, hour, minute, second)
{
  is(tp.value, testtag_gettimestring(hour, minute, second), testid + " value");
  is(tp.getAttribute("value"),
                testtag_gettimestring(hour, minute, second),
                testid + " value attribute");

  var dateValue = tp.dateValue;
  ok(dateValue.getHours() == hour &&
                dateValue.getMinutes() == minute &&
                dateValue.getSeconds() == second,
                testid + " dateValue");

  is(tp.hour, hour, testid + " hour");
  is(tp.minute, minute, testid + " minute");
  is(tp.second, second, testid + " second");
}

]]>

</script>

</window>
