Bug 677626 - Add a profiling test suite for mochitest. r=jmaher

This commit is contained in:
Malini Das 2011-09-19 14:52:04 -04:00
Родитель e2679efebc
Коммит be9aa36b26
9 изменённых файлов: 196 добавлений и 163 удалений

Просмотреть файл

@ -1,5 +1,11 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests,
// we only need ChromeUtils.js for a few files which is why we are using loadSubScript.
var chromeUtils = {};
this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader);
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", chromeUtils);
function test() {
// Make sure the bookmarks bar is visible and restore its state on cleanup.
@ -31,11 +37,11 @@ function test() {
let simulateDragDrop = function(aEffect, aMimeType) {
const uriSpec = "http://www.mozilla.org/D1995729-A152-4e30-8329-469B01F30AA7";
let uri = makeURI(uriSpec);
EventUtils.synthesizeDrop(placesItems.childNodes[0],
chromeUtils.synthesizeDrop(placesItems.childNodes[0],
placesItems,
[[{type: aMimeType,
data: uriSpec}]],
aEffect, window);
aEffect, window, EventUtils);
// Verify that the drop produces exactly one bookmark.
let bookmarkIds = PlacesUtils.bookmarks

Просмотреть файл

@ -1,3 +1,10 @@
// Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests,
// we only need ChromeUtils.js for a few files which is why we are using loadSubScript.
var chromeUtils = {};
this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader);
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", chromeUtils);
function test() {
waitForExplicitFinish();
@ -137,7 +144,7 @@ function test() {
searchBar.addEventListener("popupshowing", stopPopup, true);
// drop on the search button so that we don't need to worry about the
// default handlers for textboxes.
EventUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/plain", data: "Some Text" } ]], "copy", window);
chromeUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/plain", data: "Some Text" } ]], "copy", window, EventUtils);
doOnloadOnce(function(event) {
is(searchBar.value, "Some Text", "drop text/plain on searchbar");
testDropInternalText();
@ -146,7 +153,7 @@ function test() {
function testDropInternalText() {
init();
EventUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/x-moz-text-internal", data: "More Text" } ]], "copy", window);
chromeUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/x-moz-text-internal", data: "More Text" } ]], "copy", window, EventUtils);
doOnloadOnce(function(event) {
is(searchBar.value, "More Text", "drop text/x-moz-text-internal on searchbar");
testDropLink();
@ -155,7 +162,7 @@ function test() {
function testDropLink() {
init();
EventUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/uri-list", data: "http://www.mozilla.org" } ]], "copy", window);
chromeUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/uri-list", data: "http://www.mozilla.org" } ]], "copy", window, EventUtils);
is(searchBar.value, "More Text", "drop text/uri-list on searchbar");
SimpleTest.executeSoon(testRightClick);
}

Просмотреть файл

@ -7,6 +7,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=574596
<title>Test for Bug 574596</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>

Просмотреть файл

@ -1,10 +1,12 @@
/**
* ChromeUtils.js is a set of mochitest utilities that are used to
* synthesize events in the browser. These are only used by
* mochitest-chrome tests. Originally these functions were in
* mochitest-chrome and browser-chrome tests. Originally these functions were in
* EventUtils.js, but when porting to specialPowers, we didn't want
* to move unnecessary functions.
*
* ChromeUtils.js depends on EventUtils.js being loaded.
*
*/
/**
@ -122,3 +124,155 @@ function synthesizeSelectionSet(aOffset, aLength, aReverse, aWindow)
}
return utils.sendSelectionSetEvent(aOffset, aLength, aReverse);
}
/**
* Emulate a dragstart event.
* element - element to fire the dragstart event on
* expectedDragData - the data you expect the data transfer to contain afterwards
* This data is in the format:
* [ [ {type: value, data: value, test: function}, ... ], ... ]
* can be null
* aWindow - optional; defaults to the current window object.
* x - optional; initial x coordinate
* y - optional; initial y coordinate
* 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
*
*/
function synthesizeDragStart(element, expectedDragData, aWindow, x, y)
{
if (!aWindow)
aWindow = window;
x = x || 2;
y = y || 2;
const step = 9;
var result = "trapDrag was not called";
var trapDrag = function(event) {
try {
var dataTransfer = event.dataTransfer;
result = null;
if (!dataTransfer)
throw "no dataTransfer";
if (expectedDragData == null ||
dataTransfer.mozItemCount != expectedDragData.length)
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;
}
}
} catch(ex) {
result = ex;
}
event.preventDefault();
event.stopPropagation();
}
aWindow.addEventListener("dragstart", trapDrag, false);
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);
aWindow.removeEventListener("dragstart", trapDrag, false);
synthesizeMouse(element, x, y, { type: "mouseup" }, aWindow);
return result;
}
/**
* 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.
*
* Returns the drop effect that was desired.
*/
function synthesizeDrop(srcElement, destElement, dragData, dropEffect, aWindow, eventUtils)
{
if (!aWindow)
aWindow = window;
if (typeof(eventUtils) != 'undefined') {
synthesizeMouseAtCenter = eventUtils.synthesizeMouseAtCenter;
synthesizeMouse = eventUtils.synthesizeMouse;
}
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();
}
ds.startDragSession();
try {
// need to use real mouse action
aWindow.addEventListener("dragstart", trapDrag, true);
synthesizeMouseAtCenter(srcElement, { 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 (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);
gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
}
synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aWindow);
return dataTransfer.dropEffect;
} finally {
ds.endDragSession(true);
}
}

Просмотреть файл

@ -521,153 +521,6 @@ function synthesizeKeyExpectEvent(key, aEvent, aExpectedTarget, aExpectedEvent,
_checkExpectedEvent(aExpectedTarget, aExpectedEvent, eventHandler, aTestName);
}
/**
* Emulate a dragstart event.
* element - element to fire the dragstart event on
* expectedDragData - the data you expect the data transfer to contain afterwards
* This data is in the format:
* [ [ {type: value, data: value, test: function}, ... ], ... ]
* can be null
* aWindow - optional; defaults to the current window object.
* x - optional; initial x coordinate
* y - optional; initial y coordinate
* 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
*
*/
function synthesizeDragStart(element, expectedDragData, aWindow, x, y)
{
if (!aWindow)
aWindow = window;
x = x || 2;
y = y || 2;
const step = 9;
var result = "trapDrag was not called";
var trapDrag = function(event) {
try {
var dataTransfer = event.dataTransfer;
result = null;
if (!dataTransfer)
throw "no dataTransfer";
if (expectedDragData == null ||
dataTransfer.mozItemCount != expectedDragData.length)
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;
}
}
} catch(ex) {
result = ex;
}
event.preventDefault();
event.stopPropagation();
}
aWindow.addEventListener("dragstart", trapDrag, false);
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);
aWindow.removeEventListener("dragstart", trapDrag, false);
synthesizeMouse(element, x, y, { type: "mouseup" }, aWindow);
return result;
}
/**
* 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.
*
* 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();
}
ds.startDragSession();
try {
// need to use real mouse action
aWindow.addEventListener("dragstart", trapDrag, true);
synthesizeMouseAtCenter(srcElement, { 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 (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);
gWindowUtils.dispatchDOMEventViaPresShell(destElement, event, true);
}
synthesizeMouseAtCenter(destElement, { type: "mouseup" }, aWindow);
return dataTransfer.dropEffect;
} finally {
ds.endDragSession(true);
}
}
function disableNonTestMouseEvents(aDisable)
{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

Просмотреть файл

@ -48,6 +48,8 @@
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"/>
<script type="application/javascript"><![CDATA[
const Ci = Components.interfaces;

Просмотреть файл

@ -9,6 +9,8 @@
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"/>
<script>
<![CDATA[

Просмотреть файл

@ -63,6 +63,8 @@ close window
src="utils.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"></script>
<script type="application/javascript">
<![CDATA[

Просмотреть файл

@ -8,7 +8,13 @@
// Tests are only simulations of the drag and drop events, we cannot really do
// this automatically.
// Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests,
// we only need ChromeUtils.js for a few files which is why we are using loadSubScript.
var gManagerWindow;
var chromeUtils = {};
this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader);
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", chromeUtils);
// This listens for the next opened window and checks it is of the right url.
// opencallback is called when the new window is fully loaded
@ -112,9 +118,9 @@ add_test(function() {
}, run_next_test);
var viewContainer = gManagerWindow.document.getElementById("view-port");
var effect = EventUtils.synthesizeDrop(viewContainer, viewContainer,
var effect = chromeUtils.synthesizeDrop(viewContainer, viewContainer,
[[{type: "text/x-moz-url", data: url}]],
"copy", gManagerWindow);
"copy", gManagerWindow, EventUtils);
is(effect, "copy", "Drag should be accepted");
});
@ -127,9 +133,9 @@ add_test(function() {
}, run_next_test);
var viewContainer = gManagerWindow.document.getElementById("view-port");
var effect = EventUtils.synthesizeDrop(viewContainer, viewContainer,
var effect = chromeUtils.synthesizeDrop(viewContainer, viewContainer,
[[{type: "application/x-moz-file", data: fileurl.file}]],
"copy", gManagerWindow);
"copy", gManagerWindow, EventUtils);
is(effect, "copy", "Drag should be accepted");
});
@ -143,10 +149,10 @@ add_test(function() {
}, run_next_test);
var viewContainer = gManagerWindow.document.getElementById("view-port");
var effect = EventUtils.synthesizeDrop(viewContainer, viewContainer,
var effect = chromeUtils.synthesizeDrop(viewContainer, viewContainer,
[[{type: "text/x-moz-url", data: url1}],
[{type: "text/x-moz-url", data: url2}]],
"copy", gManagerWindow);
"copy", gManagerWindow, EventUtils);
is(effect, "copy", "Drag should be accepted");
});
@ -160,10 +166,10 @@ add_test(function() {
}, run_next_test);
var viewContainer = gManagerWindow.document.getElementById("view-port");
var effect = EventUtils.synthesizeDrop(viewContainer, viewContainer,
var effect = chromeUtils.synthesizeDrop(viewContainer, viewContainer,
[[{type: "application/x-moz-file", data: fileurl1.file}],
[{type: "application/x-moz-file", data: fileurl2.file}]],
"copy", gManagerWindow);
"copy", gManagerWindow, EventUtils);
is(effect, "copy", "Drag should be accepted");
});
@ -177,9 +183,9 @@ add_test(function() {
}, run_next_test);
var viewContainer = gManagerWindow.document.getElementById("view-port");
var effect = EventUtils.synthesizeDrop(viewContainer, viewContainer,
var effect = chromeUtils.synthesizeDrop(viewContainer, viewContainer,
[[{type: "text/x-moz-url", data: url}],
[{type: "application/x-moz-file", data: fileurl.file}]],
"copy", gManagerWindow);
"copy", gManagerWindow, EventUtils);
is(effect, "copy", "Drag should be accepted");
});