diff options
Diffstat (limited to 'toolkit/content/tests/chrome/test_datepicker.xul')
-rw-r--r-- | toolkit/content/tests/chrome/test_datepicker.xul | 415 |
1 files changed, 415 insertions, 0 deletions
diff --git a/toolkit/content/tests/chrome/test_datepicker.xul b/toolkit/content/tests/chrome/test_datepicker.xul new file mode 100644 index 000000000..e7a61f43b --- /dev/null +++ b/toolkit/content/tests/chrome/test_datepicker.xul @@ -0,0 +1,415 @@ +<?xml version="1.0"?> +<?xml-stylesheet href="chrome://global/skin" type="text/css"?> +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> +<!-- + XUL Widget Test for datepicker + --> +<window title="datepicker" width="500" height="600" + onload="setTimeout(testtag_datepickers, 0);" + xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script> + +<hbox onpopupshown="testtag_datepicker_UI_popup()" + onpopuphidden="testtag_finish()"> +<datepicker id="datepicker"/> +<datepicker id="datepicker-popup" type="popup"/> +<hbox onDOMMouseScroll="mouseScrolled = event.defaultPrevented;"> + <datepicker id="datepicker-grid" type="grid" value="2007-04-21"/> +</hbox> +</hbox> + +<!-- Test-only key bindings, but must not conflict with the application. --> +<keyset id="mainKeyset"> + <key id="key_alt_z" key="Z" oncommand="return" modifiers="alt"/> + <key id="key_ctrl_q" key="Q" oncommand="return" modifiers="control"/> + <key id="key_meta_e" key="E" oncommand="return" modifiers="meta"/> +</keyset> + +<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[ + +var mouseScrolled = false; + +SimpleTest.waitForExplicitFinish(); + +function testtag_datepickers() +{ + var dppopup = document.getElementById("datepicker-popup"); + testtag_datepicker(document.getElementById("datepicker"), "", "datepicker"); + testtag_datepicker(dppopup, "popup", "datepicker popup"); + + var gridpicker = document.getElementById("datepicker-grid"); + is(gridpicker.monthField.selectedIndex, "3", "datepicker grid correct month is initially selected"); + testtag_datepicker(gridpicker, "grid", "datepicker grid"); + dppopup.open = true; +} + +function testtag_finish() +{ + ok(!document.getElementById("datepicker-popup").open, "datepicker popup open false again"); + + var dpgrid = document.getElementById("datepicker-grid"); + synthesizeWheel(dpgrid, 5, 5, { deltaY: 10.0, + deltaMode: WheelEvent.DOM_DELTA_LINE }); + is(mouseScrolled, true, "mouse scrolled"); + is(dpgrid.displayedMonth, 2, "mouse scroll changed month"); + + SimpleTest.finish(); +} + +function testtag_datepicker(dp, type, testid) +{ + testid += " "; + + var today = new Date(); + var tyear = today.getFullYear(); + var tmonth = today.getMonth(); + var tdate = today.getDate(); + + // testtag_comparedate(dp, testid + "initial", tyear, tmonth, tdate); + + // check that setting the value property works + dp.value = testtag_getdatestring(tyear, tmonth, tdate); + testtag_comparedate(dp, testid + "set value", tyear, tmonth, tdate); + + // check that setting the dateValue property works + dp.dateValue = today; + testtag_comparedate(dp, testid + "set dateValue", tyear, tmonth, tdate); + ok(dp.value !== today, testid + " set dateValue different date"); + + ok(!dp.readOnly, testid + "readOnly"); + dp.readOnly = true; + ok(dp.readOnly, testid + "set readOnly"); + dp.readOnly = false; + ok(!dp.readOnly, testid + "clear readOnly"); + + var setDateField = function(field, value, expectException, + expectedYear, expectedMonth, expectedDate) + { + var exh = false; + try { + dp[field] = value; + } catch (ex) { exh = true; } + is(exh, expectException, testid + "set " + field + " " + value); + testtag_comparedate(dp, testid + "set " + field + " " + value, + expectedYear, expectedMonth, expectedDate); + } + + // check the value property + setDateField("value", "2003-1-27", false, 2003, 0, 27); + setDateField("value", "2002-11-8", false, 2002, 10, 8); + setDateField("value", "2001-07-02", false, 2001, 6, 2); + setDateField("value", "2002-10-25", false, 2002, 9, 25); + + // check that the year, month and date fields can be set properly + setDateField("year", 2002, false, 2002, 9, 25); + setDateField("year", 0, true, 2002, 9, 25); + + setDateField("month", 6, false, 2002, 6, 25); + setDateField("month", 9, false, 2002, 9, 25); + setDateField("month", 10, false, 2002, 10, 25); + setDateField("month", -1, true, 2002, 10, 25); + setDateField("month", 12, true, 2002, 10, 25); + + setDateField("date", 9, false, 2002, 10, 9); + setDateField("date", 10, false, 2002, 10, 10); + setDateField("date", 15, false, 2002, 10, 15); + setDateField("date", 0, true, 2002, 10, 15); + setDateField("date", 32, true, 2002, 10, 15); + + // check leap year handling + setDateField("value", "1600-2-29", false, 1600, 1, 29); + setDateField("value", "2000-2-29", false, 2000, 1, 29); + setDateField("value", "2003-2-29", false, 2003, 2, 1); + setDateField("value", "2004-2-29", false, 2004, 1, 29); + setDateField("value", "2100-2-29", false, 2100, 2, 1); + + // check invalid values for the value and dateValue properties + dp.value = "2002-07-15"; + setDateField("value", "", true, 2002, 6, 15); + setDateField("value", "2-2", true, 2002, 6, 15); + setDateField("value", "2000-5-6-6", true, 2002, 6, 15); + setDateField("value", "2000-a-19", true, 2002, 6, 15); + setDateField("dateValue", "none", true, 2002, 6, 15); + + // grid and popup types can display a different month than the current one + var isGridOrPopup = (type == "grid" || type == "popup"); + dp.displayedMonth = 3; + testtag_comparedate(dp, testid + "set displayedMonth", + 2002, isGridOrPopup ? 6 : 3, 15, 3); + + dp.displayedYear = 2009; + testtag_comparedate(dp, testid + "set displayedYear", + isGridOrPopup ? 2002 : 2009, isGridOrPopup ? 6 : 3, 15, 3, 2009); + + if (isGridOrPopup) { + dp.value = "2008-02-29"; + dp.displayedYear = 2009; + is(dp.displayedMonth, 1, "set displayedYear during leap year"); + } + + is(dp.open, false, testid + "open false"); + if (type != "popup") { + dp.open = true; + ok(!dp.open, testid + "open still false"); + } + + // check the fields + if (type != "grid") { + ok(dp.yearField instanceof HTMLInputElement, testid + "yearField"); + ok(dp.monthField instanceof HTMLInputElement, testid + "monthField"); + ok(dp.dateField instanceof HTMLInputElement, testid + "dateField"); + + testtag_datepicker_UI_fields(dp, testid); + + dp.readOnly = true; + + // check that keyboard usage doesn't change the value when the datepicker + // is read only + testtag_datepicker_UI_key(dp, testid + "readonly ", "2003-01-29", + dp.yearField, 2003, 0, 29, 2003, 0, 29); + testtag_datepicker_UI_key(dp, testid + "readonly ", "2003-04-29", + dp.monthField, 2003, 3, 29, 2003, 3, 29); + testtag_datepicker_UI_key(dp, testid + "readonly ", "2003-06-15", + dp.dateField, 2003, 5, 15, 2003, 5, 15); + + dp.readOnly = false; + } + else { + testtag_datepicker_UI_grid(dp, "grid", testid); + } +} + +function testtag_datepicker_UI_fields(dp, testid) +{ + testid += "UI"; + dp.focus(); + + // test adjusting the date with the up and down keys + testtag_datepicker_UI_key(dp, testid, "2003-01-29", dp.yearField, 2004, 0, 29, 2003, 0, 29); + testtag_datepicker_UI_key(dp, testid, "1600-02-29", dp.yearField, 1601, 1, 28, 1600, 1, 28); + testtag_datepicker_UI_key(dp, testid, "2000-02-29", dp.yearField, 2001, 1, 28, 2000, 1, 28); + testtag_datepicker_UI_key(dp, testid, "2004-02-29", dp.yearField, 2005, 1, 28, 2004, 1, 28); + + testtag_datepicker_UI_key(dp, testid, "2003-04-29", dp.monthField, 2003, 4, 29, 2003, 3, 29); + testtag_datepicker_UI_key(dp, testid, "2003-01-15", dp.monthField, 2003, 1, 15, 2003, 0, 15); + testtag_datepicker_UI_key(dp, testid, "2003-12-29", dp.monthField, 2003, 0, 29, 2003, 11, 29); + testtag_datepicker_UI_key(dp, testid, "2003-03-31", dp.monthField, 2003, 3, 30, 2003, 2, 30); + + testtag_datepicker_UI_key(dp, testid, "2003-06-15", dp.dateField, 2003, 5, 16, 2003, 5, 15); + testtag_datepicker_UI_key(dp, testid, "2003-06-01", dp.dateField, 2003, 5, 2, 2003, 5, 1); + testtag_datepicker_UI_key(dp, testid, "2003-06-30", dp.dateField, 2003, 5, 1, 2003, 5, 30); + testtag_datepicker_UI_key(dp, testid, "1600-02-28", dp.dateField, 1600, 1, 29, 1600, 1, 28); + testtag_datepicker_UI_key(dp, testid, "2000-02-28", dp.dateField, 2000, 1, 29, 2000, 1, 28); + testtag_datepicker_UI_key(dp, testid, "2003-02-28", dp.dateField, 2003, 1, 1, 2003, 1, 28); + testtag_datepicker_UI_key(dp, testid, "2004-02-28", dp.dateField, 2004, 1, 29, 2004, 1, 28); + testtag_datepicker_UI_key(dp, testid, "2100-02-28", dp.dateField, 2100, 1, 1, 2100, 1, 28); + + synthesizeKeyExpectEvent('Z', { altKey: true }, $("key_alt_z"), "command", testid + " alt shortcut"); + synthesizeKeyExpectEvent('Q', { ctrlKey: true }, $("key_ctrl_q"), "command", testid + " ctrl shortcut"); + synthesizeKeyExpectEvent('E', { metaKey: true }, $("key_meta_e"), "command", testid + " meta shortcut"); +} + +function testtag_datepicker_UI_grid(dp, type, testid) +{ + testid += "UI "; + + // check that pressing the cursor keys moves the date properly. For grid + // types, focus the grid first. For popup types, the grid should be focused + // automatically when opening the popup. + var ktarget = dp; + if (type == "grid") + dp.focus(); + else + ktarget = dp.attachedControl; + + dp.value = "2003-02-22"; + + synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "change", testid + "key left"); + is(dp.value, "2003-02-21", testid + "key left"); + + synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "change", testid + "key right"); + is(dp.value, "2003-02-22", testid + "key right"); + synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "change", testid + "key right next week"); + is(dp.value, "2003-02-23", testid + "key right next week"); + synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "change", testid + "key left previous week"); + is(dp.value, "2003-02-22", testid + "key left previous week"); + + synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "change", testid + "key up"); + is(dp.value, "2003-02-15", testid + "key up"); + synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "change", testid + "key down"); + is(dp.value, "2003-02-22", testid + "key down"); + synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "change"); + is(dp.value, "2003-03-01", testid + "key down next month", testid + "key down next month"); + synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "change"); + is(dp.value, "2003-02-22", testid + "key up previous month", testid + "key up previous month"); + + // the displayed month may be changed with the page up and page down keys, + // however this only changes the displayed month, not the current value. + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down"); + is(dp.value, "2003-02-22", testid + "key page down"); + + // the monthchange event is fired when the displayed month is changed + synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "monthchange", testid + "key up after month change"); + is(dp.value, "2003-02-15", testid + "key up after month change"); + + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up"); + is(dp.value, "2003-02-15", testid + "key page up"); + + // check handling at the start and end of the month + dp.value = "2010-10-01"; + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-10-01"); + is(dp.displayedMonth, 8, testid + "key page up 2010-10-01 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-10-01 displayedYear"); + + dp.value = "2010-10-01"; + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-10-01"); + is(dp.displayedMonth, 10, testid + "key page down 2010-10-01 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page down 2010-10-01 displayedYear"); + + dp.value = "2010-10-31"; + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-10-31"); + is(dp.displayedMonth, 8, testid + "key page up 2010-10-31 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-10-01 displayedYear"); + dp.value = "2010-10-31"; + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-10-31"); + is(dp.displayedMonth, 10, testid + "key page down 2010-10-31 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-10-31 displayedYear"); + + // check handling at the end of february + dp.value = "2010-03-31"; + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-03-31"); + is(dp.displayedMonth, 1, testid + "key page up 2010-03-31 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-03-31 displayedYear"); + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2010-02-28"); + is(dp.displayedMonth, 0, testid + "key page up 2010-02-28 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-02-28 displayedYear"); + + dp.value = "2010-01-31"; + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-01-31"); + is(dp.displayedMonth, 1, testid + "key page down 2010-01-31 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-01-31 displayedYear"); + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2010-02-28"); + is(dp.displayedMonth, 2, testid + "key page down 2010-02-28 displayedMonth"); + is(dp.displayedYear, 2010, testid + "key page up 2010-02-28 displayedYear"); + + // check handling at the end of february during a leap year + dp.value = "2008-01-31"; + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", testid + "key page down 2008-01-31"); + is(dp.displayedMonth, 1, testid + "key page down 2008-01-31 displayedMonth"); + is(dp.displayedYear, 2008, testid + "key page up 2008-01-31 displayedYear"); + dp.value = "2008-03-31"; + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", testid + "key page up 2008-03-31"); + is(dp.displayedMonth, 1, testid + "key page up 2008-03-31 displayedMonth"); + is(dp.displayedYear, 2008, testid + "key page up 2008-03-31 displayedYear"); + + // the value of a read only datepicker cannot be changed + dp.value = "2003-02-15"; + + dp.readOnly = true; + synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "!change", testid + "key left read only"); + is(dp.value, "2003-02-15", testid + "key left read only"); + synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "!change", testid + "key right read only"); + is(dp.value, "2003-02-15", testid + "key right read only"); + synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "!change", testid + "key down read only"); + is(dp.value, "2003-02-15", testid + "key down read only"); + synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "!change", testid + "key up read only"); + is(dp.value, "2003-02-15", testid + "key up read only"); + + // month can still be changed even when readonly + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "monthchange", + testid + "key page up read only"); + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "monthchange", + testid + "key page down read only"); + + dp.readOnly = false; + synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "change", testid + "key left changeable again"); + is(dp.value, "2003-02-14", testid + "key left changeable again"); + + // the value of a disabled datepicker cannot be changed + dp.disabled = true; + synthesizeKeyExpectEvent("VK_LEFT", { }, ktarget, "!change", testid + "key left disabled"); + is(dp.value, "2003-02-14", testid + "key left disabled"); + synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "!change", testid + "key right disabled"); + is(dp.value, "2003-02-14", testid + "key right disabled"); + synthesizeKeyExpectEvent("VK_DOWN", { }, ktarget, "!change", testid + "key down disabled"); + is(dp.value, "2003-02-14", testid + "key down disabled"); + synthesizeKeyExpectEvent("VK_UP", { }, ktarget, "!change", testid + "key up disabled"); + is(dp.value, "2003-02-14", testid + "key up disabled"); + + // month cannot be changed even when disabled + synthesizeKeyExpectEvent("VK_PAGE_DOWN", { }, ktarget, "!monthchange", + testid + "key page down disabled"); + synthesizeKeyExpectEvent("VK_PAGE_UP", { }, ktarget, "!monthchange", + testid + "key page up disabled"); + + dp.disabled = false; + synthesizeKeyExpectEvent("VK_RIGHT", { }, ktarget, "change", testid + "key right enabled again"); + is(dp.value, "2003-02-15", testid + "key right enabled again"); +} + +function testtag_datepicker_UI_popup() +{ + var dppopup = document.getElementById("datepicker-popup"); + is(dppopup.open, true, "datepicker popup after open"); + testtag_datepicker_UI_grid(dppopup, "popup", "datepicker popup "); + dppopup.open = false; +} + +function testtag_datepicker_UI_key(dp, testid, value, field, + uyear, umonth, udate, + dyear, dmonth, ddate) +{ + dp.value = value; + field.focus(); + + synthesizeKey("VK_UP", { }); + testtag_comparedate(dp, testid + " " + value + " key up", uyear, umonth, udate); + + synthesizeKey("VK_DOWN", { }); + testtag_comparedate(dp, testid + " " + value + " key down", dyear, dmonth, ddate); +} + +function testtag_getdatestring(year, month, date) +{ + month = (month < 9) ? ("0" + ++month) : month + 1; + if (date < 10) + date = "0" + date; + return year + "-" + month + "-" + date; +} + +function testtag_comparedate(dp, testid, year, month, date, displayedMonth, displayedYear) +{ + is(dp.value, testtag_getdatestring(year, month, date), testid + " value"); + if (testid.indexOf("initial") == -1) + is(dp.getAttribute("value"), + testtag_getdatestring(year, month, date), + testid + " value attribute"); + + var dateValue = dp.dateValue; + ok(dateValue.getFullYear() == year && + dateValue.getMonth() == month && + dateValue.getDate() == date, + testid + " dateValue"); + + is(dp.year, year, testid + " year"); + is(dp.month, month, testid + " month"); + is(dp.displayedMonth, displayedMonth ? displayedMonth : month, testid + " displayedMonth"); + is(dp.displayedYear, displayedYear ? displayedYear : year, testid + " displayedYear"); + is(dp.date, date, testid + " date"); +} + +]]> + +</script> + +</window> |