datepicker_test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.ui.DatePickerTest');
  15. goog.setTestOnly('goog.ui.DatePickerTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.Role');
  18. goog.require('goog.date.Date');
  19. goog.require('goog.date.DateRange');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.dom.classlist');
  23. goog.require('goog.events');
  24. goog.require('goog.events.KeyCodes');
  25. goog.require('goog.i18n.DateTimeSymbols');
  26. goog.require('goog.i18n.DateTimeSymbols_en_US');
  27. goog.require('goog.i18n.DateTimeSymbols_zh_HK');
  28. goog.require('goog.style');
  29. goog.require('goog.testing.events');
  30. goog.require('goog.testing.jsunit');
  31. goog.require('goog.testing.recordFunction');
  32. goog.require('goog.ui.DatePicker');
  33. var picker;
  34. var $$ = goog.dom.getElementsByTagNameAndClass;
  35. var sandbox;
  36. function setUpPage() {
  37. sandbox = goog.dom.getElement('sandbox');
  38. }
  39. function tearDown() {
  40. picker.dispose();
  41. goog.dom.removeChildren(sandbox);
  42. }
  43. function testIsMonthOnLeft() {
  44. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_en_US;
  45. picker = new goog.ui.DatePicker();
  46. picker.create(sandbox);
  47. var head = $$('tr', 'goog-date-picker-head')[0];
  48. var month = $$('button', 'goog-date-picker-month', head.firstChild)[0];
  49. assertSameElements(
  50. 'Button element must have expected class names',
  51. ['goog-date-picker-btn', 'goog-date-picker-month'],
  52. goog.dom.classlist.get(month));
  53. }
  54. function testIsYearOnLeft() {
  55. goog.i18n.DateTimeSymbols = goog.i18n.DateTimeSymbols_zh_HK;
  56. picker = new goog.ui.DatePicker();
  57. picker.create(sandbox);
  58. var head = $$('tr', 'goog-date-picker-head')[0];
  59. var year = $$('button', 'goog-date-picker-year', head.firstChild)[0];
  60. assertSameElements(
  61. 'Button element must have expected class names',
  62. ['goog-date-picker-btn', 'goog-date-picker-year'],
  63. goog.dom.classlist.get(year));
  64. }
  65. function testHidingOfTableFoot0() {
  66. picker = new goog.ui.DatePicker();
  67. picker.setAllowNone(false);
  68. picker.setShowToday(false);
  69. picker.create(sandbox);
  70. var tFoot = $$('tfoot')[0];
  71. assertFalse(goog.style.isElementShown(tFoot));
  72. }
  73. function testHidingOfTableFoot1() {
  74. picker = new goog.ui.DatePicker();
  75. picker.setAllowNone(false);
  76. picker.setShowToday(true);
  77. picker.create(sandbox);
  78. var tFoot = $$('tfoot')[0];
  79. assertTrue(goog.style.isElementShown(tFoot));
  80. }
  81. function testHidingOfTableFoot2() {
  82. picker = new goog.ui.DatePicker();
  83. picker.setAllowNone(true);
  84. picker.setShowToday(false);
  85. picker.create(sandbox);
  86. var tFoot = $$('tfoot')[0];
  87. assertTrue(goog.style.isElementShown(tFoot));
  88. }
  89. function testHidingOfTableFoot3() {
  90. picker = new goog.ui.DatePicker();
  91. picker.setAllowNone(true);
  92. picker.setShowToday(true);
  93. picker.create(sandbox);
  94. var tFoot = $$('tfoot')[0];
  95. assertTrue(goog.style.isElementShown(tFoot));
  96. }
  97. function testHidingOfTableFootAfterCreate0() {
  98. picker = new goog.ui.DatePicker();
  99. picker.create(sandbox);
  100. picker.setAllowNone(false);
  101. picker.setShowToday(false);
  102. var tFoot = $$('tfoot')[0];
  103. assertFalse(goog.style.isElementShown(tFoot));
  104. }
  105. function testHidingOfTableFootAfterCreate1() {
  106. picker = new goog.ui.DatePicker();
  107. picker.create(sandbox);
  108. picker.setAllowNone(false);
  109. picker.setShowToday(true);
  110. var tFoot = $$('tfoot')[0];
  111. assertTrue(goog.style.isElementShown(tFoot));
  112. }
  113. function testHidingOfTableFootAfterCreate2() {
  114. picker = new goog.ui.DatePicker();
  115. picker.create(sandbox);
  116. picker.setAllowNone(true);
  117. picker.setShowToday(false);
  118. var tFoot = $$('tfoot')[0];
  119. assertTrue(goog.style.isElementShown(tFoot));
  120. }
  121. function testHidingOfTableFootAfterCreate3() {
  122. picker = new goog.ui.DatePicker();
  123. picker.create(sandbox);
  124. picker.setAllowNone(true);
  125. picker.setShowToday(true);
  126. var tFoot = $$('tfoot')[0];
  127. assertTrue(goog.style.isElementShown(tFoot));
  128. }
  129. function testLongDateFormat() {
  130. picker = new goog.ui.DatePicker();
  131. picker.create(sandbox);
  132. picker.setLongDateFormat(true);
  133. var dates = $$('td', 'goog-date-picker-date');
  134. for (var i = 0; i < dates.length; i++) {
  135. assertEquals(2, goog.dom.getTextContent(dates[i]).length);
  136. }
  137. }
  138. function testGetActiveMonth() {
  139. picker = new goog.ui.DatePicker(new Date(2000, 5, 5));
  140. var month = picker.getActiveMonth();
  141. assertObjectEquals(new goog.date.Date(2000, 5, 1), month);
  142. month.setMonth(10);
  143. assertObjectEquals(
  144. 'modifying the returned object is safe', new goog.date.Date(2000, 5, 1),
  145. picker.getActiveMonth());
  146. }
  147. function testGetActiveMonthBeforeYear100() {
  148. var date = new Date(23, 5, 5);
  149. // Above statement will create date with year 1923, need to set full year
  150. // explicitly.
  151. date.setFullYear(23);
  152. var expectedMonth = new goog.date.Date(23, 5, 1);
  153. expectedMonth.setFullYear(23);
  154. picker = new goog.ui.DatePicker(date);
  155. var month = picker.getActiveMonth();
  156. assertObjectEquals(expectedMonth, month);
  157. month.setMonth(10);
  158. assertObjectEquals(
  159. 'modifying the returned object is safe', expectedMonth,
  160. picker.getActiveMonth());
  161. }
  162. function testGetDate() {
  163. picker = new goog.ui.DatePicker(new Date(2000, 0, 1));
  164. var date = picker.getDate();
  165. assertObjectEquals(new goog.date.Date(2000, 0, 1), date);
  166. date.setMonth(1);
  167. assertObjectEquals(
  168. 'modifying the returned date is safe', new goog.date.Date(2000, 0, 1),
  169. picker.getDate());
  170. picker.setDate(null);
  171. assertNull('no date is selected', picker.getDate());
  172. }
  173. function testGetDateBeforeYear100() {
  174. var inputDate = new Date(23, 5, 5);
  175. // Above statement will create date with year 1923, need to set full year
  176. // explicitly.
  177. inputDate.setFullYear(23);
  178. picker = new goog.ui.DatePicker(inputDate);
  179. var date = picker.getDate();
  180. var expectedDate = new goog.date.Date(23, 5, 5);
  181. expectedDate.setFullYear(23);
  182. assertObjectEquals(expectedDate, date);
  183. picker.setDate(inputDate);
  184. assertObjectEquals(expectedDate, picker.getDate());
  185. var expectedMonth = new goog.date.Date(23, 5, 1);
  186. expectedMonth.setFullYear(23);
  187. assertObjectEquals(expectedMonth, picker.getActiveMonth());
  188. }
  189. function testGridForDecember23() {
  190. // Initialize picker to December 23.
  191. var inputDate = new Date(23, 11, 5);
  192. // Above statement will create date with year 1923, need to set full year
  193. // explicitly.
  194. inputDate.setFullYear(23);
  195. picker = new goog.ui.DatePicker(inputDate);
  196. picker.create(sandbox);
  197. // Grid start with last days of November 23, shows December 23 and first days
  198. // of January 24.
  199. for (var i = 0; i < 6; i++) {
  200. for (var j = 0; j < 7; j++) {
  201. var date = picker.getDateAt(i, j);
  202. if (date.getMonth() == 0) {
  203. assertEquals(24, date.getFullYear());
  204. } else {
  205. assertEquals(23, date.getFullYear());
  206. }
  207. }
  208. }
  209. }
  210. function testGridForJanuary22() {
  211. // Initialize picker to January 22.
  212. var inputDate = new Date(22, 0, 5);
  213. // Above statement will create date with year 1922, need to set full year
  214. // explicitly.
  215. inputDate.setFullYear(22);
  216. picker = new goog.ui.DatePicker(inputDate);
  217. picker.create(sandbox);
  218. // Grid start with last days of December 21, shows January 22 and first days
  219. // of February 22.
  220. for (var i = 0; i < 6; i++) {
  221. for (var j = 0; j < 7; j++) {
  222. var date = picker.getDateAt(i, j);
  223. if (date.getMonth() == 11) {
  224. assertEquals(21, date.getFullYear());
  225. } else {
  226. assertEquals(22, date.getFullYear());
  227. }
  228. }
  229. }
  230. }
  231. function testGetDateAt() {
  232. picker = new goog.ui.DatePicker();
  233. picker.create(sandbox);
  234. picker.setDate(new Date(2000, 5, 5));
  235. var date = picker.getDateAt(0, 0);
  236. assertTrue(date.equals(picker.grid_[0][0]));
  237. date.setMonth(1);
  238. assertFalse(date.equals(picker.grid_[0][0]));
  239. }
  240. function testGetDateAt_NotInGrid() {
  241. picker = new goog.ui.DatePicker();
  242. picker.create(sandbox);
  243. picker.setDate(new Date(2000, 5, 5));
  244. var date = picker.getDateAt(-1, 0);
  245. assertNull(date);
  246. date = picker.getDateAt(0, -1);
  247. assertNull(date);
  248. }
  249. function testGetDateElementAt() {
  250. picker = new goog.ui.DatePicker();
  251. picker.create(sandbox);
  252. picker.setDate(new Date(2000, 5, 5));
  253. var element = picker.getDateElementAt(0, 0);
  254. assertEquals('td', element.tagName.toLowerCase());
  255. assertObjectEquals(element, picker.elTable_[1][1]);
  256. }
  257. function testGetDateElementAt_NotInTable() {
  258. picker = new goog.ui.DatePicker();
  259. picker.create(sandbox);
  260. picker.setDate(new Date(2000, 5, 5));
  261. var element = picker.getDateElementAt(-1, 0);
  262. assertNull(element);
  263. element = picker.getDateElementAt(0, -1);
  264. assertNull(element);
  265. element = picker.getDateElementAt(picker.elTable_.length - 1, 0);
  266. assertNull(element);
  267. element = picker.getDateElementAt(0, picker.elTable_[0].length - 1);
  268. assertNull(element);
  269. }
  270. function testSetDate() {
  271. picker = new goog.ui.DatePicker();
  272. picker.createDom();
  273. picker.enterDocument();
  274. var selectEvents = 0;
  275. var changeEvents = 0;
  276. var changeActiveMonthEvents = 0;
  277. goog.events.listen(
  278. picker, goog.ui.DatePicker.Events.SELECT, function() { selectEvents++; });
  279. goog.events.listen(
  280. picker, goog.ui.DatePicker.Events.CHANGE, function() { changeEvents++; });
  281. goog.events.listen(
  282. picker, goog.ui.DatePicker.Events.CHANGE_ACTIVE_MONTH,
  283. function() { changeActiveMonthEvents++; });
  284. // Set date.
  285. picker.setDate(new Date(2010, 1, 26));
  286. assertEquals('no select event dispatched', 1, selectEvents);
  287. assertEquals('no change event dispatched', 1, changeEvents);
  288. assertEquals(
  289. 'no change active month event dispatched', 1, changeActiveMonthEvents);
  290. assertTrue(
  291. 'date is set', new goog.date.Date(2010, 1, 26).equals(picker.getDate()));
  292. // Set date to same date.
  293. picker.setDate(new Date(2010, 1, 26));
  294. assertEquals('1 select event dispatched', 2, selectEvents);
  295. assertEquals('no change event dispatched', 1, changeEvents);
  296. assertEquals(
  297. 'no change active month event dispatched', 1, changeActiveMonthEvents);
  298. // Set date to different date.
  299. picker.setDate(new Date(2010, 1, 27));
  300. assertEquals('another select event dispatched', 3, selectEvents);
  301. assertEquals('1 change event dispatched', 2, changeEvents);
  302. assertEquals(
  303. '2 change active month events dispatched', 1, changeActiveMonthEvents);
  304. // Set date to a date in a different month.
  305. picker.setDate(new Date(2010, 2, 27));
  306. assertEquals('another select event dispatched', 4, selectEvents);
  307. assertEquals('another change event dispatched', 3, changeEvents);
  308. assertEquals(
  309. '3 change active month event dispatched', 2, changeActiveMonthEvents);
  310. // Set date to none.
  311. picker.setDate(null);
  312. assertEquals('another select event dispatched', 5, selectEvents);
  313. assertEquals('another change event dispatched', 4, changeEvents);
  314. assertNull('date cleared', picker.getDate());
  315. }
  316. function testChangeActiveMonth() {
  317. picker = new goog.ui.DatePicker();
  318. var changeActiveMonthEvents = 0;
  319. goog.events.listen(
  320. picker, goog.ui.DatePicker.Events.CHANGE_ACTIVE_MONTH,
  321. function() { changeActiveMonthEvents++; });
  322. // Set date.
  323. picker.setDate(new Date(2010, 1, 26));
  324. assertEquals(
  325. 'no change active month event dispatched', 1, changeActiveMonthEvents);
  326. assertTrue(
  327. 'date is set', new goog.date.Date(2010, 1, 26).equals(picker.getDate()));
  328. // Change to next month.
  329. picker.nextMonth();
  330. assertEquals(
  331. '1 change active month event dispatched', 2, changeActiveMonthEvents);
  332. assertTrue(
  333. 'date should still be the same',
  334. new goog.date.Date(2010, 1, 26).equals(picker.getDate()));
  335. // Change to next year.
  336. picker.nextYear();
  337. assertEquals(
  338. '2 change active month events dispatched', 3, changeActiveMonthEvents);
  339. // Change to previous month.
  340. picker.previousMonth();
  341. assertEquals(
  342. '3 change active month events dispatched', 4, changeActiveMonthEvents);
  343. // Change to previous year.
  344. picker.previousYear();
  345. assertEquals(
  346. '4 change active month events dispatched', 5, changeActiveMonthEvents);
  347. }
  348. function testUserSelectableDates() {
  349. var dateRange = new goog.date.DateRange(
  350. new goog.date.Date(2010, 1, 25), new goog.date.Date(2010, 1, 27));
  351. picker = new goog.ui.DatePicker();
  352. picker.setUserSelectableDateRange(dateRange);
  353. assertFalse(
  354. 'should not be selectable date',
  355. picker.isUserSelectableDate_(new goog.date.Date(2010, 1, 24)));
  356. assertTrue(
  357. 'should be a selectable date',
  358. picker.isUserSelectableDate_(new goog.date.Date(2010, 1, 25)));
  359. assertTrue(
  360. 'should be a selectable date',
  361. picker.isUserSelectableDate_(new goog.date.Date(2010, 1, 26)));
  362. assertTrue(
  363. 'should be a selectable date',
  364. picker.isUserSelectableDate_(new goog.date.Date(2010, 1, 27)));
  365. assertFalse(
  366. 'should not be selectable date',
  367. picker.isUserSelectableDate_(new goog.date.Date(2010, 1, 28)));
  368. }
  369. function testGetUserSelectableDateRange() {
  370. picker = new goog.ui.DatePicker();
  371. var dateRange = picker.getUserSelectableDateRange();
  372. assertTrue(
  373. 'default date range is all time',
  374. goog.date.DateRange.equals(dateRange, goog.date.DateRange.allTime()));
  375. var newDateRange = new goog.date.DateRange(
  376. new goog.date.Date(2010, 1, 25), new goog.date.Date(2010, 1, 27));
  377. picker.setUserSelectableDateRange(newDateRange);
  378. dateRange = picker.getUserSelectableDateRange();
  379. assertTrue(
  380. 'should be equal to updated date range',
  381. goog.date.DateRange.equals(dateRange, newDateRange));
  382. }
  383. function testUniqueCellIds() {
  384. picker = new goog.ui.DatePicker();
  385. picker.render();
  386. var cells = goog.dom.getElementsByTagNameAndClass(
  387. goog.dom.TagName.TD, undefined, picker.getElement());
  388. var existingIds = {};
  389. var numCells = cells.length;
  390. for (var i = 0; i < numCells; i++) {
  391. assertNotNull(cells[i]);
  392. if (goog.a11y.aria.getRole(cells[i]) == goog.a11y.aria.Role.GRIDCELL) {
  393. assertNonEmptyString('cell id is non empty', cells[i].id);
  394. assertUndefined('cell id is not unique', existingIds[cells[i].id]);
  395. existingIds[cells[i].id] = 1;
  396. }
  397. }
  398. }
  399. function testDecoratePreservesClasses() {
  400. picker = new goog.ui.DatePicker();
  401. var div = goog.dom.createDom(goog.dom.TagName.DIV, 'existing-class');
  402. picker.decorate(div);
  403. assertTrue(goog.dom.classlist.contains(div, picker.getBaseCssClass()));
  404. assertTrue(goog.dom.classlist.contains(div, 'existing-class'));
  405. }
  406. function testKeyboardNavigation() {
  407. picker = new goog.ui.DatePicker();
  408. picker.render(goog.dom.getElement('sandbox'));
  409. var selectEvents = goog.testing.recordFunction();
  410. var changeEvents = goog.testing.recordFunction();
  411. goog.events.listen(picker, goog.ui.DatePicker.Events.SELECT, selectEvents);
  412. goog.events.listen(picker, goog.ui.DatePicker.Events.CHANGE, changeEvents);
  413. goog.testing.events.fireNonAsciiKeySequence(
  414. picker.getElement(), goog.events.KeyCodes.DOWN,
  415. goog.events.KeyCodes.DOWN);
  416. changeEvents.assertCallCount(1);
  417. selectEvents.assertCallCount(0);
  418. goog.testing.events.fireNonAsciiKeySequence(
  419. picker.getElement(), goog.events.KeyCodes.ENTER,
  420. goog.events.KeyCodes.ENTER);
  421. changeEvents.assertCallCount(1);
  422. selectEvents.assertCallCount(1);
  423. }
  424. function testDayGridHasNonEmptyAriaLabels() {
  425. picker = new goog.ui.DatePicker(new Date(2017, 8, 9));
  426. picker.render(goog.dom.getElement('sandbox'));
  427. var cells = goog.dom.getElementsByTagNameAndClass(
  428. goog.dom.TagName.TD, undefined, picker.getElement());
  429. var numCells = cells.length;
  430. for (var i = 0; i < numCells; i++) {
  431. assertNotNull(cells[i]);
  432. if (goog.a11y.aria.getRole(cells[i]) == goog.a11y.aria.Role.GRIDCELL) {
  433. assertNonEmptyString(
  434. 'Aria label in date cell should not be empty',
  435. goog.a11y.aria.getLabel(cells[i]));
  436. }
  437. }
  438. }