Permalink
Browse files

More tests for the various adapters, made prototype-adapter use names…

…paced events
  • Loading branch information...
1 parent 910ef8e commit 798880a5c24518a36fde2d62714958e164e3487e @eolsson eolsson committed Jul 11, 2011
Showing with 185 additions and 35 deletions.
  1. +19 −4 js/adapters/prototype-adapter.src.js
  2. +166 −31 test/unit/AdaptersTest.js
@@ -84,11 +84,23 @@ return {
});
}
},
-
+
+ /**
+ * Custom events in prototype needs to be namespaced. This method adds a namespace 'h:' in front of
+ * events that are not recognized as native.
+ */
+ addNS: function(eventName) {
+ var HTMLEvents = /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
+ MouseEvents = /^(?:click|mouse(?:down|up|over|move|out))$/;
+ return (HTMLEvents.test(eventName) || MouseEvents.test(eventName)) ?
+ eventName :
+ 'h:' + eventName;
+ },
+
// el needs an event to be attached. el is not necessarily a dom element
addEvent: function(el, event, fn) {
if (el.addEventListener || el.attachEvent) {
- Event.observe($(el), event, fn);
+ Event.observe($(el), HighchartsAdapter.addNS(event), fn);
} else {
HighchartsAdapter._extend(el);
@@ -137,12 +149,12 @@ return {
// fire an event based on an event name (event) and an object (el).
// again, el may not be a dom element
fireEvent: function(el, event, eventArguments, defaultFunction){
- if (event.preventDefault) {
+ if (event.preventDefault) { // TODO: event is a string here so this doesnt make sense at all
defaultFunction = null;
}
if (el.fire) {
- el.fire(event, eventArguments);
+ el.fire(HighchartsAdapter.addNS(event), eventArguments);
} else if (el._highcharts_extended) {
el._highcharts_fire(event, eventArguments);
}
@@ -154,6 +166,9 @@ return {
removeEvent: function(el, event, handler){
if ($(el).stopObserving) {
+ if (event) {
+ event = HighchartsAdapter.addNS(event);
+ }
$(el).stopObserving(event, handler);
} else {
HighchartsAdapter._extend(el);
@@ -104,19 +104,19 @@ AdaptersTest.prototype.testObjectEventSelfRemove = function() {
var o = {clickedCount: 0},
f = function() {
o.clickedCount++;
- removeEvent(o, 'myEvent', f);
+ removeEvent(o, 'customEvent', f);
};
// Setup event handler
- addEvent(o, 'myEvent', f);
+ addEvent(o, 'customEvent', f);
assertEquals('not yet clicked', 0, o.clickedCount);
// Fire it once
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('now clicked', 1, o.clickedCount);
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('clicked again, no change', 1, o.clickedCount);
};
@@ -125,7 +125,7 @@ AdaptersTest.prototype.testObjectEventSelfRemove = function() {
*
* The counter is just a property of an object.
*/
-AdaptersTest.prototype.tXstObjectEventChainedRemove = function() {
+AdaptersTest.prototype.testObjectEventChainedRemove = function() {
var o = {clickedCount: 0},
f = function() {
o.clickedCount++;
@@ -136,7 +136,6 @@ AdaptersTest.prototype.tXstObjectEventChainedRemove = function() {
// remove it on chart destroy
addEvent(document, 'outerEvent', function() {
- jstestdriver.console.log('about to remove innerEvent');
removeEvent(o, 'innerEvent', f);
});
@@ -167,18 +166,18 @@ AdaptersTest.prototype.testObjectEventRemoveAll = function() {
};
// Setup event handler
- addEvent(o, 'myEvent', f);
+ addEvent(o, 'customEvent', f);
assertEquals('not yet clicked', 0, o.clickedCount);
// Fire it once
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('now clicked', 1, o.clickedCount);
// Remove all handlers
removeEvent(o);
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('clicked again, no change', 1, o.clickedCount);
};
@@ -195,18 +194,18 @@ AdaptersTest.prototype.testObjectEventRemoveType = function() {
};
// Setup event handler
- addEvent(o, 'myEvent', f);
+ addEvent(o, 'customEvent', f);
assertEquals('not yet clicked', 0, o.clickedCount);
// Fire it once
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('now clicked', 1, o.clickedCount);
// Remove the handler (Only specifying event type)
- removeEvent(o, 'myEvent');
+ removeEvent(o, 'customEvent');
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('clicked again, no change', 1, o.clickedCount);
};
@@ -223,18 +222,18 @@ AdaptersTest.prototype.testObjectEventRemoveHandler = function() {
};
// Setup event handler
- addEvent(o, 'myEvent', f);
+ addEvent(o, 'customEvent', f);
assertEquals('not yet clicked', 0, o.clickedCount);
// Fire it once
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('now clicked', 1, o.clickedCount);
// Remove the handler (Most fine-grained)
- removeEvent(o, 'myEvent', f);
+ removeEvent(o, 'customEvent', f);
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'myEvent', null, null);
+ fireEvent(o, 'customEvent', null, null);
assertEquals('clicked again, no change', 1, o.clickedCount);
};
@@ -250,21 +249,42 @@ AdaptersTest.prototype.testDomElementEventRemoveAll = function() {
o.innerHTML = pInt(o.innerHTML) + 1;
};
+ // 1. Test custom events
// Setup event handler
- addEvent(o, 'my:Event', f);
+ addEvent(o, 'customEvent', f);
+ assertEquals('custom not yet clicked', 0, pInt(o.innerHTML));
+
+ // Fire it once
+ fireEvent(o, 'customEvent', null, null);
+ assertEquals('custom now clicked', 1, pInt(o.innerHTML));
+
+ // Remove all handlers
+ removeEvent(o);
+
+ // Fire it again, should do nothing, since the handler is removed
+ fireEvent(o, 'customEvent', null, null);
+ assertEquals('custom clicked again, no change', 1, pInt(o.innerHTML));
+
+
+ // 2. Test HTML events
+ // Reset the counter
+ o.innerHTML = 0;
+
+ // Setup event handler
+ addEvent(o, 'click', f);
assertEquals('not yet clicked', 0, pInt(o.innerHTML));
// Fire it once
- fireEvent(o, 'my:Event', null, null);
+ this.safeFireEvent(o, 'click');
assertEquals('now clicked', 1, pInt(o.innerHTML));
// Remove all handlers
removeEvent(o);
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'my:Event', null, null);
+ this.safeFireEvent(o, 'click');
assertEquals('clicked again, no change', 1, pInt(o.innerHTML));
-}
+};
/**
* Test event add/fire/remove on DOM element.
@@ -278,21 +298,41 @@ AdaptersTest.prototype.testDomElementEventRemoveType = function() {
o.innerHTML = pInt(o.innerHTML) + 1;
};
+ // 1. Test custom events
// Setup event handler
- addEvent(o, 'my:Event', f);
+ addEvent(o, 'customEvent', f);
+ assertEquals('custom not yet clicked', 0, pInt(o.innerHTML));
+
+ // Fire it once
+ fireEvent(o, 'customEvent', null, null);
+ assertEquals('custom now clicked', 1, pInt(o.innerHTML));
+
+ // Remove the handler (Only specifying event type)
+ removeEvent(o, 'customEvent');
+
+ // Fire it again, should do nothing, since the handler is removed
+ fireEvent(o, 'customEvent', null, null);
+ assertEquals('custom clicked again, no change', 1, pInt(o.innerHTML));
+
+ // 2. Test HTML events
+ // Reset the counter
+ o.innerHTML = 0;
+
+ // Setup event handler
+ addEvent(o, 'click', f);
assertEquals('not yet clicked', 0, pInt(o.innerHTML));
// Fire it once
- fireEvent(o, 'my:Event', null, null);
+ this.safeFireEvent(o, 'click');
assertEquals('now clicked', 1, pInt(o.innerHTML));
// Remove the handler (Only specifying event type)
- removeEvent(o, 'my:Event');
+ removeEvent(o, 'click');
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'my:Event', null, null);
+ this.safeFireEvent(o, 'click');
assertEquals('clicked again, no change', 1, pInt(o.innerHTML));
-}
+};
/**
* Test event add/fire/remove on DOM element.
@@ -306,19 +346,39 @@ AdaptersTest.prototype.testDomElementEventRemoveHandler = function() {
o.innerHTML = pInt(o.innerHTML) + 1;
};
+ // 1. Test custom events.
// Setup event handler
- addEvent(o, 'my:Event', f);
+ addEvent(o, 'customEvent', f);
+ assertEquals('custom not yet clicked', 0, pInt(o.innerHTML));
+
+ // Fire it once
+ fireEvent(o, 'customEvent', null, null);
+ assertEquals('custom clicked', 1, pInt(o.innerHTML));
+
+ // Remove the handler (Most fine-grained)
+ removeEvent(o, 'customEvent', f);
+
+ // Fire it again, should do nothing, since the handler is removed
+ fireEvent(o, 'customEvent', null, null);
+ assertEquals('custom clicked again, no change', 1, pInt(o.innerHTML));
+
+ // 2. Test HTML events
+ // Reset the counter
+ o.innerHTML = 0;
+
+ // Setup event handler
+ addEvent(o, 'click', f);
assertEquals('not yet clicked', 0, pInt(o.innerHTML));
// Fire it once
- fireEvent(o, 'my:Event', null, null);
+ this.safeFireEvent(o, 'click');
assertEquals('now clicked', 1, pInt(o.innerHTML));
// Remove the handler (Most fine-grained)
- removeEvent(o, 'my:Event', f);
+ removeEvent(o, 'click', f);
// Fire it again, should do nothing, since the handler is removed
- fireEvent(o, 'my:Event', null, null);
+ this.safeFireEvent(o, 'click');
assertEquals('clicked again, no change', 1, pInt(o.innerHTML));
};
@@ -334,4 +394,79 @@ AdaptersTest.prototype.testDocumentEvent = function() {
AdaptersTest.prototype.testWindowEvent = function() {
TODO: Implement
}
-*/
+*/
+
+/**
+ * A safe way of doing fireEvent. Prototype does not support fireing
+ * HTML events it seems.
+ */
+AdaptersTest.prototype.safeFireEvent = function(target, eventName) {
+ if (!this.isPrototypeAdapter()) {
+ fireEvent(target, eventName);
+ } else {
+ this.simulate(target, eventName);
+ }
+}
+
+/**
+ * Simple way to test if we are using the prototype adapter. The prototype
+ * library does not fire dom events properly so, use this test to shortcut those tests.
+ * Uses implementation details of the adapter.
+ */
+AdaptersTest.prototype.isPrototypeAdapter = function() {
+ return globalAdapter && globalAdapter._extend;
+};
+
+/**
+ * Simulates events of type HTMLEvent and MouseEvent.
+ */
+AdaptersTest.prototype.simulate = function(element, eventName) {
+ var eventMatchers = {
+ 'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
+ 'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out))$/
+ },
+ defaultOptions = {
+ pointerX: 0,
+ pointerY: 0,
+ button: 0,
+ ctrlKey: false,
+ altKey: false,
+ shiftKey: false,
+ metaKey: false,
+ bubbles: true,
+ cancelable: true
+ },
+ options = extend(defaultOptions, arguments[2] || {}),
+ oEvent, eventType = null;
+
+ for (var name in eventMatchers) {
+ if (eventMatchers[name].test(eventName)) { eventType = name; break; }
+ }
+
+ if (!eventType)
+ throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
+
+ if (document.createEvent) {
+ oEvent = document.createEvent(eventType);
+ if (eventType == 'HTMLEvents')
+ {
+ oEvent.initEvent(eventName, options.bubbles, options.cancelable);
+ }
+ else
+ {
+ oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView,
+ options.button, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
+ options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
+ }
+ element.dispatchEvent(oEvent);
+ } else {
+ options.clientX = options.pointerX;
+ options.clientY = options.pointerY;
+ var evt = document.createEventObject();
+ oEvent = extend(evt, options);
+ element.fireEvent('on' + eventName, oEvent);
+ }
+
+ return element;
+}
+

0 comments on commit 798880a

Please sign in to comment.