| 1 |
// Copyright 2007 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.ComponentTest'); |
| 16 |
goog.setTestOnly('goog.ui.ComponentTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.DomHelper'); |
| 20 |
goog.require('goog.dom.NodeType'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.events.EventTarget'); |
| 23 |
goog.require('goog.testing.PropertyReplacer'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.ui.Component'); |
| 26 |
|
| 27 |
var component; |
| 28 |
var propertyReplacer = new goog.testing.PropertyReplacer(); |
| 29 |
var sandbox; |
| 30 |
|
| 31 |
function setUp() { |
| 32 |
sandbox = goog.dom.getElement('sandbox'); |
| 33 |
component = new goog.ui.Component(); |
| 34 |
} |
| 35 |
|
| 36 |
function tearDown() { |
| 37 |
component.dispose(); |
| 38 |
goog.dom.removeChildren(sandbox); |
| 39 |
propertyReplacer.reset(); |
| 40 |
} |
| 41 |
|
| 42 |
function testConstructor() { |
| 43 |
assertTrue( |
| 44 |
'Instance must be non-null and have the expected class', |
| 45 |
component instanceof goog.ui.Component); |
| 46 |
assertTrue( |
| 47 |
'DOM helper must be non-null and have the expected class', |
| 48 |
component.dom_ instanceof goog.dom.DomHelper); |
| 49 |
|
| 50 |
var fakeDom = {}; |
| 51 |
var otherComponent = new goog.ui.Component(fakeDom); |
| 52 |
assertEquals( |
| 53 |
'DOM helper must refer to expected object', fakeDom, otherComponent.dom_); |
| 54 |
|
| 55 |
otherComponent.dispose(); |
| 56 |
} |
| 57 |
|
| 58 |
function testGetId() { |
| 59 |
assertNull('Component ID should be initialized to null', component.id_); |
| 60 |
var id = component.getId(); |
| 61 |
assertNotNull('Component ID should be generated on demand', id); |
| 62 |
assertEquals( |
| 63 |
'Subsequent calls to getId() must return same value', id, |
| 64 |
component.getId()); |
| 65 |
} |
| 66 |
|
| 67 |
function testSetId() { |
| 68 |
component.setId('myId'); |
| 69 |
assertEquals( |
| 70 |
'getId() must return explicitly set ID', 'myId', component.getId()); |
| 71 |
|
| 72 |
var child = new goog.ui.Component(); |
| 73 |
var childId = child.getId(); |
| 74 |
component.addChild(child); |
| 75 |
assertEquals( |
| 76 |
'Parent component must find child by ID', child, |
| 77 |
component.getChild(childId)); |
| 78 |
|
| 79 |
child.setId('someNewId'); |
| 80 |
assertEquals( |
| 81 |
'Parent component must find child by new ID', child, |
| 82 |
component.getChild('someNewId')); |
| 83 |
|
| 84 |
child.dispose(); |
| 85 |
} |
| 86 |
|
| 87 |
function testGetSetElement() { |
| 88 |
assertNull('Element must be null by default', component.getElement()); |
| 89 |
var element = goog.dom.createElement(goog.dom.TagName.DIV); |
| 90 |
component.setElementInternal(element); |
| 91 |
assertEquals( |
| 92 |
'getElement() must return expected element', element, |
| 93 |
component.getElement()); |
| 94 |
} |
| 95 |
|
| 96 |
function testGetSetParent() { |
| 97 |
assertNull('Parent must be null by default', component.getParent()); |
| 98 |
|
| 99 |
var parent = new goog.ui.Component(); |
| 100 |
component.setParent(parent); |
| 101 |
assertEquals( |
| 102 |
'getParent() must return expected component', parent, |
| 103 |
component.getParent()); |
| 104 |
|
| 105 |
component.setParent(null); |
| 106 |
assertNull('Parent must be null', component.getParent()); |
| 107 |
|
| 108 |
assertThrows( |
| 109 |
'Setting a component\'s parent to itself must throw error', |
| 110 |
function() { component.setParent(component); }); |
| 111 |
|
| 112 |
parent.addChild(component); |
| 113 |
assertEquals( |
| 114 |
'getParent() must return expected component', parent, |
| 115 |
component.getParent()); |
| 116 |
assertThrows( |
| 117 |
'Changing a child component\'s parent must throw error', |
| 118 |
function() { component.setParent(new goog.ui.Component()); }); |
| 119 |
|
| 120 |
parent.dispose(); |
| 121 |
} |
| 122 |
|
| 123 |
function testGetParentEventTarget() { |
| 124 |
assertNull( |
| 125 |
'Parent event target must be null by default', |
| 126 |
component.getParentEventTarget()); |
| 127 |
|
| 128 |
var parent = new goog.ui.Component(); |
| 129 |
component.setParent(parent); |
| 130 |
assertEquals( |
| 131 |
'Parent event target must be the parent component', parent, |
| 132 |
component.getParentEventTarget()); |
| 133 |
assertThrows( |
| 134 |
'Directly setting the parent event target to other than ' + |
| 135 |
'the parent component when the parent component is set must throw ' + |
| 136 |
'error', |
| 137 |
function() { component.setParentEventTarget(new goog.ui.Component()); }); |
| 138 |
|
| 139 |
parent.dispose(); |
| 140 |
} |
| 141 |
|
| 142 |
function testSetParentEventTarget() { |
| 143 |
var parentEventTarget = new goog.events.EventTarget(); |
| 144 |
component.setParentEventTarget(parentEventTarget); |
| 145 |
assertEquals('Parent component must be null', null, component.getParent()); |
| 146 |
|
| 147 |
parentEventTarget.dispose(); |
| 148 |
} |
| 149 |
|
| 150 |
function testGetDomHelper() { |
| 151 |
var domHelper = new goog.dom.DomHelper(); |
| 152 |
var component = new goog.ui.Component(domHelper); |
| 153 |
assertEquals( |
| 154 |
'Component must return the same DomHelper passed', domHelper, |
| 155 |
component.getDomHelper()); |
| 156 |
} |
| 157 |
|
| 158 |
function testIsInDocument() { |
| 159 |
assertFalse( |
| 160 |
'Component must not be in the document by default', |
| 161 |
component.isInDocument()); |
| 162 |
component.enterDocument(); |
| 163 |
assertTrue('Component must be in the document', component.isInDocument()); |
| 164 |
} |
| 165 |
|
| 166 |
function testCreateDom() { |
| 167 |
assertNull('Component must not have DOM by default', component.getElement()); |
| 168 |
component.createDom(); |
| 169 |
assertEquals( |
| 170 |
'Component\'s DOM must be an element node', goog.dom.NodeType.ELEMENT, |
| 171 |
component.getElement().nodeType); |
| 172 |
} |
| 173 |
|
| 174 |
function testRender() { |
| 175 |
assertFalse( |
| 176 |
'Component must not be in the document by default', |
| 177 |
component.isInDocument()); |
| 178 |
assertNull('Component must not have DOM by default', component.getElement()); |
| 179 |
assertFalse( |
| 180 |
'wasDecorated() must be false before component is rendered', |
| 181 |
component.wasDecorated()); |
| 182 |
|
| 183 |
component.render(sandbox); |
| 184 |
assertTrue( |
| 185 |
'Rendered component must be in the document', component.isInDocument()); |
| 186 |
assertEquals( |
| 187 |
'Component\'s element must be a child of the parent element', sandbox, |
| 188 |
component.getElement().parentNode); |
| 189 |
assertFalse( |
| 190 |
'wasDecorated() must still be false for rendered component', |
| 191 |
component.wasDecorated()); |
| 192 |
|
| 193 |
assertThrows('Trying to re-render component must throw error', function() { |
| 194 |
component.render(); |
| 195 |
}); |
| 196 |
} |
| 197 |
|
| 198 |
function testRender_NoParent() { |
| 199 |
component.render(); |
| 200 |
assertTrue( |
| 201 |
'Rendered component must be in the document', component.isInDocument()); |
| 202 |
assertEquals( |
| 203 |
'Component\'s element must be a child of the document body', |
| 204 |
document.body, component.getElement().parentNode); |
| 205 |
} |
| 206 |
|
| 207 |
function testRender_ParentNotInDocument() { |
| 208 |
var parent = new goog.ui.Component(); |
| 209 |
component.setParent(parent); |
| 210 |
|
| 211 |
assertFalse( |
| 212 |
'Parent component must not be in the document', parent.isInDocument()); |
| 213 |
assertFalse( |
| 214 |
'Child component must not be in the document', component.isInDocument()); |
| 215 |
assertNull('Child component must not have DOM', component.getElement()); |
| 216 |
|
| 217 |
component.render(); |
| 218 |
assertFalse( |
| 219 |
'Parent component must not be in the document', parent.isInDocument()); |
| 220 |
assertFalse( |
| 221 |
'Child component must not be in the document', component.isInDocument()); |
| 222 |
assertNotNull('Child component must have DOM', component.getElement()); |
| 223 |
|
| 224 |
parent.dispose(); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
function testRenderBefore() { |
| 229 |
var sibling = goog.dom.createElement(goog.dom.TagName.DIV); |
| 230 |
sandbox.appendChild(sibling); |
| 231 |
|
| 232 |
component.renderBefore(sibling); |
| 233 |
assertTrue( |
| 234 |
'Rendered component must be in the document', component.isInDocument()); |
| 235 |
assertEquals( |
| 236 |
'Component\'s element must be a child of the parent element', sandbox, |
| 237 |
component.getElement().parentNode); |
| 238 |
assertEquals( |
| 239 |
'Component\'s element must have expected nextSibling', sibling, |
| 240 |
component.getElement().nextSibling); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
function testRenderChild() { |
| 245 |
var parent = new goog.ui.Component(); |
| 246 |
|
| 247 |
parent.createDom(); |
| 248 |
assertFalse('Parent must not be in the document', parent.isInDocument()); |
| 249 |
assertNotNull('Parent must have a DOM', parent.getElement()); |
| 250 |
|
| 251 |
parent.addChild(component); |
| 252 |
assertFalse('Child must not be in the document', component.isInDocument()); |
| 253 |
assertNull('Child must not have a DOM', component.getElement()); |
| 254 |
|
| 255 |
component.render(parent.getElement()); |
| 256 |
assertFalse('Parent must not be in the document', parent.isInDocument()); |
| 257 |
assertFalse( |
| 258 |
'Child must not be in the document if the parent isn\'t', |
| 259 |
component.isInDocument()); |
| 260 |
assertNotNull('Child must have a DOM', component.getElement()); |
| 261 |
assertEquals( |
| 262 |
'Child\'s element must be a child of the parent\'s element', |
| 263 |
parent.getElement(), component.getElement().parentNode); |
| 264 |
|
| 265 |
parent.render(sandbox); |
| 266 |
assertTrue('Parent must be in the document', parent.isInDocument()); |
| 267 |
assertTrue('Child must be in the document', component.isInDocument()); |
| 268 |
|
| 269 |
parent.dispose(); |
| 270 |
} |
| 271 |
|
| 272 |
function testDecorate() { |
| 273 |
sandbox.innerHTML = '<div id="foo">Foo</div>'; |
| 274 |
var foo = goog.dom.getElement('foo'); |
| 275 |
|
| 276 |
assertFalse( |
| 277 |
'wasDecorated() must be false by default', component.wasDecorated()); |
| 278 |
|
| 279 |
component.decorate(foo); |
| 280 |
assertTrue('Component must be in the document', component.isInDocument()); |
| 281 |
assertEquals( |
| 282 |
'Component\'s element must be the decorated element', foo, |
| 283 |
component.getElement()); |
| 284 |
assertTrue( |
| 285 |
'wasDecorated() must be true for decorated component', |
| 286 |
component.wasDecorated()); |
| 287 |
|
| 288 |
assertThrows( |
| 289 |
'Trying to decorate with a control already in the document' + |
| 290 |
' must throw error', |
| 291 |
function() { component.decorate(foo); }); |
| 292 |
} |
| 293 |
|
| 294 |
function testDecorate_AllowDetached_NotInDocument() { |
| 295 |
goog.ui.Component.ALLOW_DETACHED_DECORATION = true; |
| 296 |
var element = goog.dom.createElement(goog.dom.TagName.DIV); |
| 297 |
component.decorate(element); |
| 298 |
assertFalse( |
| 299 |
'Component should not call enterDocument when decorated ' + |
| 300 |
'with an element that is not in the document.', |
| 301 |
component.isInDocument()); |
| 302 |
goog.ui.Component.ALLOW_DETACHED_DECORATION = false; |
| 303 |
} |
| 304 |
|
| 305 |
function testDecorate_AllowDetached_InDocument() { |
| 306 |
goog.ui.Component.ALLOW_DETACHED_DECORATION = true; |
| 307 |
var element = goog.dom.createElement(goog.dom.TagName.DIV); |
| 308 |
sandbox.appendChild(element); |
| 309 |
component.decorate(element); |
| 310 |
assertTrue( |
| 311 |
'Component should call enterDocument when decorated ' + |
| 312 |
'with an element that is in the document.', |
| 313 |
component.isInDocument()); |
| 314 |
goog.ui.Component.ALLOW_DETACHED_DECORATION = false; |
| 315 |
} |
| 316 |
|
| 317 |
function testCannotDecorate() { |
| 318 |
sandbox.innerHTML = '<div id="foo">Foo</div>'; |
| 319 |
var foo = goog.dom.getElement('foo'); |
| 320 |
|
| 321 |
// Have canDecorate() return false. |
| 322 |
propertyReplacer.set(component, 'canDecorate', function() { return false; }); |
| 323 |
|
| 324 |
assertThrows( |
| 325 |
'Trying to decorate an element for which canDecorate()' + |
| 326 |
' returns false must throw error', |
| 327 |
function() { component.decorate(foo); }); |
| 328 |
} |
| 329 |
|
| 330 |
function testCanDecorate() { |
| 331 |
assertTrue( |
| 332 |
'canDecorate() must return true by default', |
| 333 |
component.canDecorate(sandbox)); |
| 334 |
} |
| 335 |
|
| 336 |
function testWasDecorated() { |
| 337 |
assertFalse( |
| 338 |
'wasDecorated() must return false by default', component.wasDecorated()); |
| 339 |
} |
| 340 |
|
| 341 |
function testDecorateInternal() { |
| 342 |
assertNull('Element must be null by default', component.getElement()); |
| 343 |
var element = goog.dom.createElement(goog.dom.TagName.DIV); |
| 344 |
component.decorateInternal(element); |
| 345 |
assertEquals( |
| 346 |
'Element must have expected value', element, component.getElement()); |
| 347 |
} |
| 348 |
|
| 349 |
function testGetElementAndGetElementsByClass() { |
| 350 |
sandbox.innerHTML = '<ul id="task-list">' + |
| 351 |
'<li class="task">Unclog drain' + |
| 352 |
'</ul>' + |
| 353 |
'<ul id="completed-tasks">' + |
| 354 |
'<li id="groceries" class="task">Buy groceries' + |
| 355 |
'<li class="task">Rotate tires' + |
| 356 |
'<li class="task">Clean kitchen' + |
| 357 |
'</ul>' + |
| 358 |
assertNull('Should be nothing to return before the component has a DOM', |
| 359 |
component.getElementByClass('task')); |
| 360 |
assertEquals( |
| 361 |
'Should return an empty list before the component has a DOM', 0, |
| 362 |
component.getElementsByClass('task').length); |
| 363 |
|
| 364 |
component.decorate(goog.dom.getElement('completed-tasks')); |
| 365 |
assertEquals( |
| 366 |
'getElementByClass() should return the first completed task', 'groceries', |
| 367 |
component.getElementByClass('task').id); |
| 368 |
assertEquals( |
| 369 |
'getElementsByClass() should return only the completed tasks', 3, |
| 370 |
component.getElementsByClass('task').length); |
| 371 |
} |
| 372 |
|
| 373 |
function testGetRequiredElementByClass() { |
| 374 |
sandbox.innerHTML = '<ul id="task-list">' + |
| 375 |
'<li class="task">Unclog drain' + |
| 376 |
'</ul>' + |
| 377 |
'<ul id="completed-tasks">' + |
| 378 |
'<li id="groceries" class="task">Buy groceries' + |
| 379 |
'<li class="task">Rotate tires' + |
| 380 |
'<li class="task">Clean kitchen' + |
| 381 |
'</ul>'; |
| 382 |
component.decorate(goog.dom.getElement('completed-tasks')); |
| 383 |
assertEquals( |
| 384 |
'getRequiredElementByClass() should return the first completed task', |
| 385 |
'groceries', component.getRequiredElementByClass('task').id); |
| 386 |
assertThrows( |
| 387 |
'Attempting to retrieve a required element that does not' + |
| 388 |
'exist should fail', |
| 389 |
function() { component.getRequiredElementByClass('undefinedClass'); }); |
| 390 |
} |
| 391 |
|
| 392 |
function testEnterExitDocument() { |
| 393 |
var c1 = new goog.ui.Component(); |
| 394 |
var c2 = new goog.ui.Component(); |
| 395 |
|
| 396 |
component.addChild(c1); |
| 397 |
component.addChild(c2); |
| 398 |
|
| 399 |
component.createDom(); |
| 400 |
c1.createDom(); |
| 401 |
c2.createDom(); |
| 402 |
|
| 403 |
assertFalse('Parent must not be in the document', component.isInDocument()); |
| 404 |
assertFalse( |
| 405 |
'Neither child must be in the document', |
| 406 |
c1.isInDocument() || c2.isInDocument()); |
| 407 |
|
| 408 |
component.enterDocument(); |
| 409 |
assertTrue('Parent must be in the document', component.isInDocument()); |
| 410 |
assertTrue( |
| 411 |
'Both children must be in the document', |
| 412 |
c1.isInDocument() && c2.isInDocument()); |
| 413 |
|
| 414 |
component.exitDocument(); |
| 415 |
assertFalse('Parent must not be in the document', component.isInDocument()); |
| 416 |
assertFalse( |
| 417 |
'Neither child must be in the document', |
| 418 |
c1.isInDocument() || c2.isInDocument()); |
| 419 |
|
| 420 |
c1.dispose(); |
| 421 |
c2.dispose(); |
| 422 |
} |
| 423 |
|
| 424 |
function testDispose() { |
| 425 |
var c1, c2; |
| 426 |
|
| 427 |
component.createDom(); |
| 428 |
component.addChild((c1 = new goog.ui.Component()), true); |
| 429 |
component.addChild((c2 = new goog.ui.Component()), true); |
| 430 |
|
| 431 |
var element = component.getElement(); |
| 432 |
var c1Element = c1.getElement(); |
| 433 |
var c2Element = c2.getElement(); |
| 434 |
|
| 435 |
component.render(sandbox); |
| 436 |
assertTrue('Parent must be in the document', component.isInDocument()); |
| 437 |
assertEquals( |
| 438 |
'Parent\'s element must be a child of the sandbox element', sandbox, |
| 439 |
element.parentNode); |
| 440 |
assertTrue( |
| 441 |
'Both children must be in the document', |
| 442 |
c1.isInDocument() && c2.isInDocument()); |
| 443 |
assertEquals( |
| 444 |
'First child\'s element must be a child of the parent\'s' + |
| 445 |
' element', |
| 446 |
element, c1Element.parentNode); |
| 447 |
assertEquals( |
| 448 |
'Second child\'s element must be a child of the parent\'s' + |
| 449 |
' element', |
| 450 |
element, c2Element.parentNode); |
| 451 |
|
| 452 |
assertFalse('Parent must not have been disposed of', component.isDisposed()); |
| 453 |
assertFalse( |
| 454 |
'Neither child must have been disposed of', |
| 455 |
c1.isDisposed() || c2.isDisposed()); |
| 456 |
|
| 457 |
component.dispose(); |
| 458 |
assertTrue('Parent must have been disposed of', component.isDisposed()); |
| 459 |
assertFalse('Parent must not be in the document', component.isInDocument()); |
| 460 |
assertNotEquals( |
| 461 |
'Parent\'s element must no longer be a child of the' + |
| 462 |
' sandbox element', |
| 463 |
sandbox, element.parentNode); |
| 464 |
assertTrue( |
| 465 |
'Both children must have been disposed of', |
| 466 |
c1.isDisposed() && c2.isDisposed()); |
| 467 |
assertFalse( |
| 468 |
'Neither child must be in the document', |
| 469 |
c1.isInDocument() || c2.isInDocument()); |
| 470 |
assertNotEquals( |
| 471 |
'First child\'s element must no longer be a child of' + |
| 472 |
' the parent\'s element', |
| 473 |
element, c1Element.parentNode); |
| 474 |
assertNotEquals( |
| 475 |
'Second child\'s element must no longer be a child of' + |
| 476 |
' the parent\'s element', |
| 477 |
element, c2Element.parentNode); |
| 478 |
} |
| 479 |
|
| 480 |
function testDispose_Decorated() { |
| 481 |
sandbox.innerHTML = '<div id="foo">Foo</div>'; |
| 482 |
var foo = goog.dom.getElement('foo'); |
| 483 |
|
| 484 |
component.decorate(foo); |
| 485 |
assertTrue('Component must be in the document', component.isInDocument()); |
| 486 |
assertFalse( |
| 487 |
'Component must not have been disposed of', component.isDisposed()); |
| 488 |
assertEquals( |
| 489 |
'Component\'s element must have expected value', foo, |
| 490 |
component.getElement()); |
| 491 |
assertEquals( |
| 492 |
'Decorated element must be a child of the sandbox', sandbox, |
| 493 |
foo.parentNode); |
| 494 |
|
| 495 |
component.dispose(); |
| 496 |
assertFalse( |
| 497 |
'Component must not be in the document', component.isInDocument()); |
| 498 |
assertTrue('Component must have been disposed of', component.isDisposed()); |
| 499 |
assertNull('Component\'s element must be null', component.getElement()); |
| 500 |
assertEquals( |
| 501 |
'Previously decorated element must still be a child of the' + |
| 502 |
' sandbox', |
| 503 |
sandbox, foo.parentNode); |
| 504 |
} |
| 505 |
|
| 506 |
function testMakeIdAndGetFragmentFromId() { |
| 507 |
assertEquals( |
| 508 |
'Unique id must have expected value', component.getId() + '.foo', |
| 509 |
component.makeId('foo')); |
| 510 |
assertEquals( |
| 511 |
'Fragment must have expected value', 'foo', |
| 512 |
component.getFragmentFromId(component.makeId('foo'))); |
| 513 |
} |
| 514 |
|
| 515 |
function testMakeIdsWithObject() { |
| 516 |
var EnumDef = {ENUM_1: 'enum 1', ENUM_2: 'enum 2', ENUM_3: 'enum 3'}; |
| 517 |
var ids = component.makeIds(EnumDef); |
| 518 |
assertEquals(component.makeId(EnumDef.ENUM_1), ids.ENUM_1); |
| 519 |
assertEquals(component.makeId(EnumDef.ENUM_2), ids.ENUM_2); |
| 520 |
assertEquals(component.makeId(EnumDef.ENUM_3), ids.ENUM_3); |
| 521 |
} |
| 522 |
|
| 523 |
function testGetElementByFragment() { |
| 524 |
component.render(sandbox); |
| 525 |
|
| 526 |
var element = component.dom_.createDom( |
| 527 |
goog.dom.TagName.DIV, {id: component.makeId('foo')}, 'Hello'); |
| 528 |
sandbox.appendChild(element); |
| 529 |
|
| 530 |
assertEquals( |
| 531 |
'Element must have expected value', element, |
| 532 |
component.getElementByFragment('foo')); |
| 533 |
} |
| 534 |
|
| 535 |
function testGetSetModel() { |
| 536 |
assertNull('Model must be null by default', component.getModel()); |
| 537 |
|
| 538 |
var model = 'someModel'; |
| 539 |
component.setModel(model); |
| 540 |
assertEquals('Model must have expected value', model, component.getModel()); |
| 541 |
|
| 542 |
component.setModel(null); |
| 543 |
assertNull('Model must be null', component.getModel()); |
| 544 |
} |
| 545 |
|
| 546 |
function testAddChild() { |
| 547 |
var child = new goog.ui.Component(); |
| 548 |
child.setId('child'); |
| 549 |
|
| 550 |
assertFalse('Parent must not be in the document', component.isInDocument()); |
| 551 |
|
| 552 |
component.addChild(child); |
| 553 |
assertTrue('Parent must have children.', component.hasChildren()); |
| 554 |
assertEquals('Child must have expected parent', component, child.getParent()); |
| 555 |
assertEquals( |
| 556 |
'Parent must find child by ID', child, component.getChild('child')); |
| 557 |
} |
| 558 |
|
| 559 |
function testAddChild_Render() { |
| 560 |
var child = new goog.ui.Component(); |
| 561 |
|
| 562 |
component.render(sandbox); |
| 563 |
assertTrue('Parent must be in the document', component.isInDocument()); |
| 564 |
assertEquals( |
| 565 |
'Parent must be in the sandbox', sandbox, |
| 566 |
component.getElement().parentNode); |
| 567 |
|
| 568 |
component.addChild(child, true); |
| 569 |
assertTrue('Child must be in the document', child.isInDocument()); |
| 570 |
assertEquals( |
| 571 |
'Child element must be a child of the parent element', |
| 572 |
component.getElement(), child.getElement().parentNode); |
| 573 |
} |
| 574 |
|
| 575 |
function testAddChild_DomOnly() { |
| 576 |
var child = new goog.ui.Component(); |
| 577 |
|
| 578 |
component.createDom(); |
| 579 |
assertNotNull('Parent must have a DOM', component.getElement()); |
| 580 |
assertFalse('Parent must not be in the document', component.isInDocument()); |
| 581 |
|
| 582 |
component.addChild(child, true); |
| 583 |
assertNotNull('Child must have a DOM', child.getElement()); |
| 584 |
assertEquals( |
| 585 |
'Child element must be a child of the parent element', |
| 586 |
component.getElement(), child.getElement().parentNode); |
| 587 |
assertFalse('Child must not be in the document', child.isInDocument()); |
| 588 |
} |
| 589 |
|
| 590 |
function testAddChildAt() { |
| 591 |
var a = new goog.ui.Component(); |
| 592 |
var b = new goog.ui.Component(); |
| 593 |
var c = new goog.ui.Component(); |
| 594 |
var d = new goog.ui.Component(); |
| 595 |
|
| 596 |
a.setId('a'); |
| 597 |
b.setId('b'); |
| 598 |
c.setId('c'); |
| 599 |
d.setId('d'); |
| 600 |
|
| 601 |
component.addChildAt(b, 0); |
| 602 |
assertEquals('b', component.getChildIds().join('')); |
| 603 |
component.addChildAt(d, 1); |
| 604 |
assertEquals('bd', component.getChildIds().join('')); |
| 605 |
component.addChildAt(a, 0); |
| 606 |
assertEquals('abd', component.getChildIds().join('')); |
| 607 |
component.addChildAt(c, 2); |
| 608 |
assertEquals('abcd', component.getChildIds().join('')); |
| 609 |
|
| 610 |
assertEquals(a, component.getChildAt(0)); |
| 611 |
assertEquals(b, component.getChildAt(1)); |
| 612 |
assertEquals(c, component.getChildAt(2)); |
| 613 |
assertEquals(d, component.getChildAt(3)); |
| 614 |
|
| 615 |
assertThrows( |
| 616 |
'Adding child at out-of-bounds index must throw error', |
| 617 |
function() { component.addChildAt(new goog.ui.Component(), 5); }); |
| 618 |
} |
| 619 |
|
| 620 |
function testAddChildAtThrowsIfNull() { |
| 621 |
assertThrows('Adding a null child must throw an error', function() { |
| 622 |
component.addChildAt(null, 0); |
| 623 |
}); |
| 624 |
} |
| 625 |
|
| 626 |
function testHasChildren() { |
| 627 |
assertFalse('Component must not have children', component.hasChildren()); |
| 628 |
|
| 629 |
component.addChildAt(new goog.ui.Component(), 0); |
| 630 |
assertTrue('Component must have children', component.hasChildren()); |
| 631 |
|
| 632 |
component.removeChildAt(0); |
| 633 |
assertFalse('Component must not have children', component.hasChildren()); |
| 634 |
} |
| 635 |
|
| 636 |
function testGetChildCount() { |
| 637 |
assertEquals('Component must have 0 children', 0, component.getChildCount()); |
| 638 |
|
| 639 |
component.addChild(new goog.ui.Component()); |
| 640 |
assertEquals('Component must have 1 child', 1, component.getChildCount()); |
| 641 |
|
| 642 |
component.addChild(new goog.ui.Component()); |
| 643 |
assertEquals('Component must have 2 children', 2, component.getChildCount()); |
| 644 |
|
| 645 |
component.removeChildAt(1); |
| 646 |
assertEquals('Component must have 1 child', 1, component.getChildCount()); |
| 647 |
|
| 648 |
component.removeChildAt(0); |
| 649 |
assertEquals('Component must have 0 children', 0, component.getChildCount()); |
| 650 |
} |
| 651 |
|
| 652 |
function testGetChildIds() { |
| 653 |
var a = new goog.ui.Component(); |
| 654 |
var b = new goog.ui.Component(); |
| 655 |
|
| 656 |
a.setId('a'); |
| 657 |
b.setId('b'); |
| 658 |
|
| 659 |
component.addChild(a); |
| 660 |
assertEquals('a', component.getChildIds().join('')); |
| 661 |
|
| 662 |
component.addChild(b); |
| 663 |
assertEquals('ab', component.getChildIds().join('')); |
| 664 |
|
| 665 |
var ids = component.getChildIds(); |
| 666 |
ids.push('c'); |
| 667 |
assertEquals( |
| 668 |
'Changes to the array returned by getChildIds() must not' + |
| 669 |
' affect the component', |
| 670 |
'ab', component.getChildIds().join('')); |
| 671 |
} |
| 672 |
|
| 673 |
function testGetChild() { |
| 674 |
assertNull('Parent must have no children', component.getChild('myId')); |
| 675 |
|
| 676 |
var c = new goog.ui.Component(); |
| 677 |
c.setId('myId'); |
| 678 |
component.addChild(c); |
| 679 |
assertEquals('Parent must find child by ID', c, component.getChild('myId')); |
| 680 |
|
| 681 |
c.setId('newId'); |
| 682 |
assertNull( |
| 683 |
'Parent must not find child by old ID', component.getChild('myId')); |
| 684 |
assertEquals( |
| 685 |
'Parent must find child by new ID', c, component.getChild('newId')); |
| 686 |
} |
| 687 |
|
| 688 |
function testGetChildAt() { |
| 689 |
var a = new goog.ui.Component(); |
| 690 |
var b = new goog.ui.Component(); |
| 691 |
|
| 692 |
a.setId('a'); |
| 693 |
b.setId('b'); |
| 694 |
|
| 695 |
component.addChildAt(a, 0); |
| 696 |
assertEquals('Parent must find child by index', a, component.getChildAt(0)); |
| 697 |
|
| 698 |
component.addChildAt(b, 1); |
| 699 |
assertEquals('Parent must find child by index', b, component.getChildAt(1)); |
| 700 |
|
| 701 |
assertNull( |
| 702 |
'Parent must return null for out-of-bounds index', |
| 703 |
component.getChildAt(3)); |
| 704 |
} |
| 705 |
|
| 706 |
function testForEachChild() { |
| 707 |
var invoked = false; |
| 708 |
component.forEachChild(function(child) { |
| 709 |
assertNotNull('Child must never be null', child); |
| 710 |
invoked = true; |
| 711 |
}); |
| 712 |
assertFalse( |
| 713 |
'forEachChild must not call its argument if the parent has ' + |
| 714 |
'no children', |
| 715 |
invoked); |
| 716 |
|
| 717 |
component.addChild(new goog.ui.Component()); |
| 718 |
component.addChild(new goog.ui.Component()); |
| 719 |
component.addChild(new goog.ui.Component()); |
| 720 |
var callCount = 0; |
| 721 |
component.forEachChild(function(child, index) { |
| 722 |
assertEquals(component, this); |
| 723 |
callCount++; |
| 724 |
}, component); |
| 725 |
assertEquals(3, callCount); |
| 726 |
} |
| 727 |
|
| 728 |
function testIndexOfChild() { |
| 729 |
var a = new goog.ui.Component(); |
| 730 |
var b = new goog.ui.Component(); |
| 731 |
var c = new goog.ui.Component(); |
| 732 |
|
| 733 |
a.setId('a'); |
| 734 |
b.setId('b'); |
| 735 |
c.setId('c'); |
| 736 |
|
| 737 |
component.addChild(a); |
| 738 |
assertEquals(0, component.indexOfChild(a)); |
| 739 |
|
| 740 |
component.addChild(b); |
| 741 |
assertEquals(1, component.indexOfChild(b)); |
| 742 |
|
| 743 |
component.addChild(c); |
| 744 |
assertEquals(2, component.indexOfChild(c)); |
| 745 |
|
| 746 |
assertEquals( |
| 747 |
'indexOfChild must return -1 for nonexistent child', -1, |
| 748 |
component.indexOfChild(new goog.ui.Component())); |
| 749 |
} |
| 750 |
|
| 751 |
function testRemoveChild() { |
| 752 |
var a = new goog.ui.Component(); |
| 753 |
var b = new goog.ui.Component(); |
| 754 |
var c = new goog.ui.Component(); |
| 755 |
|
| 756 |
a.setId('a'); |
| 757 |
b.setId('b'); |
| 758 |
c.setId('c'); |
| 759 |
|
| 760 |
component.addChild(a); |
| 761 |
component.addChild(b); |
| 762 |
component.addChild(c); |
| 763 |
|
| 764 |
assertEquals( |
| 765 |
'Parent must remove and return child', c, component.removeChild(c)); |
| 766 |
assertNull( |
| 767 |
'Parent must no longer contain this child', component.getChild('c')); |
| 768 |
|
| 769 |
assertEquals( |
| 770 |
'Parent must remove and return child by ID', b, |
| 771 |
component.removeChild('b')); |
| 772 |
assertNull( |
| 773 |
'Parent must no longer contain this child', component.getChild('b')); |
| 774 |
|
| 775 |
assertEquals( |
| 776 |
'Parent must remove and return child by index', a, |
| 777 |
component.removeChildAt(0)); |
| 778 |
assertNull( |
| 779 |
'Parent must no longer contain this child', component.getChild('a')); |
| 780 |
} |
| 781 |
|
| 782 |
function testMovingChildrenUsingAddChildAt() { |
| 783 |
component.render(sandbox); |
| 784 |
|
| 785 |
var a = new goog.ui.Component(); |
| 786 |
var b = new goog.ui.Component(); |
| 787 |
var c = new goog.ui.Component(); |
| 788 |
var d = new goog.ui.Component(); |
| 789 |
a.setElementInternal(goog.dom.createElement(goog.dom.TagName.A)); |
| 790 |
b.setElementInternal(goog.dom.createElement(goog.dom.TagName.B)); |
| 791 |
c.setElementInternal(goog.dom.createElement(goog.dom.TagName.C)); |
| 792 |
d.setElementInternal(goog.dom.createElement(goog.dom.TagName.D)); |
| 793 |
|
| 794 |
a.setId('a'); |
| 795 |
b.setId('b'); |
| 796 |
c.setId('c'); |
| 797 |
d.setId('d'); |
| 798 |
|
| 799 |
component.addChild(a, true); |
| 800 |
component.addChild(b, true); |
| 801 |
component.addChild(c, true); |
| 802 |
component.addChild(d, true); |
| 803 |
|
| 804 |
assertEquals('abcd', component.getChildIds().join('')); |
| 805 |
assertEquals(a, component.getChildAt(0)); |
| 806 |
assertEquals(b, component.getChildAt(1)); |
| 807 |
assertEquals(c, component.getChildAt(2)); |
| 808 |
assertEquals(d, component.getChildAt(3)); |
| 809 |
|
| 810 |
// Move child d to the top and b to the bottom. |
| 811 |
component.addChildAt(d, 0); |
| 812 |
component.addChildAt(b, 3); |
| 813 |
|
| 814 |
assertEquals('dacb', component.getChildIds().join('')); |
| 815 |
assertEquals(d, component.getChildAt(0)); |
| 816 |
assertEquals(a, component.getChildAt(1)); |
| 817 |
assertEquals(c, component.getChildAt(2)); |
| 818 |
assertEquals(b, component.getChildAt(3)); |
| 819 |
|
| 820 |
// Move child a to the top, and check that DOM nodes are in correct order. |
| 821 |
component.addChildAt(a, 0); |
| 822 |
assertEquals('adcb', component.getChildIds().join('')); |
| 823 |
assertEquals(a, component.getChildAt(0)); |
| 824 |
assertEquals(a.getElement(), component.getElement().childNodes[0]); |
| 825 |
} |
| 826 |
|
| 827 |
function testAddChildAfterDomCreatedDoesNotEnterDocument() { |
| 828 |
var parent = new goog.ui.Component(); |
| 829 |
var child = new goog.ui.Component(); |
| 830 |
|
| 831 |
var nestedDiv = goog.dom.createDom(goog.dom.TagName.DIV); |
| 832 |
parent.setElementInternal( |
| 833 |
goog.dom.createDom(goog.dom.TagName.DIV, undefined, nestedDiv)); |
| 834 |
parent.render(); |
| 835 |
|
| 836 |
// Now add a child, whose DOM already exists. This happens, for example, |
| 837 |
// if the child itself performs an addChild(x, true). |
| 838 |
child.createDom(); |
| 839 |
parent.addChild(child, false); |
| 840 |
// The parent shouldn't call enterDocument on the child, since the child |
| 841 |
// actually isn't in the document yet. |
| 842 |
assertFalse(child.isInDocument()); |
| 843 |
|
| 844 |
// Now, actually render the child; it should be in the document. |
| 845 |
child.render(nestedDiv); |
| 846 |
assertTrue(child.isInDocument()); |
| 847 |
assertEquals( |
| 848 |
'Child should be rendered in the expected div', nestedDiv, |
| 849 |
child.getElement().parentNode); |
| 850 |
} |
| 851 |
|
| 852 |
function testAddChildAfterDomManuallyInserted() { |
| 853 |
var parent = new goog.ui.Component(); |
| 854 |
var child = new goog.ui.Component(); |
| 855 |
|
| 856 |
var nestedDiv = goog.dom.createDom(goog.dom.TagName.DIV); |
| 857 |
parent.setElementInternal( |
| 858 |
goog.dom.createDom(goog.dom.TagName.DIV, undefined, nestedDiv)); |
| 859 |
parent.render(); |
| 860 |
|
| 861 |
// This sequence is weird, but some people do it instead of just manually |
| 862 |
// doing render. The addChild will detect that the child is in the DOM |
| 863 |
// and call enterDocument. |
| 864 |
child.createDom(); |
| 865 |
nestedDiv.appendChild(child.getElement()); |
| 866 |
parent.addChild(child, false); |
| 867 |
|
| 868 |
assertTrue(child.isInDocument()); |
| 869 |
assertEquals( |
| 870 |
'Child should be rendered in the expected div', nestedDiv, |
| 871 |
child.getElement().parentNode); |
| 872 |
} |
| 873 |
|
| 874 |
function testRemoveChildren() { |
| 875 |
var a = new goog.ui.Component(); |
| 876 |
var b = new goog.ui.Component(); |
| 877 |
var c = new goog.ui.Component(); |
| 878 |
|
| 879 |
component.addChild(a); |
| 880 |
component.addChild(b); |
| 881 |
component.addChild(c); |
| 882 |
|
| 883 |
a.setId('a'); |
| 884 |
b.setId('b'); |
| 885 |
c.setId('c'); |
| 886 |
|
| 887 |
assertArrayEquals( |
| 888 |
'Parent must remove and return children.', [a, b, c], |
| 889 |
component.removeChildren()); |
| 890 |
assertNull( |
| 891 |
'Parent must no longer contain this child', component.getChild('a')); |
| 892 |
assertNull( |
| 893 |
'Parent must no longer contain this child', component.getChild('b')); |
| 894 |
assertNull( |
| 895 |
'Parent must no longer contain this child', component.getChild('c')); |
| 896 |
} |
| 897 |
|
| 898 |
function testRemoveChildren_Unrender() { |
| 899 |
var a = new goog.ui.Component(); |
| 900 |
var b = new goog.ui.Component(); |
| 901 |
|
| 902 |
component.render(sandbox); |
| 903 |
component.addChild(a); |
| 904 |
component.addChild(b); |
| 905 |
|
| 906 |
assertArrayEquals( |
| 907 |
'Prent must remove and return children.', [a, b], |
| 908 |
component.removeChildren(true)); |
| 909 |
assertNull( |
| 910 |
'Parent must no longer contain this child', component.getChild('a')); |
| 911 |
assertFalse('Child must no longer be in the document.', a.isInDocument()); |
| 912 |
assertNull( |
| 913 |
'Parent must no longer contain this child', component.getChild('b')); |
| 914 |
assertFalse('Child must no longer be in the document.', b.isInDocument()); |
| 915 |
} |