B2G/gecko/browser/components/places/tests/browser/browser_drag_bookmarks_on_toolbar.js
/**
 * Tests dragging on toolbar.
 *
 * We must test these 2 cases:
 *   - Dragging toward left, top, right should start a drag.
 *   - Dragging toward down should should open the container if the item is a
 *     container, drag the item otherwise.
 *
 * @param aElement
 *        DOM node element we will drag
 * @param aExpectedDragData
 *        Array of flavors and values in the form:
 *        [ ["text/plain: sometext", "text/html: <b>sometext</b>"], [...] ]
 *        Pass an empty array to check that drag even has been canceled.
 * @param aDirection
 *        Direction for the dragging gesture, see dragDirections helper object.
 */
Tests dragging on toolbar.
We must test these 2 cases:
- Dragging toward left, top, right should start a drag.
- Dragging toward down should should open the container if the item is a
container, drag the item otherwise.

@param aElement
DOM node element we will drag
@param aExpectedDragData
Array of flavors and values in the form:
[ ["text/plain: sometext", "text/html: sometext"], [...] ]
Pass an empty array to check that drag even has been canceled.
@param aDirection
Direction for the dragging gesture, see dragDirections helper object.
Tests dragging on toolbar . We must test these 2 cases : - Dragging toward left top right should start a drag . - Dragging toward down should should open the container if the item is a container drag the item otherwise . @param aElement DOM node element we will drag @param aExpectedDragData Array of flavors and values in the form : [ [ " text / plain : sometext " " text / html : sometext " ] [ ... ] ] Pass an empty array to check that drag even has been canceled . @param aDirection Direction for the dragging gesture see dragDirections helper object .
function synthesizeDragWithDirection(aElement, aExpectedDragData, aDirection, aCallback) {
  gBookmarksToolbar.addEventListener("dragstart", function(event)
  {
    info("A dragstart event has been trapped.");
    var dataTransfer = event.dataTransfer;
    is(dataTransfer.mozItemCount, aExpectedDragData.length,
       "Number of dragged items should be the same.");
    for (var t = 0; t < dataTransfer.mozItemCount; t++) {
      var types = dataTransfer.mozTypesAt(t);
      var expecteditem = aExpectedDragData[t];
      is(types.length, expecteditem.length,
        "Number of flavors for item " + t + " should be the same.");
      for (var f = 0; f < types.length; f++) {
        is(types[f], expecteditem[f].substring(0, types[f].length),
           "Flavor " + types[f] + " for item " + t + " should be the same.");
        is(dataTransfer.mozGetDataAt(types[f], t),
           expecteditem[f].substring(types[f].length + 2),
           "Contents for item " + t + " with flavor " + types[f] + " should be the same.");
      }
    }
    if (!aExpectedDragData.length)
      ok(event.defaultPrevented, "Drag has been canceled.");
    event.preventDefault();
    event.stopPropagation();
    gBookmarksToolbar.removeEventListener("dragstart", arguments.callee, false);
    aElement.addEventListener("click", prevent, false);
    EventUtils.synthesizeMouse(aElement,
                               startingPoint.x + xIncrement * 9,
                               startingPoint.y + yIncrement * 9,
                               { type: "mouseup" });
    aElement.removeEventListener("click", prevent, false);
    if (aElement.localName == "menu" && aElement.open)
      aElement.open = false;
    aCallback()
  }, false);
  var prevent = function(aEvent) {aEvent.preventDefault();}
  var xIncrement = 0;
  var yIncrement = 0;
  switch (aDirection) {
    case dragDirections.LEFT:
      xIncrement = -1;
      break;
    case dragDirections.RIGHT:
      xIncrement = +1;
      break;
    case dragDirections.UP:
      yIncrement = -1;
      break;
    case dragDirections.DOWN:
      yIncrement = +1;
      break;
  }
  var rect = aElement.getBoundingClientRect();
  var startingPoint = { x: (rect.right - rect.left)/2,
                        y: (rect.bottom - rect.top)/2 };
  EventUtils.synthesizeMouse(aElement,
                             startingPoint.x,
                             startingPoint.y,
                             { type: "mousedown" });
  EventUtils.synthesizeMouse(aElement,
                             startingPoint.x + xIncrement * 1,
                             startingPoint.y + yIncrement * 1,
                             { type: "mousemove" });
  EventUtils.synthesizeMouse(aElement,
                             startingPoint.x + xIncrement * 9,
                             startingPoint.y + yIncrement * 9,
                             { type: "mousemove" });
}
function synthesizeDragWithDirection ( aElement , aExpectedDragData , aDirection , aCallback ) { gBookmarksToolbar . addEventListener ( "dragstart" , function ( event ) { info ( "A dragstart event has been trapped." ) ; var dataTransfer = event . dataTransfer ; is ( dataTransfer . mozItemCount , aExpectedDragData . length , "Number of dragged items should be the same." ) ; for ( var t = 0 ; t < dataTransfer . mozItemCount ; t ++ ) { var types = dataTransfer . mozTypesAt ( t ) ; var expecteditem = aExpectedDragData [ t ] ; is ( types . length , expecteditem . length , "Number of flavors for item " + t + " should be the same." ) ; for ( var f = 0 ; f < types . length ; f ++ ) { is ( types [ f ] , expecteditem [ f ] . substring ( 0 , types [ f ] . length ) , "Flavor " + types [ f ] + " for item " + t + " should be the same." ) ; is ( dataTransfer . mozGetDataAt ( types [ f ] , t ) , expecteditem [ f ] . substring ( types [ f ] . length + 2 ) , "Contents for item " + t + " with flavor " + types [ f ] + " should be the same." ) ; } } if ( ! aExpectedDragData . length ) ok ( event . defaultPrevented , "Drag has been canceled." ) ; event . preventDefault ( ) ; event . stopPropagation ( ) ; gBookmarksToolbar . removeEventListener ( "dragstart" , arguments . callee , false ) ; aElement . addEventListener ( "click" , prevent , false ) ; EventUtils . synthesizeMouse ( aElement , startingPoint . x + xIncrement * 9 , startingPoint . y + yIncrement * 9 , { type : "mouseup" } ) ; aElement . removeEventListener ( "click" , prevent , false ) ; if ( aElement . localName == "menu" && aElement . open ) aElement . open = false ; aCallback ( ) } , false ) ; var prevent = function ( aEvent ) { aEvent . preventDefault ( ) ; } var xIncrement = 0 ; var yIncrement = 0 ; switch ( aDirection ) { case dragDirections . LEFT : xIncrement = - 1 ; break ; case dragDirections . RIGHT : xIncrement = + 1 ; break ; case dragDirections . UP : yIncrement = - 1 ; break ; case dragDirections . DOWN : yIncrement = + 1 ; break ; } var rect = aElement . getBoundingClientRect ( ) ; var startingPoint = { x : ( rect . right - rect . left ) / 2 , y : ( rect . bottom - rect . top ) / 2 } ; EventUtils . synthesizeMouse ( aElement , startingPoint . x , startingPoint . y , { type : "mousedown" } ) ; EventUtils . synthesizeMouse ( aElement , startingPoint . x + xIncrement * 1 , startingPoint . y + yIncrement * 1 , { type : "mousemove" } ) ; EventUtils . synthesizeMouse ( aElement , startingPoint . x + xIncrement * 9 , startingPoint . y + yIncrement * 9 , { type : "mousemove" } ) ; }
Tests dragging on toolbar.
Tests dragging on toolbar .
// Dragstart listener function. // This is likely to cause a click event, and, in case we are dragging a // bookmark, an unwanted page visit.  Prevent the click event. // Cleanup eventually opened menus.