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
|
/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
/* import-globals-from helpers.js */
var bufferCache = [];
var utils = SpecialPowers.getDOMWindowUtils(window);
function getBuffer(size) {
let buffer = new ArrayBuffer(size);
is(buffer.byteLength, size, "Correct byte length");
return buffer;
}
function getRandomBuffer(size) {
let buffer = getBuffer(size);
let view = new Uint8Array(buffer);
for (let i = 0; i < size; i++) {
view[i] = parseInt(Math.random() * 255);
}
return buffer;
}
function getView(size) {
let buffer = new ArrayBuffer(size);
let view = new Uint8Array(buffer);
is(buffer.byteLength, size, "Correct byte length");
return view;
}
function getRandomView(size) {
let view = getView(size);
for (let i = 0; i < size; i++) {
view[i] = parseInt(Math.random() * 255);
}
return view;
}
function compareBuffers(buffer1, buffer2) {
if (buffer1.byteLength != buffer2.byteLength) {
return false;
}
let view1 = buffer1 instanceof Uint8Array ? buffer1 : new Uint8Array(buffer1);
let view2 = buffer2 instanceof Uint8Array ? buffer2 : new Uint8Array(buffer2);
for (let i = 0; i < buffer1.byteLength; i++) {
if (view1[i] != view2[i]) {
return false;
}
}
return true;
}
function getBlob(type, view) {
return new Blob([view], { type });
}
function getFile(name, type, view) {
return new File([view], name, { type });
}
function getRandomBlob(size) {
return getBlob("binary/random", getRandomView(size));
}
function getRandomFile(name, size) {
return getFile(name, "binary/random", getRandomView(size));
}
function getNullBlob(size) {
return getBlob("binary/null", getView(size));
}
function getNullFile(name, size) {
return getFile(name, "binary/null", getView(size));
}
function getWasmModule(binary) {
let module = new WebAssembly.Module(binary);
return module;
}
function verifyBuffers(buffer1, buffer2) {
ok(compareBuffers(buffer1, buffer2), "Correct buffer data");
}
function verifyBlob(blob1, blob2, fileId, blobReadHandler) {
// eslint-disable-next-line mozilla/use-cc-etc
is(SpecialPowers.wrap(Blob).isInstance(blob1), true, "Instance of Blob");
is(blob1 instanceof File, blob2 instanceof File, "Instance of DOM File");
is(blob1.size, blob2.size, "Correct size");
is(blob1.type, blob2.type, "Correct type");
if (blob2 instanceof File) {
is(blob1.name, blob2.name, "Correct name");
}
is(utils.getFileId(blob1), fileId, "Correct file id");
let buffer1;
let buffer2;
for (let i = 0; i < bufferCache.length; i++) {
if (bufferCache[i].blob == blob2) {
buffer2 = bufferCache[i].buffer;
break;
}
}
if (!buffer2) {
let reader = new FileReader();
reader.readAsArrayBuffer(blob2);
reader.onload = function(event) {
buffer2 = event.target.result;
bufferCache.push({ blob: blob2, buffer: buffer2 });
if (buffer1) {
verifyBuffers(buffer1, buffer2);
if (blobReadHandler) {
blobReadHandler();
} else {
testGenerator.next();
}
}
};
}
let reader = new FileReader();
reader.readAsArrayBuffer(blob1);
reader.onload = function(event) {
buffer1 = event.target.result;
if (buffer2) {
verifyBuffers(buffer1, buffer2);
if (blobReadHandler) {
blobReadHandler();
} else {
testGenerator.next();
}
}
};
}
function verifyBlobArray(blobs1, blobs2, expectedFileIds) {
is(blobs1 instanceof Array, true, "Got an array object");
is(blobs1.length, blobs2.length, "Correct length");
if (!blobs1.length) {
return;
}
let verifiedCount = 0;
function blobReadHandler() {
if (++verifiedCount == blobs1.length) {
testGenerator.next();
} else {
verifyBlob(
blobs1[verifiedCount],
blobs2[verifiedCount],
expectedFileIds[verifiedCount],
blobReadHandler
);
}
}
verifyBlob(
blobs1[verifiedCount],
blobs2[verifiedCount],
expectedFileIds[verifiedCount],
blobReadHandler
);
}
function verifyMutableFile(mutableFile1, file2) {
ok(mutableFile1 instanceof IDBMutableFile, "Instance of IDBMutableFile");
is(mutableFile1.name, file2.name, "Correct name");
is(mutableFile1.type, file2.type, "Correct type");
continueToNextStep();
}
function verifyView(view1, view2) {
is(view1.byteLength, view2.byteLength, "Correct byteLength");
verifyBuffers(view1, view2);
continueToNextStep();
}
function verifyWasmModule(module1, module2) {
// We assume the given modules have no imports and export a single function
// named 'run'.
var instance1 = new WebAssembly.Instance(module1);
var instance2 = new WebAssembly.Instance(module2);
is(instance1.exports.run(), instance2.exports.run(), "same run() result");
continueToNextStep();
}
function grabFileUsageAndContinueHandler(request) {
testGenerator.next(request.result.fileUsage);
}
function getCurrentUsage(usageHandler) {
let qms = SpecialPowers.Services.qms;
let principal = SpecialPowers.wrap(document).nodePrincipal;
let cb = SpecialPowers.wrapCallback(usageHandler);
qms.getUsageForPrincipal(principal, cb);
}
function getFileId(file) {
return utils.getFileId(file);
}
function getFilePath(file) {
return utils.getFilePath(file);
}
function* assertEventuallyHasFileInfo(name, id) {
yield* assertEventuallyWithGC(
() => utils.getFileReferences(name, id),
`Expect existing DatabaseFileInfo for ${name}/${id}`
);
}
function* assertEventuallyHasNoFileInfo(name, id) {
yield* assertEventuallyWithGC(
() => !utils.getFileReferences(name, id),
`Expect no existing DatabaseFileInfo for ${name}/${id}`
);
}
function* assertEventuallyFileRefCount(name, id, expectedCount) {
yield* assertEventuallyWithGC(() => {
let count = {};
utils.getFileReferences(name, id, count);
return count.value == expectedCount;
}, `Expect ${expectedCount} existing references for ${name}/${id}`);
}
function getFileDBRefCount(name, id) {
let count = {};
utils.getFileReferences(name, id, {}, count);
return count.value;
}
function flushPendingFileDeletions() {
utils.flushPendingFileDeletions();
}
async function createReadWriteFileWithInitialContent(dbName, content) {
// BEGIN DUPLICATED BLOCK
// The functionality of this setup code is duplicated from test_filehandle_truncate.html
// (and maybe other test cases), but it has been modified to use async/await.
let request = indexedDB.open(dbName);
await expectingUpgrade(request);
let event = await expectingSuccess(request);
let db = event.target.result;
// We cannot use errorHandler because we shouldn't call finishTest() for aysnc tests
db.onerror = evt =>
ok(false, "indexedDB error, '" + evt.target.error.name + "'");
request = db.createMutableFile("test.bin");
event = await expectingSuccess(request);
let mutableFile = event.target.result;
mutableFile.onerror = evt =>
ok(false, "indexedDB error, '" + evt.target.error.name + "'");
let fileHandle = mutableFile.open("readwrite");
request = fileHandle.write(content);
event = await expectingSuccess(request);
// END DUPLICATED BLOCK
ok(fileHandle instanceof IDBFileHandle, "Instance of IDBFileHandle");
return fileHandle;
}
|