| 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 |
|
| 15 |
goog.provide('goog.ui.ContainerTest'); |
| 16 |
goog.setTestOnly('goog.ui.ContainerTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.dom.classlist'); |
| 22 |
goog.require('goog.events'); |
| 23 |
goog.require('goog.events.Event'); |
| 24 |
goog.require('goog.events.KeyCodes'); |
| 25 |
goog.require('goog.events.KeyEvent'); |
| 26 |
goog.require('goog.testing.events'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
goog.require('goog.ui.Component'); |
| 29 |
goog.require('goog.ui.Container'); |
| 30 |
goog.require('goog.ui.Control'); |
| 31 |
|
| 32 |
var sandbox; |
| 33 |
var containerElement; |
| 34 |
var container; |
| 35 |
var keyContainer; |
| 36 |
var listContainer; |
| 37 |
|
| 38 |
function setUpPage() { |
| 39 |
sandbox = goog.dom.getElement('sandbox'); |
| 40 |
} |
| 41 |
|
| 42 |
function setUp() { |
| 43 |
container = new goog.ui.Container(); |
| 44 |
keyContainer = null; |
| 45 |
listContainer = null; |
| 46 |
|
| 47 |
sandbox.innerHTML = '<div id="containerElement" class="goog-container">\n' + |
| 48 |
' <div class="goog-control" id="hello">Hello</div>\n' + |
| 49 |
' <div class="goog-control" id="world">World</div>\n' + |
| 50 |
'</div>'; |
| 51 |
containerElement = goog.dom.getElement('containerElement'); |
| 52 |
} |
| 53 |
|
| 54 |
function tearDown() { |
| 55 |
goog.dom.removeChildren(sandbox); |
| 56 |
container.dispose(); |
| 57 |
goog.dispose(keyContainer); |
| 58 |
goog.dispose(listContainer); |
| 59 |
} |
| 60 |
|
| 61 |
function testDecorateHidden() { |
| 62 |
containerElement.style.display = 'none'; |
| 63 |
|
| 64 |
assertTrue('Container must be visible', container.isVisible()); |
| 65 |
container.decorate(containerElement); |
| 66 |
assertFalse('Container must be hidden', container.isVisible()); |
| 67 |
container.forEachChild(function(control) { |
| 68 |
assertTrue( |
| 69 |
'Child control ' + control.getId() + ' must report being ' + |
| 70 |
'visible, even if in a hidden container', |
| 71 |
control.isVisible()); |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
function testDecorateDisabled() { |
| 76 |
goog.dom.classlist.add(containerElement, 'goog-container-disabled'); |
| 77 |
|
| 78 |
assertTrue('Container must be enabled', container.isEnabled()); |
| 79 |
container.decorate(containerElement); |
| 80 |
assertFalse('Container must be disabled', container.isEnabled()); |
| 81 |
container.forEachChild(function(control) { |
| 82 |
assertFalse( |
| 83 |
'Child control ' + control.getId() + ' must be disabled, ' + |
| 84 |
'because the host container is disabled', |
| 85 |
control.isEnabled()); |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
function testDecorateFocusableContainer() { |
| 90 |
container.decorate(containerElement); |
| 91 |
assertTrue('Container must be focusable', container.isFocusable()); |
| 92 |
container.forEachChild(function(control) { |
| 93 |
assertFalse( |
| 94 |
'Child control ' + control.getId() + ' must not be ' + |
| 95 |
'focusable', |
| 96 |
control.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 97 |
}); |
| 98 |
} |
| 99 |
|
| 100 |
function testDecorateFocusableChildrenContainer() { |
| 101 |
container.setFocusable(false); |
| 102 |
container.setFocusableChildrenAllowed(true); |
| 103 |
container.decorate(containerElement); |
| 104 |
assertFalse('Container must not be focusable', container.isFocusable()); |
| 105 |
container.forEachChild(function(control) { |
| 106 |
assertTrue( |
| 107 |
'Child control ' + control.getId() + ' must be ' + |
| 108 |
'focusable', |
| 109 |
control.isSupportedState(goog.ui.Component.State.FOCUSED)); |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
function testHighlightOnEnter() { |
| 114 |
// This interaction test ensures that containers enforce that children |
| 115 |
// get highlighted on mouseover, and that one and only one child may |
| 116 |
// be highlighted at a time. Although integration tests aren't the |
| 117 |
// best, it's difficult to test these event-based interactions due to |
| 118 |
// their disposition toward the "misunderstood contract" problem. |
| 119 |
|
| 120 |
container.decorate(containerElement); |
| 121 |
assertFalse( |
| 122 |
'Child 0 should initially not be highlighted', |
| 123 |
container.getChildAt(0).isHighlighted()); |
| 124 |
|
| 125 |
goog.testing.events.fireMouseOverEvent( |
| 126 |
container.getChildAt(0).getElement(), sandbox); |
| 127 |
assertTrue( |
| 128 |
'Child 0 should become highlighted after a mouse over', |
| 129 |
container.getChildAt(0).isHighlighted()); |
| 130 |
assertEquals( |
| 131 |
'Child 0 should be the active descendant', |
| 132 |
container.getChildAt(0).getElement(), |
| 133 |
goog.a11y.aria.getActiveDescendant(container.getElement())); |
| 134 |
|
| 135 |
goog.testing.events.fireMouseOverEvent( |
| 136 |
container.getChildAt(1).getElement(), |
| 137 |
container.getChildAt(0).getElement()); |
| 138 |
assertFalse( |
| 139 |
'Child 0 should lose highlight when child 1 is moused ' + |
| 140 |
'over, even if no mouseout occurs.', |
| 141 |
container.getChildAt(0).isHighlighted()); |
| 142 |
assertTrue( |
| 143 |
'Child 1 should now be highlighted.', |
| 144 |
container.getChildAt(1).isHighlighted()); |
| 145 |
assertEquals( |
| 146 |
'Child 1 should be the active descendant', |
| 147 |
container.getChildAt(1).getElement(), |
| 148 |
goog.a11y.aria.getActiveDescendant(container.getElement())); |
| 149 |
} |
| 150 |
|
| 151 |
function testHighlightOnEnterPreventable() { |
| 152 |
container.decorate(containerElement); |
| 153 |
goog.events.listen( |
| 154 |
container, goog.ui.Component.EventType.ENTER, |
| 155 |
function(event) { event.preventDefault(); }); |
| 156 |
goog.testing.events.fireMouseOverEvent( |
| 157 |
container.getChildAt(0).getElement(), sandbox); |
| 158 |
assertFalse( |
| 159 |
'Child 0 should not be highlighted if preventDefault called', |
| 160 |
container.getChildAt(0).isHighlighted()); |
| 161 |
} |
| 162 |
|
| 163 |
function testHighlightDisabled() { |
| 164 |
// Another interaction test. Already tested in control_test. |
| 165 |
container.decorate(containerElement); |
| 166 |
container.getChildAt(0).setEnabled(false); |
| 167 |
goog.testing.events.fireMouseOverEvent( |
| 168 |
container.getChildAt(0).getElement(), sandbox); |
| 169 |
assertFalse( |
| 170 |
'Disabled children should not be highlighted', |
| 171 |
container.getChildAt(0).isHighlighted()); |
| 172 |
} |
| 173 |
|
| 174 |
function testGetOwnerControl() { |
| 175 |
container.decorate(containerElement); |
| 176 |
|
| 177 |
assertEquals( |
| 178 |
'Must return appropriate control given an element in the ' + |
| 179 |
'control.', |
| 180 |
container.getChildAt(1), |
| 181 |
container.getOwnerControl(container.getChildAt(1).getElement())); |
| 182 |
|
| 183 |
assertNull( |
| 184 |
'Must return null for element not associated with control.', |
| 185 |
container.getOwnerControl(document.body)); |
| 186 |
assertNull( |
| 187 |
'Must return null if given null node', container.getOwnerControl(null)); |
| 188 |
} |
| 189 |
|
| 190 |
function testShowEvent() { |
| 191 |
container.decorate(containerElement); |
| 192 |
container.setVisible(false); |
| 193 |
var eventFired = false; |
| 194 |
goog.events.listen(container, goog.ui.Component.EventType.SHOW, function() { |
| 195 |
assertFalse( |
| 196 |
'Container must not be visible when SHOW event is ' + |
| 197 |
'fired', |
| 198 |
container.isVisible()); |
| 199 |
eventFired = true; |
| 200 |
}); |
| 201 |
container.setVisible(true); |
| 202 |
assertTrue('SHOW event expected', eventFired); |
| 203 |
} |
| 204 |
|
| 205 |
function testAfterShowEvent() { |
| 206 |
container.decorate(containerElement); |
| 207 |
container.setVisible(false); |
| 208 |
var eventFired = false; |
| 209 |
goog.events.listen( |
| 210 |
container, goog.ui.Container.EventType.AFTER_SHOW, function() { |
| 211 |
assertTrue( |
| 212 |
'Container must be visible when AFTER_SHOW event is ' + |
| 213 |
'fired', |
| 214 |
container.isVisible()); |
| 215 |
eventFired = true; |
| 216 |
}); |
| 217 |
container.setVisible(true); |
| 218 |
assertTrue('AFTER_SHOW event expected', eventFired); |
| 219 |
} |
| 220 |
|
| 221 |
function testHideEvents() { |
| 222 |
var events = []; |
| 223 |
container.decorate(containerElement); |
| 224 |
container.setVisible(true); |
| 225 |
var eventFired = false; |
| 226 |
goog.events.listen(container, goog.ui.Component.EventType.HIDE, function(e) { |
| 227 |
assertTrue( |
| 228 |
'Container must be visible when HIDE event is fired', |
| 229 |
container.isVisible()); |
| 230 |
events.push(e.type); |
| 231 |
}); |
| 232 |
goog.events.listen( |
| 233 |
container, goog.ui.Container.EventType.AFTER_HIDE, function(e) { |
| 234 |
assertFalse( |
| 235 |
'Container must not be visible when AFTER_HIDE event is fired', |
| 236 |
container.isVisible()); |
| 237 |
events.push(e.type); |
| 238 |
}); |
| 239 |
container.setVisible(false); |
| 240 |
assertArrayEquals( |
| 241 |
'HIDE event followed by AFTER_HIDE expected', |
| 242 |
[ |
| 243 |
goog.ui.Component.EventType.HIDE, goog.ui.Container.EventType.AFTER_HIDE |
| 244 |
], |
| 245 |
events); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
/** |
| 251 |
* Test container to which the elements have to be added with |
| 252 |
* {@code container.addChild(element, false)} |
| 253 |
* @constructor |
| 254 |
* @extends {goog.ui.Container} |
| 255 |
*/ |
| 256 |
function ListContainer() { |
| 257 |
goog.ui.Container.call(this); |
| 258 |
} |
| 259 |
goog.inherits(ListContainer, goog.ui.Container); |
| 260 |
|
| 261 |
|
| 262 |
/** @override */ |
| 263 |
ListContainer.prototype.createDom = function() { |
| 264 |
ListContainer.superClass_.createDom.call(this); |
| 265 |
var ul = this.getDomHelper().createDom(goog.dom.TagName.UL); |
| 266 |
this.forEachChild(function(child) { |
| 267 |
child.createDom(); |
| 268 |
var childEl = child.getElement(); |
| 269 |
ul.appendChild( |
| 270 |
this.getDomHelper().createDom(goog.dom.TagName.LI, {}, childEl)); |
| 271 |
}, this); |
| 272 |
this.getContentElement().appendChild(ul); |
| 273 |
}; |
| 274 |
|
| 275 |
function testGetOwnerControlWithNoRenderingInAddChild() { |
| 276 |
listContainer = new ListContainer(); |
| 277 |
var control = new goog.ui.Control('item'); |
| 278 |
listContainer.addChild(control); |
| 279 |
listContainer.render(); |
| 280 |
var ownerControl = listContainer.getOwnerControl(control.getElement()); |
| 281 |
|
| 282 |
assertEquals( |
| 283 |
'Control was added with addChild(control, false)', control, ownerControl); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* Test container for tracking key events being handled. |
| 290 |
* @constructor |
| 291 |
* @extends {goog.ui.Container} |
| 292 |
*/ |
| 293 |
function KeyHandlingContainer() { |
| 294 |
goog.ui.Container.call(this); |
| 295 |
this.keyEventsHandled = 0; |
| 296 |
} |
| 297 |
goog.inherits(KeyHandlingContainer, goog.ui.Container); |
| 298 |
|
| 299 |
|
| 300 |
/** @override */ |
| 301 |
KeyHandlingContainer.prototype.handleKeyEventInternal = function() { |
| 302 |
this.keyEventsHandled++; |
| 303 |
return false; |
| 304 |
}; |
| 305 |
|
| 306 |
function testHandleKeyEvent_onlyHandlesWhenVisible() { |
| 307 |
keyContainer = new KeyHandlingContainer(); |
| 308 |
keyContainer.decorate(containerElement); |
| 309 |
|
| 310 |
keyContainer.setVisible(false); |
| 311 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 312 |
assertEquals( |
| 313 |
'No key events should be handled', 0, keyContainer.keyEventsHandled); |
| 314 |
|
| 315 |
keyContainer.setVisible(true); |
| 316 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 317 |
assertEquals( |
| 318 |
'One key event should be handled', 1, keyContainer.keyEventsHandled); |
| 319 |
} |
| 320 |
|
| 321 |
function testHandleKeyEvent_onlyHandlesWhenEnabled() { |
| 322 |
keyContainer = new KeyHandlingContainer(); |
| 323 |
keyContainer.decorate(containerElement); |
| 324 |
keyContainer.setVisible(true); |
| 325 |
|
| 326 |
keyContainer.setEnabled(false); |
| 327 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 328 |
assertEquals( |
| 329 |
'No key events should be handled', 0, keyContainer.keyEventsHandled); |
| 330 |
|
| 331 |
keyContainer.setEnabled(true); |
| 332 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 333 |
assertEquals( |
| 334 |
'One key event should be handled', 1, keyContainer.keyEventsHandled); |
| 335 |
} |
| 336 |
|
| 337 |
function testHandleKeyEvent_childlessContainersIgnoreKeyEvents() { |
| 338 |
keyContainer = new KeyHandlingContainer(); |
| 339 |
keyContainer.render(); |
| 340 |
keyContainer.setVisible(true); |
| 341 |
|
| 342 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 343 |
assertEquals( |
| 344 |
'No key events should be handled', 0, keyContainer.keyEventsHandled); |
| 345 |
|
| 346 |
keyContainer.addChild(new goog.ui.Control()); |
| 347 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 348 |
assertEquals( |
| 349 |
'One key event should be handled', 1, keyContainer.keyEventsHandled); |
| 350 |
} |
| 351 |
|
| 352 |
function testHandleKeyEvent_alwaysHandlesWithKeyEventTarget() { |
| 353 |
keyContainer = new KeyHandlingContainer(); |
| 354 |
keyContainer.render(); |
| 355 |
keyContainer.setKeyEventTarget(goog.dom.createDom(goog.dom.TagName.DIV)); |
| 356 |
keyContainer.setVisible(true); |
| 357 |
|
| 358 |
keyContainer.handleKeyEvent(new goog.events.Event()); |
| 359 |
assertEquals( |
| 360 |
'One key events should be handled', 1, keyContainer.keyEventsHandled); |
| 361 |
} |
| 362 |
|
| 363 |
function testHandleKeyEventInternal_onlyHandlesUnmodified() { |
| 364 |
container.setKeyEventTarget(sandbox); |
| 365 |
var event = |
| 366 |
new goog.events.KeyEvent(goog.events.KeyCodes.ESC, 0, false, null); |
| 367 |
|
| 368 |
var propertyNames = ['shiftKey', 'altKey', 'ctrlKey', 'metaKey']; |
| 369 |
|
| 370 |
// Verify that the event is not handled whenever a modifier key is true. |
| 371 |
for (var i = 0, propertyName; propertyName = propertyNames[i]; i++) { |
| 372 |
assertTrue( |
| 373 |
'Event should be handled when modifer key is not pressed.', |
| 374 |
container.handleKeyEventInternal(event)); |
| 375 |
event[propertyName] = true; |
| 376 |
assertFalse( |
| 377 |
'Event should not be handled when modifer key is pressed.', |
| 378 |
container.handleKeyEventInternal(event)); |
| 379 |
event[propertyName] = false; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
function testOpenFollowsHighlight() { |
| 384 |
container.decorate(containerElement); |
| 385 |
container.setOpenFollowsHighlight(true); |
| 386 |
assertTrue( |
| 387 |
'isOpenFollowsHighlight should return true', |
| 388 |
container.isOpenFollowsHighlight()); |
| 389 |
|
| 390 |
// Make the children openable. |
| 391 |
container.forEachChild(function(child) { |
| 392 |
child.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 393 |
}); |
| 394 |
// Open child 1 initially. |
| 395 |
container.getChildAt(1).setOpen(true); |
| 396 |
|
| 397 |
assertFalse( |
| 398 |
'Child 0 should initially not be highlighted', |
| 399 |
container.getChildAt(0).isHighlighted()); |
| 400 |
goog.testing.events.fireMouseOverEvent( |
| 401 |
container.getChildAt(0).getElement(), sandbox); |
| 402 |
assertTrue( |
| 403 |
'Child 0 should become highlighted after a mouse over', |
| 404 |
container.getChildAt(0).isHighlighted()); |
| 405 |
assertTrue( |
| 406 |
'Child 0 should become open after higlighted', |
| 407 |
container.getChildAt(0).isOpen()); |
| 408 |
assertFalse( |
| 409 |
'Child 1 should become closed once 0 is open', |
| 410 |
container.getChildAt(1).isOpen()); |
| 411 |
assertEquals( |
| 412 |
'OpenItem should be child 0', container.getChildAt(0), |
| 413 |
container.getOpenItem()); |
| 414 |
} |
| 415 |
|
| 416 |
function testOpenNotFollowsHighlight() { |
| 417 |
container.decorate(containerElement); |
| 418 |
container.setOpenFollowsHighlight(false); |
| 419 |
assertFalse( |
| 420 |
'isOpenFollowsHighlight should return false', |
| 421 |
container.isOpenFollowsHighlight()); |
| 422 |
|
| 423 |
// Make the children openable. |
| 424 |
container.forEachChild(function(child) { |
| 425 |
child.setSupportedState(goog.ui.Component.State.OPENED, true); |
| 426 |
}); |
| 427 |
// Open child 1 initially. |
| 428 |
container.getChildAt(1).setOpen(true); |
| 429 |
|
| 430 |
assertFalse( |
| 431 |
'Child 0 should initially not be highlighted', |
| 432 |
container.getChildAt(0).isHighlighted()); |
| 433 |
goog.testing.events.fireMouseOverEvent( |
| 434 |
container.getChildAt(0).getElement(), sandbox); |
| 435 |
assertTrue( |
| 436 |
'Child 0 should become highlighted after a mouse over', |
| 437 |
container.getChildAt(0).isHighlighted()); |
| 438 |
assertFalse( |
| 439 |
'Child 0 should remain closed after higlighted', |
| 440 |
container.getChildAt(0).isOpen()); |
| 441 |
assertTrue('Child 1 should remain open', container.getChildAt(1).isOpen()); |
| 442 |
assertEquals( |
| 443 |
'OpenItem should be child 1', container.getChildAt(1), |
| 444 |
container.getOpenItem()); |
| 445 |
} |
| 446 |
|
| 447 |
function testRemoveChild() { |
| 448 |
goog.dom.removeChildren(containerElement); |
| 449 |
container.decorate(containerElement); |
| 450 |
|
| 451 |
var a = new goog.ui.Control('A'); |
| 452 |
var b = new goog.ui.Control('B'); |
| 453 |
var c = new goog.ui.Control('C'); |
| 454 |
|
| 455 |
a.setId('a'); |
| 456 |
b.setId('b'); |
| 457 |
c.setId('c'); |
| 458 |
|
| 459 |
container.addChild(a, true); |
| 460 |
container.addChild(b, true); |
| 461 |
container.addChild(c, true); |
| 462 |
|
| 463 |
container.setHighlightedIndex(2); |
| 464 |
|
| 465 |
assertEquals( |
| 466 |
'Parent must remove and return child by ID', b, |
| 467 |
container.removeChild('b')); |
| 468 |
assertNull( |
| 469 |
'Parent must no longer contain this child', container.getChild('b')); |
| 470 |
assertEquals( |
| 471 |
'Highlighted index must be decreased', 1, |
| 472 |
container.getHighlightedIndex()); |
| 473 |
assertTrue( |
| 474 |
'The removed control must handle its own mouse events', |
| 475 |
b.isHandleMouseEvents()); |
| 476 |
|
| 477 |
assertEquals( |
| 478 |
'Parent must remove and return child', c, container.removeChild(c)); |
| 479 |
assertNull( |
| 480 |
'Parent must no longer contain this child', container.getChild('c')); |
| 481 |
assertFalse('This child must no longer be highlighted', c.isHighlighted()); |
| 482 |
assertTrue( |
| 483 |
'The removed control must handle its own mouse events', |
| 484 |
c.isHandleMouseEvents()); |
| 485 |
|
| 486 |
assertEquals( |
| 487 |
'Parent must remove and return child by index', a, |
| 488 |
container.removeChildAt(0)); |
| 489 |
assertNull( |
| 490 |
'Parent must no longer contain this child', container.getChild('a')); |
| 491 |
assertTrue( |
| 492 |
'The removed control must handle its own mouse events', |
| 493 |
a.isHandleMouseEvents()); |
| 494 |
} |
| 495 |
|
| 496 |
function testRemoveHighlightedDisposedChild() { |
| 497 |
goog.dom.removeChildren(containerElement); |
| 498 |
container.decorate(containerElement); |
| 499 |
|
| 500 |
var a = new goog.ui.Control('A'); |
| 501 |
container.addChild(a, true); |
| 502 |
|
| 503 |
container.setHighlightedIndex(0); |
| 504 |
a.dispose(); |
| 505 |
container.removeChild(a); |
| 506 |
container.dispose(); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
/** |
| 511 |
* Checks that getHighlighted() returns the expected value and checks |
| 512 |
* that the child at this index is highlighted and other children are not. |
| 513 |
* @param {string} explanation Message indicating what is expected. |
| 514 |
* @param {number} index Expected return value of getHighlightedIndex(). |
| 515 |
*/ |
| 516 |
function assertHighlightedIndex(explanation, index) { |
| 517 |
assertEquals(explanation, index, container.getHighlightedIndex()); |
| 518 |
for (var i = 0; i < container.getChildCount(); i++) { |
| 519 |
if (i == index) { |
| 520 |
assertTrue( |
| 521 |
'Child at highlighted index should be highlighted', |
| 522 |
container.getChildAt(i).isHighlighted()); |
| 523 |
} else { |
| 524 |
assertFalse( |
| 525 |
'Only child at highlighted index should be highlighted', |
| 526 |
container.getChildAt(i).isHighlighted()); |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
function testUpdateHighlightedIndex_updatesWhenChildrenAreAdded() { |
| 532 |
goog.dom.removeChildren(containerElement); |
| 533 |
container.decorate(containerElement); |
| 534 |
|
| 535 |
var a = new goog.ui.Control('A'); |
| 536 |
var b = new goog.ui.Control('B'); |
| 537 |
var c = new goog.ui.Control('C'); |
| 538 |
|
| 539 |
container.addChild(a); |
| 540 |
container.setHighlightedIndex(0); |
| 541 |
assertHighlightedIndex('Highlighted index should match set value', 0); |
| 542 |
|
| 543 |
// Add child before the highlighted one. |
| 544 |
container.addChildAt(b, 0); |
| 545 |
assertHighlightedIndex('Highlighted index should be increased', 1); |
| 546 |
|
| 547 |
// Add child after the highlighted one. |
| 548 |
container.addChildAt(c, 2); |
| 549 |
assertHighlightedIndex('Highlighted index should not change', 1); |
| 550 |
|
| 551 |
container.dispose(); |
| 552 |
} |
| 553 |
|
| 554 |
function testUpdateHighlightedIndex_updatesWhenChildrenAreMoved() { |
| 555 |
goog.dom.removeChildren(containerElement); |
| 556 |
container.decorate(containerElement); |
| 557 |
|
| 558 |
var a = new goog.ui.Control('A'); |
| 559 |
var b = new goog.ui.Control('B'); |
| 560 |
var c = new goog.ui.Control('C'); |
| 561 |
|
| 562 |
container.addChild(a); |
| 563 |
container.addChild(b); |
| 564 |
container.addChild(c); |
| 565 |
|
| 566 |
// Highlight 'c' and swap 'a' and 'b' |
| 567 |
// [a, b, c] -> [a, b, *c] -> [b, a, *c] (* indicates the highlighted child) |
| 568 |
container.setHighlightedIndex(2); |
| 569 |
container.addChildAt(a, 1, false); |
| 570 |
assertHighlightedIndex('Highlighted index should not change', 2); |
| 571 |
|
| 572 |
// Move the highlighted child 'c' from index 2 to index 1. |
| 573 |
// [b, a, *c] -> [b, *c, a] |
| 574 |
container.addChildAt(c, 1, false); |
| 575 |
assertHighlightedIndex('Highlighted index must follow the moved child', 1); |
| 576 |
|
| 577 |
// Take the element in front of the highlighted index and move it behind it. |
| 578 |
// [b, *c, a] -> [*c, a, b] |
| 579 |
container.addChildAt(b, 2, false); |
| 580 |
assertHighlightedIndex('Highlighted index must be decreased', 0); |
| 581 |
|
| 582 |
// And move the element back to the front. |
| 583 |
// [*c, a, b] -> [b, *c, a] |
| 584 |
container.addChildAt(b, 0, false); |
| 585 |
assertHighlightedIndex('Highlighted index must be increased', 1); |
| 586 |
|
| 587 |
container.dispose(); |
| 588 |
} |
| 589 |
|
| 590 |
function testUpdateHighlightedIndex_notChangedOnNoOp() { |
| 591 |
goog.dom.removeChildren(containerElement); |
| 592 |
container.decorate(containerElement); |
| 593 |
|
| 594 |
container.addChild(new goog.ui.Control('A')); |
| 595 |
container.addChild(new goog.ui.Control('B')); |
| 596 |
container.setHighlightedIndex(1); |
| 597 |
|
| 598 |
// Re-add a child to its current position. |
| 599 |
container.addChildAt(container.getChildAt(0), 0, false); |
| 600 |
assertHighlightedIndex('Highlighted index must not change', 1); |
| 601 |
|
| 602 |
container.dispose(); |
| 603 |
} |
| 604 |
|
| 605 |
function testUpdateHighlightedIndex_notChangedWhenNoChildSelected() { |
| 606 |
goog.dom.removeChildren(containerElement); |
| 607 |
container.decorate(containerElement); |
| 608 |
|
| 609 |
var a = new goog.ui.Control('A'); |
| 610 |
var b = new goog.ui.Control('B'); |
| 611 |
var c = new goog.ui.Control('C'); |
| 612 |
container.addChild(a); |
| 613 |
container.addChild(b); |
| 614 |
container.addChild(c); |
| 615 |
|
| 616 |
// Move children around. |
| 617 |
container.addChildAt(a, 2, false); |
| 618 |
container.addChildAt(b, 1, false); |
| 619 |
container.addChildAt(c, 2, false); |
| 620 |
|
| 621 |
assertHighlightedIndex('Highlighted index must not change', -1); |
| 622 |
|
| 623 |
container.dispose(); |
| 624 |
} |
| 625 |
|
| 626 |
function testUpdateHighlightedIndex_indexStaysInBoundsWhenMovedToMaxIndex() { |
| 627 |
goog.dom.removeChildren(containerElement); |
| 628 |
container.decorate(containerElement); |
| 629 |
|
| 630 |
var a = new goog.ui.Control('A'); |
| 631 |
var b = new goog.ui.Control('B'); |
| 632 |
container.addChild(a); |
| 633 |
container.addChild(b); |
| 634 |
|
| 635 |
// Move higlighted child to an index one behind last child. |
| 636 |
container.setHighlightedIndex(0); |
| 637 |
container.addChildAt(a, 2); |
| 638 |
|
| 639 |
assertEquals('Child should be moved to index 1', a, container.getChildAt(1)); |
| 640 |
assertEquals('Child count should not change', 2, container.getChildCount()); |
| 641 |
assertHighlightedIndex('Highlighted index must point to new index', 1); |
| 642 |
|
| 643 |
container.dispose(); |
| 644 |
} |