blob: 2c5f8d95d1bc6c803ed6545c023ce4fc25f17349 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright 2023 The Cobalt Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
Tests postMessage() for workers.
-->
<html>
<head>
<title>Cobalt Worker postMessage() Test</title>
<script src='black_box_js_test_utils.js'></script>
</head>
<body>
<script>
const timeoutId = window.setTimeout(notReached, 10000);
window.sharedArrayBufferFromWorker = null;
window.sharedArrayBufferFromWorkerView = null;
window.sharedArrayBufferFromWindow = new SharedArrayBuffer(4);
window.sharedArrayBufferFromWindowView = new Uint8Array(window.sharedArrayBufferFromWindow);
const worker = new Worker('worker_post_message_test.js');
worker.addEventListener('message', event => {
if (event.data === 'ready') {
const arrayBuffer = new ArrayBuffer(3);
const arrayBufferView = new Uint8Array(arrayBuffer);
arrayBufferView[0] = 4;
arrayBufferView[1] = 5;
arrayBufferView[2] = 6;
worker.postMessage({
type: 'check-data',
data: {
number: 1,
array: [2, 3],
string: 'abc',
arrayBuffer,
sharedArrayBuffer: sharedArrayBufferFromWindow,
},
});
return;
}
if (event.data.type === 'check-data') {
const data = event.data.data;
assertEqual(1, data.number);
assertEqual(2, data.array.length);
assertEqual(2, data.array[0]);
assertEqual(3, data.array[1]);
assertEqual('abc', data.string);
assertEqual(3, data.arrayBuffer.byteLength);
const arrayBufferView = new Uint8Array(data.arrayBuffer);
assertEqual(4, arrayBufferView[0]);
assertEqual(5, arrayBufferView[1]);
assertEqual(6, arrayBufferView[2]);
assertEqual(5, data.sharedArrayBuffer.byteLength);
window.sharedArrayBufferFromWorker = data.sharedArrayBuffer;
window.sharedArrayBufferFromWorkerView = new Uint8Array(window.sharedArrayBufferFromWorker);
worker.postMessage({
type: 'update-shared-array-buffers',
index: 1,
value: 42,
});
return;
}
if (event.data.type === 'update-shared-array-buffers-done') {
assertEqual(42, window.sharedArrayBufferFromWindowView[1]);
assertEqual(42, window.sharedArrayBufferFromWorkerView[1]);
window.sharedArrayBufferFromWindowView[0] = 47;
window.sharedArrayBufferFromWorkerView[0] = 47;
worker.postMessage({
type: 'check-shared-array-buffers',
index: 0,
value: 47,
})
return;
}
if (event.data.type === 'check-shared-array-buffers-done') {
clearTimeout(timeoutId);
onEndTest();
return;
}
console.log(JSON.stringify(event.data));
notReached();
});
setupFinished();
</script>
</body>
</html>