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;
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 the various TypedArray's.
23   *
24   * @author Ronald Brill
25   */
26  public class NativeTypedArrayTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"Float32Array", "false", "true",
33               "undefined", "false", "true",
34               "undefined", "true", "true",
35               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
36      public void toStringFloat32Array() throws Exception {
37          loadPageVerifyTitle2(toStringTagTest("new Float32Array(1)"));
38      }
39  
40      /**
41       * @throws Exception if the test fails
42       */
43      @Test
44      @Alerts({"Float64Array", "false", "true",
45               "undefined", "false", "true",
46               "undefined", "true", "true",
47               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
48      public void toStringFloat64Array() throws Exception {
49          loadPageVerifyTitle2(toStringTagTest("new Float64Array(1)"));
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      @Alerts({"Int8Array", "false", "true",
57               "undefined", "false", "true",
58               "undefined", "true", "true",
59               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
60      public void toStringInt8Array() throws Exception {
61          loadPageVerifyTitle2(toStringTagTest("new Int8Array(1)"));
62      }
63  
64      /**
65       * @throws Exception if the test fails
66       */
67      @Test
68      @Alerts({"Int16Array", "false", "true",
69               "undefined", "false", "true",
70               "undefined", "true", "true",
71               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
72      public void toStringInt16Array() throws Exception {
73          loadPageVerifyTitle2(toStringTagTest("new Int16Array(1)"));
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      @Alerts({"Int32Array", "false", "true",
81               "undefined", "false", "true",
82               "undefined", "true", "true",
83               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
84      public void toStringInt32Array() throws Exception {
85          loadPageVerifyTitle2(toStringTagTest("new Int32Array(1)"));
86      }
87  
88      /**
89       * @throws Exception if the test fails
90       */
91      @Test
92      @Alerts({"Uint8Array", "false", "true",
93               "undefined", "false", "true",
94               "undefined", "true", "true",
95               "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
96      public void toStringUint8Array() throws Exception {
97          loadPageVerifyTitle2(toStringTagTest("new Uint8Array(1)"));
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts({"Uint16Array", "false", "true",
105              "undefined", "false", "true",
106              "undefined", "true", "true",
107              "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
108     public void toStringUint16Array() throws Exception {
109         loadPageVerifyTitle2(toStringTagTest("new Uint16Array(1)"));
110     }
111 
112     /**
113      * @throws Exception if the test fails
114      */
115     @Test
116     @Alerts({"Uint32Array", "false", "true",
117              "undefined", "false", "true",
118              "undefined", "true", "true",
119              "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
120     public void toStringUint32Array() throws Exception {
121         loadPageVerifyTitle2(toStringTagTest("new Uint32Array(1)"));
122     }
123 
124     /**
125      * @throws Exception if the test fails
126      */
127     @Test
128     @Alerts({"Uint8ClampedArray", "false", "true",
129              "undefined", "false", "true",
130              "undefined", "true", "true",
131              "Symbol(Symbol.iterator),Symbol(Symbol.toStringTag)"})
132     public void toStringUint8ClampedArray() throws Exception {
133         loadPageVerifyTitle2(toStringTagTest("new Uint8ClampedArray(1)"));
134     }
135 
136     private static String toStringTagTest(final String init) {
137         return
138             DOCTYPE_HTML
139             + "<html></head>\n"
140             + "<body>"
141             + "<script>\n"
142             + LOG_TITLE_FUNCTION
143             + "  function sortFunction(s1, s2) {\n"
144             + "    var s1lc = s1.toLowerCase();\n"
145             + "    var s2lc =  s2.toLowerCase();\n"
146             + "    if (s1lc > s2lc) { return 1; }\n"
147             + "    if (s1lc < s2lc) { return -1; }\n"
148             + "    return s1 > s2 ? 1 : -1;\n"
149             + "  }\n"
150 
151             + "  var typedArray = " + init + ";\n"
152             + "  log((typedArray)[Symbol.toStringTag]);\n"
153             + "  log(typedArray.hasOwnProperty(Symbol.toStringTag));\n"
154             + "  log(Symbol.toStringTag in typedArray);\n"
155 
156             + "  var typedArrayProto = typedArray.__proto__;"
157             + "  log((typedArrayProto)[Symbol.toStringTag]);\n"
158             + "  log(typedArrayProto.hasOwnProperty(Symbol.toStringTag));\n"
159             + "  log(Symbol.toStringTag in typedArrayProto);\n"
160 
161             + "  var typedArrayProtoProto = typedArray.__proto__.__proto__;"
162             + "  log((typedArrayProtoProto)[Symbol.toStringTag]);\n"
163             + "  log(typedArrayProtoProto.hasOwnProperty(Symbol.toStringTag));\n"
164             + "  log(Symbol.toStringTag in typedArrayProtoProto);\n"
165 
166             + "  var syms = Object.getOwnPropertySymbols(typedArrayProtoProto);\n"
167             + "  var symStrs = [];"
168             + "  for (i = 0; i < syms.length; i++) {\n"
169             + "    symStrs.push(syms[i].toString());\n"
170             + "  }\n"
171             + "  symStrs.sort(sortFunction);\n"
172             + "  log(symStrs);"
173             + "</script>\n"
174             + "</body></html>";
175     }
176 }