PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/toolkit/content/tests/chrome/test_datepicker.xul

http://github.com/zpao/v8monkey
Unknown | 416 lines | 345 code | 71 blank | 0 comment | 0 complexity | 7f62d2e2a355ea8603039d4ff64ebb0a 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 datepicker
  6  -->
  7<window title="datepicker" width="500" height="600"
  8        onload="setTimeout(testtag_datepickers, 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<hbox onpopupshown="testtag_datepicker_UI_popup()"
 14      onpopuphidden="testtag_finish()">
 15<datepicker id="datepicker"/>
 16<datepicker id="datepicker-popup" type="popup"/>
 17<hbox onDOMMouseScroll="mouseScrolled = event.defaultPrevented;">
 18  <datepicker id="datepicker-grid" type="grid"/>
 19</hbox>
 20</hbox>
 21
 22<!-- Test-only key bindings, but must not conflict with the application. -->
 23<keyset id="mainKeyset">
 24  <key id="key_alt_z" key="Z" oncommand="return" modifiers="alt"/>
 25  <key id="key_ctrl_q" key="Q" oncommand="return" modifiers="control"/>
 26  <key id="key_meta_e" key="E" oncommand="return" modifiers="meta"/>
 27</keyset>
 28
 29<body xmlns="http://www.w3.org/1999/xhtml">
 30<p id="display"></p>
 31<div id="content" style="display: none">
 32</div>
 33<pre id="test">
 34</pre>
 35</body>
 36
 37<script>
 38<![CDATA[
 39
 40var mouseScrolled = false;
 41
 42SimpleTest.waitForExplicitFinish();
 43
 44function testtag_datepickers()
 45{
 46  var dppopup = document.getElementById("datepicker-popup");
 47  testtag_datepicker(document.getElementById("datepicker"), "", "datepicker");
 48  testtag_datepicker(dppopup, "popup", "datepicker popup");
 49  testtag_datepicker(document.getElementById("datepicker-grid"), "grid", "datepicker grid");
 50  dppopup.open = true;
 51}
 52
 53function testtag_finish()
 54{
 55  ok(!document.getElementById("datepicker-popup").open, "datepicker popup open false again");
 56
 57  var dpgrid = document.getElementById("datepicker-grid");
 58  synthesizeMouseScroll(dpgrid, 5, 5, { delta: 10 });
 59  is(mouseScrolled, true, "mouse scrolled");
 60  is(dpgrid.displayedMonth, 2, "mouse scroll changed month");
 61
 62  SimpleTest.finish();
 63}
 64
 65function testtag_datepicker(dp, type, testid)
 66{
 67  testid += " ";
 68
 69  var today = new Date();
 70  var tyear = today.getFullYear();
 71  var tmonth = today.getMonth();
 72  var tdate = today.getDate();
 73
 74  // testtag_comparedate(dp, testid + "initial", tyear, tmonth, tdate);
 75
 76  // check that setting the value property works
 77  dp.value = testtag_getdatestring(tyear, tmonth, tdate);
 78  testtag_comparedate(dp, testid + "set value", tyear, tmonth, tdate);
 79
 80  // check that setting the dateValue property works
 81  dp.dateValue = today;
 82  testtag_comparedate(dp, testid + "set dateValue", tyear, tmonth, tdate);
 83  ok(dp.value !== today, testid + " set dateValue different date");
 84
 85  ok(!dp.readOnly, testid + "readOnly");
 86  dp.readOnly = true;
 87  ok(dp.readOnly, testid + "set readOnly");
 88  dp.readOnly = false;
 89  ok(!dp.readOnly, testid + "clear readOnly");
 90
 91  var setDateField = function(field, value, expectException,
 92                              expectedYear, expectedMonth, expectedDate)
 93  {
 94    var exh = false;
 95    try {
 96      dp[field] = value;
 97    } catch (ex) { exh = true; }
 98    is(exh, expectException, testid + "set " + field + " " + value);
 99    testtag_comparedate(dp, testid + "set " + field + " " + value,
100                        expectedYear, expectedMonth, expectedDate);
101  }
102
103  // check the value property
104  setDateField("value", "2003-1-27", false, 2003, 0, 27);
105  setDateField("value", "2002-11-8", false, 2002, 10, 8);
106  setDateField("value", "2001-07-02", false, 2001, 6, 2);
107  setDateField("value", "2002-10-25", false, 2002, 9, 25);
108
109  // check that the year, month and date fields can be set properly
110  setDateField("year", 2002, false, 2002, 9, 25);
111  setDateField("year", 0, true, 2002, 9, 25);
112
113  setDateField("month", 6, false, 2002, 6, 25);
114  setDateField("month", 9, false, 2002, 9, 25);
115  setDateField("month", 10, false, 2002, 10, 25);
116  setDateField("month", -1, true, 2002, 10, 25);
117  setDateField("month", 12, true, 2002, 10, 25);
118
119  setDateField("date", 9, false, 2002, 10, 9);
120  setDateField("date", 10, false, 2002, 10, 10);
121  setDateField("date", 15, false, 2002, 10, 15);
122  setDateField("date", 0, true, 2002, 10, 15);
123  setDateField("date", 32, true, 2002, 10, 15);
124
125  // check that dates overflow properly
126  setDateField("value", "2002-2-40", false, 2002, 2, 12);
127  setDateField("value", "2003-03-32", false, 2003, 3, 1);
128  setDateField("value", "2003-12-32", false, 2004, 0, 1);
129
130  // check leap year handling
131  setDateField("value", "1600-2-29", false, 1600, 1, 29);
132  setDateField("value", "2000-2-29", false, 2000, 1, 29);
133  setDateField("value", "2003-2-29", false, 2003, 2, 1);
134  setDateField("value", "2004-2-29", false, 2004, 1, 29);
135  setDateField("value", "2100-2-29", false, 2100, 2, 1);
136
137  // check invalid values for the value and dateValue properties
138  dp.value = "2002-07-15";
139  setDateField("value", "", true, 2002, 6, 15);
140  setDateField("value", "2-2", true, 2002, 6, 15);
141  setDateField("value", "2000-5-6-6", true, 2002, 6, 15);
142  setDateField("value", "2000-a-19", true, 2002, 6, 15);
143  setDateField("dateValue", "none", true, 2002, 6, 15);
144
145  // grid and popup types can display a different month than the current one
146  var isGridOrPopup = (type == "grid" || type == "popup");
147  dp.displayedMonth = 3;
148  testtag_comparedate(dp, testid + "set displayedMonth",
149                      2002, isGridOrPopup ? 6 : 3, 15, 3);
150
151  dp.displayedYear = 2009;
152  testtag_comparedate(dp, testid + "set displayedYear",
153                      isGridOrPopup ? 2002 : 2009, isGridOrPopup ? 6 : 3, 15, 3, 2009);
154
155  if (isGridOrPopup) {
156    dp.value = "2008-02-29";
157    dp.displayedYear = 2009;
158    is(dp.displayedMonth, 1, "set displayedYear during leap year");
159  }
160
161  is(dp.open, false, testid + "open false");
162  if (type != "popup") {
163    dp.open = true;
164    ok(!dp.open, testid + "open still false");
165  }
166
167  // check the fields
168  if (type != "grid") {
169    ok(dp.yearField instanceof HTMLInputElement, testid + "yearField");
170    ok(dp.monthField instanceof HTMLInputElement, testid + "monthField");
171    ok(dp.dateField instanceof HTMLInputElement, testid + "dateField");
172
173    testtag_datepicker_UI_fields(dp, testid);
174
175    dp.readOnly = true;
176
177    // check that keyboard usage doesn't change the value when the datepicker
178    // is read only
179    testtag_datepicker_UI_key(dp, testid + "readonly ", "2003-01-29",
180                              dp.yearField, 2003, 0, 29, 2003, 0, 29);
181    testtag_datepicker_UI_key(dp, testid + "readonly ", "2003-04-29",
182                              dp.monthField, 2003, 3, 29, 2003, 3, 29);
183    testtag_datepicker_UI_key(dp, testid + "readonly ", "2003-06-15",
184                              dp.dateField, 2003, 5, 15, 2003, 5, 15);
185
186    dp.readOnly = false;
187  }
188  else {
189    testtag_datepicker_UI_grid(dp, "grid", testid);
190  }
191}
192
193function testtag_datepicker_UI_fields(dp, testid)
194{
195  testid += "UI";
196  dp.focus();
197
198  // test adjusting the date with the up and down keys
199  testtag_datepicker_UI_key(dp, testid, "2003-01-29", dp.yearField, 2004, 0, 29, 2003, 0, 29);
200  testtag_datepicker_UI_key(dp, testid, "1600-02-29", dp.yearField, 1601, 1, 28, 1600, 1, 28);
201  testtag_datepicker_UI_key(dp, testid, "2000-02-29", dp.yearField, 2001, 1, 28, 2000, 1, 28);
202  testtag_datepicker_UI_key(dp, testid, "2004-02-29", dp.yearField, 2005, 1, 28, 2004, 1, 28);
203
204  testtag_datepicker_UI_key(dp, testid, "2003-04-29", dp.monthField, 2003, 4, 29, 2003, 3, 29);
205  testtag_datepicker_UI_key(dp, testid, "2003-01-15", dp.monthField, 2003, 1, 15, 2003, 0, 15);
206  testtag_datepicker_UI_key(dp, testid, "2003-12-29", dp.monthField, 2003, 0, 29, 2003, 11, 29);
207  testtag_datepicker_UI_key(dp, testid, "2003-03-31", dp.monthField, 2003, 3, 30, 2003, 2, 30);
208
209  testtag_datepicker_UI_key(dp, testid, "2003-06-15", dp.dateField, 2003, 5, 16, 2003, 5, 15);
210  testtag_datepicker_UI_key(dp, testid, "2003-06-01", dp.dateField, 2003, 5, 2, 2003, 5, 1);
211  testtag_datepicker_UI_key(dp, testid, "2003-06-30", dp.dateField, 2003, 5, 1, 2003, 5, 30);
212  testtag_datepicker_UI_key(dp, testid, "1600-02-28", dp.dateField, 1600, 1, 29, 1600, 1, 28);
213  testtag_datepicker_UI_key(dp, testid, "2000-02-28", dp.dateField, 2000, 1, 29, 2000, 1, 28);
214  testtag_datepicker_UI_key(dp, testid, "2003-02-28", dp.dateField, 2003, 1, 1, 2003, 1, 28);
215  testtag_datepicker_UI_key(dp, testid, "2004-02-28", dp.dateField, 2004, 1, 29, 2004, 1, 28);
216  testtag_datepicker_UI_key(dp, testid, "2100-02-28", dp.dateField, 2100, 1, 1, 2100, 1, 28);
217
218  synthesizeKeyExpectEvent('Z', { altKey: true }, $("key_alt_z"), "command", testid + " alt shortcut");
219  synthesizeKeyExpectEvent('Q', { ctrlKey: true }, $("key_ctrl_q"), "command", testid + " ctrl shortcut");
220  synthesizeKeyExpectEvent('E', { metaKey: true }, $("key_meta_e"), "command", testid + " meta shortcut");
221}
222
223function testtag_datepicker_UI_grid(dp, type, testid)
224{
225  testid += "UI ";
226
227  // check that pressing the cursor keys moves the date properly. For grid
228  // types, focus the grid first. For popup types, the grid should be focused
229  // automatically when opening the popup.
230  var ktarget = dp;
231  if (type == "grid")
232    dp.focus();
233  else
234    ktarget = dp.attachedControl;
235
236  dp.value = "2003-02-22";
237
238  synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "change", testid + "key left");
239  is(dp.value, "2003-02-21", testid + "key left");
240
241  synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "change", testid + "key right");
242  is(dp.value, "2003-02-22", testid + "key right");
243  synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "change", testid + "key right next week");
244  is(dp.value, "2003-02-23", testid + "key right next week");
245  synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "change", testid + "key left previous week");
246  is(dp.value, "2003-02-22", testid + "key left previous week");
247
248  synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "change", testid + "key up");
249  is(dp.value, "2003-02-15", testid + "key up");
250  synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "change", testid + "key down");
251  is(dp.value, "2003-02-22", testid + "key down");
252  synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "change");
253  is(dp.value, "2003-03-01", testid + "key down next month", testid + "key down next month");
254  synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "change");
255  is(dp.value, "2003-02-22", testid + "key up previous month", testid + "key up previous month");
256
257  // the displayed month may be changed with the page up and page down keys,
258  // however this only changes the displayed month, not the current value.
259  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down");
260  is(dp.value, "2003-02-22", testid + "key page down");
261
262  // the monthchange event is fired when the displayed month is changed
263  synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "monthchange", testid + "key up after month change");
264  is(dp.value, "2003-02-15", testid + "key up after month change");
265
266  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up");
267  is(dp.value, "2003-02-15", testid + "key page up");
268
269  // check handling at the start and end of the month
270  dp.value = "2010-10-01";
271  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-10-01");
272  is(dp.displayedMonth, 8, testid + "key page up 2010-10-01 displayedMonth");
273  is(dp.displayedYear, 2010, testid + "key page up 2010-10-01 displayedYear");
274
275  dp.value = "2010-10-01";
276  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-10-01");
277  is(dp.displayedMonth, 10, testid + "key page down 2010-10-01 displayedMonth");
278  is(dp.displayedYear, 2010, testid + "key page down 2010-10-01 displayedYear");
279
280  dp.value = "2010-10-31";
281  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-10-31");
282  is(dp.displayedMonth, 8, testid + "key page up 2010-10-31 displayedMonth");
283  is(dp.displayedYear, 2010, testid + "key page up 2010-10-01 displayedYear");
284  dp.value = "2010-10-31";
285  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-10-31");
286  is(dp.displayedMonth, 10, testid + "key page down 2010-10-31 displayedMonth");
287  is(dp.displayedYear, 2010, testid + "key page up 2010-10-31 displayedYear");
288
289  // check handling at the end of february
290  dp.value = "2010-03-31";
291  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-03-31");
292  is(dp.displayedMonth, 1, testid + "key page up 2010-03-31 displayedMonth");
293  is(dp.displayedYear, 2010, testid + "key page up 2010-03-31 displayedYear");
294  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-02-28");
295  is(dp.displayedMonth, 0, testid + "key page up 2010-02-28 displayedMonth");
296  is(dp.displayedYear, 2010, testid + "key page up 2010-02-28 displayedYear");
297
298  dp.value = "2010-01-31";
299  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-01-31");
300  is(dp.displayedMonth, 1, testid + "key page down 2010-01-31 displayedMonth");
301  is(dp.displayedYear, 2010, testid + "key page up 2010-01-31 displayedYear");
302  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-02-28");
303  is(dp.displayedMonth, 2, testid + "key page down 2010-02-28 displayedMonth");
304  is(dp.displayedYear, 2010, testid + "key page up 2010-02-28 displayedYear");
305
306  // check handling at the end of february during a leap year
307  dp.value = "2008-01-31";
308  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2008-01-31");
309  is(dp.displayedMonth, 1, testid + "key page down 2008-01-31 displayedMonth");
310  is(dp.displayedYear, 2008, testid + "key page up 2008-01-31 displayedYear");
311  dp.value = "2008-03-31";
312  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2008-03-31");
313  is(dp.displayedMonth, 1, testid + "key page up 2008-03-31 displayedMonth");
314  is(dp.displayedYear, 2008, testid + "key page up 2008-03-31 displayedYear");
315
316  // the value of a read only datepicker cannot be changed
317  dp.value = "2003-02-15";
318
319  dp.readOnly = true;
320  synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "!change", testid + "key left read only");
321  is(dp.value, "2003-02-15", testid + "key left read only");
322  synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "!change", testid + "key right read only");
323  is(dp.value, "2003-02-15", testid + "key right read only");
324  synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "!change", testid + "key down read only");
325  is(dp.value, "2003-02-15", testid + "key down read only");
326  synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "!change", testid + "key up read only");
327  is(dp.value, "2003-02-15", testid + "key up read only");
328
329  // month can still be changed even when readonly
330  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange",
331                           testid + "key page up read only");
332  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange",
333                           testid + "key page down read only");
334
335  dp.readOnly = false;
336  synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "change", testid + "key left changeable again");
337  is(dp.value, "2003-02-14", testid + "key left changeable again");
338
339  // the value of a disabled datepicker cannot be changed
340  dp.disabled = true;
341  synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "!change", testid + "key left disabled");
342  is(dp.value, "2003-02-14", testid + "key left disabled");
343  synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "!change", testid + "key right disabled");
344  is(dp.value, "2003-02-14", testid + "key right disabled");
345  synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "!change", testid + "key down disabled");
346  is(dp.value, "2003-02-14", testid + "key down disabled");
347  synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "!change", testid + "key up disabled");
348  is(dp.value, "2003-02-14", testid + "key up disabled");
349
350  // month cannot be changed even when disabled
351  synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "!monthchange",
352                           testid + "key page down disabled");
353  synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "!monthchange",
354                           testid + "key page up disabled");
355
356  dp.disabled = false;
357  synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "change", testid + "key right enabled again");
358  is(dp.value, "2003-02-15", testid + "key right enabled again");
359}
360
361function testtag_datepicker_UI_popup()
362{
363  var dppopup = document.getElementById("datepicker-popup");
364  is(dppopup.open, true, "datepicker popup after open");
365  testtag_datepicker_UI_grid(dppopup, "popup", "datepicker popup ");
366  dppopup.open = false;
367}
368
369function testtag_datepicker_UI_key(dp, testid, value, field,
370                                   uyear, umonth, udate,
371                                   dyear, dmonth, ddate)
372{
373  dp.value = value;
374  field.focus();
375
376  synthesizeKey("VK_UP", { });
377  testtag_comparedate(dp, testid + " " + value + " key up", uyear, umonth, udate);
378
379  synthesizeKey("VK_DOWN", { });
380  testtag_comparedate(dp, testid + " " + value + " key down", dyear, dmonth, ddate);
381}
382
383function testtag_getdatestring(year, month, date)
384{
385  month = (month < 9) ? ("0" + ++month) : month + 1;
386  if (date < 10)
387    date = "0" + date;
388  return year + "-" + month + "-" + date;
389}
390
391function testtag_comparedate(dp, testid, year, month, date, displayedMonth, displayedYear)
392{
393  is(dp.value, testtag_getdatestring(year, month, date), testid + " value");
394  if (testid.indexOf("initial") == -1)
395    is(dp.getAttribute("value"),
396                  testtag_getdatestring(year, month, date),
397                  testid + " value attribute");
398
399  var dateValue = dp.dateValue;
400  ok(dateValue.getFullYear() == year &&
401                dateValue.getMonth() == month &&
402                dateValue.getDate() == date,
403                testid + " dateValue");
404
405  is(dp.year, year, testid + " year");
406  is(dp.month, month, testid + " month");
407  is(dp.displayedMonth, displayedMonth ? displayedMonth : month, testid + " displayedMonth");
408  is(dp.displayedYear, displayedYear ? displayedYear : year, testid + " displayedYear");
409  is(dp.date, date, testid + " date");
410}
411
412]]>
413
414</script>
415
416</window>