1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/* eslint-env mozilla/chrome-script */
const { FormHistory } = ChromeUtils.importESModule(
"resource://gre/modules/FormHistory.sys.mjs"
);
const { ContentTaskUtils } = ChromeUtils.importESModule(
"resource://testing-common/ContentTaskUtils.sys.mjs"
);
const { TestUtils } = ChromeUtils.importESModule(
"resource://testing-common/TestUtils.sys.mjs"
);
var gAutocompletePopup =
Services.ww.activeWindow.document.getElementById("PopupAutoComplete");
assert.ok(gAutocompletePopup, "Got autocomplete popup");
var ParentUtils = {
// Returns a object with two fields:
// labels - an array of the labels of the current dropdown
// comments - an array of the comments of the current dropdown
getMenuEntries() {
let labels = [],
comments = [];
let numRows = gAutocompletePopup.view.matchCount;
for (let i = 0; i < numRows; i++) {
labels.push(gAutocompletePopup.view.getLabelAt(i));
comments.push(gAutocompletePopup.view.getCommentAt(i));
}
return { labels, comments };
},
cleanUpFormHistory() {
return FormHistory.update({ op: "remove" });
},
updateFormHistory(changes) {
FormHistory.update(changes).then(
() => {
sendAsyncMessage("formHistoryUpdated", { ok: true });
},
error => {
sendAsyncMessage("formHistoryUpdated", { ok: false });
assert.ok(false, error);
}
);
},
popupshownListener() {
let entries = this.getMenuEntries();
sendAsyncMessage("onpopupshown", entries);
},
countEntries(name, value) {
let obj = {};
if (name) {
obj.fieldname = name;
}
if (value) {
obj.value = value;
}
FormHistory.count(obj).then(
count => {
sendAsyncMessage("entriesCounted", { ok: true, count });
},
error => {
assert.ok(false, error);
sendAsyncMessage("entriesCounted", { ok: false });
}
);
},
async checkRowCount(expectedCount, expectedFirstValue = null) {
await ContentTaskUtils.waitForCondition(() => {
// This may be called before gAutocompletePopup has initialised
// which causes it to throw
try {
return (
gAutocompletePopup.view.matchCount === expectedCount &&
(!expectedFirstValue ||
expectedCount <= 1 ||
gAutocompletePopup.view.getValueAt(0) === expectedFirstValue)
);
} catch (e) {
return false;
}
}, `Waiting for row count change to ${expectedCount}, first value: ${expectedFirstValue}.`);
return this.getMenuEntries();
},
async checkSelectedIndex(expectedIndex) {
await ContentTaskUtils.waitForCondition(
() =>
gAutocompletePopup.popupOpen &&
gAutocompletePopup.selectedIndex === expectedIndex,
"Checking selected index"
);
},
// Tests using this function need to flip pref for exceptional use of
// `new Function` / `eval()`.
// See test_autofill_and_ordinal_forms.html for example.
testMenuEntry(index, statement) {
ContentTaskUtils.waitForCondition(() => {
let el = gAutocompletePopup.richlistbox.getItemAtIndex(index);
let testFunc = new Services.ww.activeWindow.Function(
"el",
`return ${statement}`
);
return gAutocompletePopup.popupOpen && el && testFunc(el);
}, "Testing menu entry").then(() => {
sendAsyncMessage("menuEntryTested");
});
},
getPopupState() {
function reply() {
sendAsyncMessage("gotPopupState", {
open: gAutocompletePopup.popupOpen,
selectedIndex: gAutocompletePopup.selectedIndex,
direction: gAutocompletePopup.style.direction,
});
}
// If the popup state is stable, we can reply immediately. However, if
// it's showing or hiding, we should wait its finish and then, send the
// reply.
if (
gAutocompletePopup.state == "open" ||
gAutocompletePopup.state == "closed"
) {
reply();
return;
}
const stablerState =
gAutocompletePopup.state == "showing" ? "open" : "closed";
TestUtils.waitForCondition(
() => gAutocompletePopup.state == stablerState,
`Waiting for autocomplete popup getting "${stablerState}" state`
).then(reply);
},
observe(_subject, topic, data) {
// This function can be called after SimpleTest.finish().
// Do not write assertions here, they will lead to intermittent failures.
sendAsyncMessage("satchel-storage-changed", { subject: null, topic, data });
},
async cleanup() {
gAutocompletePopup.removeEventListener(
"popupshown",
this._popupshownListener
);
await this.cleanUpFormHistory();
},
};
ParentUtils._popupshownListener =
ParentUtils.popupshownListener.bind(ParentUtils);
gAutocompletePopup.addEventListener(
"popupshown",
ParentUtils._popupshownListener
);
ParentUtils.cleanUpFormHistory();
addMessageListener("updateFormHistory", msg => {
ParentUtils.updateFormHistory(msg.changes);
});
addMessageListener("countEntries", ({ name, value }) => {
ParentUtils.countEntries(name, value);
});
addMessageListener(
"waitForMenuChange",
({ expectedCount, expectedFirstValue }) =>
ParentUtils.checkRowCount(expectedCount, expectedFirstValue)
);
addMessageListener("waitForSelectedIndex", ({ expectedIndex }) =>
ParentUtils.checkSelectedIndex(expectedIndex)
);
addMessageListener("waitForMenuEntryTest", ({ index, statement }) => {
ParentUtils.testMenuEntry(index, statement);
});
addMessageListener("getPopupState", () => {
ParentUtils.getPopupState();
});
addMessageListener("addObserver", () => {
Services.obs.addObserver(ParentUtils, "satchel-storage-changed");
});
addMessageListener("removeObserver", () => {
Services.obs.removeObserver(ParentUtils, "satchel-storage-changed");
});
addMessageListener("cleanup", async () => {
await ParentUtils.cleanup();
});
|