1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/satchel/test/satchel_common.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,162 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +var Services = SpecialPowers.Services; 1.9 + 1.10 +/* 1.11 + * $_ 1.12 + * 1.13 + * Returns the element with the specified |name| attribute. 1.14 + */ 1.15 +function $_(formNum, name) { 1.16 + var form = document.getElementById("form" + formNum); 1.17 + if (!form) { 1.18 + ok(false, "$_ couldn't find requested form " + formNum); 1.19 + return null; 1.20 + } 1.21 + 1.22 + var element = form.elements.namedItem(name); 1.23 + if (!element) { 1.24 + ok(false, "$_ couldn't find requested element " + name); 1.25 + return null; 1.26 + } 1.27 + 1.28 + // Note that namedItem is a bit stupid, and will prefer an 1.29 + // |id| attribute over a |name| attribute when looking for 1.30 + // the element. 1.31 + 1.32 + if (element.hasAttribute("name") && element.getAttribute("name") != name) { 1.33 + ok(false, "$_ got confused."); 1.34 + return null; 1.35 + } 1.36 + 1.37 + return element; 1.38 +} 1.39 + 1.40 +// Mochitest gives us a sendKey(), but it's targeted to a specific element. 1.41 +// This basically sends an untargeted key event, to whatever's focused. 1.42 +function doKey(aKey, modifier) { 1.43 + var keyName = "DOM_VK_" + aKey.toUpperCase(); 1.44 + var key = SpecialPowers.Ci.nsIDOMKeyEvent[keyName]; 1.45 + 1.46 + // undefined --> null 1.47 + if (!modifier) 1.48 + modifier = null; 1.49 + 1.50 + // Window utils for sending fake sey events. 1.51 + var wutils = SpecialPowers.getDOMWindowUtils(window); 1.52 + 1.53 + if (wutils.sendKeyEvent("keydown", key, 0, modifier)) { 1.54 + wutils.sendKeyEvent("keypress", key, 0, modifier); 1.55 + } 1.56 + wutils.sendKeyEvent("keyup", key, 0, modifier); 1.57 +} 1.58 + 1.59 + 1.60 +function getAutocompletePopup() { 1.61 + var Ci = SpecialPowers.Ci; 1.62 + chromeWin = SpecialPowers.wrap(window) 1.63 + .QueryInterface(Ci.nsIInterfaceRequestor) 1.64 + .getInterface(Ci.nsIWebNavigation) 1.65 + .QueryInterface(Ci.nsIDocShellTreeItem) 1.66 + .rootTreeItem 1.67 + .QueryInterface(Ci.nsIInterfaceRequestor) 1.68 + .getInterface(Ci.nsIDOMWindow) 1.69 + .QueryInterface(Ci.nsIDOMChromeWindow); 1.70 + autocompleteMenu = chromeWin.document.getElementById("PopupAutoComplete"); 1.71 + ok(autocompleteMenu, "Got autocomplete popup"); 1.72 + 1.73 + return autocompleteMenu; 1.74 +} 1.75 + 1.76 + 1.77 +function cleanUpFormHist() { 1.78 + SpecialPowers.formHistory.update({ op : "remove" }); 1.79 +} 1.80 +cleanUpFormHist(); 1.81 + 1.82 + 1.83 +var checkObserver = { 1.84 + verifyStack: [], 1.85 + callback: null, 1.86 + 1.87 + waitForChecks: function(callback) { 1.88 + if (this.verifyStack.length == 0) 1.89 + callback(); 1.90 + else 1.91 + this.callback = callback; 1.92 + }, 1.93 + 1.94 + observe: function(subject, topic, data) { 1.95 + if (data != "formhistory-add" && data != "formhistory-update") 1.96 + return; 1.97 + ok(this.verifyStack.length > 0, "checking if saved form data was expected"); 1.98 + 1.99 + // Make sure that every piece of data we expect to be saved is saved, and no 1.100 + // more. Here it is assumed that for every entry satchel saves or modifies, a 1.101 + // message is sent. 1.102 + // 1.103 + // We don't actually check the content of the message, but just that the right 1.104 + // quantity of messages is received. 1.105 + // - if there are too few messages, test will time out 1.106 + // - if there are too many messages, test will error out here 1.107 + // 1.108 + var expected = this.verifyStack.shift(); 1.109 + 1.110 + countEntries(expected.name, expected.value, 1.111 + function(num) { 1.112 + ok(num > 0, expected.message); 1.113 + if (checkObserver.verifyStack.length == 0) { 1.114 + var callback = checkObserver.callback; 1.115 + checkObserver.callback = null; 1.116 + callback(); 1.117 + } 1.118 + }); 1.119 + } 1.120 +}; 1.121 + 1.122 +function checkForSave(name, value, message) { 1.123 + checkObserver.verifyStack.push({ name : name, value: value, message: message }); 1.124 +} 1.125 + 1.126 + 1.127 +function getFormSubmitButton(formNum) { 1.128 + var form = $("form" + formNum); // by id, not name 1.129 + ok(form != null, "getting form " + formNum); 1.130 + 1.131 + // we can't just call form.submit(), because that doesn't seem to 1.132 + // invoke the form onsubmit handler. 1.133 + var button = form.firstChild; 1.134 + while (button && button.type != "submit") { button = button.nextSibling; } 1.135 + ok(button != null, "getting form submit button"); 1.136 + 1.137 + return button; 1.138 +} 1.139 + 1.140 +// Count the number of entries with the given name and value, and call then(number) 1.141 +// when done. If name or value is null, then the value of that field does not matter. 1.142 +function countEntries(name, value, then) { 1.143 + var obj = {}; 1.144 + if (name !== null) 1.145 + obj.fieldname = name; 1.146 + if (value !== null) 1.147 + obj.value = value; 1.148 + 1.149 + var count = 0; 1.150 + SpecialPowers.formHistory.count(obj, { handleResult: function (result) { count = result }, 1.151 + handleError: function (error) { 1.152 + do_throw("Error occurred searching form history: " + error); 1.153 + }, 1.154 + handleCompletion: function (reason) { if (!reason) then(count); } 1.155 + }); 1.156 +} 1.157 + 1.158 +// Wrapper around FormHistory.update which handles errors. Calls then() when done. 1.159 +function updateFormHistory(changes, then) { 1.160 + SpecialPowers.formHistory.update(changes, { handleError: function (error) { 1.161 + do_throw("Error occurred updating form history: " + error); 1.162 + }, 1.163 + handleCompletion: function (reason) { if (!reason) then(); }, 1.164 + }); 1.165 +}