B2G/gecko/testing/mochitest/tests/SimpleTest/ChromeUtils.js
/**
 * Emulate a drop by emulating a dragstart and firing events dragenter, dragover, and drop.
 *  srcElement - the element to use to start the drag, usually the same as destElement
 *               but if destElement isn't suitable to start a drag on pass a suitable
 *               element for srcElement
 *  destElement - the element to fire the dragover, dragleave and drop events
 *  dragData - the data to supply for the data transfer
 *                     This data is in the format:
 *                       [ [ {type: value, data: value}, ...], ... ]
 *  dropEffect - the drop effect to set during the dragstart event, or 'move' if null
 *  aWindow - optional; defaults to the current window object.
 *  eventUtils - optional; allows you to pass in a reference to EventUtils.js. 
 *               If the eventUtils parameter is not passed in, we assume EventUtils.js is 
 *               in the scope. Used by browser-chrome tests.
 *  aDestWindow - optional; defaults to aWindow.
 *                Used when destElement is in a different window than srcElement.
 *
 * Returns the drop effect that was desired.
 */
Emulate a drop by emulating a dragstart and firing events dragenter, dragover, and drop.
srcElement - the element to use to start the drag, usually the same as destElement
but if destElement isn't suitable to start a drag on pass a suitable
element for srcElement
destElement - the element to fire the dragover, dragleave and drop events
dragData - the data to supply for the data transfer
This data is in the format:
[ [ {type: value, data: value}, ...], ... ]
dropEffect - the drop effect to set during the dragstart event, or 'move' if null
aWindow - optional; defaults to the current window object.
eventUtils - optional; allows you to pass in a reference to EventUtils.js.
If the eventUtils parameter is not passed in, we assume EventUtils.js is
in the scope. Used by browser-chrome tests.
aDestWindow - optional; defaults to aWindow.
Used when destElement is in a different window than srcElement.

