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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// This file expects utils.js to be included in its scope
/* import-globals-from ./util.js */
var MOCHITEST = false;
function Test(name, test) {
this.name = name;
this.startTime = null;
this.endTime = null;
this.result = null;
this.row = null;
this.run = function () {
// Note the start time
this.startTime = new Date();
// Run the test
try {
test.call(this);
} catch (e) {
console.log(e);
console.log(e.stack);
this.complete(false);
}
};
this.memcmp_complete = function (x, y) {
var passfail = util.memcmp(x, y);
if (!passfail) {
console.log("expected: " + util.abv2hex(x));
console.log(" got: " + util.abv2hex(y));
}
this.complete(passfail);
};
this.complete = function (result) {
if (MOCHITEST) {
ok(result, this.name);
}
// Note the end time
this.endTime = new Date();
// Set result
this.result = result;
// Re-draw the row
this.draw();
this.next();
};
this.next = function () {
if (this.oncomplete) {
this.oncomplete();
}
};
this.setRow = function (id) {
this.row = document.getElementById(id).getElementsByTagName("td");
};
this.draw = function () {
if (!this.row) {
return;
}
// Print the name of the test
if (this.name) {
this.row[0].textContent = this.name;
var that = this;
this.row[0].onclick = function () {
that.run();
};
} else {
this.row[0] = "";
}
// Print the result of the test
if (this.result === true) {
this.row[1].className = "pass";
this.row[1].innerHTML = "PASS";
} else if (this.result === false) {
this.row[1].className = "fail";
this.row[1].innerHTML = "FAIL";
} else {
// this.row[1].innerHTML = "";
this.row[1].textContent = this.result;
}
// Print the elapsed time, if known
if (this.startTime && this.endTime) {
this.row[2].textContent = this.endTime - this.startTime + " ms";
} else {
this.row[2].innerHTML = "";
}
};
}
function WorkerTest(worker, name, test) {
this.name = `${name} (Worker)`;
this.startTime = null;
this.endTime = null;
this.result = null;
this.row = null;
this.run = function () {
// Note the start time
this.startTime = new Date();
// Send the test code to the worker.
worker.postMessage(test.toString());
// We expect only boolean responses from the worker script.
worker.onmessage = e => this.complete(e.data);
worker.onerror = () => this.complete(false);
};
var base = new Test(name, test);
// Inherit what we need from the |Test| class. We can't simply use its
// prototype as Test is just a function that can be used like a constructor.
for (var method of ["draw", "setRow", "next", "complete"]) {
this[method] = base[method].bind(this);
}
}
var TestArray = {
tests: [],
table: null,
passSpan: null,
failSpan: null,
pendingSpan: null,
pass: 0,
fail: 0,
pending: 0,
currTest: 0,
worker: new Worker("test-worker.js"),
addTest(name, testFn) {
// Give it a reference to the array
var test = new Test(name, testFn);
test.ta = this;
// Add test to tests
this.tests.push(test);
// Run the test in a worker too.
this.tests.push(new WorkerTest(this.worker, name, testFn));
},
updateSummary() {
this.pass = this.fail = this.pending = 0;
for (var i = 0; i < this.tests.length; ++i) {
if (this.tests[i].result === true) {
this.pass++;
}
if (this.tests[i].result === false) {
this.fail++;
}
if (this.tests[i].result == null) {
this.pending++;
}
}
this.passSpan.textContent = this.pass;
this.failSpan.textContent = this.fail;
this.pendingSpan.textContent = this.pending;
},
load() {
// Grab reference to table and summary numbers
this.table = document.getElementById("results");
this.passSpan = document.getElementById("passN");
this.failSpan = document.getElementById("failN");
this.pendingSpan = document.getElementById("pendingN");
// Populate everything initially
this.updateSummary();
for (var i = 0; i < this.tests.length; ++i) {
var tr = document.createElement("tr");
tr.id = "test" + i;
tr.appendChild(document.createElement("td"));
tr.appendChild(document.createElement("td"));
tr.appendChild(document.createElement("td"));
this.table.appendChild(tr);
this.tests[i].setRow(tr.id);
this.tests[i].draw();
}
},
run() {
this.currTest = 0;
this.runNextTest();
},
runNextTest() {
this.updateSummary();
var i = this.currTest++;
if (i >= this.tests.length) {
if (MOCHITEST) {
SimpleTest.finish();
}
return;
}
var self = this;
this.tests[i].oncomplete = function () {
self.runNextTest();
};
this.tests[i].run();
},
};
if (window.addEventListener) {
window.addEventListener("load", function () {
TestArray.load();
});
} else {
window.attachEvent("onload", function () {
TestArray.load();
});
}
function start() {
TestArray.run();
}
MOCHITEST = "SimpleTest" in window;
if (MOCHITEST) {
SimpleTest.waitForExplicitFinish();
SimpleTest.requestLongerTimeout(2);
window.addEventListener("load", function () {
SimpleTest.waitForFocus(start);
});
}
function error(test) {
return function (x) {
console.log("ERROR :: " + x);
test.complete(false);
throw x;
};
}
function complete(test, valid) {
return function (x) {
console.log("COMPLETE");
console.log(x);
if (valid) {
test.complete(valid(x));
} else {
test.complete(true);
}
};
}
function memcmp_complete(test, value) {
return function (x) {
console.log("COMPLETE");
console.log(x);
test.memcmp_complete(value, x);
};
}
|