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