Skip to content

Commit

Permalink
test: update wpt tests for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
F3n67u committed Jun 24, 2022
1 parent 3507b3f commit 514b2d8
Show file tree
Hide file tree
Showing 27 changed files with 3,090 additions and 394 deletions.
3 changes: 2 additions & 1 deletion test/common/wpt/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const resource = new ResourceLoader(workerData.wptPath);

global.self = global;
global.GLOBAL = {
isWindow() { return false; }
isWindow() { return false; },
isShadowRealm() { return false; }
};
global.require = require;

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Last update:
- console: https://github.com/web-platform-tests/wpt/tree/3b1f72e99a/console
- dom/abort: https://github.com/web-platform-tests/wpt/tree/c49cafb491/dom/abort
- dom/events: https://github.com/web-platform-tests/wpt/tree/f8821adb28/dom/events
- encoding: https://github.com/web-platform-tests/wpt/tree/35f70910d3/encoding
- encoding: https://github.com/web-platform-tests/wpt/tree/0e5b126cd0/encoding
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
- hr-time: https://github.com/web-platform-tests/wpt/tree/9910784394/hr-time
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
Expand All @@ -23,7 +23,7 @@ Last update:
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/5873f2d8f1/html/webappapis/timers
- interfaces: https://github.com/web-platform-tests/wpt/tree/fc086c82d5/interfaces
- performance-timeline: https://github.com/web-platform-tests/wpt/tree/17ebc3aea0/performance-timeline
- resources: https://github.com/web-platform-tests/wpt/tree/fbee645164/resources
- resources: https://github.com/web-platform-tests/wpt/tree/1df9d51265/resources
- streams: https://github.com/web-platform-tests/wpt/tree/8f60d94439/streams
- url: https://github.com/web-platform-tests/wpt/tree/77d54aa9e0/url
- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing
Expand Down
25 changes: 14 additions & 11 deletions test/fixtures/wpt/encoding/encodeInto.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,22 @@
});
});

[DataView,
Int8Array,
Int16Array,
Int32Array,
Uint16Array,
Uint32Array,
Uint8ClampedArray,
Float32Array,
Float64Array].forEach(view => {
["DataView",
"Int8Array",
"Int16Array",
"Int32Array",
"Uint16Array",
"Uint32Array",
"Uint8ClampedArray",
"BigInt64Array",
"BigUint64Array",
"Float32Array",
"Float64Array"].forEach(type => {
["ArrayBuffer", "SharedArrayBuffer"].forEach((arrayBufferOrSharedArrayBuffer) => {
test(() => {
assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", new view(createBuffer(arrayBufferOrSharedArrayBuffer, 0))));
}, "Invalid encodeInto() destination: " + view.name + ", backed by: " + arrayBufferOrSharedArrayBuffer);
const viewInstance = new self[type](createBuffer(arrayBufferOrSharedArrayBuffer, 0));
assert_throws_js(TypeError, () => new TextEncoder().encodeInto("", viewInstance));
}, "Invalid encodeInto() destination: " + type + ", backed by: " + arrayBufferOrSharedArrayBuffer);
});
});

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/wpt/encoding/idlharness-shadowrealm.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["encoding"], ["streams"]);
14 changes: 14 additions & 0 deletions test/fixtures/wpt/encoding/sharedarraybuffer.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

test(() => {
const decoder = new TextDecoder('utf-8');
const buffer = new SharedArrayBuffer(4);
assert_throws_js(TypeError, () => {
decoder.decode(new Uint8Array(buffer));
}, 'constructing TextDecoder with SharedArrayBuffer view should throw');
}, 'decoding SharedArrayBuffer');

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cross-Origin-Opener-Policy:same-origin
Cross-Origin-Embedder-Policy:require-corp
107 changes: 107 additions & 0 deletions test/fixtures/wpt/encoding/single-byte-decoder.window.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion test/fixtures/wpt/encoding/streams/decode-utf8.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@
const array = await readableStreamToArray(output);
assert_array_equals(array, [expectedOutputString],
'the output should be in one chunk');
}, 'a trailing empty chunk should be ignored- ' + arrayBufferOrSharedArrayBuffer);
}, 'a trailing empty chunk should be ignored - ' + arrayBufferOrSharedArrayBuffer);

