dom.html revision 3641f0baf10c9737e4ac6aac1566bfeaca00eeff
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney<html>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney<head>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <title>YUI DOM Event Tests</title>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney</head>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney<body class="yui-skin-sam">
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <div id="doc">
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <div id="clickcontainer">
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <p id="clicker1" class="clickers">click1</p>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <p id="clicker2" class="clickers">click2</p>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney </div>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney </div>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <!-- YUI3 Core //-->
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <script type="text/javascript" src="/build/yui/yui-debug.js"></script>
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney <!-- Initialization process //-->
1dac070d5991af1a83315be5c43374921abe4aeaMatt Sweeney <script type="text/javascript">
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney YUI({
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney base: "/build/",
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney filter: 'debug',
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney combine: false,
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney useConsole: true,
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney logExclude: {deprecated:1, yui:true, get: true, 'selector-native': true, loader: true, Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney }).use('console', 'test', 'node', 'event-simulate', function (Y) {
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney // creating the console...
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney (new Y.Console()).render();
1dac070d5991af1a83315be5c43374921abe4aeaMatt Sweeney
1dac070d5991af1a83315be5c43374921abe4aeaMatt Sweeney // starting the testing process
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
1dac070d5991af1a83315be5c43374921abe4aeaMatt Sweeney // add the test cases and suites
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney Y.Test.Runner.add(new Y.Test.Case({
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney name: "DOM Event Tests",
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney test_on_with_string: function() {
c7a0918bf9dc49db2a98ee5ec43a5e139e938f1cMatt Sweeney
var clicked = false,
context,
ex1,
ex2,
obj = {
a: 1
};
Y.on('click', function(e, extra1, extra2) {
clicked = true;
context = this;
ex1 = extra1;
ex2 = extra2;
}, '#clickcontainer', obj, 2, 3);
Y.Event.simulate(document.getElementById('clickcontainer'), 'click');
Y.Assert.isTrue(clicked, "click handler via Y.on didn't work when passed a string.");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
},
test_on_with_node: function() {
var clicked = false,
context,
ex1,
ex2,
obj = {
a: 1
},
node = Y.get('#clickcontainer');
Y.on('click', function(e, extra1, extra2) {
clicked = true;
context = this;
ex1 = extra1;
ex2 = extra2;
}, node, obj, 2, 3);
Y.Event.simulate(document.getElementById('clickcontainer'), 'click');
Y.Assert.isTrue(clicked, "click handler via Y.on didn't work when passed a node.");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
},
test_node_on: function() {
var clicked = false,
context,
ex1,
ex2,
obj = {
a: 1
},
node = Y.get('#clickcontainer');
node.on('click', function(e, extra1, extra2) {
clicked = true;
context = this;
ex1 = extra1;
ex2 = extra2;
}, obj, 2, 3);
Y.Event.simulate(document.getElementById('clickcontainer'), 'click');
Y.Assert.isTrue(clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
},
test_node_detachall: function() {
var clicked = false, overed = false,
node = Y.get('#clickcontainer');
node.on('click', function(e, extra1, extra2) {
clicked = true;
});
node.on('mouseover', function(e, extra1, extra2) {
overed = true;
});
node.detachAll();
Y.Event.simulate(document.getElementById('clickcontainer'), 'click');
Y.Assert.isFalse(clicked, "click handler was not removed");
Y.Assert.isFalse(overed, "mouseover handler was not removed");
},
test_nodelist_on: function() {
var clicked = 0,
context,
ex1,
ex2,
obj = {
a: 1
},
nodelist = Y.all('.clickers');
nodelist.on('click', function(e, extra1, extra2) {
clicked++;
context = this;
ex1 = extra1;
ex2 = extra2;
}, obj, 2, 3);
Y.Event.simulate(document.getElementById('clicker1'), 'click');
Y.Assert.areEqual(1, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work"); // bug #2528155
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work"); // bug #2528155
Y.Event.simulate(document.getElementById('clicker2'), 'click');
Y.Assert.areEqual(2, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work"); // bug #2528155
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work"); // bug #2528155
},
test_y_on_with_nodelist: function() {
var clicked = 0,
context,
ex1,
ex2,
obj = {
a: 1
},
nodelist = Y.all('.clickers');
Y.on('click', function(e, extra1, extra2) {
clicked++;
context = this;
ex1 = extra1;
ex2 = extra2;
}, nodelist, obj, 2, 3);
Y.Event.simulate(document.getElementById('clicker1'), 'click');
Y.Assert.areEqual(1, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
Y.Event.simulate(document.getElementById('clicker2'), 'click');
Y.Assert.areEqual(2, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
},
test_y_on_with_selector_multiple_match: function() {
var clicked = 0,
context,
ex1,
ex2,
obj = {
a: 1
};
Y.on('click', function(e, extra1, extra2) {
clicked++;
context = this;
ex1 = extra1;
ex2 = extra2;
}, '.clickers', obj, 2, 3);
Y.Event.simulate(document.getElementById('clicker1'), 'click');
Y.Assert.areEqual(1, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
Y.Event.simulate(document.getElementById('clicker2'), 'click');
Y.Assert.areEqual(2, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
},
test_y_on_with_htmlelementcollection: function() {
var clicked = 0,
context,
ex1,
ex2,
obj = {
a: 1
};
Y.on('click', function(e, extra1, extra2) {
clicked++;
context = this;
ex1 = extra1;
ex2 = extra2;
}, document.getElementsByTagName('p'), obj, 2, 3);
Y.Event.simulate(document.getElementById('clicker1'), 'click');
Y.Assert.areEqual(1, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
Y.Event.simulate(document.getElementById('clicker2'), 'click');
Y.Assert.areEqual(2, clicked, "click handler didn't work");
Y.Assert.areEqual(1, context.a, "context didn't work");
Y.Assert.areEqual(2, ex1, "extra arg1 didn't work");
Y.Assert.areEqual(3, ex2, "extra arg2 didn't work");
},
test_detach_by_sig: function() {
var clicked = 0,
context,
ex1,
ex2,
obj = {
a: 1
}, fn = function(e, extra1, extra2) {
clicked++;
context = this;
ex1 = extra1;
ex2 = extra2;
},
els = document.getElementsByTagName('p');
Y.on('click', fn, 'clicker1', obj, 2, 3);
Y.Event.detach('click', fn, 'clicker1', obj, 2, 3);
Y.Event.simulate(document.getElementById('clicker1'), 'click');
Y.Assert.areEqual(0, clicked, "detach by signature was a failure");
},
test_multi_detach: function() {
var clicked = 0,
context,
ex1,
ex2,
obj = {
a: 1
}, fn = function(e, extra1, extra2) {
clicked++;
context = this;
ex1 = extra1;
ex2 = extra2;
},
els = document.getElementsByTagName('p');
var handle = Y.on('click', fn, '.clickers', obj, 2, 3);
// Y.log(handle);
handle.detach();
Y.Event.simulate(document.getElementById('clicker1'), 'click');
Y.Event.simulate(document.getElementById('clicker2'), 'click');
Y.Assert.areEqual(0, clicked, "single detach handle for multiple listeners doesn't work.");
},
test_onavail_detach: function() {
var avail = 0,
handle = Y.on('available', function() {
avail++;
}, '#notthereyet'),
div = document.createElement("div");
div.id = 'notthereyet';
handle.detach();
document.body.appendChild(div);
this.wait(function() {
Y.Assert.areEqual(0, avail, "onavailable wasn't canceled.");
}, 300);
},
test_lazy_attach_detach: function() {
var avail = 0,
handle = Y.on('click', function() {
avail++;
}, '#notthereyet'),
div = document.createElement("div");
div.id = 'notthereyet';
document.body.appendChild(div);
this.wait(function() {
handle.detach();
Y.Event.simulate(document.getElementById('notthereyet'), 'click');
Y.Assert.areEqual(0, avail, "lazy detacher didn't work.");
}, 300);
}
}));
//run all tests
Y.Test.Runner.run();
/* finishing the testing process */
});
</script>
</body>
</html>