Returns the drop effect that was desired.
Emulate a drop by emulating a dragstart and firing events dragenter dragover and drop . srcElement - the element to use to start the drag usually the same as destElement but if destElement isn ' t suitable to start a drag on pass a suitable element for srcElement destElement - the element to fire the dragover dragleave and drop events dragData - the data to supply for the data transfer This data is in the format : [ [ { type : value data : value } ... ] ... ] dropEffect - the drop effect to set during the dragstart event or ' move ' if null aWindow - optional ; defaults to the current window object . eventUtils - optional ; allows you to pass in a reference to EventUtils . js . If the eventUtils parameter is not passed in we assume EventUtils . js is in the scope . Used by browser - chrome tests . aDestWindow - optional ; defaults to aWindow . Used when destElement is in a different window than srcElement . Returns the drop effect that was desired .
function synthesizeDrop(srcElement, destElement, dragData, dropEffect, aWindow, eventUtils, aDestWindow)
{
  if (!aWindow)
    aWindow = window;
  if (!aDestWindow)
    aDestWindow = aWindow;
  var synthesizeMouseAtCenter = (eventUtils || window).synthesizeMouseAtCenter;
  var synthesizeMouse = (eventUtils || window).synthesizeMouse;
  var gWindowUtils = aDestWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
                                 getInterface(Components.interfaces.nsIDOMWindowUtils);
  var ds = Components.classes["@mozilla.org/widget/dragservice;1"].
           getService(Components.interfaces.nsIDragService);
  var dataTransfer;
  var trapDrag = function(event) {
    dataTransfer = event.dataTransfer;
    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);
      }
    }
    dataTransfer.dropEffect = dropEffect || "move";
    event.preventDefault();
    event.stopPropagation();
  }
  ds.startDragSession();
  try {
    aWindow.addEventListener("dragstart", trapDrag, true);
    synthesizeMouseAtCenter(srcElement, { type: "mousedown" }, aWindow);
    var rect = srcElement.getBoundingClientRect();
    var x = rect.width / 2;
    var y = rect.height / 2;
    synthesizeMouse(srcElement, x, y, { type: "mousemove" }, aWindow);
    synthesizeMouse(srcElement, x+10, y+10, { type: "mousemove" }, aWindow);
    aWindow.removeEventListener("dragstart", trapDrag, true);
    event = aDestWindow.document.createEvent("DragEvents");
    event.initDragEvent("dragenter", true, true, aDestWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
    gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
    var event = aDestWindow.document.createEvent("DragEvents");
    event.initDragEvent("dragover", true, true, aDestWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
    if (gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true)) {
      synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aDestWindow);
      return "none";
    }
    if (dataTransfer.dropEffect != "none") {
      event = aDestWindow.document.createEvent("DragEvents");
      event.initDragEvent("drop", true, true, aDestWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
      gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
    }
    synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aDestWindow);
    return dataTransfer.dropEffect;
  } finally {
    ds.endDragSession(true);
  }
}
function synthesizeDrop ( srcElement , destElement , dragData , dropEffect , aWindow , eventUtils , aDestWindow ) { if ( ! aWindow ) aWindow = window ; if ( ! aDestWindow ) aDestWindow = aWindow ; var synthesizeMouseAtCenter = ( eventUtils || window ) . synthesizeMouseAtCenter ; var synthesizeMouse = ( eventUtils || window ) . synthesizeMouse ; var gWindowUtils = aDestWindow . QueryInterface ( Components . interfaces . nsIInterfaceRequestor ) . getInterface ( Components . interfaces . nsIDOMWindowUtils ) ; var ds = Components . classes [ "@mozilla.org/widget/dragservice;1" ] . getService ( Components . interfaces . nsIDragService ) ; var dataTransfer ; var trapDrag = function ( event ) { dataTransfer = event . dataTransfer ; 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 ) ; } } dataTransfer . dropEffect = dropEffect || "move" ; event . preventDefault ( ) ; event . stopPropagation ( ) ; } ds . startDragSession ( ) ; try { aWindow . addEventListener ( "dragstart" , trapDrag , true ) ; synthesizeMouseAtCenter ( srcElement , { type : "mousedown" } , aWindow ) ; var rect = srcElement . getBoundingClientRect ( ) ; var x = rect . width / 2 ; var y = rect . height / 2 ; synthesizeMouse ( srcElement , x , y , { type : "mousemove" } , aWindow ) ; synthesizeMouse ( srcElement , x + 10 , y + 10 , { type : "mousemove" } , aWindow ) ; aWindow . removeEventListener ( "dragstart" , trapDrag , true ) ; event = aDestWindow . document . createEvent ( "DragEvents" ) ; event . initDragEvent ( "dragenter" , true , true , aDestWindow , 0 , 0 , 0 , 0 , 0 , false , false , false , false , 0 , null , dataTransfer ) ; gWindowUtils . dispatchDOMEventViaPresShell ( destElement , event , true ) ; var event = aDestWindow . document . createEvent ( "DragEvents" ) ; event . initDragEvent ( "dragover" , true , true , aDestWindow , 0 , 0 , 0 , 0 , 0 , false , false , false , false , 0 , null , dataTransfer ) ; if ( gWindowUtils . dispatchDOMEventViaPresShell ( destElement , event , true ) ) { synthesizeMouseAtCenter ( destElement , { type : "mouseup" } , aDestWindow ) ; return "none" ; } if ( dataTransfer . dropEffect != "none" ) { event = aDestWindow . document . createEvent ( "DragEvents" ) ; event . initDragEvent ( "drop" , true , true , aDestWindow , 0 , 0 , 0 , 0 , 0 , false , false , false , false , 0 , null , dataTransfer ) ; gWindowUtils . dispatchDOMEventViaPresShell ( destElement , event , true ) ; } synthesizeMouseAtCenter ( destElement , { type : "mouseup" } , aDestWindow ) ; return dataTransfer . dropEffect ; } finally { ds . endDragSession ( true ) ; } }
Emulate a drop by emulating a dragstart and firing events dragenter, dragover, and drop.
Emulate a drop by emulating a dragstart and firing events dragenter dragover and drop .
// need to use real mouse action