PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/toolkit/content/tests/chrome/test_timepicker.xul

http://github.com/zpao/v8monkey
Unknown | 206 lines | 166 code | 40 blank | 0 comment | 0 complexity | 61d877bfc08ed571cc31856342588607 MD5 | raw file
  1<?xml version="1.0"?>
  2<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
  3<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
  4<!--
  5  XUL Widget Test for timepicker
  6  -->
  7<window title="timepicker" width="500" height="600"
  8        onload="setTimeout(testtag_timepicker, 0);"
  9        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
 10  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>  
 11  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>  
 12
 13<timepicker id="timepicker"/>
 14
 15<body xmlns="http://www.w3.org/1999/xhtml">
 16<p id="display"></p>
 17<div id="content" style="display: none">
 18</div>
 19<pre id="test">
 20</pre>
 21</body>
 22
 23<script>
 24<![CDATA[
 25
 26SimpleTest.waitForExplicitFinish();
 27
 28function testtag_timepicker()
 29{
 30  var tp = document.getElementById("timepicker");
 31  
 32  var testid = "timepicker ";
 33
 34  var today = new Date();
 35  var thour = today.getHours();
 36  var tminute = today.getMinutes();
 37  var tsecond = today.getSeconds();
 38
 39  // testtag_comparetime(tp, testid + "initial", thour, tminute, tsecond);
 40
 41  // check that setting the value property works
 42  tp.value = testtag_gettimestring(thour, tminute, tsecond);
 43  testtag_comparetime(tp, testid + "set value", thour, tminute, tsecond);
 44
 45  var numberOrder = /^(\D*)\s*(\d+)(\D*)(\d+)(\D*)(\d+)\s*(\D*)$/;
 46  var fdt = new Date(2000,0,1,16,7,9).toLocaleFormat("%X");
 47  is(tp.is24HourClock, Number(fdt.match(numberOrder)[2]) > 12, "is24HourClock");
 48
 49  // check that setting the dateValue property works
 50  tp.dateValue = today;
 51  testtag_comparetime(tp, testid + "set dateValue", thour, tminute, tsecond);
 52  ok(tp.value !== today, testid + " set dateValue different time");
 53
 54  ok(!tp.readOnly, testid + "readOnly");
 55  tp.readOnly = true;
 56  ok(tp.readOnly, testid + "set readOnly");
 57  tp.readOnly = false;
 58  ok(!tp.readOnly, testid + "clear readOnly");
 59
 60  function setTimeField(field, value, expectException,
 61                        expectedHour, expectedMinute, expectedSecond)
 62  {
 63    var exh = false;
 64    try {
 65      tp[field] = value;
 66    } catch (ex) { exh = true; }
 67    is(exh, expectException, testid + "set " + field + " " + value);
 68    testtag_comparetime(tp, testid + "set " + field + " " + value,
 69                        expectedHour, expectedMinute, expectedSecond);
 70  }
 71
 72  // check the value property
 73  setTimeField("value", "0:0:0", false, 0, 0, 0);
 74  setTimeField("value", "21:1:40", false, 21, 1, 40);
 75  setTimeField("value", "7:11:8", false, 7, 11, 8);
 76  setTimeField("value", "04:07:02", false, 4, 7, 2);
 77  setTimeField("value", "10:42:20", false, 10, 42, 20);
 78
 79  // check that the hour, minute and second fields can be set properly
 80  setTimeField("hour", 7, false, 7, 42, 20);
 81  setTimeField("hour", 0, false, 0, 42, 20);
 82  setTimeField("hour", 21, false, 21, 42, 20);
 83  setTimeField("hour", -1, true, 21, 42, 20);
 84  setTimeField("hour", 24, true, 21, 42, 20);
 85
 86  setTimeField("minute", 0, false, 21, 0, 20);
 87  setTimeField("minute", 9, false, 21, 9, 20);
 88  setTimeField("minute", 10, false, 21, 10, 20);
 89  setTimeField("minute", 35, false, 21, 35, 20);
 90  setTimeField("minute", -1, true, 21, 35, 20);
 91  setTimeField("minute", 60, true, 21, 35, 20);
 92
 93  setTimeField("second", 0, false, 21, 35, 0);
 94  setTimeField("second", 9, false, 21, 35, 9);
 95  setTimeField("second", 10, false, 21, 35, 10);
 96  setTimeField("second", 51, false, 21, 35, 51);
 97  setTimeField("second", -1, true, 21, 35, 51);
 98  setTimeField("second", 60, true, 21, 35, 51);
 99
100  // check when seconds is not specified
101  setTimeField("value", "06:05", false, 6, 5, 0);
102  setTimeField("value", "06:15", false, 6, 15, 0);
103  setTimeField("value", "16:15", false, 16, 15, 0);
104
105  // check that times overflow properly
106  setTimeField("value", "5:65:21", false, 6, 5, 21);
107  setTimeField("value", "5:25:72", false, 5, 26, 12);
108
109  // check invalid values for the value and dateValue properties
110  tp.value = "14:25:48";
111  setTimeField("value", "", true, 14, 25, 48);
112  setTimeField("value", "1:5:6:6", true, 14, 25, 48);
113  setTimeField("value", "2:a:19", true, 14, 25, 48);
114  setTimeField("dateValue", "none", true, 14, 25, 48);
115
116  // check the fields
117  ok(tp.hourField instanceof HTMLInputElement, testid + "hourField");
118  ok(tp.minuteField instanceof HTMLInputElement, testid + "minuteField");
119  ok(tp.secondField instanceof HTMLInputElement, testid + "secondField");
120
121  testtag_timepicker_UI(tp, testid);
122
123  tp.readOnly = true;
124
125  // check that keyboard usage doesn't change the value when the timepicker
126  // is read only
127  testtag_timepicker_UI_key(tp, testid + "readonly ", "14:25:48",
128                            tp.hourField, 14, 25, 48, 14, 25, 48);
129  testtag_timepicker_UI_key(tp, testid + "readonly ", "14:25:48",
130                            tp.minuteField, 14, 25, 48, 14, 25, 48);
131  testtag_timepicker_UI_key(tp, testid + "readonly ", "14:25:48",
132                            tp.secondField, 14, 25, 48, 14, 25, 48);
133
134  SimpleTest.finish();
135}
136
137function testtag_timepicker_UI(tp, testid)
138{
139  testid += "UI";
140
141  // test adjusting the time with the up and down keys
142  testtag_timepicker_UI_key(tp, testid, "0:12:25", tp.hourField, 1, 12, 25, 0, 12, 25);
143  testtag_timepicker_UI_key(tp, testid, "11:12:25", tp.hourField, 12, 12, 25, 11, 12, 25);
144  testtag_timepicker_UI_key(tp, testid, "7:12:25", tp.hourField, 8, 12, 25, 7, 12, 25);
145  testtag_timepicker_UI_key(tp, testid, "16:12:25", tp.hourField, 17, 12, 25, 16, 12, 25);
146  testtag_timepicker_UI_key(tp, testid, "23:12:25", tp.hourField, 0, 12, 25, 23, 12, 25);
147
148  testtag_timepicker_UI_key(tp, testid, "15:23:46", tp.minuteField, 15, 24, 46, 15, 23, 46);
149  testtag_timepicker_UI_key(tp, testid, "15:0:46", tp.minuteField, 15, 1, 46, 15, 0, 46);
150  testtag_timepicker_UI_key(tp, testid, "15:59:46", tp.minuteField, 15, 0, 46, 15, 59, 46);
151
152  testtag_timepicker_UI_key(tp, testid, "11:50:46", tp.secondField, 11, 50, 47, 11, 50, 46);
153  testtag_timepicker_UI_key(tp, testid, "11:50:0", tp.secondField, 11, 50, 1, 11, 50, 0);
154  testtag_timepicker_UI_key(tp, testid, "11:50:59", tp.secondField, 11, 50, 0, 11, 50, 59);
155}
156
157function testtag_timepicker_UI_key(tp, testid, value, field,
158                                   uhour, uminute, usecond,
159                                   dhour, dminute, dsecond)
160{
161  tp.value = value;
162  field.focus();
163
164  var eventTarget = tp.readOnly ? null : tp;
165
166  var testname = testid + " " + value + " key up";
167  synthesizeKeyExpectEvent("VK_UP", { }, eventTarget, "change", testname);
168  testtag_comparetime(tp, testname, uhour, uminute, usecond);
169
170  testname = testid + " " + value + " key down";
171  synthesizeKeyExpectEvent("VK_DOWN", { }, eventTarget, "change", testname);
172  testtag_comparetime(tp, testname, dhour, dminute, dsecond);
173}
174
175function testtag_gettimestring(hour, minute, second)
176{
177  if (minute < 10)
178    minute = "0" + minute;
179  if (second < 10)
180    second = "0" + second;
181  return hour + ":" + minute + ":" + second;
182}
183
184function testtag_comparetime(tp, testid, hour, minute, second)
185{
186  is(tp.value, testtag_gettimestring(hour, minute, second), testid + " value");
187  is(tp.getAttribute("value"),
188                testtag_gettimestring(hour, minute, second),
189                testid + " value attribute");
190
191  var dateValue = tp.dateValue;
192  ok(dateValue.getHours() == hour &&
193                dateValue.getMinutes() == minute &&
194                dateValue.getSeconds() == second,
195                testid + " dateValue");
196
197  is(tp.hour, hour, testid + " hour");
198  is(tp.minute, minute, testid + " minute");
199  is(tp.second, second, testid + " second");
200}
201
202]]>
203
204</script>
205
206</window>