toolkit/components/satchel/test/satchel_common.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 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var Services = SpecialPowers.Services;
michael@0 6
michael@0 7 /*
michael@0 8 * $_
michael@0 9 *
michael@0 10 * Returns the element with the specified |name| attribute.
michael@0 11 */
michael@0 12 function $_(formNum, name) {
michael@0 13 var form = document.getElementById("form" + formNum);
michael@0 14 if (!form) {
michael@0 15 ok(false, "$_ couldn't find requested form " + formNum);
michael@0 16 return null;
michael@0 17 }
michael@0 18
michael@0 19 var element = form.elements.namedItem(name);
michael@0 20 if (!element) {
michael@0 21 ok(false, "$_ couldn't find requested element " + name);
michael@0 22 return null;
michael@0 23 }
michael@0 24
michael@0 25 // Note that namedItem is a bit stupid, and will prefer an
michael@0 26 // |id| attribute over a |name| attribute when looking for
michael@0 27 // the element.
michael@0 28
michael@0 29 if (element.hasAttribute("name") && element.getAttribute("name") != name) {
michael@0 30 ok(false, "$_ got confused.");
michael@0 31 return null;
michael@0 32 }
michael@0 33
michael@0 34 return element;
michael@0 35 }
michael@0 36
michael@0 37 // Mochitest gives us a sendKey(), but it's targeted to a specific element.
michael@0 38 // This basically sends an untargeted key event, to whatever's focused.
michael@0 39 function doKey(aKey, modifier) {
michael@0 40 var keyName = "DOM_VK_" + aKey.toUpperCase();
michael@0 41 var key = SpecialPowers.Ci.nsIDOMKeyEvent[keyName];
michael@0 42
michael@0 43 // undefined --> null
michael@0 44 if (!modifier)
michael@0 45 modifier = null;
michael@0 46
michael@0 47 // Window utils for sending fake sey events.
michael@0 48 var wutils = SpecialPowers.getDOMWindowUtils(window);
michael@0 49
michael@0 50 if (wutils.sendKeyEvent("keydown", key, 0, modifier)) {
michael@0 51 wutils.sendKeyEvent("keypress", key, 0, modifier);
michael@0 52 }
michael@0 53 wutils.sendKeyEvent("keyup", key, 0, modifier);
michael@0 54 }
michael@0 55
michael@0 56
michael@0 57 function getAutocompletePopup() {
michael@0 58 var Ci = SpecialPowers.Ci;
michael@0 59 chromeWin = SpecialPowers.wrap(window)
michael@0 60 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 61 .getInterface(Ci.nsIWebNavigation)
michael@0 62 .QueryInterface(Ci.nsIDocShellTreeItem)
michael@0 63 .rootTreeItem
michael@0 64 .QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 65 .getInterface(Ci.nsIDOMWindow)
michael@0 66 .QueryInterface(Ci.nsIDOMChromeWindow);
michael@0 67 autocompleteMenu = chromeWin.document.getElementById("PopupAutoComplete");
michael@0 68 ok(autocompleteMenu, "Got autocomplete popup");
michael@0 69
michael@0 70 return autocompleteMenu;
michael@0 71 }
michael@0 72
michael@0 73
michael@0 74 function cleanUpFormHist() {
michael@0 75 SpecialPowers.formHistory.update({ op : "remove" });
michael@0 76 }
michael@0 77 cleanUpFormHist();
michael@0 78
michael@0 79
michael@0 80 var checkObserver = {
michael@0 81 verifyStack: [],
michael@0 82 callback: null,
michael@0 83
michael@0 84 waitForChecks: function(callback) {
michael@0 85 if (this.verifyStack.length == 0)
michael@0 86 callback();
michael@0 87 else
michael@0 88 this.callback = callback;
michael@0 89 },
michael@0 90
michael@0 91 observe: function(subject, topic, data) {
michael@0 92 if (data != "formhistory-add" && data != "formhistory-update")
michael@0 93 return;
michael@0 94 ok(this.verifyStack.length > 0, "checking if saved form data was expected");
michael@0 95
michael@0 96 // Make sure that every piece of data we expect to be saved is saved, and no
michael@0 97 // more. Here it is assumed that for every entry satchel saves or modifies, a
michael@0 98 // message is sent.
michael@0 99 //
michael@0 100 // We don't actually check the content of the message, but just that the right
michael@0 101 // quantity of messages is received.
michael@0 102 // - if there are too few messages, test will time out
michael@0 103 // - if there are too many messages, test will error out here
michael@0 104 //
michael@0 105 var expected = this.verifyStack.shift();
michael@0 106
michael@0 107 countEntries(expected.name, expected.value,
michael@0 108 function(num) {
michael@0 109 ok(num > 0, expected.message);
michael@0 110 if (checkObserver.verifyStack.length == 0) {
michael@0 111 var callback = checkObserver.callback;
michael@0 112 checkObserver.callback = null;
michael@0 113 callback();
michael@0 114 }
michael@0 115 });
michael@0 116 }
michael@0 117 };
michael@0 118
michael@0 119 function checkForSave(name, value, message) {
michael@0 120 checkObserver.verifyStack.push({ name : name, value: value, message: message });
michael@0 121 }
michael@0 122
michael@0 123
michael@0 124 function getFormSubmitButton(formNum) {
michael@0 125 var form = $("form" + formNum); // by id, not name
michael@0 126 ok(form != null, "getting form " + formNum);
michael@0 127
michael@0 128 // we can't just call form.submit(), because that doesn't seem to
michael@0 129 // invoke the form onsubmit handler.
michael@0 130 var button = form.firstChild;
michael@0 131 while (button && button.type != "submit") { button = button.nextSibling; }
michael@0 132 ok(button != null, "getting form submit button");
michael@0 133
michael@0 134 return button;
michael@0 135 }
michael@0 136
michael@0 137 // Count the number of entries with the given name and value, and call then(number)
michael@0 138 // when done. If name or value is null, then the value of that field does not matter.
michael@0 139 function countEntries(name, value, then) {
michael@0 140 var obj = {};
michael@0 141 if (name !== null)
michael@0 142 obj.fieldname = name;
michael@0 143 if (value !== null)
michael@0 144 obj.value = value;
michael@0 145
michael@0 146 var count = 0;
michael@0 147 SpecialPowers.formHistory.count(obj, { handleResult: function (result) { count = result },
michael@0 148 handleError: function (error) {
michael@0 149 do_throw("Error occurred searching form history: " + error);
michael@0 150 },
michael@0 151 handleCompletion: function (reason) { if (!reason) then(count); }
michael@0 152 });
michael@0 153 }
michael@0 154
michael@0 155 // Wrapper around FormHistory.update which handles errors. Calls then() when done.
michael@0 156 function updateFormHistory(changes, then) {
michael@0 157 SpecialPowers.formHistory.update(changes, { handleError: function (error) {
michael@0 158 do_throw("Error occurred updating form history: " + error);
michael@0 159 },
michael@0 160 handleCompletion: function (reason) { if (!reason) then(); },
michael@0 161 });
michael@0 162 }

mercurial