event-simulate.html revision 1d4faea63148971515330703c6e02d3d13057408
<html>
<head>
<title>event-simulate tests</title>
<link type="text/css" rel="stylesheet" href="/build/logreader/assets/skins/sam/logreader.css" />
<script type="text/javascript" src="/build/yui/yui.js"></script>
</head>
<body class="yui-skin-sam">
<h1>Event-Simulate Tests</h1>
<div id="c"></div>
<script type="text/javascript">
YUI({
base: '/build/',
filter: "debug",
logInclude: { TestRunner: 1}
}).use('test', 'event-simulate', 'console', function (Y) {
Y.namespace("Tests");
Y.Tests.EventSimulate = (function(){
var Assert = Y.Assert,
ObjectAssert = Y.ObjectAssert;
//-------------------------------------------------------------------------
// Generic Event Test Case
//-------------------------------------------------------------------------
function GenericEventTestCase(type /*:String*/){
GenericEventTestCase.superclass.constructor.call(this);
this.eventType = type;
this.name = "Event '" + type + "' Tests";
this.result = null;
this.element = null;
this.elementTagName = "div";
}
Y.extend(GenericEventTestCase, Y.Test.Case, {
//---------------------------------------------------------------------
// Setup and teardown of test harnesses
//---------------------------------------------------------------------
/*
* Sets up several event handlers used to test UserAction mouse events.
*/
setUp : function() /*:Void*/{
//create the element
this.element = document.createElement(this.elementTagName);
document.body.appendChild(this.element);
//reset the result
this.result = null;
this.element["on" + this.eventType] = Y.bind(function(event){
this.handleEvent(event || window.event);
}, this);
//assign event handler
//this.handler = Y.on(this.eventType, Y.bind(this.handleEvent,this), this.element);
},
/*
* Removes event handlers that were used during the test.
*/
tearDown : function() /*:Void*/{
//remove the element
document.body.removeChild(this.element);
//remove event handler
this.element["on" + this.eventType] = null;
},
//---------------------------------------------------------------------
// Event handler
//---------------------------------------------------------------------
/*
* Uses to trap and assign the event object for interrogation.
* @param {Event} event The event object created from the event.
*/
handleEvent : function(event /*:Event*/) /*:Void*/ {
this.result = event;
}
});
//-------------------------------------------------------------------------
// UIEvent Test Case
//-------------------------------------------------------------------------
function UIEventTestCase(type){
UIEventTestCase.superclass.constructor.call(this, type);
this.elementTagName = "input";
}
Y.extend(UIEventTestCase, GenericEventTestCase, {
/*
* Tests with default options.
*/
testDefault : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType);
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isFalse(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
}
});
//-------------------------------------------------------------------------
// Focus/Blur Event Test Case
//-------------------------------------------------------------------------
function FocusBlurEventTestCase(type){
FocusBlurEventTestCase.superclass.constructor.call(this, type);
this.elementTagName = "input";
}
Y.extend(FocusBlurEventTestCase, GenericEventTestCase, {
/*
* Tests with default options.
*/
testDefault : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType);
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isFalse(this.result.bubbles, "bubbles is incorrect.");
Assert.isFalse(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
}
});
//-------------------------------------------------------------------------
// MouseButtonEvent Test Case
//-------------------------------------------------------------------------
function MouseButtonEventTestCase(type /*:String*/){
MouseButtonEventTestCase.superclass.constructor.call(this, type);
}
Y.extend(MouseButtonEventTestCase, GenericEventTestCase, {
//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------
/*
* Tests with default options.
*/
testDefault : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType);
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
},
/*
* Tests when using the right mouse button.
*/
testRightBtn : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { button: 2 });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(2, this.result.button, "Button is incorrect.");
},
/*
* Tests when using coordinates.
*/
testCoords : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { clientX: 100, clientY: 150, screenX: 200, screenY: 250 });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
Assert.areEqual(100, this.result.clientX, "ClientX is incorrect.");
Assert.areEqual(150, this.result.clientY, "ClientX is incorrect.");
Assert.areEqual(200, this.result.screenX, "ScreenX is incorrect.");
Assert.areEqual(250, this.result.screenY, "ScreenY is incorrect.");
},
/*
* Tests UserAction.click() when using CTRL key.
*/
testCtrlKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { ctrlKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
Assert.isTrue(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests when using ALT key.
*/
testAltKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { altKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isTrue(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests when using Shift key.
*/
testShiftKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { shiftKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isTrue(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests when using Meta key.
*/
testMetaKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { metaKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isTrue(this.result.metaKey, "MetaKey is incorrect.");
}
});
//-------------------------------------------------------------------------
// MouseMovementEvent Test Case
//-------------------------------------------------------------------------
function MouseMovementEventTestCase(type /*:String*/) {
MouseMovementEventTestCase.superclass.constructor.call(this,type);
}
Y.extend(MouseMovementEventTestCase, MouseButtonEventTestCase, {
/*
* Tests that the relatedTarget property is correct.
*/
testRelatedTarget : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { relatedTarget: document.body });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.areSame(window, this.result.view, "View is incorrect.");
Assert.areEqual(1, this.result.detail, "Details is incorrect.");
//Assert.areEqual(0, this.result.button, "Button is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
Assert.areSame(document.body, this.result.relatedTarget || this.result.fromElement || this.result.toElement, "RelatedTarget is incorrect.");
}
});
//-------------------------------------------------------------------------
// KeyEvent Test Case
//-------------------------------------------------------------------------
function KeyEventTestCase(type /*:String*/) {
KeyEventTestCase.superclass.constructor.call(this,type);
}
Y.extend(KeyEventTestCase, GenericEventTestCase, {
/*
* Tests that the default properties are correct.
*/
testDefault : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType);
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests UserAction.click() when using CTRL key.
*/
testCtrlKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { ctrlKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isTrue(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests when using ALT key.
*/
testAltKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { altKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isTrue(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests when using Shift key.
*/
testShiftKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { shiftKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isTrue(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
},
/*
* Tests when using Meta key.
*/
testMetaKey : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { metaKey: true });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isTrue(this.result.metaKey, "MetaKey is incorrect.");
}
});
//-------------------------------------------------------------------------
// KeyDirection Test Case
//-------------------------------------------------------------------------
function KeyDirectionEventTestCase(type /*:String*/){
KeyDirectionEventTestCase.superclass.constructor.call(this, type);
}
Y.extend(KeyDirectionEventTestCase, KeyEventTestCase, {
/*
* Tests that the default properties are correct.
*/
testKeyCode : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { keyCode: 97 });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
Assert.areEqual(97, this.result.keyCode, "KeyCode is incorrect.");
}
});
//-------------------------------------------------------------------------
// TextEvent Test Case
//-------------------------------------------------------------------------
function TextEventTestCase(type /*:String*/){
TextEventTestCase.superclass.constructor.call(this, type);
}
Y.extend(TextEventTestCase, KeyEventTestCase, {
/*
* Tests that the default properties are correct.
*/
testCharCode : function () /*:Void*/{
//fire the click event
Y.Event.simulate(this.element, this.eventType, { charCode: 97 });
//test the data coming back
Assert.isObject(this.result, "No event object created.");
Assert.areSame(this.element, this.result.target || this.result.srcElement, "Target is not correct.");
Assert.areEqual(this.eventType, this.result.type, "Event type is incorrect.");
Assert.isTrue(this.result.bubbles, "bubbles is incorrect.");
Assert.isTrue(this.result.cancelable, "Cancelable is incorrect.");
Assert.isFalse(this.result.ctrlKey, "CtrlKey is incorrect.");
Assert.isFalse(this.result.altKey, "AltKey is incorrect.");
Assert.isFalse(this.result.shiftKey, "ShiftKey is incorrect.");
Assert.isFalse(this.result.metaKey, "MetaKey is incorrect.");
Assert.areEqual(97, this.result.charCode || this.result.keyCode, "CharCode is incorrect.");
}
});
//-------------------------------------------------------------------------
// UserAction Tests
//-------------------------------------------------------------------------
//the user action suite
var suite /*:Y.Test.Suite*/
= new Y.Test.Suite("Event Simulate Tests");
var mouseEventsSuite /*:Y.Test.Suite*/
= new Y.Test.Suite("MouseEvent Tests");
suite.add(mouseEventsSuite);
var keyEventsSuite /*:Y.Test.Suite*/
= new Y.Test.Suite("KeyEvent Tests");
suite.add(keyEventsSuite);
var uiEventsSuite /*:Y.Test.Suite*/
= new Y.Test.Suite("UIEvents Tests");
suite.add(uiEventsSuite);
//-------------------------------------------------------------------------
// Mouse Tests
//-------------------------------------------------------------------------
mouseEventsSuite.add(new MouseButtonEventTestCase("click"));
mouseEventsSuite.add(new MouseButtonEventTestCase("dblclick"));
mouseEventsSuite.add(new MouseButtonEventTestCase("mousedown"));
mouseEventsSuite.add(new MouseButtonEventTestCase("mouseup"));
mouseEventsSuite.add(new MouseMovementEventTestCase("mouseover"));
mouseEventsSuite.add(new MouseMovementEventTestCase("mouseout"));
//-------------------------------------------------------------------------
// Key Tests
//-------------------------------------------------------------------------
keyEventsSuite.add(new KeyDirectionEventTestCase("keyup"));
keyEventsSuite.add(new KeyDirectionEventTestCase("keydown"));
keyEventsSuite.add(new TextEventTestCase("keypress"));
//-------------------------------------------------------------------------
// UI Tests
//-------------------------------------------------------------------------
uiEventsSuite.add(new UIEventTestCase("change"));
uiEventsSuite.add(new UIEventTestCase("select"));
uiEventsSuite.add(new FocusBlurEventTestCase("blur"));
uiEventsSuite.add(new FocusBlurEventTestCase("focus"));
//return it
return suite;
})();
var r = new Y.Console({
verbose : true,
//consoleLimit : 10,
newestOnTop : false
});
r.render('#c');
//add to the testrunner and run
Y.Test.Runner.add(Y.Tests.EventSimulate);
Y.Test.Runner.run();
/*if (parent && parent != window) {
YAHOO.tool.TestManager.load();
} else {
YAHOO.tool.TestRunner.run();
}*/
});
</script>
</body>
</html>