customevent.html revision bd6676c46a56d23b5e6f4702054bbd52e3d6f05f
<html>
<head>
<title>YUI Event Tests</title>
</head>
<body class="yui-skin-sam">
<h1>Event Tests</h1>
<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
<script type="text/javascript">
(function() {
YUI({
base: "/build/",
filter: "debug",
useConsole: true,
// logInclude: ['event', 'test']
// logExclude: ['Dom', 'Selector', 'Node']
}).use("dump", "test", "console", "event-custom", function(Y) {
var button = Y.get('#btnRun');
// Set up the page
button.set("disabled", false);
Y.on("click", function() {
}, button);
var myConsole = new Y.Console().render();
var testEventTarget = new Y.Test.Case({
name: "Event.Target tests",
testAugment: function() {
var fired = false;
var O = function(id) {
this.id = id;
Y.log('O constructor executed ' + id);
}
O.prototype = {
oOo: function(ok) {
Y.log('oOo');
}
}
// pass configuration info into Event.Target with the following
// construct
Y.augment(O, Y.Event.Target, null, null, {
emitFacade: true
});
var o = new O();
o.on('testAugment', function(e, arg1, arg2) {
Y.Assert.isTrue(this instanceof O);
Y.Assert.isTrue(e instanceof Y.Event.Facade);
Y.Assert.isTrue(e.foo === 'afoo');
Y.Assert.isTrue(e.details[1] === 1);
Y.Assert.isTrue(arg1 === 1);
Y.Assert.isTrue(arg2 === 2);
fired = true;
});
o.fire('testAugment', { foo: 'afoo' }, 1, 2);
Y.Assert.isTrue(fired);
},
testExtend: function() {
var fired = false;
var Base = function() {
Y.log('Base constructor executed');
arguments.callee.superclass.constructor.apply(this, arguments);
}
Y.extend(Base, Y.Event.Target, {
base: function() {
Y.log('all your base...');
}
});
var b = new Base();
b.on('testExtend', function(arg1, arg2) {
Y.Assert.isTrue(this instanceof Base);
Y.Assert.isTrue(arg1 === 1);
Y.Assert.isTrue(arg2 === 2);
fired = true;
});
b.fire('testExtend', 1, 2);
Y.Assert.isTrue(fired);
},
testPrefix: function() {
var fired1 = false,
fired2 = false;
var O = function(id) {
this.id = id;
Y.log('O constructor executed ' + id);
}
O.prototype = {
oOo: function(ok) {
Y.log('oOo');
}
}
// pass configuration info into Event.Target with the following
// construct
Y.augment(O, Y.Event.Target, null, null, {
emitFacade: true,
prefix: 'prefix'
});
var o = new O();
o.on('testPrefix', function(e, arg1, arg2) {
Y.Assert.isTrue(this instanceof O);
fired1 = true;
});
o.on('prefix:testPrefix', function(e, arg1, arg2) {
Y.Assert.isTrue(this instanceof O);
fired2 = true;
});
o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
Y.Assert.isTrue(fired1);
// Y.Assert.isTrue(fired2);
fired1 = false;
fired2 = false;
o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
Y.Assert.isTrue(fired1);
Y.Assert.isTrue(fired2);
},
testDetachKey: function() {
var fired1 = false,
fired2 = false;
Y.on('handle, test:event', function() {
fired1 = true;
});
// one listener
Y.fire('test:event');
Y.Assert.isTrue(fired1);
Y.Assert.isFalse(fired2);
Y.detach('handle, test:event');
fired1 = false;
fired2 = false;
Y.on('handle, test:event', function() {
fired2 = true;
});
// first lisener detached, added a new listener
Y.fire('test:event');
Y.Assert.isFalse(fired1);
Y.Assert.isTrue(fired2);
Y.detach('handle,test:event');
fired1 = false;
fired2 = false;
Y.after('handle, test:event', function() {
Y.Assert.isTrue(fired1);
fired2 = true;
});
Y.on('handle, test:event', function() {
Y.Assert.isFalse(fired2);
fired1 = true;
});
// testing on and after order
Y.fire('test:event');
fired1 = false;
fired2 = false;
// spaces after the comma or lack thereof should have
// no effect on the addition or removal of listeners
Y.detach('handle,test:event');
// added both an on listener and an after listener,
// then detached both
Y.fire('test:event');
Y.Assert.isFalse(fired1);
Y.Assert.isFalse(fired2);
},
testChain: function() {
YUI({ chainOn: true }).use('*', function(Y) {
var fired1 = false,
fired2 = false,
fired3 = false;
var f1 = function() {
Y.Assert.isTrue(fired2);
fired1 = true;
};
var f2 = function() {
Y.Assert.isFalse(fired1);
fired2 = true;
};
var f3 = function() {
fired3 = true;
};
Y.after('p:e', f1).on('p:e', f2).on('p:e2', f3).fire('p:e').fire('p:e2');
Y.Assert.isTrue(fired1);
Y.Assert.isTrue(fired2);
Y.Assert.isTrue(fired3);
});
}
});
Y.Test.Runner.add(testEventTarget);
});
})();
</script>
</body>
</html>