| 1 |
// Copyright 2010 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.SelectTest'); |
| 16 |
goog.setTestOnly('goog.ui.SelectTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.Role'); |
| 20 |
goog.require('goog.a11y.aria.State'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.events'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
goog.require('goog.testing.recordFunction'); |
| 25 |
goog.require('goog.ui.Component'); |
| 26 |
goog.require('goog.ui.CustomButtonRenderer'); |
| 27 |
goog.require('goog.ui.Menu'); |
| 28 |
goog.require('goog.ui.MenuItem'); |
| 29 |
goog.require('goog.ui.Select'); |
| 30 |
goog.require('goog.ui.Separator'); |
| 31 |
|
| 32 |
var defaultCaption = 'initial caption'; |
| 33 |
var sandboxEl; |
| 34 |
var select; |
| 35 |
|
| 36 |
function setUp() { |
| 37 |
sandboxEl = goog.dom.getElement('sandbox'); |
| 38 |
select = new goog.ui.Select(defaultCaption); |
| 39 |
} |
| 40 |
|
| 41 |
function tearDown() { |
| 42 |
select.dispose(); |
| 43 |
goog.dom.removeChildren(sandboxEl); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Checks that the default caption passed in the constructor and in the setter |
| 49 |
* is returned by getDefaultCaption, and acts as a default caption, i.e. is |
| 50 |
* shown as a caption when no items are selected. |
| 51 |
*/ |
| 52 |
function testDefaultCaption() { |
| 53 |
select.render(sandboxEl); |
| 54 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 55 |
select.addItem(item1); |
| 56 |
select.addItem(new goog.ui.MenuItem('item 2')); |
| 57 |
assertEquals(defaultCaption, select.getDefaultCaption()); |
| 58 |
assertEquals(defaultCaption, select.getCaption()); |
| 59 |
|
| 60 |
var newCaption = 'new caption'; |
| 61 |
select.setDefaultCaption(newCaption); |
| 62 |
assertEquals(newCaption, select.getDefaultCaption()); |
| 63 |
assertEquals(newCaption, select.getCaption()); |
| 64 |
|
| 65 |
select.setSelectedItem(item1); |
| 66 |
assertNotEquals(newCaption, select.getCaption()); |
| 67 |
|
| 68 |
select.setSelectedItem(null); |
| 69 |
assertEquals(newCaption, select.getCaption()); |
| 70 |
} |
| 71 |
|
| 72 |
function testNoDefaultCaption() { |
| 73 |
assertNull(new goog.ui.Select().getDefaultCaption()); |
| 74 |
assertEquals('', new goog.ui.Select('').getDefaultCaption()); |
| 75 |
} |
| 76 |
|
| 77 |
// Confirms that aria roles for select conform to spec: |
| 78 |
// http://www.w3.org/TR/wai-aria/roles#listbox |
| 79 |
// Basically the select should have a role of LISTBOX and all the items should |
| 80 |
// have a role of OPTION. |
| 81 |
function testAriaRoles() { |
| 82 |
select.render(sandboxEl); |
| 83 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 84 |
select.addItem(item1); |
| 85 |
// Added a separator to make sure that the SETSIZE ignores the separator |
| 86 |
// items. |
| 87 |
var separator = new goog.ui.Separator(); |
| 88 |
select.addItem(separator); |
| 89 |
var item2 = new goog.ui.MenuItem('item 2'); |
| 90 |
select.addItem(item2); |
| 91 |
assertNotNull(select.getElement()); |
| 92 |
assertNotNull(item1.getElement()); |
| 93 |
assertNotNull(item2.getElement()); |
| 94 |
assertEquals( |
| 95 |
goog.a11y.aria.Role.LISTBOX, goog.a11y.aria.getRole(select.getElement())); |
| 96 |
assertEquals( |
| 97 |
goog.a11y.aria.Role.OPTION, goog.a11y.aria.getRole(item1.getElement())); |
| 98 |
assertEquals( |
| 99 |
goog.a11y.aria.Role.OPTION, goog.a11y.aria.getRole(item2.getElement())); |
| 100 |
assertNotNull( |
| 101 |
goog.a11y.aria.getState( |
| 102 |
select.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT)); |
| 103 |
var contentElement = |
| 104 |
select.getRenderer().getContentElement(select.getElement()); |
| 105 |
assertEquals( |
| 106 |
'2', |
| 107 |
goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.SETSIZE)); |
| 108 |
assertEquals( |
| 109 |
'0', |
| 110 |
goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.POSINSET)); |
| 111 |
select.setSelectedItem(item1); |
| 112 |
assertEquals( |
| 113 |
'1', |
| 114 |
goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.POSINSET)); |
| 115 |
select.setSelectedItem(item2); |
| 116 |
assertEquals( |
| 117 |
'2', |
| 118 |
goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.POSINSET)); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Checks that the select control handles ACTION events from its items. |
| 124 |
*/ |
| 125 |
function testHandlesItemActions() { |
| 126 |
select.render(sandboxEl); |
| 127 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 128 |
var item2 = new goog.ui.MenuItem('item 2'); |
| 129 |
select.addItem(item1); |
| 130 |
select.addItem(item2); |
| 131 |
|
| 132 |
item1.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 133 |
assertEquals(item1, select.getSelectedItem()); |
| 134 |
assertEquals(item1.getCaption(), select.getCaption()); |
| 135 |
|
| 136 |
item2.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 137 |
assertEquals(item2, select.getSelectedItem()); |
| 138 |
assertEquals(item2.getCaption(), select.getCaption()); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Tests goog.ui.Select.prototype.setValue. |
| 144 |
*/ |
| 145 |
function testSetValue() { |
| 146 |
select.render(sandboxEl); |
| 147 |
var item1 = new goog.ui.MenuItem('item 1', 1); |
| 148 |
var item2 = new goog.ui.MenuItem('item 2', 2); |
| 149 |
select.addItem(item1); |
| 150 |
select.addItem(item2); |
| 151 |
|
| 152 |
select.setValue(1); |
| 153 |
assertEquals(item1, select.getSelectedItem()); |
| 154 |
|
| 155 |
select.setValue(2); |
| 156 |
assertEquals(item2, select.getSelectedItem()); |
| 157 |
|
| 158 |
select.setValue(3); |
| 159 |
assertNull(select.getSelectedItem()); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Checks that the current selection is cleared when the selected item is |
| 165 |
* removed. |
| 166 |
*/ |
| 167 |
function testSelectionIsClearedWhenSelectedItemIsRemoved() { |
| 168 |
select.render(sandboxEl); |
| 169 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 170 |
select.addItem(item1); |
| 171 |
select.addItem(new goog.ui.MenuItem('item 2')); |
| 172 |
|
| 173 |
select.setSelectedItem(item1); |
| 174 |
select.removeItem(item1); |
| 175 |
assertNull(select.getSelectedItem()); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Check that the select control is subscribed to its selection model events |
| 181 |
* after being added, removed and added back again into the document. |
| 182 |
*/ |
| 183 |
function testExitAndEnterDocument() { |
| 184 |
var component = new goog.ui.Component(); |
| 185 |
component.render(sandboxEl); |
| 186 |
|
| 187 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 188 |
var item2 = new goog.ui.MenuItem('item 2'); |
| 189 |
var item3 = new goog.ui.MenuItem('item 3'); |
| 190 |
|
| 191 |
select.addItem(item1); |
| 192 |
select.addItem(item2); |
| 193 |
select.addItem(item3); |
| 194 |
|
| 195 |
component.addChild(select, true); |
| 196 |
item2.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 197 |
assertEquals(item2.getCaption(), select.getCaption()); |
| 198 |
|
| 199 |
component.removeChild(select, true); |
| 200 |
item1.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 201 |
assertEquals(item2.getCaption(), select.getCaption()); |
| 202 |
|
| 203 |
component.addChild(select, true); |
| 204 |
item3.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 205 |
assertEquals(item3.getCaption(), select.getCaption()); |
| 206 |
} |
| 207 |
|
| 208 |
function testSelectEventFiresForProgrammaticChange() { |
| 209 |
select.render(); |
| 210 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 211 |
var item2 = new goog.ui.MenuItem('item 2'); |
| 212 |
select.addItem(item1); |
| 213 |
select.addItem(item2); |
| 214 |
|
| 215 |
var recordingHandler = new goog.testing.recordFunction(); |
| 216 |
goog.events.listen( |
| 217 |
select, goog.ui.Component.EventType.CHANGE, recordingHandler); |
| 218 |
|
| 219 |
select.setSelectedItem(item2); |
| 220 |
assertEquals( |
| 221 |
'Selecting new item should fire CHANGE event.', 1, |
| 222 |
recordingHandler.getCallCount()); |
| 223 |
|
| 224 |
select.setSelectedItem(item2); |
| 225 |
assertEquals( |
| 226 |
'Selecting the same item should not fire CHANGE event.', 1, |
| 227 |
recordingHandler.getCallCount()); |
| 228 |
|
| 229 |
select.setSelectedIndex(0); |
| 230 |
assertEquals( |
| 231 |
'Selecting new item should fire CHANGE event.', 2, |
| 232 |
recordingHandler.getCallCount()); |
| 233 |
|
| 234 |
select.setSelectedIndex(0); |
| 235 |
assertEquals( |
| 236 |
'Selecting the same item should not fire CHANGE event.', 2, |
| 237 |
recordingHandler.getCallCount()); |
| 238 |
} |
| 239 |
|
| 240 |
function testSelectEventFiresForUserInitiatedAction() { |
| 241 |
select.render(); |
| 242 |
var item1 = new goog.ui.MenuItem('item 1'); |
| 243 |
var item2 = new goog.ui.MenuItem('item 2'); |
| 244 |
select.addItem(item1); |
| 245 |
select.addItem(item2); |
| 246 |
|
| 247 |
var recordingHandler = new goog.testing.recordFunction(); |
| 248 |
goog.events.listen( |
| 249 |
select, goog.ui.Component.EventType.CHANGE, recordingHandler); |
| 250 |
|
| 251 |
select.setOpen(true); |
| 252 |
|
| 253 |
item2.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 254 |
assertEquals( |
| 255 |
'Selecting new item should fire CHANGE event.', 1, |
| 256 |
recordingHandler.getCallCount()); |
| 257 |
assertFalse(select.isOpen()); |
| 258 |
|
| 259 |
select.setOpen(true); |
| 260 |
|
| 261 |
item2.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 262 |
assertEquals( |
| 263 |
'Selecting the same item should not fire CHANGE event.', 1, |
| 264 |
recordingHandler.getCallCount()); |
| 265 |
assertFalse(select.isOpen()); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Checks that if an item is selected before decorate is called, the selection |
| 271 |
* is preserved after decorate. |
| 272 |
*/ |
| 273 |
function testSetSelectedItemBeforeRender() { |
| 274 |
select.addItem(new goog.ui.MenuItem('item 1')); |
| 275 |
select.addItem(new goog.ui.MenuItem('item 2')); |
| 276 |
var item3 = new goog.ui.MenuItem('item 3'); |
| 277 |
select.addItem(item3); |
| 278 |
select.setSelectedItem(item3); |
| 279 |
assertEquals(2, select.getSelectedIndex()); |
| 280 |
|
| 281 |
select.decorate(sandboxEl); |
| 282 |
assertEquals(2, select.getSelectedIndex()); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
/** |
| 287 |
* Checks that if a value is set before decorate is called, the value is |
| 288 |
* preserved after decorate. |
| 289 |
*/ |
| 290 |
function testSetValueBeforeRender() { |
| 291 |
select.addItem(new goog.ui.MenuItem('item 1', 1)); |
| 292 |
select.addItem(new goog.ui.MenuItem('item 2', 2)); |
| 293 |
select.setValue(2); |
| 294 |
assertEquals(2, select.getValue()); |
| 295 |
|
| 296 |
select.decorate(sandboxEl); |
| 297 |
assertEquals(2, select.getValue()); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
function testUpdateCaption_aria() { |
| 302 |
select.render(sandboxEl); |
| 303 |
|
| 304 |
// Verify default state. |
| 305 |
assertEquals(defaultCaption, select.getCaption()); |
| 306 |
assertFalse( |
| 307 |
!!goog.a11y.aria.getLabel( |
| 308 |
select.getRenderer().getContentElement(select.getElement()))); |
| 309 |
|
| 310 |
// Add and select an item with aria-label. |
| 311 |
var item1 = new goog.ui.MenuItem(); |
| 312 |
select.addItem(item1); |
| 313 |
item1.getElement().setAttribute('aria-label', 'item1'); |
| 314 |
select.setSelectedIndex(0); |
| 315 |
assertEquals( |
| 316 |
'item1', |
| 317 |
goog.a11y.aria.getLabel( |
| 318 |
select.getRenderer().getContentElement(select.getElement()))); |
| 319 |
|
| 320 |
// Add and select an item without a label. |
| 321 |
var item2 = new goog.ui.MenuItem(); |
| 322 |
select.addItem(item2); |
| 323 |
select.setSelectedIndex(1); |
| 324 |
assertFalse( |
| 325 |
!!goog.a11y.aria.getLabel( |
| 326 |
select.getRenderer().getContentElement(select.getElement()))); |
| 327 |
} |
| 328 |
|
| 329 |
function testDisposeWhenInnerHTMLHasBeenClearedInIE10() { |
| 330 |
assertNotThrows(function() { |
| 331 |
var customSelect = new goog.ui.Select( |
| 332 |
null /* label */, new goog.ui.Menu(), |
| 333 |
new goog.ui.CustomButtonRenderer()); |
| 334 |
customSelect.render(sandboxEl); |
| 335 |
|
| 336 |
// In IE10 setting the innerHTML of a node invalidates the parent child |
| 337 |
// relation of all its child nodes (unlike removeNode). |
| 338 |
goog.dom.removeChildren(sandboxEl); |
| 339 |
|
| 340 |
// goog.ui.Select's disposeInternal trigger's goog.ui.Component's |
| 341 |
// disposeInternal, which triggers goog.ui.MenuButton's exitDocument, |
| 342 |
// which closes the associated menu and updates the activeDescendent. |
| 343 |
// In the case of a CustomMenuButton the contentElement is referenced by |
| 344 |
// element.firstChild.firstChild, an invalid relation in IE 10. |
| 345 |
customSelect.dispose(); |
| 346 |
}); |
| 347 |
} |