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 Uint8ClampedArray.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   * @author Michael Rimov
28   */
29  public class Uint8ClampedArrayTest extends WebDriverTestCase {
30  
31      /**
32       * @throws Exception if the test fails
33       */
34      @Test
35      @Alerts({"0", "93", "255", "0", "93", "-1"})
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 Uint8ClampedArray([-17, 93, 350]);\n"
44              + "    for (var i = 0; i < array.length; i++)\n"
45              + "      log(array[i]);\n"
46              + "    var array2 = new Int8Array(array.buffer);\n"
47              + "    for (var i = 0; i < array2.length; i++)\n"
48              + "      log(array2[i]);\n"
49              + "  } catch(e) {logEx(e);}\n"
50              + "}\n"
51              + "</script></head><body onload='test()'>\n"
52              + "</body></html>";
53  
54          loadPageVerifyTitle2(html);
55      }
56  
57      /**
58       * @throws Exception if the test fails
59       */
60      @Test
61      @Alerts({"undefined", "11", "undefined", "undefined"})
62      public void index() throws Exception {
63          final String html = DOCTYPE_HTML
64              + "<html><head>\n"
65              + "<script>\n"
66              + LOG_TITLE_FUNCTION
67              + "function test() {\n"
68              + "  var array = new Uint8ClampedArray([11]);\n"
69              + "  log(array[-1]);\n"
70              + "  log(array[0]);\n"
71              + "  log(array[1]);\n"
72              + "  log(array[21]);\n"
73              + "}\n"
74              + "</script></head><body onload='test()'>\n"
75              + "</body></html>";
76  
77          loadPageVerifyTitle2(html);
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts({"false", "true", "false", "false"})
85      public void in() throws Exception {
86          final String html = DOCTYPE_HTML
87              + "<html><head>\n"
88              + "<script>\n"
89              + LOG_TITLE_FUNCTION
90              + "function test() {\n"
91              + "  var array = new Uint8ClampedArray([11]);\n"
92              + "  log(-1 in array);\n"
93              + "  log(0 in array);\n"
94              + "  log(1 in array);\n"
95              + "  log(42 in array);\n"
96              + "}\n"
97              + "</script></head><body onload='test()'>\n"
98              + "</body></html>";
99  
100         loadPageVerifyTitle2(html);
101     }
102 
103     /**
104      * @throws Exception if the test fails
105      */
106     @Test
107     @Alerts({"undefined", "6", "0", "0", "0", "255", "0", "4", "undefined"})
108     public void undefinedValueInArray() throws Exception {
109         final String html = DOCTYPE_HTML
110             + "<html><head>\n"
111             + "<script>\n"
112             + LOG_TITLE_FUNCTION
113             + "function test() {\n"
114             + "  var array = [];\n"
115             + "  array[1] = null;\n"
116             + "  array[2] = Number.NaN;\n"
117             + "  array[3] = Number.POSITIVE_INFINITY;\n"
118             + "  array[4] = Number.NEGATIVE_INFINITY;\n"
119             + "  array[5] = 4;\n"
120             + "  log(array[0]);\n"
121 
122             + "  var nativeArray = new Uint8ClampedArray(array);\n"
123             + "  log(nativeArray.length);\n"
124             + "  log(nativeArray[0]);\n"
125             + "  log(nativeArray[1]);\n"
126             + "  log(nativeArray[2]);\n"
127             + "  log(nativeArray[3]);\n"
128             + "  log(nativeArray[4]);\n"
129             + "  log(nativeArray[5]);\n"
130             + "  log(nativeArray[6]);\n"
131             + "}\n"
132             + "</script></head><body onload='test()'>\n"
133             + "</body></html>";
134 
135         loadPageVerifyTitle2(html);
136     }
137 
138     /**
139      * @throws Exception if the test fails
140      */
141     @Test
142     @Alerts({"0", "1", "0", "17"})
143     public void specialValueInArray() throws Exception {
144         final String html = DOCTYPE_HTML
145             + "<html><head>\n"
146             + "<script>\n"
147             + LOG_TITLE_FUNCTION
148             + "function test() {\n"
149             + "  var array = [];\n"
150             + "  array[0] = NaN;\n"
151             + "  array[1] = true;\n"
152             + "  array[2] = false;\n"
153             + "  array[3] = '17';\n"
154             + "  var nativeArray = new Uint8ClampedArray(array);\n"
155             + "  log(nativeArray[0]);\n"
156             + "  log(nativeArray[1]);\n"
157             + "  log(nativeArray[2]);\n"
158             + "  log(nativeArray[3]);\n"
159             + "}\n"
160             + "</script></head><body onload='test()'>\n"
161             + "</body></html>";
162 
163         loadPageVerifyTitle2(html);
164     }
165 
166     /**
167      * @throws Exception if the test fails
168      */
169     @Test
170     @Alerts("0")
171     public void nullConstructor() throws Exception {
172         final String html = DOCTYPE_HTML
173             + "<html><head>\n"
174             + "<script>\n"
175             + LOG_TITLE_FUNCTION
176             + "function test() {\n"
177             + "  try {\n"
178             + "    var array = new Uint8ClampedArray(null);\n"
179             + "    log(array.length);\n"
180             + "  } catch(e) {logEx(e);}\n"
181             + "}\n"
182             + "</script></head><body onload='test()'>\n"
183             + "</body></html>";
184 
185         loadPageVerifyTitle2(html);
186     }
187 
188     /**
189      * @throws Exception if the test fails
190      */
191     @Test
192     @Alerts({"", "0", "1", "1,3", "1,3,4,7,11,0,123"})
193     public void asString() throws Exception {
194         final String html = DOCTYPE_HTML
195             + "<html><head>\n"
196             + "<script>\n"
197             + LOG_TITLE_FUNCTION
198             + "function test() {\n"
199             + "  var array = new Uint8ClampedArray(0);\n"
200             + "  log(array.toString());\n"
201 
202             + "  array = new Uint8ClampedArray(1);\n"
203             + "  log(array.toString());\n"
204 
205             + "  array = new Uint8ClampedArray([1]);\n"
206             + "  log(array.toString());\n"
207 
208             + "  array = new Uint8ClampedArray([1,3]);\n"
209             + "  log(array.toString());\n"
210 
211             + "  array = new Uint8ClampedArray([1,3,4,7,11,0,123]);\n"
212             + "  log(array.toString());\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("Uint8ClampedArray")
225     public void name() throws Exception {
226         final String html = DOCTYPE_HTML
227             + "<html><head>\n"
228             + "<script>\n"
229             + LOG_TITLE_FUNCTION
230             + "function test() {\n"
231             + "  log(Uint8ClampedArray.name);\n"
232             + "}\n"
233             + "</script></head><body onload='test()'>\n"
234             + "</body></html>";
235 
236         loadPageVerifyTitle2(html);
237     }
238 }