aop.html revision 0a9c6f9f30a66e52ec4ea4ed93504580b3a5669a
4f2de35716c5df46152747980516af484d0ab208Adam Moore<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4f2de35716c5df46152747980516af484d0ab208Adam Moore<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
4f2de35716c5df46152747980516af484d0ab208Adam Moore<script type="text/javascript" src="/build/yui/yui.js"></script>
4f2de35716c5df46152747980516af484d0ab208Adam Moore(function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore filter: "debug",
4f2de35716c5df46152747980516af484d0ab208Adam Moore useConsole: true,
4f2de35716c5df46152747980516af484d0ab208Adam Moore // logInclude: ['event', 'test']
4f2de35716c5df46152747980516af484d0ab208Adam Moore logExclude: {Dom: true, Selector: true, Node: true, attribute: true, base: true, event: true, widget: true}
4f2de35716c5df46152747980516af484d0ab208Adam Moore }).use("dump", "test", "console", "event-custom", function(Y) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var button = Y.get('#btnRun');
4f2de35716c5df46152747980516af484d0ab208Adam Moore // Set up the page
4f2de35716c5df46152747980516af484d0ab208Adam Moore button.set("disabled", false);
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.on("click", function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var myConsole = new Y.Console().render();
4f2de35716c5df46152747980516af484d0ab208Adam Moore var testEventTarget = new Y.Test.Case({
4f2de35716c5df46152747980516af484d0ab208Adam Moore name: "EventTarget tests",
4f2de35716c5df46152747980516af484d0ab208Adam Moore testAugment: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var fired = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore var O = function(id) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.log('O constructor executed ' + id);
4f2de35716c5df46152747980516af484d0ab208Adam Moore oOo: function(ok) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore // pass configuration info into EventTarget with the following
4f2de35716c5df46152747980516af484d0ab208Adam Moore // construct
4f2de35716c5df46152747980516af484d0ab208Adam Moore emitFacade: true
4f2de35716c5df46152747980516af484d0ab208Adam Moore var o = new O(),
4f2de35716c5df46152747980516af484d0ab208Adam Moore handle = o.on('testAugment', function(e, arg1, arg2) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(this instanceof O);
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.fire('testAugment', { foo: 'afoo' }, 1, 2);
4f2de35716c5df46152747980516af484d0ab208Adam Moore // if the first argument is not an object, the
4f2de35716c5df46152747980516af484d0ab208Adam Moore // event facade is moved in front of the args rather
4f2de35716c5df46152747980516af484d0ab208Adam Moore // than overwriting existing object.
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.on('testAugment', function(e, arg1, arg2) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.fire('testAugment', 1, 2);
4f2de35716c5df46152747980516af484d0ab208Adam Moore testExtend: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var fired = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore var Base = function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.log('Base constructor executed');
4f2de35716c5df46152747980516af484d0ab208Adam Moore arguments.callee.superclass.constructor.apply(this, arguments);
4f2de35716c5df46152747980516af484d0ab208Adam Moore base: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.log('all your base...');
4f2de35716c5df46152747980516af484d0ab208Adam Moore var b = new Base();
4f2de35716c5df46152747980516af484d0ab208Adam Moore b.on('testExtend', function(arg1, arg2) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(this instanceof Base);
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore b.fire('testExtend', 1, 2);
4f2de35716c5df46152747980516af484d0ab208Adam Moore testPrefix: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var fired1 = false,
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore var O = function(id) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.log('O constructor executed ' + id);
4f2de35716c5df46152747980516af484d0ab208Adam Moore oOo: function(ok) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore // pass configuration info into EventTarget with the following
4f2de35716c5df46152747980516af484d0ab208Adam Moore // construct
4f2de35716c5df46152747980516af484d0ab208Adam Moore emitFacade: true,
4f2de35716c5df46152747980516af484d0ab208Adam Moore prefix: 'prefix'
4f2de35716c5df46152747980516af484d0ab208Adam Moore var o = new O();
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.on('testPrefix', function(e, arg1, arg2) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(this instanceof O);
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.on('prefix:testPrefix', function(e, arg1, arg2) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(this instanceof O);
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.fire('testPrefix', { foo: 'afoo' }, 1, 2);
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore o.fire('prefix:testPrefix', { foo: 'afoo' }, 1, 2);
4f2de35716c5df46152747980516af484d0ab208Adam Moore testDetachKey: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var fired1 = false,
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.on('handle, test:event', function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // one listener
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.fire('test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.detach('handle, test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.on('handle, test:event', function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // first lisener detached, added a new listener
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.fire('test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.detach('handle,test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.after('handle, test:event', function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // comma or pipe
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.on('handle|test:event', function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // testing on and after order
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.fire('test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // spaces after the comma or lack thereof should have
4f2de35716c5df46152747980516af484d0ab208Adam Moore // no effect on the addition or removal of listeners
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.detach('handle,test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore // added both an on listener and an after listener,
4f2de35716c5df46152747980516af484d0ab208Adam Moore // then detached both
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.fire('test:event');
4f2de35716c5df46152747980516af484d0ab208Adam Moore testChain: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var fired1 = false,
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = false,
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired3 = false,
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired4 = false,
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired5 = false;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // should be executed once, after f2
4f2de35716c5df46152747980516af484d0ab208Adam Moore var f1 = function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired1 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // should be executed once, before f1
4f2de35716c5df46152747980516af484d0ab208Adam Moore var f2 = function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired2 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // should be executed once, different event from f1 and f2
4f2de35716c5df46152747980516af484d0ab208Adam Moore var f3 = function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired3 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // detached before fired, should not executed
4f2de35716c5df46152747980516af484d0ab208Adam Moore var f4 = function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired4 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // should fire once, preserving the custom prefix rather
4f2de35716c5df46152747980516af484d0ab208Adam Moore // than using the configured event target prefix
4f2de35716c5df46152747980516af484d0ab208Adam Moore var f5 = function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore fired5 = true;
4f2de35716c5df46152747980516af484d0ab208Adam Moore // configure chaining via global default or on the event target
4f2de35716c5df46152747980516af484d0ab208Adam Moore YUI({ /* chain: true */ }).use('*', function(Y2) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore prefix: 'foo',
4f2de35716c5df46152747980516af484d0ab208Adam Moore chain : true
4f2de35716c5df46152747980516af484d0ab208Adam Moore // without event target prefix manipulation (incomplete now)
4f2de35716c5df46152747980516af484d0ab208Adam Moore // @TODO an error here is throwing an uncaught exception rather than failing the test
4f2de35716c5df46152747980516af484d0ab208Adam Moore // 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');
4f2de35716c5df46152747980516af484d0ab208Adam Moore // with event target prefix manipulation ('e' is the same event as 'foo:e',
4f2de35716c5df46152747980516af484d0ab208Adam Moore // but 'pre:e' is a different event only accessible by using that exact name)
4f2de35716c5df46152747980516af484d0ab208Adam Mooreo.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');
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(fired1); // verifies chaining, on/after order, and adding the event target prefix
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(fired2); // verifies chaining, on/after order, and accepting the prefix in the event name
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(fired3); // verifies no interaction between events, and prefix manipulation
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isFalse(fired4); // verifies detach works (regardless of spaces after comma)
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Assert.isTrue(fired5); // verifies custom prefix
4f2de35716c5df46152747980516af484d0ab208Adam Moore testObjType: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore 'y:click': function() {f1 = true},
4f2de35716c5df46152747980516af484d0ab208Adam Moore 'y:clack': function() {f2 = true}
4f2de35716c5df46152747980516af484d0ab208Adam Moore testPreventFnOnce: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore var count = 0;
4f2de35716c5df46152747980516af484d0ab208Adam Moore emitFacade: true,
4f2de35716c5df46152747980516af484d0ab208Adam Moore preventedFn: function() {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.on('y:foo', function(e) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.on('y:foo', function(e) {
4f2de35716c5df46152747980516af484d0ab208Adam Moore Y.Test.Runner.add(testEventTarget);