promise_test(async () => {
const chunk = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, 3));
chunk.set([0xF0, 0x9F, 0x92]);
const input = readableStreamFromArray([chunk]);
const output = input.pipeThrough(new TextDecoderStream());
const array = await readableStreamToArray(output);
assert_array_equals(array, ['\uFFFD']);
}, 'UTF-8 EOF handling - ' + arrayBufferOrSharedArrayBuffer);
});

promise_test(async () => {
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/wpt/encoding/textdecoder-eof.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
test(() => {
// Truncated sequences
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0])), "\uFFFD");
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0, 0x9F])), "\uFFFD");
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0, 0x9F, 0x92])), "\uFFFD");

// Errors near end-of-queue
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0, 0x9F, 0x41])), "\uFFFDA");
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0, 0x41, 0x42])), "\uFFFDAB");
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0, 0x41, 0xF0])), "\uFFFDA\uFFFD");
assert_equals(new TextDecoder().decode(new Uint8Array([0xF0, 0x8F, 0x92])), "\uFFFD\uFFFD\uFFFD");
}, "TextDecoder end-of-queue handling");

test(() => {
const decoder = new TextDecoder();
decoder.decode(new Uint8Array([0xF0]), { stream: true });
assert_equals(decoder.decode(), "\uFFFD");

decoder.decode(new Uint8Array([0xF0]), { stream: true });
decoder.decode(new Uint8Array([0x9F]), { stream: true });
assert_equals(decoder.decode(), "\uFFFD");

decoder.decode(new Uint8Array([0xF0, 0x9F]), { stream: true });
assert_equals(decoder.decode(new Uint8Array([0x92])), "\uFFFD");

assert_equals(decoder.decode(new Uint8Array([0xF0, 0x9F]), { stream: true }), "");
assert_equals(decoder.decode(new Uint8Array([0x41]), { stream: true }), "\uFFFDA");
assert_equals(decoder.decode(), "");

assert_equals(decoder.decode(new Uint8Array([0xF0, 0x41, 0x42]), { stream: true }), "\uFFFDAB");
assert_equals(decoder.decode(), "");

assert_equals(decoder.decode(new Uint8Array([0xF0, 0x41, 0xF0]), { stream: true }), "\uFFFDA");
assert_equals(decoder.decode(), "\uFFFD");

assert_equals(decoder.decode(new Uint8Array([0xF0]), { stream: true }), "");
assert_equals(decoder.decode(new Uint8Array([0x8F]), { stream: true }), "\uFFFD\uFFFD");
assert_equals(decoder.decode(new Uint8Array([0x92]), { stream: true }), "\uFFFD");
assert_equals(decoder.decode(), "");
}, "TextDecoder end-of-queue handling using stream: true");
53 changes: 50 additions & 3 deletions test/fixtures/wpt/encoding/textdecoder-streaming.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,62 @@ var octets = {
var decoder = new TextDecoder(encoding);
for (var i = 0; i < encoded.length; i += len) {
var sub = [];
for (var j = i; j < encoded.length && j < i + len; ++j)
for (var j = i; j < encoded.length && j < i + len; ++j) {
sub.push(encoded[j]);
var uintArray = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, sub.length));
uintArray.set(sub);
}
var uintArray = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, sub.length));
uintArray.set(sub);
out += decoder.decode(uintArray, {stream: true});
}
out += decoder.decode();
assert_equals(out, string);
}, 'Streaming decode: ' + encoding + ', ' + len + ' byte window (' + arrayBufferOrSharedArrayBuffer + ')');
}
});

