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