2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* EventUtils provides some utility methods for creating and sending DOM events.
|
|
|
|
* Current methods:
|
2007-03-26 06:19:33 -07:00
|
|
|
* sendMouseEvent
|
2007-03-22 10:30:00 -07:00
|
|
|
* sendChar
|
|
|
|
* sendString
|
|
|
|
* sendKey
|
|
|
|
*/
|
|
|
|
|
2007-03-26 06:19:33 -07:00
|
|
|
/**
|
|
|
|
* Send a mouse event to the node with id aTarget. The "event" passed in to
|
|
|
|
* aEvent is just a JavaScript object with the properties set that the real
|
|
|
|
* mouse event object should have. This includes the type of the mouse event.
|
|
|
|
* E.g. to send an click event to the node with id 'node' you might do this:
|
|
|
|
*
|
|
|
|
* sendMouseEvent({type:'click'}, 'node');
|
|
|
|
*/
|
2007-11-26 21:32:23 -08:00
|
|
|
function sendMouseEvent(aEvent, aTarget, aWindow) {
|
2007-03-26 06:19:33 -07:00
|
|
|
if (['click', 'mousedown', 'mouseup', 'mouseover', 'mouseout'].indexOf(aEvent.type) == -1) {
|
|
|
|
throw new Error("sendMouseEvent doesn't know about event type '"+aEvent.type+"'");
|
|
|
|
}
|
|
|
|
|
2007-11-26 21:32:23 -08:00
|
|
|
if (!aWindow) {
|
|
|
|
aWindow = window;
|
|
|
|
}
|
|
|
|
|
2007-03-26 06:19:33 -07:00
|
|
|
// For events to trigger the UA's default actions they need to be "trusted"
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');
|
|
|
|
|
2007-11-26 21:32:23 -08:00
|
|
|
var event = aWindow.document.createEvent('MouseEvent');
|
2007-03-26 06:19:33 -07:00
|
|
|
|
|
|
|
var typeArg = aEvent.type;
|
|
|
|
var canBubbleArg = true;
|
|
|
|
var cancelableArg = true;
|
2007-11-26 21:32:23 -08:00
|
|
|
var viewArg = aWindow;
|
2007-03-26 06:19:33 -07:00
|
|
|
var detailArg = aEvent.detail || (aEvent.type == 'click' ||
|
|
|
|
aEvent.type == 'mousedown' ||
|
|
|
|
aEvent.type == 'mouseup' ? 1 : 0);
|
|
|
|
var screenXArg = aEvent.screenX || 0;
|
|
|
|
var screenYArg = aEvent.screenY || 0;
|
|
|
|
var clientXArg = aEvent.clientX || 0;
|
|
|
|
var clientYArg = aEvent.clientY || 0;
|
|
|
|
var ctrlKeyArg = aEvent.ctrlKey || false;
|
|
|
|
var altKeyArg = aEvent.altKey || false;
|
|
|
|
var shiftKeyArg = aEvent.shiftKey || false;
|
|
|
|
var metaKeyArg = aEvent.metaKey || false;
|
|
|
|
var buttonArg = aEvent.button || 0;
|
|
|
|
var relatedTargetArg = aEvent.relatedTarget || null;
|
|
|
|
|
|
|
|
event.initMouseEvent(typeArg, canBubbleArg, cancelableArg, viewArg, detailArg,
|
|
|
|
screenXArg, screenYArg, clientXArg, clientYArg,
|
|
|
|
ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg,
|
|
|
|
buttonArg, relatedTargetArg);
|
|
|
|
|
2007-11-26 21:32:23 -08:00
|
|
|
aWindow.document.getElementById(aTarget).dispatchEvent(event);
|
2007-03-26 06:19:33 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Send the char aChar to the node with id aTarget. If aTarget is not
|
|
|
|
* provided, use "target". This method handles casing of chars (sends the
|
|
|
|
* right charcode, and sends a shift key for uppercase chars). No other
|
|
|
|
* modifiers are handled at this point.
|
|
|
|
*
|
|
|
|
* For now this method only works for English letters (lower and upper case)
|
|
|
|
* and the digits 0-9.
|
|
|
|
*
|
|
|
|
* Returns true if the keypress event was accepted (no calls to preventDefault
|
|
|
|
* or anything like that), false otherwise.
|
|
|
|
*/
|
|
|
|
function sendChar(aChar, aTarget) {
|
|
|
|
// DOM event charcodes match ASCII (JS charcodes) for a-zA-Z0-9.
|
|
|
|
var hasShift = (aChar == aChar.toUpperCase());
|
|
|
|
var charCode = aChar.charCodeAt(0);
|
|
|
|
var keyCode = charCode;
|
|
|
|
if (!hasShift) {
|
|
|
|
// For lowercase letters, the keyCode is actually 32 less than the charCode
|
|
|
|
keyCode -= 0x20;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __doEventDispatch(aTarget, charCode, keyCode, hasShift);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the string aStr to the node with id aTarget. If aTarget is not
|
|
|
|
* provided, use "target".
|
|
|
|
*
|
|
|
|
* For now this method only works for English letters (lower and upper case)
|
|
|
|
* and the digits 0-9.
|
|
|
|
*/
|
|
|
|
function sendString(aStr, aTarget) {
|
|
|
|
for (var i = 0; i < aStr.length; ++i) {
|
|
|
|
sendChar(aStr.charAt(i), aTarget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the non-character key aKey to the node with id aTarget. If aTarget is
|
|
|
|
* not provided, use "target". The name of the key should be a lowercase
|
|
|
|
* version of the part that comes after "DOM_VK_" in the KeyEvent constant
|
|
|
|
* name for this key. No modifiers are handled at this point.
|
|
|
|
*
|
|
|
|
* Returns true if the keypress event was accepted (no calls to preventDefault
|
|
|
|
* or anything like that), false otherwise.
|
|
|
|
*/
|
|
|
|
function sendKey(aKey, aTarget) {
|
|
|
|
keyName = "DOM_VK_" + aKey.toUpperCase();
|
|
|
|
|
|
|
|
if (!KeyEvent[keyName]) {
|
|
|
|
throw "Unknown key: " + keyName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __doEventDispatch(aTarget, 0, KeyEvent[keyName], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Actually perform event dispatch given a charCode, keyCode, and boolean for
|
|
|
|
* whether "shift" was pressed. Send the event to the node with id aTarget. If
|
|
|
|
* aTarget is not provided, use "target".
|
|
|
|
*
|
|
|
|
* Returns true if the keypress event was accepted (no calls to preventDefault
|
|
|
|
* or anything like that), false otherwise.
|
|
|
|
*/
|
|
|
|
function __doEventDispatch(aTarget, aCharCode, aKeyCode, aHasShift) {
|
|
|
|
if (aTarget === undefined) {
|
|
|
|
aTarget = "target";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make our events trusted
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
|
|
|
|
|
|
|
var event = document.createEvent("KeyEvents");
|
|
|
|
event.initKeyEvent("keydown", true, true, document.defaultView,
|
|
|
|
false, false, aHasShift, false,
|
|
|
|
aKeyCode, 0);
|
|
|
|
var accepted = $(aTarget).dispatchEvent(event);
|
|
|
|
|
2008-08-25 11:31:38 -07:00
|
|
|
// Preventing the default keydown action also prevents the default
|
|
|
|
// keypress action.
|
|
|
|
event = document.createEvent("KeyEvents");
|
|
|
|
if (aCharCode) {
|
|
|
|
event.initKeyEvent("keypress", true, true, document.defaultView,
|
|
|
|
false, false, aHasShift, false,
|
|
|
|
0, aCharCode);
|
|
|
|
} else {
|
|
|
|
event.initKeyEvent("keypress", true, true, document.defaultView,
|
|
|
|
false, false, aHasShift, false,
|
|
|
|
aKeyCode, 0);
|
|
|
|
}
|
|
|
|
if (!accepted) {
|
|
|
|
event.preventDefault();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-08-25 11:31:38 -07:00
|
|
|
accepted = $(aTarget).dispatchEvent(event);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Always send keyup
|
|
|
|
var event = document.createEvent("KeyEvents");
|
|
|
|
event.initKeyEvent("keyup", true, true, document.defaultView,
|
|
|
|
false, false, aHasShift, false,
|
|
|
|
aKeyCode, 0);
|
|
|
|
$(aTarget).dispatchEvent(event);
|
|
|
|
return accepted;
|
|
|
|
}
|
2007-05-11 09:15:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the key modifier flags from aEvent. Used to share code between
|
|
|
|
* synthesizeMouse and synthesizeKey.
|
|
|
|
*/
|
|
|
|
function _parseModifiers(aEvent)
|
|
|
|
{
|
2007-05-28 13:21:28 -07:00
|
|
|
const masks = Components.interfaces.nsIDOMNSEvent;
|
2007-05-11 09:15:26 -07:00
|
|
|
var mval = 0;
|
|
|
|
if (aEvent.shiftKey)
|
|
|
|
mval |= masks.SHIFT_MASK;
|
|
|
|
if (aEvent.ctrlKey)
|
|
|
|
mval |= masks.CONTROL_MASK;
|
|
|
|
if (aEvent.altKey)
|
|
|
|
mval |= masks.ALT_MASK;
|
|
|
|
if (aEvent.metaKey)
|
|
|
|
mval |= masks.META_MASK;
|
|
|
|
if (aEvent.accelKey)
|
|
|
|
mval |= (navigator.platform.indexOf("Mac") >= 0) ? masks.META_MASK :
|
|
|
|
masks.CONTROL_MASK;
|
|
|
|
|
|
|
|
return mval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a mouse event on a target. The actual client point is determined
|
|
|
|
* by taking the aTarget's client box and offseting it by aOffsetX and
|
|
|
|
* aOffsetY. This allows mouse clicks to be simulated by calling this method.
|
|
|
|
*
|
|
|
|
* aEvent is an object which may contain the properties:
|
2008-08-12 20:08:59 -07:00
|
|
|
* shiftKey, ctrlKey, altKey, metaKey, accessKey, clickCount, button, type
|
2007-05-11 09:15:26 -07:00
|
|
|
*
|
|
|
|
* If the type is specified, an mouse event of that type is fired. Otherwise,
|
|
|
|
* a mousedown followed by a mouse up is performed.
|
2008-01-28 09:12:17 -08:00
|
|
|
*
|
|
|
|
* aWindow is optional, and defaults to the current window object.
|
2007-05-11 09:15:26 -07:00
|
|
|
*/
|
2008-01-28 09:12:17 -08:00
|
|
|
function synthesizeMouse(aTarget, aOffsetX, aOffsetY, aEvent, aWindow)
|
2007-05-11 09:15:26 -07:00
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
2008-01-28 09:12:17 -08:00
|
|
|
if (!aWindow)
|
|
|
|
aWindow = window;
|
|
|
|
|
|
|
|
var utils = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
|
|
|
|
getInterface(Components.interfaces.nsIDOMWindowUtils);
|
2007-05-11 09:15:26 -07:00
|
|
|
if (utils) {
|
|
|
|
var button = aEvent.button || 0;
|
|
|
|
var clickCount = aEvent.clickCount || 1;
|
|
|
|
var modifiers = _parseModifiers(aEvent);
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
var rect = aTarget.getBoundingClientRect();
|
2008-11-26 13:52:42 -08:00
|
|
|
|
|
|
|
var left = rect.left + aOffsetX;
|
|
|
|
var top = rect.top + aOffsetY;
|
2007-05-11 09:15:26 -07:00
|
|
|
|
|
|
|
if (aEvent.type) {
|
2008-11-26 13:52:42 -08:00
|
|
|
utils.sendMouseEvent(aEvent.type, left, top, button, clickCount, modifiers);
|
2007-05-11 09:15:26 -07:00
|
|
|
}
|
|
|
|
else {
|
2008-11-26 13:52:42 -08:00
|
|
|
utils.sendMouseEvent("mousedown", left, top, button, clickCount, modifiers);
|
|
|
|
utils.sendMouseEvent("mouseup", left, top, button, clickCount, modifiers);
|
2007-05-11 09:15:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 20:08:59 -07:00
|
|
|
/**
|
|
|
|
* Synthesize a mouse scroll event on a target. The actual client point is determined
|
|
|
|
* by taking the aTarget's client box and offseting it by aOffsetX and
|
|
|
|
* aOffsetY.
|
|
|
|
*
|
|
|
|
* aEvent is an object which may contain the properties:
|
2008-09-17 04:27:19 -07:00
|
|
|
* shiftKey, ctrlKey, altKey, metaKey, accessKey, button, type, axis, delta, hasPixels
|
2008-08-12 20:08:59 -07:00
|
|
|
*
|
2008-09-17 04:27:19 -07:00
|
|
|
* If the type is specified, a mouse scroll event of that type is fired. Otherwise,
|
2008-08-12 20:08:59 -07:00
|
|
|
* "DOMMouseScroll" is used.
|
|
|
|
*
|
|
|
|
* If the axis is specified, it must be one of "horizontal" or "vertical". If not specified,
|
|
|
|
* "vertical" is used.
|
2010-02-07 07:44:48 -08:00
|
|
|
*
|
2008-08-12 20:08:59 -07:00
|
|
|
* 'delta' is the amount to scroll by (can be positive or negative). It must
|
2008-09-17 04:27:19 -07:00
|
|
|
* be specified.
|
|
|
|
*
|
|
|
|
* 'hasPixels' specifies whether kHasPixels should be set in the scrollFlags.
|
2008-08-12 20:08:59 -07:00
|
|
|
*
|
|
|
|
* aWindow is optional, and defaults to the current window object.
|
|
|
|
*/
|
|
|
|
function synthesizeMouseScroll(aTarget, aOffsetX, aOffsetY, aEvent, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
if (!aWindow)
|
|
|
|
aWindow = window;
|
|
|
|
|
|
|
|
var utils = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
|
|
|
|
getInterface(Components.interfaces.nsIDOMWindowUtils);
|
|
|
|
if (utils) {
|
|
|
|
// See nsMouseScrollFlags in nsGUIEvent.h
|
|
|
|
const kIsVertical = 0x02;
|
|
|
|
const kIsHorizontal = 0x04;
|
2008-09-17 04:27:19 -07:00
|
|
|
const kHasPixels = 0x08;
|
2008-08-12 20:08:59 -07:00
|
|
|
|
|
|
|
var button = aEvent.button || 0;
|
|
|
|
var modifiers = _parseModifiers(aEvent);
|
|
|
|
|
2009-02-12 02:44:38 -08:00
|
|
|
var rect = aTarget.getBoundingClientRect();
|
|
|
|
|
|
|
|
var left = rect.left;
|
|
|
|
var top = rect.top;
|
2008-08-12 20:08:59 -07:00
|
|
|
|
|
|
|
var type = aEvent.type || "DOMMouseScroll";
|
|
|
|
var axis = aEvent.axis || "vertical";
|
|
|
|
var scrollFlags = (axis == "horizontal") ? kIsHorizontal : kIsVertical;
|
2008-09-17 04:27:19 -07:00
|
|
|
if (aEvent.hasPixels) {
|
|
|
|
scrollFlags |= kHasPixels;
|
2008-08-12 20:08:59 -07:00
|
|
|
}
|
|
|
|
utils.sendMouseScrollEvent(type, left + aOffsetX, top + aOffsetY, button,
|
|
|
|
scrollFlags, aEvent.delta, modifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-11 09:15:26 -07:00
|
|
|
/**
|
|
|
|
* Synthesize a key event. It is targeted at whatever would be targeted by an
|
|
|
|
* actual keypress by the user, typically the focused element.
|
|
|
|
*
|
|
|
|
* aKey should be either a character or a keycode starting with VK_ such as
|
|
|
|
* VK_ENTER.
|
|
|
|
*
|
|
|
|
* aEvent is an object which may contain the properties:
|
|
|
|
* shiftKey, ctrlKey, altKey, metaKey, accessKey, type
|
|
|
|
*
|
|
|
|
* If the type is specified, a key event of that type is fired. Otherwise,
|
|
|
|
* a keydown, a keypress and then a keyup event are fired in sequence.
|
2008-01-28 09:12:17 -08:00
|
|
|
*
|
|
|
|
* aWindow is optional, and defaults to the current window object.
|
2007-05-11 09:15:26 -07:00
|
|
|
*/
|
2008-01-28 09:12:17 -08:00
|
|
|
function synthesizeKey(aKey, aEvent, aWindow)
|
2007-05-11 09:15:26 -07:00
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
2008-01-28 09:12:17 -08:00
|
|
|
if (!aWindow)
|
|
|
|
aWindow = window;
|
|
|
|
|
|
|
|
var utils = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
|
|
|
|
getInterface(Components.interfaces.nsIDOMWindowUtils);
|
2007-05-11 09:15:26 -07:00
|
|
|
if (utils) {
|
|
|
|
var keyCode = 0, charCode = 0;
|
|
|
|
if (aKey.indexOf("VK_") == 0)
|
|
|
|
keyCode = KeyEvent["DOM_" + aKey];
|
|
|
|
else
|
|
|
|
charCode = aKey.charCodeAt(0);
|
|
|
|
|
|
|
|
var modifiers = _parseModifiers(aEvent);
|
|
|
|
|
|
|
|
if (aEvent.type) {
|
|
|
|
utils.sendKeyEvent(aEvent.type, keyCode, charCode, modifiers);
|
|
|
|
}
|
|
|
|
else {
|
2008-08-25 11:31:38 -07:00
|
|
|
var keyDownDefaultHappened =
|
|
|
|
utils.sendKeyEvent("keydown", keyCode, charCode, modifiers);
|
|
|
|
utils.sendKeyEvent("keypress", keyCode, charCode, modifiers,
|
|
|
|
!keyDownDefaultHappened);
|
2007-05-11 09:15:26 -07:00
|
|
|
utils.sendKeyEvent("keyup", keyCode, charCode, modifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _gSeenEvent = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicate that an event with an original target of aExpectedTarget and
|
|
|
|
* a type of aExpectedEvent is expected to be fired, or not expected to
|
|
|
|
* be fired.
|
|
|
|
*/
|
|
|
|
function _expectEvent(aExpectedTarget, aExpectedEvent, aTestName)
|
|
|
|
{
|
|
|
|
if (!aExpectedTarget || !aExpectedEvent)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
_gSeenEvent = false;
|
|
|
|
|
|
|
|
var type = (aExpectedEvent.charAt(0) == "!") ?
|
|
|
|
aExpectedEvent.substring(1) : aExpectedEvent;
|
|
|
|
var eventHandler = function(event) {
|
|
|
|
var epassed = (!_gSeenEvent && event.originalTarget == aExpectedTarget &&
|
|
|
|
event.type == type);
|
|
|
|
is(epassed, true, aTestName + " " + type + " event target " + (_gSeenEvent ? "twice" : ""));
|
|
|
|
_gSeenEvent = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
aExpectedTarget.addEventListener(type, eventHandler, false);
|
|
|
|
return eventHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the event was fired or not. The event handler aEventHandler
|
|
|
|
* will be removed.
|
|
|
|
*/
|
|
|
|
function _checkExpectedEvent(aExpectedTarget, aExpectedEvent, aEventHandler, aTestName)
|
|
|
|
{
|
|
|
|
if (aEventHandler) {
|
|
|
|
var expectEvent = (aExpectedEvent.charAt(0) != "!");
|
|
|
|
var type = expectEvent ? aExpectedEvent : aExpectedEvent.substring(1);
|
|
|
|
aExpectedTarget.removeEventListener(type, aEventHandler, false);
|
|
|
|
var desc = type + " event";
|
|
|
|
if (!expectEvent)
|
|
|
|
desc += " not";
|
|
|
|
is(_gSeenEvent, expectEvent, aTestName + " " + desc + " fired");
|
|
|
|
}
|
|
|
|
|
|
|
|
_gSeenEvent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to synthesizeMouse except that a test is performed to see if an
|
|
|
|
* event is fired at the right target as a result.
|
|
|
|
*
|
|
|
|
* aExpectedTarget - the expected originalTarget of the event.
|
|
|
|
* aExpectedEvent - the expected type of the event, such as 'select'.
|
|
|
|
* aTestName - the test name when outputing results
|
|
|
|
*
|
|
|
|
* To test that an event is not fired, use an expected type preceded by an
|
|
|
|
* exclamation mark, such as '!select'. This might be used to test that a
|
|
|
|
* click on a disabled element doesn't fire certain events for instance.
|
2008-01-28 09:12:17 -08:00
|
|
|
*
|
|
|
|
* aWindow is optional, and defaults to the current window object.
|
2007-05-11 09:15:26 -07:00
|
|
|
*/
|
|
|
|
function synthesizeMouseExpectEvent(aTarget, aOffsetX, aOffsetY, aEvent,
|
2008-01-28 09:12:17 -08:00
|
|
|
aExpectedTarget, aExpectedEvent, aTestName,
|
|
|
|
aWindow)
|
2007-05-11 09:15:26 -07:00
|
|
|
{
|
|
|
|
var eventHandler = _expectEvent(aExpectedTarget, aExpectedEvent, aTestName);
|
2008-01-28 09:12:17 -08:00
|
|
|
synthesizeMouse(aTarget, aOffsetX, aOffsetY, aEvent, aWindow);
|
2007-05-11 09:15:26 -07:00
|
|
|
_checkExpectedEvent(aExpectedTarget, aExpectedEvent, eventHandler, aTestName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Similar to synthesizeKey except that a test is performed to see if an
|
|
|
|
* event is fired at the right target as a result.
|
|
|
|
*
|
|
|
|
* aExpectedTarget - the expected originalTarget of the event.
|
|
|
|
* aExpectedEvent - the expected type of the event, such as 'select'.
|
|
|
|
* aTestName - the test name when outputing results
|
|
|
|
*
|
|
|
|
* To test that an event is not fired, use an expected type preceded by an
|
|
|
|
* exclamation mark, such as '!select'.
|
2008-01-28 09:12:17 -08:00
|
|
|
*
|
|
|
|
* aWindow is optional, and defaults to the current window object.
|
2007-05-11 09:15:26 -07:00
|
|
|
*/
|
2008-01-28 09:12:17 -08:00
|
|
|
function synthesizeKeyExpectEvent(key, aEvent, aExpectedTarget, aExpectedEvent,
|
|
|
|
aTestName, aWindow)
|
2007-05-11 09:15:26 -07:00
|
|
|
{
|
|
|
|
var eventHandler = _expectEvent(aExpectedTarget, aExpectedEvent, aTestName);
|
2008-01-28 09:12:17 -08:00
|
|
|
synthesizeKey(key, aEvent, aWindow);
|
2007-05-11 09:15:26 -07:00
|
|
|
_checkExpectedEvent(aExpectedTarget, aExpectedEvent, eventHandler, aTestName);
|
|
|
|
}
|
2010-03-24 07:33:00 -07:00
|
|
|
|
2008-12-10 07:19:16 -08:00
|
|
|
/**
|
|
|
|
* Emulate a dragstart event.
|
|
|
|
* element - element to fire the dragstart event on
|
|
|
|
* expectedDragData - the data you expect the data transfer to contain afterwards
|
2010-02-07 07:44:48 -08:00
|
|
|
* This data is in the format:
|
|
|
|
* [ [ {type: value, data: value, test: function}, ... ], ... ]
|
|
|
|
* can be null
|
|
|
|
* aWindow - optional; defaults to the current window object.
|
2010-03-24 07:33:00 -07:00
|
|
|
* x - optional; initial x coordinate
|
|
|
|
* y - optional; initial y coordinate
|
2010-02-07 07:44:48 -08:00
|
|
|
* Returns null if data matches.
|
|
|
|
* Returns the event.dataTransfer if data does not match
|
|
|
|
*
|
|
|
|
* eqTest is an optional function if comparison can't be done with x == y;
|
|
|
|
* function (actualData, expectedData) {return boolean}
|
|
|
|
* @param actualData from dataTransfer
|
|
|
|
* @param expectedData from expectedDragData
|
|
|
|
* see bug 462172 for example of use
|
|
|
|
*
|
2008-12-10 07:19:16 -08:00
|
|
|
*/
|
2010-03-24 07:33:00 -07:00
|
|
|
function synthesizeDragStart(element, expectedDragData, aWindow, x, y)
|
2008-12-10 07:19:16 -08:00
|
|
|
{
|
2010-02-07 07:44:48 -08:00
|
|
|
if (!aWindow)
|
|
|
|
aWindow = window;
|
2010-03-24 07:33:00 -07:00
|
|
|
x = x || 2;
|
|
|
|
y = y || 2;
|
|
|
|
const step = 9;
|
2008-12-10 07:19:16 -08:00
|
|
|
|
2010-02-07 07:44:48 -08:00
|
|
|
var result = "trapDrag was not called";
|
2008-12-10 07:19:16 -08:00
|
|
|
var trapDrag = function(event) {
|
|
|
|
try {
|
|
|
|
var dataTransfer = event.dataTransfer;
|
2010-02-07 07:44:48 -08:00
|
|
|
result = null;
|
|
|
|
if (!dataTransfer)
|
2010-03-24 07:33:00 -07:00
|
|
|
throw "no dataTransfer";
|
2010-03-10 12:03:23 -08:00
|
|
|
if (expectedDragData == null ||
|
|
|
|
dataTransfer.mozItemCount != expectedDragData.length)
|
2010-02-07 07:44:48 -08:00
|
|
|
throw dataTransfer;
|
|
|
|
for (var i = 0; i < dataTransfer.mozItemCount; i++) {
|
|
|
|
var dtTypes = dataTransfer.mozTypesAt(i);
|
|
|
|
if (dtTypes.length != expectedDragData[i].length)
|
|
|
|
throw dataTransfer;
|
|
|
|
for (var j = 0; j < dtTypes.length; j++) {
|
|
|
|
if (dtTypes[j] != expectedDragData[i][j].type)
|
|
|
|
throw dataTransfer;
|
|
|
|
var dtData = dataTransfer.mozGetDataAt(dtTypes[j],i);
|
|
|
|
if (expectedDragData[i][j].eqTest) {
|
|
|
|
if (!expectedDragData[i][j].eqTest(dtData, expectedDragData[i][j].data))
|
|
|
|
throw dataTransfer;
|
|
|
|
}
|
|
|
|
else if (expectedDragData[i][j].data != dtData)
|
|
|
|
throw dataTransfer;
|
2008-12-10 07:19:16 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
2010-02-07 07:44:48 -08:00
|
|
|
result = ex;
|
2008-12-10 07:19:16 -08:00
|
|
|
}
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
2010-02-07 07:44:48 -08:00
|
|
|
aWindow.addEventListener("dragstart", trapDrag, false);
|
2010-03-24 07:33:00 -07:00
|
|
|
synthesizeMouse(element, x, y, { type: "mousedown" }, aWindow);
|
|
|
|
x += step; y += step;
|
|
|
|
synthesizeMouse(element, x, y, { type: "mousemove" }, aWindow);
|
|
|
|
x += step; y += step;
|
|
|
|
synthesizeMouse(element, x, y, { type: "mousemove" }, aWindow);
|
2010-02-07 07:44:48 -08:00
|
|
|
aWindow.removeEventListener("dragstart", trapDrag, false);
|
2010-03-24 07:33:00 -07:00
|
|
|
synthesizeMouse(element, x, y, { type: "mouseup" }, aWindow);
|
2010-02-07 07:44:48 -08:00
|
|
|
return result;
|
2008-12-10 07:19:16 -08:00
|
|
|
}
|
2010-03-24 07:33:00 -07:00
|
|
|
|
2008-12-10 07:19:16 -08:00
|
|
|
/**
|
2010-02-07 07:44:48 -08:00
|
|
|
* Emulate a drop by emulating a dragstart and firing events dragenter, dragover, and drop.
|
|
|
|
* element - the element to fire the dragover, dragleave and drop events
|
2008-12-10 07:19:16 -08:00
|
|
|
* dragData - the data to supply for the data transfer
|
|
|
|
* This data is in the format:
|
2010-02-07 07:44:48 -08:00
|
|
|
* [ [ {type: value, data: value}, ...], ... ]
|
2010-03-24 07:33:00 -07:00
|
|
|
* dropEffect - the drop effect to set during the dragstart event, or 'move' if null
|
|
|
|
* aWindow - optional; defaults to the current window object.
|
2008-12-10 07:19:16 -08:00
|
|
|
*
|
|
|
|
* Returns the drop effect that was desired.
|
|
|
|
*/
|
2010-02-18 13:37:28 -08:00
|
|
|
function synthesizeDrop(element, dragData, dropEffect, aWindow)
|
2008-12-10 07:19:16 -08:00
|
|
|
{
|
2010-02-07 07:44:48 -08:00
|
|
|
if (!aWindow)
|
|
|
|
aWindow = window;
|
|
|
|
|
2008-12-10 07:19:16 -08:00
|
|
|
var dataTransfer;
|
|
|
|
var trapDrag = function(event) {
|
|
|
|
dataTransfer = event.dataTransfer;
|
2010-02-07 07:44:48 -08:00
|
|
|
for (var i = 0; i < dragData.length; i++) {
|
|
|
|
var item = dragData[i];
|
|
|
|
for (var j = 0; j < item.length; j++) {
|
|
|
|
dataTransfer.mozSetDataAt(item[j].type, item[j].data, i);
|
2008-12-10 07:19:16 -08:00
|
|
|
}
|
|
|
|
}
|
2010-02-18 13:37:28 -08:00
|
|
|
dataTransfer.dropEffect = dropEffect || "move";
|
2008-12-10 07:19:16 -08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2010-02-07 07:44:48 -08:00
|
|
|
// need to use real mouse action
|
|
|
|
aWindow.addEventListener("dragstart", trapDrag, true);
|
|
|
|
synthesizeMouse(element, 2, 2, { type: "mousedown" }, aWindow);
|
|
|
|
synthesizeMouse(element, 11, 11, { type: "mousemove" }, aWindow);
|
|
|
|
synthesizeMouse(element, 20, 20, { type: "mousemove" }, aWindow);
|
|
|
|
aWindow.removeEventListener("dragstart", trapDrag, true);
|
2008-12-10 07:19:16 -08:00
|
|
|
|
2010-02-07 07:44:48 -08:00
|
|
|
event = aWindow.document.createEvent("DragEvents");
|
|
|
|
event.initDragEvent("dragenter", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
|
2008-12-10 07:19:16 -08:00
|
|
|
element.dispatchEvent(event);
|
|
|
|
|
2010-02-07 07:44:48 -08:00
|
|
|
var event = aWindow.document.createEvent("DragEvents");
|
|
|
|
event.initDragEvent("dragover", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
|
|
|
|
if (element.dispatchEvent(event)) {
|
|
|
|
synthesizeMouse(element, 20, 20, { type: "mouseup" }, aWindow);
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
|
2008-12-10 07:19:16 -08:00
|
|
|
if (dataTransfer.dropEffect != "none") {
|
2010-02-07 07:44:48 -08:00
|
|
|
event = aWindow.document.createEvent("DragEvents");
|
|
|
|
event.initDragEvent("drop", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
|
2008-12-10 07:19:16 -08:00
|
|
|
element.dispatchEvent(event);
|
|
|
|
}
|
2010-02-07 07:44:48 -08:00
|
|
|
synthesizeMouse(element, 20, 20, { type: "mouseup" }, aWindow);
|
2008-12-10 07:19:16 -08:00
|
|
|
|
|
|
|
return dataTransfer.dropEffect;
|
|
|
|
}
|
2009-02-12 02:44:38 -08:00
|
|
|
|
|
|
|
function disableNonTestMouseEvents(aDisable)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils =
|
|
|
|
window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
|
|
|
|
getInterface(Components.interfaces.nsIDOMWindowUtils);
|
|
|
|
if (utils)
|
|
|
|
utils.disableNonTestMouseEvents(aDisable);
|
|
|
|
}
|
2010-03-18 22:02:53 -07:00
|
|
|
|
|
|
|
function _getDOMWindowUtils(aWindow)
|
|
|
|
{
|
|
|
|
if (!aWindow) {
|
|
|
|
aWindow = window;
|
|
|
|
}
|
|
|
|
return aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
|
|
|
|
getInterface(Components.interfaces.nsIDOMWindowUtils);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a composition event.
|
|
|
|
*
|
|
|
|
* @param aIsCompositionStart If true, this synthesize compositionstart event.
|
|
|
|
* Otherwise, compositionend event.
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
*/
|
|
|
|
function synthesizeComposition(aIsCompositionStart, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.sendCompositionEvent(aIsCompositionStart ?
|
|
|
|
"compositionstart" : "compositionend");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a text event.
|
|
|
|
*
|
|
|
|
* @param aEvent The text event's information, this has |composition|
|
|
|
|
* and |caret| members. |composition| has |string| and
|
|
|
|
* |clauses| members. |clauses| must be array object. Each
|
|
|
|
* object has |length| and |attr|. And |caret| has |start| and
|
|
|
|
* |length|. See the following tree image.
|
|
|
|
*
|
|
|
|
* aEvent
|
|
|
|
* +-- composition
|
|
|
|
* | +-- string
|
|
|
|
* | +-- clauses[]
|
|
|
|
* | +-- length
|
|
|
|
* | +-- attr
|
|
|
|
* +-- caret
|
|
|
|
* +-- start
|
|
|
|
* +-- length
|
|
|
|
*
|
|
|
|
* Set the composition string to |composition.string|. Set its
|
|
|
|
* clauses information to the |clauses| array.
|
|
|
|
*
|
|
|
|
* When it's composing, set the each clauses' length to the
|
|
|
|
* |composition.clauses[n].length|. The sum of the all length
|
|
|
|
* values must be same as the length of |composition.string|.
|
|
|
|
* Set nsIDOMWindowUtils.COMPOSITION_ATTR_* to the
|
|
|
|
* |composition.clauses[n].attr|.
|
|
|
|
*
|
|
|
|
* When it's not composing, set 0 to the
|
|
|
|
* |composition.clauses[0].length| and
|
|
|
|
* |composition.clauses[0].attr|.
|
|
|
|
*
|
|
|
|
* Set caret position to the |caret.start|. It's offset from
|
|
|
|
* the start of the composition string. Set caret length to
|
|
|
|
* |caret.length|. If it's larger than 0, it should be wide
|
|
|
|
* caret. However, current nsEditor doesn't support wide
|
|
|
|
* caret, therefore, you should always set 0 now.
|
|
|
|
*
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
*/
|
|
|
|
function synthesizeText(aEvent, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aEvent.composition || !aEvent.composition.clauses ||
|
|
|
|
!aEvent.composition.clauses[0]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var firstClauseLength = aEvent.composition.clauses[0].length;
|
|
|
|
var firstClauseAttr = aEvent.composition.clauses[0].attr;
|
|
|
|
var secondClauseLength = 0;
|
|
|
|
var secondClauseAttr = 0;
|
|
|
|
var thirdClauseLength = 0;
|
|
|
|
var thirdClauseAttr = 0;
|
|
|
|
if (aEvent.composition.clauses[1]) {
|
|
|
|
secondClauseLength = aEvent.composition.clauses[1].length;
|
|
|
|
secondClauseAttr = aEvent.composition.clauses[1].attr;
|
|
|
|
if (aEvent.composition.clauses[2]) {
|
|
|
|
thirdClauseLength = aEvent.composition.clauses[2].length;
|
|
|
|
thirdClauseAttr = aEvent.composition.clauses[2].attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var caretStart = -1;
|
|
|
|
var caretLength = 0;
|
|
|
|
if (aEvent.caret) {
|
|
|
|
caretStart = aEvent.caret.start;
|
|
|
|
caretLength = aEvent.caret.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.sendTextEvent(aEvent.composition.string,
|
|
|
|
firstClauseLength, firstClauseAttr,
|
|
|
|
secondClauseLength, secondClauseAttr,
|
|
|
|
thirdClauseLength, thirdClauseAttr,
|
|
|
|
caretStart, caretLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a query selected text event.
|
|
|
|
*
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return An nsIQueryContentEventResult object. If this failed,
|
|
|
|
* the result might be null.
|
|
|
|
*/
|
|
|
|
function synthesizeQuerySelectedText(aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return utils.sendQueryContentEvent(utils.QUERY_SELECTED_TEXT, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a query text content event.
|
|
|
|
*
|
|
|
|
* @param aOffset The character offset. 0 means the first character in the
|
|
|
|
* selection root.
|
|
|
|
* @param aLength The length of getting text. If the length is too long,
|
|
|
|
* the extra length is ignored.
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return An nsIQueryContentEventResult object. If this failed,
|
|
|
|
* the result might be null.
|
|
|
|
*/
|
|
|
|
function synthesizeQueryTextContent(aOffset, aLength, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return utils.sendQueryContentEvent(utils.QUERY_TEXT_CONTENT,
|
|
|
|
aOffset, aLength, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a query caret rect event.
|
|
|
|
*
|
|
|
|
* @param aOffset The caret offset. 0 means left side of the first character
|
|
|
|
* in the selection root.
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return An nsIQueryContentEventResult object. If this failed,
|
|
|
|
* the result might be null.
|
|
|
|
*/
|
|
|
|
function synthesizeQueryCaretRect(aOffset, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return utils.sendQueryContentEvent(utils.QUERY_CARET_RECT,
|
|
|
|
aOffset, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a query text rect event.
|
|
|
|
*
|
|
|
|
* @param aOffset The character offset. 0 means the first character in the
|
|
|
|
* selection root.
|
|
|
|
* @param aLength The length of the text. If the length is too long,
|
|
|
|
* the extra length is ignored.
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return An nsIQueryContentEventResult object. If this failed,
|
|
|
|
* the result might be null.
|
|
|
|
*/
|
|
|
|
function synthesizeQueryTextRect(aOffset, aLength, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return utils.sendQueryContentEvent(utils.QUERY_TEXT_RECT,
|
|
|
|
aOffset, aLength, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a query editor rect event.
|
|
|
|
*
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return An nsIQueryContentEventResult object. If this failed,
|
|
|
|
* the result might be null.
|
|
|
|
*/
|
|
|
|
function synthesizeQueryEditorRect(aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return utils.sendQueryContentEvent(utils.QUERY_EDITOR_RECT, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a character at point event.
|
|
|
|
*
|
|
|
|
* @param aX, aY The offset in the client area of the DOM window.
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return An nsIQueryContentEventResult object. If this failed,
|
|
|
|
* the result might be null.
|
|
|
|
*/
|
|
|
|
function synthesizeCharAtPoint(aX, aY, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return utils.sendQueryContentEvent(utils.QUERY_CHARACTER_AT_POINT,
|
|
|
|
0, 0, aX, aY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synthesize a selection set event.
|
|
|
|
*
|
|
|
|
* @param aOffset The character offset. 0 means the first character in the
|
|
|
|
* selection root.
|
|
|
|
* @param aLength The length of the text. If the length is too long,
|
|
|
|
* the extra length is ignored.
|
|
|
|
* @param aReverse If true, the selection is from |aOffset + aLength| to
|
|
|
|
* |aOffset|. Otherwise, from |aOffset| to |aOffset + aLength|.
|
|
|
|
* @param aWindow Optional (If null, current |window| will be used)
|
|
|
|
* @return True, if succeeded. Otherwise false.
|
|
|
|
*/
|
|
|
|
function synthesizeSelectionSet(aOffset, aLength, aReverse, aWindow)
|
|
|
|
{
|
|
|
|
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
|
|
|
|
|
|
|
|
var utils = _getDOMWindowUtils(aWindow);
|
|
|
|
if (!utils) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return utils.sendSelectionSetEvent(aOffset, aLength, aReverse);
|
|
|
|
}
|