popupdatepicker_test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2013 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.PopupDatePickerTest');
  15. goog.setTestOnly('goog.ui.PopupDatePickerTest');
  16. goog.require('goog.date.Date');
  17. goog.require('goog.events');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.recordFunction');
  20. goog.require('goog.ui.PopupBase');
  21. goog.require('goog.ui.PopupDatePicker');
  22. var popupDatePicker;
  23. function setUp() {
  24. popupDatePicker = new goog.ui.PopupDatePicker();
  25. }
  26. function tearDown() {
  27. popupDatePicker.dispose();
  28. }
  29. function testIsVisible() {
  30. assertFalse(popupDatePicker.isVisible());
  31. popupDatePicker.createDom();
  32. assertFalse(popupDatePicker.isVisible());
  33. popupDatePicker.render();
  34. assertFalse(popupDatePicker.isVisible());
  35. popupDatePicker.showPopup(document.body);
  36. assertTrue(popupDatePicker.isVisible());
  37. popupDatePicker.hidePopup();
  38. assertFalse(popupDatePicker.isVisible());
  39. }
  40. function testFiresShowAndHideEvents() {
  41. var showHandler = goog.testing.recordFunction();
  42. var hideHandler = goog.testing.recordFunction();
  43. goog.events.listen(
  44. popupDatePicker, goog.ui.PopupBase.EventType.SHOW, showHandler);
  45. goog.events.listen(
  46. popupDatePicker, goog.ui.PopupBase.EventType.HIDE, hideHandler);
  47. popupDatePicker.createDom();
  48. popupDatePicker.render();
  49. assertEquals(0, showHandler.getCallCount());
  50. assertEquals(0, hideHandler.getCallCount());
  51. popupDatePicker.showPopup(document.body);
  52. // Bug in goog.ui.Popup: the SHOW event is fired twice.
  53. assertEquals(2, showHandler.getCallCount());
  54. assertEquals(0, hideHandler.getCallCount());
  55. showHandler.reset();
  56. popupDatePicker.hidePopup();
  57. assertEquals(0, showHandler.getCallCount());
  58. assertEquals(1, hideHandler.getCallCount());
  59. }
  60. function testShow() {
  61. popupDatePicker.createDom();
  62. popupDatePicker.render();
  63. var datePicker = popupDatePicker.getDatePicker();
  64. var date = new goog.date.Date();
  65. // Date should be overwritten when opt_keepDate not specified.
  66. datePicker.setDate(date);
  67. popupDatePicker.showPopup(document.body, undefined /* opt_keepDate */);
  68. assertNull(datePicker.getDate());
  69. // Date should be overwritten when opt_keepDate is false.
  70. datePicker.setDate(date);
  71. popupDatePicker.showPopup(document.body, false /* opt_keepDate */);
  72. assertNull(datePicker.getDate());
  73. // Date should be preserved when opt_keepDate is true.
  74. datePicker.setDate(date);
  75. popupDatePicker.showPopup(document.body, true /* opt_keepDate */);
  76. assertTrue(date.equals(datePicker.getDate()));
  77. }