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