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