Skip to content

Commit 2e28285

Browse files
committed
Bug 1921583 - Part 5: Refactor compression tests to cover brotli r=smaug
Differential Revision: https://phabricator.services.mozilla.com/D269961
1 parent 67add2e commit 2e28285

5 files changed

Lines changed: 83 additions & 108 deletions

File tree

testing/web-platform/tests/compression/compression-including-empty-chunk.any.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// META: global=window,worker,shadowrealm
22
// META: script=third_party/pako/pako_inflate.min.js
3+
// META: script=resources/decompress.js
4+
// META: script=resources/formats.js
35
// META: timeout=long
46

57
'use strict';
@@ -42,22 +44,13 @@ const chunkLists = [
4244
];
4345
const expectedValue = new TextEncoder().encode('HelloHello');
4446

45-
for (const chunkList of chunkLists) {
46-
promise_test(async t => {
47-
const compressedData = await compressChunkList(chunkList, 'deflate');
48-
// decompress with pako, and check that we got the same result as our original string
49-
assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
50-
}, `the result of compressing [${chunkList}] with deflate should be 'HelloHello'`);
51-
52-
promise_test(async t => {
53-
const compressedData = await compressChunkList(chunkList, 'gzip');
54-
// decompress with pako, and check that we got the same result as our original string
55-
assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
56-
}, `the result of compressing [${chunkList}] with gzip should be 'HelloHello'`);
57-
58-
promise_test(async t => {
59-
const compressedData = await compressChunkList(chunkList, 'deflate-raw');
60-
// decompress with pako, and check that we got the same result as our original string
61-
assert_array_equals(expectedValue, pako.inflateRaw(compressedData), 'value should match');
62-
}, `the result of compressing [${chunkList}] with deflate-raw should be 'HelloHello'`);
47+
for (const format of formats) {
48+
for (const chunkList of chunkLists) {
49+
promise_test(async t => {
50+
const compressedData = await compressChunkList(chunkList, format);
51+
const decompressedData = await decompressDataOrPako(compressedData, format);
52+
// check that we got the same result as our original string
53+
assert_array_equals(expectedValue, decompressedData, 'value should match');
54+
}, `the result of compressing [${chunkList}] with ${format} should be 'HelloHello'`);
55+
}
6356
}
Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// META: global=window,worker,shadowrealm
22
// META: script=third_party/pako/pako_inflate.min.js
33
// META: script=resources/concatenate-stream.js
4+
// META: script=resources/decompress.js
5+
// META: script=resources/formats.js
46
// META: timeout=long
57

68
'use strict';
@@ -21,21 +23,11 @@ const fullData = new TextEncoder().encode(JSON.stringify(Array.from({ length: 10
2123
const data = fullData.subarray(0, 35_579);
2224
const expectedValue = data;
2325

24-
promise_test(async t => {
25-
const compressedData = await compressData(data, 'deflate');
26-
// decompress with pako, and check that we got the same result as our original string
27-
assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
28-
}, `deflate compression with large flush output`);
29-
30-
promise_test(async t => {
31-
const compressedData = await compressData(data, 'gzip');
32-
// decompress with pako, and check that we got the same result as our original string
33-
assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
34-
}, `gzip compression with large flush output`);
35-
36-
promise_test(async t => {
37-
const compressedData = await compressData(data, 'deflate-raw');
38-
// decompress with pako, and check that we got the same result as our original string
39-
assert_array_equals(expectedValue, pako.inflateRaw(compressedData), 'value should match');
40-
}, `deflate-raw compression with large flush output`);
41-
26+
for (const format of formats) {
27+
promise_test(async t => {
28+
const compressedData = await compressData(data, format);
29+
const decompressedData = await decompressDataOrPako(compressedData, format);
30+
// check that we got the same result as our original string
31+
assert_array_equals(decompressedData, expectedValue, 'value should match');
32+
}, `${format} compression with large flush output`);
33+
}

testing/web-platform/tests/compression/compression-multiple-chunks.any.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// META: global=window,worker,shadowrealm
22
// META: script=third_party/pako/pako_inflate.min.js
3+
// META: script=resources/decompress.js
4+
// META: script=resources/formats.js
35
// META: timeout=long
46

57
'use strict';
@@ -43,25 +45,14 @@ async function compressMultipleChunks(input, numberOfChunks, format) {
4345

4446
const hello = 'Hello';
4547

46-
for (let numberOfChunks = 2; numberOfChunks <= 16; ++numberOfChunks) {
47-
promise_test(async t => {
48-
const compressedData = await compressMultipleChunks(hello, numberOfChunks, 'deflate');
49-
const expectedValue = makeExpectedChunk(hello, numberOfChunks);
50-
// decompress with pako, and check that we got the same result as our original string
51-
assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
52-
}, `compressing ${numberOfChunks} chunks with deflate should work`);
53-
54-
promise_test(async t => {
55-
const compressedData = await compressMultipleChunks(hello, numberOfChunks, 'gzip');
56-
const expectedValue = makeExpectedChunk(hello, numberOfChunks);
57-
// decompress with pako, and check that we got the same result as our original string
58-
assert_array_equals(expectedValue, pako.inflate(compressedData), 'value should match');
59-
}, `compressing ${numberOfChunks} chunks with gzip should work`);
60-
61-
promise_test(async t => {
62-
const compressedData = await compressMultipleChunks(hello, numberOfChunks, 'deflate-raw');
63-
const expectedValue = makeExpectedChunk(hello, numberOfChunks);
64-
// decompress with pako, and check that we got the same result as our original string
65-
assert_array_equals(expectedValue, pako.inflateRaw(compressedData), 'value should match');
66-
}, `compressing ${numberOfChunks} chunks with deflate-raw should work`);
48+
for (const format of formats) {
49+
for (let numberOfChunks = 2; numberOfChunks <= 16; ++numberOfChunks) {
50+
promise_test(async t => {
51+
const compressedData = await compressMultipleChunks(hello, numberOfChunks, format);
52+
const decompressedData = await decompressDataOrPako(compressedData, format);
53+
const expectedValue = makeExpectedChunk(hello, numberOfChunks);
54+
// check that we got the same result as our original string
55+
assert_array_equals(decompressedData, expectedValue, 'value should match');
56+
}, `compressing ${numberOfChunks} chunks with ${format} should work`);
57+
}
6758
}
Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// META: global=window,worker,shadowrealm
22
// META: script=third_party/pako/pako_inflate.min.js
3+
// META: script=resources/decompress.js
4+
// META: script=resources/formats.js
35
// META: timeout=long
46

57
'use strict';
68

79
const SMALL_FILE = "/media/foo.vtt";
810
const LARGE_FILE = "/media/test-av-384k-44100Hz-1ch-320x240-30fps-10kfr.webm";
911

12+
let dataPromiseList = [
13+
["empty data", Promise.resolve(new Uint8Array(0))],
14+
["small amount data", fetch(SMALL_FILE).then(response => response.bytes())],
15+
["large amount data", fetch(LARGE_FILE).then(response => response.bytes())],
16+
];
17+
1018
async function compressArrayBuffer(input, format) {
1119
const cs = new CompressionStream(format);
1220
const writer = cs.writable.getWriter();
@@ -38,54 +46,14 @@ test(() => {
3846
}, "non supported format should throw");
3947
}, "CompressionStream constructor should throw on invalid format");
4048

41-
promise_test(async () => {
42-
const buffer = new ArrayBuffer(0);
43-
const bufferView = new Uint8Array(buffer);
44-
const compressedData = await compressArrayBuffer(bufferView, "deflate");
45-
// decompress with pako, and check that we got the same result as our original string
46-
assert_array_equals(bufferView, pako.inflate(compressedData));
47-
}, "deflated empty data should be reinflated back to its origin");
48-
49-
promise_test(async () => {
50-
const response = await fetch(SMALL_FILE)
51-
const buffer = await response.arrayBuffer();
52-
const bufferView = new Uint8Array(buffer);
53-
const compressedData = await compressArrayBuffer(bufferView, "deflate");
54-
// decompress with pako, and check that we got the same result as our original string
55-
assert_array_equals(bufferView, pako.inflate(compressedData));
56-
}, "deflated small amount data should be reinflated back to its origin");
57-
58-
promise_test(async () => {
59-
const response = await fetch(LARGE_FILE)
60-
const buffer = await response.arrayBuffer();
61-
const bufferView = new Uint8Array(buffer);
62-
const compressedData = await compressArrayBuffer(bufferView, "deflate");
63-
// decompress with pako, and check that we got the same result as our original string
64-
assert_array_equals(bufferView, pako.inflate(compressedData));
65-
}, "deflated large amount data should be reinflated back to its origin");
66-
67-
promise_test(async () => {
68-
const buffer = new ArrayBuffer(0);
69-
const bufferView = new Uint8Array(buffer);
70-
const compressedData = await compressArrayBuffer(bufferView, "gzip");
71-
// decompress with pako, and check that we got the same result as our original string
72-
assert_array_equals(bufferView, pako.inflate(compressedData));
73-
}, "gzipped empty data should be reinflated back to its origin");
74-
75-
promise_test(async () => {
76-
const response = await fetch(SMALL_FILE)
77-
const buffer = await response.arrayBuffer();
78-
const bufferView = new Uint8Array(buffer);
79-
const compressedData = await compressArrayBuffer(bufferView, "gzip");
80-
// decompress with pako, and check that we got the same result as our original string
81-
assert_array_equals(bufferView, pako.inflate(compressedData));
82-
}, "gzipped small amount data should be reinflated back to its origin");
83-
84-
promise_test(async () => {
85-
const response = await fetch(LARGE_FILE)
86-
const buffer = await response.arrayBuffer();
87-
const bufferView = new Uint8Array(buffer);
88-
const compressedData = await compressArrayBuffer(bufferView, "gzip");
89-
// decompress with pako, and check that we got the same result as our original string
90-
assert_array_equals(bufferView, pako.inflate(compressedData));
91-
}, "gzipped large amount data should be reinflated back to its origin");
49+
for (const format of formats) {
50+
for (const [label, dataPromise] of dataPromiseList) {
51+
promise_test(async () => {
52+
const bufferView = await dataPromise;
53+
const compressedData = await compressArrayBuffer(bufferView, format);
54+
const decompressedData = await decompressDataOrPako(compressedData, format);
55+
// check that we got the same result as our original string
56+
assert_array_equals(decompressedData, bufferView, 'value should match');
57+
}, `${format} ${label} should be reinflated back to its origin`);
58+
}
59+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {Uint8Array} chunk
3+
* @param {string} format
4+
*/
5+
async function decompressData(chunk, format) {
6+
const ds = new DecompressionStream(format);
7+
const writer = ds.writable.getWriter();
8+
writer.write(chunk);
9+
writer.close();
10+
const decompressedChunkList = await Array.fromAsync(ds.readable);
11+
const mergedBlob = new Blob(decompressedChunkList);
12+
return await mergedBlob.bytes();
13+
}
14+
15+
/**
16+
* @param {Uint8Array} chunk
17+
* @param {string} format
18+
*/
19+
async function decompressDataOrPako(chunk, format) {
20+
// Keep using pako for zlib to preserve existing test behavior
21+
if (["deflate", "gzip"].includes(format)) {
22+
return pako.inflate(chunk);
23+
}
24+
if (format === "deflate-raw") {
25+
return pako.inflateRaw(chunk);
26+
}
27+
28+
// Use DecompressionStream for any newer formats, assuming implementations
29+
// always implement decompression if they implement compression.
30+
return decompressData(chunk, format);
31+
}

0 commit comments

Comments
 (0)