Bug 622264 - Make synthesizeDrop works in more situations. r=enndeakin a=tests
authorMounir Lamouri <mounir.lamouri@gmail.com>
Tue, 25 Jan 2011 11:30:24 +0100
changeset 61261 fa9b50ad86a60ae7fc4e2ccfd18a27f9d20010bf
parent 61260 c93381b53df3c59c3be379d247d5663bc796db99
child 61262 1fc29746701e7011bd4edf9dc69b5ee5a930332a
push id18281
push usermlamouri@mozilla.com
push dateTue, 25 Jan 2011 10:35:30 +0000
treeherdermozilla-central@fa9b50ad86a6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersenndeakin, tests
bugs622264
milestone2.0b10pre
first release with
nightly win64
fa9b50ad86a6 / 4.0b10pre / 20110125030207 / files
nightly linux32
nightly linux64
nightly mac
nightly win32
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly win64
Bug 622264 - Make synthesizeDrop works in more situations. r=enndeakin a=tests
testing/mochitest/tests/SimpleTest/EventUtils.js
--- a/testing/mochitest/tests/SimpleTest/EventUtils.js
+++ b/testing/mochitest/tests/SimpleTest/EventUtils.js
@@ -599,56 +599,71 @@ function synthesizeDragStart(element, ex
  *
  * Returns the drop effect that was desired.
  */
 function synthesizeDrop(srcElement, destElement, dragData, dropEffect, aWindow)
 {
   if (!aWindow)
     aWindow = window;
 
+  // For events to trigger the UA's default actions they need to be "trusted".
+  netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+
+  var gWindowUtils  = window.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();
   }
 
-  // need to use real mouse action
-  aWindow.addEventListener("dragstart", trapDrag, true);
-  synthesizeMouse(srcElement, 2, 2, { type: "mousedown" }, aWindow);
-  synthesizeMouse(srcElement, 11, 11, { type: "mousemove" }, aWindow);
-  synthesizeMouse(srcElement, 20, 20, { type: "mousemove" }, aWindow);
-  aWindow.removeEventListener("dragstart", trapDrag, true);
+  ds.startDragSession();
 
-  event = aWindow.document.createEvent("DragEvents");
-  event.initDragEvent("dragenter", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
-  destElement.dispatchEvent(event);
+  try {
+    // need to use real mouse action
+    aWindow.addEventListener("dragstart", trapDrag, true);
+    synthesizeMouse(srcElement, 2, 2, { type: "mousedown" }, aWindow);
+    synthesizeMouse(srcElement, 11, 11, { type: "mousemove" }, aWindow);
+    synthesizeMouse(srcElement, 20, 20, { type: "mousemove" }, aWindow);
+    aWindow.removeEventListener("dragstart", trapDrag, true);
+
+    event = aWindow.document.createEvent("DragEvents");
+    event.initDragEvent("dragenter", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
+    gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
 
-  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 (destElement.dispatchEvent(event)) {
-    synthesizeMouse(destElement, 20, 20, { type: "mouseup" }, aWindow);
-    return "none";
-  }
+    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 (gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true)) {
+      synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aWindow);
+      return "none";
+    }
 
-  if (dataTransfer.dropEffect != "none") {
-    event = aWindow.document.createEvent("DragEvents");
-    event.initDragEvent("drop", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
-    destElement.dispatchEvent(event);
+    if (dataTransfer.dropEffect != "none") {
+      event = aWindow.document.createEvent("DragEvents");
+      event.initDragEvent("drop", true, true, aWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
+      gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
+    }
+
+    synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aWindow);
+
+    return dataTransfer.dropEffect;
+  } finally {
+    ds.endDragSession(true);
   }
-  synthesizeMouse(destElement, 20, 20, { type: "mouseup" }, aWindow);
-
-  return dataTransfer.dropEffect;
 }
 
 function disableNonTestMouseEvents(aDisable)
 {
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
 
   var utils =
     window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).