-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtesting.js
More file actions
274 lines (244 loc) · 7.58 KB
/
testing.js
File metadata and controls
274 lines (244 loc) · 7.58 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
// A simple testing framework for SIDL defined interfaces.
"use strict";
function deep_equals(a, b) {
// If they're triple equals, then it must be equals!
if (a === b) {
return true;
}
// Special case for URL objects: we compare the full href.
if (
Object.getPrototypeOf(a) === URL.prototype &&
Object.getPrototypeOf(b) === URL.prototype
) {
return a.href === b.href;
}
// If they weren't equal, they must be objects to be different
if (typeof a != "object" || typeof b != "object") {
return false;
}
// But null objects won't have properties to compare
if (a === null || b === null) {
return false;
}
// Make sure all of a's keys have a matching value in b
for (let k in a) {
if (!deep_equals(a[k], b[k])) {
return false;
}
}
// Do the same for b's keys but skip those that we already checked
for (let k in b) {
if (!(k in a) && !deep_equals(a[k], b[k])) {
return false;
}
}
return true;
}
class SidlEventHandler {
constructor(target, event_kind) {
this.values = [];
this.waiters = [];
this.handler = this.handle_event.bind(this);
this.target = target;
this.event_kind = event_kind;
this.target.addEventListener(event_kind, this.handler);
}
handle_event(value) {
// We got a new event value. Push it to our queue, and check
// if we have waiters for it.
this.values.push(value);
this.notify_next_waiter();
}
notify_next_waiter() {
if (!this.waiters.length || !this.values.length) {
return;
}
this.waiters.shift()(this.values.shift());
}
add_waiter(func) {
this.waiters.push(func);
this.notify_next_waiter();
}
stop() {
this.target.removeEventListener(this.event_kind, this.handler);
}
}
class ServiceTester {
constructor(service, tester_name, session) {
this.service = service;
this.tester_name = tester_name || "";
this.results = [];
this.session = session;
}
// Returns a handler to wait on an event to be dispatched.
setup_event(event_kind) {
return this.setup_event_on(this.service, event_kind);
}
// Returns a handler to wait on an event to be dispatched.
setup_event_on(object, event_kind) {
return new SidlEventHandler(object, event_kind);
}
// Return a promise than resolves with the next value dispatched
// for this event.
next_event_value(event_handler) {
return new Promise((resolve) => {
event_handler.add_waiter(resolve);
});
}
assert_event_eq(description, event_handler, expected = undefined) {
return this.assert_eq(
description,
() => {
return this.next_event_value(event_handler);
},
expected
);
}
// Runs an async runnable and checks the returned value.
assert_eq(description, runnable, expected, transform) {
let start = Date.now();
description = `${this.tester_name}:${description}`;
return new Promise((resolve) => {
try {
runnable(this.service).then(
(observed) => {
let elapsed = Date.now() - start;
if (transform) {
observed = transform(observed);
}
if (deep_equals(observed, expected)) {
this.results.push({ description, success: true, elapsed });
} else {
this.results.push({
description,
success: false,
observed: `${typeof observed} ${JSON.stringify(observed)}`,
expected: `${typeof expected} ${JSON.stringify(expected)}`,
});
}
resolve();
},
(error) => {
console.error(
`Testing Errror is ${error} ${JSON.stringify(error)}`
);
this.results.push({ description, success: false, error });
resolve();
}
);
} catch (error) {
console.error(`Testing Exception: ${error}`);
console.error(error.stack);
this.results.push({ description, success: false, error });
resolve();
}
});
}
// Asserts the rejected value of a call instead of the resolved one.
assert_rej_eq(description, runnable, expected, transform) {
let start = Date.now();
description = `${this.tester_name}:${description}`;
return new Promise((resolve) => {
try {
runnable(this.service).then(
(error) => {
this.results.push({ description, success: false, error });
resolve();
},
(observed) => {
observed = observed.value;
let elapsed = Date.now() - start;
if (transform) {
observed = transform(observed);
}
if (deep_equals(observed, expected)) {
this.results.push({ description, success: true, elapsed });
} else {
this.results.push({
description,
success: false,
observed: `${typeof observed} ${JSON.stringify(observed)}`,
expected: `${typeof expected} ${JSON.stringify(expected)}`,
});
}
resolve();
}
);
} catch (error) {
this.results.push({ description, success: false, error });
resolve();
}
});
}
}
// Returns a promise resolving to a ServiceTester attached to the service.
function test_service(service, tester_name, existing_session) {
return new Promise((resolve, reject) => {
let session = existing_session
? existing_session
: new lib_session.Session();
let sessionstate = {
onsessionconnected() {
service.get(session).then((service) => {
resolve(new ServiceTester(service, tester_name, session));
}, reject);
},
onsessiondisconnected() {
reject("Session Disconnected");
},
};
if (navigator.b2g.externalapi) {
navigator.b2g.externalapi.getToken().then((token) => {
session.open("websocket", "localhost:8081", token, sessionstate);
});
} else {
reject("Failed to open session.");
}
});
}
// Aggregates the results of several testers and combine them to produce
// a single result set.
class TestReporter {
constructor(testers) {
this.results = [];
testers.forEach((tester) => {
this.results = this.results.concat(tester.results);
});
}
// Creates a test report in an element with id "test-results".
output() {
let element = document.createElement("div");
element.setAttribute("id", "test-results");
let success = 0;
let failures = 0;
let html = `<div id="header">${this.results.length} tests completed.</div>`;
this.results.forEach((item) => {
if (item.success) {
success += 1;
html += `<div class="success">[${item.description}] : Success</div>`;
} else {
failures += 1;
html += `<div class="failure">[${item.description}] : Failure
<div>Expected: <pre>${item.expected}</pre></div>
<div>Observed: <pre>${item.observed}</pre></div>
</div>`;
}
});
html += `<div id="footer" class="${
failures == 0 ? "success" : ""
}"><span class="success">Success: ${success}</span>, <span class="failure">Failures: ${failures}</span></div>`;
element.innerHTML = html;
let hidden = document.createElement("div");
hidden.classList.add("json");
hidden.textContent = JSON.stringify(this.results);
document.body.appendChild(element);
document.body.appendChild(hidden);
}
}
function createAsyncTask() {
const asyncTask = {};
asyncTask.isFinished = new Promise((resolve) => {
asyncTask.finish = resolve;
});
return asyncTask;
}