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