test(() => {
function bytes(byteArray) {
const view = new Uint8Array(createBuffer(arrayBufferOrSharedArrayBuffer, byteArray.length));
view.set(byteArray);
return view;
}

const decoder = new TextDecoder();

assert_equals(decoder.decode(bytes([0xC1]), {stream: true}), "\uFFFD");
assert_equals(decoder.decode(), "");

assert_equals(decoder.decode(bytes([0xF5]), {stream: true}), "\uFFFD");
assert_equals(decoder.decode(), "");

assert_equals(decoder.decode(bytes([0xE0, 0x41]), {stream: true}), "\uFFFDA");
assert_equals(decoder.decode(bytes([0x42])), "B");

assert_equals(decoder.decode(bytes([0xE0, 0x80]), {stream: true}), "\uFFFD\uFFFD");
assert_equals(decoder.decode(bytes([0x80])), "\uFFFD");

assert_equals(decoder.decode(bytes([0xED, 0xA0]), {stream: true}), "\uFFFD\uFFFD");
assert_equals(decoder.decode(bytes([0x80])), "\uFFFD");

assert_equals(decoder.decode(bytes([0xF0, 0x41]), {stream: true}), "\uFFFDA");
assert_equals(decoder.decode(bytes([0x42]), {stream: true}), "B");
assert_equals(decoder.decode(bytes([0x43])), "C");

assert_equals(decoder.decode(bytes([0xF0, 0x80]), {stream: true}), "\uFFFD\uFFFD");
assert_equals(decoder.decode(bytes([0x80]), {stream: true}), "\uFFFD");
assert_equals(decoder.decode(bytes([0x80])), "\uFFFD");

assert_equals(decoder.decode(bytes([0xF4, 0xA0]), {stream: true}), "\uFFFD\uFFFD");
assert_equals(decoder.decode(bytes([0x80]), {stream: true}), "\uFFFD");
assert_equals(decoder.decode(bytes([0x80])), "\uFFFD");

assert_equals(decoder.decode(bytes([0xF0, 0x90, 0x41]), {stream: true}), "\uFFFDA");
assert_equals(decoder.decode(bytes([0x42])), "B");

// 4-byte UTF-8 sequences always correspond to non-BMP characters. Here
// we make sure that, although the first 3 bytes are enough to emit the
// lead surrogate, it only gets emitted when the fourth byte is read.
assert_equals(decoder.decode(bytes([0xF0, 0x9F, 0x92]), {stream: true}), "");
assert_equals(decoder.decode(bytes([0xA9])), "\u{1F4A9}");
}, `Streaming decode: UTF-8 chunk tests (${arrayBufferOrSharedArrayBuffer})`);
})
34 changes: 34 additions & 0 deletions test/fixtures/wpt/resources/accesskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Function that sends an accesskey using the proper key combination depending on the browser and OS.
*
* This needs that the test imports the following scripts:
* <script src="/resources/testdriver.js"></script>
* <script src="/resources/testdriver-actions.js"></script>
* <script src="/resources/testdriver-vendor.js"></script>
*/
function pressAccessKey(accessKey){
let controlKey = '\uE009'; // left Control key
let altKey = '\uE00A'; // left Alt key
let optionKey = altKey; // left Option key
let shiftKey = '\uE008'; // left Shift key
// There are differences in using accesskey across browsers and OS's.
// See: // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/accesskey
let isMacOSX = navigator.userAgent.indexOf("Mac") != -1;
let osAccessKey = isMacOSX ? [controlKey, optionKey] : [shiftKey, altKey];
let actions = new test_driver.Actions();
// Press keys.
for (let key of osAccessKey) {
actions = actions.keyDown(key);
}
actions = actions
.keyDown(accessKey)
.addTick()
.keyUp(accessKey);
osAccessKey.reverse();
for (let key of osAccessKey) {
actions = actions.keyUp(key);
}
return actions.send();
}


16 changes: 16 additions & 0 deletions test/fixtures/wpt/resources/blank.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Blank Page</title>
<script>
window.onload = function(event) {
// This is needed to ensure the onload event fires when this page is
// opened as a popup.
// See https://github.com/web-platform-tests/wpt/pull/18157
};
</script>
</head>
<body>
</body>
</html>
Loading

0 comments on commit 514b2d8

Please sign in to comment.