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
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/FormHistory.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://testing-common/ContentTaskUtils.jsm");
var gAutocompletePopup = Services.ww.activeWindow.
document.
getElementById("PopupAutoComplete");
assert.ok(gAutocompletePopup, "Got autocomplete popup");
var ParentUtils = {
getMenuEntries() {
let entries = [];
let numRows = gAutocompletePopup.view.matchCount;
for (let i = 0; i < numRows; i++) {
entries.push(gAutocompletePopup.view.getValueAt(i));
}
return entries;
},
cleanUpFormHist() {
FormHistory.update({ op: "remove" });
},
updateFormHistory(changes) {
let handler = {
handleError: function (error) {
assert.ok(false, error);
sendAsyncMessage("formHistoryUpdated", { ok: false });
},
handleCompletion: function (reason) {
if (!reason)
sendAsyncMessage("formHistoryUpdated", { ok: true });
},
};
FormHistory.update(changes, handler);
},
popupshownListener() {
let results = this.getMenuEntries();
sendAsyncMessage("onpopupshown", { results });
},
countEntries(name, value) {
let obj = {};
if (name)
obj.fieldname = name;
if (value)
obj.value = value;
let count = 0;
let listener = {
handleResult(result) { count = result },
handleError(error) {
assert.ok(false, error);
sendAsyncMessage("entriesCounted", { ok: false });
},
handleCompletion(reason) {
if (!reason) {
sendAsyncMessage("entriesCounted", { ok: true, count });
}
}
};
FormHistory.count(obj, listener);
},
checkRowCount(expectedCount, expectedFirstValue = null) {
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: " + expectedCount + " First value: " + expectedFirstValue).then(() => {
let results = this.getMenuEntries();
sendAsyncMessage("gotMenuChange", { results });
});
},
checkSelectedIndex(expectedIndex) {
ContentTaskUtils.waitForCondition(() => {
return gAutocompletePopup.popupOpen &&
gAutocompletePopup.selectedIndex === expectedIndex;
}, "Checking selected index").then(() => {
sendAsyncMessage("gotSelectedIndex");
});
},
getPopupState() {
sendAsyncMessage("gotPopupState", {
open: gAutocompletePopup.popupOpen,
selectedIndex: gAutocompletePopup.selectedIndex,
direction: gAutocompletePopup.style.direction,
});
},
observe(subject, topic, data) {
assert.ok(topic === "satchel-storage-changed");
sendAsyncMessage("satchel-storage-changed", { subject: null, topic, data });
},
cleanup() {
gAutocompletePopup.removeEventListener("popupshown", this._popupshownListener);
this.cleanUpFormHist();
}
};
ParentUtils._popupshownListener =
ParentUtils.popupshownListener.bind(ParentUtils);
gAutocompletePopup.addEventListener("popupshown", ParentUtils._popupshownListener);
ParentUtils.cleanUpFormHist();
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("getPopupState", () => {
ParentUtils.getPopupState();
});
addMessageListener("addObserver", () => {
Services.obs.addObserver(ParentUtils, "satchel-storage-changed", false);
});
addMessageListener("removeObserver", () => {
Services.obs.removeObserver(ParentUtils, "satchel-storage-changed");
});
addMessageListener("cleanup", () => {
ParentUtils.cleanup();
});
|