michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var Services = SpecialPowers.Services; michael@0: michael@0: /* michael@0: * $_ michael@0: * michael@0: * Returns the element with the specified |name| attribute. michael@0: */ michael@0: function $_(formNum, name) { michael@0: var form = document.getElementById("form" + formNum); michael@0: if (!form) { michael@0: ok(false, "$_ couldn't find requested form " + formNum); michael@0: return null; michael@0: } michael@0: michael@0: var element = form.elements.namedItem(name); michael@0: if (!element) { michael@0: ok(false, "$_ couldn't find requested element " + name); michael@0: return null; michael@0: } michael@0: michael@0: // Note that namedItem is a bit stupid, and will prefer an michael@0: // |id| attribute over a |name| attribute when looking for michael@0: // the element. michael@0: michael@0: if (element.hasAttribute("name") && element.getAttribute("name") != name) { michael@0: ok(false, "$_ got confused."); michael@0: return null; michael@0: } michael@0: michael@0: return element; michael@0: } michael@0: michael@0: // Mochitest gives us a sendKey(), but it's targeted to a specific element. michael@0: // This basically sends an untargeted key event, to whatever's focused. michael@0: function doKey(aKey, modifier) { michael@0: var keyName = "DOM_VK_" + aKey.toUpperCase(); michael@0: var key = SpecialPowers.Ci.nsIDOMKeyEvent[keyName]; michael@0: michael@0: // undefined --> null michael@0: if (!modifier) michael@0: modifier = null; michael@0: michael@0: // Window utils for sending fake sey events. michael@0: var wutils = SpecialPowers.getDOMWindowUtils(window); michael@0: michael@0: if (wutils.sendKeyEvent("keydown", key, 0, modifier)) { michael@0: wutils.sendKeyEvent("keypress", key, 0, modifier); michael@0: } michael@0: wutils.sendKeyEvent("keyup", key, 0, modifier); michael@0: } michael@0: michael@0: michael@0: function getAutocompletePopup() { michael@0: var Ci = SpecialPowers.Ci; michael@0: chromeWin = SpecialPowers.wrap(window) michael@0: .QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIWebNavigation) michael@0: .QueryInterface(Ci.nsIDocShellTreeItem) michael@0: .rootTreeItem michael@0: .QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindow) michael@0: .QueryInterface(Ci.nsIDOMChromeWindow); michael@0: autocompleteMenu = chromeWin.document.getElementById("PopupAutoComplete"); michael@0: ok(autocompleteMenu, "Got autocomplete popup"); michael@0: michael@0: return autocompleteMenu; michael@0: } michael@0: michael@0: michael@0: function cleanUpFormHist() { michael@0: SpecialPowers.formHistory.update({ op : "remove" }); michael@0: } michael@0: cleanUpFormHist(); michael@0: michael@0: michael@0: var checkObserver = { michael@0: verifyStack: [], michael@0: callback: null, michael@0: michael@0: waitForChecks: function(callback) { michael@0: if (this.verifyStack.length == 0) michael@0: callback(); michael@0: else michael@0: this.callback = callback; michael@0: }, michael@0: michael@0: observe: function(subject, topic, data) { michael@0: if (data != "formhistory-add" && data != "formhistory-update") michael@0: return; michael@0: ok(this.verifyStack.length > 0, "checking if saved form data was expected"); michael@0: michael@0: // Make sure that every piece of data we expect to be saved is saved, and no michael@0: // more. Here it is assumed that for every entry satchel saves or modifies, a michael@0: // message is sent. michael@0: // michael@0: // We don't actually check the content of the message, but just that the right michael@0: // quantity of messages is received. michael@0: // - if there are too few messages, test will time out michael@0: // - if there are too many messages, test will error out here michael@0: // michael@0: var expected = this.verifyStack.shift(); michael@0: michael@0: countEntries(expected.name, expected.value, michael@0: function(num) { michael@0: ok(num > 0, expected.message); michael@0: if (checkObserver.verifyStack.length == 0) { michael@0: var callback = checkObserver.callback; michael@0: checkObserver.callback = null; michael@0: callback(); michael@0: } michael@0: }); michael@0: } michael@0: }; michael@0: michael@0: function checkForSave(name, value, message) { michael@0: checkObserver.verifyStack.push({ name : name, value: value, message: message }); michael@0: } michael@0: michael@0: michael@0: function getFormSubmitButton(formNum) { michael@0: var form = $("form" + formNum); // by id, not name michael@0: ok(form != null, "getting form " + formNum); michael@0: michael@0: // we can't just call form.submit(), because that doesn't seem to michael@0: // invoke the form onsubmit handler. michael@0: var button = form.firstChild; michael@0: while (button && button.type != "submit") { button = button.nextSibling; } michael@0: ok(button != null, "getting form submit button"); michael@0: michael@0: return button; michael@0: } michael@0: michael@0: // Count the number of entries with the given name and value, and call then(number) michael@0: // when done. If name or value is null, then the value of that field does not matter. michael@0: function countEntries(name, value, then) { michael@0: var obj = {}; michael@0: if (name !== null) michael@0: obj.fieldname = name; michael@0: if (value !== null) michael@0: obj.value = value; michael@0: michael@0: var count = 0; michael@0: SpecialPowers.formHistory.count(obj, { handleResult: function (result) { count = result }, michael@0: handleError: function (error) { michael@0: do_throw("Error occurred searching form history: " + error); michael@0: }, michael@0: handleCompletion: function (reason) { if (!reason) then(count); } michael@0: }); michael@0: } michael@0: michael@0: // Wrapper around FormHistory.update which handles errors. Calls then() when done. michael@0: function updateFormHistory(changes, then) { michael@0: SpecialPowers.formHistory.update(changes, { handleError: function (error) { michael@0: do_throw("Error occurred updating form history: " + error); michael@0: }, michael@0: handleCompletion: function (reason) { if (!reason) then(); }, michael@0: }); michael@0: }