Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / workers / worker-sharedarraybuffer-transfer.html
blob686b826cf8128031634dba0e3fa0cae8745ac869
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <p>Test sharing SharedArrayBuffers between dedicated workers.</p>
5 <div id="result"></div>
6 <script type="text/javascript">
7 function log(message)
9 document.getElementById("result").innerHTML += message + "</br>";
12 if (window.testRunner) {
13 testRunner.dumpAsText();
14 testRunner.waitUntilDone();
17 var testCases = [
18 {name: "SharedArrayBuffer"},
19 {name: "Int8Array"},
20 {name: "Uint8Array"},
21 {name: "Uint8ClampedArray"},
22 {name: "Int16Array"},
23 {name: "Uint16Array"},
24 {name: "Int32Array"},
25 {name: "Uint32Array"},
26 {name: "Float32Array"},
27 {name: "Float64Array"}
29 var currentTestCase = 0;
31 function runTestCase(testCase) {
32 var length = 8;
33 var name = testCase.name;
34 var type = window[name];
35 var sab;
36 var ta;
38 log("Running " + name + " test case");
40 if (testCase.name == 'SharedArrayBuffer') {
41 sab = new SharedArrayBuffer(length);
42 ta = new Uint8Array(sab);
43 } else {
44 sab = new SharedArrayBuffer(length * type.BYTES_PER_ELEMENT);
45 ta = new type(sab);
48 var i;
49 for (i = 0; i < length; ++i)
50 ta[i] = i;
52 // Without Atomics, we can't safely test modifying the contents of the
53 // SharedArrayBuffer. All we can test for now is that the SharedArrayBuffer
54 // is not neutered when transferred to a Worker.
55 if (testCase.name == 'SharedArrayBuffer')
56 worker.postMessage({name: name, data: sab, length: length}, [sab]);
57 else
58 worker.postMessage({name: name, data: ta, length: length}, [sab]);
60 if (sab.length === 0)
61 log("FAIL: SharedArrayBuffer was neutered during transfer.");
62 else
63 log("PASS: SharedArrayBuffer not neutered during transfer.");
65 // Ensure the data wasn't changed.
66 for (i = 0; i < length; ++i) {
67 if (ta[i] != i) {
68 log("FAIL: Original data changed during transfer. Expected " + i +
69 " got " + ta[i]);
70 break;
73 if (i == length)
74 log("PASS: Original data not changed during transfer.");
77 if (window.internals && internals.runtimeFlags.sharedArrayBufferEnabled && window.SharedArrayBuffer) {
78 var worker = new Worker('resources/worker-sharedarraybuffer-transfer.js');
80 runTestCase(testCases[currentTestCase]);
82 worker.onmessage = function(e) {
83 if (e.data == 'DONE') {
84 // The current test case is finished.
85 if (++currentTestCase == testCases.length) {
86 log("DONE");
87 testRunner.notifyDone();
88 } else {
89 runTestCase(testCases[currentTestCase]);
91 } else {
92 // The worker sent a pass/fail message.
93 log(e.data);
97 } else {
98 log("SharedArrayBuffers are not enabled -- skipping test.");
99 testRunner.notifyDone();
102 </script>
103 </body>
104 </html>