View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Tests for ArrayBuffer.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class ArrayBufferTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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       * @throws Exception if the test fails
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      * @throws Exception if the test fails
122      */
123     @Test
124     @Alerts({"2", "1234", "0"})
125     public void sliceInvalidStartIndexNumberString() throws Exception {
126         sliceInvalidIndex("'4'");
127     }
128 
129     /**
130      * @throws Exception if the test fails
131      */
132     @Test
133     @Alerts("RangeError")
134     public void sliceInvalidStartIndexDouble() throws Exception {
135         sliceInvalidIndex("2.14");
136     }
137 
138     /**
139      * @throws Exception if the test fails
140      */
141     @Test
142     @Alerts({"3", "0", "1234", "0"})
143     public void sliceInvalidStartIndexString() throws Exception {
144         sliceInvalidIndex("'four'");
145     }
146 
147     /**
148      * @throws Exception if the test fails
149      */
150     @Test
151     @Alerts("RangeError")
152     public void sliceInvalidStartIndexTrue() throws Exception {
153         sliceInvalidIndex("true");
154     }
155 
156     /**
157      * @throws Exception if the test fails
158      */
159     @Test
160     @Alerts("0")
161     public void sliceInvalidStartIndexPositiveInfinity() throws Exception {
162         sliceInvalidIndex("Number.POSITIVE_INFINITY");
163     }
164 
165     /**
166      * @throws Exception if the test fails
167      */
168     @Test
169     @Alerts({"3", "0", "1234", "0"})
170     public void sliceInvalidStartIndexNegativeInfinity() throws Exception {
171         sliceInvalidIndex("Number.NEGATIVE_INFINITY");
172     }
173 
174     /**
175      * @throws Exception if the test fails
176      */
177     @Test
178     @Alerts({"3", "0", "1234", "0"})
179     public void sliceInvalidStartIndexNaN() throws Exception {
180         sliceInvalidIndex("NaN");
181     }
182 
183     /**
184      * @throws Exception if the test fails
185      */
186     @Test
187     @Alerts({"3", "0", "1234", "0"})
188     public void sliceInvalidStartIndexNull() throws Exception {
189         sliceInvalidIndex("null");
190     }
191 
192     /**
193      * @throws Exception if the test fails
194      */
195     @Test
196     @Alerts("0")
197     public void sliceInvalidEndIndexNumberString() throws Exception {
198         sliceInvalidIndex("4, '4'");
199     }
200 
201     /**
202      * @throws Exception if the test fails
203      */
204     @Test
205     @Alerts("0")
206     public void sliceInvalidEndIndexString() throws Exception {
207         sliceInvalidIndex("4, 'four'");
208     }
209 
210     /**
211      * @throws Exception if the test fails
212      */
213     @Test
214     @Alerts("0")
215     public void sliceInvalidEndIndexTrue() throws Exception {
216         sliceInvalidIndex("4, true");
217     }
218 
219     /**
220      * @throws Exception if the test fails
221      */
222     @Test
223     @Alerts("0")
224     public void sliceInvalidEndIndexNaN() throws Exception {
225         sliceInvalidIndex("4, NaN");
226     }
227 
228     /**
229      * @throws Exception if the test fails
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      * @throws Exception if the test fails
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 }