| 1 |
// Copyright 2011 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.LabelInputTest'); |
| 16 |
goog.setTestOnly('goog.ui.LabelInputTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.State'); |
| 20 |
goog.require('goog.dom'); |
| 21 |
goog.require('goog.dom.classlist'); |
| 22 |
goog.require('goog.events.EventType'); |
| 23 |
goog.require('goog.testing.MockClock'); |
| 24 |
goog.require('goog.testing.events'); |
| 25 |
goog.require('goog.testing.events.Event'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.ui.LabelInput'); |
| 28 |
goog.require('goog.userAgent'); |
| 29 |
|
| 30 |
var labelInput; |
| 31 |
var mockClock; |
| 32 |
|
| 33 |
function setUp() { |
| 34 |
mockClock = new goog.testing.MockClock(true); |
| 35 |
} |
| 36 |
|
| 37 |
function tearDown() { |
| 38 |
mockClock.dispose(); |
| 39 |
labelInput.dispose(); |
| 40 |
} |
| 41 |
|
| 42 |
function testGetLabel() { |
| 43 |
labelInput = new goog.ui.LabelInput(); |
| 44 |
assertEquals('no label', '', labelInput.getLabel()); |
| 45 |
|
| 46 |
labelInput = new goog.ui.LabelInput('search'); |
| 47 |
assertEquals('label is given in the ctor', 'search', labelInput.getLabel()); |
| 48 |
} |
| 49 |
|
| 50 |
function testSetLabel() { |
| 51 |
labelInput = new goog.ui.LabelInput(); |
| 52 |
labelInput.setLabel('search'); |
| 53 |
assertEquals('label is set', 'search', labelInput.getLabel()); |
| 54 |
|
| 55 |
labelInput.createDom(); |
| 56 |
labelInput.enterDocument(); |
| 57 |
mockClock.tick(10); |
| 58 |
assertNotNull(labelInput.getElement()); |
| 59 |
assertLabelValue(labelInput, 'search'); |
| 60 |
|
| 61 |
labelInput.setLabel('new label'); |
| 62 |
assertLabelValue(labelInput, 'new label'); |
| 63 |
} |
| 64 |
|
| 65 |
function assertLabelValue(labelInput, expectedLabel) { |
| 66 |
assertEquals( |
| 67 |
'label should have aria-label attribute \'' + expectedLabel + '\'', |
| 68 |
expectedLabel, goog.a11y.aria.getState( |
| 69 |
labelInput.getElement(), goog.a11y.aria.State.LABEL)); |
| 70 |
// When browsers support the placeholder attribute, we use that instead of |
| 71 |
// the value property - and this test will fail. |
| 72 |
if (!isPlaceholderSupported()) { |
| 73 |
assertEquals( |
| 74 |
'label is updated', expectedLabel, labelInput.getElement().value); |
| 75 |
} else { |
| 76 |
assertEquals('value is empty', '', labelInput.getElement().value); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
function testPlaceholderAttribute() { |
| 81 |
labelInput = new goog.ui.LabelInput(); |
| 82 |
labelInput.setLabel('search'); |
| 83 |
|
| 84 |
// Some browsers don't support the placeholder attribute, in which case we |
| 85 |
// this test will fail. |
| 86 |
if (!isPlaceholderSupported()) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
labelInput.createDom(); |
| 91 |
labelInput.enterDocument(); |
| 92 |
mockClock.tick(10); |
| 93 |
assertEquals( |
| 94 |
'label should have placeholder attribute \'search\'', 'search', |
| 95 |
labelInput.getElement().placeholder); |
| 96 |
|
| 97 |
labelInput.setLabel('new label'); |
| 98 |
assertEquals( |
| 99 |
'label should have aria-label attribute \'new label\'', 'new label', |
| 100 |
labelInput.getElement().placeholder); |
| 101 |
} |
| 102 |
|
| 103 |
function testDecorateElementWithExistingPlaceholderAttribute() { |
| 104 |
labelInput = new goog.ui.LabelInput(); |
| 105 |
labelInput.setLabel('search'); |
| 106 |
|
| 107 |
labelInput.decorate(goog.dom.getElement('p')); |
| 108 |
labelInput.enterDocument(); |
| 109 |
mockClock.tick(10); |
| 110 |
|
| 111 |
// The presence of an existing placeholder doesn't necessarily mean the |
| 112 |
// browser supports placeholders. Make sure labels are used for browsers |
| 113 |
// without placeholder support: |
| 114 |
if (isPlaceholderSupported()) { |
| 115 |
assertEquals( |
| 116 |
'label should have placeholder attribute \'search\'', 'search', |
| 117 |
labelInput.getElement().placeholder); |
| 118 |
} else { |
| 119 |
assertNotNull(labelInput.getElement()); |
| 120 |
assertEquals('label is rendered', 'search', labelInput.getElement().value); |
| 121 |
assertEquals( |
| 122 |
'label should have aria-label attribute \'search\'', 'search', |
| 123 |
goog.a11y.aria.getState( |
| 124 |
labelInput.getElement(), goog.a11y.aria.State.LABEL)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
function testDecorateElementWithFocus() { |
| 129 |
labelInput = new goog.ui.LabelInput(); |
| 130 |
labelInput.setLabel('search'); |
| 131 |
|
| 132 |
var decoratedElement = goog.dom.getElement('i'); |
| 133 |
|
| 134 |
decoratedElement.value = ''; |
| 135 |
decoratedElement.focus(); |
| 136 |
|
| 137 |
labelInput.decorate(decoratedElement); |
| 138 |
labelInput.enterDocument(); |
| 139 |
mockClock.tick(10); |
| 140 |
|
| 141 |
assertEquals( |
| 142 |
'label for pre-focused input should not have LABEL_CLASS_NAME', -1, |
| 143 |
labelInput.getElement().className.indexOf(labelInput.labelCssClassName)); |
| 144 |
|
| 145 |
if (!isPlaceholderSupported()) { |
| 146 |
assertEquals( |
| 147 |
'label rendered for pre-focused element', '', |
| 148 |
labelInput.getElement().value); |
| 149 |
// NOTE(user): element.blur() doesn't seem to trigger the BLUR event in |
| 150 |
// IE in the test environment. This could be related to the IE issues with |
| 151 |
// testClassName() below. |
| 152 |
goog.testing.events.fireBrowserEvent( |
| 153 |
new goog.testing.events.Event( |
| 154 |
goog.events.EventType.BLUR, decoratedElement)); |
| 155 |
mockClock.tick(10); |
| 156 |
assertEquals( |
| 157 |
'label not rendered for blurred element', 'search', |
| 158 |
labelInput.getElement().value); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
function testDecorateElementWithFocusDelay() { |
| 163 |
if (isPlaceholderSupported()) { |
| 164 |
return; // Delay only affects the older browsers. |
| 165 |
} |
| 166 |
var placeholder = 'search'; |
| 167 |
|
| 168 |
labelInput = new goog.ui.LabelInput(); |
| 169 |
labelInput.setLabel(placeholder); |
| 170 |
var delay = 150; |
| 171 |
labelInput.labelRestoreDelayMs = delay; |
| 172 |
|
| 173 |
var decoratedElement = goog.dom.getElement('i'); |
| 174 |
|
| 175 |
decoratedElement.value = ''; |
| 176 |
decoratedElement.focus(); |
| 177 |
|
| 178 |
labelInput.decorate(decoratedElement); |
| 179 |
labelInput.enterDocument(); |
| 180 |
// wait for all initial setup to settle |
| 181 |
mockClock.tick(delay); |
| 182 |
|
| 183 |
// NOTE(user): element.blur() doesn't seem to trigger the BLUR event in |
| 184 |
// IE in the test environment. This could be related to the IE issues with |
| 185 |
// testClassName() below. |
| 186 |
goog.testing.events.fireBrowserEvent( |
| 187 |
new goog.testing.events.Event( |
| 188 |
goog.events.EventType.BLUR, decoratedElement)); |
| 189 |
|
| 190 |
mockClock.tick(delay - 1); |
| 191 |
assertEquals( |
| 192 |
'label should not be restored before labelRestoreDelay', '', |
| 193 |
labelInput.getElement().value); |
| 194 |
|
| 195 |
mockClock.tick(1); |
| 196 |
assertEquals( |
| 197 |
'label not rendered for blurred element with labelRestoreDelay', |
| 198 |
placeholder, labelInput.getElement().value); |
| 199 |
} |
| 200 |
|
| 201 |
function testClassName() { |
| 202 |
labelInput = new goog.ui.LabelInput(); |
| 203 |
|
| 204 |
// EDGE/IE always fails this test, suspect it is a focus issue. |
| 205 |
if (goog.userAgent.EDGE_OR_IE) { |
| 206 |
return; |
| 207 |
} |
| 208 |
// FF does not perform focus if the window is not active in the first place. |
| 209 |
if (goog.userAgent.GECKO && document.hasFocus && !document.hasFocus()) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
labelInput.decorate(goog.dom.getElement('i')); |
| 214 |
labelInput.setLabel('search'); |
| 215 |
|
| 216 |
var el = labelInput.getElement(); |
| 217 |
assertTrue( |
| 218 |
'label before focus should have LABEL_CLASS_NAME', |
| 219 |
goog.dom.classlist.contains(el, labelInput.labelCssClassName)); |
| 220 |
|
| 221 |
labelInput.getElement().focus(); |
| 222 |
|
| 223 |
assertFalse( |
| 224 |
'label after focus should not have LABEL_CLASS_NAME', |
| 225 |
goog.dom.classlist.contains(el, labelInput.labelCssClassName)); |
| 226 |
|
| 227 |
labelInput.getElement().blur(); |
| 228 |
|
| 229 |
assertTrue( |
| 230 |
'label after blur should have LABEL_CLASS_NAME', |
| 231 |
goog.dom.classlist.contains(el, labelInput.labelCssClassName)); |
| 232 |
} |
| 233 |
|
| 234 |
function isPlaceholderSupported() { |
| 235 |
if (goog.dom.getElement('i').placeholder != null) { |
| 236 |
return true; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
function testEnable() { |
| 241 |
labelInput = new goog.ui.LabelInput(); |
| 242 |
labelInput.createDom(); |
| 243 |
labelInput.enterDocument(); |
| 244 |
|
| 245 |
var labelElement = labelInput.getElement(); |
| 246 |
var disabledClass = goog.getCssName(labelInput.labelCssClassName, 'disabled'); |
| 247 |
|
| 248 |
assertTrue('label should be enabled', labelInput.isEnabled()); |
| 249 |
assertFalse( |
| 250 |
'label should not have the disabled class', |
| 251 |
goog.dom.classlist.contains(labelElement, disabledClass)); |
| 252 |
|
| 253 |
labelInput.setEnabled(false); |
| 254 |
assertFalse('label should be disabled', labelInput.isEnabled()); |
| 255 |
assertTrue( |
| 256 |
'label should have the disabled class', |
| 257 |
goog.dom.classlist.contains(labelElement, disabledClass)); |
| 258 |
|
| 259 |
labelInput.setEnabled(true); |
| 260 |
assertTrue('label should be enabled', labelInput.isEnabled()); |
| 261 |
assertFalse( |
| 262 |
'label should not have the disabled class', |
| 263 |
goog.dom.classlist.contains(labelElement, disabledClass)); |
| 264 |
} |