1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.arrays;
16
17 import org.htmlunit.WebDriverTestCase;
18 import org.htmlunit.junit.annotation.Alerts;
19 import org.junit.jupiter.api.Test;
20
21
22
23
24
25
26
27
28 public class ArrayBufferTest extends WebDriverTestCase {
29
30
31
32
33 @Test
34 @Alerts("0")
35 public void ctorLengthZero() throws Exception {
36 final String html = DOCTYPE_HTML
37 + "<html><head>\n"
38 + "<script>\n"
39 + LOG_TITLE_FUNCTION
40 + "function test() {\n"
41 + " var buff = new ArrayBuffer(0);\n"
42 + " log(buff.byteLength);\n"
43 + "}\n"
44 + "</script></head><body onload='test()'>\n"
45 + "</body></html>";
46
47 loadPageVerifyTitle2(html);
48 }
49
50
51
52
53 @Test
54 @Alerts("RangeError")
55 public void ctorLengthNegative() throws Exception {
56 final String html = DOCTYPE_HTML
57 + "<html><head>\n"
58 + "<script>\n"
59 + LOG_TITLE_FUNCTION
60 + "function test() {\n"
61 + " try {\n"
62 + " var buff = new ArrayBuffer(-1);\n"
63 + " log(buff.byteLength);\n"
64 + " } catch(e) { logEx(e); }"
65 + "}\n"
66 + "</script></head><body onload='test()'>\n"
67 + "</body></html>";
68
69 loadPageVerifyTitle2(html);
70 }
71
72
73
74
75 @Test
76 @Alerts("5")
77 public void byteLength() throws Exception {
78 final String html = DOCTYPE_HTML
79 + "<html><head>\n"
80 + "<script>\n"
81 + LOG_TITLE_FUNCTION
82 + "function test() {\n"
83 + " var buff = new ArrayBuffer(5);\n"
84 + " log(buff.byteLength);\n"
85 + "}\n"
86 + "</script></head><body onload='test()'>\n"
87 + "</body></html>";
88
89 loadPageVerifyTitle2(html);
90 }
91
92
93
94
95 @Test
96 @Alerts({"1234", "1234", "6789", "1234"})
97 public void slice() throws Exception {
98 final String html = DOCTYPE_HTML
99 + "<html><head>\n"
100 + "<script>\n"
101 + LOG_TITLE_FUNCTION
102 + "function test() {\n"
103 + " var buffer = new ArrayBuffer(12);\n"
104 + " var x = new Int32Array(buffer);\n"
105 + " x[1] = 1234;\n"
106 + " var slice = buffer.slice(4);\n"
107 + " var y = new Int32Array(slice);\n"
108 + " log(x[1]);\n"
109 + " log(y[0]);\n"
110 + " x[1] = 6789;\n"
111 + " log(x[1]);\n"
112 + " log(y[0]);\n"
113 + "}\n"
114 + "</script></head><body onload='test()'>\n"
115 + "</body></html>";
116
117 loadPageVerifyTitle2(html);
118 }
119
120
121
122
123 @Test
124 @Alerts({"2", "1234", "0"})
125 public void sliceInvalidStartIndexNumberString() throws Exception {
126 sliceInvalidIndex("'4'");
127 }
128
129
130
131
132 @Test
133 @Alerts("RangeError")
134 public void sliceInvalidStartIndexDouble() throws Exception {
135 sliceInvalidIndex("2.14");
136 }
137
138
139
140
141 @Test
142 @Alerts({"3", "0", "1234", "0"})
143 public void sliceInvalidStartIndexString() throws Exception {
144 sliceInvalidIndex("'four'");
145 }
146
147
148
149
150 @Test
151 @Alerts("RangeError")
152 public void sliceInvalidStartIndexTrue() throws Exception {
153 sliceInvalidIndex("true");
154 }
155
156
157
158
159 @Test
160 @Alerts("0")
161 public void sliceInvalidStartIndexPositiveInfinity() throws Exception {
162 sliceInvalidIndex("Number.POSITIVE_INFINITY");
163 }
164
165
166
167
168 @Test
169 @Alerts({"3", "0", "1234", "0"})
170 public void sliceInvalidStartIndexNegativeInfinity() throws Exception {
171 sliceInvalidIndex("Number.NEGATIVE_INFINITY");
172 }
173
174
175
176
177 @Test
178 @Alerts({"3", "0", "1234", "0"})
179 public void sliceInvalidStartIndexNaN() throws Exception {
180 sliceInvalidIndex("NaN");
181 }
182
183
184
185
186 @Test
187 @Alerts({"3", "0", "1234", "0"})
188 public void sliceInvalidStartIndexNull() throws Exception {
189 sliceInvalidIndex("null");
190 }
191
192
193
194
195 @Test
196 @Alerts("0")
197 public void sliceInvalidEndIndexNumberString() throws Exception {
198 sliceInvalidIndex("4, '4'");
199 }
200
201
202
203
204 @Test
205 @Alerts("0")
206 public void sliceInvalidEndIndexString() throws Exception {
207 sliceInvalidIndex("4, 'four'");
208 }
209
210
211
212
213 @Test
214 @Alerts("0")
215 public void sliceInvalidEndIndexTrue() throws Exception {
216 sliceInvalidIndex("4, true");
217 }
218
219
220
221
222 @Test
223 @Alerts("0")
224 public void sliceInvalidEndIndexNaN() throws Exception {
225 sliceInvalidIndex("4, NaN");
226 }
227
228
229
230
231 @Test
232 @Alerts("0")
233 public void sliceInvalidEndIndexNull() throws Exception {
234 sliceInvalidIndex("4, null");
235 }
236
237 private void sliceInvalidIndex(final String index) throws Exception {
238 final String html = DOCTYPE_HTML
239 + "<html><head>\n"
240 + "<script>\n"
241 + LOG_TITLE_FUNCTION
242 + "function test() {\n"
243 + " try {\n"
244 + " var buffer = new ArrayBuffer(12);\n"
245 + " var x = new Int32Array(buffer);\n"
246 + " x[1] = 1234;\n"
247 + " var slice = buffer.slice(" + index + ");\n"
248 + " var y = new Int32Array(slice);\n"
249 + " log(y.length);\n"
250 + " for(var i = 0; i < y.length; i++) {\n"
251 + " log(y[i]);\n"
252 + " }\n"
253 + " } catch(e) {logEx(e);}\n"
254 + "}\n"
255 + "</script></head><body onload='test()'>\n"
256 + "</body></html>";
257
258 loadPageVerifyTitle2(html);
259 }
260
261
262
263
264 @Test
265 @Alerts("0")
266 public void nullConstructor() throws Exception {
267 final String html = DOCTYPE_HTML
268 + "<html><head>\n"
269 + "<script>\n"
270 + LOG_TITLE_FUNCTION
271 + "function test() {\n"
272 + " var array = new ArrayBuffer(null);\n"
273 + " log(array.byteLength);\n"
274 + "}\n"
275 + "</script></head><body onload='test()'>\n"
276 + "</body></html>";
277
278 loadPageVerifyTitle2(html);
279 }
280
281 }