forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
harness.js
246 lines (212 loc) · 8.75 KB
/
harness.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const TESTROOT = "http://example.com/browser/xpinstall/tests/";
const TESTROOT2 = "http://example.org/browser/xpinstall/tests/";
const XPINSTALL_URL = "chrome://mozapps/content/xpinstall/xpinstallConfirm.xul";
const PROMPT_URL = "chrome://global/content/commonDialog.xul";
const ADDONS_URL = "chrome://mozapps/content/extensions/extensions.xul";
var rootDir = getRootDirectory(gTestPath);
var path = rootDir.split('/');
var chromeName = path[0] + '//' + path[2];
var croot = chromeName + "/content/browser/xpinstall/tests/";
var jar = getJar(croot);
if (jar) {
var tmpdir = extractJarToTmp(jar);
croot = 'file://' + tmpdir.path + '/';
}
const CHROMEROOT = croot;
/**
* This is a test harness designed to handle responding to UI during the process
* of installing an XPI. A test can set callbacks to hear about specific parts
* of the sequence.
* Before use setup must be called and finish must be called afterwards.
*/
var Harness = {
// If set then the install is expected to be blocked by the whitelist. The
// callback should return true to continue with the install anyway.
installBlockedCallback: null,
// If set will be called in the event of authentication being needed to get
// the xpi. Should return a 2 element array of username and password, or
// null to not authenticate.
authenticationCallback: null,
// If set this will be called to allow checking the contents of the xpinstall
// confirmation dialog. The callback should return true to continue the install.
installConfirmCallback: null,
// If set will be called when downloading of an item has begun.
downloadStartedCallback: null,
// If set will be called during the download of an item.
downloadProgressCallback: null,
// If set will be called when downloading of an item has ended.
downloadEndedCallback: null,
// If set will be called when installation by the extension manager of an xpi
// item starts
installStartedCallback: null,
// If set will be called when each xpi item to be installed completes
// installation.
installEndedCallback: null,
// If set will be called when all triggered items are installed or the install
// is canceled.
installsCompletedCallback: null,
listenerIndex: null,
// Setup and tear down functions
setup: function() {
waitForExplicitFinish();
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
os.addObserver(this, "xpinstall-install-blocked", false);
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
wm.addListener(this);
var em = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager);
this.listenerIndex = em.addInstallListener(this);
},
finish: function() {
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
os.removeObserver(this, "xpinstall-install-blocked");
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
wm.removeListener(this);
var win = wm.getMostRecentWindow("Extension:Manager");
if (win)
win.close();
var em = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager);
em.removeInstallListenerAt(this.listenerIndex);
finish();
},
endTest: function() {
// Defer the final notification to allow things like the InstallTrigger
// callback to complete
executeSoon(this.installsCompletedCallback);
this.installBlockedCallback = null;
this.authenticationCallback = null;
this.installConfirmCallback = null;
this.downloadStartedCallback = null;
this.downloadProgressCallback = null;
this.downloadEndedCallback = null;
this.installStartedCallback = null;
this.installEndedCallback = null;
this.installsCompletedCallback = null;
},
// Window open handling
windowLoad: function(window) {
// Allow any other load handlers to execute
var self = this;
executeSoon(function() { self.windowReady(window); } );
},
windowReady: function(window) {
if (window.document.location.href == XPINSTALL_URL) {
if (this.installBlockedCallback)
ok(false, "Should have been blocked by the whitelist");
// If there is a confirm callback then its return status determines whether
// to install the items or not. If not the test is over.
if (this.installConfirmCallback && !this.installConfirmCallback(window)) {
window.document.documentElement.cancelDialog();
this.endTest();
}
else {
// Initially the accept button is disabled on a countdown timer
var button = window.document.documentElement.getButton("accept");
button.disabled = false;
window.document.documentElement.acceptDialog();
}
}
else if (window.document.location.href == PROMPT_URL) {
switch (window.args.promptType) {
default:
if (window.opener.document.location.href == ADDONS_URL) {
// A prompt opened by the add-ons manager is liable to be an
// xpinstall error, just close it, we'll see the error in
// onInstallEnded anyway.
window.document.documentElement.acceptDialog();
}
break;
case "promptUserAndPass":
// This is a login dialog, hopefully an authentication prompt
// for the xpi.
if (this.authenticationCallback) {
var auth = this.authenticationCallback();
if (auth && auth.length == 2) {
window.document.getElementById("loginTextbox").value = auth[0];
window.document.getElementById("password1Textbox").value = auth[1];
window.document.documentElement.acceptDialog();
}
else {
window.document.documentElement.cancelDialog();
}
}
else {
window.document.documentElement.cancelDialog();
}
break;
}
}
},
// Install blocked handling
installBlocked: function(installInfo) {
ok(!!this.installBlockedCallback, "Shouldn't have been blocked by the whitelist");
if (this.installBlockedCallback && this.installBlockedCallback(installInfo)) {
this.installBlockedCallback = null;
var mgr = Components.classes["@mozilla.org/xpinstall/install-manager;1"]
.createInstance(Components.interfaces.nsIXPInstallManager);
mgr.initManagerWithInstallInfo(installInfo);
}
else {
this.endTest();
}
},
// nsIWindowMediatorListener
onWindowTitleChange: function(window, title) {
},
onOpenWindow: function(window) {
var domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
var self = this;
domwindow.addEventListener("load", function() {
self.windowLoad(domwindow);
}, false);
},
onCloseWindow: function(window) {
},
// nsIAddonInstallListener
onDownloadStarted: function(addon) {
if (this.downloadStartedCallback)
this.downloadStartedCallback(addon);
},
onDownloadProgress: function(addon, value, maxValue) {
if (this.downloadProgressCallback)
this.downloadProgressCallback(addon, value, maxValue);
},
onDownloadEnded: function(addon) {
if (this.downloadEndedCallback)
this.downloadEndedCallback(addon);
},
onInstallStarted: function(addon) {
if (this.installStartedCallback)
this.installStartedCallback(addon);
},
onCompatibilityCheckStarted: function(addon) {
},
onCompatibilityCheckEnded: function(addon, status) {
},
onInstallEnded: function(addon, status) {
if (this.installEndedCallback)
this.installEndedCallback(addon, status);
},
onInstallsCompleted: function() {
this.endTest();
},
// nsIObserver
observe: function(subject, topic, data) {
var installInfo = subject.QueryInterface(Components.interfaces.nsIXPIInstallInfo);
this.installBlocked(installInfo);
},
QueryInterface: function(iid) {
if (iid.equals(Components.interfaces.nsIObserver) ||
iid.equals(Components.interfaces.nsIAddonInstallListener) ||
iid.equals(Components.interfaces.nsIWindowMediatorListener) ||
iid.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
}
}