sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
725 lines (590 loc) • 25.3 kB
HTML
<html>
<!--
Copyright 2007 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<!--
Author: pupius@google.com (Daniel Pupius)
Author: attila@google.com (Attila Bodis)
-->
<head>
<title>Closure Unit Tests - goog.ui.Component</title>
<script src="../base.js"></script>
<script>
goog.require('goog.dom');
goog.require('goog.dom.DomHelper');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.TagName');
goog.require('goog.string.StringBuffer');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.require('goog.ui.Component');
</script>
</head>
<body>
<div id="sandbox"></div>
<script>
var component;
var propertyReplacer = new goog.testing.PropertyReplacer();
var sandbox = goog.dom.getElement('sandbox');
function setUp() {
component = new goog.ui.Component();
}
function tearDown() {
component.dispose();
goog.dom.removeChildren(sandbox);
propertyReplacer.reset();
}
function testConstructor() {
assertTrue('Instance must be non-null and have the expected class',
component instanceof goog.ui.Component);
assertTrue('DOM helper must be non-null and have the expected class',
component.dom_ instanceof goog.dom.DomHelper);
var fakeDom = {};
var otherComponent = new goog.ui.Component(fakeDom);
assertEquals('DOM helper must refer to expected object', fakeDom,
otherComponent.dom_);
otherComponent.dispose();
}
function testGetId() {
assertNull('Component ID should be initialized to null', component.id_);
var id = component.getId();
assertNotNull('Component ID should be generated on demand', id);
assertEquals('Subsequent calls to getId() must return same value', id,
component.getId());
}
function testSetId() {
component.setId('myId');
assertEquals('getId() must return explicitly set ID', 'myId',
component.getId());
var child = new goog.ui.Component();
var childId = child.getId();
component.addChild(child);
assertEquals('Parent component must find child by ID', child,
component.getChild(childId));
child.setId('someNewId');
assertEquals('Parent component must find child by new ID', child,
component.getChild('someNewId'));
child.dispose();
}
function testGetSetElement() {
assertNull('Element must be null by default', component.getElement());
var element = goog.dom.createElement(goog.dom.TagName.DIV);
component.setElementInternal(element);
assertEquals('getElement() must return expected element', element,
component.getElement());
}
function testGetSetParent() {
assertNull('Parent must be null by default', component.getParent());
var parent = new goog.ui.Component();
component.setParent(parent);
assertEquals('getParent() must return expected component', parent,
component.getParent());
component.setParent(null);
assertNull('Parent must be null', component.getParent());
assertThrows('Setting a component\'s parent to itself must throw error',
function() {
component.setParent(component);
});
parent.addChild(component);
assertEquals('getParent() must return expected component', parent,
component.getParent());
assertThrows('Changing a child component\'s parent must throw error',
function() {
component.setParent(new goog.ui.Component());
});
parent.dispose();
}
function testGetParentEventTarget() {
assertNull('Parent event target must be null by default',
component.getParentEventTarget());
var parent = new goog.ui.Component();
component.setParent(parent);
assertEquals('Parent event target must be the parent component', parent,
component.getParentEventTarget());
assertThrows('Directly setting the parent event target to other than ' +
'the parent component when the parent component is set must throw ' +
'error',
function() {
component.setParentEventTarget(new goog.ui.Component());
});
parent.dispose();
}
function testSetParentEventTarget() {
var parentEventTarget = new goog.events.EventTarget();
component.setParentEventTarget(parentEventTarget);
assertEquals('Parent component must be null', null,
component.getParent());
parentEventTarget.dispose();
}
function testGetDomHelper() {
var domHelper = new goog.dom.DomHelper();
var component = new goog.ui.Component(domHelper);
assertEquals('Component must return the same DomHelper passed', domHelper,
component.getDomHelper());
}
function testIsInDocument() {
assertFalse('Component must not be in the document by default',
component.isInDocument());
component.enterDocument();
assertTrue('Component must be in the document', component.isInDocument());
}
function testCreateDom() {
assertNull('Component must not have DOM by default',
component.getElement());
component.createDom();
assertEquals('Component\'s DOM must be an element node',
goog.dom.NodeType.ELEMENT, component.getElement().nodeType);
}
function testRender() {
assertFalse('Component must not be in the document by default',
component.isInDocument());
assertNull('Component must not have DOM by default',
component.getElement());
assertFalse('wasDecorated() must be false before component is rendered',
component.wasDecorated());
component.render(sandbox);
assertTrue('Rendered component must be in the document',
component.isInDocument());
assertEquals('Component\'s element must be a child of the parent element',
sandbox, component.getElement().parentNode);
assertFalse('wasDecorated() must still be false for rendered component',
component.wasDecorated());
assertThrows('Trying to re-render component must throw error',
function() {
component.render();
});
}
function testRender_NoParent() {
component.render();
assertTrue('Rendered component must be in the document',
component.isInDocument());
assertEquals('Component\'s element must be a child of the document body',
document.body, component.getElement().parentNode);
}
function testRender_ParentNotInDocument() {
var parent = new goog.ui.Component();
component.setParent(parent);
assertFalse('Parent component must not be in the document',
parent.isInDocument());
assertFalse('Child component must not be in the document',
component.isInDocument());
assertNull('Child component must not have DOM', component.getElement());
component.render();
assertFalse('Parent component must not be in the document',
parent.isInDocument());
assertFalse('Child component must not be in the document',
component.isInDocument());
assertNotNull('Child component must have DOM', component.getElement());
parent.dispose();
}
function testRenderBefore() {
var sibling = goog.dom.createElement(goog.dom.TagName.DIV);
sandbox.appendChild(sibling);
component.renderBefore(sibling);
assertTrue('Rendered component must be in the document',
component.isInDocument());
assertEquals('Component\'s element must be a child of the parent element',
sandbox, component.getElement().parentNode);
assertEquals('Component\'s element must have expected nextSibling',
sibling, component.getElement().nextSibling);
}
function testRenderChild() {
var parent = new goog.ui.Component();
parent.createDom();
assertFalse('Parent must not be in the document', parent.isInDocument());
assertNotNull('Parent must have a DOM', parent.getElement());
parent.addChild(component);
assertFalse('Child must not be in the document',
component.isInDocument());
assertNull('Child must not have a DOM', component.getElement());
component.render(parent.getElement());
assertFalse('Parent must not be in the document', parent.isInDocument());
assertFalse('Child must not be in the document if the parent isn\'t',
component.isInDocument());
assertNotNull('Child must have a DOM', component.getElement());
assertEquals('Child\'s element must be a child of the parent\'s element',
parent.getElement(), component.getElement().parentNode);
parent.render(sandbox);
assertTrue('Parent must be in the document', parent.isInDocument());
assertTrue('Child must be in the document', component.isInDocument());
parent.dispose();
}
function testDecorate() {
sandbox.innerHTML = '<div id="foo">Foo</div>';
var foo = goog.dom.getElement('foo');
assertFalse('wasDecorated() must be false by default',
component.wasDecorated());
component.decorate(foo);
assertTrue('Component must be in the document', component.isInDocument());
assertEquals('Component\'s element must be the decorated element', foo,
component.getElement());
assertTrue('wasDecorated() must be true for decorated component',
component.wasDecorated());
assertThrows('Trying to decorate with a control already in the document' +
' must throw error',
function() {
component.decorate(foo);
});
}
function testCannotDecorate() {
sandbox.innerHTML = '<div id="foo">Foo</div>';
var foo = goog.dom.getElement('foo');
// Have canDecorate() return false.
propertyReplacer.set(component, 'canDecorate', function() {
return false;
});
assertThrows('Trying to decorate an element for which canDecorate()' +
' returns false must throw error',
function() {
component.decorate(foo);
});
}
function testCanDecorate() {
assertTrue('canDecorate() must return true by default',
component.canDecorate(sandbox));
}
function testWasDecorated() {
assertFalse('wasDecorated() must return false by default',
component.wasDecorated());
}
function testDecorateInternal() {
assertNull('Element must be null by default', component.getElement());
var element = goog.dom.createElement(goog.dom.TagName.DIV);
component.decorateInternal(element);
assertEquals('Element must have expected value', element,
component.getElement());
}
function testEnterExitDocument() {
var c1 = new goog.ui.Component();
var c2 = new goog.ui.Component();
component.addChild(c1);
component.addChild(c2);
component.createDom();
c1.createDom();
c2.createDom();
assertFalse('Parent must not be in the document',
component.isInDocument());
assertFalse('Neither child must be in the document',
c1.isInDocument() || c2.isInDocument());
component.enterDocument();
assertTrue('Parent must be in the document', component.isInDocument());
assertTrue('Both children must be in the document',
c1.isInDocument() && c2.isInDocument());
component.exitDocument();
assertFalse('Parent must not be in the document',
component.isInDocument());
assertFalse('Neither child must be in the document',
c1.isInDocument() || c2.isInDocument());
c1.dispose();
c2.dispose();
}
function testDispose() {
var c1, c2;
component.createDom();
component.addChild((c1 = new goog.ui.Component()), true);
component.addChild((c2 = new goog.ui.Component()), true);
var element = component.getElement();
var c1Element = c1.getElement();
var c2Element = c2.getElement();
component.render(sandbox);
assertTrue('Parent must be in the document', component.isInDocument());
assertEquals('Parent\'s element must be a child of the sandbox element',
sandbox, element.parentNode);
assertTrue('Both children must be in the document',
c1.isInDocument() && c2.isInDocument());
assertEquals('First child\'s element must be a child of the parent\'s' +
' element', element, c1Element.parentNode);
assertEquals('Second child\'s element must be a child of the parent\'s' +
' element', element, c2Element.parentNode);
assertFalse('Parent must not have been disposed of',
component.isDisposed());
assertFalse('Neither child must have been disposed of',
c1.isDisposed() || c2.isDisposed());
component.dispose();
assertTrue('Parent must have been disposed of', component.isDisposed());
assertFalse('Parent must not be in the document',
component.isInDocument());
assertNotEquals('Parent\'s element must no longer be a child of the' +
' sandbox element', sandbox, element.parentNode);
assertTrue('Both children must have been disposed of',
c1.isDisposed() && c2.isDisposed());
assertFalse('Neither child must be in the document',
c1.isInDocument() || c2.isInDocument());
assertNotEquals('First child\'s element must no longer be a child of' +
' the parent\'s element', element, c1Element.parentNode);
assertNotEquals('Second child\'s element must no longer be a child of' +
' the parent\'s element', element, c2Element.parentNode);
}
function testDispose_Decorated() {
sandbox.innerHTML = '<div id="foo">Foo</div>';
var foo = goog.dom.getElement('foo');
component.decorate(foo);
assertTrue('Component must be in the document', component.isInDocument());
assertFalse('Component must not have been disposed of',
component.isDisposed());
assertEquals('Component\'s element must have expected value', foo,
component.getElement());
assertEquals('Decorated element must be a child of the sandbox', sandbox,
foo.parentNode);
component.dispose();
assertFalse('Component must not be in the document',
component.isInDocument());
assertTrue('Component must have been disposed of',
component.isDisposed());
assertNull('Component\'s element must be null', component.getElement());
assertEquals('Previously decorated element must still be a child of the' +
' sandbox', sandbox, foo.parentNode);
}
function testMakeIdAndGetFragmentFromId() {
assertEquals('Unique id must have expected value',
component.getId() + '.foo', component.makeId('foo'));
assertEquals('Fragment must have expected value', 'foo',
component.getFragmentFromId(component.makeId('foo')));
}
function testGetElementByFragment() {
component.render(sandbox);
var element = component.dom_.createDom('DIV', {
id: component.makeId('foo')
}, 'Hello');
sandbox.appendChild(element);
assertEquals('Element must have expected value', element,
component.getElementByFragment('foo'));
}
function testGetSetModel() {
assertNull('Model must be null by default', component.getModel());
var model = 'someModel';
component.setModel(model);
assertEquals('Model must have expected value', model,
component.getModel());
component.setModel(null);
assertNull('Model must be null', component.getModel());
}
function testAddChild() {
var child = new goog.ui.Component();
child.setId('child');
assertFalse('Parent must not be in the document',
component.isInDocument());
component.addChild(child);
assertTrue('Parent must have children.', component.hasChildren());
assertEquals('Child must have expected parent', component,
child.getParent());
assertEquals('Parent must find child by ID', child,
component.getChild('child'));
}
function testAddChild_Render() {
var child = new goog.ui.Component();
component.render(sandbox);
assertTrue('Parent must be in the document', component.isInDocument());
assertEquals('Parent must be in the sandbox', sandbox,
component.getElement().parentNode);
component.addChild(child, true);
assertTrue('Child must be in the document', child.isInDocument());
assertEquals('Child element must be a child of the parent element',
component.getElement(), child.getElement().parentNode);
}
function testAddChild_DomOnly() {
var child = new goog.ui.Component();
component.createDom();
assertNotNull('Parent must have a DOM', component.getElement());
assertFalse('Parent must not be in the document',
component.isInDocument());
component.addChild(child, true);
assertNotNull('Child must have a DOM', child.getElement());
assertEquals('Child element must be a child of the parent element',
component.getElement(), child.getElement().parentNode);
assertFalse('Child must not be in the document', child.isInDocument());
}
function testAddChildAt() {
var a = new goog.ui.Component();
var b = new goog.ui.Component();
var c = new goog.ui.Component();
var d = new goog.ui.Component();
a.setId('a');
b.setId('b');
c.setId('c');
d.setId('d');
component.addChildAt(b, 0);
assertEquals('b', component.getChildIds().join(''));
component.addChildAt(d, 1);
assertEquals('bd', component.getChildIds().join(''));
component.addChildAt(a, 0);
assertEquals('abd', component.getChildIds().join(''));
component.addChildAt(c, 2);
assertEquals('abcd', component.getChildIds().join(''));
assertEquals(a, component.getChildAt(0));
assertEquals(b, component.getChildAt(1));
assertEquals(c, component.getChildAt(2));
assertEquals(d, component.getChildAt(3));
assertThrows('Adding child at out-of-bounds index must throw error',
function() {
component.addChildAt(new goog.ui.Component(), 5);
});
}
function testHasChildren() {
assertFalse('Component must not have children', component.hasChildren());
component.addChildAt(new goog.ui.Component(), 0);
assertTrue('Component must have children', component.hasChildren());
component.removeChildAt(0);
assertFalse('Component must not have children', component.hasChildren());
}
function testGetChildCount() {
assertEquals('Component must have 0 children', 0,
component.getChildCount());
component.addChild(new goog.ui.Component());
assertEquals('Component must have 1 child', 1,
component.getChildCount());
component.addChild(new goog.ui.Component());
assertEquals('Component must have 2 children', 2,
component.getChildCount());
component.removeChildAt(1);
assertEquals('Component must have 1 child', 1,
component.getChildCount());
component.removeChildAt(0);
assertEquals('Component must have 0 children', 0,
component.getChildCount());
}
function testGetChildIds() {
var a = new goog.ui.Component();
var b = new goog.ui.Component();
a.setId('a');
b.setId('b');
component.addChild(a);
assertEquals('a', component.getChildIds().join(''));
component.addChild(b);
assertEquals('ab', component.getChildIds().join(''));
var ids = component.getChildIds();
ids.push('c');
assertEquals('Changes to the array returned by getChildIds() must not' +
' affect the component', 'ab', component.getChildIds().join(''));
}
function testGetChild() {
assertNull('Parent must have no children', component.getChild('myId'));
var c = new goog.ui.Component();
c.setId('myId');
component.addChild(c);
assertEquals('Parent must find child by ID', c,
component.getChild('myId'));
c.setId('newId');
assertNull('Parent must not find child by old ID',
component.getChild('myId'));
assertEquals('Parent must find child by new ID', c,
component.getChild('newId'));
}
function testGetChildAt() {
var a = new goog.ui.Component();
var b = new goog.ui.Component();
a.setId('a');
b.setId('b');
component.addChildAt(a, 0);
assertEquals('Parent must find child by index', a,
component.getChildAt(0));
component.addChildAt(b, 1);
assertEquals('Parent must find child by index', b,
component.getChildAt(1));
assertNull('Parent must return null for out-of-bounds index',
component.getChildAt(3));
}
function testForEachChild() {
var invoked = false;
component.forEachChild(function(child) {
assertNotNull('Child must never be null', child);
invoked = true;
});
assertFalse('forEachChild must not call its argument if the parent has ' +
'no children', invoked);
component.addChild(new goog.ui.Component());
component.addChild(new goog.ui.Component());
component.addChild(new goog.ui.Component());
var sb = new goog.string.StringBuffer();
component.forEachChild(function(child, index) {
// Note: This is just to test that 'this' is set correctly.
var count = this.getChildCount();
sb.append(index + 1).append(' of ').append(count);
if (index < count - 1) {
sb.append(', ');
}
}, component);
assertEquals('1 of 3, 2 of 3, 3 of 3', sb.toString());
}
function testIndexOfChild() {
var a = new goog.ui.Component();
var b = new goog.ui.Component();
var c = new goog.ui.Component();
a.setId('a');
b.setId('b');
c.setId('c');
component.addChild(a);
assertEquals(0, component.indexOfChild(a));
component.addChild(b);
assertEquals(1, component.indexOfChild(b));
component.addChild(c);
assertEquals(2, component.indexOfChild(c));
assertEquals('indexOfChild must return -1 for nonexistent child', -1,
component.indexOfChild(new goog.ui.Component()));
}
function testRemoveChild() {
var a = new goog.ui.Component();
var b = new goog.ui.Component();
var c = new goog.ui.Component();
a.setId('a');
b.setId('b');
c.setId('c');
component.addChild(a);
component.addChild(b);
component.addChild(c);
assertEquals('Parent must remove and return child', c,
component.removeChild(c));
assertNull('Parent must no longer contain this child',
component.getChild('c'));
assertEquals('Parent must remove and return child by ID', b,
component.removeChild('b'));
assertNull('Parent must no longer contain this child',
component.getChild('b'));
assertEquals('Parent must remove and return child by index', a,
component.removeChildAt(0));
assertNull('Parent must no longer contain this child',
component.getChild('a'));
}
function testMovingChildrenUsingAddChildAt() {
component.render(sandbox);
var a = new goog.ui.Component();
var b = new goog.ui.Component();
var c = new goog.ui.Component();
var d = new goog.ui.Component();
a.setElementInternal(goog.dom.createElement('a'));
b.setElementInternal(goog.dom.createElement('b'));
c.setElementInternal(goog.dom.createElement('c'));
d.setElementInternal(goog.dom.createElement('d'));
a.setId('a');
b.setId('b');
c.setId('c');
d.setId('d');
component.addChild(a);
component.addChild(b);
component.addChild(c);
component.addChild(d);
assertEquals('abcd', component.getChildIds().join(''));
assertEquals(a, component.getChildAt(0));
assertEquals(b, component.getChildAt(1));
assertEquals(c, component.getChildAt(2));
assertEquals(d, component.getChildAt(3));
// Move child d to the top and b to the bottom.
component.addChildAt(d, 0);
component.addChildAt(b, 3);
assertEquals('dacb', component.getChildIds().join(''));
assertEquals(d, component.getChildAt(0));
assertEquals(a, component.getChildAt(1));
assertEquals(c, component.getChildAt(2));
assertEquals(b, component.getChildAt(3));
// Move child a to the top, and check that DOM nodes are in correct order.
component.addChildAt(a, 0);
assertEquals('adcb', component.getChildIds().join(''));
assertEquals(a, component.getChildAt(0));
assertEquals(a.getElement(), component.getElement().childNodes[0]);
}
</script>
</body>
</html>