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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
<!DOCTYPE html>
<html>
<head>
<title>Test for sending IndexedDB Blobs through MessageManager</title>
<script src="/tests/SimpleTest/SimpleTest.js">
</script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript">
"use strict";
async function chromeScriptFunc() {
function childFrameScript() {
/* eslint-env mozilla/frame-script */
"use strict";
const mmName = "test:idb-and-mm";
const dbName = "test_message_manager_ipc.html - CHILD";
const dbVersion = 1;
const objStoreName = "bar";
const key = 1;
const blobData = ["So", " ", "many", " ", "blobs!"];
const blobText = blobData.join("");
const blobType = "text/plain";
function info(msg) {
sendAsyncMessage(mmName, { op: "info", msg });
}
function ok(condition, name, diag) {
sendAsyncMessage(mmName,
{ op: "ok",
condition,
name,
diag });
}
function is(a, b, name) {
let pass = a == b;
let diag = pass ? "" : "got " + a + ", expected " + b;
ok(pass, name, diag);
}
function finish(result) {
sendAsyncMessage(mmName, { op: "done", result });
}
function grabAndContinue(arg) {
testGenerator.next(arg);
}
function errorHandler(event) {
ok(false,
event.target + " received error event: '" + event.target.error.name +
"'");
finish();
}
function* testSteps() {
addMessageListener(mmName, grabAndContinue);
let message = yield undefined;
let blob = message.data;
ok(Blob.isInstance(blob), "Message manager sent a blob");
is(blob.size, blobText.length, "Blob has correct length");
is(blob.type, blobType, "Blob has correct type");
info("Reading blob");
let reader = new FileReader();
reader.addEventListener("load", grabAndContinue);
reader.readAsText(blob);
yield undefined;
is(reader.result, blobText, "Blob has correct data");
let slice = blob.slice(0, blobData[0].length, blobType);
ok(Blob.isInstance(slice), "Slice returned a blob");
is(slice.size, blobData[0].length, "Slice has correct length");
is(slice.type, blobType, "Slice has correct type");
info("Reading slice");
reader = new FileReader();
reader.addEventListener("load", grabAndContinue);
reader.readAsText(slice);
yield undefined;
is(reader.result, blobData[0], "Slice has correct data");
info("Deleting database");
let req = indexedDB.deleteDatabase(dbName);
req.onerror = errorHandler;
req.onsuccess = grabAndContinue;
let event = yield undefined;
is(event.type, "success", "Got success event");
info("Opening database");
req = indexedDB.open(dbName, dbVersion);
req.onerror = errorHandler;
req.onupgradeneeded = grabAndContinue;
req.onsuccess = grabAndContinue;
event = yield undefined;
is(event.type, "upgradeneeded", "Got upgradeneeded event");
event.target.result.createObjectStore(objStoreName);
event = yield undefined;
is(event.type, "success", "Got success event");
let db = event.target.result;
info("Storing blob from message manager in database");
let objectStore =
db.transaction(objStoreName, "readwrite").objectStore(objStoreName);
req = objectStore.add(blob, key);
req.onerror = errorHandler;
req.onsuccess = grabAndContinue;
event = yield undefined;
info("Getting blob from database");
objectStore = db.transaction(objStoreName).objectStore(objStoreName);
req = objectStore.get(key);
req.onerror = errorHandler;
req.onsuccess = grabAndContinue;
event = yield undefined;
blob = event.target.result;
ok(Blob.isInstance(blob), "Database gave us a blob");
is(blob.size, blobText.length, "Blob has correct length");
is(blob.type, blobType, "Blob has correct type");
info("Reading blob");
reader = new FileReader();
reader.addEventListener("load", grabAndContinue);
reader.readAsText(blob);
yield undefined;
is(reader.result, blobText, "Blob has correct data");
info("Storing slice from message manager in database");
objectStore =
db.transaction(objStoreName, "readwrite").objectStore(objStoreName);
req = objectStore.put(slice, key);
req.onerror = errorHandler;
req.onsuccess = grabAndContinue;
event = yield undefined;
info("Getting slice from database");
objectStore = db.transaction(objStoreName).objectStore(objStoreName);
req = objectStore.get(key);
req.onerror = errorHandler;
req.onsuccess = grabAndContinue;
event = yield undefined;
slice = event.target.result;
ok(Blob.isInstance(slice), "Database gave us a blob");
is(slice.size, blobData[0].length, "Slice has correct length");
is(slice.type, blobType, "Slice has correct type");
info("Reading Slice");
reader = new FileReader();
reader.addEventListener("load", grabAndContinue);
reader.readAsText(slice);
yield undefined;
is(reader.result, blobData[0], "Slice has correct data");
info("Sending blob and slice from database to message manager");
finish([blob, slice]);
}
let testGenerator = testSteps();
testGenerator.next();
}
/* globals Services, createWindowlessBrowser */
let { windowlessBrowser, browser } = await createWindowlessBrowser();
const system = Services.scriptSecurityManager.getSystemPrincipal();
browser.loadURI(
Services.io.newURI("data:text/html,<!DOCTYPE HTML><html><body></body></html>"),
{ triggeringPrincipal: system }
);
function finish() {
sendAsyncMessage("done");
windowlessBrowser.close();
}
let mm = browser.messageManager;
const messageName = "test:idb-and-mm";
const blobData = ["So", " ", "many", " ", "blobs!"];
const blobText = blobData.join("");
const blobType = "text/plain";
const blob = new Blob(blobData, { type: blobType });
function grabAndContinue(arg) {
testGenerator.next(arg);
}
function* testSteps() {
let result = yield undefined;
is(Cu.getClassName(result, true), "Array", "Child delivered an array of results");
is(result.length, 2, "Child delivered two results");
let blob = result[0];
is(Blob.isInstance(blob), true, "Child delivered a blob");
is(blob.size, blobText.length, "Blob has correct size");
is(blob.type, blobType, "Blob has correct type");
let slice = result[1];
is(Blob.isInstance(slice), true, "Child delivered a slice");
is(slice.size, blobData[0].length, "Slice has correct size");
is(slice.type, blobType, "Slice has correct type");
info("Reading blob");
let reader = new FileReader();
reader.onload = grabAndContinue;
reader.readAsText(blob);
yield undefined;
is(reader.result, blobText, "Blob has correct data");
info("Reading slice");
reader = new FileReader();
reader.onload = grabAndContinue;
reader.readAsText(slice);
yield undefined;
is(reader.result, blobData[0], "Slice has correct data");
slice = blob.slice(0, blobData[0].length, blobType);
is(Blob.isInstance(slice), true, "Child delivered a slice");
is(slice.size, blobData[0].length, "Second slice has correct size");
is(slice.type, blobType, "Second slice has correct type");
info("Reading second slice");
reader = new FileReader();
reader.onload = grabAndContinue;
reader.readAsText(slice);
yield undefined;
is(reader.result, blobData[0], "Second slice has correct data");
finish();
}
let testGenerator = testSteps();
testGenerator.next();
mm.addMessageListener(messageName, function(message) {
let data = message.data;
switch (data.op) {
case "info": {
info(data.msg);
break;
}
case "ok": {
ok(data.condition, data.name + " - " + data.diag);
break;
}
case "done": {
testGenerator.next(data.result);
break;
}
default: {
ok(false, "Unknown op: " + data.op);
finish();
}
}
});
mm.loadFrameScript(`data:,(${childFrameScript})();`,
false);
mm.sendAsyncMessage(messageName, blob);
}
add_task(async function() {
let chromeScript = SpecialPowers.loadChromeScript(chromeScriptFunc);
await chromeScript.promiseOneMessage("done");
await chromeScript.destroy();
});
</script>
</body>
</html>
|