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