testing/mochitest/tests/SimpleTest/ChromeUtils.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /**
michael@0 2 * ChromeUtils.js is a set of mochitest utilities that are used to
michael@0 3 * synthesize events in the browser. These are only used by
michael@0 4 * mochitest-chrome and browser-chrome tests. Originally these functions were in
michael@0 5 * EventUtils.js, but when porting to specialPowers, we didn't want
michael@0 6 * to move unnecessary functions.
michael@0 7 *
michael@0 8 */
michael@0 9
michael@0 10 const EventUtils = {};
michael@0 11 const scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].
michael@0 12 getService(Components.interfaces.mozIJSSubScriptLoader);
michael@0 13 scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", EventUtils);
michael@0 14
michael@0 15 /**
michael@0 16 * Synthesize a query text content event.
michael@0 17 *
michael@0 18 * @param aOffset The character offset. 0 means the first character in the
michael@0 19 * selection root.
michael@0 20 * @param aLength The length of getting text. If the length is too long,
michael@0 21 * the extra length is ignored.
michael@0 22 * @param aWindow Optional (If null, current |window| will be used)
michael@0 23 * @return An nsIQueryContentEventResult object. If this failed,
michael@0 24 * the result might be null.
michael@0 25 */
michael@0 26 function synthesizeQueryTextContent(aOffset, aLength, aWindow)
michael@0 27 {
michael@0 28 var utils = _getDOMWindowUtils(aWindow);
michael@0 29 if (!utils) {
michael@0 30 return nullptr;
michael@0 31 }
michael@0 32 return utils.sendQueryContentEvent(utils.QUERY_TEXT_CONTENT,
michael@0 33 aOffset, aLength, 0, 0,
michael@0 34 QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 35 }
michael@0 36
michael@0 37 /**
michael@0 38 * Synthesize a query text rect event.
michael@0 39 *
michael@0 40 * @param aOffset The character offset. 0 means the first character in the
michael@0 41 * selection root.
michael@0 42 * @param aLength The length of the text. If the length is too long,
michael@0 43 * the extra length is ignored.
michael@0 44 * @param aWindow Optional (If null, current |window| will be used)
michael@0 45 * @return An nsIQueryContentEventResult object. If this failed,
michael@0 46 * the result might be null.
michael@0 47 */
michael@0 48 function synthesizeQueryTextRect(aOffset, aLength, aWindow)
michael@0 49 {
michael@0 50 var utils = _getDOMWindowUtils(aWindow);
michael@0 51 if (!utils) {
michael@0 52 return nullptr;
michael@0 53 }
michael@0 54 return utils.sendQueryContentEvent(utils.QUERY_TEXT_RECT,
michael@0 55 aOffset, aLength, 0, 0,
michael@0 56 QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 57 }
michael@0 58
michael@0 59 /**
michael@0 60 * Synthesize a query editor rect event.
michael@0 61 *
michael@0 62 * @param aWindow Optional (If null, current |window| will be used)
michael@0 63 * @return An nsIQueryContentEventResult object. If this failed,
michael@0 64 * the result might be null.
michael@0 65 */
michael@0 66 function synthesizeQueryEditorRect(aWindow)
michael@0 67 {
michael@0 68 var utils = _getDOMWindowUtils(aWindow);
michael@0 69 if (!utils) {
michael@0 70 return nullptr;
michael@0 71 }
michael@0 72 return utils.sendQueryContentEvent(utils.QUERY_EDITOR_RECT, 0, 0, 0, 0,
michael@0 73 QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 74 }
michael@0 75
michael@0 76 /**
michael@0 77 * Synthesize a character at point event.
michael@0 78 *
michael@0 79 * @param aX, aY The offset in the client area of the DOM window.
michael@0 80 * @param aWindow Optional (If null, current |window| will be used)
michael@0 81 * @return An nsIQueryContentEventResult object. If this failed,
michael@0 82 * the result might be null.
michael@0 83 */
michael@0 84 function synthesizeCharAtPoint(aX, aY, aWindow)
michael@0 85 {
michael@0 86 var utils = _getDOMWindowUtils(aWindow);
michael@0 87 if (!utils) {
michael@0 88 return nullptr;
michael@0 89 }
michael@0 90 return utils.sendQueryContentEvent(utils.QUERY_CHARACTER_AT_POINT,
michael@0 91 0, 0, aX, aY,
michael@0 92 QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
michael@0 93 }
michael@0 94
michael@0 95 /**
michael@0 96 * Emulate a dragstart event.
michael@0 97 * element - element to fire the dragstart event on
michael@0 98 * expectedDragData - the data you expect the data transfer to contain afterwards
michael@0 99 * This data is in the format:
michael@0 100 * [ [ {type: value, data: value, test: function}, ... ], ... ]
michael@0 101 * can be null
michael@0 102 * aWindow - optional; defaults to the current window object.
michael@0 103 * x - optional; initial x coordinate
michael@0 104 * y - optional; initial y coordinate
michael@0 105 * Returns null if data matches.
michael@0 106 * Returns the event.dataTransfer if data does not match
michael@0 107 *
michael@0 108 * eqTest is an optional function if comparison can't be done with x == y;
michael@0 109 * function (actualData, expectedData) {return boolean}
michael@0 110 * @param actualData from dataTransfer
michael@0 111 * @param expectedData from expectedDragData
michael@0 112 * see bug 462172 for example of use
michael@0 113 *
michael@0 114 */
michael@0 115 function synthesizeDragStart(element, expectedDragData, aWindow, x, y)
michael@0 116 {
michael@0 117 if (!aWindow)
michael@0 118 aWindow = window;
michael@0 119 x = x || 2;
michael@0 120 y = y || 2;
michael@0 121 const step = 9;
michael@0 122
michael@0 123 var result = "trapDrag was not called";
michael@0 124 var trapDrag = function(event) {
michael@0 125 try {
michael@0 126 var dataTransfer = event.dataTransfer;
michael@0 127 result = null;
michael@0 128 if (!dataTransfer)
michael@0 129 throw "no dataTransfer";
michael@0 130 if (expectedDragData == null ||
michael@0 131 dataTransfer.mozItemCount != expectedDragData.length)
michael@0 132 throw dataTransfer;
michael@0 133 for (var i = 0; i < dataTransfer.mozItemCount; i++) {
michael@0 134 var dtTypes = dataTransfer.mozTypesAt(i);
michael@0 135 if (dtTypes.length != expectedDragData[i].length)
michael@0 136 throw dataTransfer;
michael@0 137 for (var j = 0; j < dtTypes.length; j++) {
michael@0 138 if (dtTypes[j] != expectedDragData[i][j].type)
michael@0 139 throw dataTransfer;
michael@0 140 var dtData = dataTransfer.mozGetDataAt(dtTypes[j],i);
michael@0 141 if (expectedDragData[i][j].eqTest) {
michael@0 142 if (!expectedDragData[i][j].eqTest(dtData, expectedDragData[i][j].data))
michael@0 143 throw dataTransfer;
michael@0 144 }
michael@0 145 else if (expectedDragData[i][j].data != dtData)
michael@0 146 throw dataTransfer;
michael@0 147 }
michael@0 148 }
michael@0 149 } catch(ex) {
michael@0 150 result = ex;
michael@0 151 }
michael@0 152 event.preventDefault();
michael@0 153 event.stopPropagation();
michael@0 154 }
michael@0 155 aWindow.addEventListener("dragstart", trapDrag, false);
michael@0 156 EventUtils.synthesizeMouse(element, x, y, { type: "mousedown" }, aWindow);
michael@0 157 x += step; y += step;
michael@0 158 EventUtils.synthesizeMouse(element, x, y, { type: "mousemove" }, aWindow);
michael@0 159 x += step; y += step;
michael@0 160 EventUtils.synthesizeMouse(element, x, y, { type: "mousemove" }, aWindow);
michael@0 161 aWindow.removeEventListener("dragstart", trapDrag, false);
michael@0 162 EventUtils.synthesizeMouse(element, x, y, { type: "mouseup" }, aWindow);
michael@0 163 return result;
michael@0 164 }
michael@0 165
michael@0 166 /**
michael@0 167 * Emulate a drop by emulating a dragstart and firing events dragenter, dragover, and drop.
michael@0 168 * srcElement - the element to use to start the drag, usually the same as destElement
michael@0 169 * but if destElement isn't suitable to start a drag on pass a suitable
michael@0 170 * element for srcElement
michael@0 171 * destElement - the element to fire the dragover, dragleave and drop events
michael@0 172 * dragData - the data to supply for the data transfer
michael@0 173 * This data is in the format:
michael@0 174 * [ [ {type: value, data: value}, ...], ... ]
michael@0 175 * dropEffect - the drop effect to set during the dragstart event, or 'move' if null
michael@0 176 * aWindow - optional; defaults to the current window object.
michael@0 177 * aDestWindow - optional; defaults to aWindow.
michael@0 178 * Used when destElement is in a different window than srcElement.
michael@0 179 *
michael@0 180 * Returns the drop effect that was desired.
michael@0 181 */
michael@0 182 function synthesizeDrop(srcElement, destElement, dragData, dropEffect, aWindow, aDestWindow)
michael@0 183 {
michael@0 184 if (!aWindow)
michael@0 185 aWindow = window;
michael@0 186 if (!aDestWindow)
michael@0 187 aDestWindow = aWindow;
michael@0 188
michael@0 189 var gWindowUtils = aDestWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
michael@0 190 getInterface(Components.interfaces.nsIDOMWindowUtils);
michael@0 191 var ds = Components.classes["@mozilla.org/widget/dragservice;1"].
michael@0 192 getService(Components.interfaces.nsIDragService);
michael@0 193
michael@0 194 var dataTransfer;
michael@0 195 var trapDrag = function(event) {
michael@0 196 dataTransfer = event.dataTransfer;
michael@0 197 for (var i = 0; i < dragData.length; i++) {
michael@0 198 var item = dragData[i];
michael@0 199 for (var j = 0; j < item.length; j++) {
michael@0 200 dataTransfer.mozSetDataAt(item[j].type, item[j].data, i);
michael@0 201 }
michael@0 202 }
michael@0 203 dataTransfer.dropEffect = dropEffect || "move";
michael@0 204 event.preventDefault();
michael@0 205 event.stopPropagation();
michael@0 206 }
michael@0 207
michael@0 208 ds.startDragSession();
michael@0 209
michael@0 210 try {
michael@0 211 // need to use real mouse action
michael@0 212 aWindow.addEventListener("dragstart", trapDrag, true);
michael@0 213 EventUtils.synthesizeMouseAtCenter(srcElement, { type: "mousedown" }, aWindow);
michael@0 214
michael@0 215 var rect = srcElement.getBoundingClientRect();
michael@0 216 var x = rect.width / 2;
michael@0 217 var y = rect.height / 2;
michael@0 218 EventUtils.synthesizeMouse(srcElement, x, y, { type: "mousemove" }, aWindow);
michael@0 219 EventUtils.synthesizeMouse(srcElement, x+10, y+10, { type: "mousemove" }, aWindow);
michael@0 220 aWindow.removeEventListener("dragstart", trapDrag, true);
michael@0 221
michael@0 222 event = aDestWindow.document.createEvent("DragEvents");
michael@0 223 event.initDragEvent("dragenter", true, true, aDestWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
michael@0 224 gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
michael@0 225 var event = aDestWindow.document.createEvent("DragEvents");
michael@0 226 event.initDragEvent("dragover", true, true, aDestWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
michael@0 227 if (gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true)) {
michael@0 228 EventUtils.synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aDestWindow);
michael@0 229 return "none";
michael@0 230 }
michael@0 231
michael@0 232 if (dataTransfer.dropEffect != "none") {
michael@0 233 event = aDestWindow.document.createEvent("DragEvents");
michael@0 234 event.initDragEvent("drop", true, true, aDestWindow, 0, 0, 0, 0, 0, false, false, false, false, 0, null, dataTransfer);
michael@0 235 gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
michael@0 236 }
michael@0 237
michael@0 238 EventUtils.synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aDestWindow);
michael@0 239
michael@0 240 return dataTransfer.dropEffect;
michael@0 241 } finally {
michael@0 242 ds.endDragSession(true);
michael@0 243 }
michael@0 244 };
michael@0 245
michael@0 246 var PluginUtils =
michael@0 247 {
michael@0 248 withTestPlugin : function(callback)
michael@0 249 {
michael@0 250 if (typeof Components == "undefined")
michael@0 251 {
michael@0 252 todo(false, "Not a Mozilla-based browser");
michael@0 253 return false;
michael@0 254 }
michael@0 255
michael@0 256 var ph = Components.classes["@mozilla.org/plugin/host;1"]
michael@0 257 .getService(Components.interfaces.nsIPluginHost);
michael@0 258 var tags = ph.getPluginTags();
michael@0 259
michael@0 260 // Find the test plugin
michael@0 261 for (var i = 0; i < tags.length; i++)
michael@0 262 {
michael@0 263 if (tags[i].name == "Test Plug-in")
michael@0 264 {
michael@0 265 callback(tags[i]);
michael@0 266 return true;
michael@0 267 }
michael@0 268 }
michael@0 269 todo(false, "Need a test plugin on this platform");
michael@0 270 return false;
michael@0 271 }
michael@0 272 };

mercurial