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.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for ArrayBufferView.
25   *
26   * @author Ahmed Ashour
27   * @author Frank Danek
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class ArrayBufferViewTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"18", "93", "42"})
38      public void set_int8() 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 array = new Int8Array(10);\n"
45              + "  array.set(new Int8Array([18, 93, 42]), 3);\n"
46              + "  log(array[3]);\n"
47              + "  log(array[4]);\n"
48              + "  log(array[5]);\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("10")
61      public void set_empty() 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 Int8Array(10);\n"
68              + "  array.set({});\n"
69              + "  log(array.length);\n"
70              + "}\n"
71              + "</script></head><body onload='test()'>\n"
72              + "</body></html>";
73  
74          loadPageVerifyTitle2(html);
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      @Alerts({"3", "2", "3", "-1"})
82      public void subarray_int8() throws Exception {
83          final String html = DOCTYPE_HTML
84              + "<html><head>\n"
85              + "<script>\n"
86              + LOG_TITLE_FUNCTION
87              + "function test() {\n"
88              + "  var x = new Int8Array([0, 1, 2, 3, 4, 5]);\n"
89              + "  var y = x.subarray(2, 5);\n"
90              + "  log(y.length);\n"
91              + "  log(y[0]);\n"
92              + "  log(y[1]);\n"
93              + "  y[0] = -1;\n"
94              + "  log(x[2]);\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({"8", "0", "0", "0", "10", "1", "2", "2", "2"})
107     public void ctorInvalidValuesInt() 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 x = new Int8Array([null, 'null', undefined, '10', true, 2.4, 2.5, '2.6']);\n"
114             + "  log(x.length);\n"
115             + "  for(var i = 0; i < x.length; i++) {\n"
116             + "    log(x[i]);\n"
117             + "  }\n"
118             + "}\n"
119             + "</script></head><body onload='test()'>\n"
120             + "</body></html>";
121 
122         loadPageVerifyTitle2(html);
123     }
124 
125     /**
126      * @throws Exception if the test fails
127      */
128     @Test
129     @Alerts({"7", "0", "NaN", "NaN", "10", "1", "2.5", "2.75"})
130     public void ctorInvalidValuesFloat() throws Exception {
131         final String html = DOCTYPE_HTML
132             + "<html><head>\n"
133             + "<script>\n"
134             + LOG_TITLE_FUNCTION
135             + "function test() {\n"
136             + "  try {\n"
137             + "    var x = new Float32Array([null, 'null', undefined, '10', true, 2.5, '2.75']);\n"
138             + "    log(x.length);\n"
139             + "    for(var i = 0; i < x.length; i++) {\n"
140             + "      log(x[i]);\n"
141             + "    }\n"
142             + "  } catch(e) {logEx(e);}\n"
143             + "}\n"
144             + "</script></head><body onload='test()'>\n"
145             + "</body></html>";
146 
147         loadPageVerifyTitle2(html);
148     }
149 }