customevent.html revision efa57736d44cf446f1661497a8645bd388b493fb
0N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
1331N/A<html>
0N/A<head>
0N/A<title>YUI Event Tests</title>
0N/A<script type="text/javascript" src="/build/yui/yui-debug.js"></script>
0N/A</head>
0N/A
0N/A<body class="yui-skin-sam">
0N/A<h1>Event Tests</h1>
0N/A<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
0N/A
0N/A
0N/A<script type="text/javascript">
0N/A
0N/A(function() {
0N/A //YUI.add("selector-native", function(){});
0N/A YUI({
0N/A filter: "debug",
0N/A // combine: false,
0N/A useConsole: true,
0N/A onCSS: function(Y) {
0N/A Y.log('CSS is done loading');
0N/A },
0N/A // logInclude: ['event', 'test']
0N/A logExclude: {
0N/A 'deprecated': 1,
0N/A get: true,
0N/A Dom: true,
0N/A Selector: true,
0N/A Node: true,
0N/A yui: true,
0N/A attribute: true,
0N/A base: true,
0N/A loader: true,
0N/A widget: true
0N/A },
0N/A
0N/A filters: {
0N/A base: 'raw',
0N/A // dom: null,
0N/A attribute: 'min'
0N/A }
0N/A }).use("dump", "test", "console", function(Y) {
0N/A
0N/A
0N/A // Y.Global.on('yui:log', function(e) {
0N/A // console.log('GLOBAL LOG: ' + e.msg);
0N/A // });
0N/A
0N/A var button = Y.get('#btnRun');
0N/A
0N/A // Set up the page
0N/A button.set("disabled", false);
0N/A Y.on("click", function() {
0N/A Y.Test.Runner.run();
0N/A }, button);
0N/A
0N/A
0N/A var myConsole = new Y.Console().render();
0N/A Y.log('{}');
0N/A
0N/A var testEventTarget = new Y.Test.Case({
0N/A name: "EventTarget tests",
0N/A
0N/A testAugment: function() {
0N/A
0N/A var fired = false;
0N/A
0N/A var O = function(id) {
0N/A this.id = id;
0N/A Y.log('O constructor executed ' + id);
0N/A }
0N/A
0N/A O.prototype = {
0N/A oOo: function(ok) {
0N/A Y.log('oOo');
0N/A }
0N/A }
0N/A
0N/A // pass configuration info into EventTarget with the following
0N/A // construct
0N/A Y.augment(O, Y.EventTarget, null, null, {
0N/A emitFacade: true
0N/A });
0N/A
0N/A var o = new O(),
0N/A handle = o.on('testAugment', function(e, arg1, arg2) {
0N/A Y.Assert.isTrue(this instanceof O);
0N/A Y.Assert.isTrue(e instanceof Y.Event.Facade);
0N/A Y.Assert.isTrue(e.foo === 'afoo');
0N/A Y.Assert.isTrue(e.details[1] === 1);
0N/A Y.Assert.isTrue(arg1 === 1);
0N/A Y.Assert.isTrue(arg2 === 2);
0N/A fired = true;
0N/A });
0N/A
0N/A o.fire('testAugment', { foo: 'afoo' }, 1, 2);
0N/A
0N/A Y.Assert.isTrue(fired);
0N/A
0N/A handle.detach();
0N/A
0N/A // if the first argument is not an object, the
0N/A // event facade is moved in front of the args rather
0N/A // than overwriting existing object.
0N/A o.on('testAugment', function(e, arg1, arg2) {
0N/A Y.Assert.areEqual(1, arg1);
0N/A Y.Assert.areEqual(2, arg2);
0N/A });
0N/A
0N/A o.fire('testAugment', 1, 2);
0N/A
0N/A },
0N/A
0N/A testExtend: function() {
0N/A
0N/A var fired = false;
0N/A
0N/A var Base = function() {
0N/A Y.log('Base constructor executed');
0N/A arguments.callee.superclass.constructor.apply(this, arguments);
0N/A }
0N/A
0N/A Y.extend(Base, Y.EventTarget, {
0N/A base: function() {
0N/A Y.log('all your base...');
0N/A }
0N/A });
0N/A
0N/A var b = new Base();
0N/A b.on('testExtend', function(arg1, arg2) {
0N/A Y.Assert.isTrue(this instanceof Base);
0N/A Y.Assert.isTrue(arg1 === 1);
0N/A Y.Assert.isTrue(arg2 === 2);
0N/A fired = true;
0N/A });
0N/A
0N/A b.fire('testExtend', 1, 2);
0N/A
0N/A Y.Assert.isTrue(fired);
0N/A },
0N/A
0N/A testPrefix: function() {
0N/A
0N/A var fired1 = false,
0N/A fired2 = false;
0N/A
0N/A var O = function(id) {
0N/A this.id = id;
0N/A Y.log('O constructor executed ' + id);
0N/A }
0N/A
0N/A O.prototype = {
0N/A oOo: function(ok) {
0N/A Y.log('oOo');
0N/A }
0N/A }
0N/A
0N/A // pass configuration info into EventTarget with the following
0N/A // construct
0N/A Y.augment(O, Y.EventTarget, null, null, {
0N/A emitFacade: true,
0N/A prefix: 'prefix'
0N/A });
0N/A
0N/A var o = new O();
0N/A o.on('testPrefix', function(e, arg1, arg2) {
0N/A Y.Assert.isTrue(this instanceof O);
0N/A fired1 = true;
0N/A });
0N/A
0N/A o.on('prefix:testPrefix', function(e, arg1, arg2) {
0N/A Y.Assert.isTrue(this instanceof O);
0N/A fired2 = true;
0N/A });
0N/A
0N/A o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
0N/A
0N/A Y.Assert.isTrue(fired1);
0N/A // Y.Assert.isTrue(fired2);
0N/A
0N/A fired1 = false;
0N/A fired2 = false;
0N/A
0N/A o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
0N/A Y.Assert.isTrue(fired1);
0N/A Y.Assert.isTrue(fired2);
0N/A },
0N/A
0N/A testDetachKey: function() {
0N/A
0N/A var fired1 = false,
0N/A fired2 = false;
0N/A
0N/A Y.on('handle|test:event', function() {
0N/A fired1 = true;
0N/A });
0N/A
0N/A // one listener
0N/A Y.fire('test:event');
0N/A Y.Assert.isTrue(fired1);
0N/A Y.Assert.isFalse(fired2);
1331N/A
0N/A Y.detach('handle|test:event');
0N/A
0N/A fired1 = false;
0N/A fired2 = false;
1331N/A
0N/A Y.on('handle|test:event', function() {
0N/A fired2 = true;
0N/A });
0N/A
0N/A // first lisener detached, added a new listener
0N/A Y.fire('test:event');
0N/A Y.Assert.isFalse(fired1);
0N/A Y.Assert.isTrue(fired2);
0N/A
0N/A Y.detach('handle|test:event');
0N/A fired1 = false;
0N/A fired2 = false;
0N/A
0N/A Y.after('handle|test:event', function(arg1) {
0N/A Y.Assert.areEqual('orange', arg1);
0N/A Y.Assert.isTrue(fired1);
0N/A fired2 = true;
0N/A });
0N/A
0N/A // comma or pipe
0N/A Y.on('handle|test:event', function(arg1) {
0N/A Y.Assert.areEqual('orange', arg1);
0N/A Y.Assert.isFalse(fired2);
0N/A fired1 = true;
0N/A });
0N/A
0N/A // testing on and after order
0N/A Y.fire('test:event', 'orange');
0N/A
0N/A fired1 = false;
0N/A fired2 = false;
0N/A
0N/A // spaces after the comma or lack thereof should have
0N/A // no effect on the addition or removal of listeners
0N/A Y.detach('handle|test:event');
0N/A
0N/A // added both an on listener and an after listener,
0N/A // then detached both
0N/A Y.fire('test:event', 'orange');
0N/A Y.Assert.isFalse(fired1);
0N/A Y.Assert.isFalse(fired2);
0N/A
0N/A },
0N/A
0N/A testDetachAllByKey: function() {
0N/A
0N/A var fired1 = false,
0N/A fired2 = false;
0N/A
0N/A Y.after('handle|event2', function() {
0N/A fired2 = true;
0N/A });
0N/A
0N/A Y.on('handle|event2', function() {
0N/A fired1 = true;
0N/A });
0N/A
0N/A // detachAll
0N/A Y.detach('handle|*');
0N/A
0N/A Y.fire('event2');
0N/A
0N/A Y.Assert.isFalse(fired1, 'fired1, the after listener should not have fired.');
0N/A Y.Assert.isFalse(fired2, 'fired2, the on listener should not have fired.');
0N/A
0N/A },
0N/A
0N/A testChain: function() {
0N/A
0N/A var fired1 = false,
0N/A fired2 = false,
0N/A fired3 = false,
0N/A fired4 = false,
0N/A fired5 = false;
0N/A
0N/A // should be executed once, after f2
0N/A var f1 = function() {
0N/A Y.Assert.isTrue(fired2);
0N/A fired1 = true;
0N/A };
0N/A
0N/A // should be executed once, before f1
0N/A var f2 = function() {
0N/A Y.Assert.isFalse(fired1);
0N/A fired2 = true;
0N/A };
0N/A
0N/A // should be executed once, different event from f1 and f2
0N/A var f3 = function() {
0N/A fired3 = true;
0N/A };
0N/A
0N/A // detached before fired, should not executed
0N/A var f4 = function() {
0N/A fired4 = true;
0N/A };
0N/A
0N/A // should fire once, preserving the custom prefix rather
0N/A // than using the configured event target prefix
0N/A var f5 = function() {
0N/A fired5 = true;
0N/A };
0N/A
0N/A // configure chaining via global default or on the event target
0N/A YUI({ /* chain: true */
0N/A base:'/build/'
0N/A }).use('*', function(Y2) {
0N/A
0N/A var o = new Y2.EventTarget({
0N/A prefix: 'foo',
0N/A chain : true
0N/A });
0N/A
0N/A // without event target prefix manipulation (incomplete now)
0N/A // @TODO an error here is throwing an uncaught exception rather than failing the test
0N/A // Y2.after('p:e', f1).on('p:e', f2).on('p:e2', f3).on('detach, p:e', f4).detach('detach, p:e').fire('p:e').fire('p:e2');
0N/A
0N/A // with event target prefix manipulation ('e' is the same event as 'foo:e',
0N/A // but 'pre:e' is a different event only accessible by using that exact name)
0N/Ao.after('e', f1).on('foo:e', f2).on('foo:e2', f3).on('detach, e', f4).detach('detach,e').fire('foo:e').fire('e2').on('pre:e', f5).fire('pre:e');
0N/A
0N/A Y.Assert.isTrue(fired1); // verifies chaining, on/after order, and adding the event target prefix
0N/A Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name
0N/A Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation
0N/A Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
0N/A Y.Assert.isTrue(fired5); // verifies custom prefix
0N/A
0N/A });
0N/A
0N/A },
0N/A
0N/A testObjType: function() {
0N/A var f1, f2;
0N/A Y.on({
0N/A 'y:click': function() {f1 = true},
0N/A 'y:clack': function() {f2 = true}
0N/A });
0N/A
0N/A Y.fire('y:click');
0N/A Y.fire('y:clack');
0N/A
0N/A Y.Assert.isTrue(f1);
0N/A Y.Assert.isTrue(f2);
0N/A },
0N/A
0N/A testBubble: function() {
0N/A var count = 0,
0N/A ret,
0N/A config = {
0N/A emitFacade: true,
0N/A bubbles: true
0N/A },
0N/A a = new Y.EventTarget(config),
0N/A b = new Y.EventTarget(config);
0N/A
0N/A b.addTarget(a);
0N/A
0N/A // this should not be necessary
0N/A b.publish('test:foo');
0N/A
0N/A a.on('test:foo', function() {
0N/A count++;
0N/A });
0N/A
0N/A ret = b.fire('test:foo', {}, b);
0N/A
0N/A Y.Assert.areEqual(1, count);
0N/A Y.Assert.isTrue(ret);
0N/A
0N/A b.on('test:foo', function(e) {
0N/A e.stopPropagation();
0N/A });
0N/A
0N/A ret = b.fire('test:foo', {}, b);
0N/A
0N/A Y.Assert.areEqual(1, count);
0N/A Y.Assert.isFalse(ret);
0N/A },
0N/A
0N/A testPreventFnOnce: function() {
0N/A var count = 0;
0N/A Y.publish('y:foo1', {
0N/A emitFacade: true,
0N/A preventedFn: function() {
0N/A count++;
0N/A Y.Assert.isTrue(this instanceof YUI);
0N/A }
0N/A });
0N/A
0N/A Y.on('y:foo1', function(e) {
0N/A e.preventDefault();
0N/A });
0N/A
0N/A Y.on('y:foo1', function(e) {
0N/A e.preventDefault();
0N/A });
0N/A
0N/A Y.fire('y:foo1');
0N/A
0N/A Y.Assert.areEqual(1, count);
0N/A },
0N/A
0N/A testDetachHandle: function() {
0N/A var count = 0, handle, handle2;
0N/A Y.publish('y:foo', {
0N/A emitFacade: true
0N/A });
0N/A
0N/A Y.on('y:foo', function(e) {
0N/A count++;
0N/A handle2.detach();
0N/A });
0N/A
0N/A handle = Y.on('y:foo', function(e) {
0N/A count += 100;
0N/A });
0N/A
0N/A handle2 = Y.on('y:foo', function(e) {
0N/A count += 1000;
0N/A });
0N/A
0N/A Y.detach(handle);
0N/A
0N/A Y.fire('y:foo');
0N/A
0N/A Y.Assert.areEqual(1, count);
0N/A
0N/A count = 0;
0N/A
0N/A var handle3 = Y.on('y:click', function() {
0N/A count++;
0N/A handle3.detach();
0N/A });
0N/A
0N/A Y.fire('y:click');
0N/A Y.fire('y:click');
0N/A
0N/A var o = new Y.EventTarget();
0N/A
0N/A count = 0;
0N/A
0N/A o.on('foo', function(e) {
0N/A count++;
0N/A });
0N/A
0N/A o.on('foo', function(e) {
0N/A count++;
0N/A });
0N/A
0N/A o.detachAll();
0N/A
0N/A o.fire('foo');
0N/A
0N/A Y.Assert.areEqual(0, count);
0N/A
0N/A var handle3 = Y.on('y:click', function() {
0N/A count++;
0N/A });
0N/A
0N/A // detachAll can't be allowed to work on the YUI instance.
0N/A Y.detachAll();
0N/A
0N/A Y.fire('y:click');
0N/A
0N/A Y.Assert.areEqual(1, count);
0N/A },
0N/A
0N/A testBroadcast: function() {
0N/A var o = new Y.EventTarget(), s1, s2, s3, s4;
0N/A
0N/A o.publish('y:foo2', {
0N/A emitFacade: true,
0N/A broadcast: 1
0N/A });
0N/A
0N/A Y.on('y:foo2', function() {
0N/A Y.log('Y foo2 executed');
0N/A s1 = 1;
0N/A });
0N/A
0N/A Y.Global.on('y:foo2', function() {
0N/A Y.log('GLOBAL foo2 executed');
0N/A s2 = 1;
0N/A });
0N/A
0N/A o.fire('y:foo2');
0N/A
0N/A Y.Assert.areEqual(1, s1);
0N/A Y.Assert.areNotEqual(1, s2);
0N/A
0N/A s1 = 0;
0N/A s2 = 0;
0N/A
0N/A o.publish('y:bar', {
0N/A emitFacade: true,
0N/A broadcast: 2
0N/A });
0N/A
0N/A Y.on('y:bar', function() {
0N/A Y.log('Y bar executed');
0N/A s3 = 1;
0N/A });
0N/A
0N/A Y.Global.on('y:bar', function() {
0N/A Y.log('GLOBAL bar executed');
0N/A s4 = 1;
0N/A });
0N/A
0N/A o.fire('y:bar');
0N/A
0N/A Y.Assert.areEqual(1, s3);
0N/A Y.Assert.areEqual(1, s4);
0N/A }
0N/A
0N/A });
0N/A
0N/A Y.Test.Runner.add(testEventTarget);
0N/A Y.Test.Runner.run();
0N/A });
0N/A
0N/A // YUI({
0N/A // base: "/build/",
0N/A // filter: "debug",
0N/A // combine: false,
0N/A // useConsole: true,
0N/A // logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, loader: true, get: true, widget: true}
0N/A // }).use("datasource", function(Y) {
0N/A // Y.log('loaded datasource: ' + Y.DataSource);
0N/A // });
0N/A
0N/A
0N/A})();
0N/A</script>
0N/A</body>
0N/A</html>
0N/A