Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / webgl / script-tests / array-message-passing.js
blob09e77a968450ab847c1a049ee04bf4fcd7d0185d
1 window.jsTestIsAsync = true;
3 description('Test passing ArrayBuffers and ArrayBufferViews in messages.');
4 window.testsComplete = 0;
6 function classCompare(testName, got, sent) {
7 var classString = Object.prototype.toString;
8 var gotClass = classString.call(got);
9 var sentClass = classString.call(sent);
10 if (gotClass !== sentClass) {
11 testFailed(testName + ": class " + sentClass + " became " + gotClass);
12 return false;
13 } else {
14 testPassed(testName + ": classes are " + sentClass);
15 return true;
19 function bufferCompare(testName, got, sent) {
20 if (!classCompare(testName, got, sent)) {
21 return false;
23 if (got.byteLength !== sent.byteLength) {
24 testFailed(testName + ": expected byteLength " + sent.byteLength + " bytes, got " + got.byteLength);
25 return false;
26 } else {
27 testPassed(testName + ": buffer lengths are " + sent.byteLength);
29 var gotReader = new Uint8Array(got);
30 var sentReader = new Uint8Array(sent);
31 for (var i = 0; i < sent.byteLength; ++i) {
32 if (gotReader[i] !== sentReader[i]) {
33 testFailed(testName + ": buffers differ starting at byte " + i);
34 return false;
37 testPassed(testName + ": buffers have the same contents");
38 return true;
41 function viewCompare(testName, got, sent) {
42 if (!classCompare(testName, got, sent)) {
43 return false;
45 if (!bufferCompare(testName, got.buffer, sent.buffer)) {
46 return false;
48 if (got.byteOffset !== sent.byteOffset) {
49 testFailed(testName + ": offset " + sent.byteOffset + " became " + got.byteOffset);
50 return false;
51 } else {
52 testPassed(testName + ": offset is " + sent.byteOffset);
54 if (got.byteLength !== sent.byteLength) {
55 testFailed(testName + ": length " + sent.byteLength + " became " + got.byteLength);
56 return false;
57 } else {
58 testPassed(testName + ": length is " + sent.byteLength);
60 return true;
63 function typedArrayCompare(testName, got, sent) {
64 if (!viewCompare(testName, got, sent)) {
65 return false;
67 if (got.BYTES_PER_ELEMENT !== sent.BYTES_PER_ELEMENT) {
68 // Sanity checking.
69 testFailed(testName + ": expected BYTES_PER_ELEMENT " + sent.BYTES_PER_ELEMENT + ", saw " + got.BYTES_PER_ELEMENT);
70 return false;
72 return true;
75 function dataViewCompare(testName, got, sent) {
76 return viewCompare(testName, got, sent);
79 function dataViewCompare2(testName, got, sent) {
80 for (var i = 0; i < 2; ++i) {
81 if (!dataViewCompare(testName, got[i], sent[i])) {
82 return false;
85 if (got[0].buffer !== got[1].buffer) {
86 testFailed(testName + ": expected the same ArrayBuffer for both views");
87 return false;
89 return true;
91 function dataViewCompare3(testName, got, sent) {
92 for (var i = 0; i < 3; i += 2) {
93 if (!dataViewCompare(testName, got[i], sent[i])) {
94 return false;
97 if (got[1].x !== sent[1].x || got[1].y !== sent[1].y) {
98 testFailed(testName + ": {x:1, y:1} was not transferred properly");
99 return false;
101 if (got[0].buffer !== got[2].buffer) {
102 testFailed(testName + ": expected the same ArrayBuffer for both views");
103 return false;
105 return false;
109 function createBuffer(length) {
110 var buffer = new ArrayBuffer(length);
111 var view = new Uint8Array(buffer);
112 for (var i = 0; i < length; ++i) {
113 view[i] = i + 1;
115 return buffer;
118 function createTypedArray(typedArrayType, length) {
119 var view = new typedArrayType(length);
120 for (var i = 0; i < length; ++i) {
121 view[i] = i + 1;
123 return view;
126 function createTypedArrayOverBuffer(typedArrayType, typedArrayElementSize, length, subStart, subLength) {
127 var buffer = new ArrayBuffer(length * typedArrayElementSize);
128 if (subStart === undefined) {
129 subStart = 0;
130 subLength = length;
132 return new typedArrayType(buffer, subStart * typedArrayElementSize, subLength);
135 var basicBufferTypes = [
136 ["Int32", Int32Array, 4],
137 ["Uint32", Uint32Array, 4],
138 ["Int8", Int8Array, 1],
139 ["Uint8", Uint8Array, 1],
140 ["Uint8Clamped", Uint8ClampedArray, 1],
141 ["Int16", Int16Array, 2],
142 ["Uint16", Uint16Array, 2],
143 ["Float32", Float32Array, 4],
144 ["Float64", Float64Array, 8]
147 var arrayBuffer1 = createBuffer(1);
149 var testList = [
150 ['ArrayBuffer0', new ArrayBuffer(0), bufferCompare],
151 ['ArrayBuffer1', createBuffer(1), bufferCompare],
152 ['ArrayBuffer128', createBuffer(128), bufferCompare],
153 ['DataView0', new DataView(new ArrayBuffer(0)), dataViewCompare],
154 ['DataView1', new DataView(createBuffer(1)), dataViewCompare],
155 ['DataView1-dup', [new DataView(arrayBuffer1), new DataView(arrayBuffer1)], dataViewCompare2],
156 ['DataView1-dup2', [new DataView(arrayBuffer1), {x:1, y:1}, new DataView(arrayBuffer1)], dataViewCompare3],
157 ['DataView128', new DataView(createBuffer(128)), dataViewCompare],
158 ['DataView1_offset_at_end', new DataView(createBuffer(1), 1, 0), dataViewCompare],
159 ['DataView128_offset_at_end', new DataView(createBuffer(128), 128, 0), dataViewCompare],
160 ['DataView128_offset_slice_length_0', new DataView(createBuffer(128), 64, 0), dataViewCompare],
161 ['DataView128_offset_slice_length_1', new DataView(createBuffer(128), 64, 1), dataViewCompare],
162 ['DataView128_offset_slice_length_16', new DataView(createBuffer(128), 64, 16), dataViewCompare],
163 ['DataView128_offset_slice_unaligned', new DataView(createBuffer(128), 63, 15), dataViewCompare]
166 testList = testList.concat(basicBufferTypes.map(function(t)
167 {return [t[0] + "_0", createTypedArray(t[1], 0), typedArrayCompare];}));
168 testList = testList.concat(basicBufferTypes.map(function(t)
169 {return [t[0] + "_1", createTypedArray(t[1], 1), typedArrayCompare];}));
170 testList = testList.concat(basicBufferTypes.map(function(t)
171 {return [t[0] + "_128", createTypedArray(t[1], 128), typedArrayCompare];}));
172 testList = testList.concat(basicBufferTypes.map(function(t)
173 {return [t[0] + "_0_buffer", createTypedArrayOverBuffer(t[1], t[2], 0), typedArrayCompare];}));
174 testList = testList.concat(basicBufferTypes.map(function(t)
175 {return [t[0] + "_1_buffer", createTypedArrayOverBuffer(t[1], t[2], 1), typedArrayCompare];}));
176 testList = testList.concat(basicBufferTypes.map(function(t)
177 {return [t[0] + "_128_buffer", createTypedArrayOverBuffer(t[1], t[2], 128), typedArrayCompare];}));
178 testList = testList.concat(basicBufferTypes.map(function(t)
179 {return [t[0] + "_1_buffer_offset_at_end", createTypedArrayOverBuffer(t[1], t[2], 1, 1, 0), typedArrayCompare];}));
180 testList = testList.concat(basicBufferTypes.map(function(t)
181 {return [t[0] + "_128_buffer_offset_at_end", createTypedArrayOverBuffer(t[1], t[2], 128, 128, 0), typedArrayCompare];}));
182 testList = testList.concat(basicBufferTypes.map(function(t)
183 {return [t[0] + "_128_buffer_offset_slice_length_0", createTypedArrayOverBuffer(t[1], t[2], 128, 64, 0), typedArrayCompare];}));
184 testList = testList.concat(basicBufferTypes.map(function(t)
185 {return [t[0] + "_128_buffer_offset_slice_length_1", createTypedArrayOverBuffer(t[1], t[2], 128, 64, 1), typedArrayCompare];}));
186 testList = testList.concat(basicBufferTypes.map(function(t)
187 {return [t[0] + "_128_buffer_offset_slice_length_16", createTypedArrayOverBuffer(t[1], t[2], 128, 64, 16), typedArrayCompare];}));
189 function doneTest() {
190 if (++window.testsComplete == testList.length) {
191 finishJSTest();
195 function windowHandleMessage(e) {
196 var currentTest = testList[e.data.testNum];
197 var expectedResult = currentTest[1];
198 try {
199 currentTest[2](currentTest[0], e.data.testData, expectedResult);
200 } catch(e) {
201 testFailed(currentTest[0] + ": unexpected exception " + e);
203 doneTest();
205 window.addEventListener('message', windowHandleMessage);
207 for (var t = 0; t < testList.length; ++t) {
208 var currentTest = testList[t];
209 var message = {testNum: t, testData: currentTest[1]};
210 try {
211 window.postMessage(message, '*');
212 } catch(e) {
213 testFailed(currentTest[0], ": unexpected postMessage exception " + e);
214 doneTest();