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 ArrayBufferView.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class ArrayBufferViewTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts({"18", "93", "42"})
35      public void set_int8() 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 array = new Int8Array(10);\n"
42              + "  array.set(new Int8Array([18, 93, 42]), 3);\n"
43              + "  log(array[3]);\n"
44              + "  log(array[4]);\n"
45              + "  log(array[5]);\n"
46              + "}\n"
47              + "</script></head><body onload='test()'>\n"
48              + "</body></html>";
49  
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts("10")
58      public void set_empty() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><head>\n"
61              + "<script>\n"
62              + LOG_TITLE_FUNCTION
63              + "function test() {\n"
64              + "  var array = new Int8Array(10);\n"
65              + "  array.set({});\n"
66              + "  log(array.length);\n"
67              + "}\n"
68              + "</script></head><body onload='test()'>\n"
69              + "</body></html>";
70  
71          loadPageVerifyTitle2(html);
72      }
73  
74      /**
75       * @throws Exception if the test fails
76       */
77      @Test
78      @Alerts({"3", "2", "3", "-1"})
79      public void subarray_int8() throws Exception {
80          final String html = DOCTYPE_HTML
81              + "<html><head>\n"
82              + "<script>\n"
83              + LOG_TITLE_FUNCTION
84              + "function test() {\n"
85              + "  var x = new Int8Array([0, 1, 2, 3, 4, 5]);\n"
86              + "  var y = x.subarray(2, 5);\n"
87              + "  log(y.length);\n"
88              + "  log(y[0]);\n"
89              + "  log(y[1]);\n"
90              + "  y[0] = -1;\n"
91              + "  log(x[2]);\n"
92              + "}\n"
93              + "</script></head><body onload='test()'>\n"
94              + "</body></html>";
95  
96          loadPageVerifyTitle2(html);
97      }
98  
99      /**
100      * @throws Exception if the test fails
101      */
102     @Test
103     @Alerts({"8", "0", "0", "0", "10", "1", "2", "2", "2"})
104     public void ctorInvalidValuesInt() throws Exception {
105         final String html = DOCTYPE_HTML
106             + "<html><head>\n"
107             + "<script>\n"
108             + LOG_TITLE_FUNCTION
109             + "function test() {\n"
110             + "  var x = new Int8Array([null, 'null', undefined, '10', true, 2.4, 2.5, '2.6']);\n"
111             + "  log(x.length);\n"
112             + "  for(var i = 0; i < x.length; i++) {\n"
113             + "    log(x[i]);\n"
114             + "  }\n"
115             + "}\n"
116             + "</script></head><body onload='test()'>\n"
117             + "</body></html>";
118 
119         loadPageVerifyTitle2(html);
120     }
121 
122     /**
123      * @throws Exception if the test fails
124      */
125     @Test
126     @Alerts({"7", "0", "NaN", "NaN", "10", "1", "2.5", "2.75"})
127     public void ctorInvalidValuesFloat() throws Exception {
128         final String html = DOCTYPE_HTML
129             + "<html><head>\n"
130             + "<script>\n"
131             + LOG_TITLE_FUNCTION
132             + "function test() {\n"
133             + "  try {\n"
134             + "    var x = new Float32Array([null, 'null', undefined, '10', true, 2.5, '2.75']);\n"
135             + "    log(x.length);\n"
136             + "    for(var i = 0; i < x.length; i++) {\n"
137             + "      log(x[i]);\n"
138             + "    }\n"
139             + "  } catch(e) {logEx(e);}\n"
140             + "}\n"
141             + "</script></head><body onload='test()'>\n"
142             + "</body></html>";
143 
144         loadPageVerifyTitle2(html);
145     }
146 }