widget.html revision e2f68eb6c08e1cb4fefd3aa3099a58b4f5a17dad
<html>
<head>
<title>Widget Test Suite</title>
#console .yui3-console-entry {
padding:2px;
margin:0px;
min-height:0;
}
#console .yui3-console-entry-fail .yui3-console-entry-cat {
background-color:red;
}
#console .yui3-console-entry-pass .yui3-console-entry-cat {
background-color:green;
}
#console .yui3-console-entry-perf .yui3-console-entry-cat {
background-color:blue;
}
#console {
position:static;
}
html, body {
height:100%;
}
</style>
</head>
<body class="yui3-skin-sam">
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
<div id="testbed" class="yui3-skin-foo"></div>
<div id="widgetRenderToContainer" style="height:300px;width:300px"></div>
<script>
YUI({
useBrowserConsole:false,
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
}).use('test', 'widget', 'node-event-simulate', 'console', function (Y) {
var suite = new Y.Test.Suite("Widget Tests");
suite.add(new Y.Test.Case({
name : "Core",
"testInstantiation" : function() {
var w = new Y.Widget();
Y.Assert.isTrue(w instanceof Y.Widget);
// Basic API
w.destroy();
},
"testInitState" : function() {
var w = new Y.Widget();
Y.Assert.areEqual("div", w.get("boundingBox").get("tagName").toLowerCase());
Y.Assert.areEqual("div", w.get("contentBox").get("tagName").toLowerCase());
Y.Assert.isFalse(w.get("disabled"), "disabled should be false");
Y.Assert.isFalse(w.get("focused"), "focused should be false");
Y.Assert.areSame("", w.get("height"), "height should be empty string");
Y.Assert.areSame("", w.get("width"), "width should be empty string");
Y.Assert.isString(w.get("id"), "id should be a string");
Y.Assert.isTrue(w.get("initialized"), "initialized should be true");
Y.Assert.isFalse(w.get("destroyed"), "destroyed should be false");
Y.Assert.isFalse(w.get("rendered"), "rendered should be false");
Y.Assert.isTrue(w.get("visible"), "visible should be true");
Y.Assert.isNull(w.get("tabIndex"), "tabIndex should be null");
w.destroy();
},
"testNonRenderedStateUpdate" : function() {
var w = new Y.Widget({
// WRITE ONCE
id: "foobar",
});
w.set("disabled", true);
w.set("height", 100);
w.set("width", 200);
w.set("visible", false);
w.set("tabIndex", 5);
Y.Assert.areEqual("span", w.get("boundingBox").get("tagName").toLowerCase());
Y.Assert.areEqual("span", w.get("contentBox").get("tagName").toLowerCase());
Y.Assert.areEqual("bb", w.get("boundingBox").get("id"));
Y.Assert.areEqual("cb", w.get("contentBox").get("id"));
Y.Assert.isTrue(w.get("disabled"), "disabled should be true");
Y.Assert.isFalse(w.get("focused"), "focused should be false"); // focused is READONLY
Y.Assert.areEqual("100", w.get("height"), "height should be 100px");
Y.Assert.areEqual("200", w.get("width"), "width should be 200px");
Y.Assert.areEqual("foobar", w.get("id"), "id should be foobar");
Y.Assert.isFalse(w.get("visible"), "visible should be false");
Y.Assert.areEqual(5, w.get("tabIndex"), "tabIndex should be 5");
w.destroy();
},
"testValidationReadonlyWriteonce" : function() {
var w = new Y.Widget();
// READONLY
w.set("focused", true);
w.set("rendered", true);
// WRITE ONCE
w.set("render", true);
// INVALID
w.set("tabIndex", "foo");
// State should be the same as the initial state
Y.Assert.areEqual("div", w.get("boundingBox").get("tagName").toLowerCase());
Y.Assert.areEqual("div", w.get("contentBox").get("tagName").toLowerCase());
Y.Assert.isFalse(w.get("disabled"), "disabled should be false");
Y.Assert.isFalse(w.get("focused"), "focused should be false");
Y.Assert.areSame("", w.get("height"), "height should be empty string");
Y.Assert.areSame("", w.get("width"), "width should be empty string");
Y.Assert.isString(w.get("id"), "id should be a string");
Y.Assert.isTrue(w.get("initialized"), "initialized should be true");
Y.Assert.isFalse(w.get("destroyed"), "destroyed should be false");
Y.Assert.isFalse(w.get("rendered"), "rendered should be false");
Y.Assert.isTrue(w.get("visible"), "visible should be true");
Y.Assert.isNull(w.get("tabIndex"), "tabIndex should be null");
w.destroy();
},
"testRender" : function() {
var w = new Y.Widget({
id: "widgetRender"
});
w.render();
var bbFromDom = Y.Node.one("#widgetRender");
Y.Assert.isTrue(w.get("boundingBox").compareTo(bbFromDom), "boundingBox not found in DOM");
Y.Assert.isTrue(bbFromDom.get("firstChild").compareTo(w.get("contentBox")), "contentBox not first child of boundingBox");
Y.Assert.isTrue(bbFromDom.compareTo(Y.Node.one("body").get("firstChild")), "widget not inserted to body");
Y.Assert.isTrue(w.get("rendered"), "Rendered flag not set");
w.destroy();
},
"testRenderTo" : function() {
var w = new Y.Widget({
id: "widgetRenderTo"
});
w.render("#widgetRenderToContainer");
var bbFromDom = Y.Node.one("#widgetRenderTo");
Y.Assert.isTrue(bbFromDom.get("parentNode").compareTo(Y.Node.one("#widgetRenderToContainer")), "widget not rendered to container passed to render()");
Y.Assert.isTrue(w.get("boundingBox").compareTo(bbFromDom), "boundingBox not found in DOM");
Y.Assert.isTrue(bbFromDom.get("firstChild").compareTo(w.get("contentBox")), "contentBox not first child of boundingBox");
Y.Assert.isTrue(w.get("rendered"), "Rendered flag not set");
w.destroy();
},
"testBaseClassNames" : function() {
var w = new Y.Widget();
w.render();
var bb = w.get("boundingBox");
var cb = w.get("contentBox");
Y.Assert.isTrue(bb.hasClass("yui3-widget"), "bb missing yui3-widget marker");
Y.Assert.isTrue(cb.hasClass("yui3-widget-content"), "cb missing yui3-widget-content marker");
w.destroy();
},
"testExtendedClassNames" : function() {
function MyWidget() {
MyWidget.superclass.constructor.apply(this, arguments);
}
MyWidget.NAME = "myWidget";
var myWidget = new MyWidget();
var bb = myWidget.get("boundingBox");
var cb = myWidget.get("contentBox");
Y.Assert.isTrue(bb.hasClass("yui3-widget"), "bb missing generic yui3-widget marker");
Y.Assert.isTrue(bb.hasClass("yui3-mywidget"), "bb missing yui3-mywidget marker");
Y.Assert.isTrue(cb.hasClass("yui3-mywidget-content"), "cb missing yui3-mywidget-content marker");
Y.Assert.isFalse(cb.hasClass("yui3-widget-content"), "cb shouldn't have yui3-widget-content marker");
},
"testHeight" : function() {
var w = new Y.Widget({
height:100,
render:"#widgetRenderToContainer"
});
var bb = w.get("boundingBox"),
cb = w.get("contentBox");
Y.Assert.areEqual(100, bb.get("offsetHeight"), "100 height not set correctly in DOM");
Y.Assert.areEqual(100, cb.get("offsetHeight"), "100 height not set correctly in DOM");
w.set("height", "200px");
Y.Assert.areEqual(200, bb.get("offsetHeight"), "200px height not set correctly in DOM");
Y.Assert.areEqual(200, cb.get("offsetHeight"), "200px height not set correctly in DOM");
w.set("height", "50%");
Y.Assert.areEqual(150, bb.get("offsetHeight"), "% height not set correctly in DOM");
Y.Assert.areEqual(150, cb.get("offsetHeight"), "% height not set correctly in DOM");
w.destroy();
},
"testWidth" : function() {
var w = new Y.Widget({
width:100,
render:"#widgetRenderToContainer"
});
var bb = w.get("boundingBox"),
cb = w.get("contentBox");
Y.Assert.areEqual(100, bb.get("offsetWidth"), "100 width not set correctly in DOM");
Y.Assert.areEqual(100, cb.get("offsetWidth"), "100 width not set correctly in DOM");
w.set("width", "200px");
Y.Assert.areEqual(200, bb.get("offsetWidth"), "200px width not set correctly in DOM");
Y.Assert.areEqual(200, cb.get("offsetWidth"), "200px width not set correctly in DOM");
w.set("width", "50%");
Y.Assert.areEqual(150, bb.get("offsetWidth"), "% width not set correctly in DOM");
Y.Assert.areEqual(150, cb.get("offsetWidth"), "% width not set correctly in DOM");
w.destroy();
},
"testDisabled" : function() {
var w = new Y.Widget({render:true});
var bb = w.get("boundingBox");
Y.Assert.isFalse(w.get("disabled"), "disabled should be false by default");
Y.Assert.isFalse(bb.hasClass("yui3-widget-disabled"), "bb should not have a disabled marker class");
w.set("disabled", true);
Y.Assert.isTrue(w.get("disabled"), "disabled should be true");
Y.Assert.isTrue(bb.hasClass("yui3-widget-disabled"), "bb should have a disabled marker");
w.set("disabled", false);
Y.Assert.isFalse(w.get("disabled"), "disabled should be false");
Y.Assert.isFalse(bb.hasClass("yui3-widget-disabled"), "bb should not have a disabled marker class");
w.destroy();
},
"testDisableEnable" : function() {
var w = new Y.Widget({render:true});
var bb = w.get("boundingBox");
Y.Assert.isFalse(w.get("disabled"), "disabled should be false by default");
Y.Assert.isFalse(bb.hasClass("yui3-widget-disabled"), "bb should not have a disabled marker class");
w.disable();
Y.Assert.isTrue(w.get("disabled"), "disabled should be true");
Y.Assert.isTrue(bb.hasClass("yui3-widget-disabled"), "bb should have a disabled marker");
w.enable();
Y.Assert.isFalse(w.get("disabled"), "disabled should be false");
Y.Assert.isFalse(bb.hasClass("yui3-widget-disabled"), "bb should not have a disabled marker class");
w.destroy();
},
"testFocusBlur" : function() {
// The focused attribute is read-only, so we test through the public api
var w = new Y.Widget({render:true});
var bb = w.get("boundingBox");
// TODO ENH REQUEST: This doesn't seem right. We should focus on render.
Y.Assert.isFalse(w.get("focused"), "focused should be false by default");
Y.Assert.isFalse(bb.hasClass("yui3-widget-focused"), "bb should not have a disabled marker class");
w.focus();
Y.Assert.isTrue(w.get("focused"), "focused should be true");
Y.Assert.isTrue(bb.hasClass("yui3-widget-focused"), "bb should have a focused marker");
w.blur();
Y.Assert.isFalse(w.get("focused"), "focused should be false");
Y.Assert.isFalse(bb.hasClass("yui3-widget-focused"), "bb should not have a focused marker class");
w.destroy();
},
"testTabIndex" : function() {
var w = new Y.Widget({render:true});
var bb = w.get("boundingBox");
Y.Assert.isNull(w.get("tabIndex"), "tabIndex should be null by default");
Y.Assert.isTrue(bb.get("tabIndex") <= 0, "tabIndex should not be set by default"); // default varies across browsers 0 or -1
w.destroy();
},
"testId" : function() {
id: "testId",
render:true
});
// ID is writeOnce. Cannot reset ID
w.destroy();
},
"testVisible" : function() {
Y.Assert.areEqual("none", bb.getStyle("display"), "Default CSS should hide widget using display:none");
w.destroy();
},
"testBoundingBox" : function() {
container.append("<div id='bbTest'></div>");
boundingBox: "#bbTest",
render:true
});
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "boundingBox moved from it's place in the DOM");
w.destroy();
},
"testContentBox" : function() {
container.append("<div id='cbTest'></div>");
contentBox: "#cbTest",
render:true
});
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "contentBox moved from it's place in the DOM");
w.destroy();
},
"testBoundingBoxContentBox" : function() {
container.append("<div id='bbTest'></div>");
bb.append("<div id='cbTest'></div>");
boundingBox: "#bbTest",
contentBox: "#cbTest",
render:true
});
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "bb moved from it's place in the DOM");
w.destroy();
},
"testBoundingBoxRenderTo" : function() {
// NOTE: PE content sits in body, not container
boundingBox: "#bbTest",
render:container
});
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "boundingBox moved from it's place in the DOM");
w.destroy();
},
"testContentBoxRenderTo" : function() {
contentBox: "#cbTest",
render:container
});
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "contentBox moved from it's place in the DOM");
w.destroy();
},
"testBoundingBoxContentBoxRenderTo" : function() {
bb.append("<div id='cbTest'></div>");
boundingBox: "#bbTest",
contentBox: "#cbTest",
render:container
});
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "bb moved from it's place in the DOM");
w.destroy();
},
"testSrcNode" : function() {
container.append("<div id='srcNode'><div id='foo'></div></div>");
srcNode: "#srcNode",
render:true
});
Y.Assert.isTrue(w.get("srcNode").compareTo(sn), "srcNode should still be accessible from srcNode attribute");
Y.Assert.isTrue(w.get("boundingBox").get("parentNode").compareTo(container), "srcNode moved from it's place in the DOM");
w.destroy(true);
},
"testGetByNode" : function() {
id:"outer"
});
o.render();
ocb.append("<div><div id='outerContent'>Outer</div></div>");
id:"inner"
});
i.render();
icb.append("<div><div id='innerContent'>Inner</div></div>");
Y.Assert.areSame(o, Y.Widget.getByNode(o.get("contentBox")), "Couldn't find outer widget from outer contentBox");
Y.Assert.areSame(o, Y.Widget.getByNode(o.get("boundingBox")), "Couldn't find outer widget from outer boundingBox");
Y.Assert.areSame(i, Y.Widget.getByNode(i.get("contentBox")), "Couldn't find inner widget from inner contentBox");
Y.Assert.areSame(i, Y.Widget.getByNode(i.get("boundingBox")), "Couldn't find inner widget from inner boundingBox");
i.destroy(true);
o.destroy(true);
},
"todoTestStrings" : function() {
},
"todoTestLoading" : function() {
srcNode:"#srcNode"
});
w.render();
}
}));
name : "getSkinName",
"getSkinName should return null if not rendered" : function () {
Y.Assert.isNull( w.getSkinName() );
},
"getSkinName should return name from BB if available": function () {
cb = bb.one( 'div' ),
w = new Y.Widget( {
boundingBox: bb,
contentBox: cb
} );
},
"getSkinName should return name from body or null": function () {
body = Y.one( 'body' );
body.removeClass( "yui3-skin-sam" );
Y.Assert.isNull( w.getSkinName() );
body.addClass( "yui3-skin-sam" );
},
"getSkinName should return name from ancestor if both ancestor and body are classed": function () {
body = Y.one( 'body' );
body.addClass( "yui3-skin-sam" );
}
}));
name:"destroy",
testRenderedDestroy: function() {
try {
w.destroy();
} catch(e) {
}
},
testRenderedDeepDestroy: function() {
try {
w.destroy(true);
} catch(e) {
}
},
testUnrenderedDestroy: function() {
try {
w.destroy();
} catch(e) {
}
},
testSingleBoxDestroy: function() {
function MyWidget() {
};
MyWidget.NAME = "myWidget";
CONTENT_TEMPLATE:null
});
var w = new MyWidget({
id:"foo"
});
w.render();
try {
w.destroy();
} catch(e) {
}
},
testSingleBoxDeepDestroy: function() {
function MyWidget() {
};
MyWidget.NAME = "myWidget";
CONTENT_TEMPLATE:null
});
var w = new MyWidget({
id:"foo"
});
w.render();
try {
w.destroy(true);
} catch(e) {
}
}
}));
name:"UI Events",
testSingleSimple: function() {
var w, h, cb,
actualEvents = [],
expectedEvents = ["widget:click"];
w = new Y.Widget();
cb = w.get("contentBox");
cb.append("<p class='et'>Some Content For My Widget</p>");
w.render();
w.destroy();
},
testSingleComplex: function() {
var w, h, cb
actualEvents = [],
expectedEvents = ["widget:render",
"widget:renderedChange",
"widget:render",
"widget:mousedown",
"widget:mouseup",
"widget:mouseup",
"widget:mouseup",
"widget:mouseup",
"widget:mouseup",
"widget:mouseup",
"widget:click"];
w = new Y.Widget();
cb = w.get("contentBox");
cb.append("<p class='et'>Some Content For My Widget</p>");
w.on({
"click": h,
"render": h,
"renderedChange": h
});
w.after({
"mousedown" : h,
"render" : h
});
w.render();
w.destroy();
},
testNested: function() {
var expectedEvents = ["outerClick", "innerClick", "outerClick"];
var actualEvents = [];
outer.render();
ocb.setContent("<span class='oet'>Outer Content</span>");
inner.render(ocb);
icb.setContent("<span class='iet'>Inner Content</span>");
// Only outer
// One Inner, One Outer
},
testMultipleInstances : function() {
var actualEvents = [],
expectedEvents = ["clickOuter", "clickInner", "clickOuter"],
w1,
w2;
YUI().use('widget', function (Y) {
actualEvents.push("clickOuter");
});
YUI().use('widget', function (Y1) {
actualEvents.push("clickInner");
});
});
});
w1.destroy();
w2.destroy();
}
}));
name:"clone",
testWidgetClone : function() {
},
testWidgetHashClone : function() {
// When Widget's are properties of an object it seems to break apart
// something not passed to the recursive call maybe?
var a = new Y.Widget();
var b = new Y.Widget();
var c = new Y.Widget();
var o = {
a : a,
b : b,
c : c
};
var o1 = Y.clone(o);
var o2 = Y.clone(o1);
var o3 = Y.clone(o2);
Y.Assert.isTrue(o3.a instanceof Y.Widget);
Y.Assert.isTrue(o3.b instanceof Y.Widget);
Y.Assert.isTrue(o3.c instanceof Y.Widget);
},
testBaseClone : function() {
var a = new Y.Base();
var b = new Y.Base();
var c = new Y.Base();
// Base works fine
var a1 = Y.clone(a);
var a2 = Y.clone(a1);
var a3 = Y.clone(a2);
Y.Assert.isTrue(a instanceof Y.Base);
Y.Assert.isTrue(a1 instanceof Y.Base);
Y.Assert.isTrue(a2 instanceof Y.Base);
Y.Assert.isTrue(a3 instanceof Y.Base);
var b1 = Y.clone(b);
var b2 = Y.clone(b1);
var b3 = Y.clone(b2);
Y.Assert.isTrue(b instanceof Y.Base);
Y.Assert.isTrue(b1 instanceof Y.Base);
Y.Assert.isTrue(b2 instanceof Y.Base);
Y.Assert.isTrue(b3 instanceof Y.Base);
var c1 = Y.clone(c);
var c2 = Y.clone(c1);
var c3 = Y.clone(c2);
Y.Assert.isTrue(c instanceof Y.Base);
Y.Assert.isTrue(c1 instanceof Y.Base);
Y.Assert.isTrue(c2 instanceof Y.Base);
Y.Assert.isTrue(c3 instanceof Y.Base);
},
testBaseHashClone : function() {
var a = new Y.Base();
var b = new Y.Base();
var c = new Y.Base();
var o = {
a : a,
b : b,
c : c
};
var o1 = Y.clone(o);
var o2 = Y.clone(o1);
var o3 = Y.clone(o2);
Y.Assert.isTrue(o3.a instanceof Y.Base);
Y.Assert.isTrue(o3.b instanceof Y.Base);
Y.Assert.isTrue(o3.c instanceof Y.Base);
}
}));
Y.Test.Runner.setName("Widget Tests");
Y.Test.Runner.add(suite);
var console;
Y.one("#btnRun").set("disabled", false).on("click", function() {
if (!console) {
console = new Y.Console({
id:"console",
width:"100%",
height:"90%",
verbose : false,
printTimeout: 0,
newestOnTop : false,
entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
'<span class="{entry_cat_class}">{label}</span>'+
'<span class="{entry_content_class}">{message}</span>'+
'</pre>'
}).render();
}
});
});
</script>
</body>
</html>